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.

227 lines
6.4 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. conv.h
  5. Abstract:
  6. UNICODE / ASCII conversion macros
  7. Author:
  8. Hakki T. Bostanci (hakkib) 5-Aug-1998
  9. Revision History:
  10. --*/
  11. #ifndef __CONV_H__
  12. #define __CONV_H__
  13. //////////////////////////////////////////////////////////////////////////
  14. //
  15. // cross dependencies
  16. //
  17. #include <winnt.h>
  18. #include <winbase.h>
  19. #include <winnls.h>
  20. #include <malloc.h>
  21. //////////////////////////////////////////////////////////////////////////
  22. //
  23. // in case MFC or ATL conversion helpers are already defined, undef these
  24. //
  25. #undef USES_CONVERSION
  26. #undef W2A
  27. #undef A2W
  28. #undef T2A
  29. #undef A2T
  30. #undef T2W
  31. #undef W2T
  32. #undef T2O
  33. #undef O2T
  34. #undef T2DA
  35. #undef A2DT
  36. #undef T2DW
  37. #undef W2DT
  38. #undef _wcsdupa
  39. #undef _strdupa
  40. #undef _tcsdupa
  41. // USES_CONVERSION must be defined in every function that uses the
  42. // conversion macros
  43. #define USES_CONVERSION int __nLength; PCWSTR __pUnicode; PCSTR __pAscii
  44. //////////////////////////////////////////////////////////////////////////
  45. //
  46. // W2A
  47. //
  48. // Routine Description:
  49. // Converts a UNICODE string to ASCII. Allocates the coversion buffer
  50. // off the stack using _alloca
  51. //
  52. // Arguments:
  53. // pStr UNICODE string
  54. //
  55. // Return Value:
  56. // the converted ASCII string
  57. //
  58. #define W2A(pStr) \
  59. \
  60. ((__pUnicode = pStr) == 0 ? (PSTR) 0 : ( \
  61. \
  62. __nLength = WideCharToMultiByte( \
  63. CP_ACP, \
  64. 0, \
  65. __pUnicode, \
  66. -1, \
  67. 0, \
  68. 0, \
  69. 0, \
  70. 0 \
  71. ), \
  72. \
  73. __pAscii = (PCSTR) _alloca(__nLength * sizeof(CHAR)), \
  74. \
  75. WideCharToMultiByte( \
  76. CP_ACP, \
  77. 0, \
  78. __pUnicode, \
  79. -1, \
  80. (PSTR) __pAscii, \
  81. __nLength, \
  82. 0, \
  83. 0 \
  84. ), \
  85. \
  86. (PSTR) __pAscii)) \
  87. //////////////////////////////////////////////////////////////////////////
  88. //
  89. // A2W
  90. //
  91. // Routine Description:
  92. // Converts an ASCII string to UNICODE. Allocates the coversion buffer
  93. // off the stack using _alloca
  94. //
  95. // Arguments:
  96. // pStr ASCII string
  97. //
  98. // Return Value:
  99. // the converted UNICODE string
  100. //
  101. #define A2W(pStr) \
  102. \
  103. ((__pAscii = pStr) == 0 ? (PWSTR) 0 : ( \
  104. \
  105. __nLength = MultiByteToWideChar( \
  106. CP_ACP, \
  107. MB_PRECOMPOSED, \
  108. __pAscii, \
  109. -1, \
  110. 0, \
  111. 0 \
  112. ), \
  113. \
  114. __pUnicode = (PCWSTR) _alloca(__nLength * sizeof(WCHAR)), \
  115. \
  116. MultiByteToWideChar( \
  117. CP_ACP, \
  118. MB_PRECOMPOSED, \
  119. __pAscii, \
  120. -1, \
  121. (PWSTR) __pUnicode, \
  122. __nLength \
  123. ), \
  124. \
  125. (PWSTR) __pUnicode)) \
  126. //////////////////////////////////////////////////////////////////////////
  127. //
  128. // _tcsdupa
  129. //
  130. // Routine Description:
  131. // Duplicates a string to a buffer allocated off the stack using _alloca
  132. //
  133. // Arguments:
  134. // pStr input string
  135. //
  136. // Return Value:
  137. // duplicated string
  138. //
  139. #define _wcsdupa(pStr) \
  140. \
  141. (__pAscii, (__pUnicode = pStr) == 0 ? (PWSTR) 0 : ( \
  142. \
  143. __nLength = wcslen(__pUnicode) + 1, \
  144. \
  145. lstrcpyW((PWSTR) _alloca(__nLength * sizeof(WCHAR)), __pUnicode))) \
  146. #define _strdupa(pStr) \
  147. \
  148. (__pUnicode, (__pAscii = pStr) == 0 ? (PSTR) 0 : ( \
  149. \
  150. __nLength = strlen(__pAscii) + 1, \
  151. \
  152. lstrcpyA((PSTR) _alloca(__nLength * sizeof(CHAR)), __pAscii))) \
  153. #ifdef UNICODE
  154. #define _tcsdupa _wcsdupa
  155. #else
  156. #define _tcsdupa _strdupa
  157. #endif
  158. //////////////////////////////////////////////////////////////////////////
  159. //
  160. // T2A, A2T, T2W, W2T, T2O, O2T, T2DA, A2DT, T2DW, W2DT
  161. //
  162. // Routine Description:
  163. // These macros expand to the corresponding correct form according to the
  164. // #definition of UNICODE.
  165. //
  166. // We use the cryptic form (__nLength, __pAscii, __pUnicode, pStr) to avoid
  167. // the compiler warning "symbol defined but not used" due to the variables
  168. // defined in USES_CONVERSION macro.
  169. //
  170. #ifdef UNICODE
  171. #define T2A(pStr) W2A(pStr)
  172. #define A2T(pStr) A2W(pStr)
  173. #define T2W(pStr) (__nLength, __pAscii, __pUnicode, pStr)
  174. #define W2T(pStr) (__nLength, __pAscii, __pUnicode, pStr)
  175. #define T2O(pStr) W2A(pStr)
  176. #define O2T(pStr) A2W(pStr)
  177. #define T2DA(pStr) W2A(pStr)
  178. #define A2DT(pStr) A2W(pStr)
  179. #define T2DW(pStr) _wcsdupa(pStr)
  180. #define W2DT(pStr) _wcsdupa(pStr)
  181. typedef CHAR OCHAR, *LPOSTR, *POSTR;
  182. typedef CONST CHAR *LPCOSTR, *PCOSTR;
  183. #else //UNICODE
  184. #define T2A(pStr) (__nLength, __pAscii, __pUnicode, pStr)
  185. #define A2T(pStr) (__nLength, __pAscii, __pUnicode, pStr)
  186. #define T2W(pStr) A2W(pStr)
  187. #define W2T(pStr) W2A(pStr)
  188. #define T2O(pStr) A2W(pStr)
  189. #define O2T(pStr) W2A(pStr)
  190. #define T2DA(pStr) _strdupa(pStr)
  191. #define A2DT(pStr) _strdupa(pStr)
  192. #define T2DW(pStr) A2W(pStr)
  193. #define W2DT(pStr) W2A(pStr)
  194. typedef WCHAR OCHAR, *LPOSTR, *POSTR;
  195. typedef CONST WCHAR *LPCOSTR, *PCOSTR;
  196. #endif //UNICODE
  197. #endif //__CONV_H__