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.

231 lines
5.5 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1991 - 2000.
  5. //
  6. // File: DYNSTRM.CXX
  7. //
  8. // Contents: Dynamic Stream
  9. //
  10. // History: 17-May-1993 BartoszM Created
  11. // 07-Dec-1993 DwightKr Added Shrink() method
  12. // 28-Jul-97 SitaramR Added sequential read/write
  13. //
  14. //----------------------------------------------------------------------------
  15. #include <pch.cxx>
  16. #pragma hdrstop
  17. #include <dynstrm.hxx>
  18. CDynStream::CDynStream()
  19. : _pStream(0)
  20. {
  21. }
  22. CDynStream::CDynStream( PMmStream* stream)
  23. : _pStream(stream)
  24. {
  25. }
  26. CDynStream::~CDynStream()
  27. {
  28. if ( 0 != _pStream )
  29. {
  30. _pStream->Unmap( _bufStream );
  31. delete _pStream;
  32. }
  33. } //~CDynStream
  34. //+---------------------------------------------------------------------------
  35. //
  36. // Member: CDynStream::Flush
  37. //
  38. // Synopsis: Marks a stream clean and flushes it to disk
  39. //
  40. // History: 5-Oct-98 dlee Created
  41. //
  42. //----------------------------------------------------------------------------
  43. void CDynStream::Flush()
  44. {
  45. if ( 0 != _pStream && _pStream->Ok() && _pStream->isWritable() )
  46. {
  47. MarkClean();
  48. _pStream->Flush( _bufStream, _bufStream.Size() );
  49. }
  50. } //Flush
  51. BOOL CDynStream::CheckVersion ( PStorage& storage, ULONG version, BOOL fIsReadOnly )
  52. {
  53. if (!_pStream->Ok())
  54. {
  55. Win4Assert( !"Catalog corruption" );
  56. THROW( CException( CI_CORRUPT_DATABASE ));
  57. }
  58. if (_pStream->isEmpty() )
  59. {
  60. if ( !fIsReadOnly )
  61. {
  62. _pStream->SetSize( storage, CommonPageRound(HEADER_SIZE));
  63. _pStream->MapAll ( _bufStream );
  64. SetVersion(version);
  65. SetCount(0);
  66. SetDataSize(0);
  67. Flush();
  68. }
  69. else
  70. return FALSE;
  71. }
  72. else
  73. _pStream->MapAll ( _bufStream );
  74. if ( version != Version() )
  75. return FALSE;
  76. else
  77. return TRUE;
  78. }
  79. BOOL CDynStream::MarkDirty()
  80. {
  81. BOOL fDirty = IsDirty();
  82. *((ULONG *) _bufStream.Get() + HEADER_DIRTY) = TRUE;
  83. _pStream->Flush( _bufStream, _bufStream.Size() );
  84. return fDirty;
  85. }
  86. void CDynStream::Grow ( PStorage& storage, ULONG newAvailSize )
  87. {
  88. if (newAvailSize > _pStream->SizeLow() - HEADER_SIZE )
  89. {
  90. _pStream->SetSize ( storage,
  91. CommonPageRound(newAvailSize + HEADER_SIZE) );
  92. _pStream->Unmap(_bufStream);
  93. _pStream->MapAll ( _bufStream );
  94. }
  95. Win4Assert ( _pStream->SizeHigh() == 0 );
  96. }
  97. void CDynStream::Shrink ( PStorage& storage, ULONG newAvailSize )
  98. {
  99. if (newAvailSize < _pStream->SizeLow() - HEADER_SIZE )
  100. {
  101. _pStream->Unmap(_bufStream);
  102. _pStream->SetSize ( storage,
  103. CommonPageRound(newAvailSize + HEADER_SIZE) );
  104. _pStream->MapAll ( _bufStream );
  105. }
  106. Win4Assert ( _pStream->SizeHigh() == 0 );
  107. }
  108. //+---------------------------------------------------------------------------
  109. //
  110. // Member: CDynStream::InitializeForRead
  111. //
  112. // Synopsis: Prepare for reading in from disk
  113. //
  114. // History: 28-Jul-97 SitaramR Created
  115. //
  116. //----------------------------------------------------------------------------
  117. void CDynStream::InitializeForRead()
  118. {
  119. _ulDataSize = DataSize();
  120. _pStartPos = Get();
  121. _pCurPos = _pStartPos;
  122. }
  123. //+---------------------------------------------------------------------------
  124. //
  125. // Member: CDynStream::InitializeForWrite
  126. //
  127. // Synopsis: Prepare for writing to disk
  128. //
  129. // Arguments: [cbSize] -- Total file size after writing
  130. //
  131. // History: 28-Jul-97 SitaramR Created
  132. //
  133. //----------------------------------------------------------------------------
  134. void CDynStream::InitializeForWrite( ULONG cbSize )
  135. {
  136. PStorage * pStorage = 0;
  137. Grow( *pStorage, cbSize );
  138. _ulDataSize = cbSize;
  139. SetCount( 0 );
  140. SetDataSize( _ulDataSize );
  141. _pStartPos = Get();
  142. _pCurPos = _pStartPos;
  143. }
  144. //+---------------------------------------------------------------------------
  145. //
  146. // Member: CDynStream::Read
  147. //
  148. // Synopsis: Sequential read
  149. //
  150. // Arguments: [pBuf] -- Buffer to read into
  151. // [cbToRead] -- # bytes to read
  152. //
  153. // History: 28-Jul-97 SitaramR Created
  154. //
  155. // Notes: Call InitializeForRead before reading sequentially
  156. //
  157. //----------------------------------------------------------------------------
  158. ULONG CDynStream::Read( void *pBuf, ULONG cbToRead )
  159. {
  160. ULONG cbRead = 0;
  161. if ( _pCurPos + cbToRead < _pStartPos + _ulDataSize )
  162. {
  163. BYTE *pbDest = (BYTE *) pBuf;
  164. RtlCopyMemory( pbDest, _pCurPos, cbToRead );
  165. cbRead = cbToRead;
  166. _pCurPos += cbRead;
  167. }
  168. return cbRead;
  169. }
  170. //+---------------------------------------------------------------------------
  171. //
  172. // Member: CDynStream::Write
  173. //
  174. // Synopsis: Sequential write
  175. //
  176. // Arguments: [pBuf] -- Buffer to write from
  177. // [cbToWrite] -- # bytes to write
  178. //
  179. // History: 28-Jul-97 SitaramR Created
  180. //
  181. // Notes: Call InitializeForWrite before writing sequentially
  182. //
  183. //----------------------------------------------------------------------------
  184. void CDynStream::Write( void *pBuf, ULONG cbToWrite )
  185. {
  186. if ( _pCurPos + cbToWrite > _pStartPos + _ulDataSize )
  187. {
  188. Win4Assert( _pCurPos + cbToWrite <= _pStartPos + _ulDataSize );
  189. THROW( CException( E_INVALIDARG ) );
  190. }
  191. BYTE *pSrc = (BYTE *)pBuf;
  192. RtlCopyMemory( _pCurPos, pSrc, cbToWrite );
  193. _pCurPos += cbToWrite;
  194. }