Rewrite cookies to use shared list.h list code.
[wine] / dlls / quartz / enumregfilters.c
1 /*
2  * Implementation of IEnumRegFilters Interface
3  *
4  * Copyright 2004 Christian Costa
5  *
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.
10  *
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.
15  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "quartz_private.h"
22
23 #include "wine/unicode.h"
24
25 #include "wine/debug.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
28
29 typedef struct IEnumRegFiltersImpl
30 {
31     const IEnumRegFiltersVtbl * lpVtbl;
32     ULONG refCount;
33     ULONG size;
34     REGFILTER* RegFilters;
35     ULONG uIndex;
36 } IEnumRegFiltersImpl;
37
38 static const struct IEnumRegFiltersVtbl IEnumRegFiltersImpl_Vtbl;
39
40 HRESULT IEnumRegFiltersImpl_Construct(REGFILTER* pInRegFilters, const ULONG size, IEnumRegFilters ** ppEnum)
41 {
42     IEnumRegFiltersImpl* pEnumRegFilters;
43     REGFILTER* pRegFilters = NULL;
44     int i;
45
46     TRACE("(%p, %ld, %p)\n", pInRegFilters, size, ppEnum);
47
48     pEnumRegFilters = CoTaskMemAlloc(sizeof(IEnumRegFiltersImpl));
49     if (!pEnumRegFilters)
50     {
51         *ppEnum = NULL;
52         return E_OUTOFMEMORY;
53     }
54
55     /* Accept size of 0 */
56     if (size)
57     {
58         pRegFilters = CoTaskMemAlloc(sizeof(REGFILTER)*size);
59         if (!pRegFilters)
60         {
61             CoTaskMemFree(pEnumRegFilters);
62             *ppEnum = NULL;
63            return E_OUTOFMEMORY;
64         }
65     }
66
67     for(i = 0; i < size; i++)
68     {
69         pRegFilters[i].Clsid = pInRegFilters[i].Clsid;
70         pRegFilters[i].Name = (WCHAR*)CoTaskMemAlloc((strlenW(pInRegFilters[i].Name)+1)*sizeof(WCHAR));
71         if (!pRegFilters[i].Name)
72         {
73             while(i)
74                 CoTaskMemFree(pRegFilters[--i].Name);
75             CoTaskMemFree(pRegFilters);
76             CoTaskMemFree(pEnumRegFilters);
77             return E_OUTOFMEMORY;
78         }
79         CopyMemory(pRegFilters[i].Name, pInRegFilters[i].Name, (strlenW(pInRegFilters[i].Name)+1)*sizeof(WCHAR));
80     }
81
82     pEnumRegFilters->lpVtbl = &IEnumRegFiltersImpl_Vtbl;
83     pEnumRegFilters->refCount = 1;
84     pEnumRegFilters->uIndex = 0;
85     pEnumRegFilters->RegFilters = pRegFilters;
86     pEnumRegFilters->size = size;
87
88     *ppEnum = (IEnumRegFilters *)(&pEnumRegFilters->lpVtbl);
89
90     return S_OK;
91 }
92
93 static HRESULT WINAPI IEnumRegFiltersImpl_QueryInterface(IEnumRegFilters * iface, REFIID riid, LPVOID * ppv)
94 {
95     TRACE("(%p)->(%s, %p)\n", iface, qzdebugstr_guid(riid), ppv);
96
97     *ppv = NULL;
98
99     if (IsEqualIID(riid, &IID_IUnknown))
100         *ppv = (LPVOID)iface;
101     else if (IsEqualIID(riid, &IID_IEnumRegFilters))
102         *ppv = (LPVOID)iface;
103
104     if (*ppv)
105     {
106         IUnknown_AddRef((IUnknown *)(*ppv));
107         return S_OK;
108     }
109
110     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
111
112     return E_NOINTERFACE;
113 }
114
115 static ULONG WINAPI IEnumRegFiltersImpl_AddRef(IEnumRegFilters * iface)
116 {
117     ICOM_THIS(IEnumRegFiltersImpl, iface);
118
119     TRACE("(%p)\n", iface);
120
121     return ++This->refCount;
122 }
123
124 static ULONG WINAPI IEnumRegFiltersImpl_Release(IEnumRegFilters * iface)
125 {
126     ICOM_THIS(IEnumRegFiltersImpl, iface);
127
128     TRACE("(%p)\n", iface);
129
130     if (!--This->refCount)
131     {
132         CoTaskMemFree(This);
133         return 0;
134     } else
135         return This->refCount;
136 }
137
138 static HRESULT WINAPI IEnumRegFiltersImpl_Next(IEnumRegFilters * iface, ULONG cFilters, REGFILTER ** ppRegFilter, ULONG * pcFetched)
139 {
140     ULONG cFetched; 
141     ICOM_THIS(IEnumRegFiltersImpl, iface);
142     int i;
143
144     cFetched = min(This->size, This->uIndex + cFilters) - This->uIndex;
145
146     TRACE("(%p)->(%lu, %p, %p)\n", iface, cFilters, ppRegFilter, pcFetched);
147
148     if (cFetched > 0)
149     {
150         for(i = 0; i < cFetched; i++)
151         {
152             /* The string in the REGFILTER structure must be allocated in the same block as the REGFILTER structure itself */
153             ppRegFilter[i] = (REGFILTER*)CoTaskMemAlloc(sizeof(REGFILTER)+(strlenW(This->RegFilters[i].Name)+1)*sizeof(WCHAR));
154             if (!ppRegFilter[i])
155             {
156                 while(i)
157                 {
158                     CoTaskMemFree(ppRegFilter[--i]);
159                     ppRegFilter[i] = NULL;
160                 }
161                 return E_OUTOFMEMORY;
162         }
163             ppRegFilter[i]->Clsid = This->RegFilters[i].Clsid;
164             ppRegFilter[i]->Name = (WCHAR*)((char*)ppRegFilter[i]+sizeof(REGFILTER));
165             CopyMemory(ppRegFilter[i]->Name, This->RegFilters[i].Name, (strlenW(This->RegFilters[i].Name)+1)*sizeof(WCHAR));
166         }
167
168         This->uIndex += cFetched;
169         if (pcFetched)
170             *pcFetched = cFetched;
171         return S_OK;
172     }
173
174     return S_FALSE;
175 }
176
177 static HRESULT WINAPI IEnumRegFiltersImpl_Skip(IEnumRegFilters * iface, ULONG n)
178 {
179     TRACE("(%p)->(%lu)\n", iface, n);
180
181     return E_NOTIMPL;
182 }
183
184 static HRESULT WINAPI IEnumRegFiltersImpl_Reset(IEnumRegFilters * iface)
185 {
186     ICOM_THIS(IEnumRegFiltersImpl, iface);
187
188     TRACE("(%p)\n", iface);
189
190     This->uIndex = 0;
191     return S_OK;
192 }
193
194 static HRESULT WINAPI IEnumRegFiltersImpl_Clone(IEnumRegFilters * iface, IEnumRegFilters ** ppEnum)
195 {
196     TRACE("(%p)->(%p)\n", iface, ppEnum);
197
198     return E_NOTIMPL;
199 }
200
201 static const IEnumRegFiltersVtbl IEnumRegFiltersImpl_Vtbl =
202 {
203     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
204     IEnumRegFiltersImpl_QueryInterface,
205     IEnumRegFiltersImpl_AddRef,
206     IEnumRegFiltersImpl_Release,
207     IEnumRegFiltersImpl_Next,
208     IEnumRegFiltersImpl_Skip,
209     IEnumRegFiltersImpl_Reset,
210     IEnumRegFiltersImpl_Clone
211 };