+SingleCharacterInputStream( TextInputStreamType& inputStream ) : m_inputStream( inputStream ), m_cur( m_buffer ), m_end( m_buffer ){
+}
+bool readChar( char& c ){
+ if ( m_cur == m_end && !fillBuffer() ) {
+ return false;
+ }
+
+ c = *m_cur++;
+ return true;
+}
+};
+
+/// \brief A wrapper for a TextOutputStream, optimised for writing a single character at a time.
+class SingleCharacterOutputStream : public TextOutputStream
+{
+enum unnamed0 { m_bufsize = 1024 };
+TextOutputStream& m_ostream;
+char m_buffer[m_bufsize];
+char* m_pos;
+const char* m_end;
+
+const char* end() const {
+ return m_end;
+}
+void reset(){
+ m_pos = m_buffer;
+}
+void flush(){
+ m_ostream.write( m_buffer, m_pos - m_buffer );
+ reset();
+}
+public:
+SingleCharacterOutputStream( TextOutputStream& ostream ) : m_ostream( ostream ), m_pos( m_buffer ), m_end( m_buffer + m_bufsize ){
+}
+~SingleCharacterOutputStream(){
+ flush();
+}
+void write( const char c ){
+ if ( m_pos == end() ) {
+ flush();
+ }
+ *m_pos++ = c;
+}
+std::size_t write( const char* buffer, std::size_t length ){
+ const char*const end = buffer + length;
+ for ( const char* p = buffer; p != end; ++p )
+ {
+ write( *p );
+ }
+ return length;
+}
+};
+
+
+/// \brief A wrapper for a TextInputStream used for reading one text line at a time.
+template<typename TextInputStreamType, int SIZE = 1024>
+class TextLinesInputStream
+{
+TextInputStreamType& m_inputStream;
+char m_buffer[SIZE + 1];
+char* m_cur;
+char* m_end;
+
+int fillBuffer(){
+ m_end = m_buffer + m_inputStream.read( m_buffer, SIZE );
+ m_cur = m_buffer;
+ m_buffer[SIZE] = '\0';
+ *m_end = '\0';
+ return m_end - m_cur;
+}
+public:
+
+TextLinesInputStream( TextInputStreamType& inputStream ) : m_inputStream( inputStream ), m_cur( m_buffer ), m_end( m_buffer ){
+ m_buffer[0] = '\0';
+}
+
+CopiedString readLine(){
+ std::string s;
+ char* m_fin;
+
+ while ( (m_fin = strchr( m_cur, '\n' )) == 0 )
+ {
+ s.append( m_cur, m_end - m_cur );
+ if ( fillBuffer() <= 0 ) break;
+ }
+ if ( m_fin != 0 ) {
+ s.append( m_cur, m_fin - m_cur + 1 );
+ m_cur = m_fin + 1;
+ }
+
+ return CopiedString( s.c_str() );
+}
+};
+
+
+/// \brief A wrapper for a TextOutputStream, optimised for writing a few characters at a time.
+template<typename TextOutputStreamType, int SIZE = 1024>
+class BufferedTextOutputStream : public TextOutputStream
+{
+TextOutputStreamType outputStream;
+char m_buffer[SIZE];
+char* m_cur;
+
+public:
+BufferedTextOutputStream( TextOutputStreamType& outputStream ) : outputStream( outputStream ), m_cur( m_buffer ){
+}
+~BufferedTextOutputStream(){
+ outputStream.write( m_buffer, m_cur - m_buffer );
+}
+std::size_t write( const char* buffer, std::size_t length ){
+ std::size_t remaining = length;
+ for (;; )
+ {
+ std::size_t n = std::min( remaining, std::size_t( ( m_buffer + SIZE ) - m_cur ) );
+ m_cur = std::copy( buffer, buffer + n, m_cur );
+ remaining -= n;
+ if ( remaining == 0 ) {
+ return 0;
+ }
+ outputStream.write( m_buffer, SIZE );
+ m_cur = m_buffer;
+ }
+}