1
0

AutoWide.h 1.4 KB

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