source: trunk/src/shell32/memorystream.cpp@ 1330

Last change on this file since 1330 was 1215, checked in by sandervl, 26 years ago

Added $Id$

File size: 7.2 KB
Line 
1/* $Id: memorystream.cpp,v 1.2 1999-10-09 11:17:00 sandervl Exp $ */
2/*
3 * this class implements a pure IStream object
4 * and can be used for many purposes
5 *
6 * the main reason for implementing this was
7 * a cleaner implementation of IShellLink which
8 * needs to be able to load lnk's from a IStream
9 * interface so it was obvious to capsule the file
10 * access in a IStream to.
11 */
12
13#include <string.h>
14#include <odin.h>
15
16#define ICOM_CINTERFACE 1
17#define CINTERFACE 1
18
19#include "wine/obj_storage.h"
20#include "heap.h"
21#include "winerror.h"
22#include "debugtools.h"
23#include "shell32_main.h"
24
25#include <misc.h>
26
27DEFAULT_DEBUG_CHANNEL(shell)
28
29static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj);
30static ULONG WINAPI IStream_fnAddRef(IStream *iface);
31static ULONG WINAPI IStream_fnRelease(IStream *iface);
32static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead);
33static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten);
34static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition);
35static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize);
36static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten);
37static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags);
38static HRESULT WINAPI IStream_fnRevert (IStream * iface);
39static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
40static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
41static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag);
42static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm);
43
44static ICOM_VTABLE(IStream) stvt =
45{
46 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
47 IStream_fnQueryInterface,
48 IStream_fnAddRef,
49 IStream_fnRelease,
50 IStream_fnRead,
51 IStream_fnWrite,
52 IStream_fnSeek,
53 IStream_fnSetSize,
54 IStream_fnCopyTo,
55 IStream_fnCommit,
56 IStream_fnRevert,
57 IStream_fnLockRegion,
58 IStream_fnUnlockRegion,
59 IStream_fnStat,
60 IStream_fnClone
61
62};
63
64typedef struct
65{ ICOM_VTABLE(IStream) *lpvtst;
66 DWORD ref;
67 LPBYTE pImage;
68 HANDLE hMapping;
69 DWORD dwLength;
70 DWORD dwPos;
71} ISHFileStream;
72
73/**************************************************************************
74 * CreateStreamOnFile()
75 *
76 * similar to CreateStreamOnHGlobal
77 */
78HRESULT CreateStreamOnFile (LPCSTR pszFilename, IStream ** ppstm)
79{
80 ISHFileStream* fstr;
81 OFSTRUCT ofs;
82 HFILE hFile = OpenFile( pszFilename, &ofs, OF_READ );
83 HRESULT ret = E_FAIL;
84
85 fstr = (ISHFileStream*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ISHFileStream));
86 fstr->lpvtst=&stvt;
87 fstr->ref = 1;
88 fstr->dwLength = GetFileSize (hFile, NULL);
89
90 shell32_ObjCount++;
91
92 if (!(fstr->hMapping = CreateFileMappingA(hFile,NULL,PAGE_READONLY|SEC_COMMIT,0,0,NULL)))
93 {
94 WARN("failed to create filemap.\n");
95 goto end_2;
96 }
97
98 if (!(fstr->pImage = (BYTE*)MapViewOfFile(fstr->hMapping,FILE_MAP_READ,0,0,0)))
99 {
100 WARN("failed to mmap filemap.\n");
101 goto end_3;
102 }
103
104 ret = S_OK;
105 goto end_1;
106
107end_3: CloseHandle(fstr->hMapping);
108end_2: HeapFree(GetProcessHeap(), 0, fstr);
109 fstr = NULL;
110
111end_1: _lclose(hFile);
112 (*ppstm) = (IStream*)fstr;
113 return ret;
114}
115
116/**************************************************************************
117* IStream_fnQueryInterface
118*/
119static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
120{
121 ICOM_THIS(ISHFileStream, iface);
122
123 char xriid[50];
124 WINE_StringFromCLSID((LPCLSID)riid,xriid);
125
126 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,xriid,ppvObj);
127
128 *ppvObj = NULL;
129
130 if(IsEqualIID(riid, &IID_IUnknown) ||
131 IsEqualIID(riid, &IID_IStream))
132 {
133 *ppvObj = This;
134 }
135
136 if(*ppvObj)
137 {
138 IStream_AddRef((IStream*)*ppvObj);
139 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
140 return S_OK;
141 }
142 TRACE("-- Interface: E_NOINTERFACE\n");
143 return E_NOINTERFACE;
144}
145
146/**************************************************************************
147* IStream_fnAddRef
148*/
149static ULONG WINAPI IStream_fnAddRef(IStream *iface)
150{
151 ICOM_THIS(ISHFileStream, iface);
152
153 TRACE("(%p)->(count=%lu)\n",This, This->ref);
154
155 shell32_ObjCount++;
156 return ++(This->ref);
157}
158
159/**************************************************************************
160* IStream_fnRelease
161*/
162static ULONG WINAPI IStream_fnRelease(IStream *iface)
163{
164 ICOM_THIS(ISHFileStream, iface);
165
166 TRACE("(%p)->()\n",This);
167
168 shell32_ObjCount--;
169
170 if (!--(This->ref))
171 { TRACE(" destroying SHFileStream (%p)\n",This);
172
173 UnmapViewOfFile(This->pImage);
174 CloseHandle(This->hMapping);
175
176 HeapFree(GetProcessHeap(),0,This);
177 return 0;
178 }
179 return This->ref;
180}
181
182static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
183{
184 ICOM_THIS(ISHFileStream, iface);
185
186 DWORD dwBytesToRead, dwBytesLeft;
187
188 TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
189
190 if ( !pv )
191 return STG_E_INVALIDPOINTER;
192
193 dwBytesLeft = This->dwLength - This->dwPos;
194
195 if ( 0 >= dwBytesLeft ) /* end of buffer */
196 return S_FALSE;
197
198 dwBytesToRead = ( cb > dwBytesLeft) ? dwBytesLeft : cb;
199
200 memmove ( pv, (This->pImage) + (This->dwPos), dwBytesToRead);
201
202 This->dwPos += dwBytesToRead; /* adjust pointer */
203
204 if (pcbRead)
205 *pcbRead = dwBytesToRead;
206
207 return S_OK;
208}
209static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
210{
211 ICOM_THIS(ISHFileStream, iface);
212
213 TRACE("(%p)\n",This);
214
215 return E_NOTIMPL;
216}
217static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
218{
219 ICOM_THIS(ISHFileStream, iface);
220
221 TRACE("(%p)\n",This);
222
223 return E_NOTIMPL;
224}
225static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
226{
227 ICOM_THIS(ISHFileStream, iface);
228
229 TRACE("(%p)\n",This);
230
231 return E_NOTIMPL;
232}
233static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
234{
235 ICOM_THIS(ISHFileStream, iface);
236
237 TRACE("(%p)\n",This);
238
239 return E_NOTIMPL;
240}
241static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
242{
243 ICOM_THIS(ISHFileStream, iface);
244
245 TRACE("(%p)\n",This);
246
247 return E_NOTIMPL;
248}
249static HRESULT WINAPI IStream_fnRevert (IStream * iface)
250{
251 ICOM_THIS(ISHFileStream, iface);
252
253 TRACE("(%p)\n",This);
254
255 return E_NOTIMPL;
256}
257static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
258{
259 ICOM_THIS(ISHFileStream, iface);
260
261 TRACE("(%p)\n",This);
262
263 return E_NOTIMPL;
264}
265static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
266{
267 ICOM_THIS(ISHFileStream, iface);
268
269 TRACE("(%p)\n",This);
270
271 return E_NOTIMPL;
272}
273static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag)
274{
275 ICOM_THIS(ISHFileStream, iface);
276
277 TRACE("(%p)\n",This);
278
279 return E_NOTIMPL;
280}
281static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
282{
283 ICOM_THIS(ISHFileStream, iface);
284
285 TRACE("(%p)\n",This);
286
287 return E_NOTIMPL;
288}
Note: See TracBrowser for help on using the repository browser.