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.

286 lines
6.2 KiB

  1. #ifndef _STRINGA_HXX_
  2. #define _STRINGA_HXX_
  3. # include <buffer.hxx>
  4. /*++
  5. class STRA:
  6. Intention:
  7. A light-weight string class supporting encapsulated string class.
  8. This object is derived from BUFFER class.
  9. It maintains following state:
  10. m_cchLen - string length cached when we update the string.
  11. --*/
  12. class STRA;
  13. class STRA
  14. {
  15. public:
  16. STRA()
  17. : m_Buff(),
  18. m_cchLen( 0)
  19. {
  20. *QueryStr() = '\0';
  21. }
  22. //
  23. // creates a stack version of the STRA object - uses passed in stack buffer
  24. // STRA does not free this pbInit on its own.
  25. //
  26. STRA(CHAR *pbInit,
  27. DWORD cbInit)
  28. : m_Buff((BYTE *)pbInit, cbInit),
  29. m_cchLen(0)
  30. {
  31. *pbInit = '\0';
  32. }
  33. BOOL IsValid( VOID ) const { return ( m_Buff.IsValid() ) ; }
  34. //
  35. // Checks and returns TRUE if this string has no valid data else FALSE
  36. //
  37. BOOL IsEmpty() const
  38. {
  39. return (m_cchLen == 0);
  40. }
  41. //
  42. // Returns the number of bytes in the string excluding the terminating
  43. // NULL
  44. //
  45. UINT QueryCB() const
  46. {
  47. return m_cchLen;
  48. }
  49. //
  50. // Returns # of characters in the string excluding the terminating NULL
  51. //
  52. UINT QueryCCH() const
  53. {
  54. return m_cchLen;
  55. }
  56. //
  57. // Returns number of bytes in storage buffer
  58. //
  59. UINT QuerySize() const
  60. {
  61. return m_Buff.QuerySize();
  62. }
  63. //
  64. // Return the string buffer
  65. //
  66. CHAR *QueryStr()
  67. {
  68. return (CHAR *)m_Buff.QueryPtr();
  69. }
  70. const CHAR *QueryStr() const
  71. {
  72. return (const CHAR *)m_Buff.QueryPtr();
  73. }
  74. //
  75. // Resets the internal string to be NULL string. Buffer remains cached.
  76. //
  77. VOID Reset()
  78. {
  79. *(QueryStr()) = '\0';
  80. m_cchLen = 0;
  81. }
  82. //
  83. // Resize the internal buffer
  84. //
  85. HRESULT Resize(DWORD size)
  86. {
  87. if (!m_Buff.Resize(size))
  88. {
  89. return HRESULT_FROM_WIN32(GetLastError());
  90. }
  91. return S_OK;
  92. }
  93. //
  94. // Append something to the end of the string
  95. //
  96. HRESULT Append(const CHAR *pchInit)
  97. {
  98. if (pchInit)
  99. {
  100. return AuxAppend((const BYTE *)pchInit,
  101. (ULONG) strlen(pchInit),
  102. QueryCB());
  103. }
  104. return S_OK;
  105. }
  106. HRESULT Append(const CHAR * pchInit,
  107. DWORD cchLen)
  108. {
  109. if (pchInit && cchLen)
  110. {
  111. return AuxAppend((const BYTE *) pchInit,
  112. cchLen,
  113. QueryCB());
  114. }
  115. return S_OK;
  116. }
  117. HRESULT Append(const STRA & str)
  118. {
  119. if (str.QueryCCH())
  120. {
  121. return AuxAppend((const BYTE *)str.QueryStr(),
  122. str.QueryCCH(),
  123. QueryCCH());
  124. }
  125. return S_OK;
  126. }
  127. //
  128. // Copy the contents of another string to this one
  129. //
  130. HRESULT Copy(const CHAR *pchInit)
  131. {
  132. if (pchInit)
  133. {
  134. return AuxAppend((const BYTE *)pchInit,
  135. (ULONG) strlen(pchInit),
  136. 0);
  137. }
  138. return HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER);
  139. }
  140. HRESULT Copy(const CHAR * pchInit,
  141. DWORD cchLen)
  142. {
  143. if (pchInit && cchLen)
  144. {
  145. return AuxAppend((const BYTE *)pchInit,
  146. cchLen,
  147. 0);
  148. }
  149. if (cchLen)
  150. {
  151. return HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER);
  152. }
  153. return S_OK;
  154. }
  155. HRESULT Copy(const STRA & str)
  156. {
  157. return AuxAppend((const BYTE *)str.QueryStr(),
  158. str.QueryCB(),
  159. 0);
  160. }
  161. // Makes a copy of the stored string into the given buffer
  162. //
  163. HRESULT CopyToBuffer(CHAR *lpszBuffer,
  164. LPDWORD lpcb) const;
  165. BOOL SetLen(IN DWORD cchLen)
  166. /*++
  167. Routine Description:
  168. Set the length of the string and null terminate, if there
  169. is sufficient buffer already allocated. Will not reallocate.
  170. NOTE: The actual wcslen may be less than cchLen if you are
  171. expanding the string. If this is the case SyncWithBuffer
  172. should be called to ensure consistency. The real use of this
  173. method is to truncate.
  174. Arguments:
  175. cchLen - The number of characters in the new string.
  176. Return Value:
  177. TRUE - if the buffer size is sufficient to hold the string.
  178. FALSE - insufficient buffer.
  179. --*/
  180. {
  181. if(cchLen >= m_Buff.QuerySize())
  182. {
  183. return FALSE;
  184. }
  185. *((CHAR *) m_Buff.QueryPtr() + cchLen) = '\0';
  186. m_cchLen = cchLen;
  187. return TRUE;
  188. }
  189. //
  190. // Recalculate the length of the string, etc. because we've modified
  191. // the buffer directly.
  192. //
  193. VOID
  194. SyncWithBuffer(
  195. VOID
  196. )
  197. {
  198. m_cchLen = (LONG) strlen( QueryStr() );
  199. }
  200. //
  201. // Makes a clone of the current string in the string pointer passed in.
  202. //
  203. HRESULT
  204. Clone( OUT STRA * pstrClone ) const
  205. {
  206. return ( ( pstrClone == NULL ) ?
  207. ( HRESULT_FROM_WIN32( ERROR_INVALID_PARAMETER ) ) :
  208. ( pstrClone->Copy( *this ) )
  209. );
  210. } // STR::Clone()
  211. private:
  212. //
  213. // Avoid C++ errors. This object should never go through a copy
  214. // constructor, unintended cast or assignment.
  215. //
  216. STRA( const STRA &)
  217. {}
  218. STRA( const CHAR *)
  219. {}
  220. STRA(CHAR *)
  221. {}
  222. STRA & operator = (const STRA &)
  223. { return *this; }
  224. HRESULT AuxAppend(const BYTE *pStr,
  225. ULONG cbStr,
  226. ULONG cbOffset,
  227. BOOL fAddSlop = TRUE);
  228. BUFFER m_Buff;
  229. LONG m_cchLen;
  230. };
  231. #endif