yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
4c76b2759
master
1#ifndef SLANG_CORE_TEXT_IO_H 2#define SLANG_CORE_TEXT_IO_H 3 4#include "slang-char-encode.h" 5#include "slang-secure-crt.h" 6#include "slang-stream.h" 7 8namespace Slang 9{ 10using Slang ::List ; 11using Slang ::_EndLine ; 12 13class TextReader 14{ 15public : 16virtual void close () {} 17virtual SlangResult readToEnd (String & outString )= 0 ; 18virtual bool isEnd ()= 0 ; 19 20char read () 21 { 22if (m_decodedCharIndex == m_decodedCharSize ) 23readChar (); 24if (m_decodedCharIndex < m_decodedCharSize ) 25return m_decodedChar [m_decodedCharIndex ++ ]; 26else 27return 0 ; 28 } 29char peek () 30 { 31if (m_decodedCharIndex == m_decodedCharSize ) 32readChar (); 33if (m_decodedCharIndex < m_decodedCharSize ) 34return m_decodedChar [m_decodedCharIndex ]; 35else 36return 0 ; 37 } 38 39virtual ~TextReader () {close (); } 40 41protected : 42char m_decodedChar [5 ]; 43Index m_decodedCharIndex = 0 ; 44Index m_decodedCharSize = 0 ; 45 46virtual void readChar ()= 0 ; 47}; 48 49 50class StreamReader :public TextReader 51{ 52public : 53virtual SlangResult readToEnd (String & outString )SLANG_OVERRIDE ; 54virtual bool isEnd ()SLANG_OVERRIDE 55 { 56return m_index == m_buffer .getCount ()&& m_stream -> isEnd (); 57 } 58virtual void close ()SLANG_OVERRIDE {m_stream -> close (); } 59 60void releaseStream () {m_stream .setNull (); } 61 62StreamReader (); 63 64SlangResult init (const String & path ); 65SlangResult init (RefPtr < Stream > stream ,CharEncoding * encoding = nullptr ); 66 67protected : 68virtual void readChar ()SLANG_OVERRIDE 69 { 70m_decodedCharIndex = 0 ; 71 72Char32 codePoint = 0 ; 73switch (m_encodingType ) 74 { 75case CharEncodeType ::UTF8 : 76 { 77codePoint = getUnicodePointFromUTF8 ([& ]() -> Byte { return readBufferByte (); }); 78break ; 79} 80case CharEncodeType:: UTF16 : 81{ 82codePoint = getUnicodePointFromUTF16([ & ]() -> Byte { return readBufferByte (); }); 83break ; 84} 85case CharEncodeType:: UTF16Reversed : 86{ 87codePoint = 88getUnicodePointFromUTF16Reversed([ & ]() -> Byte { return readBufferByte (); }); 89break ; 90} 91case CharEncodeType:: UTF32 : 92{ 93codePoint = getUnicodePointFromUTF32([ & ]() -> Byte { return readBufferByte (); }); 94break ; 95} 96} 97 98m_decodedCharSize = encodeUnicodePointToUTF8 (codePoint, m_decodedChar); 99} 100 101private : 102Byte readBufferByte (); 103SlangResult readBuffer (); 104 105RefPtr < Stream > m_stream; 106List < Byte > m_buffer; 107 108CharEncodeType m_encodingType = CharEncodeType:: UTF8 ; 109CharEncoding * m_encoding = nullptr ; 110Index m_index = 0 ; ///< Index into buffer 111}; 112 113class TextWriter 114{ 115public : 116virtual SlangResult writeSlice( const UnownedStringSlice & slice) = 0 ; 117virtual void close () {} 118 119SlangResult write ( const UnownedStringSlice & slice) { return writeSlice (slice); } 120SlangResult write ( const char * str) { return writeSlice ( UnownedStringSlice (str)); } 121SlangResult write ( const String & str) { return writeSlice (str. getUnownedSlice ()); } 122 123virtual ~ TextWriter () { close (); } 124 125template < typename T > 126TextWriter & operator<<( const T & val) 127{ 128write (val. ToString ()); 129return * this; 130} 131TextWriter & operator<<( int value ) 132{ 133write ( String (value)); 134return * this; 135} 136TextWriter & operator<<( float value) 137{ 138write ( String (value)); 139return * this; 140} 141TextWriter & operator<<( double value) 142{ 143write ( String (value)); 144return * this; 145} 146TextWriter & operator<<( const char * value) 147{ 148writeSlice ( UnownedStringSlice (value)); 149return * this; 150} 151TextWriter & operator<<( const String & val) 152{ 153writeSlice (val. getUnownedSlice ()); 154return * this; 155} 156TextWriter & operator<<( const _EndLine & ) 157{ 158#ifdef _WIN32 159writeSlice (UnownedStringSlice:: fromLiteral ( "\r\n" )); 160#else 161writeSlice ( UnownedStringSlice :: fromLiteral ( "\n" )); 162#endif 163return * this; 164} 165}; 166 167class StreamWriter : public TextWriter 168{ 169public : 170// TextWriter 171virtual SlangResult writeSlice( const UnownedStringSlice & slice) SLANG_OVERRIDE ; 172virtual void close () SLANG_OVERRIDE { m_stream -> close (); } 173 174void releaseStream () { m_stream. setNull (); } 175 176StreamWriter () {} 177 178SlangResult init ( const String & path, CharEncoding * encoding = CharEncoding:: UTF8 ); 179SlangResult init ( RefPtr < Stream > stream, CharEncoding * encoding = CharEncoding:: UTF8 ); 180 181private : 182List < Byte > m_encodingBuffer; 183RefPtr < Stream > m_stream; 184CharEncoding * m_encoding = nullptr ; 185}; 186 187} // namespace Slang 188 189#endif