Tidy up French GUI.
[nssm.git] / utf8.cpp
1 #include "nssm.h"\r
2 \r
3 static unsigned long cp;\r
4 \r
5 void setup_utf8() {\r
6 #ifdef UNICODE\r
7   /*\r
8     Ensure we write in UTF-8 mode, so that non-ASCII characters don't get\r
9     mangled.  If we were compiled in ANSI mode it won't work.\r
10    */\r
11   cp = GetConsoleOutputCP();\r
12   SetConsoleOutputCP(CP_UTF8);\r
13   _setmode(_fileno(stdout), _O_U8TEXT);\r
14   _setmode(_fileno(stderr), _O_U8TEXT);\r
15 #endif\r
16 }\r
17 \r
18 void unsetup_utf8() {\r
19   if (cp) SetConsoleOutputCP(cp);\r
20 }\r
21 \r
22 /*\r
23   Conversion functions.\r
24 \r
25   to_utf8/16() converts a string which may be either utf8 or utf16 to\r
26   the desired format.  If no conversion is needed a new string is still\r
27   allocated and the old string is copied.\r
28 \r
29   from_utf8/16() converts a string which IS in the specified format to\r
30   whichever format is needed according to whether UNICODE is defined.\r
31   It simply wraps the appropriate to_utf8/16() function.\r
32 \r
33   Therefore the caller must ALWAYS free the destination pointer after a\r
34   successful (return code 0) call to one of these functions.\r
35 \r
36   The length pointer is optional.  Pass NULL if you don't care about\r
37   the length of the converted string.\r
38 \r
39   Both the destination and the length, if supplied, will be zeroed if\r
40   no conversion was done.\r
41 */\r
42 int to_utf8(const wchar_t *utf16, char **utf8, unsigned long *utf8len) {\r
43   *utf8 = 0;\r
44   if (utf8len) *utf8len = 0;\r
45   int size = WideCharToMultiByte(CP_UTF8, 0, utf16, -1, NULL, 0, NULL, NULL);\r
46   if (! size) return 1;\r
47 \r
48   *utf8 = (char *) HeapAlloc(GetProcessHeap(), 0, size);\r
49   if (! *utf8) return 2;\r
50 \r
51   if (! WideCharToMultiByte(CP_UTF8, 0, utf16, -1, *utf8, size, NULL, NULL)) {\r
52     HeapFree(GetProcessHeap(), 0, *utf8);\r
53     *utf8 = 0;\r
54     return 3;\r
55   }\r
56 \r
57   if (utf8len) *utf8len = (unsigned long) strlen(*utf8);\r
58 \r
59   return 0;\r
60 }\r
61 \r
62 int to_utf8(const char *ansi, char **utf8, unsigned long *utf8len) {\r
63   *utf8 = 0;\r
64   if (utf8len) *utf8len = 0;\r
65   size_t len = strlen(ansi);\r
66   int size = (int) len + 1;\r
67 \r
68   *utf8 = (char *) HeapAlloc(GetProcessHeap(), 0, size);\r
69   if (! *utf8) return 2;\r
70 \r
71   if (utf8len) *utf8len = (unsigned long) len;\r
72   memmove(*utf8, ansi, size);\r
73 \r
74   return 0;\r
75 }\r
76 \r
77 int to_utf16(const char *utf8, wchar_t **utf16, unsigned long *utf16len) {\r
78   *utf16 = 0;\r
79   if (utf16len) *utf16len = 0;\r
80   int size = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);\r
81   if (! size) return 1;\r
82 \r
83   *utf16 = (wchar_t *) HeapAlloc(GetProcessHeap(), 0, size * sizeof(wchar_t));\r
84   if (! *utf16) return 2;\r
85 \r
86   if (! MultiByteToWideChar(CP_UTF8, 0, utf8, -1, *utf16, size)) {\r
87     HeapFree(GetProcessHeap(), 0, *utf16);\r
88     *utf16 = 0;\r
89     return 3;\r
90   }\r
91 \r
92   if (utf16len) *utf16len = (unsigned long) wcslen(*utf16);\r
93 \r
94   return 0;\r
95 }\r
96 \r
97 int to_utf16(const wchar_t *unicode, wchar_t **utf16, unsigned long *utf16len) {\r
98   *utf16 = 0;\r
99   if (utf16len) *utf16len = 0;\r
100   size_t len = wcslen(unicode);\r
101   int size = ((int) len + 1) * sizeof(wchar_t);\r
102 \r
103   *utf16 = (wchar_t *) HeapAlloc(GetProcessHeap(), 0, size);\r
104   if (! *utf16) return 2;\r
105 \r
106   if (utf16len) *utf16len = (unsigned long) len;\r
107   memmove(*utf16, unicode, size);\r
108 \r
109   return 0;\r
110 }\r
111 \r
112 int from_utf8(const char *utf8, TCHAR **buffer, unsigned long *buflen) {\r
113 #ifdef UNICODE\r
114   return to_utf16(utf8, buffer, buflen);\r
115 #else\r
116   return to_utf8(utf8, buffer, buflen);\r
117 #endif\r
118 }\r
119 \r
120 int from_utf16(const wchar_t *utf16, TCHAR **buffer, unsigned long *buflen) {\r
121 #ifdef UNICODE\r
122   return to_utf16(utf16, buffer, buflen);\r
123 #else\r
124   return to_utf8(utf16, buffer, buflen);\r
125 #endif\r
126 }\r