wined3d: Replace an open-coded implementation of LIST_FOR_EACH_ENTRY_SAFE.
[wine.git] / dlls / dmstyle / style.c
blob515a8f36f3cda6f91d583c49bc582341f499abea
1 /* IDirectMusicStyle8 Implementation
3 * Copyright (C) 2003-2004 Rok Mandeljc
4 * Copyright (C) 2003-2004 Raphael Junqueira
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "dmstyle_private.h"
22 #include "dmobject.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(dmstyle);
25 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
27 /*****************************************************************************
28 * IDirectMusicStyleImpl implementation
30 typedef struct IDirectMusicStyle8Impl {
31 IDirectMusicStyle8 IDirectMusicStyle8_iface;
32 struct dmobject dmobj;
33 LONG ref;
34 DMUS_IO_STYLE style;
35 struct list Motifs;
36 struct list Bands;
37 } IDirectMusicStyle8Impl;
39 static inline IDirectMusicStyle8Impl *impl_from_IDirectMusicStyle8(IDirectMusicStyle8 *iface)
41 return CONTAINING_RECORD(iface, IDirectMusicStyle8Impl, IDirectMusicStyle8_iface);
44 /* DirectMusicStyle8Impl IDirectMusicStyle8 part: */
45 static HRESULT WINAPI IDirectMusicStyle8Impl_QueryInterface(IDirectMusicStyle8 *iface, REFIID riid,
46 void **ret_iface)
48 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
50 TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ret_iface);
52 *ret_iface = NULL;
54 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDirectMusicStyle) ||
55 IsEqualIID(riid, &IID_IDirectMusicStyle8))
56 *ret_iface = iface;
57 else if (IsEqualIID(riid, &IID_IDirectMusicObject))
58 *ret_iface = &This->dmobj.IDirectMusicObject_iface;
59 else if (IsEqualIID(riid, &IID_IPersistStream))
60 *ret_iface = &This->dmobj.IPersistStream_iface;
61 else {
62 WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ret_iface);
63 return E_NOINTERFACE;
66 IUnknown_AddRef((IUnknown*)*ret_iface);
67 return S_OK;
70 static ULONG WINAPI IDirectMusicStyle8Impl_AddRef(IDirectMusicStyle8 *iface)
72 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
73 LONG ref = InterlockedIncrement(&This->ref);
75 TRACE("(%p) ref=%d\n", This, ref);
77 return ref;
80 static ULONG WINAPI IDirectMusicStyle8Impl_Release(IDirectMusicStyle8 *iface)
82 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
83 LONG ref = InterlockedDecrement(&This->ref);
85 TRACE("(%p) ref=%d\n", This, ref);
87 if (!ref) {
88 HeapFree(GetProcessHeap(), 0, This);
89 DMSTYLE_UnlockModule();
92 return ref;
95 /* IDirectMusicStyle8Impl IDirectMusicStyle(8) part: */
96 static HRESULT WINAPI IDirectMusicStyle8Impl_GetBand(IDirectMusicStyle8 *iface, WCHAR *pwszName,
97 IDirectMusicBand **ppBand)
99 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
100 FIXME("(%p, %p, %p): stub\n", This, pwszName, ppBand);
101 return S_OK;
104 static HRESULT WINAPI IDirectMusicStyle8Impl_EnumBand(IDirectMusicStyle8 *iface, DWORD dwIndex,
105 WCHAR *pwszName)
107 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
108 FIXME("(%p, %d, %p): stub\n", This, dwIndex, pwszName);
109 return S_OK;
112 static HRESULT WINAPI IDirectMusicStyle8Impl_GetDefaultBand(IDirectMusicStyle8 *iface,
113 IDirectMusicBand **ppBand)
115 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
116 FIXME("(%p, %p): stub\n", This, ppBand);
117 return S_OK;
120 static HRESULT WINAPI IDirectMusicStyle8Impl_EnumMotif(IDirectMusicStyle8 *iface, DWORD dwIndex,
121 WCHAR *pwszName)
123 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
124 FIXME("(%p, %d, %p): stub\n", This, dwIndex, pwszName);
125 return S_OK;
128 static HRESULT WINAPI IDirectMusicStyle8Impl_GetMotif(IDirectMusicStyle8 *iface, WCHAR *pwszName,
129 IDirectMusicSegment **ppSegment)
131 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
132 FIXME("(%p, %p, %p): stub\n", This, pwszName, ppSegment);
133 return S_OK;
136 static HRESULT WINAPI IDirectMusicStyle8Impl_GetDefaultChordMap(IDirectMusicStyle8 *iface,
137 IDirectMusicChordMap **ppChordMap)
139 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
140 FIXME("(%p, %p): stub\n", This, ppChordMap);
141 return S_OK;
144 static HRESULT WINAPI IDirectMusicStyle8Impl_EnumChordMap(IDirectMusicStyle8 *iface, DWORD dwIndex,
145 WCHAR *pwszName)
147 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
148 FIXME("(%p, %d, %p): stub\n", This, dwIndex, pwszName);
149 return S_OK;
152 static HRESULT WINAPI IDirectMusicStyle8Impl_GetChordMap(IDirectMusicStyle8 *iface, WCHAR *pwszName,
153 IDirectMusicChordMap **ppChordMap)
155 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
156 FIXME("(%p, %p, %p): stub\n", This, pwszName, ppChordMap);
157 return S_OK;
160 static HRESULT WINAPI IDirectMusicStyle8Impl_GetTimeSignature(IDirectMusicStyle8 *iface,
161 DMUS_TIMESIGNATURE *pTimeSig)
163 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
164 FIXME("(%p, %p): stub\n", This, pTimeSig);
165 return S_OK;
168 static HRESULT WINAPI IDirectMusicStyle8Impl_GetEmbellishmentLength(IDirectMusicStyle8 *iface,
169 DWORD dwType, DWORD dwLevel, DWORD *pdwMin, DWORD *pdwMax)
171 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
172 FIXME("(%p, %d, %d, %p, %p): stub\n", This, dwType, dwLevel, pdwMin, pdwMax);
173 return S_OK;
176 static HRESULT WINAPI IDirectMusicStyle8Impl_GetTempo(IDirectMusicStyle8 *iface, double *pTempo)
178 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
179 FIXME("(%p, %p): stub\n", This, pTempo);
180 return S_OK;
183 static HRESULT WINAPI IDirectMusicStyle8Impl_EnumPattern(IDirectMusicStyle8 *iface, DWORD dwIndex,
184 DWORD dwPatternType, WCHAR *pwszName)
186 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
187 FIXME("(%p, %d, %d, %p): stub\n", This, dwIndex, dwPatternType, pwszName);
188 return S_OK;
191 static const IDirectMusicStyle8Vtbl dmstyle8_vtbl = {
192 IDirectMusicStyle8Impl_QueryInterface,
193 IDirectMusicStyle8Impl_AddRef,
194 IDirectMusicStyle8Impl_Release,
195 IDirectMusicStyle8Impl_GetBand,
196 IDirectMusicStyle8Impl_EnumBand,
197 IDirectMusicStyle8Impl_GetDefaultBand,
198 IDirectMusicStyle8Impl_EnumMotif,
199 IDirectMusicStyle8Impl_GetMotif,
200 IDirectMusicStyle8Impl_GetDefaultChordMap,
201 IDirectMusicStyle8Impl_EnumChordMap,
202 IDirectMusicStyle8Impl_GetChordMap,
203 IDirectMusicStyle8Impl_GetTimeSignature,
204 IDirectMusicStyle8Impl_GetEmbellishmentLength,
205 IDirectMusicStyle8Impl_GetTempo,
206 IDirectMusicStyle8Impl_EnumPattern
209 /* IDirectMusicStyle8Impl IDirectMusicObject part: */
210 static inline IDirectMusicStyle8Impl *impl_from_IDirectMusicObject(IDirectMusicObject *iface)
212 return CONTAINING_RECORD(iface, IDirectMusicStyle8Impl, dmobj.IDirectMusicObject_iface);
215 static HRESULT WINAPI IDirectMusicObjectImpl_ParseDescriptor(IDirectMusicObject *iface,
216 IStream *pStream, DMUS_OBJECTDESC *pDesc)
218 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicObject(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 pDesc->guidClass = CLSID_DirectMusicStyle;
229 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
230 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", 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 == DMUS_FOURCC_STYLE_FORM) {
238 TRACE_(dmfile)(": style 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%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
243 switch (Chunk.fccID) {
244 case DMUS_FOURCC_GUID_CHUNK: {
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 /* evil M$ UNFO list, which can (!?) contain INFO elements */
269 case DMUS_FOURCC_UNFO_LIST: {
270 TRACE_(dmfile)(": UNFO 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%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
275 switch (Chunk.fccID) {
276 /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
277 (though strings seem to be valid unicode) */
278 case mmioFOURCC('I','N','A','M'):
279 case DMUS_FOURCC_UNAM_CHUNK: {
280 TRACE_(dmfile)(": name chunk\n");
281 pDesc->dwValidData |= DMUS_OBJ_NAME;
282 IStream_Read (pStream, pDesc->wszName, Chunk.dwSize, NULL);
283 break;
285 case mmioFOURCC('I','A','R','T'):
286 case DMUS_FOURCC_UART_CHUNK: {
287 TRACE_(dmfile)(": artist chunk (ignored)\n");
288 liMove.QuadPart = Chunk.dwSize;
289 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
290 break;
292 case mmioFOURCC('I','C','O','P'):
293 case DMUS_FOURCC_UCOP_CHUNK: {
294 TRACE_(dmfile)(": copyright chunk (ignored)\n");
295 liMove.QuadPart = Chunk.dwSize;
296 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
297 break;
299 case mmioFOURCC('I','S','B','J'):
300 case DMUS_FOURCC_USBJ_CHUNK: {
301 TRACE_(dmfile)(": subject chunk (ignored)\n");
302 liMove.QuadPart = Chunk.dwSize;
303 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
304 break;
306 case mmioFOURCC('I','C','M','T'):
307 case DMUS_FOURCC_UCMT_CHUNK: {
308 TRACE_(dmfile)(": comment chunk (ignored)\n");
309 liMove.QuadPart = Chunk.dwSize;
310 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
311 break;
313 default: {
314 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
315 liMove.QuadPart = Chunk.dwSize;
316 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
317 break;
320 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
321 } while (ListCount[0] < ListSize[0]);
322 break;
324 default: {
325 TRACE_(dmfile)(": unknown (skipping)\n");
326 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
327 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
328 break;
331 break;
333 default: {
334 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
335 liMove.QuadPart = Chunk.dwSize;
336 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
337 break;
340 TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
341 } while (StreamCount < StreamSize);
342 } else {
343 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
344 liMove.QuadPart = StreamSize;
345 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
346 return E_FAIL;
349 TRACE_(dmfile)(": reading finished\n");
350 break;
352 default: {
353 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
354 liMove.QuadPart = Chunk.dwSize;
355 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
356 return DMUS_E_INVALIDFILE;
360 TRACE(": returning descriptor:\n%s\n", debugstr_DMUS_OBJECTDESC (pDesc));
362 return S_OK;
365 static const IDirectMusicObjectVtbl dmobject_vtbl = {
366 dmobj_IDirectMusicObject_QueryInterface,
367 dmobj_IDirectMusicObject_AddRef,
368 dmobj_IDirectMusicObject_Release,
369 dmobj_IDirectMusicObject_GetDescriptor,
370 dmobj_IDirectMusicObject_SetDescriptor,
371 IDirectMusicObjectImpl_ParseDescriptor
374 /* IDirectMusicStyle8Impl IPersistStream part: */
375 static inline IDirectMusicStyle8Impl *impl_from_IPersistStream(IPersistStream *iface)
377 return CONTAINING_RECORD(iface, IDirectMusicStyle8Impl, dmobj.IPersistStream_iface);
380 static HRESULT load_band(IStream *pClonedStream, IDirectMusicBand **ppBand)
382 HRESULT hr = E_FAIL;
383 IPersistStream* pPersistStream = NULL;
385 hr = CoCreateInstance (&CLSID_DirectMusicBand, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicBand, (LPVOID*) ppBand);
386 if (FAILED(hr)) {
387 ERR(": could not create object\n");
388 return hr;
390 /* acquire PersistStream interface */
391 hr = IDirectMusicBand_QueryInterface (*ppBand, &IID_IPersistStream, (LPVOID*) &pPersistStream);
392 if (FAILED(hr)) {
393 ERR(": could not acquire IPersistStream\n");
394 return hr;
396 /* load */
397 hr = IPersistStream_Load (pPersistStream, pClonedStream);
398 if (FAILED(hr)) {
399 ERR(": failed to load object\n");
400 return hr;
403 /* release all loading-related stuff */
404 IPersistStream_Release (pPersistStream);
406 return S_OK;
409 static HRESULT parse_part_ref_list(DMUS_PRIVATE_CHUNK *pChunk, IStream *pStm,
410 DMUS_PRIVATE_STYLE_MOTIF *pNewMotif)
412 HRESULT hr = E_FAIL;
413 DMUS_PRIVATE_CHUNK Chunk;
414 DWORD ListSize[3], ListCount[3];
415 LARGE_INTEGER liMove; /* used when skipping chunks */
417 LPDMUS_PRIVATE_STYLE_PARTREF_ITEM pNewItem = NULL;
420 if (pChunk->fccID != DMUS_FOURCC_PARTREF_LIST) {
421 ERR_(dmfile)(": %s chunk should be a PARTREF list\n", debugstr_fourcc (pChunk->fccID));
422 return E_FAIL;
425 ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
426 ListCount[0] = 0;
428 do {
429 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
430 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
431 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
432 switch (Chunk.fccID) {
433 case DMUS_FOURCC_PARTREF_CHUNK: {
434 TRACE_(dmfile)(": PartRef chunk\n");
435 pNewItem = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_STYLE_PARTREF_ITEM));
436 if (!pNewItem) {
437 ERR(": no more memory\n");
438 return E_OUTOFMEMORY;
440 hr = IStream_Read (pStm, &pNewItem->part_ref, sizeof(DMUS_IO_PARTREF), NULL);
441 /*TRACE_(dmfile)(" - sizeof %lu\n", sizeof(DMUS_IO_PARTREF));*/
442 list_add_tail (&pNewMotif->Items, &pNewItem->entry);
443 DM_STRUCT_INIT(&pNewItem->desc);
444 break;
446 case FOURCC_LIST: {
447 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
448 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
449 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
450 ListCount[1] = 0;
451 switch (Chunk.fccID) {
452 case DMUS_FOURCC_UNFO_LIST: {
453 TRACE_(dmfile)(": UNFO list\n");
454 do {
455 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
456 ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
457 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
459 if (!pNewItem) {
460 ERR(": pNewItem not yet allocated, chunk order bad?\n");
461 return E_OUTOFMEMORY;
463 hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, &pNewItem->desc);
464 if (FAILED(hr)) return hr;
466 if (hr == S_FALSE) {
467 switch (Chunk.fccID) {
468 default: {
469 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
470 liMove.QuadPart = Chunk.dwSize;
471 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
472 break;
476 TRACE_(dmfile)(": ListCount[1] = %d < ListSize[1] = %d\n", ListCount[1], ListSize[1]);
477 } while (ListCount[1] < ListSize[1]);
478 break;
480 default: {
481 TRACE_(dmfile)(": unknown chunk (skipping)\n");
482 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
483 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
484 break;
487 break;
489 default: {
490 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
491 liMove.QuadPart = Chunk.dwSize;
492 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
493 break;
496 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
497 } while (ListCount[0] < ListSize[0]);
499 return S_OK;
502 static HRESULT parse_part_list(DMUS_PRIVATE_CHUNK *pChunk, IStream *pStm)
504 HRESULT hr = E_FAIL;
505 DMUS_PRIVATE_CHUNK Chunk;
506 DWORD ListSize[3], ListCount[3];
507 LARGE_INTEGER liMove; /* used when skipping chunks */
509 DMUS_OBJECTDESC desc;
510 DWORD dwSize = 0;
511 DWORD cnt = 0;
513 if (pChunk->fccID != DMUS_FOURCC_PART_LIST) {
514 ERR_(dmfile)(": %s chunk should be a PART list\n", debugstr_fourcc (pChunk->fccID));
515 return E_FAIL;
518 ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
519 ListCount[0] = 0;
521 do {
522 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
523 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
524 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
525 switch (Chunk.fccID) {
526 case DMUS_FOURCC_PART_CHUNK: {
527 TRACE_(dmfile)(": Part chunk (skipping for now)\n" );
528 liMove.QuadPart = Chunk.dwSize;
529 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
530 break;
532 case DMUS_FOURCC_NOTE_CHUNK: {
533 TRACE_(dmfile)(": Note chunk (skipping for now)\n");
534 IStream_Read (pStm, &dwSize, sizeof(DWORD), NULL);
535 cnt = (Chunk.dwSize - sizeof(DWORD));
536 TRACE_(dmfile)(" - dwSize: %u\n", dwSize);
537 TRACE_(dmfile)(" - cnt: %u (%u / %u)\n", cnt / dwSize, (DWORD)(Chunk.dwSize - sizeof(DWORD)), dwSize);
538 if (cnt % dwSize != 0) {
539 ERR("Invalid Array Size\n");
540 return E_FAIL;
542 cnt /= dwSize;
543 /** skip for now */
544 liMove.QuadPart = cnt * dwSize;
545 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
546 break;
548 case DMUS_FOURCC_CURVE_CHUNK: {
549 TRACE_(dmfile)(": Curve chunk (skipping for now)\n");
550 IStream_Read (pStm, &dwSize, sizeof(DWORD), NULL);
551 cnt = (Chunk.dwSize - sizeof(DWORD));
552 TRACE_(dmfile)(" - dwSize: %u\n", dwSize);
553 TRACE_(dmfile)(" - cnt: %u (%u / %u)\n", cnt / dwSize, (DWORD)(Chunk.dwSize - sizeof(DWORD)), dwSize);
554 if (cnt % dwSize != 0) {
555 ERR("Invalid Array Size\n");
556 return E_FAIL;
558 cnt /= dwSize;
559 /** skip for now */
560 liMove.QuadPart = cnt * dwSize;
561 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
562 break;
564 case DMUS_FOURCC_MARKER_CHUNK: {
565 TRACE_(dmfile)(": Marker chunk (skipping for now)\n");
566 liMove.QuadPart = Chunk.dwSize;
567 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
568 break;
570 case DMUS_FOURCC_RESOLUTION_CHUNK: {
571 TRACE_(dmfile)(": Resolution chunk (skipping for now)\n");
572 liMove.QuadPart = Chunk.dwSize;
573 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
574 break;
576 case DMUS_FOURCC_ANTICIPATION_CHUNK: {
577 TRACE_(dmfile)(": Anticipation chunk (skipping for now)\n");
578 liMove.QuadPart = Chunk.dwSize;
579 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
580 break;
582 case FOURCC_LIST: {
583 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
584 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
585 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
586 ListCount[1] = 0;
587 switch (Chunk.fccID) {
588 case DMUS_FOURCC_UNFO_LIST: {
589 TRACE_(dmfile)(": UNFO list\n");
590 do {
591 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
592 ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
593 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
595 hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, &desc);
596 if (FAILED(hr)) return hr;
598 if (hr == S_FALSE) {
599 switch (Chunk.fccID) {
600 default: {
601 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
602 liMove.QuadPart = Chunk.dwSize;
603 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
604 break;
608 TRACE_(dmfile)(": ListCount[1] = %d < ListSize[1] = %d\n", ListCount[1], ListSize[1]);
609 } while (ListCount[1] < ListSize[1]);
610 break;
612 default: {
613 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
614 liMove.QuadPart = Chunk.dwSize;
615 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
616 break;
619 break;
621 default: {
622 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
623 liMove.QuadPart = Chunk.dwSize;
624 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
625 break;
628 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
629 } while (ListCount[0] < ListSize[0]);
631 return S_OK;
634 static HRESULT parse_pattern_list(IDirectMusicStyle8Impl *This, DMUS_PRIVATE_CHUNK *pChunk,
635 IStream *pStm)
637 HRESULT hr = E_FAIL;
638 DMUS_PRIVATE_CHUNK Chunk;
639 DWORD ListSize[3], ListCount[3];
640 LARGE_INTEGER liMove; /* used when skipping chunks */
642 DMUS_OBJECTDESC desc;
643 IDirectMusicBand* pBand = NULL;
644 LPDMUS_PRIVATE_STYLE_MOTIF pNewMotif = NULL;
646 DM_STRUCT_INIT(&desc);
648 if (pChunk->fccID != DMUS_FOURCC_PATTERN_LIST) {
649 ERR_(dmfile)(": %s chunk should be a PATTERN list\n", debugstr_fourcc (pChunk->fccID));
650 return E_FAIL;
653 ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
654 ListCount[0] = 0;
656 do {
657 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
658 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
659 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
660 switch (Chunk.fccID) {
661 case DMUS_FOURCC_PATTERN_CHUNK: {
662 TRACE_(dmfile)(": Pattern chunk\n");
663 /** alloc new motif entry */
664 pNewMotif = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_STYLE_MOTIF));
665 if (NULL == pNewMotif) {
666 ERR(": no more memory\n");
667 return E_OUTOFMEMORY;
669 list_add_tail (&This->Motifs, &pNewMotif->entry);
671 IStream_Read (pStm, &pNewMotif->pattern, Chunk.dwSize, NULL);
672 /** TODO trace pattern */
674 /** reset all data, as a new pattern begin */
675 DM_STRUCT_INIT(&pNewMotif->desc);
676 list_init (&pNewMotif->Items);
677 break;
679 case DMUS_FOURCC_RHYTHM_CHUNK: {
680 TRACE_(dmfile)(": Rhythm chunk\n");
681 IStream_Read (pStm, &pNewMotif->dwRhythm, sizeof(DWORD), NULL);
682 TRACE_(dmfile)(" - dwRhythm: %u\n", pNewMotif->dwRhythm);
683 /** TODO understand why some Chunks have size > 4 */
684 liMove.QuadPart = Chunk.dwSize - sizeof(DWORD);
685 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
686 break;
688 case DMUS_FOURCC_MOTIFSETTINGS_CHUNK: {
689 TRACE_(dmfile)(": MotifSettings chunk (skipping for now)\n");
690 IStream_Read (pStm, &pNewMotif->settings, Chunk.dwSize, NULL);
691 /** TODO trace settings */
692 break;
694 case FOURCC_RIFF: {
696 * should be embedded Bands into pattern
698 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
699 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
700 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
701 ListCount[1] = 0;
702 switch (Chunk.fccID) {
703 case DMUS_FOURCC_BAND_FORM: {
704 LPSTREAM pClonedStream = NULL;
706 TRACE_(dmfile)(": BAND RIFF\n");
708 IStream_Clone (pStm, &pClonedStream);
710 liMove.QuadPart = 0;
711 liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
712 IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
714 hr = load_band(pClonedStream, &pBand);
715 if (FAILED(hr)) {
716 ERR(": could not load track\n");
717 return hr;
719 IStream_Release (pClonedStream);
721 pNewMotif->pBand = pBand;
722 IDirectMusicBand_AddRef(pBand);
724 IDirectMusicTrack_Release(pBand); pBand = NULL; /* now we can release it as it's inserted */
726 /** now safe move the cursor */
727 liMove.QuadPart = ListSize[1];
728 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
730 break;
732 default: {
733 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
734 liMove.QuadPart = ListSize[1];
735 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
736 break;
739 break;
741 case FOURCC_LIST: {
742 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
743 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
744 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
745 ListCount[1] = 0;
746 switch (Chunk.fccID) {
747 case DMUS_FOURCC_UNFO_LIST: {
748 TRACE_(dmfile)(": UNFO list\n");
749 do {
750 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
751 ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
752 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
754 hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, &pNewMotif->desc);
755 if (FAILED(hr)) return hr;
757 if (hr == S_FALSE) {
758 switch (Chunk.fccID) {
759 default: {
760 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
761 liMove.QuadPart = Chunk.dwSize;
762 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
763 break;
767 TRACE_(dmfile)(": ListCount[1] = %d < ListSize[1] = %d\n", ListCount[1], ListSize[1]);
768 } while (ListCount[1] < ListSize[1]);
769 break;
771 case DMUS_FOURCC_PARTREF_LIST: {
772 TRACE_(dmfile)(": PartRef list\n");
773 hr = parse_part_ref_list(&Chunk, pStm, pNewMotif);
774 if (FAILED(hr)) return hr;
775 break;
777 default: {
778 TRACE_(dmfile)(": unknown (skipping)\n");
779 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
780 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
781 break;
784 break;
786 default: {
787 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
788 liMove.QuadPart = Chunk.dwSize;
789 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
790 break;
793 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
794 } while (ListCount[0] < ListSize[0]);
796 return S_OK;
799 static HRESULT parse_style_form(IDirectMusicStyle8Impl *This, DMUS_PRIVATE_CHUNK *pChunk,
800 IStream *pStm)
802 HRESULT hr = E_FAIL;
803 DMUS_PRIVATE_CHUNK Chunk;
804 DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
805 LARGE_INTEGER liMove; /* used when skipping chunks */
807 IDirectMusicBand* pBand = NULL;
809 if (pChunk->fccID != DMUS_FOURCC_STYLE_FORM) {
810 ERR_(dmfile)(": %s chunk should be a STYLE form\n", debugstr_fourcc (pChunk->fccID));
811 return E_FAIL;
814 StreamSize = pChunk->dwSize - sizeof(FOURCC);
815 StreamCount = 0;
817 do {
818 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
819 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
820 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
822 hr = IDirectMusicUtils_IPersistStream_ParseDescGeneric(&Chunk, pStm, &This->dmobj.desc);
823 if (FAILED(hr)) return hr;
825 if (hr == S_FALSE) {
826 switch (Chunk.fccID) {
827 case DMUS_FOURCC_STYLE_CHUNK: {
828 TRACE_(dmfile)(": Style chunk\n");
829 IStream_Read (pStm, &This->style, sizeof(DMUS_IO_STYLE), NULL);
830 /** TODO dump DMUS_IO_TIMESIG style.timeSig */
831 TRACE_(dmfile)(" - dblTempo: %g\n", This->style.dblTempo);
832 break;
834 case FOURCC_RIFF: {
836 * should be embedded Bands into style
838 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
839 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
840 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
841 ListCount[0] = 0;
842 switch (Chunk.fccID) {
843 case DMUS_FOURCC_BAND_FORM: {
844 LPSTREAM pClonedStream = NULL;
845 LPDMUS_PRIVATE_STYLE_BAND pNewBand;
847 TRACE_(dmfile)(": BAND RIFF\n");
849 IStream_Clone (pStm, &pClonedStream);
851 liMove.QuadPart = 0;
852 liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
853 IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
855 hr = load_band(pClonedStream, &pBand);
856 if (FAILED(hr)) {
857 ERR(": could not load track\n");
858 return hr;
860 IStream_Release (pClonedStream);
862 pNewBand = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_STYLE_BAND));
863 if (NULL == pNewBand) {
864 ERR(": no more memory\n");
865 return E_OUTOFMEMORY;
867 pNewBand->pBand = pBand;
868 IDirectMusicBand_AddRef(pBand);
869 list_add_tail (&This->Bands, &pNewBand->entry);
871 IDirectMusicTrack_Release(pBand); pBand = NULL; /* now we can release it as it's inserted */
873 /** now safely move the cursor */
874 liMove.QuadPart = ListSize[0];
875 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
877 break;
879 default: {
880 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
881 liMove.QuadPart = ListSize[0];
882 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
883 break;
886 break;
888 case FOURCC_LIST: {
889 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
890 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
891 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
892 ListCount[0] = 0;
893 switch (Chunk.fccID) {
894 case DMUS_FOURCC_UNFO_LIST: {
895 TRACE_(dmfile)(": UNFO list\n");
896 do {
897 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
898 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
899 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
901 hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, &This->dmobj.desc);
902 if (FAILED(hr)) return hr;
904 if (hr == S_FALSE) {
905 switch (Chunk.fccID) {
906 default: {
907 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
908 liMove.QuadPart = Chunk.dwSize;
909 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
910 break;
914 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
915 } while (ListCount[0] < ListSize[0]);
916 break;
918 case DMUS_FOURCC_PART_LIST: {
919 TRACE_(dmfile)(": PART list\n");
920 hr = parse_part_list(&Chunk, pStm);
921 if (FAILED(hr)) return hr;
922 break;
924 case DMUS_FOURCC_PATTERN_LIST: {
925 TRACE_(dmfile)(": PATTERN list\n");
926 hr = parse_pattern_list(This, &Chunk, pStm);
927 if (FAILED(hr)) return hr;
928 break;
930 default: {
931 TRACE_(dmfile)(": unknown (skipping)\n");
932 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
933 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
934 break;
937 break;
939 default: {
940 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
941 liMove.QuadPart = Chunk.dwSize;
942 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
943 break;
947 TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
948 } while (StreamCount < StreamSize);
950 return S_OK;
953 static HRESULT WINAPI IPersistStreamImpl_Load(IPersistStream *iface, IStream *pStm)
955 IDirectMusicStyle8Impl *This = impl_from_IPersistStream(iface);
956 DMUS_PRIVATE_CHUNK Chunk;
957 LARGE_INTEGER liMove; /* used when skipping chunks */
958 HRESULT hr;
960 FIXME("(%p, %p): Loading\n", This, pStm);
962 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
963 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
964 switch (Chunk.fccID) {
965 case FOURCC_RIFF: {
966 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
967 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
968 switch (Chunk.fccID) {
969 case DMUS_FOURCC_STYLE_FORM: {
970 TRACE_(dmfile)(": Style form\n");
971 hr = parse_style_form(This, &Chunk, pStm);
972 if (FAILED(hr)) return hr;
973 break;
975 default: {
976 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
977 liMove.QuadPart = Chunk.dwSize;
978 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
979 return E_FAIL;
982 TRACE_(dmfile)(": reading finished\n");
983 break;
985 default: {
986 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
987 liMove.QuadPart = Chunk.dwSize;
988 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
989 return E_FAIL;
993 return S_OK;
996 static const IPersistStreamVtbl persiststream_vtbl = {
997 dmobj_IPersistStream_QueryInterface,
998 dmobj_IPersistStream_AddRef,
999 dmobj_IPersistStream_Release,
1000 dmobj_IPersistStream_GetClassID,
1001 unimpl_IPersistStream_IsDirty,
1002 IPersistStreamImpl_Load,
1003 unimpl_IPersistStream_Save,
1004 unimpl_IPersistStream_GetSizeMax
1007 /* for ClassFactory */
1008 HRESULT WINAPI create_dmstyle(REFIID lpcGUID, void **ppobj)
1010 IDirectMusicStyle8Impl* obj;
1011 HRESULT hr;
1013 obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicStyle8Impl));
1014 if (NULL == obj) {
1015 *ppobj = NULL;
1016 return E_OUTOFMEMORY;
1018 obj->IDirectMusicStyle8_iface.lpVtbl = &dmstyle8_vtbl;
1019 obj->ref = 1;
1020 dmobject_init(&obj->dmobj, &CLSID_DirectMusicStyle, (IUnknown *)&obj->IDirectMusicStyle8_iface);
1021 obj->dmobj.IDirectMusicObject_iface.lpVtbl = &dmobject_vtbl;
1022 obj->dmobj.IPersistStream_iface.lpVtbl = &persiststream_vtbl;
1023 list_init (&obj->Bands);
1024 list_init (&obj->Motifs);
1026 DMSTYLE_LockModule();
1027 hr = IDirectMusicStyle8_QueryInterface(&obj->IDirectMusicStyle8_iface, lpcGUID, ppobj);
1028 IDirectMusicStyle8_Release(&obj->IDirectMusicStyle8_iface);
1030 return hr;