-#include "nssm.h"
-
-static unsigned long cp;
-
-void setup_utf8() {
-#ifdef UNICODE
- /*
- Ensure we write in UTF-8 mode, so that non-ASCII characters don't get
- mangled. If we were compiled in ANSI mode it won't work.
- */
- cp = GetConsoleOutputCP();
- SetConsoleOutputCP(CP_UTF8);
- _setmode(_fileno(stdout), _O_U8TEXT);
- _setmode(_fileno(stderr), _O_U8TEXT);
-#endif
-}
-
-void unsetup_utf8() {
- if (cp) SetConsoleOutputCP(cp);
-}
-
-/*
- Conversion functions.
-
- to_utf8/16() converts a string which may be either utf8 or utf16 to
- the desired format. If no conversion is needed a new string is still
- allocated and the old string is copied.
-
- from_utf8/16() converts a string which IS in the specified format to
- whichever format is needed according to whether UNICODE is defined.
- It simply wraps the appropriate to_utf8/16() function.
-
- Therefore the caller must ALWAYS free the destination pointer after a
- successful (return code 0) call to one of these functions.
-
- The length pointer is optional. Pass NULL if you don't care about
- the length of the converted string.
-
- Both the destination and the length, if supplied, will be zeroed if
- no conversion was done.
-*/
-int to_utf8(const wchar_t *utf16, char **utf8, unsigned long *utf8len) {
- *utf8 = 0;
- if (utf8len) *utf8len = 0;
- int size = WideCharToMultiByte(CP_UTF8, 0, utf16, -1, NULL, 0, NULL, NULL);
- if (! size) return 1;
-
- *utf8 = (char *) HeapAlloc(GetProcessHeap(), 0, size);
- if (! *utf8) return 2;
-
- if (! WideCharToMultiByte(CP_UTF8, 0, utf16, -1, (LPSTR) utf8, size, NULL, NULL)) {
- HeapFree(GetProcessHeap(), 0, *utf8);
- *utf8 = 0;
- return 3;
- }
-
- if (utf8len) *utf8len = (unsigned long) strlen(*utf8);
-
- return 0;
-}
-
-int to_utf8(const char *ansi, char **utf8, unsigned long *utf8len) {
- *utf8 = 0;
- if (utf8len) *utf8len = 0;
- size_t len = strlen(ansi);
- int size = (int) len + 1;
-
- *utf8 = (char *) HeapAlloc(GetProcessHeap(), 0, size);
- if (! *utf8) return 2;
-
- if (utf8len) *utf8len = (unsigned long) len;
- memmove(*utf8, ansi, size);
-
- return 0;
-}
-
-int to_utf16(const char *utf8, wchar_t **utf16, unsigned long *utf16len) {
- *utf16 = 0;
- if (utf16len) *utf16len = 0;
- int size = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
- if (! size) return 1;
-
- *utf16 = (wchar_t *) HeapAlloc(GetProcessHeap(), 0, size * sizeof(wchar_t));
- if (! *utf16) return 2;
-
- if (! MultiByteToWideChar(CP_UTF8, 0, utf8, -1, *utf16, size)) {
- HeapFree(GetProcessHeap(), 0, *utf16);
- *utf16 = 0;
- return 3;
- }
-
- if (utf16len) *utf16len = (unsigned long) wcslen(*utf16);
-
- return 0;
-}
-
-int to_utf16(const wchar_t *unicode, wchar_t **utf16, unsigned long *utf16len) {
- *utf16 = 0;
- if (utf16len) *utf16len = 0;
- size_t len = wcslen(unicode);
- int size = ((int) len + 1) * sizeof(wchar_t);
-
- *utf16 = (wchar_t *) HeapAlloc(GetProcessHeap(), 0, size);
- if (! *utf16) return 2;
-
- if (utf16len) *utf16len = (unsigned long) len;
- memmove(*utf16, unicode, size);
-
- return 0;
-}
-
-int from_utf8(const char *utf8, TCHAR **buffer, unsigned long *buflen) {
-#ifdef UNICODE
- return to_utf16(utf8, buffer, buflen);
-#else
- return to_utf8(utf8, buffer, buflen);
-#endif
-}
-
-int from_utf16(const wchar_t *utf16, TCHAR **buffer, unsigned long *buflen) {
-#ifdef UNICODE
- return to_utf16(utf16, buffer, buflen);
-#else
- return to_utf8(utf16, buffer, buflen);
-#endif
-}
+#include "nssm.h"\r
+\r
+static unsigned long cp;\r
+\r
+void setup_utf8() {\r
+#ifdef UNICODE\r
+ /*\r
+ Ensure we write in UTF-8 mode, so that non-ASCII characters don't get\r
+ mangled. If we were compiled in ANSI mode it won't work.\r
+ */\r
+ cp = GetConsoleOutputCP();\r
+ SetConsoleOutputCP(CP_UTF8);\r
+ _setmode(_fileno(stdout), _O_U8TEXT);\r
+ _setmode(_fileno(stderr), _O_U8TEXT);\r
+#endif\r
+}\r
+\r
+void unsetup_utf8() {\r
+ if (cp) SetConsoleOutputCP(cp);\r
+}\r
+\r
+/*\r
+ Conversion functions.\r
+\r
+ to_utf8/16() converts a string which may be either utf8 or utf16 to\r
+ the desired format. If no conversion is needed a new string is still\r
+ allocated and the old string is copied.\r
+\r
+ from_utf8/16() converts a string which IS in the specified format to\r
+ whichever format is needed according to whether UNICODE is defined.\r
+ It simply wraps the appropriate to_utf8/16() function.\r
+\r
+ Therefore the caller must ALWAYS free the destination pointer after a\r
+ successful (return code 0) call to one of these functions.\r
+\r
+ The length pointer is optional. Pass NULL if you don't care about\r
+ the length of the converted string.\r
+\r
+ Both the destination and the length, if supplied, will be zeroed if\r
+ no conversion was done.\r
+*/\r
+int to_utf8(const wchar_t *utf16, char **utf8, unsigned long *utf8len) {\r
+ *utf8 = 0;\r
+ if (utf8len) *utf8len = 0;\r
+ int size = WideCharToMultiByte(CP_UTF8, 0, utf16, -1, NULL, 0, NULL, NULL);\r
+ if (! size) return 1;\r
+\r
+ *utf8 = (char *) HeapAlloc(GetProcessHeap(), 0, size);\r
+ if (! *utf8) return 2;\r
+\r
+ if (! WideCharToMultiByte(CP_UTF8, 0, utf16, -1, *utf8, size, NULL, NULL)) {\r
+ HeapFree(GetProcessHeap(), 0, *utf8);\r
+ *utf8 = 0;\r
+ return 3;\r
+ }\r
+\r
+ if (utf8len) *utf8len = (unsigned long) strlen(*utf8);\r
+\r
+ return 0;\r
+}\r
+\r
+int to_utf8(const char *ansi, char **utf8, unsigned long *utf8len) {\r
+ *utf8 = 0;\r
+ if (utf8len) *utf8len = 0;\r
+ size_t len = strlen(ansi);\r
+ int size = (int) len + 1;\r
+\r
+ *utf8 = (char *) HeapAlloc(GetProcessHeap(), 0, size);\r
+ if (! *utf8) return 2;\r
+\r
+ if (utf8len) *utf8len = (unsigned long) len;\r
+ memmove(*utf8, ansi, size);\r
+\r
+ return 0;\r
+}\r
+\r
+int to_utf16(const char *utf8, wchar_t **utf16, unsigned long *utf16len) {\r
+ *utf16 = 0;\r
+ if (utf16len) *utf16len = 0;\r
+ int size = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);\r
+ if (! size) return 1;\r
+\r
+ *utf16 = (wchar_t *) HeapAlloc(GetProcessHeap(), 0, size * sizeof(wchar_t));\r
+ if (! *utf16) return 2;\r
+\r
+ if (! MultiByteToWideChar(CP_UTF8, 0, utf8, -1, *utf16, size)) {\r
+ HeapFree(GetProcessHeap(), 0, *utf16);\r
+ *utf16 = 0;\r
+ return 3;\r
+ }\r
+\r
+ if (utf16len) *utf16len = (unsigned long) wcslen(*utf16);\r
+\r
+ return 0;\r
+}\r
+\r
+int to_utf16(const wchar_t *unicode, wchar_t **utf16, unsigned long *utf16len) {\r
+ *utf16 = 0;\r
+ if (utf16len) *utf16len = 0;\r
+ size_t len = wcslen(unicode);\r
+ int size = ((int) len + 1) * sizeof(wchar_t);\r
+\r
+ *utf16 = (wchar_t *) HeapAlloc(GetProcessHeap(), 0, size);\r
+ if (! *utf16) return 2;\r
+\r
+ if (utf16len) *utf16len = (unsigned long) len;\r
+ memmove(*utf16, unicode, size);\r
+\r
+ return 0;\r
+}\r
+\r
+int from_utf8(const char *utf8, TCHAR **buffer, unsigned long *buflen) {\r
+#ifdef UNICODE\r
+ return to_utf16(utf8, buffer, buflen);\r
+#else\r
+ return to_utf8(utf8, buffer, buflen);\r
+#endif\r
+}\r
+\r
+int from_utf16(const wchar_t *utf16, TCHAR **buffer, unsigned long *buflen) {\r
+#ifdef UNICODE\r
+ return to_utf16(utf16, buffer, buflen);\r
+#else\r
+ return to_utf8(utf16, buffer, buflen);\r
+#endif\r
+}\r