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.

298 lines
5.0 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 "nds.hxx"
  13. #pragma hdrstop
  14. int
  15. AnsiToUnicodeString(
  16. LPSTR pAnsi,
  17. LPWSTR pUnicode,
  18. DWORD StringLength
  19. )
  20. {
  21. int iReturn;
  22. if( StringLength == NULL_TERMINATED )
  23. StringLength = strlen( pAnsi );
  24. iReturn = MultiByteToWideChar(CP_ACP,
  25. MB_PRECOMPOSED,
  26. pAnsi,
  27. StringLength + 1,
  28. pUnicode,
  29. StringLength + 1 );
  30. //
  31. // Ensure NULL termination.
  32. //
  33. pUnicode[StringLength] = 0;
  34. return iReturn;
  35. }
  36. int
  37. UnicodeToAnsiString(
  38. LPWSTR pUnicode,
  39. LPSTR pAnsi,
  40. DWORD StringLength
  41. )
  42. {
  43. LPSTR pTempBuf = NULL;
  44. INT rc = 0;
  45. if( StringLength == NULL_TERMINATED ) {
  46. //
  47. // StringLength is just the
  48. // number of characters in the string
  49. //
  50. StringLength = wcslen( pUnicode );
  51. }
  52. //
  53. // Include one for the NULL
  54. //
  55. StringLength++;
  56. //
  57. // Unfortunately, WideCharToMultiByte doesn't do conversion in place,
  58. // so allocate a temporary buffer, which we can then copy:
  59. //
  60. if( pAnsi == (LPSTR)pUnicode )
  61. {
  62. pTempBuf = (LPSTR)AllocADsMem( StringLength );
  63. pAnsi = pTempBuf;
  64. }
  65. if( pAnsi )
  66. {
  67. rc = WideCharToMultiByte( CP_ACP,
  68. 0,
  69. pUnicode,
  70. StringLength,
  71. pAnsi,
  72. StringLength,
  73. NULL,
  74. NULL );
  75. }
  76. //
  77. // Null terminate
  78. //
  79. pAnsi[StringLength-1] = 0;
  80. /* If pTempBuf is non-null, we must copy the resulting string
  81. * so that it looks as if we did it in place:
  82. */
  83. if( pTempBuf && ( rc > 0 ) )
  84. {
  85. pAnsi = (LPSTR)pUnicode;
  86. strcpy( pAnsi, pTempBuf );
  87. LocalFree( pTempBuf );
  88. }
  89. return rc;
  90. }
  91. LPWSTR
  92. AllocateUnicodeString(
  93. LPSTR pAnsiString
  94. )
  95. {
  96. LPWSTR pUnicodeString = NULL;
  97. if (!pAnsiString)
  98. return NULL;
  99. pUnicodeString = (LPWSTR)AllocADsMem(
  100. strlen(pAnsiString)*sizeof(WCHAR) +sizeof(WCHAR)
  101. );
  102. if (pUnicodeString) {
  103. AnsiToUnicodeString(
  104. pAnsiString,
  105. pUnicodeString,
  106. NULL_TERMINATED
  107. );
  108. }
  109. return pUnicodeString;
  110. }
  111. void
  112. FreeUnicodeString(
  113. LPWSTR pUnicodeString
  114. )
  115. {
  116. if (!pUnicodeString)
  117. return;
  118. LocalFree(pUnicodeString);
  119. return;
  120. }
  121. LPSTR
  122. AllocateAnsiString(
  123. LPWSTR pUnicodeString
  124. )
  125. {
  126. LPSTR pAnsiString = NULL;
  127. if (!pUnicodeString)
  128. return NULL;
  129. pAnsiString = (LPSTR) AllocADsMem(
  130. wcslen(pUnicodeString)*sizeof(CHAR) +sizeof(CHAR)
  131. );
  132. if (pAnsiString) {
  133. UnicodeToAnsiString(
  134. pUnicodeString,
  135. pAnsiString,
  136. NULL_TERMINATED
  137. );
  138. }
  139. return pAnsiString;
  140. }
  141. void
  142. FreeAnsiString(
  143. LPSTR pAnsiString
  144. )
  145. {
  146. if (!pAnsiString)
  147. return;
  148. LocalFree(pAnsiString);
  149. return;
  150. }
  151. LPSTR*
  152. AllocateAnsiStringArray(
  153. LPWSTR *ppUnicodeStrings,
  154. DWORD dwNumElements
  155. )
  156. {
  157. LPSTR *ppAnsiStrings = NULL;
  158. DWORD i, j;
  159. if (ppUnicodeStrings && dwNumElements) {
  160. ppAnsiStrings = (LPSTR *) AllocADsMem(sizeof(LPSTR) * dwNumElements);
  161. if (!ppAnsiStrings) {
  162. goto error;
  163. }
  164. for (i=0; i < dwNumElements; i++) {
  165. ppAnsiStrings[i] = AllocateAnsiString(ppUnicodeStrings[i]);
  166. if (!ppAnsiStrings[i]) {
  167. for (j=0; j<i; j++)
  168. FreeADsMem(ppAnsiStrings[j]);
  169. FreeADsMem(ppAnsiStrings);
  170. goto error;
  171. }
  172. }
  173. }
  174. return ppAnsiStrings;
  175. error:
  176. return NULL;
  177. }
  178. void
  179. FreeAnsiStringArray(
  180. LPSTR *ppAnsiStrings,
  181. DWORD dwNumElements
  182. )
  183. {
  184. DWORD i;
  185. if (!ppAnsiStrings) {
  186. return;
  187. }
  188. for (i=0; i < dwNumElements; i++) {
  189. FreeADsMem(ppAnsiStrings[i]);
  190. }
  191. FreeADsMem(ppAnsiStrings);
  192. }
  193. DWORD
  194. ComputeMaxStrlenW(
  195. LPWSTR pString,
  196. DWORD cchBufMax
  197. )
  198. {
  199. DWORD cchLen;
  200. //
  201. // Include space for the NULL.
  202. //
  203. cchBufMax--;
  204. cchLen = wcslen(pString);
  205. if (cchLen > cchBufMax)
  206. return cchBufMax;
  207. return cchLen;
  208. }
  209. DWORD
  210. ComputeMaxStrlenA(
  211. LPSTR pString,
  212. DWORD cchBufMax
  213. )
  214. {
  215. DWORD cchLen;
  216. //
  217. // Include space for the NULL.
  218. //
  219. cchBufMax--;
  220. cchLen = lstrlenA(pString);
  221. if (cchLen > cchBufMax)
  222. return cchBufMax;
  223. return cchLen;
  224. }