Fix MsiRecordSetString for NULL strings and update test case.
[wine.git] / dlls / dmusic / collection.c
blob3b1bd9c7bd9f8b1afd870f33c51855c0c3b7b984
1 /* IDirectMusicCollection Implementation
3 * Copyright (C) 2003-2004 Rok Mandeljc
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Library General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include "dmusic_private.h"
22 WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
23 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
25 /*****************************************************************************
26 * IDirectMusicCollectionImpl implementation
28 /* IDirectMusicCollectionImpl IUnknown part: */
29 HRESULT WINAPI IDirectMusicCollectionImpl_IUnknown_QueryInterface (LPUNKNOWN iface, REFIID riid, LPVOID *ppobj) {
30 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, UnknownVtbl, iface);
31 TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ppobj);
33 if (IsEqualIID (riid, &IID_IUnknown)) {
34 *ppobj = (LPVOID)&This->UnknownVtbl;
35 IDirectMusicCollectionImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
36 return S_OK;
37 } else if (IsEqualIID (riid, &IID_IDirectMusicCollection)) {
38 *ppobj = (LPVOID)&This->CollectionVtbl;
39 IDirectMusicCollectionImpl_IDirectMusicCollection_AddRef ((LPDIRECTMUSICCOLLECTION)&This->CollectionVtbl);
40 return S_OK;
41 } else if (IsEqualIID (riid, &IID_IDirectMusicObject)) {
42 *ppobj = (LPVOID)&This->ObjectVtbl;
43 IDirectMusicCollectionImpl_IDirectMusicObject_AddRef ((LPDIRECTMUSICOBJECT)&This->ObjectVtbl);
44 return S_OK;
45 } else if (IsEqualIID (riid, &IID_IPersistStream)) {
46 *ppobj = (LPVOID)&This->PersistStreamVtbl;
47 IDirectMusicCollectionImpl_IPersistStream_AddRef ((LPPERSISTSTREAM)&This->PersistStreamVtbl);
48 return S_OK;
51 WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ppobj);
52 return E_NOINTERFACE;
55 ULONG WINAPI IDirectMusicCollectionImpl_IUnknown_AddRef (LPUNKNOWN iface) {
56 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, UnknownVtbl, iface);
57 ULONG refCount = InterlockedIncrement(&This->ref);
59 TRACE("(%p)->(ref before=%lu)\n", This, refCount - 1);
61 DMUSIC_LockModule();
63 return refCount;
66 ULONG WINAPI IDirectMusicCollectionImpl_IUnknown_Release (LPUNKNOWN iface) {
67 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, UnknownVtbl, iface);
68 ULONG refCount = InterlockedDecrement(&This->ref);
70 TRACE("(%p)->(ref before=%lu)\n", This, refCount + 1);
72 if (!refCount) {
73 HeapFree(GetProcessHeap(), 0, This);
76 DMUSIC_UnlockModule();
78 return refCount;
81 IUnknownVtbl DirectMusicCollection_Unknown_Vtbl = {
82 IDirectMusicCollectionImpl_IUnknown_QueryInterface,
83 IDirectMusicCollectionImpl_IUnknown_AddRef,
84 IDirectMusicCollectionImpl_IUnknown_Release
87 /* IDirectMusicCollectionImpl IDirectMusicCollection part: */
88 HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_QueryInterface (LPDIRECTMUSICCOLLECTION iface, REFIID riid, LPVOID *ppobj) {
89 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
90 return IDirectMusicCollectionImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
93 ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_AddRef (LPDIRECTMUSICCOLLECTION iface) {
94 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
95 return IDirectMusicCollectionImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
98 ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_Release (LPDIRECTMUSICCOLLECTION iface) {
99 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
100 return IDirectMusicCollectionImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
103 /* IDirectMusicCollection Interface follow: */
104 HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_GetInstrument (LPDIRECTMUSICCOLLECTION iface, DWORD dwPatch, IDirectMusicInstrument** ppInstrument) {
105 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
106 DMUS_PRIVATE_INSTRUMENTENTRY *tmpEntry;
107 struct list *listEntry;
108 DWORD dwInstPatch;
110 TRACE("(%p, %ld, %p)\n", This, dwPatch, ppInstrument);
112 LIST_FOR_EACH (listEntry, &This->Instruments) {
113 tmpEntry = LIST_ENTRY(listEntry, DMUS_PRIVATE_INSTRUMENTENTRY, entry);
114 IDirectMusicInstrument_GetPatch (tmpEntry->pInstrument, &dwInstPatch);
115 if (dwPatch == dwInstPatch) {
116 *ppInstrument = (LPDIRECTMUSICINSTRUMENT)tmpEntry->pInstrument;
117 IDirectMusicInstrument_AddRef (tmpEntry->pInstrument);
118 IDirectMusicInstrumentImpl_Custom_Load (tmpEntry->pInstrument, This->pStm); /* load instrument before returning it */
119 TRACE(": returning instrument %p\n", *ppInstrument);
120 return S_OK;
124 TRACE(": instrument not found\n");
126 return DMUS_E_INVALIDPATCH;
129 HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_EnumInstrument (LPDIRECTMUSICCOLLECTION iface, DWORD dwIndex, DWORD* pdwPatch, LPWSTR pwszName, DWORD dwNameLen) {
130 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
131 unsigned int r = 0;
132 DMUS_PRIVATE_INSTRUMENTENTRY *tmpEntry;
133 struct list *listEntry;
135 TRACE("(%p, %ld, %p, %p, %ld)\n", This, dwIndex, pdwPatch, pwszName, dwNameLen);
136 LIST_FOR_EACH (listEntry, &This->Instruments) {
137 tmpEntry = LIST_ENTRY(listEntry, DMUS_PRIVATE_INSTRUMENTENTRY, entry);
138 if (r == dwIndex) {
139 ICOM_NAME_MULTI (IDirectMusicInstrumentImpl, InstrumentVtbl, tmpEntry->pInstrument, pInstrument);
140 IDirectMusicInstrument_GetPatch (tmpEntry->pInstrument, pdwPatch);
141 dwNameLen = strlenW (pInstrument->wszName);
142 strncpyW (pwszName, pInstrument->wszName, dwNameLen);
143 return S_OK;
145 r++;
148 return S_FALSE;
151 IDirectMusicCollectionVtbl DirectMusicCollection_Collection_Vtbl = {
152 IDirectMusicCollectionImpl_IDirectMusicCollection_QueryInterface,
153 IDirectMusicCollectionImpl_IDirectMusicCollection_AddRef,
154 IDirectMusicCollectionImpl_IDirectMusicCollection_Release,
155 IDirectMusicCollectionImpl_IDirectMusicCollection_GetInstrument,
156 IDirectMusicCollectionImpl_IDirectMusicCollection_EnumInstrument
159 /* IDirectMusicCollectionImpl IDirectMusicObject part: */
160 HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_QueryInterface (LPDIRECTMUSICOBJECT iface, REFIID riid, LPVOID *ppobj) {
161 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
162 return IDirectMusicCollectionImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
165 ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface) {
166 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
167 return IDirectMusicCollectionImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
170 ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_Release (LPDIRECTMUSICOBJECT iface) {
171 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
172 return IDirectMusicCollectionImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
175 HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_GetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
176 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
177 TRACE("(%p, %p)\n", This, pDesc);
178 /* I think we shouldn't return pointer here since then values can be changed; it'd be a mess */
179 memcpy (pDesc, This->pDesc, This->pDesc->dwSize);
180 return S_OK;
183 HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_SetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
184 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
185 TRACE("(%p, %p): setting descriptor:\n%s\n", This, pDesc, debugstr_DMUS_OBJECTDESC (pDesc));
187 /* According to MSDN, we should copy only given values, not whole struct */
188 if (pDesc->dwValidData & DMUS_OBJ_OBJECT)
189 memcpy (&This->pDesc->guidObject, &pDesc->guidObject, sizeof (pDesc->guidObject));
190 if (pDesc->dwValidData & DMUS_OBJ_CLASS)
191 memcpy (&This->pDesc->guidClass, &pDesc->guidClass, sizeof (pDesc->guidClass));
192 if (pDesc->dwValidData & DMUS_OBJ_NAME)
193 strncpyW (This->pDesc->wszName, pDesc->wszName, DMUS_MAX_NAME);
194 if (pDesc->dwValidData & DMUS_OBJ_CATEGORY)
195 strncpyW (This->pDesc->wszCategory, pDesc->wszCategory, DMUS_MAX_CATEGORY);
196 if (pDesc->dwValidData & DMUS_OBJ_FILENAME)
197 strncpyW (This->pDesc->wszFileName, pDesc->wszFileName, DMUS_MAX_FILENAME);
198 if (pDesc->dwValidData & DMUS_OBJ_VERSION)
199 memcpy (&This->pDesc->vVersion, &pDesc->vVersion, sizeof (pDesc->vVersion));
200 if (pDesc->dwValidData & DMUS_OBJ_DATE)
201 memcpy (&This->pDesc->ftDate, &pDesc->ftDate, sizeof (pDesc->ftDate));
202 if (pDesc->dwValidData & DMUS_OBJ_MEMORY) {
203 memcpy (&This->pDesc->llMemLength, &pDesc->llMemLength, sizeof (pDesc->llMemLength));
204 memcpy (This->pDesc->pbMemData, pDesc->pbMemData, sizeof (pDesc->pbMemData));
206 if (pDesc->dwValidData & DMUS_OBJ_STREAM) {
207 /* according to MSDN, we copy the stream */
208 IStream_Clone (pDesc->pStream, &This->pDesc->pStream);
211 /* add new flags */
212 This->pDesc->dwValidData |= pDesc->dwValidData;
214 return S_OK;
217 HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_ParseDescriptor (LPDIRECTMUSICOBJECT iface, LPSTREAM pStream, LPDMUS_OBJECTDESC pDesc) {
218 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
219 DMUS_PRIVATE_CHUNK Chunk;
220 DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
221 LARGE_INTEGER liMove; /* used when skipping chunks */
223 TRACE("(%p, %p, %p)\n", This, pStream, pDesc);
225 /* FIXME: should this be determined from stream? */
226 pDesc->dwValidData |= DMUS_OBJ_CLASS;
227 memcpy (&pDesc->guidClass, &CLSID_DirectMusicCollection, sizeof(CLSID));
229 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
230 TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
231 switch (Chunk.fccID) {
232 case FOURCC_RIFF: {
233 IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);
234 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
235 StreamSize = Chunk.dwSize - sizeof(FOURCC);
236 StreamCount = 0;
237 if (Chunk.fccID == mmioFOURCC('D','L','S',' ')) {
238 TRACE_(dmfile)(": collection form\n");
239 do {
240 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
241 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
242 TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
243 switch (Chunk.fccID) {
244 case FOURCC_DLID: {
245 TRACE_(dmfile)(": GUID chunk\n");
246 pDesc->dwValidData |= DMUS_OBJ_OBJECT;
247 IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
248 break;
250 case DMUS_FOURCC_VERSION_CHUNK: {
251 TRACE_(dmfile)(": version chunk\n");
252 pDesc->dwValidData |= DMUS_OBJ_VERSION;
253 IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
254 break;
256 case DMUS_FOURCC_CATEGORY_CHUNK: {
257 TRACE_(dmfile)(": category chunk\n");
258 pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
259 IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
260 break;
262 case FOURCC_LIST: {
263 IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);
264 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
265 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
266 ListCount[0] = 0;
267 switch (Chunk.fccID) {
268 /* pure INFO list, such can be found in dls collections */
269 case mmioFOURCC('I','N','F','O'): {
270 TRACE_(dmfile)(": INFO list\n");
271 do {
272 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
273 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
274 TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
275 switch (Chunk.fccID) {
276 case mmioFOURCC('I','N','A','M'):{
277 CHAR szName[DMUS_MAX_NAME];
278 TRACE_(dmfile)(": name chunk\n");
279 pDesc->dwValidData |= DMUS_OBJ_NAME;
280 IStream_Read (pStream, szName, Chunk.dwSize, NULL);
281 MultiByteToWideChar (CP_ACP, 0, szName, -1, pDesc->wszName, DMUS_MAX_NAME);
282 if (even_or_odd(Chunk.dwSize)) {
283 ListCount[0] ++;
284 liMove.QuadPart = 1;
285 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
287 break;
289 case mmioFOURCC('I','A','R','T'): {
290 TRACE_(dmfile)(": artist chunk (ignored)\n");
291 if (even_or_odd(Chunk.dwSize)) {
292 ListCount[0] ++;
293 Chunk.dwSize++;
295 liMove.QuadPart = Chunk.dwSize;
296 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
297 break;
299 case mmioFOURCC('I','C','O','P'): {
300 TRACE_(dmfile)(": copyright chunk (ignored)\n");
301 if (even_or_odd(Chunk.dwSize)) {
302 ListCount[0] ++;
303 Chunk.dwSize++;
305 liMove.QuadPart = Chunk.dwSize;
306 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
307 break;
309 case mmioFOURCC('I','S','B','J'): {
310 TRACE_(dmfile)(": subject chunk (ignored)\n");
311 if (even_or_odd(Chunk.dwSize)) {
312 ListCount[0] ++;
313 Chunk.dwSize++;
315 liMove.QuadPart = Chunk.dwSize;
316 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
317 break;
319 case mmioFOURCC('I','C','M','T'): {
320 TRACE_(dmfile)(": comment chunk (ignored)\n");
321 if (even_or_odd(Chunk.dwSize)) {
322 ListCount[0] ++;
323 Chunk.dwSize++;
325 liMove.QuadPart = Chunk.dwSize;
326 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
327 break;
329 default: {
330 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
331 if (even_or_odd(Chunk.dwSize)) {
332 ListCount[0] ++;
333 Chunk.dwSize++;
335 liMove.QuadPart = Chunk.dwSize;
336 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
337 break;
340 TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
341 } while (ListCount[0] < ListSize[0]);
342 break;
344 default: {
345 TRACE_(dmfile)(": unknown (skipping)\n");
346 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
347 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
348 break;
351 break;
353 default: {
354 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
355 liMove.QuadPart = Chunk.dwSize;
356 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
357 break;
360 TRACE_(dmfile)(": StreamCount[0] = %ld < StreamSize[0] = %ld\n", StreamCount, StreamSize);
361 } while (StreamCount < StreamSize);
362 } else {
363 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
364 liMove.QuadPart = StreamSize;
365 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
366 return E_FAIL;
369 TRACE_(dmfile)(": reading finished\n");
370 break;
372 default: {
373 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
374 liMove.QuadPart = Chunk.dwSize;
375 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
376 return DMUS_E_INVALIDFILE;
380 TRACE(": returning descriptor:\n%s\n", debugstr_DMUS_OBJECTDESC (pDesc));
382 return S_OK;
385 IDirectMusicObjectVtbl DirectMusicCollection_Object_Vtbl = {
386 IDirectMusicCollectionImpl_IDirectMusicObject_QueryInterface,
387 IDirectMusicCollectionImpl_IDirectMusicObject_AddRef,
388 IDirectMusicCollectionImpl_IDirectMusicObject_Release,
389 IDirectMusicCollectionImpl_IDirectMusicObject_GetDescriptor,
390 IDirectMusicCollectionImpl_IDirectMusicObject_SetDescriptor,
391 IDirectMusicCollectionImpl_IDirectMusicObject_ParseDescriptor
394 /* IDirectMusicCollectionImpl IPersistStream part: */
395 HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
396 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, PersistStreamVtbl, iface);
397 return IDirectMusicCollectionImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
400 ULONG WINAPI IDirectMusicCollectionImpl_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
401 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, PersistStreamVtbl, iface);
402 return IDirectMusicCollectionImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
405 ULONG WINAPI IDirectMusicCollectionImpl_IPersistStream_Release (LPPERSISTSTREAM iface) {
406 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, PersistStreamVtbl, iface);
407 return IDirectMusicCollectionImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
410 HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
411 return E_NOTIMPL;
414 HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
415 return E_NOTIMPL;
418 HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
419 ICOM_THIS_MULTI(IDirectMusicCollectionImpl, PersistStreamVtbl, iface);
421 DMUS_PRIVATE_CHUNK Chunk;
422 DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
423 LARGE_INTEGER liMove; /* used when skipping chunks */
424 ULARGE_INTEGER dlibCollectionPosition, dlibInstrumentPosition, dlibWavePoolPosition;
426 IStream_AddRef (pStm); /* add count for later references */
427 liMove.QuadPart = 0;
428 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, &dlibCollectionPosition); /* store offset, in case it'll be needed later */
429 This->liCollectionPosition.QuadPart = dlibCollectionPosition.QuadPart;
430 This->pStm = pStm;
432 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
433 TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
434 switch (Chunk.fccID) {
435 case FOURCC_RIFF: {
436 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
437 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
438 StreamSize = Chunk.dwSize - sizeof(FOURCC);
439 StreamCount = 0;
440 switch (Chunk.fccID) {
441 case FOURCC_DLS: {
442 TRACE_(dmfile)(": collection form\n");
443 do {
444 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
445 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
446 TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
447 switch (Chunk.fccID) {
448 case FOURCC_COLH: {
449 TRACE_(dmfile)(": collection header chunk\n");
450 This->pHeader = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
451 IStream_Read (pStm, This->pHeader, Chunk.dwSize, NULL);
452 break;
454 case FOURCC_DLID: {
455 TRACE_(dmfile)(": DLID (GUID) chunk\n");
456 This->pDesc->dwValidData |= DMUS_OBJ_OBJECT;
457 IStream_Read (pStm, &This->pDesc->guidObject, Chunk.dwSize, NULL);
458 break;
460 case FOURCC_VERS: {
461 TRACE_(dmfile)(": version chunk\n");
462 This->pDesc->dwValidData |= DMUS_OBJ_VERSION;
463 IStream_Read (pStm, &This->pDesc->vVersion, Chunk.dwSize, NULL);
464 break;
466 case FOURCC_PTBL: {
467 TRACE_(dmfile)(": pool table chunk\n");
468 This->pPoolTable = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(POOLTABLE));
469 IStream_Read (pStm, This->pPoolTable, sizeof(POOLTABLE), NULL);
470 Chunk.dwSize -= sizeof(POOLTABLE);
471 This->pPoolCues = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, This->pPoolTable->cCues*sizeof(POOLCUE));
472 IStream_Read (pStm, This->pPoolCues, Chunk.dwSize, NULL);
473 break;
475 case FOURCC_LIST: {
476 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
477 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
478 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
479 ListCount[0] = 0;
480 switch (Chunk.fccID) {
481 case mmioFOURCC('I','N','F','O'): {
482 TRACE_(dmfile)(": INFO list\n");
483 do {
484 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
485 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
486 TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
487 switch (Chunk.fccID) {
488 case mmioFOURCC('I','N','A','M'): {
489 CHAR szName[DMUS_MAX_NAME];
490 TRACE_(dmfile)(": name chunk\n");
491 This->pDesc->dwValidData |= DMUS_OBJ_NAME;
492 IStream_Read (pStm, szName, Chunk.dwSize, NULL);
493 MultiByteToWideChar (CP_ACP, 0, szName, -1, This->pDesc->wszName, DMUS_MAX_NAME);
494 if (even_or_odd(Chunk.dwSize)) {
495 ListCount[0] ++;
496 liMove.QuadPart = 1;
497 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
499 break;
501 case mmioFOURCC('I','A','R','T'): {
502 TRACE_(dmfile)(": artist chunk (ignored)\n");
503 if (even_or_odd(Chunk.dwSize)) {
504 ListCount[0] ++;
505 Chunk.dwSize++;
507 liMove.QuadPart = Chunk.dwSize;
508 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
509 break;
511 case mmioFOURCC('I','C','O','P'): {
512 TRACE_(dmfile)(": copyright chunk\n");
513 This->szCopyright = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
514 IStream_Read (pStm, This->szCopyright, Chunk.dwSize, NULL);
515 if (even_or_odd(Chunk.dwSize)) {
516 ListCount[0] ++;
517 liMove.QuadPart = 1;
518 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
520 break;
522 case mmioFOURCC('I','S','B','J'): {
523 TRACE_(dmfile)(": subject chunk (ignored)\n");
524 if (even_or_odd(Chunk.dwSize)) {
525 ListCount[0] ++;
526 Chunk.dwSize++;
528 liMove.QuadPart = Chunk.dwSize;
529 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
530 break;
532 case mmioFOURCC('I','C','M','T'): {
533 TRACE_(dmfile)(": comment chunk (ignored)\n");
534 if (even_or_odd(Chunk.dwSize)) {
535 ListCount[0] ++;
536 Chunk.dwSize++;
538 liMove.QuadPart = Chunk.dwSize;
539 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
540 break;
542 default: {
543 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
544 if (even_or_odd(Chunk.dwSize)) {
545 ListCount[0] ++;
546 Chunk.dwSize++;
548 liMove.QuadPart = Chunk.dwSize;
549 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
550 break;
553 TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
554 } while (ListCount[0] < ListSize[0]);
555 break;
557 case FOURCC_WVPL: {
558 TRACE_(dmfile)(": wave pool list (mark & skip)\n");
559 liMove.QuadPart = 0;
560 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, &dlibWavePoolPosition); /* store position */
561 This->liWavePoolTablePosition.QuadPart = dlibWavePoolPosition.QuadPart;
562 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
563 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
564 break;
566 case FOURCC_LINS: {
567 TRACE_(dmfile)(": instruments list\n");
568 do {
569 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
570 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
571 TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
572 switch (Chunk.fccID) {
573 case FOURCC_LIST: {
574 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
575 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
576 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
577 ListCount[1] = 0;
578 switch (Chunk.fccID) {
579 case FOURCC_INS: {
580 LPDMUS_PRIVATE_INSTRUMENTENTRY pNewInstrument = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_INSTRUMENTENTRY));
581 TRACE_(dmfile)(": instrument list\n");
582 DMUSIC_CreateDirectMusicInstrumentImpl (&IID_IDirectMusicInstrument, (LPVOID*)&pNewInstrument->pInstrument, NULL); /* only way to create this one... even M$ does it discretly */
584 ICOM_NAME_MULTI (IDirectMusicInstrumentImpl, InstrumentVtbl, pNewInstrument->pInstrument, pInstrument);
585 liMove.QuadPart = 0;
586 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, &dlibInstrumentPosition);
587 pInstrument->liInstrumentPosition.QuadPart = dlibInstrumentPosition.QuadPart - (2*sizeof(FOURCC) + sizeof(DWORD)); /* store offset, it'll be needed later */
589 do {
590 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
591 ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
592 TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
593 switch (Chunk.fccID) {
594 case FOURCC_INSH: {
595 TRACE_(dmfile)(": instrument header chunk\n");
596 pInstrument->pHeader = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
597 IStream_Read (pStm, pInstrument->pHeader, Chunk.dwSize, NULL);
598 break;
600 case FOURCC_DLID: {
601 TRACE_(dmfile)(": DLID (GUID) chunk\n");
602 pInstrument->pInstrumentID = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
603 IStream_Read (pStm, pInstrument->pInstrumentID, Chunk.dwSize, NULL);
604 break;
606 case FOURCC_LIST: {
607 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
608 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
609 ListSize[2] = Chunk.dwSize - sizeof(FOURCC);
610 ListCount[2] = 0;
611 switch (Chunk.fccID) {
612 default: {
613 TRACE_(dmfile)(": unknown (skipping)\n");
614 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
615 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
616 break;
619 break;
621 default: {
622 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
623 liMove.QuadPart = Chunk.dwSize;
624 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
625 break;
628 TRACE_(dmfile)(": ListCount[1] = %ld < ListSize[1] = %ld\n", ListCount[1], ListSize[1]);
629 } while (ListCount[1] < ListSize[1]);
630 /* DEBUG: dumps whole instrument object tree: */
631 if (TRACE_ON(dmusic)) {
632 TRACE("*** IDirectMusicInstrument (%p) ***\n", pInstrument);
633 if (pInstrument->pInstrumentID)
634 TRACE(" - GUID = %s\n", debugstr_dmguid(pInstrument->pInstrumentID));
636 TRACE(" - Instrument header:\n");
637 TRACE(" - cRegions: %ld\n", pInstrument->pHeader->cRegions);
638 TRACE(" - Locale:\n");
639 TRACE(" - ulBank: %ld\n", pInstrument->pHeader->Locale.ulBank);
640 TRACE(" - ulInstrument: %ld\n", pInstrument->pHeader->Locale.ulInstrument);
641 TRACE(" => dwPatch: %ld\n", MIDILOCALE2Patch(&pInstrument->pHeader->Locale));
643 list_add_tail (&This->Instruments, &pNewInstrument->entry);
645 break;
648 break;
650 default: {
651 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
652 liMove.QuadPart = Chunk.dwSize;
653 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
654 break;
657 TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
658 } while (ListCount[0] < ListSize[0]);
659 break;
661 default: {
662 TRACE_(dmfile)(": unknown (skipping)\n");
663 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
664 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
665 break;
668 break;
670 default: {
671 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
672 liMove.QuadPart = Chunk.dwSize;
673 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
674 break;
677 TRACE_(dmfile)(": StreamCount = %ld < StreamSize = %ld\n", StreamCount, StreamSize);
678 } while (StreamCount < StreamSize);
679 break;
681 default: {
682 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
683 liMove.QuadPart = StreamSize;
684 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
685 return E_FAIL;
688 TRACE_(dmfile)(": reading finished\n");
689 break;
691 default: {
692 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
693 liMove.QuadPart = Chunk.dwSize;
694 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
695 return E_FAIL;
699 /* DEBUG: dumps whole collection object tree: */
700 if (TRACE_ON(dmusic)) {
701 int r = 0;
702 DMUS_PRIVATE_INSTRUMENTENTRY *tmpEntry;
703 struct list *listEntry;
705 TRACE("*** IDirectMusicCollection (%p) ***\n", This->CollectionVtbl);
706 if (This->pDesc->dwValidData & DMUS_OBJ_OBJECT)
707 TRACE(" - GUID = %s\n", debugstr_dmguid(&This->pDesc->guidObject));
708 if (This->pDesc->dwValidData & DMUS_OBJ_VERSION)
709 TRACE(" - Version = %i,%i,%i,%i\n", (This->pDesc->vVersion.dwVersionMS >> 8) && 0x0000FFFF, This->pDesc->vVersion.dwVersionMS && 0x0000FFFF, \
710 (This->pDesc->vVersion.dwVersionLS >> 8) && 0x0000FFFF, This->pDesc->vVersion.dwVersionLS && 0x0000FFFF);
711 if (This->pDesc->dwValidData & DMUS_OBJ_NAME)
712 TRACE(" - Name = %s\n", debugstr_w(This->pDesc->wszName));
714 TRACE(" - Collection header:\n");
715 TRACE(" - cInstruments: %ld\n", This->pHeader->cInstruments);
716 TRACE(" - Instruments:\n");
718 LIST_FOR_EACH (listEntry, &This->Instruments) {
719 tmpEntry = LIST_ENTRY( listEntry, DMUS_PRIVATE_INSTRUMENTENTRY, entry );
720 TRACE(" - Instrument[%i]: %p\n", r, tmpEntry->pInstrument);
721 r++;
725 return S_OK;
728 HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
729 return E_NOTIMPL;
732 HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
733 return E_NOTIMPL;
736 IPersistStreamVtbl DirectMusicCollection_PersistStream_Vtbl = {
737 IDirectMusicCollectionImpl_IPersistStream_QueryInterface,
738 IDirectMusicCollectionImpl_IPersistStream_AddRef,
739 IDirectMusicCollectionImpl_IPersistStream_Release,
740 IDirectMusicCollectionImpl_IPersistStream_GetClassID,
741 IDirectMusicCollectionImpl_IPersistStream_IsDirty,
742 IDirectMusicCollectionImpl_IPersistStream_Load,
743 IDirectMusicCollectionImpl_IPersistStream_Save,
744 IDirectMusicCollectionImpl_IPersistStream_GetSizeMax
748 /* for ClassFactory */
749 HRESULT WINAPI DMUSIC_CreateDirectMusicCollectionImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
750 IDirectMusicCollectionImpl* obj;
752 obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicCollectionImpl));
753 if (NULL == obj) {
754 *ppobj = NULL;
755 return E_OUTOFMEMORY;
757 obj->UnknownVtbl = &DirectMusicCollection_Unknown_Vtbl;
758 obj->CollectionVtbl = &DirectMusicCollection_Collection_Vtbl;
759 obj->ObjectVtbl = &DirectMusicCollection_Object_Vtbl;
760 obj->PersistStreamVtbl = &DirectMusicCollection_PersistStream_Vtbl;
761 obj->pDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DMUS_OBJECTDESC));
762 DM_STRUCT_INIT(obj->pDesc);
763 obj->pDesc->dwValidData |= DMUS_OBJ_CLASS;
764 memcpy (&obj->pDesc->guidClass, &CLSID_DirectMusicCollection, sizeof (CLSID));
765 obj->ref = 0; /* will be inited by QueryInterface */
766 list_init (&obj->Instruments);
768 return IDirectMusicCollectionImpl_IUnknown_QueryInterface ((LPUNKNOWN)&obj->UnknownVtbl, lpcGUID, ppobj);