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.

291 lines
7.2 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: tfschar.c
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "precomp.h"
  11. #include <malloc.h>
  12. /*!--------------------------------------------------------------------------
  13. StrCpyAFromW
  14. -
  15. Author: KennT
  16. ---------------------------------------------------------------------------*/
  17. TFSCORE_API(LPSTR) StrCpyAFromW(LPSTR psz, LPCWSTR pswz)
  18. {
  19. USES_CONVERSION;
  20. return StrCpyA(psz, W2CA(pswz));
  21. }
  22. /*!--------------------------------------------------------------------------
  23. StrCpyWFromA
  24. -
  25. Author: KennT
  26. ---------------------------------------------------------------------------*/
  27. TFSCORE_API(LPWSTR) StrCpyWFromA(LPWSTR pswz, LPCSTR psz)
  28. {
  29. USES_CONVERSION;
  30. return StrCpyW(pswz, A2CW(psz));
  31. }
  32. /*!--------------------------------------------------------------------------
  33. StrnCpyAFromW
  34. -
  35. Author: KennT
  36. ---------------------------------------------------------------------------*/
  37. TFSCORE_API(LPSTR) StrnCpyAFromW(LPSTR psz, LPCWSTR pswz, int iMax)
  38. {
  39. USES_CONVERSION;
  40. return StrnCpyA(psz, W2CA(pswz), iMax);
  41. }
  42. /*!--------------------------------------------------------------------------
  43. StrnCpyWFromA
  44. -
  45. Author: KennT
  46. ---------------------------------------------------------------------------*/
  47. TFSCORE_API(LPWSTR) StrnCpyWFromA(LPWSTR pswz, LPCSTR psz, int iMax)
  48. {
  49. USES_CONVERSION;
  50. return StrnCpyW(pswz, A2CW(psz), iMax);
  51. }
  52. /*!--------------------------------------------------------------------------
  53. StrDupA
  54. -
  55. Author: KennT
  56. ---------------------------------------------------------------------------*/
  57. TFSCORE_API(LPSTR) StrDupA( LPCSTR psz )
  58. {
  59. if (psz)
  60. {
  61. // Multiply by 2 to account for DBCS strings
  62. LPSTR pszcpy = Malloc(sizeof(char)*CbStrLenA(psz)*2);
  63. if (pszcpy)
  64. return StrCpyA(pszcpy, psz);
  65. }
  66. return NULL;
  67. }
  68. /*!--------------------------------------------------------------------------
  69. StrDupW
  70. -
  71. Author: KennT
  72. ---------------------------------------------------------------------------*/
  73. TFSCORE_API(LPWSTR) StrDupW( LPCWSTR pswz )
  74. {
  75. if (pswz)
  76. {
  77. LPWSTR pswzcpy = Malloc(sizeof(WCHAR)*CbStrLenW(pswz));
  78. if (pswzcpy)
  79. return StrCpyW(pswzcpy, pswz);
  80. }
  81. return NULL;
  82. }
  83. /*!--------------------------------------------------------------------------
  84. StrDupAFromW
  85. -
  86. Author: KennT
  87. ---------------------------------------------------------------------------*/
  88. TFSCORE_API(LPSTR) StrDupAFromW( LPCWSTR pwsz )
  89. {
  90. USES_CONVERSION;
  91. return StrDupA( W2CA(pwsz) );
  92. }
  93. /*!--------------------------------------------------------------------------
  94. StrDupWFromA
  95. -
  96. Author: KennT
  97. ---------------------------------------------------------------------------*/
  98. TFSCORE_API(LPWSTR) StrDupWFromA( LPCSTR psz )
  99. {
  100. USES_CONVERSION;
  101. return StrDupW( A2CW(psz) );
  102. }
  103. /*!--------------------------------------------------------------------------
  104. StrnCmpA
  105. -
  106. Author: KennT
  107. ---------------------------------------------------------------------------*/
  108. TFSCORE_API(int) StrnCmpA(LPCSTR psz1, LPCSTR psz2, int nLen)
  109. {
  110. USES_CONVERSION;
  111. // It's easier to convert it to a wide string than do the
  112. // conversion. (it's a pain having to deal with DBCS characters).
  113. return StrnCmpW(A2CW(psz1), A2CW(psz2), nLen);
  114. }
  115. /*!--------------------------------------------------------------------------
  116. StrnCmpW
  117. -
  118. Author: KennT
  119. ---------------------------------------------------------------------------*/
  120. TFSCORE_API(int) StrnCmpW(LPCWSTR pswz1, LPCWSTR pswz2, int nLen)
  121. {
  122. WCHAR *pswz1Temp = AllocaStrDupW(pswz1); // These memory allocs get memory on the stack, so we do not need to free them
  123. WCHAR *pswz2Temp = AllocaStrDupW(pswz2);
  124. // The next three if statements could be replaced with: if( !pswz1Temp || !pswz2Temp) return StrCmpW(pswz1Temp, pswz2Temp);
  125. // since lstrcmp can handle NULL parameters. But if we do this prefix gets mad.
  126. //
  127. if( pswz1Temp == NULL && pswz2Temp == NULL )
  128. {
  129. // They are equal both NULL
  130. //
  131. return 0;
  132. }
  133. if( pswz1Temp == NULL)
  134. {
  135. // The first one is NULL thus the second string is greater
  136. //
  137. return -1;
  138. }
  139. if( pswz2Temp == NULL )
  140. {
  141. // The second one is NULL thus the first one is bigger
  142. //
  143. return 1;
  144. }
  145. if (pswz1Temp != NULL && StrLenW(pswz1Temp) > nLen)
  146. pswz1Temp[nLen] = 0;
  147. if (pswz2Temp != NULL && StrLenW(pswz2Temp) > nLen)
  148. pswz2Temp[nLen] = 0;
  149. return StrCmpW(pswz1Temp, pswz2Temp);
  150. }
  151. /*!--------------------------------------------------------------------------
  152. StrniCmpA
  153. -
  154. Author: KennT
  155. ---------------------------------------------------------------------------*/
  156. TFSCORE_API(int) StrniCmpA(LPCSTR psz1, LPCSTR psz2, int nLen)
  157. {
  158. CHAR *psz1Temp = AllocaStrDupA(psz1);
  159. CHAR *psz2Temp = AllocaStrDupA(psz2);
  160. CharUpperBuffA(psz1Temp, StrLenA(psz1Temp));
  161. CharUpperBuffA(psz2Temp, StrLenA(psz2Temp));
  162. return StrnCmpA(psz1Temp, psz2Temp, nLen);
  163. }
  164. /*!--------------------------------------------------------------------------
  165. StrniCmpW
  166. -
  167. Author: KennT
  168. ---------------------------------------------------------------------------*/
  169. TFSCORE_API(int) StrniCmpW(LPCWSTR pswz1, LPCWSTR pswz2, int nLen)
  170. {
  171. WCHAR *pswz1Temp = AllocaStrDupW(pswz1);
  172. WCHAR *pswz2Temp = AllocaStrDupW(pswz2);
  173. CharUpperBuffW(pswz1Temp, StrLenW(pswz1Temp));
  174. CharUpperBuffW(pswz2Temp, StrLenW(pswz2Temp));
  175. return StrnCmpW(pswz1Temp, pswz2Temp, nLen);
  176. }
  177. /////////////////////////////////////////////////////////////////////////////
  178. // Global UNICODE<>ANSI translation helpers
  179. LPWSTR WINAPI AtlA2WHelper(LPWSTR lpw, LPCSTR lpa, int nChars)
  180. {
  181. assert(lpa != NULL);
  182. assert(lpw != NULL);
  183. // verify that no illegal character present
  184. // since lpw was allocated based on the size of lpa
  185. // don't worry about the number of chars
  186. lpw[0] = '\0';
  187. MultiByteToWideChar(CP_ACP, 0, lpa, -1, lpw, nChars);
  188. return lpw;
  189. }
  190. LPSTR WINAPI AtlW2AHelper(LPSTR lpa, LPCWSTR lpw, int nChars)
  191. {
  192. assert(lpw != NULL);
  193. assert(lpa != NULL);
  194. // verify that no illegal character present
  195. // since lpa was allocated based on the size of lpw
  196. // don't worry about the number of chars
  197. lpa[0] = '\0';
  198. WideCharToMultiByte(CP_ACP, 0, lpw, -1, lpa, nChars, NULL, NULL);
  199. return lpa;
  200. }
  201. LPTSTR LoadAndAllocString(UINT ids)
  202. {
  203. TCHAR * psz = NULL;
  204. TCHAR * pszT = NULL;
  205. INT cch = 0;
  206. int iRet;
  207. cch = 64;
  208. psz = Malloc(64*sizeof(TCHAR));
  209. if (psz == NULL)
  210. return NULL;
  211. iRet = LoadString(NULL, ids, psz, cch);
  212. if (iRet == 0)
  213. {
  214. // couldn't find the string
  215. Free(psz);
  216. return NULL;
  217. }
  218. while (iRet >= (cch - 1))
  219. {
  220. cch += 64;
  221. pszT = Realloc(psz, (cch*sizeof(TCHAR)));
  222. if (pszT == NULL)
  223. {
  224. Free(psz);
  225. return NULL;
  226. }
  227. psz = pszT;
  228. iRet = LoadString(NULL, ids, psz, cch);
  229. }
  230. return psz;
  231. }
  232. LPTSTR GetSafeString(LPTSTR psz)
  233. {
  234. static LPTSTR s_szEmpty = _T("");
  235. return psz ? psz : s_szEmpty;
  236. }
  237. LPWSTR GetSafeStringW(LPWSTR pwsz)
  238. {
  239. static LPWSTR s_wszEmpty = L"";
  240. return pwsz ? pwsz : s_wszEmpty;
  241. }
  242. LPSTR GetSafeStringA(LPSTR pasz)
  243. {
  244. static LPSTR s_aszEmpty = "";
  245. return pasz ? pasz : s_aszEmpty;
  246. }