AutoWide.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #ifndef AUTOWIDEH
  2. #define AUTOWIDEH
  3. #ifdef WIN32
  4. #include <windows.h>
  5. inline wchar_t *AutoWideDup(const char *convert, UINT codePage=CP_ACP)
  6. {
  7. wchar_t *wide = 0;
  8. if (!convert)
  9. return 0;
  10. int size = MultiByteToWideChar(codePage, 0, convert, -1, 0,0);
  11. if (!size)
  12. return 0;
  13. wide = (wchar_t *)malloc(size*sizeof(wchar_t));
  14. if (!MultiByteToWideChar(codePage, 0, convert, -1, wide,size))
  15. {
  16. free(wide);
  17. wide=0;
  18. }
  19. return wide;
  20. }
  21. class AutoWide
  22. {
  23. public:
  24. AutoWide(const char *convert, UINT codePage=CP_ACP) : wide(0)
  25. {
  26. wide = AutoWideDup(convert, codePage);
  27. }
  28. ~AutoWide()
  29. {
  30. free(wide);
  31. wide=0;
  32. }
  33. operator wchar_t *()
  34. {
  35. return wide;
  36. }
  37. private:
  38. wchar_t *wide;
  39. };
  40. #elif defined(__APPLE__)
  41. #include <string.h>
  42. inline wchar_t *AutoWideDup(const char *convert)
  43. {
  44. wchar_t *wide=0;
  45. if (!convert)
  46. return 0;
  47. int size = strlen(convert)+1;
  48. if (!size)
  49. return 0;
  50. wide = (wchar_t *)malloc(size*sizeof(wchar_t));
  51. if (mbstowcs(wide, convert, size) == (size_t)-1)
  52. {
  53. free(wide);
  54. wide=0;
  55. }
  56. return wide;
  57. }
  58. class AutoWide
  59. {
  60. public:
  61. AutoWide(const char *convert) : wide(0)
  62. {
  63. wide = AutoWideDup(convert);
  64. }
  65. ~AutoWide()
  66. {
  67. free(wide);
  68. wide=0;
  69. }
  70. operator wchar_t *()
  71. {
  72. return wide;
  73. }
  74. private:
  75. wchar_t *wide;
  76. };
  77. #endif
  78. #endif