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.

328 lines
7.3 KiB

  1. /*
  2. Copyright (c) 1997-1999 Microsoft Corporation
  3. Module Name:
  4. sdpcstrl.h
  5. Abstract:
  6. Author:
  7. */
  8. #ifndef __SDP_CHAR_STRING__
  9. #define __SDP_CHAR_STRING__
  10. #include "sdpcommo.h"
  11. #include "sdpfld.h"
  12. #include "sdpval.h"
  13. class _DllDecl SDP_CHAR_STRING : public SDP_SINGLE_FIELD
  14. {
  15. public:
  16. inline SDP_CHAR_STRING();
  17. virtual void Reset();
  18. inline BOOL ByReference() const;
  19. inline CHAR *GetCharacterString() const;
  20. inline CHAR *GetModifiableCharString();
  21. inline BOOL SetCharacterStringByReference(
  22. IN CHAR *CharacterStringByReference,
  23. IN DWORD Length = 0
  24. );
  25. inline BOOL SetCharacterStringByCopy(
  26. IN const CHAR *CharacterStringByCopy,
  27. IN DWORD Length = 0
  28. );
  29. inline DWORD GetLength() const;
  30. virtual ~SDP_CHAR_STRING();
  31. protected:
  32. virtual BOOL InternalParseToken(
  33. IN CHAR *Token
  34. );
  35. inline BOOL ReAllocCharacterString(
  36. IN UINT BufferSize
  37. );
  38. virtual BOOL InternalSetCharStrByRef(
  39. IN CHAR *CharacterStringByReference,
  40. IN DWORD Length
  41. );
  42. virtual BOOL InternalSetCharStrByCopy(
  43. IN const CHAR *CharacterStringByCopy,
  44. IN DWORD Length
  45. );
  46. virtual DWORD CalcCharacterStringSize();
  47. virtual BOOL CopyField(
  48. OUT ostrstream &OutputStream
  49. );
  50. // this method should not be used
  51. virtual BOOL PrintData(
  52. OUT ostrstream &OutputStream
  53. )
  54. {
  55. ASSERT(FALSE);
  56. return FALSE;
  57. }
  58. private:
  59. DWORD m_CharacterStringLength;
  60. CHAR *m_CharacterString;
  61. DWORD m_LengthByReference;
  62. CHAR *m_CharacterStringByReference;
  63. DWORD m_BytesAllocated;
  64. };
  65. inline
  66. SDP_CHAR_STRING::SDP_CHAR_STRING(
  67. )
  68. : SDP_SINGLE_FIELD(0, NULL), // no buffer used (custom PrintField)
  69. m_CharacterString(NULL),
  70. m_CharacterStringLength(0),
  71. m_LengthByReference(0),
  72. m_CharacterStringByReference(NULL),
  73. m_BytesAllocated(0)
  74. {}
  75. inline BOOL
  76. SDP_CHAR_STRING::ByReference(
  77. ) const
  78. {
  79. // check that both the char string and the char string by ref are not null
  80. // at the same time
  81. ASSERT((NULL == m_CharacterString) != (NULL == m_CharacterStringByReference));
  82. return (NULL == m_CharacterString);
  83. }
  84. inline CHAR *
  85. SDP_CHAR_STRING::GetCharacterString(
  86. ) const
  87. {
  88. // check that both the char string and the char string by ref are not null
  89. // at the same time
  90. ASSERT((NULL == m_CharacterString) != (NULL == m_CharacterStringByReference));
  91. return ((TRUE == ByReference())? m_CharacterStringByReference : m_CharacterString);
  92. }
  93. inline CHAR *
  94. SDP_CHAR_STRING::GetModifiableCharString(
  95. )
  96. {
  97. // check that both the char string and the char string by ref are not null
  98. // at the same time
  99. ASSERT((NULL == m_CharacterString) != (NULL == m_CharacterStringByReference));
  100. // the reference string is not modifiable
  101. ASSERT(FALSE == ByReference());
  102. return ((TRUE == ByReference())? NULL : m_CharacterString);
  103. }
  104. inline DWORD
  105. SDP_CHAR_STRING::GetLength(
  106. ) const
  107. {
  108. // check that both the char string and the char string by ref are not null
  109. // at the same time
  110. ASSERT((NULL == m_CharacterString) != (NULL == m_CharacterStringByReference));
  111. return ((TRUE == ByReference())? m_LengthByReference : m_CharacterStringLength);
  112. }
  113. inline BOOL
  114. SDP_CHAR_STRING::ReAllocCharacterString(
  115. IN UINT BufferSize
  116. )
  117. {
  118. CHAR *NewCharacterString;
  119. // check that both the char string and the char string by ref are not null
  120. // at the same time
  121. ASSERT((NULL == m_CharacterString) != (NULL == m_CharacterStringByReference));
  122. ASSERT(0 != BufferSize);
  123. // if the length of the token (not including the end of string) is not less than
  124. // the number of bytes allocated, release the bytes and allocate again
  125. if ( (BufferSize - 1) >= m_BytesAllocated )
  126. {
  127. try
  128. {
  129. NewCharacterString = new CHAR[BufferSize];
  130. }
  131. catch(...)
  132. {
  133. NewCharacterString = NULL;
  134. }
  135. if ( NULL == NewCharacterString )
  136. {
  137. SetLastError(ERROR_OUTOFMEMORY);
  138. return FALSE;
  139. }
  140. // If there is an old character string, delete it
  141. if ( NULL != m_CharacterString )
  142. {
  143. delete m_CharacterString;
  144. }
  145. m_BytesAllocated = BufferSize;
  146. m_CharacterString = NewCharacterString;
  147. }
  148. m_CharacterStringLength = BufferSize - 1;
  149. // If there was a reference to another string, remove it
  150. if ( ByReference() )
  151. {
  152. m_LengthByReference = 0;
  153. m_CharacterStringByReference = NULL;
  154. }
  155. return TRUE;
  156. }
  157. inline BOOL
  158. SDP_CHAR_STRING::SetCharacterStringByReference(
  159. IN CHAR *CharacterStringByReference,
  160. IN DWORD Length
  161. )
  162. {
  163. // check that both the char string and the char string by ref are not null
  164. // at the same time
  165. ASSERT((NULL == m_CharacterString) != (NULL == m_CharacterStringByReference));
  166. ASSERT(NULL != CharacterStringByReference);
  167. if ( NULL == CharacterStringByReference )
  168. {
  169. SetLastError(SDP_INVALID_VALUE);
  170. return FALSE;
  171. }
  172. if ( !InternalSetCharStrByRef(
  173. CharacterStringByReference,
  174. (0==Length)? strlen(CharacterStringByReference): Length
  175. ) )
  176. {
  177. return FALSE;
  178. }
  179. IsValid(TRUE);
  180. IsModified(TRUE);
  181. return TRUE;
  182. }
  183. inline BOOL
  184. SDP_CHAR_STRING::SetCharacterStringByCopy(
  185. IN const CHAR *CharacterStringByCopy,
  186. IN DWORD Length
  187. )
  188. {
  189. // check that both the char string and the char string by ref are not null
  190. // at the same time
  191. ASSERT((NULL == m_CharacterString) != (NULL == m_CharacterStringByReference));
  192. ASSERT(NULL != CharacterStringByCopy);
  193. if ( NULL == CharacterStringByCopy )
  194. {
  195. SetLastError(SDP_INVALID_VALUE);
  196. return FALSE;
  197. }
  198. if ( !InternalSetCharStrByCopy(
  199. CharacterStringByCopy,
  200. (0==Length)? strlen(CharacterStringByCopy): Length
  201. ) )
  202. {
  203. return FALSE;
  204. }
  205. IsValid(TRUE);
  206. IsModified(TRUE);
  207. return TRUE;
  208. }
  209. class _DllDecl SDP_STRING_LINE : public SDP_VALUE
  210. {
  211. public:
  212. inline SDP_STRING_LINE(
  213. IN const DWORD ErrorCode,
  214. IN const CHAR *TypeString,
  215. IN SDP_FIELD &ParseField
  216. );
  217. virtual HRESULT GetBstrCopy(IN BSTR *Bstr) = 0;
  218. virtual HRESULT SetBstr(IN BSTR Bstr) = 0;
  219. protected:
  220. SDP_FIELD &m_ParseField;
  221. virtual BOOL InternalParseLine(
  222. IN OUT CHAR *&Line
  223. );
  224. inline SDP_FIELD &GetParseField();
  225. };
  226. inline
  227. SDP_STRING_LINE::SDP_STRING_LINE(
  228. IN const DWORD ErrorCode,
  229. IN const CHAR *TypeString,
  230. IN SDP_FIELD &ParseField
  231. )
  232. : SDP_VALUE(ErrorCode, TypeString),
  233. m_ParseField(ParseField)
  234. {
  235. }
  236. inline SDP_FIELD &
  237. SDP_STRING_LINE::GetParseField(
  238. )
  239. {
  240. return m_ParseField;
  241. }
  242. #endif // __SDP_CHAR_STRING__