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.

275 lines
6.2 KiB

  1. /*
  2. Copyright (c) 1997-1999 Microsoft Corporation
  3. */
  4. #ifndef __SDP_VALUE__
  5. #define __SDP_VALUE__
  6. #include "sdpcommo.h"
  7. #include "sdpfld.h"
  8. // this value indicates that line transitions must start
  9. // this has to be a value of 0 that is same as the first (start) state for
  10. // all line transitions
  11. const DWORD LINE_START = 0;
  12. // Usage - Modifications involving change in the layout of the value
  13. // line must also modify the CArrays m_FieldArray and m_SeparatorCharArray if they are used
  14. class _DllDecl SDP_VALUE
  15. {
  16. public:
  17. inline SDP_VALUE(
  18. IN DWORD ErrorCode,
  19. IN const CHAR *TypePrefixString,
  20. IN const SDP_LINE_TRANSITION *SdpLineTransition = NULL
  21. );
  22. // SDP_VALUE instances use an inline Reset method which calls a virtual InternalReset method.
  23. // this is possible because unlike the SDP_FIELD inheritance tree, SDP_VALUE and SDP_VALUE_LIST
  24. // do not share a common base class. This combined with the fact that the SDP_VALUE inheriance
  25. // tree is quite shallow and has fewer instances (than SDP_FIELD) makes the scheme appropriate
  26. // it as it reduces the number of Reset related calls to 1 and the inline code is not repeated
  27. // to often.
  28. // For the SDP_FIELD inheritance tree, the appropriate Reset calling sequence is a series of
  29. // Reset calls starting with the top most virtual Reset body followed by the
  30. // base class Reset method (recursively). This is appropriate because, the number of calls
  31. // would not decrease if the InternalReset scheme is adopted (virtual Reset()) and that the
  32. // inheritance tree is much deeper
  33. inline void Reset();
  34. virtual BOOL IsValid() const;
  35. inline BOOL IsModified() const;
  36. inline DWORD GetCharacterStringSize();
  37. inline BOOL PrintValue(
  38. OUT ostrstream &OutputStream
  39. );
  40. inline BOOL ParseLine(
  41. IN OUT CHAR *&Line
  42. );
  43. virtual ~SDP_VALUE()
  44. {}
  45. protected:
  46. // the line state is the initial state for parsing the line and must be
  47. // assigned by the deriving value class
  48. DWORD m_LineState;
  49. // the error code, type prefix string and the transition info (table) must be
  50. // specified by the deriving class to this class's constructor
  51. const DWORD m_ErrorCode;
  52. const CHAR * const m_TypePrefixString;
  53. const SDP_LINE_TRANSITION * const m_SdpLineTransition;
  54. CArray<SDP_FIELD *, SDP_FIELD *> m_FieldArray;
  55. CArray<CHAR, CHAR> m_SeparatorCharArray;
  56. virtual void InternalReset() = 0;
  57. virtual BOOL CalcIsModified() const;
  58. virtual DWORD CalcCharacterStringSize();
  59. virtual BOOL CopyValue(
  60. OUT ostrstream &OutputStream
  61. );
  62. virtual BOOL InternalParseLine(
  63. IN OUT CHAR *&Line
  64. );
  65. BOOL GetFieldToParse(
  66. IN const CHAR SeparatorChar,
  67. IN const LINE_TRANSITION_INFO *LineTransitionInfo,
  68. OUT SDP_FIELD *&Field,
  69. OUT BOOL &Finished,
  70. OUT BOOL &AddToArray
  71. );
  72. virtual BOOL GetField(
  73. OUT SDP_FIELD *&Field,
  74. OUT BOOL &AddToArray
  75. )
  76. {
  77. // we should not reach here
  78. // this method must be overridden to be used
  79. // to be done
  80. ASSERT(FALSE);
  81. return FALSE;
  82. }
  83. };
  84. inline
  85. SDP_VALUE::SDP_VALUE(
  86. IN DWORD ErrorCode,
  87. IN const CHAR *TypePrefixString,
  88. IN const SDP_LINE_TRANSITION *SdpLineTransition
  89. )
  90. : m_ErrorCode(ErrorCode),
  91. m_TypePrefixString(TypePrefixString),
  92. m_SdpLineTransition(SdpLineTransition),
  93. m_LineState(LINE_START)
  94. {
  95. ASSERT(NULL != TypePrefixString);
  96. ASSERT(strlen(TypePrefixString) == TYPE_STRING_LEN);
  97. }
  98. inline void
  99. SDP_VALUE::Reset(
  100. )
  101. {
  102. InternalReset();
  103. // empty the separator char / field arrays
  104. m_FieldArray.RemoveAll();
  105. m_SeparatorCharArray.RemoveAll();
  106. m_LineState = LINE_START;
  107. }
  108. inline BOOL
  109. SDP_VALUE::IsModified(
  110. ) const
  111. {
  112. return ( IsValid() ? CalcIsModified() : FALSE );
  113. }
  114. inline DWORD
  115. SDP_VALUE::GetCharacterStringSize(
  116. )
  117. {
  118. return ( IsValid() ? CalcCharacterStringSize() : 0 );
  119. }
  120. inline BOOL
  121. SDP_VALUE::PrintValue(
  122. OUT ostrstream &OutputStream
  123. )
  124. {
  125. // should not be modified
  126. ASSERT(!IsModified());
  127. return ( IsValid() ? CopyValue(OutputStream) : TRUE );
  128. }
  129. inline BOOL
  130. SDP_VALUE::ParseLine(
  131. IN OUT CHAR *&Line
  132. )
  133. {
  134. // parse the line
  135. return InternalParseLine(Line);
  136. }
  137. class _DllDecl SDP_VALUE_LIST : public SDP_POINTER_ARRAY<SDP_VALUE *>
  138. {
  139. public:
  140. inline BOOL IsValid() const;
  141. inline BOOL ParseLine(
  142. IN CHAR *&Line
  143. );
  144. inline SDP_VALUE *GetCurrentElement();
  145. virtual BOOL IsModified() const;
  146. virtual DWORD GetCharacterStringSize();
  147. virtual BOOL PrintValue(
  148. OUT ostrstream &OutputStream
  149. );
  150. virtual SDP_VALUE *CreateElement() = 0;
  151. };
  152. inline BOOL
  153. SDP_VALUE_LIST::IsValid(
  154. ) const
  155. {
  156. // check each of the members in the list for validity
  157. for (int i=0; i < GetSize(); i++)
  158. {
  159. // if even one member is valid, return TRUE
  160. if ( GetAt(i)->IsValid() )
  161. {
  162. return TRUE;
  163. }
  164. }
  165. // all members are invalid
  166. return FALSE;
  167. }
  168. inline BOOL
  169. SDP_VALUE_LIST::ParseLine(
  170. IN CHAR *&Line
  171. )
  172. {
  173. SDP_VALUE *SdpValue = CreateElement();
  174. if ( NULL == SdpValue )
  175. {
  176. return FALSE;
  177. }
  178. if ( !SdpValue->ParseLine(Line) )
  179. {
  180. delete SdpValue;
  181. return FALSE;
  182. }
  183. try
  184. {
  185. Add(SdpValue);
  186. }
  187. catch(...)
  188. {
  189. delete SdpValue;
  190. SetLastError(ERROR_OUTOFMEMORY);
  191. return FALSE;
  192. }
  193. return TRUE;
  194. }
  195. inline SDP_VALUE *
  196. SDP_VALUE_LIST::GetCurrentElement(
  197. )
  198. {
  199. ASSERT(0 < GetSize());
  200. ASSERT(NULL != GetAt(GetSize()-1));
  201. return GetAt(GetSize()-1);
  202. }
  203. #endif // __SDP_VALUE__