1 //------------------------------------------------------------------------------
4 // Desc: DirectShow base classes.
6 // Copyright (c) 1992-2002 Microsoft Corporation. All rights reserved.
7 //------------------------------------------------------------------------------
20 CPersistStream::CPersistStream(IUnknown
*punk
, HRESULT
*phr
)
23 mPS_dwFileVersion
= GetSoftwareVersion();
30 CPersistStream::~CPersistStream() {
35 SAMPLE CODE TO COPY
- not active at the moment
38 // NonDelegatingQueryInterface
40 // This object supports IPersist & IPersistStream
41 STDMETHODIMP
CPersistStream::NonDelegatingQueryInterface(REFIID riid
, void **ppv
)
43 if (riid
== IID_IPersist
) {
44 return GetInterface((IPersist
*) this, ppv
);
46 else if (riid
== IID_IPersistStream
) {
47 return GetInterface((IPersistStream
*) this, ppv
);
50 return CUnknown::NonDelegatingQueryInterface(riid
, ppv
);
59 // Writes to the stream (default action is to write nothing)
60 HRESULT
CPersistStream::WriteToStream(IStream
*pStream
)
62 // You can override this to do things like
63 // hr = pStream->Write(MyStructure, sizeof(MyStructure), NULL);
70 HRESULT
CPersistStream::ReadFromStream(IStream
* pStream
)
72 // You can override this to do things like
73 // hr = pStream->Read(MyStructure, sizeof(MyStructure), NULL);
82 // Load all the data from the given stream
83 STDMETHODIMP
CPersistStream::Load(LPSTREAM pStm
)
86 // Load the version number then the data
87 mPS_dwFileVersion
= ReadInt(pStm
, hr
);
92 return ReadFromStream(pStm
);
100 // Save the contents of this Stream.
101 STDMETHODIMP
CPersistStream::Save(LPSTREAM pStm
, BOOL fClearDirty
)
104 HRESULT hr
= WriteInt(pStm
, GetSoftwareVersion());
109 hr
= WriteToStream(pStm
);
114 mPS_fDirty
= !fClearDirty
;
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.
128 STDAPI
WriteInt(IStream
*pIStream
, int n
)
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
);
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)
143 STDAPI_(int) ReadInt(IStream
*pIStream
, HRESULT
&hr
)
147 unsigned int n
= 0; // result wil be n*Sign
150 hr
= pIStream
->Read( &wch
, sizeof(wch
), NULL
);
157 hr
= pIStream
->Read( &wch
, sizeof(wch
), NULL
);
164 if (wch
>=L
'0' && wch
<=L
'9') {
165 n
= 10*n
+(int)(wch
-L
'0');
166 } else if ( wch
== L
' '
174 hr
= VFW_E_INVALID_FILE_FORMAT
;
178 hr
= pIStream
->Read( &wch
, sizeof(wch
), NULL
);
184 if (n
==0x80000000 && Sign
==-1) {
185 // This is the negative number that has no positive version!
188 else return (int)n
* Sign
;
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)