3 static unsigned long cp;
8 Ensure we write in UTF-8 mode, so that non-ASCII characters don't get
9 mangled. If we were compiled in ANSI mode it won't work.
11 cp = GetConsoleOutputCP();
12 SetConsoleOutputCP(CP_UTF8);
13 _setmode(_fileno(stdout), _O_U8TEXT);
14 _setmode(_fileno(stderr), _O_U8TEXT);
19 if (cp) SetConsoleOutputCP(cp);
25 to_utf8/16() converts a string which may be either utf8 or utf16 to
26 the desired format. If no conversion is needed a new string is still
27 allocated and the old string is copied.
29 from_utf8/16() converts a string which IS in the specified format to
30 whichever format is needed according to whether UNICODE is defined.
31 It simply wraps the appropriate to_utf8/16() function.
33 Therefore the caller must ALWAYS free the destination pointer after a
34 successful (return code 0) call to one of these functions.
36 The length pointer is optional. Pass NULL if you don't care about
37 the length of the converted string.
39 Both the destination and the length, if supplied, will be zeroed if
40 no conversion was done.
42 int to_utf8(const wchar_t *utf16, char **utf8, unsigned long *utf8len) {
44 if (utf8len) *utf8len = 0;
45 int size = WideCharToMultiByte(CP_UTF8, 0, utf16, -1, NULL, 0, NULL, NULL);
48 *utf8 = (char *) HeapAlloc(GetProcessHeap(), 0, size);
49 if (! *utf8) return 2;
51 if (! WideCharToMultiByte(CP_UTF8, 0, utf16, -1, (LPSTR) utf8, size, NULL, NULL)) {
52 HeapFree(GetProcessHeap(), 0, *utf8);
57 if (utf8len) *utf8len = (unsigned long) strlen(*utf8);
62 int to_utf8(const char *ansi, char **utf8, unsigned long *utf8len) {
64 if (utf8len) *utf8len = 0;
65 size_t len = strlen(ansi);
66 int size = (int) len + 1;
68 *utf8 = (char *) HeapAlloc(GetProcessHeap(), 0, size);
69 if (! *utf8) return 2;
71 if (utf8len) *utf8len = (unsigned long) len;
72 memmove(*utf8, ansi, size);
77 int to_utf16(const char *utf8, wchar_t **utf16, unsigned long *utf16len) {
79 if (utf16len) *utf16len = 0;
80 int size = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
83 *utf16 = (wchar_t *) HeapAlloc(GetProcessHeap(), 0, size * sizeof(wchar_t));
84 if (! *utf16) return 2;
86 if (! MultiByteToWideChar(CP_UTF8, 0, utf8, -1, *utf16, size)) {
87 HeapFree(GetProcessHeap(), 0, *utf16);
92 if (utf16len) *utf16len = (unsigned long) wcslen(*utf16);
97 int to_utf16(const wchar_t *unicode, wchar_t **utf16, unsigned long *utf16len) {
99 if (utf16len) *utf16len = 0;
100 size_t len = wcslen(unicode);
101 int size = ((int) len + 1) * sizeof(wchar_t);
103 *utf16 = (wchar_t *) HeapAlloc(GetProcessHeap(), 0, size);
104 if (! *utf16) return 2;
106 if (utf16len) *utf16len = (unsigned long) len;
107 memmove(*utf16, unicode, size);
112 int from_utf8(const char *utf8, TCHAR **buffer, unsigned long *buflen) {
114 return to_utf16(utf8, buffer, buflen);
116 return to_utf8(utf8, buffer, buflen);
120 int from_utf16(const wchar_t *utf16, TCHAR **buffer, unsigned long *buflen) {
122 return to_utf16(utf16, buffer, buflen);
124 return to_utf8(utf16, buffer, buflen);