Avoid warnings.
[wine] / dlls / dmstyle / style.c
1 /* IDirectMusicStyle8 Implementation
2  *
3  * Copyright (C) 2003-2004 Rok Mandeljc
4  * Copyright (C) 2003-2004 Raphael Junqueira
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
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
14  * GNU Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include "dmstyle_private.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(dmstyle);
24 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
25
26 /*****************************************************************************
27  * IDirectMusicStyleImpl implementation
28  */
29 /* IDirectMusicStyleImpl IUnknown part: */
30 HRESULT WINAPI IDirectMusicStyle8Impl_IUnknown_QueryInterface (LPUNKNOWN iface, REFIID riid, LPVOID *ppobj) {
31         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, UnknownVtbl, iface);
32         TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ppobj);
33         
34         if (IsEqualIID (riid, &IID_IUnknown)) {
35                 *ppobj = (LPVOID)&This->UnknownVtbl;
36                 IDirectMusicStyle8Impl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
37                 return S_OK;    
38         } else if (IsEqualIID (riid, &IID_IDirectMusicStyle)) {
39                 *ppobj = (LPVOID)&This->StyleVtbl;
40                 IDirectMusicStyle8Impl_IDirectMusicStyle8_AddRef ((LPDIRECTMUSICSTYLE8)&This->StyleVtbl);
41                 return S_OK;
42         } else if (IsEqualIID (riid, &IID_IDirectMusicStyle8)) {
43                 *ppobj = (LPVOID)&This->StyleVtbl;
44                 IDirectMusicStyle8Impl_IDirectMusicStyle8_AddRef ((LPDIRECTMUSICSTYLE8)&This->StyleVtbl);
45                 return S_OK;
46         } else if (IsEqualIID (riid, &IID_IDirectMusicObject)) {
47                 *ppobj = (LPVOID)&This->ObjectVtbl;
48                 IDirectMusicStyle8Impl_IDirectMusicObject_AddRef ((LPDIRECTMUSICOBJECT)&This->ObjectVtbl);              
49                 return S_OK;
50         } else if (IsEqualIID (riid, &IID_IPersistStream)) {
51                 *ppobj = (LPVOID)&This->PersistStreamVtbl;
52                 IDirectMusicStyle8Impl_IPersistStream_AddRef ((LPPERSISTSTREAM)&This->PersistStreamVtbl);               
53                 return S_OK;
54         }
55         
56         WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ppobj);
57         return E_NOINTERFACE;
58 }
59
60 ULONG WINAPI IDirectMusicStyle8Impl_IUnknown_AddRef (LPUNKNOWN iface) {
61         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, UnknownVtbl, iface);
62         TRACE("(%p): AddRef from %ld\n", This, This->ref);
63         return ++(This->ref);
64 }
65
66 ULONG WINAPI IDirectMusicStyle8Impl_IUnknown_Release (LPUNKNOWN iface) {
67         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, UnknownVtbl, iface);
68         ULONG ref = --This->ref;
69         TRACE("(%p): ReleaseRef to %ld\n", This, This->ref);
70         if (ref == 0) {
71                 HeapFree(GetProcessHeap(), 0, This);
72         }
73         return ref;
74 }
75
76 IUnknownVtbl DirectMusicStyle8_Unknown_Vtbl = {
77         IDirectMusicStyle8Impl_IUnknown_QueryInterface,
78         IDirectMusicStyle8Impl_IUnknown_AddRef,
79         IDirectMusicStyle8Impl_IUnknown_Release
80 };
81
82 /* IDirectMusicStyle8Impl IDirectMusicStyle8 part: */
83 HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_QueryInterface (LPDIRECTMUSICSTYLE8 iface, REFIID riid, LPVOID *ppobj) {
84         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
85         return IDirectMusicStyle8Impl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
86 }
87
88 ULONG WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_AddRef (LPDIRECTMUSICSTYLE8 iface) {
89         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
90         return IDirectMusicStyle8Impl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
91 }
92
93 ULONG WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_Release (LPDIRECTMUSICSTYLE8 iface) {
94         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
95         return IDirectMusicStyle8Impl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
96 }
97
98 /* IDirectMusicStyle8Impl IDirectMusicStyle(8) part: */
99 HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_GetBand (LPDIRECTMUSICSTYLE8 iface, WCHAR* pwszName, IDirectMusicBand** ppBand) {
100         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
101         FIXME("(%p, %p, %p): stub\n", This, pwszName, ppBand);
102         return S_OK;
103 }
104
105 HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_EnumBand (LPDIRECTMUSICSTYLE8 iface, DWORD dwIndex, WCHAR* pwszName) {
106         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
107         FIXME("(%p, %ld, %p): stub\n", This, dwIndex, pwszName);
108         return S_OK;
109 }
110
111 HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_GetDefaultBand (LPDIRECTMUSICSTYLE8 iface, IDirectMusicBand** ppBand) {
112         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
113         FIXME("(%p, %p): stub\n", This, ppBand);
114         return S_OK;
115 }
116
117 HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_EnumMotif (LPDIRECTMUSICSTYLE8 iface, DWORD dwIndex, WCHAR* pwszName) {
118         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
119         FIXME("(%p, %ld, %p): stub\n", This, dwIndex, pwszName);
120         return S_OK;
121 }
122
123 HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_GetMotif (LPDIRECTMUSICSTYLE8 iface, WCHAR* pwszName, IDirectMusicSegment** ppSegment) {
124         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
125         FIXME("(%p, %p, %p): stub\n", This, pwszName, ppSegment);
126         return S_OK;
127 }
128
129 HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_GetDefaultChordMap (LPDIRECTMUSICSTYLE8 iface, IDirectMusicChordMap** ppChordMap) {
130         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
131         FIXME("(%p, %p): stub\n", This, ppChordMap);
132         return S_OK;
133 }
134
135 HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_EnumChordMap (LPDIRECTMUSICSTYLE8 iface, DWORD dwIndex, WCHAR* pwszName) {
136         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
137         FIXME("(%p, %ld, %p): stub\n", This, dwIndex, pwszName);
138         return S_OK;
139 }
140
141 HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_GetChordMap (LPDIRECTMUSICSTYLE8 iface, WCHAR* pwszName, IDirectMusicChordMap** ppChordMap) {
142         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
143         FIXME("(%p, %p, %p): stub\n", This, pwszName, ppChordMap);
144         return S_OK;
145 }
146
147 HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_GetTimeSignature (LPDIRECTMUSICSTYLE8 iface, DMUS_TIMESIGNATURE* pTimeSig) {
148         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
149         FIXME("(%p, %p): stub\n", This, pTimeSig);
150         return S_OK;
151 }
152
153 HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_GetEmbellishmentLength (LPDIRECTMUSICSTYLE8 iface, DWORD dwType, DWORD dwLevel, DWORD* pdwMin, DWORD* pdwMax) {
154         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
155         FIXME("(%p, %ld, %ld, %p, %p): stub\n", This, dwType, dwLevel, pdwMin, pdwMax);
156         return S_OK;
157 }
158
159 HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_GetTempo (LPDIRECTMUSICSTYLE8 iface, double* pTempo) {
160         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
161         FIXME("(%p, %p): stub\n", This, pTempo);
162         return S_OK;
163 }
164
165 HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_EnumPattern (LPDIRECTMUSICSTYLE8 iface, DWORD dwIndex, DWORD dwPatternType, WCHAR* pwszName) {
166         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
167         FIXME("(%p, %ld, %ld, %p): stub\n", This, dwIndex, dwPatternType, pwszName);
168         return S_OK;
169 }
170
171 IDirectMusicStyle8Vtbl DirectMusicStyle8_Style_Vtbl = {
172         IDirectMusicStyle8Impl_IDirectMusicStyle8_QueryInterface,
173         IDirectMusicStyle8Impl_IDirectMusicStyle8_AddRef,
174         IDirectMusicStyle8Impl_IDirectMusicStyle8_Release,
175         IDirectMusicStyle8Impl_IDirectMusicStyle8_GetBand,
176         IDirectMusicStyle8Impl_IDirectMusicStyle8_EnumBand,
177         IDirectMusicStyle8Impl_IDirectMusicStyle8_GetDefaultBand,
178         IDirectMusicStyle8Impl_IDirectMusicStyle8_EnumMotif,
179         IDirectMusicStyle8Impl_IDirectMusicStyle8_GetMotif,
180         IDirectMusicStyle8Impl_IDirectMusicStyle8_GetDefaultChordMap,
181         IDirectMusicStyle8Impl_IDirectMusicStyle8_EnumChordMap,
182         IDirectMusicStyle8Impl_IDirectMusicStyle8_GetChordMap,
183         IDirectMusicStyle8Impl_IDirectMusicStyle8_GetTimeSignature,
184         IDirectMusicStyle8Impl_IDirectMusicStyle8_GetEmbellishmentLength,
185         IDirectMusicStyle8Impl_IDirectMusicStyle8_GetTempo,
186         IDirectMusicStyle8Impl_IDirectMusicStyle8_EnumPattern
187 };
188
189 /* IDirectMusicStyle8Impl IDirectMusicObject part: */
190 HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicObject_QueryInterface (LPDIRECTMUSICOBJECT iface, REFIID riid, LPVOID *ppobj) {
191         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, ObjectVtbl, iface);
192         return IDirectMusicStyle8Impl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
193 }
194
195 ULONG WINAPI IDirectMusicStyle8Impl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface) {
196         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, ObjectVtbl, iface);
197         return IDirectMusicStyle8Impl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
198 }
199
200 ULONG WINAPI IDirectMusicStyle8Impl_IDirectMusicObject_Release (LPDIRECTMUSICOBJECT iface) {
201         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, ObjectVtbl, iface);
202         return IDirectMusicStyle8Impl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
203 }
204
205 HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicObject_GetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
206         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, ObjectVtbl, iface);
207         TRACE("(%p, %p)\n", This, pDesc);
208         /* I think we shouldn't return pointer here since then values can be changed; it'd be a mess */
209         memcpy (pDesc, This->pDesc, This->pDesc->dwSize);
210         return S_OK;
211 }
212
213 HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicObject_SetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
214         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, ObjectVtbl, iface);
215         TRACE("(%p, %p): setting descriptor:\n%s\n", This, pDesc, debugstr_DMUS_OBJECTDESC (pDesc));
216         
217         /* According to MSDN, we should copy only given values, not whole struct */     
218         if (pDesc->dwValidData & DMUS_OBJ_OBJECT)
219                 memcpy (&This->pDesc->guidObject, &pDesc->guidObject, sizeof (pDesc->guidObject));
220         if (pDesc->dwValidData & DMUS_OBJ_CLASS)
221                 memcpy (&This->pDesc->guidClass, &pDesc->guidClass, sizeof (pDesc->guidClass));         
222         if (pDesc->dwValidData & DMUS_OBJ_NAME)
223                 strncpyW (This->pDesc->wszName, pDesc->wszName, DMUS_MAX_NAME);
224         if (pDesc->dwValidData & DMUS_OBJ_CATEGORY)
225                 strncpyW (This->pDesc->wszCategory, pDesc->wszCategory, DMUS_MAX_CATEGORY);             
226         if (pDesc->dwValidData & DMUS_OBJ_FILENAME)
227                 strncpyW (This->pDesc->wszFileName, pDesc->wszFileName, DMUS_MAX_FILENAME);             
228         if (pDesc->dwValidData & DMUS_OBJ_VERSION)
229                 memcpy (&This->pDesc->vVersion, &pDesc->vVersion, sizeof (pDesc->vVersion));                            
230         if (pDesc->dwValidData & DMUS_OBJ_DATE)
231                 memcpy (&This->pDesc->ftDate, &pDesc->ftDate, sizeof (pDesc->ftDate));                          
232         if (pDesc->dwValidData & DMUS_OBJ_MEMORY) {
233                 memcpy (&This->pDesc->llMemLength, &pDesc->llMemLength, sizeof (pDesc->llMemLength));                           
234                 memcpy (This->pDesc->pbMemData, pDesc->pbMemData, sizeof (pDesc->pbMemData));
235         }
236         if (pDesc->dwValidData & DMUS_OBJ_STREAM) {
237                 /* according to MSDN, we copy the stream */
238                 IStream_Clone (pDesc->pStream, &This->pDesc->pStream);  
239         }
240         
241         /* add new flags */
242         This->pDesc->dwValidData |= pDesc->dwValidData;
243
244         return S_OK;
245 }
246
247 HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicObject_ParseDescriptor (LPDIRECTMUSICOBJECT iface, LPSTREAM pStream, LPDMUS_OBJECTDESC pDesc) {
248         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, ObjectVtbl, iface);
249         DMUS_PRIVATE_CHUNK Chunk;
250         DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
251         LARGE_INTEGER liMove; /* used when skipping chunks */
252
253         TRACE("(%p, %p, %p)\n", This, pStream, pDesc);
254         
255         /* FIXME: should this be determined from stream? */
256         pDesc->dwValidData |= DMUS_OBJ_CLASS;
257         memcpy (&pDesc->guidClass, &CLSID_DirectMusicStyle, sizeof(CLSID));
258         
259         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
260         TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
261         switch (Chunk.fccID) {  
262                 case FOURCC_RIFF: {
263                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
264                         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
265                         StreamSize = Chunk.dwSize - sizeof(FOURCC);
266                         StreamCount = 0;
267                         if (Chunk.fccID == DMUS_FOURCC_STYLE_FORM) {
268                                 TRACE_(dmfile)(": style form\n");
269                                 do {
270                                         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
271                                         StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
272                                         TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
273                                         switch (Chunk.fccID) {
274                                                 case DMUS_FOURCC_GUID_CHUNK: {
275                                                         TRACE_(dmfile)(": GUID chunk\n");
276                                                         pDesc->dwValidData |= DMUS_OBJ_OBJECT;
277                                                         IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
278                                                         break;
279                                                 }
280                                                 case DMUS_FOURCC_VERSION_CHUNK: {
281                                                         TRACE_(dmfile)(": version chunk\n");
282                                                         pDesc->dwValidData |= DMUS_OBJ_VERSION;
283                                                         IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
284                                                         break;
285                                                 }
286                                                 case DMUS_FOURCC_CATEGORY_CHUNK: {
287                                                         TRACE_(dmfile)(": category chunk\n");
288                                                         pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
289                                                         IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
290                                                         break;
291                                                 }
292                                                 case FOURCC_LIST: {
293                                                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
294                                                         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
295                                                         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
296                                                         ListCount[0] = 0;
297                                                         switch (Chunk.fccID) {
298                                                                 /* evil M$ UNFO list, which can (!?) contain INFO elements */
299                                                                 case DMUS_FOURCC_UNFO_LIST: {
300                                                                         TRACE_(dmfile)(": UNFO list\n");
301                                                                         do {
302                                                                                 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
303                                                                                 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
304                                                                                 TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
305                                                                                 switch (Chunk.fccID) {
306                                                                                         /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
307                                              (though strings seem to be valid unicode) */
308                                                                                         case mmioFOURCC('I','N','A','M'):
309                                                                                         case DMUS_FOURCC_UNAM_CHUNK: {
310                                                                                                 TRACE_(dmfile)(": name chunk\n");
311                                                                                                 pDesc->dwValidData |= DMUS_OBJ_NAME;
312                                                                                                 IStream_Read (pStream, pDesc->wszName, Chunk.dwSize, NULL);
313                                                                                                 break;
314                                                                                         }
315                                                                                         case mmioFOURCC('I','A','R','T'):
316                                                                                         case DMUS_FOURCC_UART_CHUNK: {
317                                                                                                 TRACE_(dmfile)(": artist chunk (ignored)\n");
318                                                                                                 liMove.QuadPart = Chunk.dwSize;
319                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
320                                                                                                 break;
321                                                                                         }
322                                                                                         case mmioFOURCC('I','C','O','P'):
323                                                                                         case DMUS_FOURCC_UCOP_CHUNK: {
324                                                                                                 TRACE_(dmfile)(": copyright chunk (ignored)\n");
325                                                                                                 liMove.QuadPart = Chunk.dwSize;
326                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
327                                                                                                 break;
328                                                                                         }
329                                                                                         case mmioFOURCC('I','S','B','J'):
330                                                                                         case DMUS_FOURCC_USBJ_CHUNK: {
331                                                                                                 TRACE_(dmfile)(": subject chunk (ignored)\n");
332                                                                                                 liMove.QuadPart = Chunk.dwSize;
333                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
334                                                                                                 break;
335                                                                                         }
336                                                                                         case mmioFOURCC('I','C','M','T'):
337                                                                                         case DMUS_FOURCC_UCMT_CHUNK: {
338                                                                                                 TRACE_(dmfile)(": comment chunk (ignored)\n");
339                                                                                                 liMove.QuadPart = Chunk.dwSize;
340                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
341                                                                                                 break;
342                                                                                         }
343                                                                                         default: {
344                                                                                                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
345                                                                                                 liMove.QuadPart = Chunk.dwSize;
346                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
347                                                                                                 break;                                          
348                                                                                         }
349                                                                                 }
350                                                                                 TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
351                                                                         } while (ListCount[0] < ListSize[0]);
352                                                                         break;
353                                                                 }
354                                                                 default: {
355                                                                         TRACE_(dmfile)(": unknown (skipping)\n");
356                                                                         liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
357                                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
358                                                                         break;                                          
359                                                                 }
360                                                         }
361                                                         break;
362                                                 }       
363                                                 default: {
364                                                         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
365                                                         liMove.QuadPart = Chunk.dwSize;
366                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
367                                                         break;                                          
368                                                 }
369                                         }
370                                         TRACE_(dmfile)(": StreamCount[0] = %ld < StreamSize[0] = %ld\n", StreamCount, StreamSize);
371                                 } while (StreamCount < StreamSize);
372                         } else {
373                                 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
374                                 liMove.QuadPart = StreamSize;
375                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
376                                 return E_FAIL;
377                         }
378                 
379                         TRACE_(dmfile)(": reading finished\n");
380                         break;
381                 }
382                 default: {
383                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
384                         liMove.QuadPart = Chunk.dwSize;
385                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
386                         return DMUS_E_INVALIDFILE;
387                 }
388         }       
389         
390         TRACE(": returning descriptor:\n%s\n", debugstr_DMUS_OBJECTDESC (pDesc));
391         
392         return S_OK;
393 }
394
395 IDirectMusicObjectVtbl DirectMusicStyle8_Object_Vtbl = {
396   IDirectMusicStyle8Impl_IDirectMusicObject_QueryInterface,
397   IDirectMusicStyle8Impl_IDirectMusicObject_AddRef,
398   IDirectMusicStyle8Impl_IDirectMusicObject_Release,
399   IDirectMusicStyle8Impl_IDirectMusicObject_GetDescriptor,
400   IDirectMusicStyle8Impl_IDirectMusicObject_SetDescriptor,
401   IDirectMusicStyle8Impl_IDirectMusicObject_ParseDescriptor
402 };
403
404 /* IDirectMusicStyle8Impl IPersistStream part: */
405 HRESULT WINAPI IDirectMusicStyle8Impl_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
406   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
407   return IDirectMusicStyle8Impl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
408 }
409
410 ULONG WINAPI IDirectMusicStyle8Impl_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
411   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
412   return IDirectMusicStyle8Impl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
413 }
414
415 ULONG WINAPI IDirectMusicStyle8Impl_IPersistStream_Release (LPPERSISTSTREAM iface) {
416   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
417   return IDirectMusicStyle8Impl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
418 }
419
420 HRESULT WINAPI IDirectMusicStyle8Impl_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
421   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
422   TRACE("(%p, %p)\n", This, pClassID);
423   memcpy(pClassID, &CLSID_DirectMusicStyle, sizeof(CLSID));
424   return S_OK;
425 }
426
427 HRESULT WINAPI IDirectMusicStyle8Impl_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
428   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
429   FIXME("(%p): stub, always S_FALSE\n", This);
430   return S_FALSE;
431 }
432
433 static HRESULT IDirectMusicStyle8Impl_IPersistStream_LoadBand (LPPERSISTSTREAM iface, IStream* pClonedStream, IDirectMusicBand** ppBand) {
434
435   HRESULT hr = E_FAIL;
436   IPersistStream* pPersistStream = NULL;
437   
438   hr = CoCreateInstance (&CLSID_DirectMusicBand, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicBand, (LPVOID*) ppBand);
439   if (FAILED(hr)) {
440     ERR(": could not create object\n");
441     return hr;
442   }
443   /* acquire PersistStream interface */
444   hr = IDirectMusicBand_QueryInterface (*ppBand, &IID_IPersistStream, (LPVOID*) &pPersistStream);
445   if (FAILED(hr)) {
446     ERR(": could not acquire IPersistStream\n");
447     return hr;
448   }
449   /* load */
450   hr = IPersistStream_Load (pPersistStream, pClonedStream);
451   if (FAILED(hr)) {
452     ERR(": failed to load object\n");
453     return hr;
454   }
455   
456   /* release all loading-related stuff */
457   IPersistStream_Release (pPersistStream);
458
459   return S_OK;
460 }
461
462 static HRESULT IDirectMusicStyle8Impl_IPersistStream_ParsePartRefList (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm, LPDMUS_PRIVATE_STYLE_MOTIF pNewMotif) {
463   /*ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);*/
464   HRESULT hr = E_FAIL;
465   DMUS_PRIVATE_CHUNK Chunk;
466   DWORD ListSize[3], ListCount[3];
467   LARGE_INTEGER liMove; /* used when skipping chunks */
468
469   LPDMUS_PRIVATE_STYLE_PARTREF_ITEM pNewItem = NULL;
470
471
472   if (pChunk->fccID != DMUS_FOURCC_PARTREF_LIST) {
473     ERR_(dmfile)(": %s chunk should be a PARTREF list\n", debugstr_fourcc (pChunk->fccID));
474     return E_FAIL;
475   }  
476
477   ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
478   ListCount[0] = 0;
479
480   do {
481     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
482     ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
483     TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
484     switch (Chunk.fccID) {
485     case DMUS_FOURCC_PARTREF_CHUNK: {
486       TRACE_(dmfile)(": PartRef chunk\n");
487       pNewItem = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_STYLE_PARTREF_ITEM));
488       if (NULL == pNewItem) {
489         ERR(": no more memory\n");
490         return  E_OUTOFMEMORY;
491       }
492       hr = IStream_Read (pStm, &pNewItem->part_ref, sizeof(DMUS_IO_PARTREF), NULL);
493       /*TRACE_(dmfile)(" - sizeof %lu\n",  sizeof(DMUS_IO_PARTREF));*/
494       list_add_tail (&pNewMotif->Items, &pNewItem->entry);      
495       DM_STRUCT_INIT(&pNewItem->desc);
496       break;
497     }    
498     case FOURCC_LIST: {
499       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
500       TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
501       ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
502       ListCount[1] = 0;
503       switch (Chunk.fccID) {  
504       case DMUS_FOURCC_UNFO_LIST: { 
505         TRACE_(dmfile)(": UNFO list\n");
506         do {
507           IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
508           ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
509           TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
510           
511           hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, &pNewItem->desc);
512           if (FAILED(hr)) return hr;
513           
514           if (hr == S_FALSE) {
515             switch (Chunk.fccID) {
516             default: {
517               TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
518               liMove.QuadPart = Chunk.dwSize;
519               IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
520               break;                            
521             }
522             }
523           }  
524           TRACE_(dmfile)(": ListCount[1] = %ld < ListSize[1] = %ld\n", ListCount[1], ListSize[1]);
525         } while (ListCount[1] < ListSize[1]);
526         break;
527       }
528       default: {
529         TRACE_(dmfile)(": unknown chunk (skipping)\n");
530         liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
531         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
532         break;                                          
533       }
534       }
535       break;
536     }
537     default: {
538       TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
539       liMove.QuadPart = Chunk.dwSize;
540       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
541       break;                                            
542     }
543     }
544     TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
545   } while (ListCount[0] < ListSize[0]);
546
547   return S_OK;
548 }
549
550 static HRESULT IDirectMusicStyle8Impl_IPersistStream_ParsePartList (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
551
552   /*ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);*/
553   HRESULT hr = E_FAIL;
554   DMUS_PRIVATE_CHUNK Chunk;
555   DWORD ListSize[3], ListCount[3];
556   LARGE_INTEGER liMove; /* used when skipping chunks */
557
558   DMUS_OBJECTDESC desc;
559   DWORD dwSize = 0;
560   DWORD cnt = 0;
561
562   if (pChunk->fccID != DMUS_FOURCC_PART_LIST) {
563     ERR_(dmfile)(": %s chunk should be a PART list\n", debugstr_fourcc (pChunk->fccID));
564     return E_FAIL;
565   }  
566
567   ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
568   ListCount[0] = 0;
569
570   do {
571     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
572     ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
573     TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
574     switch (Chunk.fccID) {
575     case DMUS_FOURCC_PART_CHUNK: {
576       TRACE_(dmfile)(": Part chunk (skipping for now) (sizeof %u)\n", sizeof(DMUS_IO_STYLEPART));
577       liMove.QuadPart = Chunk.dwSize;
578       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
579       break;
580     }
581     case DMUS_FOURCC_NOTE_CHUNK: { 
582       TRACE_(dmfile)(": Note chunk (skipping for now) (sizeof %u)\n", sizeof(DMUS_IO_STYLENOTE));
583       IStream_Read (pStm, &dwSize, sizeof(DWORD), NULL);
584       cnt = (Chunk.dwSize - sizeof(DWORD));
585       TRACE_(dmfile)(" - dwSize: %lu\n", dwSize);
586       TRACE_(dmfile)(" - cnt: %lu (%lu / %lu)\n", cnt / dwSize, (Chunk.dwSize - sizeof(DWORD)), dwSize);
587       if (cnt % dwSize != 0) {
588         ERR("Invalid Array Size");
589         return E_FAIL;
590       }
591       cnt /= dwSize;
592       /** skip for now */
593       liMove.QuadPart = cnt * dwSize;
594       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
595       break;
596     }
597     case DMUS_FOURCC_CURVE_CHUNK: { 
598       TRACE_(dmfile)(": Curve chunk (skipping for now) (sizeof %u)\n", sizeof(DMUS_IO_STYLECURVE));
599       IStream_Read (pStm, &dwSize, sizeof(DWORD), NULL);
600       cnt = (Chunk.dwSize - sizeof(DWORD));
601       TRACE_(dmfile)(" - dwSize: %lu\n", dwSize);
602       TRACE_(dmfile)(" - cnt: %lu (%lu / %lu)\n", cnt / dwSize, (Chunk.dwSize - sizeof(DWORD)), dwSize);
603       if (cnt % dwSize != 0) {
604         ERR("Invalid Array Size");
605         return E_FAIL;
606       }
607       cnt /= dwSize;
608       /** skip for now */
609       liMove.QuadPart = cnt * dwSize;
610       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
611       break;
612     }
613     case DMUS_FOURCC_MARKER_CHUNK: { 
614       TRACE_(dmfile)(": Marker chunk (skipping for now)\n");
615       liMove.QuadPart = Chunk.dwSize;
616       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
617       break;
618     }
619     case DMUS_FOURCC_RESOLUTION_CHUNK: { 
620       TRACE_(dmfile)(": Resolution chunk (skipping for now)\n");
621       liMove.QuadPart = Chunk.dwSize;
622       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
623       break;
624     }
625     case DMUS_FOURCC_ANTICIPATION_CHUNK: { 
626       TRACE_(dmfile)(": Anticipation chunk (skipping for now)\n");
627       liMove.QuadPart = Chunk.dwSize;
628       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
629       break;
630     }
631     case FOURCC_LIST: {
632       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
633       TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
634       ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
635       ListCount[1] = 0;
636       switch (Chunk.fccID) { 
637       case DMUS_FOURCC_UNFO_LIST: { 
638         TRACE_(dmfile)(": UNFO list\n");
639         do {
640           IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
641           ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
642           TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
643           
644           hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, &desc);
645           if (FAILED(hr)) return hr;
646           
647           if (hr == S_FALSE) {
648             switch (Chunk.fccID) {
649             default: {
650               TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
651               liMove.QuadPart = Chunk.dwSize;
652               IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
653               break;                            
654             }
655             }
656           }  
657           TRACE_(dmfile)(": ListCount[1] = %ld < ListSize[1] = %ld\n", ListCount[1], ListSize[1]);
658         } while (ListCount[1] < ListSize[1]);
659         break;
660       }
661       default: {
662         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
663         liMove.QuadPart = Chunk.dwSize;
664         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
665         break;                                          
666       }
667       }
668     break;
669     }
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;                                            
675     }
676     }
677     TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
678   } while (ListCount[0] < ListSize[0]);
679
680   return S_OK;
681 }
682
683 static HRESULT IDirectMusicStyle8Impl_IPersistStream_ParsePatternList (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
684
685   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
686   HRESULT hr = E_FAIL;
687   DMUS_PRIVATE_CHUNK Chunk;
688   DWORD ListSize[3], ListCount[3];
689   LARGE_INTEGER liMove; /* used when skipping chunks */
690
691   DMUS_OBJECTDESC desc;
692   IDirectMusicBand* pBand = NULL;
693   LPDMUS_PRIVATE_STYLE_MOTIF pNewMotif = NULL;
694
695   DM_STRUCT_INIT(&desc);
696
697   if (pChunk->fccID != DMUS_FOURCC_PATTERN_LIST) {
698     ERR_(dmfile)(": %s chunk should be a PATTERN list\n", debugstr_fourcc (pChunk->fccID));
699     return E_FAIL;
700   }  
701
702   ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
703   ListCount[0] = 0;
704
705   do {
706     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
707     ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
708     TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
709     switch (Chunk.fccID) {
710     case DMUS_FOURCC_PATTERN_CHUNK: {
711       TRACE_(dmfile)(": Pattern chunk\n");
712       /** alloc new motif entry */
713       pNewMotif = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_STYLE_MOTIF));
714       list_add_tail (&This->Motifs, &pNewMotif->entry);
715       if (NULL == pNewMotif) {
716         ERR(": no more memory\n");
717         return  E_OUTOFMEMORY;
718       }
719
720       IStream_Read (pStm, &pNewMotif->pattern, Chunk.dwSize, NULL);
721       /** TODO trace pattern */
722
723       /** reset all datas, as a new pattern begin */
724       DM_STRUCT_INIT(&pNewMotif->desc);
725       list_init (&pNewMotif->Items);
726       break;
727     }
728     case DMUS_FOURCC_RHYTHM_CHUNK: { 
729       TRACE_(dmfile)(": Rythm chunk\n");
730       IStream_Read (pStm, &pNewMotif->dwRythm, sizeof(DWORD), NULL);
731       TRACE_(dmfile)(" - dwRythm: %lu\n", pNewMotif->dwRythm);
732       /** TODO understand why some Chunks have size > 4 */
733       liMove.QuadPart = Chunk.dwSize - sizeof(DWORD);
734       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
735       break;
736     }
737     case DMUS_FOURCC_MOTIFSETTINGS_CHUNK: {
738       TRACE_(dmfile)(": MotifSettigns chunk (skipping for now)\n");
739       IStream_Read (pStm, &pNewMotif->settings, Chunk.dwSize, NULL);
740       /** TODO trace settings */
741       break;
742     }
743     case FOURCC_RIFF: {
744       /**
745        * sould be embededs Bands into pattern
746        */
747       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
748       TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
749       ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
750       ListCount[1] = 0;
751       switch (Chunk.fccID) {
752       case DMUS_FOURCC_BAND_FORM: { 
753         LPSTREAM pClonedStream = NULL;
754         
755         TRACE_(dmfile)(": BAND RIFF\n");
756         
757         IStream_Clone (pStm, &pClonedStream);
758         
759         liMove.QuadPart = 0;
760         liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
761         IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
762         
763         hr = IDirectMusicStyle8Impl_IPersistStream_LoadBand (iface, pClonedStream, &pBand);
764         if (FAILED(hr)) {
765           ERR(": could not load track\n");
766           return hr;
767         }
768         IStream_Release (pClonedStream);
769         
770         pNewMotif->pBand = pBand;
771         IDirectMusicBand_AddRef(pBand);
772
773         IDirectMusicTrack_Release(pBand); pBand = NULL;  /* now we can release at as it inserted */
774         
775         /** now safe move the cursor */
776         liMove.QuadPart = ListSize[1];
777         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
778         
779         break;
780       }
781       default: {
782         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
783         liMove.QuadPart = ListSize[1];
784         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
785         break;
786       }
787       }
788       break;
789     }
790     case FOURCC_LIST: {
791       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
792       TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
793       ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
794       ListCount[1] = 0;
795       switch (Chunk.fccID) {
796       case DMUS_FOURCC_UNFO_LIST: { 
797         TRACE_(dmfile)(": UNFO list\n");
798         do {
799           IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
800           ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
801           TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
802           
803           hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, &pNewMotif->desc);
804           if (FAILED(hr)) return hr;
805           
806           if (hr == S_FALSE) {
807             switch (Chunk.fccID) {
808             default: {
809               TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
810               liMove.QuadPart = Chunk.dwSize;
811               IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
812               break;                            
813             }
814             }
815           }  
816           TRACE_(dmfile)(": ListCount[1] = %ld < ListSize[1] = %ld\n", ListCount[1], ListSize[1]);
817         } while (ListCount[1] < ListSize[1]);
818         break;
819       }
820       case DMUS_FOURCC_PARTREF_LIST: {
821         TRACE_(dmfile)(": PartRef list\n");
822         hr = IDirectMusicStyle8Impl_IPersistStream_ParsePartRefList (iface, &Chunk, pStm, pNewMotif);
823         if (FAILED(hr)) return hr;
824         break;
825       }
826       default: {
827         TRACE_(dmfile)(": unknown (skipping)\n");
828         liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
829         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
830         break;                                          
831       }
832       }
833       break;    
834     }
835     default: {
836       TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
837       liMove.QuadPart = Chunk.dwSize;
838       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
839       break;                                            
840     }
841     }
842     TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
843   } while (ListCount[0] < ListSize[0]);
844
845   return S_OK;
846 }
847
848 static HRESULT IDirectMusicStyle8Impl_IPersistStream_ParseStyleForm (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
849   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
850
851   HRESULT hr = E_FAIL;
852   DMUS_PRIVATE_CHUNK Chunk;
853   DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
854   LARGE_INTEGER liMove; /* used when skipping chunks */
855
856   IDirectMusicBand* pBand = NULL;
857
858   if (pChunk->fccID != DMUS_FOURCC_STYLE_FORM) {
859     ERR_(dmfile)(": %s chunk should be a STYLE form\n", debugstr_fourcc (pChunk->fccID));
860     return E_FAIL;
861   }  
862
863   StreamSize = pChunk->dwSize - sizeof(FOURCC);
864   StreamCount = 0;
865
866   do {
867     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
868     StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
869     TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
870     
871     hr = IDirectMusicUtils_IPersistStream_ParseDescGeneric(&Chunk, pStm, This->pDesc);
872     if (FAILED(hr)) return hr;
873
874     if (hr == S_FALSE) {
875       switch (Chunk.fccID) {
876       case DMUS_FOURCC_STYLE_CHUNK: {
877         TRACE_(dmfile)(": Style chunk\n");
878         IStream_Read (pStm, &This->style, sizeof(DMUS_IO_STYLE), NULL);
879         /** TODO dump DMUS_IO_TIMESIG style.timeSig */
880         TRACE_(dmfile)(" - dblTempo: %g\n", This->style.dblTempo);
881         break;
882       }   
883       case FOURCC_RIFF: {
884         /**
885          * sould be embededs Bands into style
886          */
887         IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
888         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
889         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
890         ListCount[0] = 0;
891         switch (Chunk.fccID) {
892         case DMUS_FOURCC_BAND_FORM: { 
893           LPSTREAM pClonedStream = NULL;
894           LPDMUS_PRIVATE_STYLE_BAND pNewBand;
895
896           TRACE_(dmfile)(": BAND RIFF\n");
897           
898           IStream_Clone (pStm, &pClonedStream);
899             
900           liMove.QuadPart = 0;
901           liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
902           IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
903           
904           hr = IDirectMusicStyle8Impl_IPersistStream_LoadBand (iface, pClonedStream, &pBand);
905           if (FAILED(hr)) {
906             ERR(": could not load track\n");
907             return hr;
908           }
909           IStream_Release (pClonedStream);
910           
911           pNewBand = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_STYLE_BAND));
912           if (NULL == pNewBand) {
913             ERR(": no more memory\n");
914             return  E_OUTOFMEMORY;
915           }
916           pNewBand->pBand = pBand;
917           IDirectMusicBand_AddRef(pBand);
918           list_add_tail (&This->Bands, &pNewBand->entry);
919
920           IDirectMusicTrack_Release(pBand); pBand = NULL;  /* now we can release at as it inserted */
921         
922           /** now safe move the cursor */
923           liMove.QuadPart = ListSize[0];
924           IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
925           
926           break;
927         }
928         default: {
929           TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
930           liMove.QuadPart = ListSize[0];
931           IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
932           break;
933         }
934         }
935         break;
936       }
937       case FOURCC_LIST: {
938         IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
939         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
940         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
941         ListCount[0] = 0;
942         switch (Chunk.fccID) {
943         case DMUS_FOURCC_UNFO_LIST: { 
944           TRACE_(dmfile)(": UNFO list\n");
945           do {
946             IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
947             ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
948             TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
949             
950             hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, This->pDesc);
951             if (FAILED(hr)) return hr;
952             
953             if (hr == S_FALSE) {
954               switch (Chunk.fccID) {
955               default: {
956                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
957                 liMove.QuadPart = Chunk.dwSize;
958                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
959                 break;                          
960               }
961               }
962             }  
963             TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
964           } while (ListCount[0] < ListSize[0]);
965           break;
966         }
967         case DMUS_FOURCC_PART_LIST: {
968           TRACE_(dmfile)(": PART list\n");
969           hr = IDirectMusicStyle8Impl_IPersistStream_ParsePartList (iface, &Chunk, pStm);
970           if (FAILED(hr)) return hr;
971           break;
972         }
973         case  DMUS_FOURCC_PATTERN_LIST: {
974           TRACE_(dmfile)(": PATTERN list\n");
975           hr = IDirectMusicStyle8Impl_IPersistStream_ParsePatternList (iface, &Chunk, pStm);
976           if (FAILED(hr)) return hr;
977           break;
978         }
979         default: {
980           TRACE_(dmfile)(": unknown (skipping)\n");
981           liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
982           IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
983           break;                                                
984         }
985         }
986         break;
987       }
988       default: {
989         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
990         liMove.QuadPart = Chunk.dwSize;
991         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
992         break;                                          
993       }
994       }
995     }
996     TRACE_(dmfile)(": StreamCount[0] = %ld < StreamSize[0] = %ld\n", StreamCount, StreamSize);
997   } while (StreamCount < StreamSize);  
998
999   return S_OK;
1000 }
1001
1002 HRESULT WINAPI IDirectMusicStyle8Impl_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
1003   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
1004   
1005   DMUS_PRIVATE_CHUNK Chunk;
1006   LARGE_INTEGER liMove; /* used when skipping chunks */
1007   HRESULT hr;
1008
1009   FIXME("(%p, %p): Loading\n", This, pStm);
1010
1011   IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
1012   TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
1013   switch (Chunk.fccID) {
1014   case FOURCC_RIFF: {
1015     IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
1016     TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
1017     switch (Chunk.fccID) {
1018     case DMUS_FOURCC_STYLE_FORM: {
1019       TRACE_(dmfile)(": Style form\n");
1020       hr = IDirectMusicStyle8Impl_IPersistStream_ParseStyleForm (iface, &Chunk, pStm);
1021       if (FAILED(hr)) return hr;
1022       break;
1023     }
1024     default: {
1025       TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
1026       liMove.QuadPart = Chunk.dwSize;
1027       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
1028       return E_FAIL;
1029     }
1030     }
1031     TRACE_(dmfile)(": reading finished\n");
1032     break;
1033   }
1034   default: {
1035     TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
1036     liMove.QuadPart = Chunk.dwSize;
1037     IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
1038     return E_FAIL;
1039   }
1040   }
1041   
1042   return S_OK;
1043 }
1044
1045 HRESULT WINAPI IDirectMusicStyle8Impl_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
1046   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
1047   FIXME("(%p): Saving not implemented yet\n", This);
1048   return E_NOTIMPL;
1049 }
1050
1051 HRESULT WINAPI IDirectMusicStyle8Impl_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
1052   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
1053   FIXME("(%p, %p): stub\n", This, pcbSize);
1054   return E_NOTIMPL;
1055
1056 }
1057
1058 IPersistStreamVtbl DirectMusicStyle8_PersistStream_Vtbl = {
1059   IDirectMusicStyle8Impl_IPersistStream_QueryInterface,
1060   IDirectMusicStyle8Impl_IPersistStream_AddRef,
1061   IDirectMusicStyle8Impl_IPersistStream_Release,
1062   IDirectMusicStyle8Impl_IPersistStream_GetClassID,
1063   IDirectMusicStyle8Impl_IPersistStream_IsDirty,
1064   IDirectMusicStyle8Impl_IPersistStream_Load,
1065   IDirectMusicStyle8Impl_IPersistStream_Save,
1066   IDirectMusicStyle8Impl_IPersistStream_GetSizeMax
1067 };
1068
1069 /* for ClassFactory */
1070 HRESULT WINAPI DMUSIC_CreateDirectMusicStyleImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
1071   IDirectMusicStyle8Impl* obj;
1072         
1073   obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicStyle8Impl));
1074   if (NULL == obj) {
1075     *ppobj = NULL;
1076     return E_OUTOFMEMORY;
1077   }
1078   obj->UnknownVtbl = &DirectMusicStyle8_Unknown_Vtbl;
1079   obj->StyleVtbl = &DirectMusicStyle8_Style_Vtbl;
1080   obj->ObjectVtbl = &DirectMusicStyle8_Object_Vtbl;
1081   obj->PersistStreamVtbl = &DirectMusicStyle8_PersistStream_Vtbl;
1082   obj->pDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DMUS_OBJECTDESC));
1083   DM_STRUCT_INIT(obj->pDesc);
1084   obj->pDesc->dwValidData |= DMUS_OBJ_CLASS;
1085   memcpy (&obj->pDesc->guidClass, &CLSID_DirectMusicStyle, sizeof (CLSID));
1086   obj->ref = 0; /* will be inited by QueryInterface */
1087   list_init (&obj->Bands);
1088   list_init (&obj->Motifs);
1089
1090   return IDirectMusicStyle8Impl_IUnknown_QueryInterface ((LPUNKNOWN)&obj->UnknownVtbl, lpcGUID, ppobj);
1091 }