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