Sync with trunk r63174.
[reactos.git] / dll / win32 / urlmon / format.c
1 /*
2 * Copyright 2005 Jacek Caban
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19 #include "urlmon_main.h"
20
21 static WCHAR wszEnumFORMATETC[] = {'_','E','n','u','m','F','O','R','M','A','T','E','T','C','_',0};
22
23 typedef struct {
24 IEnumFORMATETC IEnumFORMATETC_iface;
25
26 FORMATETC *fetc;
27 UINT fetc_cnt;
28 UINT it;
29
30 LONG ref;
31 } EnumFORMATETC;
32
33 static inline EnumFORMATETC *impl_from_IEnumFORMATETC(IEnumFORMATETC *iface)
34 {
35 return CONTAINING_RECORD(iface, EnumFORMATETC, IEnumFORMATETC_iface);
36 }
37
38 static IEnumFORMATETC *EnumFORMATETC_Create(UINT cfmtetc, const FORMATETC *rgfmtetc, UINT it);
39
40 static HRESULT WINAPI EnumFORMATETC_QueryInterface(IEnumFORMATETC *iface, REFIID riid, void **ppv)
41 {
42 EnumFORMATETC *This = impl_from_IEnumFORMATETC(iface);
43
44 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
45
46 *ppv = NULL;
47
48 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IEnumFORMATETC, riid)) {
49 IEnumFORMATETC_AddRef(iface);
50 *ppv = iface;
51 return S_OK;
52 }
53
54 WARN("not supported interface %s\n", debugstr_guid(riid));
55 return E_NOINTERFACE;
56 }
57
58 static ULONG WINAPI EnumFORMATETC_AddRef(IEnumFORMATETC *iface)
59 {
60 EnumFORMATETC *This = impl_from_IEnumFORMATETC(iface);
61 LONG ref = InterlockedIncrement(&This->ref);
62 TRACE("(%p) ref=%d\n", This, ref);
63 return ref;
64 }
65
66 static ULONG WINAPI EnumFORMATETC_Release(IEnumFORMATETC *iface)
67 {
68 EnumFORMATETC *This = impl_from_IEnumFORMATETC(iface);
69 LONG ref = InterlockedDecrement(&This->ref);
70
71 TRACE("(%p) ref=%d\n", This, ref);
72
73 if(!ref) {
74 heap_free(This->fetc);
75 heap_free(This);
76
77 URLMON_UnlockModule();
78 }
79
80 return ref;
81 }
82
83 static HRESULT WINAPI EnumFORMATETC_Next(IEnumFORMATETC *iface, ULONG celt,
84 FORMATETC *rgelt, ULONG *pceltFetched)
85 {
86 EnumFORMATETC *This = impl_from_IEnumFORMATETC(iface);
87 ULONG cnt;
88
89 TRACE("(%p)->(%d %p %p)\n", This, celt, rgelt, pceltFetched);
90
91 if(!rgelt)
92 return E_INVALIDARG;
93
94 if(This->it >= This->fetc_cnt || !celt) {
95 if(pceltFetched)
96 *pceltFetched = 0;
97 return celt ? S_FALSE : S_OK;
98 }
99
100 cnt = This->fetc_cnt-This->it > celt ? celt : This->fetc_cnt-This->it;
101
102 memcpy(rgelt, This->fetc+This->it, cnt*sizeof(FORMATETC));
103 This->it += cnt;
104
105 if(pceltFetched)
106 *pceltFetched = cnt;
107
108 return cnt == celt ? S_OK : S_FALSE;
109 }
110
111 static HRESULT WINAPI EnumFORMATETC_Skip(IEnumFORMATETC *iface, ULONG celt)
112 {
113 EnumFORMATETC *This = impl_from_IEnumFORMATETC(iface);
114
115 TRACE("(%p)->(%d)\n", This, celt);
116
117 This->it += celt;
118 return This->it > This->fetc_cnt ? S_FALSE : S_OK;
119 }
120
121 static HRESULT WINAPI EnumFORMATETC_Reset(IEnumFORMATETC *iface)
122 {
123 EnumFORMATETC *This = impl_from_IEnumFORMATETC(iface);
124
125 TRACE("(%p)\n", This);
126
127 This->it = 0;
128 return S_OK;
129 }
130
131 static HRESULT WINAPI EnumFORMATETC_Clone(IEnumFORMATETC *iface, IEnumFORMATETC **ppenum)
132 {
133 EnumFORMATETC *This = impl_from_IEnumFORMATETC(iface);
134
135 TRACE("(%p)->(%p)\n", This, ppenum);
136
137 if(!ppenum)
138 return E_INVALIDARG;
139
140 *ppenum = EnumFORMATETC_Create(This->fetc_cnt, This->fetc, This->it);
141 return S_OK;
142 }
143
144 static const IEnumFORMATETCVtbl EnumFORMATETCVtbl = {
145 EnumFORMATETC_QueryInterface,
146 EnumFORMATETC_AddRef,
147 EnumFORMATETC_Release,
148 EnumFORMATETC_Next,
149 EnumFORMATETC_Skip,
150 EnumFORMATETC_Reset,
151 EnumFORMATETC_Clone
152 };
153
154 static IEnumFORMATETC *EnumFORMATETC_Create(UINT cfmtetc, const FORMATETC *rgfmtetc, UINT it)
155 {
156 EnumFORMATETC *ret = heap_alloc(sizeof(EnumFORMATETC));
157
158 URLMON_LockModule();
159
160 ret->IEnumFORMATETC_iface.lpVtbl = &EnumFORMATETCVtbl;
161 ret->ref = 1;
162 ret->it = it;
163 ret->fetc_cnt = cfmtetc;
164
165 ret->fetc = heap_alloc(cfmtetc*sizeof(FORMATETC));
166 memcpy(ret->fetc, rgfmtetc, cfmtetc*sizeof(FORMATETC));
167
168 return &ret->IEnumFORMATETC_iface;
169 }
170
171 /**********************************************************
172 * CreateFormatEnumerator (urlmon.@)
173 */
174 HRESULT WINAPI CreateFormatEnumerator(UINT cfmtetc, FORMATETC *rgfmtetc,
175 IEnumFORMATETC** ppenumfmtetc)
176 {
177 TRACE("(%d %p %p)\n", cfmtetc, rgfmtetc, ppenumfmtetc);
178
179 if(!ppenumfmtetc)
180 return E_INVALIDARG;
181 if(!cfmtetc)
182 return E_FAIL;
183
184 *ppenumfmtetc = EnumFORMATETC_Create(cfmtetc, rgfmtetc, 0);
185 return S_OK;
186 }
187
188 /**********************************************************
189 * RegisterFormatEnumerator (urlmon.@)
190 */
191 HRESULT WINAPI RegisterFormatEnumerator(LPBC pBC, IEnumFORMATETC *pEFetc, DWORD reserved)
192 {
193 TRACE("(%p %p %d)\n", pBC, pEFetc, reserved);
194
195 if(reserved)
196 WARN("reserved != 0\n");
197
198 if(!pBC || !pEFetc)
199 return E_INVALIDARG;
200
201 return IBindCtx_RegisterObjectParam(pBC, wszEnumFORMATETC, (IUnknown*)pEFetc);
202 }
203
204 /**********************************************************
205 * RevokeFormatEnumerator (urlmon.@)
206 */
207 HRESULT WINAPI RevokeFormatEnumerator(LPBC pbc, IEnumFORMATETC *pEFetc)
208 {
209 TRACE("(%p %p)\n", pbc, pEFetc);
210
211 if(!pbc)
212 return E_INVALIDARG;
213
214 return IBindCtx_RevokeObjectParam(pbc, wszEnumFORMATETC);
215 }