wined3d: No longer make sub-resources full resources.
[wine.git] / dlls / windowscodecs / propertybag.c
blob8e81ec01ed24d89c22bcbfe6673cbf1b4aa44f9b
1 /*
2 * Copyright 2009 Vincent Povirk for CodeWeavers
3 * Copyright 2013 Ludger Sprenker
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "config.h"
22 #include <stdarg.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "objbase.h"
29 #include "wine/unicode.h"
31 #include "wincodecs_private.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
37 typedef struct PropertyBag {
38 IPropertyBag2 IPropertyBag2_iface;
39 LONG ref;
40 UINT prop_count;
41 PROPBAG2 *properties;
42 VARIANT *values;
43 } PropertyBag;
45 static inline PropertyBag *impl_from_IPropertyBag2(IPropertyBag2 *iface)
47 return CONTAINING_RECORD(iface, PropertyBag, IPropertyBag2_iface);
50 static HRESULT WINAPI PropertyBag_QueryInterface(IPropertyBag2 *iface, REFIID iid,
51 void **ppv)
53 PropertyBag *This = impl_from_IPropertyBag2(iface);
54 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
56 if (!ppv) return E_INVALIDARG;
58 if (IsEqualIID(&IID_IUnknown, iid) ||
59 IsEqualIID(&IID_IPropertyBag2, iid))
61 *ppv = &This->IPropertyBag2_iface;
63 else
65 *ppv = NULL;
66 return E_NOINTERFACE;
69 IUnknown_AddRef((IUnknown*)*ppv);
70 return S_OK;
73 static ULONG WINAPI PropertyBag_AddRef(IPropertyBag2 *iface)
75 PropertyBag *This = impl_from_IPropertyBag2(iface);
76 ULONG ref = InterlockedIncrement(&This->ref);
78 TRACE("(%p) refcount=%u\n", iface, ref);
80 return ref;
83 static ULONG WINAPI PropertyBag_Release(IPropertyBag2 *iface)
85 PropertyBag *This = impl_from_IPropertyBag2(iface);
86 ULONG ref = InterlockedDecrement(&This->ref);
88 TRACE("(%p) refcount=%u\n", iface, ref);
90 if (ref == 0)
92 ULONG i;
93 if (This->properties && This->values)
95 for (i=0; i < This->prop_count; i++)
97 HeapFree(GetProcessHeap(), 0, This->properties[i].pstrName);
98 VariantClear( This->values+i );
102 HeapFree(GetProcessHeap(), 0, This->properties);
103 HeapFree(GetProcessHeap(), 0, This->values);
104 HeapFree(GetProcessHeap(), 0, This);
107 return ref;
110 static LONG find_item(PropertyBag *This, LPCOLESTR name)
112 LONG i;
113 if (!This->properties)
114 return -1;
115 if (!name)
116 return -1;
118 for (i=0; i < This->prop_count; i++)
120 if (strcmpW(name, This->properties[i].pstrName) == 0)
121 return i;
124 return -1;
127 static HRESULT WINAPI PropertyBag_Read(IPropertyBag2 *iface, ULONG cProperties,
128 PROPBAG2 *pPropBag, IErrorLog *pErrLog, VARIANT *pvarValue, HRESULT *phrError)
130 HRESULT res = S_OK;
131 ULONG i;
132 PropertyBag *This = impl_from_IPropertyBag2(iface);
134 TRACE("(%p,%u,%p,%p,%p,%p)\n", iface, cProperties, pPropBag, pErrLog, pvarValue, phrError);
136 for (i=0; i < cProperties; i++)
138 LONG idx;
139 if (pPropBag[i].dwHint && pPropBag[i].dwHint <= This->prop_count)
140 idx = pPropBag[i].dwHint-1;
141 else
142 idx = find_item(This, pPropBag[i].pstrName);
144 if (idx > -1)
146 VariantInit(pvarValue+i);
147 res = VariantCopy(pvarValue+i, This->values+idx);
148 if (FAILED(res))
149 break;
150 phrError[i] = res;
152 else
154 res = E_FAIL;
155 break;
159 return res;
162 static HRESULT WINAPI PropertyBag_Write(IPropertyBag2 *iface, ULONG cProperties,
163 PROPBAG2 *pPropBag, VARIANT *pvarValue)
165 HRESULT res = S_OK;
166 ULONG i;
167 PropertyBag *This = impl_from_IPropertyBag2(iface);
169 TRACE("(%p,%u,%p,%p)\n", iface, cProperties, pPropBag, pvarValue);
171 for (i=0; i < cProperties; i++)
173 LONG idx;
174 if (pPropBag[i].dwHint && pPropBag[i].dwHint <= This->prop_count)
175 idx = pPropBag[i].dwHint-1;
176 else
177 idx = find_item(This, pPropBag[i].pstrName);
179 if (idx > -1)
181 if (This->properties[idx].vt != V_VT(pvarValue+i))
182 return WINCODEC_ERR_PROPERTYUNEXPECTEDTYPE;
183 res = VariantCopy(This->values+idx, pvarValue+i);
184 if (FAILED(res))
185 return E_FAIL;
187 else
189 if (pPropBag[i].pstrName)
190 FIXME("Application tried to set the unknown option %s.\n",
191 debugstr_w(pPropBag[i].pstrName));
193 /* FIXME: Function is not atomar on error, but MSDN does not say anything about it
194 * (no reset of items between 0 and i-1) */
195 return E_FAIL;
199 return res;
202 static HRESULT WINAPI PropertyBag_CountProperties(IPropertyBag2 *iface, ULONG *pcProperties)
204 PropertyBag *This = impl_from_IPropertyBag2(iface);
206 TRACE("(%p,%p)\n", iface, pcProperties);
208 if (!pcProperties)
209 return E_INVALIDARG;
211 *pcProperties = This->prop_count;
213 return S_OK;
216 static HRESULT copy_propbag2(PROPBAG2 *dest, PROPBAG2 *src, BOOL useCoAlloc)
218 dest->cfType = src->cfType;
219 dest->clsid = src->clsid;
220 dest->dwHint = src->dwHint;
221 dest->dwType = src->dwType;
222 dest->vt = src->vt;
223 if(useCoAlloc)
224 dest->pstrName = CoTaskMemAlloc((strlenW(src->pstrName)+1) * sizeof(WCHAR));
225 else
226 dest->pstrName = HeapAlloc(GetProcessHeap(), 0, (strlenW(src->pstrName)+1) * sizeof(WCHAR));
228 if(!dest->pstrName)
229 return E_OUTOFMEMORY;
231 strcpyW(dest->pstrName, src->pstrName);
233 return S_OK;
236 static HRESULT WINAPI PropertyBag_GetPropertyInfo(IPropertyBag2 *iface, ULONG iProperty,
237 ULONG cProperties, PROPBAG2 *pPropBag, ULONG *pcProperties)
239 HRESULT res = S_OK;
240 ULONG i;
241 PropertyBag *This = impl_from_IPropertyBag2(iface);
243 TRACE("(%p,%u,%u,%p,%p)\n", iface, iProperty, cProperties, pPropBag, pcProperties);
245 if (iProperty >= This->prop_count && iProperty > 0)
246 return WINCODEC_ERR_VALUEOUTOFRANGE;
247 if (iProperty+cProperties > This->prop_count )
248 return WINCODEC_ERR_VALUEOUTOFRANGE;
250 *pcProperties = min(cProperties, This->prop_count-iProperty);
252 for (i=0; i < *pcProperties; i++)
254 res = copy_propbag2(pPropBag+i, This->properties+iProperty+i, TRUE);
255 if (FAILED(res))
257 do {
258 CoTaskMemFree( pPropBag[--i].pstrName );
259 } while (i);
260 break;
264 return res;
267 static HRESULT WINAPI PropertyBag_LoadObject(IPropertyBag2 *iface, LPCOLESTR pstrName,
268 DWORD dwHint, IUnknown *pUnkObject, IErrorLog *pErrLog)
270 FIXME("(%p,%s,%u,%p,%p): stub\n", iface, debugstr_w(pstrName), dwHint, pUnkObject, pErrLog);
271 return E_NOTIMPL;
274 static const IPropertyBag2Vtbl PropertyBag_Vtbl = {
275 PropertyBag_QueryInterface,
276 PropertyBag_AddRef,
277 PropertyBag_Release,
278 PropertyBag_Read,
279 PropertyBag_Write,
280 PropertyBag_CountProperties,
281 PropertyBag_GetPropertyInfo,
282 PropertyBag_LoadObject
285 HRESULT CreatePropertyBag2(PROPBAG2 *options, UINT count,
286 IPropertyBag2 **ppPropertyBag2)
288 UINT i;
289 HRESULT res = S_OK;
290 PropertyBag *This;
292 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PropertyBag));
293 if (!This) return E_OUTOFMEMORY;
295 This->IPropertyBag2_iface.lpVtbl = &PropertyBag_Vtbl;
296 This->ref = 1;
297 This->prop_count = count;
299 if (count == 0)
301 This->properties = NULL;
302 This->values = NULL;
304 else
306 This->properties = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(PROPBAG2)*count);
307 This->values = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(VARIANT)*count);
309 if (!This->properties || !This->values)
310 res = E_OUTOFMEMORY;
311 else
312 for (i=0; i < count; i++)
314 res = copy_propbag2(This->properties+i, options+i, FALSE);
315 if (FAILED(res))
316 break;
317 This->properties[i].dwHint = i+1; /* 0 means unset, so we start with 1 */
321 if (FAILED(res))
323 PropertyBag_Release(&This->IPropertyBag2_iface);
324 *ppPropertyBag2 = NULL;
326 else
327 *ppPropertyBag2 = &This->IPropertyBag2_iface;
329 return res;