wined3d: Don't use persistent BOs from the client thread if we might need to do verte...
[wine.git] / dlls / wbemprox / table.c
blob005c767817097c712ea4bb9148f765b5950bb504
1 /*
2 * Copyright 2012 Hans Leidekker for CodeWeavers
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.
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.
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
19 #define COBJMACROS
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wbemcli.h"
27 #include "wine/debug.h"
28 #include "wbemprox_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(wbemprox);
32 HRESULT get_column_index( const struct table *table, const WCHAR *name, UINT *column )
34 UINT i;
35 for (i = 0; i < table->num_cols; i++)
37 if (!wcsicmp( table->columns[i].name, name ))
39 *column = i;
40 return S_OK;
43 return WBEM_E_INVALID_QUERY;
46 UINT get_type_size( CIMTYPE type )
48 if (type & CIM_FLAG_ARRAY) return sizeof(void *);
50 switch (type)
52 case CIM_BOOLEAN:
53 return sizeof(int);
54 case CIM_SINT8:
55 case CIM_UINT8:
56 return sizeof(INT8);
57 case CIM_SINT16:
58 case CIM_UINT16:
59 return sizeof(INT16);
60 case CIM_SINT32:
61 case CIM_UINT32:
62 return sizeof(INT32);
63 case CIM_SINT64:
64 case CIM_UINT64:
65 return sizeof(INT64);
66 case CIM_DATETIME:
67 case CIM_REFERENCE:
68 case CIM_STRING:
69 return sizeof(WCHAR *);
70 case CIM_REAL32:
71 return sizeof(FLOAT);
72 default:
73 ERR( "unhandled type %lu\n", type );
74 break;
76 return sizeof(LONGLONG);
79 static UINT get_column_size( const struct table *table, UINT column )
81 return get_type_size( table->columns[column].type & COL_TYPE_MASK );
84 static UINT get_column_offset( const struct table *table, UINT column )
86 UINT i, offset = 0;
87 for (i = 0; i < column; i++) offset += get_column_size( table, i );
88 return offset;
91 static UINT get_row_size( const struct table *table )
93 return get_column_offset( table, table->num_cols - 1 ) + get_column_size( table, table->num_cols - 1 );
96 HRESULT get_value( const struct table *table, UINT row, UINT column, LONGLONG *val )
98 UINT col_offset, row_size;
99 const BYTE *ptr;
101 col_offset = get_column_offset( table, column );
102 row_size = get_row_size( table );
103 ptr = table->data + row * row_size + col_offset;
105 if (table->columns[column].type & CIM_FLAG_ARRAY)
107 *val = (INT_PTR)*(const void **)ptr;
108 return S_OK;
110 switch (table->columns[column].type & COL_TYPE_MASK)
112 case CIM_BOOLEAN:
113 *val = *(const int *)ptr;
114 break;
115 case CIM_DATETIME:
116 case CIM_REFERENCE:
117 case CIM_STRING:
118 *val = (INT_PTR)*(const WCHAR **)ptr;
119 break;
120 case CIM_SINT8:
121 *val = *(const INT8 *)ptr;
122 break;
123 case CIM_UINT8:
124 *val = *(const UINT8 *)ptr;
125 break;
126 case CIM_SINT16:
127 *val = *(const INT16 *)ptr;
128 break;
129 case CIM_UINT16:
130 *val = *(const UINT16 *)ptr;
131 break;
132 case CIM_SINT32:
133 *val = *(const INT32 *)ptr;
134 break;
135 case CIM_UINT32:
136 *val = *(const UINT32 *)ptr;
137 break;
138 case CIM_SINT64:
139 *val = *(const INT64 *)ptr;
140 break;
141 case CIM_UINT64:
142 *val = *(const UINT64 *)ptr;
143 break;
144 case CIM_REAL32:
145 memcpy( val, ptr, sizeof(FLOAT) );
146 break;
147 default:
148 ERR("invalid column type %u\n", table->columns[column].type & COL_TYPE_MASK);
149 *val = 0;
150 break;
152 return S_OK;
155 BSTR get_value_bstr( const struct table *table, UINT row, UINT column )
157 LONGLONG val;
158 BSTR ret;
159 WCHAR number[22];
160 UINT len;
162 if (table->columns[column].type & CIM_FLAG_ARRAY)
164 FIXME("array to string conversion not handled\n");
165 return NULL;
167 if (get_value( table, row, column, &val ) != S_OK) return NULL;
169 switch (table->columns[column].type & COL_TYPE_MASK)
171 case CIM_BOOLEAN:
172 if (val) return SysAllocString( L"TRUE" );
173 else return SysAllocString( L"FALSE" );
175 case CIM_DATETIME:
176 case CIM_REFERENCE:
177 case CIM_STRING:
178 if (!val) return NULL;
179 len = lstrlenW( (const WCHAR *)(INT_PTR)val ) + 2;
180 if (!(ret = SysAllocStringLen( NULL, len ))) return NULL;
181 swprintf( ret, len, L"\"%s\"", (const WCHAR *)(INT_PTR)val );
182 return ret;
184 case CIM_SINT16:
185 case CIM_SINT32:
186 swprintf( number, ARRAY_SIZE( number ), L"%d", val );
187 return SysAllocString( number );
189 case CIM_UINT16:
190 case CIM_UINT32:
191 swprintf( number, ARRAY_SIZE( number ), L"%u", val );
192 return SysAllocString( number );
194 case CIM_SINT64:
195 wsprintfW( number, L"%I64d", val );
196 return SysAllocString( number );
198 case CIM_UINT64:
199 wsprintfW( number, L"%I64u", val );
200 return SysAllocString( number );
202 default:
203 FIXME("unhandled column type %u\n", table->columns[column].type & COL_TYPE_MASK);
204 break;
206 return NULL;
209 HRESULT set_value( const struct table *table, UINT row, UINT column, LONGLONG val,
210 CIMTYPE type )
212 UINT col_offset, row_size;
213 BYTE *ptr;
215 if ((table->columns[column].type & COL_TYPE_MASK) != type) return WBEM_E_TYPE_MISMATCH;
217 col_offset = get_column_offset( table, column );
218 row_size = get_row_size( table );
219 ptr = table->data + row * row_size + col_offset;
221 switch (table->columns[column].type & COL_TYPE_MASK)
223 case CIM_DATETIME:
224 case CIM_REFERENCE:
225 case CIM_STRING:
226 *(WCHAR **)ptr = (WCHAR *)(INT_PTR)val;
227 break;
228 case CIM_SINT8:
229 *(INT8 *)ptr = val;
230 break;
231 case CIM_UINT8:
232 *(UINT8 *)ptr = val;
233 break;
234 case CIM_SINT16:
235 *(INT16 *)ptr = val;
236 break;
237 case CIM_UINT16:
238 *(UINT16 *)ptr = val;
239 break;
240 case CIM_SINT32:
241 *(INT32 *)ptr = val;
242 break;
243 case CIM_UINT32:
244 *(UINT32 *)ptr = val;
245 break;
246 case CIM_SINT64:
247 *(INT64 *)ptr = val;
248 break;
249 case CIM_UINT64:
250 *(UINT64 *)ptr = val;
251 break;
252 default:
253 FIXME( "unhandled column type %lu\n", type );
254 return WBEM_E_FAILED;
256 return S_OK;
259 HRESULT get_method( const struct table *table, const WCHAR *name, class_method **func )
261 UINT i, j;
263 for (i = 0; i < table->num_rows; i++)
265 for (j = 0; j < table->num_cols; j++)
267 if (table->columns[j].type & COL_FLAG_METHOD && !wcscmp( table->columns[j].name, name ))
269 HRESULT hr;
270 LONGLONG val;
272 if ((hr = get_value( table, i, j, &val )) != S_OK) return hr;
273 *func = (class_method *)(INT_PTR)val;
274 return S_OK;
278 return WBEM_E_INVALID_METHOD;
282 void free_row_values( const struct table *table, UINT row )
284 UINT i, type;
285 LONGLONG val;
287 for (i = 0; i < table->num_cols; i++)
289 if (!(table->columns[i].type & COL_FLAG_DYNAMIC)) continue;
291 type = table->columns[i].type & COL_TYPE_MASK;
292 if (type == CIM_STRING || type == CIM_DATETIME || type == CIM_REFERENCE)
294 if (get_value( table, row, i, &val ) == S_OK) free( (void *)(INT_PTR)val );
296 else if (type & CIM_FLAG_ARRAY)
298 if (get_value( table, row, i, &val ) == S_OK)
299 destroy_array( (void *)(INT_PTR)val, type & CIM_TYPE_MASK );
304 void clear_table( struct table *table )
306 UINT i;
308 if (!table->data) return;
310 for (i = 0; i < table->num_rows; i++) free_row_values( table, i );
311 if (table->fill)
313 table->num_rows = 0;
314 table->num_rows_allocated = 0;
315 free( table->data );
316 table->data = NULL;
320 void free_columns( struct column *columns, UINT num_cols )
322 UINT i;
324 for (i = 0; i < num_cols; i++) { free( (WCHAR *)columns[i].name ); }
325 free( columns );
328 void free_table( struct table *table )
330 if (!table) return;
332 clear_table( table );
333 if (table->flags & TABLE_FLAG_DYNAMIC)
335 TRACE("destroying %p\n", table);
336 free( (WCHAR *)table->name );
337 free_columns( (struct column *)table->columns, table->num_cols );
338 free( table->data );
339 list_remove( &table->entry );
340 free( table );
344 void release_table( struct table *table )
346 if (!InterlockedDecrement( &table->refs )) free_table( table );
349 struct table *addref_table( struct table *table )
351 InterlockedIncrement( &table->refs );
352 return table;
355 struct table *grab_table( enum wbm_namespace ns, const WCHAR *name )
357 struct table *table;
359 if (ns == WBEMPROX_NAMESPACE_LAST) return NULL;
361 LIST_FOR_EACH_ENTRY( table, table_list[ns], struct table, entry )
363 if (name && !wcsicmp( table->name, name ))
365 TRACE("returning %p\n", table);
366 return addref_table( table );
369 return NULL;
372 struct table *create_table( const WCHAR *name, UINT num_cols, const struct column *columns,
373 UINT num_rows, UINT num_allocated, BYTE *data,
374 enum fill_status (*fill)(struct table *, const struct expr *cond) )
376 struct table *table;
378 if (!(table = malloc( sizeof(*table) ))) return NULL;
379 table->name = heap_strdupW( name );
380 table->num_cols = num_cols;
381 table->columns = columns;
382 table->num_rows = num_rows;
383 table->num_rows_allocated = num_allocated;
384 table->data = data;
385 table->fill = fill;
386 table->flags = TABLE_FLAG_DYNAMIC;
387 table->refs = 0;
388 list_init( &table->entry );
389 return table;
392 BOOL add_table( enum wbm_namespace ns, struct table *table )
394 struct table *iter;
396 if (ns == WBEMPROX_NAMESPACE_LAST) return FALSE;
398 LIST_FOR_EACH_ENTRY( iter, table_list[ns], struct table, entry )
400 if (!wcsicmp( iter->name, table->name ))
402 TRACE("table %s already exists\n", debugstr_w(table->name));
403 return FALSE;
406 list_add_tail( table_list[ns], &table->entry );
407 TRACE("added %p\n", table);
408 return TRUE;
411 BSTR get_method_name( enum wbm_namespace ns, const WCHAR *class, UINT index )
413 struct table *table;
414 UINT i, count = 0;
415 BSTR ret;
417 if (!(table = grab_table( ns, class ))) return NULL;
419 for (i = 0; i < table->num_cols; i++)
421 if (table->columns[i].type & COL_FLAG_METHOD)
423 if (index == count)
425 ret = SysAllocString( table->columns[i].name );
426 release_table( table );
427 return ret;
429 count++;
432 release_table( table );
433 return NULL;