dxdiagn vendor import
[reactos.git] / reactos / lib / dxdiagn / container.c
1 /*
2 * IDxDiagContainer Implementation
3 *
4 * Copyright 2004 Raphael Junqueira
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
22 #include "config.h"
23 #include "dxdiag_private.h"
24 #include "wine/debug.h"
25 #include "wine/unicode.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(dxdiag);
28
29 /* IDxDiagContainer IUnknown parts follow: */
30 HRESULT WINAPI IDxDiagContainerImpl_QueryInterface(PDXDIAGCONTAINER iface, REFIID riid, LPVOID *ppobj)
31 {
32 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
33
34 if (IsEqualGUID(riid, &IID_IUnknown)
35 || IsEqualGUID(riid, &IID_IDxDiagContainer)) {
36 IDxDiagContainerImpl_AddRef(iface);
37 *ppobj = This;
38 return S_OK;
39 }
40
41 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
42 return E_NOINTERFACE;
43 }
44
45 ULONG WINAPI IDxDiagContainerImpl_AddRef(PDXDIAGCONTAINER iface) {
46 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
47 ULONG refCount = InterlockedIncrement(&This->ref);
48
49 TRACE("(%p)->(ref before=%lu)\n", This, refCount - 1);
50
51 DXDIAGN_LockModule();
52
53 return refCount;
54 }
55
56 ULONG WINAPI IDxDiagContainerImpl_Release(PDXDIAGCONTAINER iface) {
57 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
58 ULONG refCount = InterlockedDecrement(&This->ref);
59
60 TRACE("(%p)->(ref before=%lu)\n", This, refCount + 1);
61
62 if (!refCount) {
63 HeapFree(GetProcessHeap(), 0, This);
64 }
65
66 DXDIAGN_UnlockModule();
67
68 return refCount;
69 }
70
71 /* IDxDiagContainer Interface follow: */
72 HRESULT WINAPI IDxDiagContainerImpl_GetNumberOfChildContainers(PDXDIAGCONTAINER iface, DWORD* pdwCount) {
73 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
74 TRACE("(%p)\n", iface);
75 if (NULL == pdwCount) {
76 return E_INVALIDARG;
77 }
78 *pdwCount = This->nSubContainers;
79 return S_OK;
80 }
81
82 HRESULT WINAPI IDxDiagContainerImpl_EnumChildContainerNames(PDXDIAGCONTAINER iface, DWORD dwIndex, LPWSTR pwszContainer, DWORD cchContainer) {
83 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
84 IDxDiagContainerImpl_SubContainer* p = NULL;
85 DWORD i = 0;
86
87 TRACE("(%p, %lu, %s, %lu)\n", iface, dwIndex, debugstr_w(pwszContainer), cchContainer);
88
89 if (NULL == pwszContainer) {
90 return E_INVALIDARG;
91 }
92 if (256 > cchContainer) {
93 return DXDIAG_E_INSUFFICIENT_BUFFER;
94 }
95
96 p = This->subContainers;
97 while (NULL != p) {
98 if (dwIndex == i) {
99 if (cchContainer <= strlenW(p->contName)) {
100 return DXDIAG_E_INSUFFICIENT_BUFFER;
101 }
102 lstrcpynW(pwszContainer, p->contName, cchContainer);
103 return S_OK;
104 }
105 p = p->next;
106 ++i;
107 }
108 return E_INVALIDARG;
109 }
110
111 HRESULT WINAPI IDxDiagContainerImpl_GetChildContainerInternal(PDXDIAGCONTAINER iface, LPCWSTR pwszContainer, IDxDiagContainer** ppInstance) {
112 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
113 IDxDiagContainerImpl_SubContainer* p = NULL;
114
115 p = This->subContainers;
116 while (NULL != p) {
117 if (0 == lstrcmpW(p->contName, pwszContainer)) {
118 *ppInstance = (PDXDIAGCONTAINER)p->pCont;
119 return S_OK;
120 }
121 p = p->next;
122 }
123 return E_INVALIDARG;
124 }
125
126 HRESULT WINAPI IDxDiagContainerImpl_GetChildContainer(PDXDIAGCONTAINER iface, LPCWSTR pwszContainer, IDxDiagContainer** ppInstance) {
127 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
128 IDxDiagContainer* pContainer = NULL;
129 LPWSTR tmp, orig_tmp;
130 INT tmp_len;
131 WCHAR* cur;
132 HRESULT hr = E_INVALIDARG;
133
134 FIXME("(%p, %s, %p)\n", iface, debugstr_w(pwszContainer), ppInstance);
135
136 if (NULL == ppInstance || NULL == pwszContainer) {
137 return E_INVALIDARG;
138 }
139
140 pContainer = (PDXDIAGCONTAINER) This;
141
142 tmp_len = strlenW(pwszContainer) + 1;
143 orig_tmp = tmp = HeapAlloc(GetProcessHeap(), 0, tmp_len * sizeof(WCHAR));
144 if (NULL == tmp) return E_FAIL;
145 lstrcpynW(tmp, pwszContainer, tmp_len);
146
147 cur = strchrW(tmp, '.');
148 while (NULL != cur) {
149 *cur = '\0'; /* cut tmp string to '.' */
150 hr = IDxDiagContainerImpl_GetChildContainerInternal(pContainer, tmp, &pContainer);
151 if (!SUCCEEDED(hr) || NULL == pContainer)
152 goto on_error;
153 *cur++; /* go after '.' (just replaced by \0) */
154 tmp = cur;
155 cur = strchrW(tmp, '.');
156 }
157
158 hr = IDxDiagContainerImpl_GetChildContainerInternal(pContainer, tmp, ppInstance);
159 if (SUCCEEDED(hr)) {
160 IDxDiagContainerImpl_AddRef((PDXDIAGCONTAINER)*ppInstance);
161 }
162
163 on_error:
164 HeapFree(GetProcessHeap(), 0, orig_tmp);
165 return hr;
166 }
167
168 HRESULT WINAPI IDxDiagContainerImpl_GetNumberOfProps(PDXDIAGCONTAINER iface, DWORD* pdwCount) {
169 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
170 TRACE("(%p)\n", iface);
171 if (NULL == pdwCount) {
172 return E_INVALIDARG;
173 }
174 *pdwCount = This->nProperties;
175 return S_OK;
176 }
177
178 HRESULT WINAPI IDxDiagContainerImpl_EnumPropNames(PDXDIAGCONTAINER iface, DWORD dwIndex, LPWSTR pwszPropName, DWORD cchPropName) {
179 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
180 IDxDiagContainerImpl_Property* p = NULL;
181 DWORD i = 0;
182
183 FIXME("(%p, %lu, %s, %lu)\n", iface, dwIndex, debugstr_w(pwszPropName), cchPropName);
184
185 if (NULL == pwszPropName) {
186 return E_INVALIDARG;
187 }
188 if (256 > cchPropName) {
189 return DXDIAG_E_INSUFFICIENT_BUFFER;
190 }
191
192 p = This->properties;
193 while (NULL != p) {
194 if (dwIndex == i) {
195 if (cchPropName <= lstrlenW(p->vName)) {
196 return DXDIAG_E_INSUFFICIENT_BUFFER;
197 }
198 lstrcpynW(pwszPropName, p->vName, cchPropName);
199 return S_OK;
200 }
201 p = p->next;
202 ++i;
203 }
204 return E_INVALIDARG;
205 }
206
207 HRESULT WINAPI IDxDiagContainerImpl_GetProp(PDXDIAGCONTAINER iface, LPCWSTR pwszPropName, VARIANT* pvarProp) {
208 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
209 IDxDiagContainerImpl_Property* p = NULL;
210 FIXME("(%p, %s, %p)\n", iface, debugstr_w(pwszPropName), pvarProp);
211
212 if (NULL == pvarProp || NULL == pwszPropName) {
213 return E_INVALIDARG;
214 }
215
216 p = This->properties;
217 while (NULL != p) {
218 if (0 == lstrcmpW(p->vName, pwszPropName)) {
219 VariantCopy(pvarProp, &p->v);
220 return S_OK;
221 }
222 p = p->next;
223 }
224 return S_OK;
225 }
226
227 HRESULT WINAPI IDxDiagContainerImpl_AddProp(PDXDIAGCONTAINER iface, LPCWSTR pwszPropName, VARIANT* pVarProp) {
228 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
229 IDxDiagContainerImpl_Property* p = NULL;
230 IDxDiagContainerImpl_Property* pNew = NULL;
231
232 FIXME("(%p, %s, %p)\n", iface, debugstr_w(pwszPropName), pVarProp);
233
234 if (NULL == pVarProp || NULL == pwszPropName) {
235 return E_INVALIDARG;
236 }
237
238 pNew = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDxDiagContainerImpl_Property));
239 if (NULL == pNew) {
240 return E_OUTOFMEMORY;
241 }
242 VariantInit(&pNew->v);
243 VariantCopy(&pNew->v, pVarProp);
244 pNew->vName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (lstrlenW(pwszPropName) + 1) * sizeof(WCHAR));
245 lstrcpyW(pNew->vName, pwszPropName);
246 pNew->next = NULL;
247
248 p = This->properties;
249 if (NULL == p) {
250 This->properties = pNew;
251 } else {
252 while (NULL != p->next) {
253 p = p->next;
254 }
255 p->next = pNew;
256 }
257 ++This->nProperties;
258 return S_OK;
259 }
260
261 HRESULT WINAPI IDxDiagContainerImpl_AddChildContainer(PDXDIAGCONTAINER iface, LPCWSTR pszContName, PDXDIAGCONTAINER pSubCont) {
262 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
263 IDxDiagContainerImpl_SubContainer* p = NULL;
264 IDxDiagContainerImpl_SubContainer* pNew = NULL;
265
266 FIXME("(%p, %s, %p)\n", iface, debugstr_w(pszContName), pSubCont);
267
268 if (NULL == pSubCont || NULL == pszContName) {
269 return E_INVALIDARG;
270 }
271
272 pNew = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDxDiagContainerImpl_SubContainer));
273 if (NULL == pNew) {
274 return E_OUTOFMEMORY;
275 }
276 pNew->pCont = pSubCont;
277 pNew->contName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (lstrlenW(pszContName) + 1) * sizeof(WCHAR));
278 lstrcpyW(pNew->contName, pszContName);
279 pNew->next = NULL;
280
281 p = This->subContainers;
282 if (NULL == p) {
283 This->subContainers = pNew;
284 } else {
285 while (NULL != p->next) {
286 p = p->next;
287 }
288 p->next = pNew;
289 }
290 ++This->nSubContainers;
291 return S_OK;
292 }
293
294 static const IDxDiagContainerVtbl DxDiagContainer_Vtbl =
295 {
296 IDxDiagContainerImpl_QueryInterface,
297 IDxDiagContainerImpl_AddRef,
298 IDxDiagContainerImpl_Release,
299 IDxDiagContainerImpl_GetNumberOfChildContainers,
300 IDxDiagContainerImpl_EnumChildContainerNames,
301 IDxDiagContainerImpl_GetChildContainer,
302 IDxDiagContainerImpl_GetNumberOfProps,
303 IDxDiagContainerImpl_EnumPropNames,
304 IDxDiagContainerImpl_GetProp
305 };
306
307
308 HRESULT DXDiag_CreateDXDiagContainer(REFIID riid, LPVOID *ppobj) {
309 IDxDiagContainerImpl* container;
310
311 TRACE("(%p, %p)\n", debugstr_guid(riid), ppobj);
312
313 container = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDxDiagContainerImpl));
314 if (NULL == container) {
315 *ppobj = NULL;
316 return E_OUTOFMEMORY;
317 }
318 container->lpVtbl = &DxDiagContainer_Vtbl;
319 container->ref = 0; /* will be inited with QueryInterface */
320 return IDxDiagContainerImpl_QueryInterface((PDXDIAGCONTAINER)container, riid, ppobj);
321 }