wbemprox: Add support for parsing WQL queries.
[wine] / dlls / wbemprox / query.c
1 /*
2  * Copyright 2012 Hans Leidekker for CodeWeavers
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 #define COBJMACROS
20
21 #include "config.h"
22 #include <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wbemcli.h"
27
28 #include "wine/debug.h"
29 #include "wbemprox_private.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(wbemprox);
32
33 HRESULT create_view( const struct property *proplist, const WCHAR *class,
34                      const struct expr *cond, struct view **ret )
35 {
36     struct view *view = heap_alloc( sizeof(struct view) );
37
38     if (!view) return E_OUTOFMEMORY;
39     view->proplist = proplist;
40     view->cond     = cond;
41     *ret = view;
42     return S_OK;
43 }
44
45 void destroy_view( struct view *view )
46 {
47     heap_free( view );
48 }
49
50 static struct query *alloc_query(void)
51 {
52     struct query *query;
53
54     if (!(query = heap_alloc( sizeof(*query) ))) return NULL;
55     list_init( &query->mem );
56     return query;
57 }
58
59 void free_query( struct query *query )
60 {
61     struct list *mem, *next;
62
63     destroy_view( query->view );
64     LIST_FOR_EACH_SAFE( mem, next, &query->mem )
65     {
66         heap_free( mem );
67     }
68     heap_free( query );
69 }
70
71 HRESULT exec_query( const WCHAR *str, IEnumWbemClassObject **result )
72 {
73     HRESULT hr;
74     struct query *query;
75
76     *result = NULL;
77     if (!(query = alloc_query())) return E_OUTOFMEMORY;
78     hr = parse_query( str, &query->view, &query->mem );
79     if (hr != S_OK)
80     {
81         free_query( query );
82         return hr;
83     }
84     hr = EnumWbemClassObject_create( NULL, query, (void **)result );
85     if (hr != S_OK) free_query( query );
86     return hr;
87 }