Leaked source code of windows server 2003
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.

301 lines
6.4 KiB

  1. #include <windows.h>
  2. #include <stdlib.h>
  3. #include "mplayer.h"
  4. #include "unicode.h"
  5. /* AnsiToUnicodeString
  6. *
  7. * Parameters:
  8. *
  9. * pAnsi - A valid source ANSI string.
  10. *
  11. * pUnicode - A pointer to a buffer large enough to accommodate
  12. * the converted string.
  13. *
  14. * StringLength - The length of the source ANSI string
  15. * excluding the null terminator. This value may be
  16. * UNKNOWN_LENGTH (-1).
  17. *
  18. *
  19. * Return:
  20. *
  21. * The return value from MultiByteToWideChar, the number of
  22. * wide characters returned.
  23. *
  24. *
  25. * andrewbe, 11 Jan 1993
  26. *
  27. * andrewbe, 01 Feb 1994: Added support for in-place conversion
  28. */
  29. INT AnsiToUnicodeString( LPCSTR pAnsi, LPWSTR pUnicode, INT StringLength )
  30. {
  31. #ifdef IN_PLACE
  32. /* #def'ed out, 'cos it turned out I didn't need it.
  33. * It might be useful sometime, however.
  34. * BUT NOTE: COMPLETELY UNTESTED
  35. */
  36. LPWSTR pTemp
  37. LPWSTR pSave;
  38. #endif
  39. INT rc;
  40. if( !pAnsi )
  41. {
  42. DPF( "NULL pointer passed to AnsiToUnicodeString\n" );
  43. return 0;
  44. }
  45. if( StringLength == UNKNOWN_LENGTH )
  46. StringLength = strlen( pAnsi );
  47. #ifdef IN_PLACE
  48. /* Allow in-place conversion. We assume that the buffer is big enough.
  49. * MultiByteToWideChar doesn't support this.
  50. */
  51. if( pAnsi == (LPCSTR)pUnicode )
  52. {
  53. pTemp = AllocMem( StringLength * sizeof( WCHAR ) + sizeof( WCHAR ) );
  54. if( !pTemp )
  55. return 0;
  56. pSave = pUnicode;
  57. pUnicode = pTemp;
  58. }
  59. #endif
  60. rc = MultiByteToWideChar( CP_ACP,
  61. MB_PRECOMPOSED,
  62. pAnsi,
  63. StringLength + 1,
  64. pUnicode,
  65. StringLength + 1 );
  66. #ifdef IN_PLACE
  67. if( pAnsi == (LPCSTR)pUnicode )
  68. {
  69. pTemp = pUnicode;
  70. pUnicode = pSave;
  71. lstrcpyW( pUnicode, pTemp );
  72. FreeMem( pTemp, StringLength * sizeof( WCHAR ) + sizeof( WCHAR ) );
  73. }
  74. #endif
  75. return rc;
  76. }
  77. /* AllocateUnicodeString
  78. *
  79. * Parameter:
  80. *
  81. * pAnsi - A valid source ANSI string.
  82. *
  83. * Return:
  84. *
  85. * A Unicode copy of the supplied ANSI string.
  86. * NULL if pAnsi is NULL or the allocation or conversion fails.
  87. *
  88. * andrewbe, 27 Jan 1994
  89. */
  90. LPWSTR AllocateUnicodeString( LPCSTR pAnsi )
  91. {
  92. LPWSTR pUnicode;
  93. INT Length;
  94. if( !pAnsi )
  95. {
  96. DPF( "NULL pointer passed to AllocateUnicodeString\n" );
  97. return NULL;
  98. }
  99. Length = strlen( pAnsi );
  100. pUnicode = AllocMem( Length * sizeof( WCHAR ) + sizeof( WCHAR ) );
  101. if( pUnicode )
  102. {
  103. if( 0 == AnsiToUnicodeString( pAnsi, pUnicode, Length ) )
  104. {
  105. FreeMem( pUnicode, Length * sizeof( WCHAR ) + sizeof( WCHAR ) );
  106. pUnicode = NULL;
  107. }
  108. }
  109. return pUnicode;
  110. }
  111. /* FreeUnicodeString
  112. *
  113. * Parameter:
  114. *
  115. * pString - A valid source Unicode string.
  116. *
  117. * Return:
  118. *
  119. * TRUE if the string was successfully freed, FALSE otherwise.
  120. *
  121. * andrewbe, 27 Jan 1994
  122. */
  123. VOID FreeUnicodeString( LPWSTR pString )
  124. {
  125. if( !pString )
  126. {
  127. DPF( "NULL pointer passed to FreeUnicodeString\n" );
  128. return;
  129. }
  130. FreeMem( pString, wcslen( pString ) * sizeof( WCHAR ) + sizeof( WCHAR ) );
  131. }
  132. /* UnicodeStringToNumber
  133. *
  134. * Parameter:
  135. *
  136. * pString - A valid source Unicode string.
  137. *
  138. * Return:
  139. *
  140. * The integer value represented by the string.
  141. *
  142. * andrewbe, 27 Jan 1994
  143. */
  144. #define BUF_LEN 265
  145. int UnicodeStringToNumber( LPCWSTR pString )
  146. {
  147. CHAR strAnsi[BUF_LEN];
  148. #ifdef DEBUG
  149. if( ( wcslen( pString ) + 1 ) > BUF_LEN )
  150. {
  151. DPF( "Buffer cannot accommodate string passed to UnicodeStringToNumber\n" );
  152. }
  153. #endif
  154. WideCharToMultiByte( CP_ACP, 0, pString, -1, strAnsi,
  155. sizeof strAnsi, NULL, NULL );
  156. return atoi( strAnsi );
  157. }
  158. #ifndef UNICODE
  159. /* UnicodeToAnsiString
  160. *
  161. * Parameters:
  162. *
  163. * pUnicode - A valid source Unicode string.
  164. *
  165. * pANSI - A pointer to a buffer large enough to accommodate
  166. * the converted string.
  167. *
  168. * StringLength - The length of the source Unicode string.
  169. * If 0 (NULL_TERMINATED), the string is assumed to be
  170. * null-terminated.
  171. *
  172. * Return:
  173. *
  174. * The return value from WideCharToMultiByte, the number of
  175. * multi-byte characters returned.
  176. *
  177. *
  178. * andrewbe, 11 Jan 1993
  179. */
  180. INT UnicodeToAnsiString( LPCWSTR pUnicode, LPSTR pAnsi, INT StringLength )
  181. {
  182. INT rc = 0;
  183. if( StringLength == UNKNOWN_LENGTH )
  184. StringLength = wcslen( pUnicode );
  185. if( pAnsi )
  186. {
  187. rc = WideCharToMultiByte( CP_ACP,
  188. 0,
  189. pUnicode,
  190. StringLength + 1,
  191. pAnsi,
  192. StringLength + 1,
  193. NULL,
  194. NULL );
  195. }
  196. return rc;
  197. }
  198. /* AllocateAnsiString
  199. *
  200. * Parameter:
  201. *
  202. * pAnsi - A valid source Unicode string.
  203. *
  204. * Return:
  205. *
  206. * An ANSI copy of the supplied Unicode string.
  207. * NULL if pUnicode is NULL or the allocation or conversion fails.
  208. *
  209. * andrewbe, 27 Jan 1994
  210. */
  211. LPSTR AllocateAnsiString( LPCWSTR pUnicode )
  212. {
  213. LPSTR pAnsi;
  214. INT Length;
  215. if( !pUnicode )
  216. {
  217. DPF( "NULL pointer passed to AllocateUnicodeString\n" );
  218. return NULL;
  219. }
  220. Length = wcslen( pUnicode );
  221. pAnsi = AllocMem( Length * sizeof( CHAR ) + sizeof( CHAR ) );
  222. if( pAnsi )
  223. {
  224. if( 0 == UnicodeToAnsiString( pUnicode, pAnsi, Length ) )
  225. {
  226. FreeMem( pAnsi, Length * sizeof( CHAR ) + sizeof( CHAR ) );
  227. pAnsi = NULL;
  228. }
  229. }
  230. return pAnsi;
  231. }
  232. /* FreeUnicodeString
  233. *
  234. * Parameter:
  235. *
  236. * pString - A valid source Unicode string.
  237. *
  238. * Return:
  239. *
  240. * TRUE if the string was successfully freed, FALSE otherwise.
  241. *
  242. * andrewbe, 27 Jan 1994
  243. */
  244. VOID FreeAnsiString( LPSTR pString )
  245. {
  246. if( !pString )
  247. {
  248. DPF( "NULL pointer passed to FreeAnsiString\n" );
  249. return;
  250. }
  251. FreeMem( pString, strlen( pString ) * sizeof( CHAR ) + sizeof( CHAR ) );
  252. }
  253. #endif /* NOT UNICODE */
  254.