AutoChar.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #ifndef NULLSOFT_AUTOCHARH
  2. #define NULLSOFT_AUTOCHARH
  3. #ifdef WIN32
  4. #include <windows.h>
  5. inline char *AutoCharDupN(const wchar_t *convert, size_t len, UINT codePage = CP_ACP, UINT flags=0)
  6. {
  7. if (!convert)
  8. return 0;
  9. int size = WideCharToMultiByte(codePage, flags, convert, (int)len, 0, 0, NULL, NULL);
  10. if (!size)
  11. return 0;
  12. char *narrow = (char *)malloc((size+1)*sizeof(char));
  13. if (!WideCharToMultiByte(codePage, flags, convert, (int)len, narrow, size, NULL, NULL))
  14. {
  15. free(narrow);
  16. narrow=0;
  17. }
  18. else
  19. narrow[size]=0;
  20. return narrow;
  21. }
  22. inline char *AutoCharDup(const wchar_t *convert, UINT codePage = CP_ACP, UINT flags=0)
  23. {
  24. if (!convert)
  25. return 0;
  26. int size = WideCharToMultiByte(codePage, flags, convert, -1, 0, 0, NULL, NULL);
  27. if (!size)
  28. return 0;
  29. char *narrow = (char *)malloc(size*sizeof(char));
  30. if (!WideCharToMultiByte(codePage, flags, convert, -1, narrow, size, NULL, NULL))
  31. {
  32. free(narrow);
  33. narrow=0;
  34. }
  35. return narrow;
  36. }
  37. class AutoChar
  38. {
  39. public:
  40. AutoChar(const wchar_t *convert, UINT codePage = CP_ACP, UINT flags=0) : narrow(0)
  41. {
  42. narrow = AutoCharDup(convert, codePage, flags);
  43. }
  44. ~AutoChar()
  45. {
  46. free(narrow);
  47. narrow=0;
  48. }
  49. operator const char *()
  50. {
  51. return narrow;
  52. }
  53. operator char *()
  54. {
  55. return narrow;
  56. }
  57. protected:
  58. AutoChar() : narrow(0)
  59. {
  60. }
  61. char *narrow;
  62. };
  63. class AutoCharN : public AutoChar
  64. {
  65. public:
  66. AutoCharN(const wchar_t *convert, size_t len, UINT codePage = CP_ACP, UINT flags=0)
  67. {
  68. narrow = AutoCharDupN(convert, len, codePage, flags);
  69. }
  70. };
  71. #else
  72. #include <stdlib.h>
  73. #include <wchar.h>
  74. inline char *AutoCharDup(const wchar_t *convert)
  75. {
  76. if (!convert)
  77. return 0;
  78. size_t size = wcslen(convert)+1;
  79. if (!size)
  80. return 0;
  81. char *narrow = (char *)malloc(size*sizeof(char));
  82. if (wcstombs(narrow, convert, size) == (size_t)-1)
  83. {
  84. free(narrow);
  85. narrow=0;
  86. }
  87. return narrow;
  88. }
  89. class AutoChar
  90. {
  91. public:
  92. AutoChar(const wchar_t *convert) : narrow(0)
  93. {
  94. narrow = AutoCharDup(convert);
  95. }
  96. ~AutoChar()
  97. {
  98. free(narrow);
  99. narrow=0;
  100. }
  101. operator const char *()
  102. {
  103. return narrow;
  104. }
  105. operator char *()
  106. {
  107. return narrow;
  108. }
  109. private:
  110. char *narrow;
  111. };
  112. #endif
  113. #endif