AutoChar.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 ( narrow == 0 )
  14. return 0;
  15. if (!WideCharToMultiByte(codePage, flags, convert, (int)len, narrow, size, NULL, NULL))
  16. {
  17. free(narrow);
  18. narrow=0;
  19. }
  20. else
  21. narrow[size]=0;
  22. return narrow;
  23. }
  24. inline char *AutoCharDup(const wchar_t *convert, UINT codePage = CP_ACP, UINT flags=0)
  25. {
  26. if (!convert)
  27. return 0;
  28. int size = WideCharToMultiByte(codePage, flags, convert, -1, 0, 0, NULL, NULL);
  29. if (!size)
  30. return 0;
  31. char *narrow = (char *)malloc(size*sizeof(char));
  32. if ( narrow == 0 )
  33. return 0;
  34. if (!WideCharToMultiByte(codePage, flags, convert, -1, narrow, size, NULL, NULL))
  35. {
  36. free(narrow);
  37. narrow=0;
  38. }
  39. return narrow;
  40. }
  41. class AutoChar
  42. {
  43. public:
  44. AutoChar(const wchar_t *convert, UINT codePage = CP_ACP, UINT flags=0) : narrow(0)
  45. {
  46. narrow = AutoCharDup(convert, codePage, flags);
  47. }
  48. ~AutoChar()
  49. {
  50. free(narrow);
  51. narrow=0;
  52. }
  53. operator const char *()
  54. {
  55. return narrow;
  56. }
  57. operator char *()
  58. {
  59. return narrow;
  60. }
  61. protected:
  62. AutoChar() : narrow(0)
  63. {
  64. }
  65. char *narrow;
  66. };
  67. class AutoCharGrow
  68. {
  69. public:
  70. AutoCharGrow()
  71. {
  72. narrow=0;
  73. size=0;
  74. }
  75. ~AutoCharGrow()
  76. {
  77. free(narrow);
  78. }
  79. const char *Convert(const wchar_t *convert, UINT codePage = CP_ACP, UINT flags=0, size_t *cch=0)
  80. {
  81. size_t new_size = WideCharToMultiByte(codePage, flags, convert, -1, 0, 0, NULL, NULL);
  82. if (!new_size)
  83. return 0;
  84. if ((size_t)new_size > size)
  85. {
  86. free(narrow);
  87. narrow = (char *)malloc(new_size * sizeof(char));
  88. if (!narrow)
  89. {
  90. size=0;
  91. return 0;
  92. }
  93. size=(size_t)new_size;
  94. }
  95. if (!WideCharToMultiByte(codePage, flags, convert, -1, narrow, (int)new_size, NULL, NULL))
  96. {
  97. return 0;
  98. }
  99. if (cch)
  100. *cch=new_size-1;
  101. return narrow;
  102. }
  103. protected:
  104. char *narrow;
  105. size_t size;
  106. };
  107. class AutoCharN : public AutoChar
  108. {
  109. public:
  110. AutoCharN(const wchar_t *convert, size_t len, UINT codePage = CP_ACP, UINT flags=0)
  111. {
  112. narrow = AutoCharDupN(convert, len, codePage, flags);
  113. }
  114. };
  115. #else
  116. #include <stdlib.h>
  117. #include <wchar.h>
  118. inline char *AutoCharDup(const wchar_t *convert)
  119. {
  120. if (!convert)
  121. return 0;
  122. size_t size = wcslen(convert)+1;
  123. if (!size)
  124. return 0;
  125. char *narrow = (char *)malloc(size*sizeof(char));
  126. if ( narrow == 0 )
  127. return 0;
  128. if (wcstombs(narrow, convert, size) == (size_t)-1)
  129. {
  130. free(narrow);
  131. narrow=0;
  132. }
  133. return narrow;
  134. }
  135. class AutoChar
  136. {
  137. public:
  138. AutoChar(const wchar_t *convert) : narrow(0)
  139. {
  140. narrow = AutoCharDup(convert);
  141. }
  142. ~AutoChar()
  143. {
  144. free(narrow);
  145. narrow=0;
  146. }
  147. operator const char *()
  148. {
  149. return narrow;
  150. }
  151. operator char *()
  152. {
  153. return narrow;
  154. }
  155. private:
  156. char *narrow;
  157. };
  158. #endif
  159. #endif