A Fast Bresenham Type Algorithm For Drawing Ellipses by John Kennedy
[xy_vsfilter.git] / src / filters / BaseClasses / pstream.cpp
blobe91ff2af07efedfdf28f9d7a0dc70b400cee346b
1 //------------------------------------------------------------------------------
2 // File: PStream.cpp
3 //
4 // Desc: DirectShow base classes.
5 //
6 // Copyright (c) 1992-2002 Microsoft Corporation. All rights reserved.
7 //------------------------------------------------------------------------------
10 #include <streams.h>
12 #ifdef PERF
13 #include <measure.h>
14 #endif
18 // Constructor
20 CPersistStream::CPersistStream(IUnknown *punk, HRESULT *phr)
21 : mPS_fDirty(FALSE)
23 mPS_dwFileVersion = GetSoftwareVersion();
28 // Destructor
30 CPersistStream::~CPersistStream() {
31 // Nothing to do
34 #if 0
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);
49 else {
50 return CUnknown::NonDelegatingQueryInterface(riid, ppv);
53 #endif
57 // WriteToStream
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);
65 return NOERROR;
70 HRESULT CPersistStream::ReadFromStream(IStream * pStream)
72 // You can override this to do things like
73 // hr = pStream->Read(MyStructure, sizeof(MyStructure), NULL);
75 return NOERROR;
80 // Load
82 // Load all the data from the given stream
83 STDMETHODIMP CPersistStream::Load(LPSTREAM pStm)
85 HRESULT hr;
86 // Load the version number then the data
87 mPS_dwFileVersion = ReadInt(pStm, hr);
88 if (FAILED(hr)) {
89 return hr;
92 return ReadFromStream(pStm);
93 } // Load
98 // Save
100 // Save the contents of this Stream.
101 STDMETHODIMP CPersistStream::Save(LPSTREAM pStm, BOOL fClearDirty)
104 HRESULT hr = WriteInt(pStm, GetSoftwareVersion());
105 if (FAILED(hr)) {
106 return hr;
109 hr = WriteToStream(pStm);
110 if (FAILED(hr)) {
111 return hr;
114 mPS_fDirty = !fClearDirty;
116 return hr;
117 } // Save
120 // WriteInt
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);
133 } // WriteInt
136 // ReadInt
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)
146 int Sign = 1;
147 unsigned int n = 0; // result wil be n*Sign
148 WCHAR wch;
150 hr = pIStream->Read( &wch, sizeof(wch), NULL);
151 if (FAILED(hr)) {
152 return 0;
155 if (wch==L'-'){
156 Sign = -1;
157 hr = pIStream->Read( &wch, sizeof(wch), NULL);
158 if (FAILED(hr)) {
159 return 0;
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'
172 break;
173 } else {
174 hr = VFW_E_INVALID_FILE_FORMAT;
175 return 0;
178 hr = pIStream->Read( &wch, sizeof(wch), NULL);
179 if (FAILED(hr)) {
180 return 0;
184 if (n==0x80000000 && Sign==-1) {
185 // This is the negative number that has no positive version!
186 return (int)n;
188 else return (int)n * Sign;
189 } // ReadInt
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)