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.

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