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.

198 lines
3.6 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995
  5. //
  6. // File: sconv.cxx
  7. //
  8. // Contents: Ansi to Unicode conversions
  9. //
  10. // History: KrishnaG Jan 22 1996
  11. //----------------------------------------------------------------------------
  12. #include "procs.hxx"
  13. #pragma hdrstop
  14. #define NULL_TERMINATED 0
  15. int
  16. AnsiToUnicodeString(
  17. LPSTR pAnsi,
  18. LPWSTR pUnicode,
  19. DWORD StringLength
  20. )
  21. {
  22. int iReturn;
  23. if( StringLength == NULL_TERMINATED )
  24. StringLength = strlen( pAnsi );
  25. iReturn = MultiByteToWideChar(CP_ACP,
  26. MB_PRECOMPOSED,
  27. pAnsi,
  28. StringLength + 1,
  29. pUnicode,
  30. StringLength + 1 );
  31. //
  32. // Ensure NULL termination.
  33. //
  34. pUnicode[StringLength] = 0;
  35. return iReturn;
  36. }
  37. int
  38. UnicodeToAnsiString(
  39. LPWSTR pUnicode,
  40. LPSTR pAnsi,
  41. DWORD StringLength
  42. )
  43. {
  44. LPSTR pTempBuf = NULL;
  45. INT rc = 0;
  46. if( StringLength == NULL_TERMINATED ) {
  47. //
  48. // StringLength is just the
  49. // number of characters in the string
  50. //
  51. StringLength = wcslen( pUnicode );
  52. }
  53. //
  54. // WideCharToMultiByte doesn't NULL terminate if we're copying
  55. // just part of the string, so terminate here.
  56. //
  57. pUnicode[StringLength] = 0;
  58. //
  59. // Include one for the NULL
  60. //
  61. StringLength++;
  62. //
  63. // Unfortunately, WideCharToMultiByte doesn't do conversion in place,
  64. // so allocate a temporary buffer, which we can then copy:
  65. //
  66. if( pAnsi == (LPSTR)pUnicode )
  67. {
  68. pTempBuf = (LPSTR)LocalAlloc( LPTR, StringLength * 2);
  69. pAnsi = pTempBuf;
  70. }
  71. if( pAnsi )
  72. {
  73. rc = WideCharToMultiByte( CP_ACP,
  74. 0,
  75. pUnicode,
  76. StringLength,
  77. pAnsi,
  78. StringLength*2,
  79. NULL,
  80. NULL );
  81. }
  82. /* If pTempBuf is non-null, we must copy the resulting string
  83. * so that it looks as if we did it in place:
  84. */
  85. if( pTempBuf && ( rc > 0 ) )
  86. {
  87. pAnsi = (LPSTR)pUnicode;
  88. strcpy( pAnsi, pTempBuf );
  89. LocalFree( pTempBuf );
  90. }
  91. return rc;
  92. }
  93. LPWSTR
  94. AllocateUnicodeString(
  95. LPSTR pAnsiString
  96. )
  97. {
  98. LPWSTR pUnicodeString = NULL;
  99. if (!pAnsiString)
  100. return NULL;
  101. pUnicodeString = (LPWSTR)LocalAlloc(
  102. LPTR,
  103. strlen(pAnsiString)*sizeof(WCHAR) +sizeof(WCHAR)
  104. );
  105. if (pUnicodeString) {
  106. AnsiToUnicodeString(
  107. pAnsiString,
  108. pUnicodeString,
  109. NULL_TERMINATED
  110. );
  111. }
  112. return pUnicodeString;
  113. }
  114. void
  115. FreeUnicodeString(
  116. LPWSTR pUnicodeString
  117. )
  118. {
  119. if (!pUnicodeString)
  120. return;
  121. LocalFree(pUnicodeString);
  122. return;
  123. }
  124. DWORD
  125. ComputeMaxStrlenW(
  126. LPWSTR pString,
  127. DWORD cchBufMax
  128. )
  129. {
  130. DWORD cchLen;
  131. //
  132. // Include space for the NULL.
  133. //
  134. cchBufMax--;
  135. cchLen = wcslen(pString);
  136. if (cchLen > cchBufMax)
  137. return cchBufMax;
  138. return cchLen;
  139. }
  140. DWORD
  141. ComputeMaxStrlenA(
  142. LPSTR pString,
  143. DWORD cchBufMax
  144. )
  145. {
  146. DWORD cchLen;
  147. //
  148. // Include space for the NULL.
  149. //
  150. cchBufMax--;
  151. cchLen = lstrlenA(pString);
  152. if (cchLen > cchBufMax)
  153. return cchBufMax;
  154. return cchLen;
  155. }