summaryrefslogtreecommitdiff
path: root/source/core/text-io.h
blob: c914e340aa36b53f849faaf63252e27b789c60e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#ifndef CORE_LIB_TEXT_IO_H
#define CORE_LIB_TEXT_IO_H

#include "secure-crt.h"
#include "stream.h"

namespace Slang
{
	using Slang::List;
	using Slang::_EndLine;

	class TextReader
	{
	protected:
		char decodedChar[5];
		int decodedCharPtr = 0, decodedCharSize = 0;
		virtual void ReadChar() = 0;
	public:
		virtual ~TextReader()
		{
			Close();
		}
		virtual void Close(){}
		virtual String ReadLine()=0;
		virtual String ReadToEnd()=0;
		virtual bool IsEnd() = 0;
		int Read(char * buffer, int count);
		char Read()
		{
			if (decodedCharPtr == decodedCharSize)
				ReadChar();
			if (decodedCharPtr < decodedCharSize)
				return decodedChar[decodedCharPtr++];
			else
				return 0;
		}
		char Peak()
		{
			if (decodedCharPtr == decodedCharSize)
				ReadChar();
			if (decodedCharPtr < decodedCharSize)
				return decodedChar[decodedCharPtr];
			else
				return 0;
		}
	};

	class TextWriter
	{
	public:
		virtual ~TextWriter()
		{
			Close();
		}
		virtual void Write(const String & str)=0;
		virtual void Write(const char * str)=0;
		virtual void Close(){}
		template<typename T>
		TextWriter & operator << (const T& val)
		{
			Write(val.ToString());
			return *this;
		}
		TextWriter & operator << (int value)
		{
			Write(String(value));
			return *this;
		}
		TextWriter & operator << (float value)
		{
			Write(String(value));
			return *this;
		}
		TextWriter & operator << (double value)
		{
			Write(String(value));
			return *this;
		}
		TextWriter & operator << (const char* value)
		{
			Write(value);
			return *this;
		}
		TextWriter & operator << (const String & val)
		{
			Write(val);
			return *this;
		}
		TextWriter & operator << (const _EndLine &)
		{
#ifdef _WIN32
			Write("\r\n");
#else
			Write("\n");
#endif
			return *this;
		}
	};

	template <typename ReadCharFunc>
	int GetUnicodePointFromUTF8(const ReadCharFunc & get)
	{
		int codePoint = 0;
		int leading = get(0);
		int mask = 0x80;
		int count = 0;
		while (leading & mask)
		{
			count++;
			mask >>= 1;
		}
		codePoint = (leading & (mask - 1));
		for (int i = 1; i <= count - 1; i++)
		{
			codePoint <<= 6;
			codePoint += (get(i) & 0x3F);
		}
		return codePoint;
	}

	template <typename ReadCharFunc>
	int GetUnicodePointFromUTF16(const ReadCharFunc & get)
	{
		int byte0 = (unsigned char)get(0);
		int byte1 = (unsigned char)get(1);
		int word0 = byte0 + (byte1 << 8);
		if (word0 >= 0xD800 && word0 <= 0xDFFF)
		{
			int byte2 = (unsigned char)get(2);
			int byte3 = (unsigned char)get(3);
			int word1 = byte2 + (byte3 << 8);
			return ((word0 & 0x3FF) << 10) + (word1 & 0x3FF) + 0x10000;
		}
		else
			return word0;
	}

	template <typename ReadCharFunc>
	int GetUnicodePointFromUTF16Reversed(const ReadCharFunc & get)
	{
		int byte0 = (unsigned char)get(0);
		int byte1 = (unsigned char)get(1);
		int word0 = (byte0 << 8) + byte1;
		if (word0 >= 0xD800 && word0 <= 0xDFFF)
		{
			int byte2 = (unsigned char)get(2);
			int byte3 = (unsigned char)get(3);
			int word1 = (byte2 << 8) + byte3;
			return ((word0 & 0x3FF) << 10) + (word1 & 0x3FF);
		}
		else
			return word0;
	}

	template <typename ReadCharFunc>
	int GetUnicodePointFromUTF32(const ReadCharFunc & get)
	{
		int byte0 = (unsigned char)get(0);
		int byte1 = (unsigned char)get(1);
		int byte2 = (unsigned char)get(2);
		int byte3 = (unsigned char)get(3);
		return byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
	}

