AutoChar.h 2.2 KB

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