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.

315 lines
8.6 KiB

  1. //============================================================================
  2. // Copyright (C) Microsoft Corporation, 1997- 1999
  3. //
  4. // File: tfschar.h
  5. //
  6. // String functions that are used in general for TFS code.
  7. //
  8. // History:
  9. //
  10. // 05/28/97 Kenn Takara Created.
  11. //
  12. // Declarations for some common code/macros.
  13. //============================================================================
  14. #ifndef _TFSCHAR_H
  15. #define _TFSCHAR_H
  16. #if _MSC_VER >= 1000 // VC 5.0 or later
  17. #pragma once
  18. #endif
  19. #define TFSCORE_API(x) x
  20. //$ Win95: kennt, the list of Unicode-enabled functions on Win95 will
  21. // have to be checked. Does lstrlenW work, but lstrcpyW doesn't? (that's
  22. // what the ATL versions of the functions imply).
  23. /*---------------------------------------------------------------------------
  24. String copy functions
  25. ---------------------------------------------------------------------------*/
  26. // Baisc conversion functions
  27. TFSCORE_API(LPSTR) StrCpyAFromW(LPSTR psz, LPCWSTR pswz);
  28. TFSCORE_API(LPWSTR) StrCpyWFromA(LPWSTR pswz, LPCSTR psz);
  29. TFSCORE_API(LPSTR) StrnCpyAFromW(LPSTR psz, LPCWSTR pswz, int iMax);
  30. TFSCORE_API(LPWSTR) StrnCpyWFromA(LPWSTR pswz, LPCSTR psz, int iMax);
  31. #define StrCpy lstrcpy
  32. #define StrnCpy lstrcpyn
  33. #define StrCpyW lstrcpyW
  34. #define StrCpyA lstrcpyA
  35. #define StrnCpyW lstrcpynW
  36. #define StrnCpyA lstrcpynA
  37. #define StrCpyOle StrCpyW
  38. #define StrnCpyOle StrnCpyW
  39. #ifdef _UNICODE
  40. #define StrCpyAFromT StrCpyAFromW
  41. #define StrCpyTFromA StrCpyWFromA
  42. #define StrCpyTFromW lstrcpy
  43. #define StrCpyWFromT lstrcpy
  44. #define StrnCpyAFromT StrnCpyAFromW
  45. #define StrnCpyTFromA StrnCpyWFromA
  46. #define StrnCpyTFromW lstrcpyn
  47. #define StrnCpyWFromT lstrcpyn
  48. #else
  49. #define StrCpyAFromT lstrcpy
  50. #define StrCpyTFromA lstrcpy
  51. #define StrCpyTFromW StrCpyAFromW
  52. #define StrCpyWFromT StrCpyWFromA
  53. #define StrnCpyAFromT lstrcpyn
  54. #define StrnCpyTFromA lstrcpyn
  55. #define StrnCpyTFromW StrnCpyAFromW
  56. #define StrnCpyWFromT StrnCpyWFromA
  57. #endif
  58. #define StrCpyOleFromT StrCpyWFromT
  59. #define StrCpyTFromOle StrCpyTFromW
  60. #define StrCpyOleFromA StrCpyWFromA
  61. #define StrCpyAFromOle StrCpyAFromW
  62. #define StrCpyWFromOle StrCpyW
  63. #define StrCpyOleFromW StrCpyW
  64. #define StrnCpyOleFromT StrnCpyWFromT
  65. #define StrnCpyTFromOle StrnCpyTFromW
  66. #define StrnCpyOleFromA StrnCpyWFromA
  67. #define StrnCpyAFromOle StrnCpyAFromW
  68. #define StrnCpyOleFromW StrnCpyW
  69. #define StrnCpyWFromOle StrnCpyW
  70. /*---------------------------------------------------------------------------
  71. String length functions
  72. ---------------------------------------------------------------------------*/
  73. #define StrLen lstrlen
  74. #define StrLenA lstrlenA
  75. #define StrLenW lstrlenW
  76. #define StrLenOle StrLenW
  77. //
  78. // CbStrLenA() is inaccurate for DBCS strings! It will return the
  79. // incorrect number of bytes needed.
  80. //
  81. #define CbStrLenA(psz) ((StrLenA(psz)+1)*sizeof(char))
  82. #define CbStrLenW(psz) ((StrLenW(psz)+1)*sizeof(WCHAR))
  83. #ifdef _UNICODE
  84. #define CbStrLen(psz) CbStrLenW(psz)
  85. #else
  86. #define CbStrLen(psz) CbStrLenA(psz)
  87. #endif
  88. // Given a number of characters, this it the minimal number of TCHARs
  89. // that needs to be allocated to hold the string
  90. #define MinTCharNeededForCch(ch) ((ch) * (2/sizeof(TCHAR)))
  91. #define MinCbNeededForCch(ch) (sizeof(TCHAR)*MinTCharNeededForCch(ch))
  92. // Given a cb (count of bytes) this is the maximal number of characters
  93. // that can be in this string
  94. #define MaxCchFromCb(cb) ((cb) / sizeof(TCHAR))
  95. #ifdef _UNICODE
  96. // Given a cb, this is the minimum number of chars found in this string
  97. // MinCchFromCb
  98. #define MinCchFromCb(cb) ((cb) / sizeof(WCHAR))
  99. #else
  100. // Number of characters is only half due to DBCS
  101. #define MinCchFromCb(cb) ((cb) / (2*sizeof(char)))
  102. #endif
  103. /*---------------------------------------------------------------------------
  104. String dup functions
  105. The returned string from these functions must be freed using delete!
  106. ---------------------------------------------------------------------------*/
  107. TFSCORE_API(LPSTR) StrDupA( LPCSTR psz );
  108. TFSCORE_API(LPWSTR) StrDupW( LPCWSTR pws );
  109. TFSCORE_API(LPSTR) StrDupAFromW( LPCWSTR pwsz );
  110. TFSCORE_API(LPWSTR) StrDupWFromA( LPCSTR psz );
  111. #ifdef _UNICODE
  112. #define StrDup StrDupW
  113. #define StrDupTFromW StrDupW
  114. #define StrDupWFromT StrDupW
  115. #define StrDupTFromA StrDupWFromA
  116. #define StrDupAFromT StrDupAFromW
  117. #define StrDupOleFromA StrDupWFromA
  118. #define StrDupAFromOle StrDupAFromW
  119. #define StrDupOleFromW StrDupW
  120. #define StrDupWFromOle StrDupW
  121. #define StrDupOleFromT StrDupOleFromW
  122. #define StrDupTFromOle StrDupWFromOle
  123. #else
  124. #define StrDup StrDupA
  125. #define StrDupTFromA StrDupA
  126. #define StrDupAFromT StrDupA
  127. #define StrDupTFromW StrDupAFromW
  128. #define StrDupWFromT StrDupWFromA
  129. #define StrDupOleFromA StrDupWFromA
  130. #define StrDupAFromOle StrDupAFromW
  131. #define StrDupOleFromW StrDupW
  132. #define StrDupWFromOle StrDupW
  133. #define StrDupOleFromT StrDupOleFromA
  134. #define StrDupTFromOle StrDupAFromOle
  135. #endif
  136. // AllocaStrDup
  137. // AllocaStrDupA
  138. // AllocaStrDupW
  139. //
  140. // These functions will dup a string on the STACK.
  141. //
  142. #define AllocaStrDupA(lpa) (\
  143. ((LPCSTR)lpa == NULL) ? NULL : (\
  144. StrCpyA((LPSTR) alloca(CbStrLenA(lpa)*2), lpa)))
  145. #define AllocaStrDupW(lpw) (\
  146. ((LPCWSTR)lpw == NULL) ? NULL : (\
  147. StrCpyW((LPWSTR) alloca(CbStrLenW(lpw)), lpw)))
  148. #ifdef _UNICODE
  149. #define AllocaStrDup AllocaStrDupW
  150. #else
  151. #define AllocaStrDup AllocaStrDupA
  152. #endif
  153. /*---------------------------------------------------------------------------
  154. String comparison functions
  155. ---------------------------------------------------------------------------*/
  156. #define StrCmpA lstrcmpA
  157. #define StrCmpW lstrcmpW
  158. #define StrCmpOle StrCmpW
  159. TFSCORE_API(int) StrnCmpA(LPCSTR psz1, LPCSTR psz2, int nLen);
  160. TFSCORE_API(int) StrnCmpW(LPCWSTR pswz1, LPCWSTR pswz2, int nLen);
  161. #define StriCmpA lstrcmpiA
  162. #define StriCmpW lstrcmpiW
  163. #define StriCmpOle StriCmpW
  164. TFSCORE_API(int) StrniCmpA(LPCSTR psz1, LPCSTR psz2, int nLen);
  165. TFSCORE_API(int) StrniCmpW(LPCWSTR pswz1, LPCWSTR pswz2, int nLen);
  166. #ifdef _UNICODE
  167. #define StrCmp StrCmpW
  168. #define StrnCmp StrnCmpW
  169. #define StriCmp StriCmpW
  170. #define StrniCmp StrniCmpW
  171. #else
  172. #define StrCmp StrCmpA
  173. #define StrnCmp StrnCmpA
  174. #define StriCmp StriCmpA
  175. #define StrniCmp StrniCmpA
  176. #endif
  177. /*---------------------------------------------------------------------------
  178. String concatenation routines
  179. ---------------------------------------------------------------------------*/
  180. #define StrCatW lstrcatW
  181. #define StrCatA lstrcatA
  182. #ifdef _UNICODE
  183. #define StrCat StrCatW
  184. #else
  185. #define StrCat StrCatA
  186. #endif
  187. /*---------------------------------------------------------------------------
  188. LoadString helpers
  189. ---------------------------------------------------------------------------*/
  190. LPTSTR LoadAndAllocString(UINT ids);
  191. /*---------------------------------------------------------------------------
  192. Get safe strings
  193. ---------------------------------------------------------------------------*/
  194. LPTSTR GetSafeString(LPTSTR psz);
  195. LPWSTR GetSafeStringW(LPWSTR pwsz);
  196. LPSTR GetSafeStringA(LPSTR pasz);
  197. /*---------------------------------------------------------------------------
  198. Local conversion routines (conversions performed on stack!)
  199. ---------------------------------------------------------------------------*/
  200. // Make sure MFC's afxconv.h hasn't already been loaded to do this
  201. #ifndef USES_CONVERSION
  202. #ifndef _DEBUG
  203. #define USES_CONVERSION int _convert; _convert
  204. #else
  205. #define USES_CONVERSION int _convert = 0
  206. #endif
  207. /////////////////////////////////////////////////////////////////////////////
  208. // Global UNICODE<>ANSI translation helpers
  209. LPWSTR WINAPI AtlA2WHelper(LPWSTR lpw, LPCSTR lpa, int nChars);
  210. LPSTR WINAPI AtlW2AHelper(LPSTR lpa, LPCWSTR lpw, int nChars);
  211. #define A2W(lpa) (\
  212. ((LPCSTR)lpa == NULL) ? NULL : (\
  213. _convert = (lstrlenA(lpa)+1),\
  214. AtlA2WHelper((LPWSTR) alloca(_convert*2), lpa, _convert)))
  215. #define W2A(lpw) (\
  216. ((LPCWSTR)lpw == NULL) ? NULL : (\
  217. _convert = (lstrlenW(lpw)+1)*2,\
  218. AtlW2AHelper((LPSTR) alloca(_convert), lpw, _convert)))
  219. #define A2CW(lpa) ((LPCWSTR)A2W(lpa))
  220. #define W2CA(lpw) ((LPCSTR)W2A(lpw))
  221. #ifdef _UNICODE
  222. #define T2A W2A
  223. #define A2T A2W
  224. #define T2W(_lp) (_lp)
  225. #define W2T(_lp) (_lp)
  226. #define T2CA W2CA
  227. #define A2CT A2CW
  228. #define T2CW(_lp) (_lp)
  229. #define W2CT(_lp) (_lp)
  230. #else
  231. #define T2W A2W
  232. #define W2T W2A
  233. #define T2A(_lp) (_lp)
  234. #define A2T(_lp) (_lp)
  235. #define T2CW A2CW
  236. #define W2CT W2CA
  237. #define T2CA(_lp) (_lp)
  238. #define A2CT(_lp) (_lp)
  239. #endif
  240. #endif // !USES_CONVERSION
  241. #endif // _TFSCHAR_H