yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Theresa FoleyCleanups related to RIFF support (#7041)4c76b2759

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