Source code of Windows XP (NT5)
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.

103 lines
3.3 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows NT **/
  3. /** Copyright(c) Microsoft Corp., 1997 **/
  4. /**********************************************************************/
  5. /*
  6. aucommon.cxx
  7. Common routines for ANSI/UNICODE classes.
  8. FILE HISTORY:
  9. 5/21/97 michth created
  10. */
  11. #include "precomp.hxx"
  12. #include "aucommon.hxx"
  13. int
  14. ConvertMultiByteToUnicode(LPSTR pszSrcAnsiString,
  15. BUFFER *pbufDstUnicodeString,
  16. DWORD dwStringLen)
  17. {
  18. DBG_ASSERT(pszSrcAnsiString != NULL);
  19. int iStrLen = -1;
  20. BOOL bTemp;
  21. bTemp = pbufDstUnicodeString->Resize((dwStringLen + 1) * sizeof(WCHAR));
  22. if (bTemp) {
  23. iStrLen = MultiByteToWideChar(CP_ACP,
  24. MB_PRECOMPOSED,
  25. pszSrcAnsiString,
  26. dwStringLen + 1,
  27. (LPWSTR)pbufDstUnicodeString->QueryPtr(),
  28. (int)pbufDstUnicodeString->QuerySize());
  29. if (iStrLen == 0) {
  30. DBG_ASSERT(GetLastError() != ERROR_INSUFFICIENT_BUFFER);
  31. iStrLen = -1;
  32. }
  33. else {
  34. //
  35. // Don't count '\0'
  36. //
  37. iStrLen--;
  38. }
  39. }
  40. return iStrLen;
  41. }
  42. int
  43. ConvertUnicodeToMultiByte(LPWSTR pszSrcUnicodeString,
  44. BUFFER *pbufDstAnsiString,
  45. DWORD dwStringLen)
  46. {
  47. DBG_ASSERT(pszSrcUnicodeString != NULL);
  48. BOOL bTemp;
  49. int iStrLen = 0;
  50. iStrLen = WideCharToMultiByte(CP_ACP,
  51. 0,
  52. pszSrcUnicodeString,
  53. dwStringLen + 1,
  54. (LPSTR)pbufDstAnsiString->QueryPtr(),
  55. (int)pbufDstAnsiString->QuerySize(),
  56. NULL,
  57. NULL);
  58. if ((iStrLen == 0) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
  59. iStrLen = WideCharToMultiByte(CP_ACP,
  60. 0,
  61. pszSrcUnicodeString,
  62. dwStringLen + 1,
  63. NULL,
  64. 0,
  65. NULL,
  66. NULL);
  67. if (iStrLen != 0) {
  68. bTemp = pbufDstAnsiString->Resize(iStrLen);
  69. if (!bTemp) {
  70. iStrLen = 0;
  71. }
  72. else {
  73. iStrLen = WideCharToMultiByte(CP_ACP,
  74. 0,
  75. pszSrcUnicodeString,
  76. dwStringLen + 1,
  77. (LPSTR)pbufDstAnsiString->QueryPtr(),
  78. (int)pbufDstAnsiString->QuerySize(),
  79. NULL,
  80. NULL);
  81. }
  82. }
  83. }
  84. //
  85. // Don't count '\0'
  86. // and convert 0 to -1 for errors
  87. //
  88. iStrLen--;
  89. return iStrLen;
  90. }