1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
#include "stdafx.h"
#include "DSUtil.h"
#include "DSMPropertyBag.h"
//
// IDSMPropertyBagImpl
//
IDSMPropertyBagImpl::IDSMPropertyBagImpl()
{
}
IDSMPropertyBagImpl::~IDSMPropertyBagImpl()
{
}
// IPropertyBag
STDMETHODIMP IDSMPropertyBagImpl::Read(LPCOLESTR pszPropName, VARIANT* pVar, IErrorLog* pErrorLog)
{
CheckPointer(pVar, E_POINTER);
if(pVar->vt != VT_EMPTY) return E_INVALIDARG;
CStringW value = Lookup(pszPropName);
if(value.IsEmpty()) return E_FAIL;
CComVariant(value).Detach(pVar);
return S_OK;
}
STDMETHODIMP IDSMPropertyBagImpl::Write(LPCOLESTR pszPropName, VARIANT* pVar)
{
return SetProperty(pszPropName, pVar);
}
// IPropertyBag2
STDMETHODIMP IDSMPropertyBagImpl::Read(ULONG cProperties, PROPBAG2* pPropBag, IErrorLog* pErrLog, VARIANT* pvarValue, HRESULT* phrError)
{
CheckPointer(pPropBag, E_POINTER);
CheckPointer(pvarValue, E_POINTER);
CheckPointer(phrError, E_POINTER);
for(ULONG i = 0; i < cProperties; phrError[i] = S_OK, i++)
CComVariant(Lookup(pPropBag[i].pstrName)).Detach(pvarValue);
return S_OK;
}
STDMETHODIMP IDSMPropertyBagImpl::Write(ULONG cProperties, PROPBAG2* pPropBag, VARIANT* pvarValue)
{
CheckPointer(pPropBag, E_POINTER);
CheckPointer(pvarValue, E_POINTER);
for(ULONG i = 0; i < cProperties; i++)
SetProperty(pPropBag[i].pstrName, &pvarValue[i]);
return S_OK;
}
STDMETHODIMP IDSMPropertyBagImpl::CountProperties(ULONG* pcProperties)
{
CheckPointer(pcProperties, E_POINTER);
*pcProperties = GetSize();
return S_OK;
}
STDMETHODIMP IDSMPropertyBagImpl::GetPropertyInfo(ULONG iProperty, ULONG cProperties, PROPBAG2* pPropBag, ULONG* pcProperties)
{
CheckPointer(pPropBag, E_POINTER);
CheckPointer(pcProperties, E_POINTER);
for(ULONG i = 0; i < cProperties; i++, iProperty++, (*pcProperties)++)
{
CStringW key = GetKeyAt(iProperty);
pPropBag[i].pstrName = (BSTR)CoTaskMemAlloc((key.GetLength()+1)*sizeof(WCHAR));
if(!pPropBag[i].pstrName) return E_FAIL;
wcscpy_s(pPropBag[i].pstrName, key.GetLength()+1, key);
}
return S_OK;
}
STDMETHODIMP IDSMPropertyBagImpl::LoadObject(LPCOLESTR pstrName, DWORD dwHint, IUnknown* pUnkObject, IErrorLog* pErrLog)
{
return E_NOTIMPL;
}
// IDSMProperyBag
HRESULT IDSMPropertyBagImpl::SetProperty(LPCWSTR key, LPCWSTR value)
{
CheckPointer(key, E_POINTER);
CheckPointer(value, E_POINTER);
if(!Lookup(key).IsEmpty()) SetAt(key, value);
else Add(key, value);
return S_OK;
}
HRESULT IDSMPropertyBagImpl::SetProperty(LPCWSTR key, VARIANT* var)
{
CheckPointer(key, E_POINTER);
CheckPointer(var, E_POINTER);
if((var->vt & (VT_BSTR | VT_BYREF)) != VT_BSTR) return E_INVALIDARG;
return SetProperty(key, var->bstrVal);
}
HRESULT IDSMPropertyBagImpl::GetProperty(LPCWSTR key, BSTR* value)
{
CheckPointer(key, E_POINTER);
CheckPointer(value, E_POINTER);
int i = FindKey(key);
if(i < 0) return E_FAIL;
*value = GetValueAt(i).AllocSysString();
return S_OK;
}
HRESULT IDSMPropertyBagImpl::DelAllProperties()
{
RemoveAll();
return S_OK;
}
HRESULT IDSMPropertyBagImpl::DelProperty(LPCWSTR key)
{
return Remove(key) ? S_OK : S_FALSE;
}
//
// CDSMResource
//
CCritSec CDSMResource::m_csResources;
CAtlMap<DWORD, CDSMResource*> CDSMResource::m_resources;
CDSMResource::CDSMResource()
: mime(_T("application/octet-stream"))
, tag(0)
{
CAutoLock cAutoLock(&m_csResources);
m_resources.SetAt((DWORD)this, this);
}
CDSMResource::CDSMResource(const CDSMResource& r)
{
*this = r;
CAutoLock cAutoLock(&m_csResources);
m_resources.SetAt((DWORD)this, this);
}
CDSMResource::CDSMResource(LPCWSTR name, LPCWSTR desc, LPCWSTR mime, BYTE* pData, int len, DWORD_PTR tag)
{
this->name = name;
this->desc = desc;
this->mime = mime;
data.SetCount(len);
memcpy(data.GetData(), pData, data.GetCount());
this->tag = tag;
CAutoLock cAutoLock(&m_csResources);
m_resources.SetAt((DWORD)this, this);
}
CDSMResource::~CDSMResource()
{
CAutoLock cAutoLock(&m_csResources);
m_resources.RemoveKey((DWORD)this);
}
void CDSMResource::operator = (const CDSMResource& r)
{
tag = r.tag;
name = r.name;
desc = r.desc;
mime = r.mime;
data.Copy(r.data);
}
//
// IDSMResourceBagImpl
//
IDSMResourceBagImpl::IDSMResourceBagImpl()
{
}
// IDSMResourceBag
STDMETHODIMP_(DWORD) IDSMResourceBagImpl::ResGetCount()
{
return m_resources.GetCount();
}
STDMETHODIMP IDSMResourceBagImpl::ResGet(DWORD iIndex, BSTR* ppName, BSTR* ppDesc, BSTR* ppMime, BYTE** ppData, DWORD* pDataLen, DWORD_PTR* pTag)
{
if(ppData) CheckPointer(pDataLen, E_POINTER);
if((INT_PTR)iIndex >= m_resources.GetCount())
return E_INVALIDARG;
CDSMResource& r = m_resources[iIndex];
if(ppName) *ppName = r.name.AllocSysString();
if(ppDesc) *ppDesc = r.desc.AllocSysString();
if(ppMime) *ppMime = r.mime.AllocSysString();
if(ppData) {*pDataLen = r.data.GetCount(); memcpy(*ppData = (BYTE*)CoTaskMemAlloc(*pDataLen), r.data.GetData(), *pDataLen);}
if(pTag) *pTag = r.tag;
return S_OK;
}
STDMETHODIMP IDSMResourceBagImpl::ResSet(DWORD iIndex, LPCWSTR pName, LPCWSTR pDesc, LPCWSTR pMime, BYTE* pData, DWORD len, DWORD_PTR tag)
{
if((INT_PTR)iIndex >= m_resources.GetCount())
return E_INVALIDARG;
CDSMResource& r = m_resources[iIndex];
if(pName) r.name = pName;
if(pDesc) r.desc = pDesc;
if(pMime) r.mime = pMime;
if(pData || len == 0) {r.data.SetCount(len); if(pData) memcpy(r.data.GetData(), pData, r.data.GetCount());}
r.tag = tag;
return S_OK;
}
STDMETHODIMP IDSMResourceBagImpl::ResAppend(LPCWSTR pName, LPCWSTR pDesc, LPCWSTR pMime, BYTE* pData, DWORD len, DWORD_PTR tag)
{
return ResSet(m_resources.Add(CDSMResource()), pName, pDesc, pMime, pData, len, tag);
}
STDMETHODIMP IDSMResourceBagImpl::ResRemoveAt(DWORD iIndex)
{
if((INT_PTR)iIndex >= m_resources.GetCount())
return E_INVALIDARG;
m_resources.RemoveAt(iIndex);
return S_OK;
}
STDMETHODIMP IDSMResourceBagImpl::ResRemoveAll(DWORD_PTR tag)
{
if(tag)
{
for(int i = m_resources.GetCount() - 1; i >= 0; i--)
if(m_resources[i].tag == tag)
m_resources.RemoveAt(i);
}
else
{
m_resources.RemoveAll();
}
return S_OK;
}
//
// CDSMChapter
//
CDSMChapter::CDSMChapter()
{
order = counter++;
rt = 0;
}
CDSMChapter::CDSMChapter(REFERENCE_TIME rt, LPCWSTR name)
{
order = counter++;
this->rt = rt;
this->name = name;
}
void CDSMChapter::operator = (const CDSMChapter& c)
{
order = c.counter;
rt = c.rt;
name = c.name;
}
int CDSMChapter::counter = 0;
int CDSMChapter::Compare(const void* a, const void* b)
{
const CDSMChapter* ca = (const CDSMChapter*)a;
const CDSMChapter* cb = (const CDSMChapter*)b;
if(ca->rt > cb->rt) return 1;
else if(ca->rt < cb->rt) return -1;
return ca->order - cb->order;
}
//
// IDSMChapterBagImpl
//
IDSMChapterBagImpl::IDSMChapterBagImpl()
{
m_fSorted = false;
}
// IDSMRChapterBag
STDMETHODIMP_(DWORD) IDSMChapterBagImpl::ChapGetCount()
{
return m_chapters.GetCount();
}
STDMETHODIMP IDSMChapterBagImpl::ChapGet(DWORD iIndex, REFERENCE_TIME* prt, BSTR* ppName)
{
if((INT_PTR)iIndex >= m_chapters.GetCount())
return E_INVALIDARG;
CDSMChapter& c = m_chapters[iIndex];
if(prt) *prt = c.rt;
if(ppName) *ppName = c.name.AllocSysString();
return S_OK;
}
STDMETHODIMP IDSMChapterBagImpl::ChapSet(DWORD iIndex, REFERENCE_TIME rt, LPCWSTR pName)
{
if((INT_PTR)iIndex >= m_chapters.GetCount())
return E_INVALIDARG;
CDSMChapter& c = m_chapters[iIndex];
c.rt = rt;
if(pName) c.name = pName;
m_fSorted = false;
return S_OK;
}
STDMETHODIMP IDSMChapterBagImpl::ChapAppend(REFERENCE_TIME rt, LPCWSTR pName)
{
return ChapSet(m_chapters.Add(CDSMChapter()), rt, pName);
}
STDMETHODIMP IDSMChapterBagImpl::ChapRemoveAt(DWORD iIndex)
{
if((INT_PTR)iIndex >= m_chapters.GetCount())
return E_INVALIDARG;
m_chapters.RemoveAt(iIndex);
return S_OK;
}
STDMETHODIMP IDSMChapterBagImpl::ChapRemoveAll()
{
m_chapters.RemoveAll();
m_fSorted = false;
return S_OK;
}
STDMETHODIMP_(long) IDSMChapterBagImpl::ChapLookup(REFERENCE_TIME* prt, BSTR* ppName)
{
CheckPointer(prt, -1);
ChapSort();
int i = range_bsearch(m_chapters, *prt);
if(i < 0) return -1;
*prt = m_chapters[i].rt;
if(ppName) *ppName = m_chapters[i].name.AllocSysString();
return i;
}
STDMETHODIMP IDSMChapterBagImpl::ChapSort()
{
if(m_fSorted) return S_FALSE;
qsort(m_chapters.GetData(), m_chapters.GetCount(), sizeof(CDSMChapter), CDSMChapter::Compare);
m_fSorted = true;
return S_OK;
}
//
// CDSMChapterBag
//
CDSMChapterBag::CDSMChapterBag(LPUNKNOWN pUnk, HRESULT* phr)
: CUnknown(_T("CDSMChapterBag"), NULL)
{
}
STDMETHODIMP CDSMChapterBag::NonDelegatingQueryInterface(REFIID riid, void** ppv)
{
CheckPointer(ppv, E_POINTER);
return
QI(IDSMChapterBag)
__super::NonDelegatingQueryInterface(riid, ppv);
}
|