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.

222 lines
3.9 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. //----------------------------------------------------------------------------
  13. //
  14. // ********* System Includes
  15. //
  16. #define UNICODE
  17. #define _UNICODE
  18. #define INC_OLE2
  19. #include <windows.h>
  20. //
  21. // ********* CRunTime Includes
  22. //
  23. #include <stdlib.h>
  24. #include <limits.h>
  25. #include <io.h>
  26. #include <stdio.h>
  27. //
  28. // ********* Local Includes
  29. //
  30. #include "sconv.hxx"
  31. #define NULL_TERMINATED 0
  32. int
  33. AnsiToUnicodeString(
  34. LPSTR pAnsi,
  35. LPWSTR pUnicode,
  36. DWORD StringLength
  37. )
  38. {
  39. int iReturn;
  40. if( StringLength == NULL_TERMINATED )
  41. StringLength = strlen( pAnsi );
  42. iReturn = MultiByteToWideChar(CP_ACP,
  43. MB_PRECOMPOSED,
  44. pAnsi,
  45. StringLength + 1,
  46. pUnicode,
  47. StringLength + 1 );
  48. //
  49. // Ensure NULL termination.
  50. //
  51. pUnicode[StringLength] = 0;
  52. return iReturn;
  53. }
  54. int
  55. UnicodeToAnsiString(
  56. LPWSTR pUnicode,
  57. LPSTR pAnsi,
  58. DWORD StringLength
  59. )
  60. {
  61. LPSTR pTempBuf = NULL;
  62. INT rc = 0;
  63. if( StringLength == NULL_TERMINATED ) {
  64. //
  65. // StringLength is just the
  66. // number of characters in the string
  67. //
  68. StringLength = wcslen( pUnicode );
  69. }
  70. //
  71. // WideCharToMultiByte doesn't NULL terminate if we're copying
  72. // just part of the string, so terminate here.
  73. //
  74. pUnicode[StringLength] = 0;
  75. //
  76. // Include one for the NULL
  77. //
  78. StringLength++;
  79. //
  80. // Unfortunately, WideCharToMultiByte doesn't do conversion in place,
  81. // so allocate a temporary buffer, which we can then copy:
  82. //
  83. if( pAnsi == (LPSTR)pUnicode )
  84. {
  85. pTempBuf = (LPSTR)LocalAlloc( LPTR, StringLength );
  86. pAnsi = pTempBuf;
  87. }
  88. if( pAnsi )
  89. {
  90. rc = WideCharToMultiByte( CP_ACP,
  91. 0,
  92. pUnicode,
  93. StringLength,
  94. pAnsi,
  95. StringLength,
  96. NULL,
  97. NULL );
  98. }
  99. /* If pTempBuf is non-null, we must copy the resulting string
  100. * so that it looks as if we did it in place:
  101. */
  102. if( pTempBuf && ( rc > 0 ) )
  103. {
  104. pAnsi = (LPSTR)pUnicode;
  105. strcpy( pAnsi, pTempBuf );
  106. LocalFree( pTempBuf );
  107. }
  108. return rc;
  109. }
  110. LPWSTR
  111. AllocateUnicodeString(
  112. LPSTR pAnsiString
  113. )
  114. {
  115. LPWSTR pUnicodeString = NULL;
  116. if (!pAnsiString)
  117. return NULL;
  118. pUnicodeString = (LPWSTR)LocalAlloc(
  119. LPTR,
  120. strlen(pAnsiString)*sizeof(WCHAR) +sizeof(WCHAR)
  121. );
  122. if (pUnicodeString) {
  123. AnsiToUnicodeString(
  124. pAnsiString,
  125. pUnicodeString,
  126. NULL_TERMINATED
  127. );
  128. }
  129. return pUnicodeString;
  130. }
  131. void
  132. FreeUnicodeString(
  133. LPWSTR pUnicodeString
  134. )
  135. {
  136. if (!pUnicodeString)
  137. return;
  138. LocalFree(pUnicodeString);
  139. return;
  140. }
  141. DWORD
  142. ComputeMaxStrlenW(
  143. LPWSTR pString,
  144. DWORD cchBufMax
  145. )
  146. {
  147. DWORD cchLen;
  148. //
  149. // Include space for the NULL.
  150. //
  151. cchBufMax--;
  152. cchLen = wcslen(pString);
  153. if (cchLen > cchBufMax)
  154. return cchBufMax;
  155. return cchLen;
  156. }
  157. DWORD
  158. ComputeMaxStrlenA(
  159. LPSTR pString,
  160. DWORD cchBufMax
  161. )
  162. {
  163. DWORD cchLen;
  164. //
  165. // Include space for the NULL.
  166. //
  167. cchBufMax--;
  168. cchLen = lstrlenA(pString);
  169. if (cchLen > cchBufMax)
  170. return cchBufMax;
  171. return cchLen;
  172. }