home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectShow / BaseClasses / pstream.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  4.7 KB  |  197 lines

  1. //------------------------------------------------------------------------------
  2. // File: PStream.cpp
  3. //
  4. // Desc: DirectShow base classes.
  5. //
  6. // Copyright (c) 1992-2001 Microsoft Corporation.  All rights reserved.
  7. //------------------------------------------------------------------------------
  8.  
  9.  
  10. #include <streams.h>
  11.  
  12. #ifdef PERF
  13. #include <measure.h>
  14. #endif
  15. // #include "pstream.h"  in streams.h
  16.  
  17. //
  18. // Constructor
  19. //
  20. CPersistStream::CPersistStream(IUnknown *punk, HRESULT *phr)
  21.     : mPS_fDirty(FALSE)
  22. {
  23.     mPS_dwFileVersion = GetSoftwareVersion();
  24. }
  25.  
  26.  
  27. //
  28. // Destructor
  29. //
  30. CPersistStream::~CPersistStream() {
  31.     // Nothing to do
  32. }
  33.  
  34. #if 0
  35. SAMPLE CODE TO COPY - not active at the moment
  36.  
  37. //
  38. // NonDelegatingQueryInterface
  39. //
  40. // This object supports IPersist & IPersistStream
  41. STDMETHODIMP CPersistStream::NonDelegatingQueryInterface(REFIID riid, void **ppv)
  42. {
  43.     if (riid == IID_IPersist) {
  44.         return GetInterface((IPersist *) this, ppv);      // ???
  45.     }
  46.     else if (riid == IID_IPersistStream) {
  47.         return GetInterface((IPersistStream *) this, ppv);
  48.     }
  49.     else {
  50.         return CUnknown::NonDelegatingQueryInterface(riid, ppv);
  51.     }
  52. }
  53. #endif
  54.  
  55.  
  56. //
  57. // WriteToStream
  58. //
  59. // Writes to the stream (default action is to write nothing)
  60. HRESULT CPersistStream::WriteToStream(IStream *pStream)
  61. {
  62.     // You can override this to do things like
  63.     // hr = pStream->Write(MyStructure, sizeof(MyStructure), NULL);
  64.  
  65.     return NOERROR;
  66. }
  67.  
  68.  
  69.  
  70. HRESULT CPersistStream::ReadFromStream(IStream * pStream)
  71. {
  72.     // You can override this to do things like
  73.     // hr = pStream->Read(MyStructure, sizeof(MyStructure), NULL);
  74.  
  75.     return NOERROR;
  76. }
  77.  
  78.  
  79. //
  80. // Load
  81. //
  82. // Load all the data from the given stream
  83. STDMETHODIMP CPersistStream::Load(LPSTREAM pStm)
  84. {
  85.     HRESULT hr;
  86.     // Load the version number then the data
  87.     mPS_dwFileVersion = ReadInt(pStm, hr);
  88.     if (FAILED(hr)) {
  89.         return hr;
  90.     }
  91.  
  92.     return ReadFromStream(pStm);
  93. }  // Load
  94.  
  95.  
  96.  
  97. //
  98. // Save
  99. //
  100. // Save the contents of this Stream.
  101. STDMETHODIMP CPersistStream::Save(LPSTREAM pStm, BOOL fClearDirty)
  102. {
  103.  
  104.     HRESULT hr = WriteInt(pStm, GetSoftwareVersion());
  105.     if (FAILED(hr)) {
  106.         return hr;
  107.     }
  108.  
  109.     hr = WriteToStream(pStm);
  110.     if (FAILED(hr)) {
  111.         return hr;
  112.     }
  113.  
  114.     mPS_fDirty = !fClearDirty;
  115.  
  116.     return hr;
  117. } // Save
  118.  
  119.  
  120. // WriteInt
  121. //
  122. // Writes an integer to an IStream as 11 UNICODE characters followed by one space.
  123. // You could use this for shorts or unsigneds or anything (up to 32 bits)
  124. // where the value isn't actually truncated by squeezing it into 32 bits.
  125. // Values such as (unsigned) 0x80000000 would come out as -2147483648
  126. // but would then load as 0x80000000 through ReadInt.  Cast as you please.
  127.  
  128. STDAPI WriteInt(IStream *pIStream, int n)
  129. {
  130.     WCHAR Buff[13];  // Allows for trailing null that we don't write
  131.     wsprintfW(Buff, L"%011d ",n);
  132.     return pIStream->Write(&(Buff[0]), 12*sizeof(WCHAR), NULL);
  133. } // WriteInt
  134.  
  135.  
  136. // ReadInt
  137. //
  138. // Reads an integer from an IStream.
  139. // Read as 4 bytes.  You could use this for shorts or unsigneds or anything
  140. // where the value isn't actually truncated by squeezing it into 32 bits
  141. // Striped down subset of what sscanf can do (without dragging in the C runtime)
  142.  
  143. STDAPI_(int) ReadInt(IStream *pIStream, HRESULT &hr)
  144. {
  145.  
  146.     int Sign = 1;
  147.     unsigned int n = 0;    // result wil be n*Sign
  148.     WCHAR wch;
  149.  
  150.     hr = pIStream->Read( &wch, sizeof(wch), NULL);
  151.     if (FAILED(hr)) {
  152.         return 0;
  153.     }
  154.  
  155.     if (wch==L'-'){
  156.         Sign = -1;
  157.         hr = pIStream->Read( &wch, sizeof(wch), NULL);
  158.         if (FAILED(hr)) {
  159.             return 0;
  160.         }
  161.     }
  162.  
  163.     for( ; ; ) {
  164.         if (wch>=L'0' && wch<=L'9') {
  165.             n = 10*n+(int)(wch-L'0');
  166.         } else if (  wch == L' '
  167.                   || wch == L'\t'
  168.                   || wch == L'\r'
  169.                   || wch == L'\n'
  170.                   || wch == L'\0'
  171.                   ) {
  172.             break;
  173.         } else {
  174.             hr = VFW_E_INVALID_FILE_FORMAT;
  175.             return 0;
  176.         }
  177.  
  178.         hr = pIStream->Read( &wch, sizeof(wch), NULL);
  179.         if (FAILED(hr)) {
  180.             return 0;
  181.         }
  182.     }
  183.  
  184.     if (n==0x80000000 && Sign==-1) {
  185.         // This is the negative number that has no positive version!
  186.         return (int)n;
  187.     }
  188.     else return (int)n * Sign;
  189. } // ReadInt
  190.  
  191.  
  192. // The microsoft C/C++ compile generates level 4 warnings to the effect that
  193. // a particular inline function (from some base class) was not needed.
  194. // This line gets rid of hundreds of such unwanted messages and makes
  195. // -W4 compilation feasible:
  196. #pragma warning(disable: 4514)
  197.