| 1 | #include <wchar.h> |
| 2 | #include "pt2_unicode.h" |
| 3 | |
| 4 | // this is probably broken, but it "works" for now |
| 5 | uint32_t unicharToAnsi(char *dstBuffer, const UNICHAR *inputString, uint32_t maxDstLen) |
| 6 | { |
| 7 | if (inputString == NULL || dstBuffer == NULL) |
| 8 | return 0; |
| 9 | |
| 10 | uint32_t i = 0; |
| 11 | while (i < maxDstLen && inputString[i] != '\0') |
| 12 | { |
| 13 | UNICHAR ch = inputString[i]; |
| 14 | #ifdef _WIN32 |
| 15 | if (ch >= 256) |
| 16 | #else |
| 17 | if ((uint8_t)ch > 127) |
| 18 | #endif |
| 19 | { |
| 20 | *dstBuffer++ = '?'; |
| 21 | |
| 22 | #ifdef _WIN32 |
| 23 | if ((ch & 0xFF00) == 0xD800) // lead surrogate, skip the next widechar (trail) |
| 24 | i++; |
| 25 | #endif |
| 26 | } |
| 27 | else |
| 28 | { |
| 29 | *dstBuffer = (char)ch; |
| 30 | if ((signed)*dstBuffer < ' ') |
| 31 | *dstBuffer = '?'; |
| 32 | |
| 33 | dstBuffer++; |
| 34 | } |
| 35 | |
| 36 | i++; |
| 37 | } |
| 38 | |
| 39 | if (maxDstLen > 1) |
| 40 | *dstBuffer = '\0'; |
| 41 | |
| 42 | return i; |
| 43 | } |