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.

331 lines
4.9 KiB

  1. /*
  2. Copyright (c) 1997-1999 Microsoft Corporation
  3. */
  4. #include "sdppch.h"
  5. #include <strstrea.h>
  6. #include "sdpfld.h"
  7. void
  8. SDP_SINGLE_FIELD::Reset(
  9. )
  10. {
  11. m_IsModified = FALSE;
  12. m_IsValid = FALSE;
  13. }
  14. BOOL
  15. SDP_SINGLE_FIELD::IsValid(
  16. ) const
  17. {
  18. return m_IsValid;
  19. }
  20. void
  21. SDP_SINGLE_FIELD::IsValid(
  22. IN BOOL ValidFlag
  23. )
  24. {
  25. m_IsValid = ValidFlag;
  26. }
  27. BOOL
  28. SDP_SINGLE_FIELD::IsModified(
  29. ) const
  30. {
  31. return m_IsModified;
  32. }
  33. void
  34. SDP_SINGLE_FIELD::IsModified(
  35. IN BOOL ModifiedFlag
  36. )
  37. {
  38. ASSERT(IsValid());
  39. m_IsModified = ModifiedFlag;
  40. }
  41. DWORD
  42. SDP_SINGLE_FIELD::GetCharacterStringSize(
  43. )
  44. {
  45. if ( m_IsValid )
  46. {
  47. if ( m_IsModified )
  48. {
  49. return CalcCharacterStringSize();
  50. }
  51. else
  52. {
  53. return m_PrintLength;
  54. }
  55. }
  56. else
  57. {
  58. return 0;
  59. }
  60. }
  61. BOOL
  62. SDP_SINGLE_FIELD::PrintField(
  63. OUT ostrstream &OutputStream
  64. )
  65. {
  66. // should not be modified
  67. ASSERT(!IsModified());
  68. return ( IsValid() ? CopyField(OutputStream) : TRUE );
  69. }
  70. BOOL
  71. SDP_SINGLE_FIELD::ParseToken(
  72. IN CHAR *Token
  73. )
  74. {
  75. ASSERT(!IsModified());
  76. if ( !InternalParseToken(Token) )
  77. {
  78. ASSERT(!IsModified());
  79. return FALSE;
  80. }
  81. IsValid(TRUE);
  82. IsModified(TRUE);
  83. return TRUE;
  84. }
  85. DWORD
  86. SDP_SINGLE_FIELD::CalcCharacterStringSize(
  87. )
  88. {
  89. ASSERT(IsModified());
  90. // this copy should not fail as the buffer size should be sufficient for
  91. // all forseeable situations
  92. ASSERT(NULL != m_PrintBuffer);
  93. ostrstream OutputStream(m_PrintBuffer, m_PrintBufferSize);
  94. BOOL Success = PrintData(OutputStream);
  95. ASSERT(Success);
  96. m_PrintLength = OutputStream.pcount();
  97. m_PrintBuffer[m_PrintLength] = EOS;
  98. IsModified(FALSE);
  99. return m_PrintLength;
  100. }
  101. // this method should only be called after GetCharacterStringSize() has
  102. // been called to determine the size of buffer required
  103. BOOL
  104. SDP_SINGLE_FIELD::CopyField(
  105. OUT ostrstream &OutputStream
  106. )
  107. {
  108. ASSERT(!IsModified());
  109. ASSERT(NULL != m_PrintBuffer);
  110. OutputStream << (CHAR *)m_PrintBuffer;
  111. if( OutputStream.fail() )
  112. {
  113. SetLastError(SDP_OUTPUT_ERROR);
  114. return FALSE;
  115. }
  116. return TRUE;
  117. }
  118. void
  119. SDP_FIELD_LIST::Reset(
  120. )
  121. {
  122. SDP_POINTER_ARRAY<SDP_FIELD *>::Reset();
  123. }
  124. BOOL
  125. SDP_FIELD_LIST::IsValid(
  126. ) const
  127. {
  128. // if there are no members, then the instance is invalid
  129. if ( 0 >= GetSize() )
  130. {
  131. return FALSE;
  132. }
  133. // check each of the members in the list for validity
  134. for ( int i=0; i < GetSize(); i++ )
  135. {
  136. // if even one member is invalid, return FALSE
  137. if ( !GetAt(i)->IsValid() )
  138. {
  139. return FALSE;
  140. }
  141. }
  142. // all members are valid
  143. return TRUE;
  144. }
  145. BOOL
  146. SDP_FIELD_LIST::IsModified(
  147. ) const
  148. {
  149. int NumElements = (int)GetSize();
  150. for ( int i = 0; i < NumElements; i++ )
  151. {
  152. if ( GetAt(i)->IsModified() )
  153. {
  154. return TRUE;
  155. }
  156. }
  157. return FALSE;
  158. }
  159. void
  160. SDP_FIELD_LIST::IsModified(
  161. IN BOOL ModifiedFlag
  162. )
  163. {
  164. // if no elements, the instance isn't "modified"
  165. if ( 0 >= GetSize() )
  166. {
  167. ModifiedFlag = FALSE;
  168. return;
  169. }
  170. for ( int i = 0; i < GetSize(); i++ )
  171. {
  172. GetAt(i)->IsModified(ModifiedFlag);
  173. }
  174. }
  175. DWORD
  176. SDP_FIELD_LIST::GetCharacterStringSize(
  177. )
  178. {
  179. DWORD ReturnValue = 0;
  180. int NumElements = (int)GetSize();
  181. for ( int i = 0; i < NumElements; i++ )
  182. {
  183. ReturnValue += GetAt(i)->GetCharacterStringSize();
  184. }
  185. // if there are elements, add one separator character for each field other
  186. // than the first
  187. if ( 0 < NumElements )
  188. {
  189. ReturnValue += (NumElements - 1);
  190. }
  191. return ReturnValue;
  192. }
  193. BOOL
  194. SDP_FIELD_LIST::ParseToken(
  195. IN CHAR *Token
  196. )
  197. {
  198. SDP_FIELD *SdpField = CreateElement();
  199. if ( NULL == SdpField )
  200. {
  201. return FALSE;
  202. }
  203. if ( !SdpField->ParseToken(Token) )
  204. {
  205. delete SdpField;
  206. return FALSE;
  207. }
  208. try
  209. {
  210. Add(SdpField);
  211. }
  212. catch(...)
  213. {
  214. delete SdpField;
  215. SetLastError(ERROR_OUTOFMEMORY);
  216. return FALSE;
  217. }
  218. return TRUE;
  219. }
  220. BOOL
  221. SDP_FIELD_LIST::PrintField(
  222. OUT ostrstream &OutputStream
  223. )
  224. {
  225. int NumElements = (int)GetSize();
  226. if ( 0 == NumElements )
  227. {
  228. return TRUE;
  229. }
  230. // write into the buffer as Value (separator Value)*
  231. // write the first element
  232. if ( !GetAt(0)->PrintField(OutputStream) )
  233. {
  234. return FALSE;
  235. }
  236. for ( int i=1; i < NumElements; i++ )
  237. {
  238. OutputStream << m_SeparatorChar;
  239. if ( OutputStream.fail() )
  240. {
  241. SetLastError(SDP_OUTPUT_ERROR);
  242. return FALSE;
  243. }
  244. if ( !GetAt(i)->PrintField(OutputStream) )
  245. {
  246. return FALSE;
  247. }
  248. }
  249. return TRUE;
  250. }