]> git.xonotic.org Git - xonotic/netradiant.git/blobdiff - libs/stream/textstream.h
picomodel: white default color of fm, md2 (was one white and rest black)
[xonotic/netradiant.git] / libs / stream / textstream.h
index e8de1391075c9c96337a91a1de0902d6ff9cb553..25f3d1ad42d90b12db37b2cb395f8011da5e1aa0 100644 (file)
 #if !defined( INCLUDED_STREAM_TEXTSTREAM_H )
 #define INCLUDED_STREAM_TEXTSTREAM_H
 
+#include "globaldefs.h"
+
 /// \file
 /// \brief Text-output-formatting.
 
 #include "itextstream.h"
+#include "string/string.h"
 
 #include <cctype>
 #include <cstddef>
@@ -33,6 +36,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <algorithm>
+#include <string>
 
 #include "generic/arrayrange.h"
 
@@ -46,7 +50,7 @@ inline char* write_unsigned_nonzero_decimal_backward( char* ptr, unsigned int de
        return ptr;
 }
 
-  #if defined ( _WIN64 ) || defined ( __LP64__ )
+  #if GDEF_ARCH_BITS_64
 inline char* write_size_t_nonzero_decimal_backward( char* ptr, size_t decimal ){
        for (; decimal != 0; decimal /= 10 )
        {
@@ -76,7 +80,7 @@ inline char* write_unsigned_nonzero_decimal_backward( char* ptr, unsigned int de
        return ptr;
 }
 
-  #if defined ( _WIN64 ) || defined ( __LP64__ )
+  #if GDEF_ARCH_BITS_64
 inline char* write_size_t_nonzero_decimal_backward( char* ptr, size_t decimal, bool show_positive ){
        ptr = write_size_t_nonzero_decimal_backward( ptr, decimal );
        if ( show_positive ) {
@@ -108,7 +112,7 @@ inline char* write_unsigned_decimal_backward( char* ptr, unsigned int decimal, b
        return ptr;
 }
 
-  #if defined ( _WIN64 ) || defined ( __LP64__ )
+  #if GDEF_ARCH_BITS_64
 inline char* write_size_t_decimal_backward( char* ptr, size_t decimal, bool show_positive ){
        if ( decimal == 0 ) {
                *--ptr = '0';
@@ -123,7 +127,7 @@ inline char* write_size_t_decimal_backward( char* ptr, size_t decimal, bool show
 }
 
 
-#ifdef WIN32
+#if GDEF_OS_WINDOWS
 #define snprintf _snprintf
 #endif
 
@@ -184,7 +188,7 @@ inline TextOutputStreamType& ostream_write( TextOutputStreamType& ostream, const
        return ostream;
 }
 
-#if defined ( _WIN64 ) || defined ( __LP64__ )
+#if GDEF_ARCH_BITS_64
 
 /// \brief Writes a size_t \p i to \p ostream in decimal form.
 template<typename TextOutputStreamType>
@@ -396,6 +400,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<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