Fixed definitions of TTTOOLINFOA/W_V1_SIZE and
[wine] / dlls / dmloader / loaderstream.c
1 /* ILoaderStream Implementation
2  *
3  * Copyright (C) 2003 Rok Mandeljc
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #define NONAMELESSUNION
21 #define NONAMELESSSTRUCT
22 #include <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "wingdi.h"
28 #include "wine/debug.h"
29 #include "wine/unicode.h"
30
31 #include "dmloader_private.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(dmloader);
34
35 /*****************************************************************************
36  * Custom functions:
37  */
38 HRESULT WINAPI ILoaderStream_Attach (ILoaderStream* This, LPCWSTR wzFile, IDirectMusicLoader *pLoader)
39 {
40         TRACE("(%p, %s, %p)\n", This, debugstr_w(wzFile), pLoader);
41         ILoaderStream_Detach (This);
42         This->hFile = CreateFileW (wzFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
43     if (This->hFile == INVALID_HANDLE_VALUE) {
44                 TRACE(": failed\n");
45         return E_FAIL;
46     }
47         /* create IDirectMusicGetLoader */
48     (LPDIRECTMUSICLOADER)This->pLoader = pLoader;
49     IDirectMusicLoader8_AddRef ((LPDIRECTMUSICLOADER8)This->pLoader);
50     strncpyW (This->wzFileName, wzFile, MAX_PATH);
51         TRACE(": succeeded\n");
52         
53     return S_OK;
54 }
55
56 void WINAPI ILoaderStream_Detach (ILoaderStream* This)
57 {
58         if (This->hFile != INVALID_HANDLE_VALUE) {
59         CloseHandle(This->hFile);
60     }
61     This->wzFileName[0] = (L'\0');
62 }
63
64 /*****************************************************************************
65  * ILoaderStream IStream:
66  */
67 HRESULT WINAPI ILoaderStream_IStream_QueryInterface (LPSTREAM iface, REFIID riid, void** ppobj)
68 {
69         ICOM_THIS_MULTI(ILoaderStream, StreamVtbl, iface);
70         
71         if (IsEqualIID (riid, &IID_IUnknown)
72                 || IsEqualIID (riid, &IID_IStream)) {
73                 *ppobj = (LPVOID)&This->StreamVtbl;
74                 ILoaderStream_IStream_AddRef (iface);
75                 return S_OK;
76         } else if (IsEqualIID (riid, &IID_IDirectMusicGetLoader)) {
77                 *ppobj = (LPVOID)&This->GetLoaderVtbl;
78                 ILoaderStream_IStream_AddRef (iface);           
79                 return S_OK;
80         }
81
82         WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
83         return E_NOINTERFACE;
84 }
85
86 ULONG WINAPI ILoaderStream_IStream_AddRef (LPSTREAM iface)
87 {
88         ICOM_THIS_MULTI(ILoaderStream, StreamVtbl, iface);
89         TRACE("(%p) : AddRef from %ld\n", This, This->ref);
90         return ++(This->ref);
91 }
92
93 ULONG WINAPI ILoaderStream_IStream_Release (LPSTREAM iface)
94 {
95         ICOM_THIS_MULTI(ILoaderStream, StreamVtbl, iface);
96         ULONG ref = --This->ref;
97         TRACE("(%p) : ReleaseRef to %ld\n", This, This->ref);
98         if (ref == 0) {
99                 HeapFree(GetProcessHeap(), 0, This);
100         }
101         return ref;
102 }
103
104 HRESULT WINAPI ILoaderStream_IStream_Read (LPSTREAM iface, void* pv, ULONG cb, ULONG* pcbRead)
105 {
106         ICOM_THIS_MULTI(ILoaderStream, StreamVtbl, iface);
107     ULONG cbRead;
108
109     if (This->hFile == INVALID_HANDLE_VALUE) return E_FAIL;
110     if (pcbRead == NULL) pcbRead = &cbRead;
111     if (!ReadFile (This->hFile, pv, cb, pcbRead, NULL) || *pcbRead != cb) return E_FAIL;
112
113     return S_OK;
114 }
115
116 HRESULT WINAPI ILoaderStream_IStream_Seek (LPSTREAM iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
117 {
118         ICOM_THIS_MULTI(ILoaderStream, StreamVtbl, iface);
119     LARGE_INTEGER liNewPos;
120
121         if (This->hFile == INVALID_HANDLE_VALUE) return E_FAIL;
122
123     liNewPos.s.HighPart = dlibMove.s.HighPart;
124     liNewPos.s.LowPart = SetFilePointer (This->hFile, dlibMove.s.LowPart, &liNewPos.s.HighPart, dwOrigin);
125
126     if (liNewPos.s.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR) return E_FAIL;
127     if (plibNewPosition) plibNewPosition->QuadPart = liNewPos.QuadPart;
128     
129     return S_OK;
130 }
131
132 HRESULT WINAPI ILoaderStream_IStream_Clone (LPSTREAM iface, IStream** ppstm)
133 {
134         ICOM_THIS_MULTI(ILoaderStream, StreamVtbl, iface);
135         ILoaderStream* pOther = NULL;
136         HRESULT result;
137
138         TRACE("(%p, %p)\n", iface, ppstm);
139         result = DMUSIC_CreateLoaderStream ((LPSTREAM*)&pOther);
140         if (FAILED(result)) return result;
141         if (This->hFile != INVALID_HANDLE_VALUE) {
142                 ULARGE_INTEGER ullCurrentPosition;
143                 result = ILoaderStream_Attach (pOther, This->wzFileName, (LPDIRECTMUSICLOADER)This->pLoader);
144                 if (SUCCEEDED(result)) {
145                         LARGE_INTEGER liZero;
146                         liZero.QuadPart = 0;
147                         result = ILoaderStream_IStream_Seek (iface, liZero, STREAM_SEEK_CUR, &ullCurrentPosition); /* get current position in current stream */
148         }
149                 if (SUCCEEDED(result)) {
150                         LARGE_INTEGER liNewPosition;
151                         liNewPosition.QuadPart = ullCurrentPosition.QuadPart;
152                         result = ILoaderStream_IStream_Seek ((LPSTREAM)pOther, liNewPosition, STREAM_SEEK_SET, &ullCurrentPosition);
153                 }
154                 if (FAILED(result)) {
155                         TRACE(": failed\n");
156                         ILoaderStream_IStream_Release ((LPSTREAM)pOther);
157                         return result;
158                 }
159         }
160         TRACE(": succeeded\n");
161         *ppstm = (IStream*)pOther;
162         return S_OK;
163 }
164
165 /* not needed*/
166 HRESULT WINAPI ILoaderStream_IStream_Write (LPSTREAM iface, const void* pv, ULONG cb, ULONG* pcbWritten)
167 {
168         return E_NOTIMPL;
169 }
170
171 HRESULT WINAPI ILoaderStream_IStream_SetSize (LPSTREAM iface, ULARGE_INTEGER libNewSize)
172 {
173     return E_NOTIMPL;
174 }
175
176 HRESULT WINAPI ILoaderStream_IStream_CopyTo (LPSTREAM iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
177 {
178     return E_NOTIMPL;
179 }
180
181 HRESULT WINAPI ILoaderStream_IStream_Commit (LPSTREAM iface, DWORD grfCommitFlags)
182 {
183     return E_NOTIMPL;
184 }
185
186 HRESULT WINAPI ILoaderStream_IStream_Revert (LPSTREAM iface)
187 {
188     return E_NOTIMPL;
189 }
190
191 HRESULT WINAPI ILoaderStream_IStream_LockRegion (LPSTREAM iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
192 {
193     return E_NOTIMPL;
194 }
195
196 HRESULT WINAPI ILoaderStream_IStream_UnlockRegion (LPSTREAM iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
197 {
198     return E_NOTIMPL;
199 }
200
201 HRESULT WINAPI ILoaderStream_IStream_Stat (LPSTREAM iface, STATSTG* pstatstg, DWORD grfStatFlag)
202 {
203     return E_NOTIMPL;
204 }
205
206 ICOM_VTABLE(IStream) LoaderStream_Stream_Vtbl =
207 {
208     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
209         ILoaderStream_IStream_QueryInterface,
210         ILoaderStream_IStream_AddRef,
211         ILoaderStream_IStream_Release,
212         ILoaderStream_IStream_Read,
213         ILoaderStream_IStream_Write,
214         ILoaderStream_IStream_Seek,
215         ILoaderStream_IStream_SetSize,
216         ILoaderStream_IStream_CopyTo,
217         ILoaderStream_IStream_Commit,
218         ILoaderStream_IStream_Revert,
219         ILoaderStream_IStream_LockRegion,
220         ILoaderStream_IStream_UnlockRegion,
221         ILoaderStream_IStream_Stat,
222         ILoaderStream_IStream_Clone
223 };
224
225 /*****************************************************************************
226  * ILoaderStream IDirectMusicGetLoader:
227  */
228 HRESULT WINAPI ILoaderStream_IDirectMusicGetLoader_QueryInterface (LPDIRECTMUSICGETLOADER iface, REFIID riid, void** ppobj)
229 {
230         ICOM_THIS_MULTI(ILoaderStream, GetLoaderVtbl, iface);
231         return ILoaderStream_IStream_QueryInterface ((LPSTREAM)&This->StreamVtbl, riid, ppobj);
232 }
233
234 ULONG WINAPI ILoaderStream_IDirectMusicGetLoader_AddRef (LPDIRECTMUSICGETLOADER iface)
235 {
236         ICOM_THIS_MULTI(ILoaderStream, GetLoaderVtbl, iface);
237         return ILoaderStream_IStream_AddRef ((LPSTREAM)&This->StreamVtbl);
238 }
239
240 ULONG WINAPI ILoaderStream_IDirectMusicGetLoader_Release (LPDIRECTMUSICGETLOADER iface)
241 {
242         ICOM_THIS_MULTI(ILoaderStream, GetLoaderVtbl, iface);
243         return ILoaderStream_IStream_Release ((LPSTREAM)&This->StreamVtbl);
244 }
245
246 HRESULT WINAPI ILoaderStream_IDirectMusicGetLoader_GetLoader (LPDIRECTMUSICGETLOADER iface, IDirectMusicLoader **ppLoader)
247 {
248         ICOM_THIS_MULTI(ILoaderStream, GetLoaderVtbl, iface);
249
250         TRACE("(%p, %p)\n", This, ppLoader);
251         *ppLoader = (LPDIRECTMUSICLOADER)This->pLoader;
252
253         return S_OK;
254 }
255
256 ICOM_VTABLE(IDirectMusicGetLoader) LoaderStream_GetLoader_Vtbl =
257 {
258     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
259         ILoaderStream_IDirectMusicGetLoader_QueryInterface,
260         ILoaderStream_IDirectMusicGetLoader_AddRef,
261         ILoaderStream_IDirectMusicGetLoader_Release,
262         ILoaderStream_IDirectMusicGetLoader_GetLoader
263 };
264
265
266 HRESULT WINAPI DMUSIC_CreateLoaderStream (LPSTREAM* ppStream)
267 {
268         ILoaderStream *pStream;
269
270         TRACE("(%p)\n", ppStream);
271
272         pStream = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(ILoaderStream));
273         if (NULL == pStream) {
274                 *ppStream = (LPSTREAM)NULL;
275                 return E_OUTOFMEMORY;
276         }
277         pStream->StreamVtbl = &LoaderStream_Stream_Vtbl;
278         pStream->GetLoaderVtbl = &LoaderStream_GetLoader_Vtbl;
279         pStream->ref = 1;
280         
281         *ppStream = (LPSTREAM)pStream;
282         return S_OK;
283 }