X-Git-Url: http://de.git.xonotic.org/?a=blobdiff_plain;f=libs%2Fstream%2Ftextstream.h;h=7f8be898c12bc503e8173cfe95ba4eeffd03ea8e;hb=9d606a0b3be053550486b8f1a00255e340ccf8a8;hp=e8de1391075c9c96337a91a1de0902d6ff9cb553;hpb=dac8329952745dbb494bad1c301e44bab05ec0db;p=xonotic%2Fnetradiant.git diff --git a/libs/stream/textstream.h b/libs/stream/textstream.h index e8de1391..7f8be898 100644 --- a/libs/stream/textstream.h +++ b/libs/stream/textstream.h @@ -26,6 +26,7 @@ /// \brief Text-output-formatting. #include "itextstream.h" +#include "string/string.h" #include #include @@ -33,6 +34,7 @@ #include #include #include +#include #include "generic/arrayrange.h" @@ -396,6 +398,48 @@ std::size_t write( const char* buffer, std::size_t length ){ } }; + +/// \brief A wrapper for a TextInputStream used for reading one text line at a time. +template +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 class BufferedTextOutputStream : public TextOutputStream