ntdll: Reimplement bsearch to avoid redundant and possibly out of bounds comparisons.
[wine/hramrach.git] / dlls / quartz / enummedia.c
blob60ff13a7b68808fd60fdc9f6f9d17bdc05463c66
1 /*
2 * Implementation of IEnumMediaTypes Interface
4 * Copyright 2003 Robert Shearman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "quartz_private.h"
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
27 HRESULT CopyMediaType(AM_MEDIA_TYPE * pDest, const AM_MEDIA_TYPE *pSrc)
29 *pDest = *pSrc;
30 if (!pSrc->pbFormat) return S_OK;
31 if (!(pDest->pbFormat = CoTaskMemAlloc(pSrc->cbFormat)))
32 return E_OUTOFMEMORY;
33 memcpy(pDest->pbFormat, pSrc->pbFormat, pSrc->cbFormat);
34 if (pDest->pUnk)
35 IUnknown_AddRef(pDest->pUnk);
36 return S_OK;
39 void FreeMediaType(AM_MEDIA_TYPE * pMediaType)
41 if (pMediaType->pbFormat)
43 CoTaskMemFree(pMediaType->pbFormat);
44 pMediaType->pbFormat = NULL;
46 if (pMediaType->pUnk)
48 IUnknown_Release(pMediaType->pUnk);
49 pMediaType->pUnk = NULL;
53 static AM_MEDIA_TYPE * CreateMediaType(AM_MEDIA_TYPE const * pSrc)
55 AM_MEDIA_TYPE * pDest;
57 pDest = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
58 if (!pDest)
59 return NULL;
61 if (FAILED(CopyMediaType(pDest, pSrc)))
63 CoTaskMemFree(pDest);
64 return NULL;
67 return pDest;
70 void DeleteMediaType(AM_MEDIA_TYPE * pMediaType)
72 FreeMediaType(pMediaType);
73 CoTaskMemFree(pMediaType);
76 BOOL CompareMediaTypes(const AM_MEDIA_TYPE * pmt1, const AM_MEDIA_TYPE * pmt2, BOOL bWildcards)
78 TRACE("pmt1: ");
79 dump_AM_MEDIA_TYPE(pmt1);
80 TRACE("pmt2: ");
81 dump_AM_MEDIA_TYPE(pmt2);
82 return (((bWildcards && (IsEqualGUID(&pmt1->majortype, &GUID_NULL) || IsEqualGUID(&pmt2->majortype, &GUID_NULL))) || IsEqualGUID(&pmt1->majortype, &pmt2->majortype)) &&
83 ((bWildcards && (IsEqualGUID(&pmt1->subtype, &GUID_NULL) || IsEqualGUID(&pmt2->subtype, &GUID_NULL))) || IsEqualGUID(&pmt1->subtype, &pmt2->subtype)));
86 void dump_AM_MEDIA_TYPE(const AM_MEDIA_TYPE * pmt)
88 if (!pmt)
89 return;
90 TRACE("\t%s\n\t%s\n\t...\n\t%s\n", qzdebugstr_guid(&pmt->majortype), qzdebugstr_guid(&pmt->subtype), qzdebugstr_guid(&pmt->formattype));
93 typedef struct IEnumMediaTypesImpl
95 const IEnumMediaTypesVtbl * lpVtbl;
96 LONG refCount;
97 ENUMMEDIADETAILS enumMediaDetails;
98 ULONG uIndex;
99 } IEnumMediaTypesImpl;
101 static const struct IEnumMediaTypesVtbl IEnumMediaTypesImpl_Vtbl;
103 HRESULT IEnumMediaTypesImpl_Construct(const ENUMMEDIADETAILS * pDetails, IEnumMediaTypes ** ppEnum)
105 ULONG i;
106 IEnumMediaTypesImpl * pEnumMediaTypes = CoTaskMemAlloc(sizeof(IEnumMediaTypesImpl));
108 if (!pEnumMediaTypes)
110 *ppEnum = NULL;
111 return E_OUTOFMEMORY;
113 pEnumMediaTypes->lpVtbl = &IEnumMediaTypesImpl_Vtbl;
114 pEnumMediaTypes->refCount = 1;
115 pEnumMediaTypes->uIndex = 0;
116 pEnumMediaTypes->enumMediaDetails.cMediaTypes = pDetails->cMediaTypes;
117 pEnumMediaTypes->enumMediaDetails.pMediaTypes = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE) * pDetails->cMediaTypes);
118 for (i = 0; i < pDetails->cMediaTypes; i++)
119 if (FAILED(CopyMediaType(&pEnumMediaTypes->enumMediaDetails.pMediaTypes[i], &pDetails->pMediaTypes[i])))
121 while (i--)
122 CoTaskMemFree(pEnumMediaTypes->enumMediaDetails.pMediaTypes[i].pbFormat);
123 CoTaskMemFree(pEnumMediaTypes->enumMediaDetails.pMediaTypes);
124 return E_OUTOFMEMORY;
126 *ppEnum = (IEnumMediaTypes *)(&pEnumMediaTypes->lpVtbl);
127 return S_OK;
130 static HRESULT WINAPI IEnumMediaTypesImpl_QueryInterface(IEnumMediaTypes * iface, REFIID riid, LPVOID * ppv)
132 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
134 *ppv = NULL;
136 if (IsEqualIID(riid, &IID_IUnknown))
137 *ppv = iface;
138 else if (IsEqualIID(riid, &IID_IEnumMediaTypes))
139 *ppv = iface;
141 if (*ppv)
143 IUnknown_AddRef((IUnknown *)(*ppv));
144 return S_OK;
147 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
149 return E_NOINTERFACE;
152 static ULONG WINAPI IEnumMediaTypesImpl_AddRef(IEnumMediaTypes * iface)
154 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
155 ULONG refCount = InterlockedIncrement(&This->refCount);
157 TRACE("(%p)->() AddRef from %d\n", iface, refCount - 1);
159 return refCount;
162 static ULONG WINAPI IEnumMediaTypesImpl_Release(IEnumMediaTypes * iface)
164 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
165 ULONG refCount = InterlockedDecrement(&This->refCount);
167 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
169 if (!refCount)
171 ULONG i;
172 for (i = 0; i < This->enumMediaDetails.cMediaTypes; i++)
173 if (This->enumMediaDetails.pMediaTypes[i].pbFormat)
174 CoTaskMemFree(This->enumMediaDetails.pMediaTypes[i].pbFormat);
175 CoTaskMemFree(This->enumMediaDetails.pMediaTypes);
176 CoTaskMemFree(This);
178 return refCount;
181 static HRESULT WINAPI IEnumMediaTypesImpl_Next(IEnumMediaTypes * iface, ULONG cMediaTypes, AM_MEDIA_TYPE ** ppMediaTypes, ULONG * pcFetched)
183 ULONG cFetched;
184 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
186 cFetched = min(This->enumMediaDetails.cMediaTypes, This->uIndex + cMediaTypes) - This->uIndex;
188 TRACE("(%u, %p, %p)\n", cMediaTypes, ppMediaTypes, pcFetched);
189 TRACE("Next uIndex: %u, cFetched: %u\n", This->uIndex, cFetched);
191 if (cFetched > 0)
193 ULONG i;
194 for (i = 0; i < cFetched; i++)
195 if (!(ppMediaTypes[i] = CreateMediaType(&This->enumMediaDetails.pMediaTypes[This->uIndex + i])))
197 while (i--)
198 DeleteMediaType(ppMediaTypes[i]);
199 *pcFetched = 0;
200 return E_OUTOFMEMORY;
204 if ((cMediaTypes != 1) || pcFetched)
205 *pcFetched = cFetched;
207 This->uIndex += cFetched;
209 if (cFetched != cMediaTypes)
210 return S_FALSE;
211 return S_OK;
214 static HRESULT WINAPI IEnumMediaTypesImpl_Skip(IEnumMediaTypes * iface, ULONG cMediaTypes)
216 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
218 TRACE("(%u)\n", cMediaTypes);
220 if (This->uIndex + cMediaTypes < This->enumMediaDetails.cMediaTypes)
222 This->uIndex += cMediaTypes;
223 return S_OK;
225 return S_FALSE;
228 static HRESULT WINAPI IEnumMediaTypesImpl_Reset(IEnumMediaTypes * iface)
230 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
232 TRACE("()\n");
234 This->uIndex = 0;
235 return S_OK;
238 static HRESULT WINAPI IEnumMediaTypesImpl_Clone(IEnumMediaTypes * iface, IEnumMediaTypes ** ppEnum)
240 HRESULT hr;
241 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
243 TRACE("(%p)\n", ppEnum);
245 hr = IEnumMediaTypesImpl_Construct(&This->enumMediaDetails, ppEnum);
246 if (FAILED(hr))
247 return hr;
248 return IEnumMediaTypes_Skip(*ppEnum, This->uIndex);
251 static const IEnumMediaTypesVtbl IEnumMediaTypesImpl_Vtbl =
253 IEnumMediaTypesImpl_QueryInterface,
254 IEnumMediaTypesImpl_AddRef,
255 IEnumMediaTypesImpl_Release,
256 IEnumMediaTypesImpl_Next,
257 IEnumMediaTypesImpl_Skip,
258 IEnumMediaTypesImpl_Reset,
259 IEnumMediaTypesImpl_Clone