From c241f071d26823ce2b6140c2687dcb750f6dfc29 Mon Sep 17 00:00:00 2001 From: Vincent Richard Date: Thu, 20 Oct 2005 16:56:04 +0000 Subject: Added flush() on 'outputStream' + added unit tests for 'charsetFilteredOutputStream' when input contains invalid sequences. --- src/charsetConverter.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'src/charsetConverter.cpp') diff --git a/src/charsetConverter.cpp b/src/charsetConverter.cpp index c6a77349..04d01db0 100644 --- a/src/charsetConverter.cpp +++ b/src/charsetConverter.cpp @@ -162,6 +162,8 @@ void charsetConverter::convert(const string& in, string& out) utility::outputStreamStringAdapter os(out); convert(is, os); + + os.flush(); } @@ -327,6 +329,60 @@ void charsetFilteredOutputStream::write } +void charsetFilteredOutputStream::flush() +{ + if (m_desc == NULL) + throw exceptions::charset_conv_error("Cannot initialize converter."); + + const iconv_t cd = *static_cast (m_desc); + + size_t offset = 0; + + // Process unconverted bytes + while (m_unconvCount != 0) + { + // Try a conversion + const char* inPtr = m_unconvBuffer + offset; + size_t inLength = m_unconvCount; + char* outPtr = m_outputBuffer; + size_t outLength = sizeof(m_outputBuffer); + + const size_t inLength0 = inLength; + + if (iconv(cd, ICONV_HACK(&inPtr), &inLength, &outPtr, &outLength) == static_cast (-1)) + { + const size_t inputConverted = inLength0 - inLength; + + // Skip a "blocking" character + if (inputConverted == 0) + { + m_stream.write("?", 1); + + offset++; + m_unconvCount--; + } + else + { + // Write successfully converted bytes + m_stream.write(m_outputBuffer, sizeof(m_outputBuffer) - outLength); + + offset += inputConverted; + m_unconvCount -= inputConverted; + } + } + else + { + // Write successfully converted bytes + m_stream.write(m_outputBuffer, sizeof(m_outputBuffer) - outLength); + + m_unconvCount = 0; + } + } + + m_stream.flush(); +} + + } // utility -- cgit