|
20 | 20 |
|
21 | 21 | #include "codepages/win1252toUtf32.h"
|
22 | 22 | #include <errno.h>
|
| 23 | +#include <libnex/base.h> |
23 | 24 | #include <libnex/bits.h>
|
24 | 25 | #include <libnex/safemalloc.h>
|
25 | 26 | #include <libnex/textstream.h>
|
@@ -175,7 +176,7 @@ PUBLIC void TextSetBufSz (TextStream_t* stream, size_t sz)
|
175 | 176 | }
|
176 | 177 |
|
177 | 178 | // Checks if we reached a newline
|
178 |
| -char _textCheckNewLine (wchar_t* buf, int i, size_t bytesLeft) |
| 179 | +char _textCheckNewLine (uint8_t* buf, int i, size_t bytesLeft) |
179 | 180 | {
|
180 | 181 | if (buf[i] == '\n' || buf[i] == '\r')
|
181 | 182 | {
|
@@ -203,7 +204,7 @@ ssize_t _textDecode (TextStream_t* stream, wchar_t* buf, size_t count, int termi
|
203 | 204 | // Check if a terminator was reached
|
204 | 205 | if (terminator == 1)
|
205 | 206 | {
|
206 |
| - char doWhat = _textCheckNewLine (buf, i, count); |
| 207 | + char doWhat = _textCheckNewLine (stream->buf, i, count); |
207 | 208 | if (doWhat == 0)
|
208 | 209 | continue;
|
209 | 210 | else if (doWhat == 1)
|
@@ -234,7 +235,7 @@ ssize_t _textDecode (TextStream_t* stream, wchar_t* buf, size_t count, int termi
|
234 | 235 | // Check if a terminator was reached
|
235 | 236 | if (terminator == 1)
|
236 | 237 | {
|
237 |
| - char doWhat = _textCheckNewLine (buf, i, count); |
| 238 | + char doWhat = _textCheckNewLine (stream->buf, i, count); |
238 | 239 | if (doWhat == 0)
|
239 | 240 | continue;
|
240 | 241 | else if (doWhat == 1)
|
@@ -264,8 +265,43 @@ ssize_t _textEncode (TextStream_t* stream, wchar_t* buf, size_t count)
|
264 | 265 | return -1;
|
265 | 266 | }
|
266 | 267 | stream->buf[i] = (uint8_t) buf[i];
|
267 |
| - charEncoded = (ssize_t) count; |
268 | 268 | }
|
| 269 | + charEncoded = (ssize_t) count; |
| 270 | + } |
| 271 | + else if (stream->encoding == TEXT_ENC_WIN1252) |
| 272 | + { |
| 273 | + // Loop and encode |
| 274 | + for (int i = 0; i < count; ++i) |
| 275 | + { |
| 276 | + // This is where the algorithm starts. Check if this character's Unicode code |
| 277 | + // is the same as its Windows-1252 one. If it is, directly copy to destination |
| 278 | + // buffer |
| 279 | + if (buf[i] <= 0x7F || (buf[i] >= 0xA0 && buf[i] <= 0xFF)) |
| 280 | + { |
| 281 | + // Copy out |
| 282 | + stream->buf[i] = (uint8_t) buf[i]; |
| 283 | + } |
| 284 | + // It's a Windows-1252 character |
| 285 | + else |
| 286 | + { |
| 287 | + // This is kind of slow, but the best way overall. |
| 288 | + // We loop through the translation table until we find character that matches |
| 289 | + // buf[i]. We set bit 7 on the index, and that's the character |
| 290 | + int tableSize = ARRAY_SIZE (win1252toUtf32); |
| 291 | + int tableIndex = 0; |
| 292 | + while (tableIndex < tableSize) |
| 293 | + { |
| 294 | + // Check for a match |
| 295 | + if (win1252toUtf32[tableIndex] == buf[i]) |
| 296 | + { |
| 297 | + // Set bit 7 on tableIndex, and that is the character |
| 298 | + stream->buf[i] = BitSetNew (tableIndex, 7); |
| 299 | + } |
| 300 | + tableIndex++; |
| 301 | + } |
| 302 | + } |
| 303 | + } |
| 304 | + charEncoded = (ssize_t) count; |
269 | 305 | }
|
270 | 306 | return charEncoded;
|
271 | 307 | }
|
@@ -303,7 +339,7 @@ PUBLIC ssize_t TextRead (TextStream_t* stream, wchar_t* buf, size_t count)
|
303 | 339 | // Read the data into the staging buffer
|
304 | 340 | ssize_t charRead = (ssize_t) fread (stream->buf, 1, count * sizeof (wchar_t), stream->file);
|
305 | 341 | // Decode the string
|
306 |
| - ssize_t charParsed = _textDecode (stream, buf, charRead, 0); |
| 342 | + ssize_t charParsed = _textDecode (stream, buf, charRead, TEXT_DECODE_ALL); |
307 | 343 | if (charParsed == -1)
|
308 | 344 | return -1;
|
309 | 345 | // That's it
|
@@ -345,7 +381,7 @@ PUBLIC ssize_t TextReadLine (TextStream_t* stream, wchar_t* buf, size_t count)
|
345 | 381 | // Read the data into the staging buffer
|
346 | 382 | ssize_t charRead = (ssize_t) fread (stream->buf, 1, count * sizeof (wchar_t), stream->file);
|
347 | 383 | // Decode the string
|
348 |
| - ssize_t charParsed = _textDecode (stream, buf, charRead, 1); |
| 384 | + ssize_t charParsed = _textDecode (stream, buf, charRead, TEXT_DECODE_TERMINATE_ON_NEWLINE); |
349 | 385 | // That's it
|
350 | 386 | TextUnlock (stream);
|
351 | 387 | return charParsed;
|
|
0 commit comments