home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / mac / SiteBldr / AMOVIE / SDK / _SETUP / COMMON.Z / pstream.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  4.8 KB  |  199 lines

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