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.

193 lines
3.5 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 "nwcompat.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. // Unfortunately, WideCharToMultiByte doesn't do conversion in place,
  55. // so allocate a temporary buffer, which we can then copy:
  56. //
  57. if( pAnsi == (LPSTR)pUnicode )
  58. {
  59. pTempBuf = (LPSTR)LocalAlloc( LPTR, StringLength+1 );
  60. pAnsi = pTempBuf;
  61. }
  62. if( pAnsi )
  63. {
  64. rc = WideCharToMultiByte( CP_ACP,
  65. 0,
  66. pUnicode,
  67. StringLength,
  68. pAnsi,
  69. StringLength,
  70. NULL,
  71. NULL );
  72. }
  73. //
  74. // Null terminate
  75. //
  76. pAnsi[StringLength] = '\0';
  77. /* If pTempBuf is non-null, we must copy the resulting string
  78. * so that it looks as if we did it in place:
  79. */
  80. if( pTempBuf && ( rc > 0 ) )
  81. {
  82. pAnsi = (LPSTR)pUnicode;
  83. strcpy( pAnsi, pTempBuf );
  84. LocalFree( pTempBuf );
  85. }
  86. return rc;
  87. }
  88. LPWSTR
  89. AllocateUnicodeString(
  90. LPSTR pAnsiString
  91. )
  92. {
  93. LPWSTR pUnicodeString = NULL;
  94. if (!pAnsiString)
  95. return NULL;
  96. pUnicodeString = (LPWSTR)LocalAlloc(
  97. LPTR,
  98. strlen(pAnsiString)*sizeof(WCHAR) +sizeof(WCHAR)
  99. );
  100. if (pUnicodeString) {
  101. AnsiToUnicodeString(
  102. pAnsiString,
  103. pUnicodeString,
  104. NULL_TERMINATED
  105. );
  106. }
  107. return pUnicodeString;
  108. }
  109. void
  110. FreeUnicodeString(
  111. LPWSTR pUnicodeString
  112. )
  113. {
  114. if (!pUnicodeString)
  115. return;
  116. LocalFree(pUnicodeString);
  117. return;
  118. }
  119. DWORD
  120. ComputeMaxStrlenW(
  121. LPWSTR pString,
  122. DWORD cchBufMax
  123. )
  124. {
  125. DWORD cchLen;
  126. //
  127. // Include space for the NULL.
  128. //
  129. cchBufMax--;
  130. cchLen = wcslen(pString);
  131. if (cchLen > cchBufMax)
  132. return cchBufMax;
  133. return cchLen;
  134. }
  135. DWORD
  136. ComputeMaxStrlenA(
  137. LPSTR pString,
  138. DWORD cchBufMax
  139. )
  140. {
  141. DWORD cchLen;
  142. //
  143. // Include space for the NULL.
  144. //
  145. cchBufMax--;
  146. cchLen = lstrlenA(pString);
  147. if (cchLen > cchBufMax)
  148. return cchBufMax;
  149. return cchLen;
  150. }