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.

239 lines
5.8 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows NT **/
  3. /** Copyright(c) Microsoft Corp., 1994 **/
  4. /**********************************************************************/
  5. /*
  6. string.hxx
  7. This module contains a light weight string class
  8. FILE HISTORY:
  9. michth 11-Apr-1997 Created
  10. */
  11. #ifndef _MLSZAU_HXX_
  12. #define _MLSZAU_HXX_
  13. # include <buffer.hxx>
  14. class IRTL_DLLEXP MLSZAU;
  15. /*
  16. Class STRAU
  17. String Ascii-Unicode
  18. This class transparently converts multisz between ANSI and Unicode.
  19. All members can take/return string in either ANSI or Unicode.
  20. For members which have a parameter of bUnicode, the actual string
  21. type must match the flag, even though it gets passed in as the
  22. declared type.
  23. Strings are sync'd on an as needed basis. If a string is set in
  24. ANSI, then a UNICODE version will only be created if a get for
  25. UNICODE is done, or if a UNICODE string is appended.
  26. All conversions are done using Latin-1. This is because the intended
  27. use of this class is for converting HTML pages, which are defined to
  28. be Latin-1.
  29. */
  30. class IRTL_DLLEXP MLSZAU
  31. {
  32. public:
  33. MLSZAU()
  34. : m_bufAnsi (0),
  35. m_bufUnicode(0),
  36. m_cchUnicodeLen ( 0),
  37. m_cbMultiByteLen ( 0),
  38. m_bInSync (TRUE),
  39. m_bUnicode (FALSE),
  40. m_bIsValid (TRUE)
  41. {}
  42. MLSZAU( const LPSTR pchInit,
  43. DWORD cbLen = 0 )
  44. : m_bufAnsi (0),
  45. m_bufUnicode(0),
  46. m_cchUnicodeLen ( 0),
  47. m_cbMultiByteLen ( 0),
  48. m_bInSync (TRUE),
  49. m_bUnicode (FALSE),
  50. m_bIsValid (TRUE)
  51. { AuxInit(pchInit, cbLen); }
  52. MLSZAU( const LPSTR pchInit,
  53. BOOL bUnicode,
  54. DWORD cchLen = 0 )
  55. : m_bufAnsi (0),
  56. m_bufUnicode(0),
  57. m_cchUnicodeLen ( 0),
  58. m_cbMultiByteLen ( 0),
  59. m_bInSync (TRUE),
  60. m_bUnicode (FALSE),
  61. m_bIsValid (TRUE)
  62. {
  63. if (bUnicode) {
  64. AuxInit((LPWSTR)pchInit, cchLen);
  65. }
  66. else {
  67. AuxInit((LPSTR)pchInit, cchLen);
  68. }
  69. }
  70. MLSZAU( const LPWSTR pchInit,
  71. DWORD cchLen = 0 )
  72. : m_bufAnsi (0),
  73. m_bufUnicode(0),
  74. m_cchUnicodeLen ( 0),
  75. m_cbMultiByteLen ( 0),
  76. m_bInSync (TRUE),
  77. m_bUnicode (FALSE),
  78. m_bIsValid (TRUE)
  79. { AuxInit(pchInit, cchLen); }
  80. MLSZAU( MLSZAU & mlsz )
  81. : m_bufAnsi(0),
  82. m_bufUnicode(0),
  83. m_cchUnicodeLen ( 0),
  84. m_cbMultiByteLen ( 0),
  85. m_bInSync (TRUE),
  86. m_bUnicode (FALSE),
  87. m_bIsValid (TRUE)
  88. {
  89. if (mlsz.IsCurrentUnicode()) {
  90. AuxInit((const LPWSTR)mlsz.QueryStr(TRUE), mlsz.QueryCCH());
  91. }
  92. else {
  93. AuxInit((const LPSTR)mlsz.QueryStr(FALSE), mlsz.QueryCBA());
  94. }
  95. }
  96. BOOL IsValid(VOID) { return m_bIsValid; }
  97. // Resets the internal string to be NULL string. Buffer remains cached.
  98. VOID Reset( VOID)
  99. {
  100. DBG_ASSERT( m_bufAnsi.QueryPtr() != NULL);
  101. DBG_ASSERT( m_bufUnicode.QueryPtr() != NULL);
  102. *QueryStrA() = '\0';
  103. /* INTRINSA suppress = null_pointers */
  104. *QueryStrW() = (WCHAR)'\0';
  105. *(QueryStrA() + 1) = '\0';
  106. *(QueryStrW() + 1) = (WCHAR)'\0';
  107. m_bUnicode = FALSE;
  108. m_bInSync = TRUE;
  109. m_cchUnicodeLen = 2;
  110. m_cbMultiByteLen = 2;
  111. }
  112. //
  113. // Returns the number of bytes in the string including the terminating
  114. // NULLs
  115. //
  116. #ifdef UNICODE
  117. UINT QueryCB(BOOL bUnicode = TRUE)
  118. #else
  119. UINT QueryCB(BOOL bUnicode = FALSE)
  120. #endif
  121. {
  122. if (bUnicode) {
  123. return QueryCBW();
  124. }
  125. else {
  126. return QueryCBA();
  127. }
  128. }
  129. UINT QueryCBA()
  130. {
  131. if (m_bUnicode && !m_bInSync) {
  132. //
  133. // Force sync to set m_cbMultiByteLen
  134. //
  135. QueryStrA();
  136. }
  137. return (m_cbMultiByteLen * sizeof(CHAR));
  138. }
  139. UINT QueryCBW()
  140. {
  141. if (!m_bUnicode && !m_bInSync) {
  142. //
  143. // Force sync to set m_cchUnicodeLen
  144. //
  145. QueryStrW();
  146. }
  147. return (m_cchUnicodeLen * sizeof(WCHAR));
  148. }
  149. //
  150. // Returns # of characters in the string excluding the terminating NULL
  151. //
  152. UINT QueryCCH( VOID )
  153. {
  154. //
  155. // Unicode Len is same as # characters,
  156. // but Multibyte Len is not, so use
  157. // Unicode Len
  158. //
  159. if (!m_bUnicode && !m_bInSync) {
  160. //
  161. // Force sync to set m_cchUnicodeLen
  162. //
  163. QueryStrW();
  164. }
  165. return (m_cchUnicodeLen);
  166. }
  167. //
  168. // Return the string buffer
  169. //
  170. #ifdef UNICODE
  171. LPTSTR QueryStr(BOOL bUnicode = TRUE);
  172. #else
  173. LPTSTR QueryStr(BOOL bUnicode = FALSE);
  174. #endif
  175. LPSTR QueryStrA( VOID ) { return ( (LPSTR)QueryStr(FALSE)); }
  176. LPWSTR QueryStrW( VOID ) { return ( (LPWSTR)QueryStr(TRUE)); }
  177. BOOL IsCurrentUnicode(void)
  178. {
  179. return m_bUnicode;
  180. }
  181. private:
  182. VOID AuxInit( const LPSTR pInit,
  183. DWORD cchLen = 0 );
  184. VOID AuxInit( const LPWSTR pInit,
  185. DWORD cchLen = 0 );
  186. DWORD m_cchUnicodeLen;
  187. DWORD m_cbMultiByteLen;
  188. BUFFER m_bufAnsi;
  189. BUFFER m_bufUnicode;
  190. BOOL m_bInSync;
  191. BOOL m_bUnicode;
  192. BOOL m_bIsValid;
  193. };
  194. #endif // !_MLSZAU_HXX_