utf8.cpp 3.4 KB

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