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.

189 lines
5.7 KiB

  1. //Copyright (c) 1998 - 1999 Microsoft Corporation
  2. /*++
  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 USES_CONVERSION
  12. // USES_CONVERSION must be defined in every function that uses the
  13. // conversion macros
  14. #define USES_CONVERSION int __nLength; PCWSTR __pUnicode; PCSTR __pAscii
  15. //////////////////////////////////////////////////////////////////////////
  16. //
  17. // W2A
  18. //
  19. // Routine Description:
  20. // Converts a UNICODE string to ASCII. Allocates the coversion buffer
  21. // off the stack using _alloca
  22. //
  23. // Arguments:
  24. // pStr UNICODE string
  25. //
  26. // Return Value:
  27. // the converted ASCII string
  28. //
  29. #define W2A(pStr) \
  30. \
  31. ((__pUnicode = pStr) == 0 ? (PSTR) 0 : ( \
  32. \
  33. __nLength = WideCharToMultiByte( \
  34. CP_ACP, \
  35. 0, \
  36. __pUnicode, \
  37. -1, \
  38. 0, \
  39. 0, \
  40. 0, \
  41. 0 \
  42. ), \
  43. \
  44. __pAscii = (PCSTR) _alloca(__nLength * sizeof(CHAR)), \
  45. \
  46. WideCharToMultiByte( \
  47. CP_ACP, \
  48. 0, \
  49. __pUnicode, \
  50. -1, \
  51. (PSTR) __pAscii, \
  52. __nLength, \
  53. 0, \
  54. 0 \
  55. ), \
  56. \
  57. (PSTR) __pAscii)) \
  58. //////////////////////////////////////////////////////////////////////////
  59. //
  60. // A2W
  61. //
  62. // Routine Description:
  63. // Converts an ASCII string to UNICODE. Allocates the coversion buffer
  64. // off the stack using _alloca
  65. //
  66. // Arguments:
  67. // pStr ASCII string
  68. //
  69. // Return Value:
  70. // the converted UNICODE string
  71. //
  72. #define A2W(pStr) \
  73. \
  74. ((__pAscii = pStr) == 0 ? (PWSTR) 0 : ( \
  75. \
  76. __nLength = MultiByteToWideChar( \
  77. CP_ACP, \
  78. MB_PRECOMPOSED, \
  79. __pAscii, \
  80. -1, \
  81. 0, \
  82. 0 \
  83. ), \
  84. \
  85. __pUnicode = (PCWSTR) _alloca(__nLength * sizeof(WCHAR)), \
  86. \
  87. MultiByteToWideChar( \
  88. CP_ACP, \
  89. MB_PRECOMPOSED, \
  90. __pAscii, \
  91. -1, \
  92. (PWSTR) __pUnicode, \
  93. __nLength \
  94. ), \
  95. \
  96. (PWSTR) __pUnicode)) \
  97. //////////////////////////////////////////////////////////////////////////
  98. //
  99. // T2A, A2T, T2W, W2T
  100. //
  101. // Routine Description:
  102. // These macros expand to the corresponding correct form according to the
  103. // #definition of UNICODE.
  104. //
  105. // We use the cryptic form (__nLength, __pAscii, __pUnicode, pStr) to avoid
  106. // the compiler warning "symbol defined but not used" due to the variables
  107. // defined in USES_CONVERSION macro.
  108. //
  109. #ifdef UNICODE
  110. #define T2A(pStr) W2A(pStr)
  111. #define A2T(pStr) A2W(pStr)
  112. #define T2W(pStr) (__nLength, __pAscii, __pUnicode, pStr)
  113. #define W2T(pStr) (__nLength, __pAscii, __pUnicode, pStr)
  114. #define T2DA(pStr) W2A(pStr)
  115. #define A2DT(pStr) A2W(pStr)
  116. #define T2DW(pStr) _wcsdupa(pStr)
  117. #define W2DT(pStr) _wcsdupa(pStr)
  118. #else //UNICODE
  119. #define T2A(pStr) (__nLength, __pAscii, __pUnicode, pStr)
  120. #define A2T(pStr) (__nLength, __pAscii, __pUnicode, pStr)
  121. #define T2W(pStr) A2W(pStr)
  122. #define W2T(pStr) W2A(pStr)
  123. #define T2DA(pStr) _strdupa(pStr)
  124. #define A2DT(pStr) _strdupa(pStr)
  125. #define T2DW(pStr) A2W(pStr)
  126. #define W2DT(pStr) W2A(pStr)
  127. #endif //UNICODE
  128. //////////////////////////////////////////////////////////////////////////
  129. //
  130. // _tcsdupa
  131. //
  132. // Routine Description:
  133. // Duplicates a string to a buffer allocated off the stack using _alloca
  134. //
  135. // Arguments:
  136. // pStr input string
  137. //
  138. // Return Value:
  139. // duplicated string
  140. //
  141. #define _wcsdupa(pStr) \
  142. \
  143. (__pAscii, (__pUnicode = pStr) == 0 ? (PWSTR) 0 : ( \
  144. \
  145. __nLength = wcslen(__pUnicode) + 1, \
  146. \
  147. wcscpy((PWSTR) _alloca(__nLength * sizeof(WCHAR)), __pUnicode))) \
  148. #define _strdupa(pStr) \
  149. \
  150. (__pUnicode, (__pAscii = pStr) == 0 ? (PSTR) 0 : ( \
  151. \
  152. __nLength = strlen(__pAscii) + 1, \
  153. \
  154. strcpy((PSTR) _alloca(__nLength * sizeof(CHAR)), __pAscii))) \
  155. #ifdef UNICODE
  156. #define _tcsdupa _wcsdupa
  157. #else
  158. #define _tcsdupa _strdupa
  159. #endif
  160. #endif //USES_CONVERSION