Added UTF-8 functions.
[nssm.git] / utf8.cpp
1 #include "nssm.h"
2
3 static unsigned long cp;
4
5 void setup_utf8() {
6 #ifdef UNICODE
7   /*
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.
10    */
11   cp = GetConsoleOutputCP();
12   SetConsoleOutputCP(CP_UTF8);
13   _setmode(_fileno(stdout), _O_U8TEXT);
14   _setmode(_fileno(stderr), _O_U8TEXT);
15 #endif
16 }
17
18 void unsetup_utf8() {
19   if (cp) SetConsoleOutputCP(cp);
20 }
21
22 /*
23   Conversion functions.
24
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.
28
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.
32
33   Therefore the caller must ALWAYS free the destination pointer after a
34   successful (return code 0) call to one of these functions.
35
36   The length pointer is optional.  Pass NULL if you don't care about
37   the length of the converted string.
38
39   Both the destination and the length, if supplied, will be zeroed if
40   no conversion was done.
41 */
42 int to_utf8(const wchar_t *utf16, char **utf8, unsigned long *utf8len) {
43   *utf8 = 0;
44   if (utf8len) *utf8len = 0;
45   int size = WideCharToMultiByte(CP_UTF8, 0, utf16, -1, NULL, 0, NULL, NULL);
46   if (! size) return 1;
47
48   *utf8 = (char *) HeapAlloc(GetProcessHeap(), 0, size);
49   if (! *utf8) return 2;
50
51   if (! WideCharToMultiByte(CP_UTF8, 0, utf16, -1, (LPSTR) utf8, size, NULL, NULL)) {
52     HeapFree(GetProcessHeap(), 0, *utf8);
53     *utf8 = 0;
54     return 3;
55   }
56
57   if (utf8len) *utf8len = (unsigned long) strlen(*utf8);
58
59   return 0;
60 }
61
62 int to_utf8(const char *ansi, char **utf8, unsigned long *utf8len) {
63   *utf8 = 0;
64   if (utf8len) *utf8len = 0;
65   size_t len = strlen(ansi);
66   int size = (int) len + 1;
67
68   *utf8 = (char *) HeapAlloc(GetProcessHeap(), 0, size);
69   if (! *utf8) return 2;
70
71   if (utf8len) *utf8len = (unsigned long) len;
72   memmove(*utf8, ansi, size);
73
74   return 0;
75 }
76
77 int to_utf16(const char *utf8, wchar_t **utf16, unsigned long *utf16len) {
78   *utf16 = 0;
79   if (utf16len) *utf16len = 0;
80   int size = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
81   if (! size) return 1;
82
83   *utf16 = (wchar_t *) HeapAlloc(GetProcessHeap(), 0, size * sizeof(wchar_t));
84   if (! *utf16) return 2;
85
86   if (! MultiByteToWideChar(CP_UTF8, 0, utf8, -1, *utf16, size)) {
87     HeapFree(GetProcessHeap(), 0, *utf16);
88     *utf16 = 0;
89     return 3;
90   }
91
92   if (utf16len) *utf16len = (unsigned long) wcslen(*utf16);
93
94   return 0;
95 }
96
97 int to_utf16(const wchar_t *unicode, wchar_t **utf16, unsigned long *utf16len) {
98   *utf16 = 0;
99   if (utf16len) *utf16len = 0;
100   size_t len = wcslen(unicode);
101   int size = ((int) len + 1) * sizeof(wchar_t);
102
103   *utf16 = (wchar_t *) HeapAlloc(GetProcessHeap(), 0, size);
104   if (! *utf16) return 2;
105
106   if (utf16len) *utf16len = (unsigned long) len;
107   memmove(*utf16, unicode, size);
108
109   return 0;
110 }
111
112 int from_utf8(const char *utf8, TCHAR **buffer, unsigned long *buflen) {
113 #ifdef UNICODE
114   return to_utf16(utf8, buffer, buflen);
115 #else
116   return to_utf8(utf8, buffer, buflen);
117 #endif
118 }
119
120 int from_utf16(const wchar_t *utf16, TCHAR **buffer, unsigned long *buflen) {
121 #ifdef UNICODE
122   return to_utf16(utf16, buffer, buflen);
123 #else
124   return to_utf8(utf16, buffer, buflen);
125 #endif
126 }