source: trunk/src/shell32/memorystream.c@ 5087

Last change on this file since 5087 was 4121, checked in by sandervl, 25 years ago

complete merge with shell32 from wine 20000801

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