Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
2.4 KiB

  1. //
  2. // xstring.h
  3. //
  4. // Unicode/ansi conversion.
  5. //
  6. #ifndef XSTRING_H
  7. #define XSTRING_H
  8. char *UnicodeToAnsi(UINT uCodePage, const WCHAR *pchW, UINT uLenW, char *pchAIn, UINT uSizeA);
  9. WCHAR *AnsiToUnicode(UINT uCodePage, const char *pchA, UINT uLenA, WCHAR *pchWIn, UINT uSizeW);
  10. void BufferAllocFree(void *pBuffer, void *pAllocMem);
  11. #ifdef __cplusplus
  12. class WtoA {
  13. public:
  14. WtoA(const WCHAR* str)
  15. {
  16. int cch = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
  17. _pch = new char[cch + 1];
  18. if (_pch)
  19. WideCharToMultiByte(CP_ACP, 0, str, -1, _pch, cch, NULL, NULL);
  20. }
  21. WtoA(const WCHAR* str, ULONG cch)
  22. {
  23. int cchA = WideCharToMultiByte(CP_ACP, 0, str, cch, NULL, 0, NULL, NULL);
  24. _pch = new char[cchA + 1];
  25. if (_pch)
  26. WideCharToMultiByte(CP_ACP, 0, str, cch, _pch, cchA, NULL, NULL);
  27. }
  28. ~WtoA()
  29. {
  30. delete _pch;
  31. }
  32. operator char*()
  33. {
  34. if (_pch)
  35. return _pch;
  36. Assert(0);
  37. return "\0";
  38. }
  39. protected:
  40. char* _pch;
  41. };
  42. class AtoW {
  43. public:
  44. AtoW(const char* str)
  45. {
  46. int cch = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
  47. _pch = new WCHAR[cch + 1];
  48. if (_pch)
  49. MultiByteToWideChar(CP_ACP, 0, str, -1, _pch, cch);
  50. }
  51. AtoW(const char* str, ULONG cch)
  52. {
  53. int cchW = MultiByteToWideChar(CP_ACP, 0, str, cch, NULL, 0);
  54. _pch = new WCHAR[cchW + 1];
  55. if (_pch)
  56. MultiByteToWideChar(CP_ACP, 0, str, cch, _pch, cchW);
  57. }
  58. ~AtoW()
  59. {
  60. delete _pch;
  61. }
  62. operator WCHAR*()
  63. {
  64. if (_pch)
  65. return _pch;
  66. Assert(0);
  67. return L"\0";
  68. }
  69. protected:
  70. WCHAR* _pch;
  71. };
  72. class WCHtoWSZ {
  73. public:
  74. WCHtoWSZ (const WCHAR *pch, ULONG cch)
  75. {
  76. Assert(pch || !cch);
  77. if (cch == (ULONG)(-1))
  78. cch = wcslen(pch);
  79. _pch = new WCHAR[cch + 1];
  80. if (_pch)
  81. {
  82. if (pch && cch)
  83. memcpy(_pch, pch, cch * sizeof(WCHAR));
  84. _pch[cch] = L'\0';
  85. }
  86. }
  87. ~WCHtoWSZ ()
  88. {
  89. delete _pch;
  90. }
  91. operator WCHAR*()
  92. {
  93. if (_pch)
  94. return _pch;
  95. Assert(0);
  96. return L"\0";
  97. }
  98. protected:
  99. WCHAR* _pch;
  100. };
  101. #endif // __cplusplus
  102. #endif // XSTRING_H