Leaked source code of windows server 2003
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.

196 lines
4.7 KiB

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