StringConvert.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Common/StringConvert.cpp
  2. #include "StdAfx.h"
  3. #include "StringConvert.h"
  4. #ifndef _WIN32
  5. #include <stdlib.h>
  6. #endif
  7. #ifdef _WIN32
  8. UString MultiByteToUnicodeString(const AString &srcString, UINT codePage)
  9. {
  10. UString resultString;
  11. if(!srcString.IsEmpty())
  12. {
  13. int numChars = MultiByteToWideChar(codePage, 0, srcString,
  14. srcString.Length(), resultString.GetBuffer(srcString.Length()),
  15. srcString.Length() + 1);
  16. #ifndef _WIN32_WCE
  17. if(numChars == 0)
  18. throw 282228;
  19. #endif
  20. resultString.ReleaseBuffer(numChars);
  21. }
  22. return resultString;
  23. }
  24. AString UnicodeStringToMultiByte(const UString &srcString, UINT codePage)
  25. {
  26. AString resultString;
  27. if(!srcString.IsEmpty())
  28. {
  29. int numRequiredBytes = srcString.Length() * 2;
  30. char defaultChar = '_';
  31. int numChars = WideCharToMultiByte(codePage, 0, srcString,
  32. srcString.Length(), resultString.GetBuffer(numRequiredBytes),
  33. numRequiredBytes + 1, &defaultChar, NULL);
  34. #ifndef _WIN32_WCE
  35. if(numChars == 0)
  36. throw 282229;
  37. #endif
  38. resultString.ReleaseBuffer(numChars);
  39. }
  40. return resultString;
  41. }
  42. #ifndef _WIN32_WCE
  43. AString SystemStringToOemString(const CSysString &srcString)
  44. {
  45. AString result;
  46. CharToOem(srcString, result.GetBuffer(srcString.Length() * 2));
  47. result.ReleaseBuffer();
  48. return result;
  49. }
  50. #endif
  51. #else
  52. UString MultiByteToUnicodeString(const AString &srcString, UINT codePage)
  53. {
  54. UString resultString;
  55. for (int i = 0; i < srcString.Length(); i++)
  56. resultString += wchar_t(srcString[i]);
  57. /*
  58. if(!srcString.IsEmpty())
  59. {
  60. int numChars = mbstowcs(resultString.GetBuffer(srcString.Length()), srcString, srcString.Length() + 1);
  61. if (numChars < 0) throw "Your environment does not support UNICODE";
  62. resultString.ReleaseBuffer(numChars);
  63. }
  64. */
  65. return resultString;
  66. }
  67. AString UnicodeStringToMultiByte(const UString &srcString, UINT codePage)
  68. {
  69. AString resultString;
  70. for (int i = 0; i < srcString.Length(); i++)
  71. resultString += char(srcString[i]);
  72. /*
  73. if(!srcString.IsEmpty())
  74. {
  75. int numRequiredBytes = srcString.Length() * 6 + 1;
  76. int numChars = wcstombs(resultString.GetBuffer(numRequiredBytes), srcString, numRequiredBytes);
  77. if (numChars < 0) throw "Your environment does not support UNICODE";
  78. resultString.ReleaseBuffer(numChars);
  79. }
  80. */
  81. return resultString;
  82. }
  83. #endif