	inline int EncodeUnicodePointToUTF8(char * buffer, int codePoint)
	{
		int count = 0;
		if (codePoint <= 0x7F)
			buffer[count++] = ((char)codePoint);
		else if (codePoint <= 0x7FF)
		{
			unsigned char byte = (unsigned char)(0xC0 + (codePoint >> 6));
			buffer[count++] = ((char)byte);
			byte = 0x80 + (codePoint & 0x3F);
			buffer[count++] = ((char)byte);
		}
		else if (codePoint <= 0xFFFF)
		{
			unsigned char byte = (unsigned char)(0xE0 + (codePoint >> 12));
			buffer[count++] = ((char)byte);
			byte = (unsigned char)(0x80 + ((codePoint >> 6) & (0x3F)));
			buffer[count++] = ((char)byte);
			byte = (unsigned char)(0x80 + (codePoint & 0x3F));
			buffer[count++] = ((char)byte);
		}
		else
		{
			unsigned char byte = (unsigned char)(0xF0 + (codePoint >> 18));
			buffer[count++] = ((char)byte);
			byte = (unsigned char)(0x80 + ((codePoint >> 12) & 0x3F));
			buffer[count++] = ((char)byte);
			byte = (unsigned char)(0x80 + ((codePoint >> 6) & 0x3F));
			buffer[count++] = ((char)byte);
			byte = (unsigned char)(0x80 + (codePoint & 0x3F));
			buffer[count++] = ((char)byte);
		}
		return count;
	}

	inline int EncodeUnicodePointToUTF16(unsigned short * buffer, int codePoint)
	{
		int count = 0;
		if (codePoint <= 0xD7FF || (codePoint >= 0xE000 && codePoint <= 0xFFFF))
			buffer[count++] = (unsigned short)codePoint;
		else
		{
			int sub = codePoint - 0x10000;
			int high = (sub >> 10) + 0xD800;
			int low = (sub & 0x3FF) + 0xDC00;
			buffer[count++] = (unsigned short)high;
			buffer[count++] = (unsigned short)low;
		}
		return count;
	}

	inline unsigned short ReverseBitOrder(unsigned short val)
	{
		int byte0 = val & 0xFF;
		int byte1 = val >> 8;
		return (unsigned short)(byte1 + (byte0 << 8));
	}

	inline int EncodeUnicodePointToUTF16Reversed(unsigned short * buffer, int codePoint)
	{
		int count = 0;
		if (codePoint <= 0xD7FF || (codePoint >= 0xE000 && codePoint <= 0xFFFF))
			buffer[count++] = ReverseBitOrder((unsigned short)codePoint);
		else
		{
			int sub = codePoint - 0x10000;
			int high = (sub >> 10) + 0xD800;
			int low = (sub & 0x3FF) + 0xDC00;
			buffer[count++] = ReverseBitOrder((unsigned short)high);
			buffer[count++] = ReverseBitOrder((unsigned short)low);
		}
		return count;
	}

	class Encoding
	{
	public:
		static Encoding * UTF8, * UTF16, *UTF16Reversed, * UTF32;
		virtual void GetBytes(List<char>& buffer, const String & str) = 0;
		virtual String ToString(const char * buffer, int length) = 0;
		virtual ~Encoding()
		{}
	};

	class StreamWriter : public TextWriter
	{
	private:
		List<char> encodingBuffer;
		RefPtr<Stream> stream;
		Encoding * encoding;
	public:
		StreamWriter(const String & path, Encoding * encoding = Encoding::UTF8);
		StreamWriter(RefPtr<Stream> stream, Encoding * encoding = Encoding::UTF8);
		virtual void Write(const String & str);
		virtual void Write(const char * str);
		virtual void Close()
		{
			stream->Close();
		}
        void ReleaseStream()
        {
            stream = 0;
        }
	};

    class StreamReader : public TextReader
	{
	private:
		RefPtr<Stream> stream;
		List<char> buffer;
		Encoding * encoding;
		UInt ptr;
		char ReadBufferChar();
		void ReadBuffer();
			
		Encoding * DetermineEncoding();
	protected:
		virtual void ReadChar()
		{
			decodedCharPtr = 0;
			int codePoint = 0;
			if (encoding == Encoding::UTF8)
				codePoint = GetUnicodePointFromUTF8([&](int) {return ReadBufferChar(); });
			else if (encoding == Encoding::UTF16)
				codePoint = GetUnicodePointFromUTF16([&](int) {return ReadBufferChar(); });
			else if (encoding == Encoding::UTF16Reversed)
				codePoint = GetUnicodePointFromUTF16Reversed([&](int) {return ReadBufferChar(); });
			else if (encoding == Encoding::UTF32)
				codePoint = GetUnicodePointFromUTF32([&](int) {return ReadBufferChar(); });
			decodedCharSize = EncodeUnicodePointToUTF8(decodedChar, codePoint);
		}
	public:
		StreamReader(const String & path);
		StreamReader(RefPtr<Stream> stream, Encoding * encoding = nullptr);
		virtual String ReadLine();
		virtual String ReadToEnd();
		virtual bool IsEnd()
		{
			return ptr == buffer.Count() && stream->IsEnd();
		}
		virtual void Close()
		{
			stream->Close();
		}
        void ReleaseStream()
        {
            stream = 0;
        }
	};
}

#endif