include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / dlls / wbemprox / table.c
blob37aa546ad2bb9d75d89d2b17f39bbedc183c0b10
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 <assert.h>
22 #include <stdarg.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wbemcli.h"
28 #include "wine/debug.h"
29 #include "wbemprox_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(wbemprox);
33 static CRITICAL_SECTION table_list_cs;
34 static CRITICAL_SECTION_DEBUG table_debug =
36 0, 0, &table_list_cs,
37 { &table_debug.ProcessLocksList, &table_debug.ProcessLocksList },
38 0, 0, { (DWORD_PTR)(__FILE__ ": table_list_cs") }
40 static CRITICAL_SECTION table_list_cs = { &table_debug, -1, 0, 0, 0, 0 };
42 HRESULT get_column_index( const struct table *table, const WCHAR *name, UINT *column )
44 UINT i;
45 for (i = 0; i < table->num_cols; i++)
47 if (!wcsicmp( table->columns[i].name, name ))
49 *column = i;
50 return S_OK;
53 return WBEM_E_INVALID_QUERY;
56 UINT get_type_size( CIMTYPE type )
58 if (type & CIM_FLAG_ARRAY) return sizeof(void *);
60 switch (type)
62 case CIM_BOOLEAN:
63 return sizeof(int);
64 case CIM_SINT8:
65 case CIM_UINT8:
66 return sizeof(INT8);
67 case CIM_SINT16:
68 case CIM_UINT16:
69 return sizeof(INT16);
70 case CIM_SINT32:
71 case CIM_UINT32:
72 return sizeof(INT32);
73 case CIM_SINT64:
74 case CIM_UINT64:
75 return sizeof(INT64);
76 case CIM_DATETIME:
77 case CIM_REFERENCE:
78 case CIM_STRING:
79 return sizeof(WCHAR *);
80 case CIM_REAL32:
81 return sizeof(FLOAT);
82 default:
83 ERR( "unhandled type %lu\n", type );
84 break;
86 return sizeof(LONGLONG);
89 static UINT get_column_size( const struct table *table, UINT column )
91 return get_type_size( table->columns[column].type & COL_TYPE_MASK );
94 static UINT get_column_offset( const struct table *table, UINT column )
96 UINT i, offset = 0;
97 for (i = 0; i < column; i++) offset += get_column_size( table, i );
98 return offset;
101 static UINT get_row_size( const struct table *table )
103 return get_column_offset( table, table->num_cols - 1 ) + get_column_size( table, table->num_cols - 1 );
106 HRESULT get_value( const struct table *table, UINT row, UINT column, LONGLONG *val )
108 UINT col_offset, row_size;
109 const BYTE *ptr;
111 col_offset = get_column_offset( table, column );
112 row_size = get_row_size( table );
113 ptr = table->data + row * row_size + col_offset;
115 if (table->columns[column].type & CIM_FLAG_ARRAY)
117 *val = (INT_PTR)*(const void **)ptr;
118 return S_OK;
120 switch (table->columns[column].type & COL_TYPE_MASK)
122 case CIM_BOOLEAN:
123 *val = *(const int *)ptr;
124 break;
125 case CIM_DATETIME:
126 case CIM_REFERENCE:
127 case CIM_STRING:
128 *val = (INT_PTR)*(const WCHAR **)ptr;
129 break;
130 case CIM_SINT8:
131 *val = *(const INT8 *)ptr;
132 break;
133 case CIM_UINT8:
134 *val = *(const UINT8 *)ptr;
135 break;
136 case CIM_SINT16:
137 *val = *(const INT16 *)ptr;
138 break;
139 case CIM_UINT16:
140 *val = *(const UINT16 *)ptr;
141 break;
142 case CIM_SINT32:
143 *val = *(const INT32 *)ptr;
144 break;
145 case CIM_UINT32:
146 *val = *(const UINT32 *)ptr;
147 break;
148 case CIM_SINT64:
149 *val = *(const INT64 *)ptr;
150 break;
151 case CIM_UINT64:
152 *val = *(const UINT64 *)ptr;
153 break;
154 case CIM_REAL32:
155 memcpy( val, ptr, sizeof(FLOAT) );
156 break;
157 default:
158 ERR("invalid column type %u\n", table->columns[column].type & COL_TYPE_MASK);
159 *val = 0;
160 break;
162 return S_OK;
165 BSTR get_value_bstr( const struct table *table, UINT row, UINT column )
167 LONGLONG val;
168 BSTR ret;
169 WCHAR number[22];
170 UINT len;
172 if (table->columns[column].type & CIM_FLAG_ARRAY)
174 FIXME("array to string conversion not handled\n");
175 return NULL;
177 if (get_value( table, row, column, &val ) != S_OK) return NULL;
179 switch (table->columns[column].type & COL_TYPE_MASK)
181 case CIM_BOOLEAN:
182 if (val) return SysAllocString( L"TRUE" );
183 else return SysAllocString( L"FALSE" );
185 case CIM_DATETIME:
186 case CIM_REFERENCE:
187 case CIM_STRING:
188 if (!val) return NULL;
189 len = lstrlenW( (const WCHAR *)(INT_PTR)val ) + 2;
190 if (!(ret = SysAllocStringLen( NULL, len ))) return NULL;
191 swprintf( ret, len + 1, L"\"%s\"", (const WCHAR *)(INT_PTR)val );
192 return ret;
194 case CIM_SINT16:
195 case CIM_SINT32:
196 swprintf( number, ARRAY_SIZE( number ), L"%d", val );
197 return SysAllocString( number );
199 case CIM_UINT16:
200 case CIM_UINT32:
201 swprintf( number, ARRAY_SIZE( number ), L"%u", val );
202 return SysAllocString( number );
204 case CIM_SINT64:
205 wsprintfW( number, L"%I64d", val );
206 return SysAllocString( number );
208 case CIM_UINT64:
209 wsprintfW( number, L"%I64u", val );
210 return SysAllocString( number );
212 default:
213 FIXME("unhandled column type %u\n", table->columns[column].type & COL_TYPE_MASK);
214 break;
216 return NULL;
219 HRESULT set_value( const struct table *table, UINT row, UINT column, LONGLONG val,
220 CIMTYPE type )
222 UINT col_offset, row_size;
223 BYTE *ptr;
225 if ((table->columns[column].type & COL_TYPE_MASK) != type) return WBEM_E_TYPE_MISMATCH;
227 col_offset = get_column_offset( table, column );
228 row_size = get_row_size( table );
229 ptr = table->data + row * row_size + col_offset;
231 switch (table->columns[column].type & COL_TYPE_MASK)
233 case CIM_DATETIME:
234 case CIM_REFERENCE:
235 case CIM_STRING:
236 *(WCHAR **)ptr = (WCHAR *)(INT_PTR)val;
237 break;
238 case CIM_SINT8:
239 *(INT8 *)ptr = val;
240 break;
241 case CIM_UINT8:
242 *(UINT8 *)ptr = val;
243 break;
244 case CIM_SINT16:
245 *(INT16 *)ptr = val;
246 break;
247 case CIM_UINT16:
248 *(UINT16 *)ptr = val;
249 break;
250 case CIM_SINT32:
251 *(INT32 *)ptr = val;
252 break;
253 case CIM_UINT32:
254 *(UINT32 *)ptr = val;
255 break;
256 case CIM_SINT64:
257 *(INT64 *)ptr = val;
258 break;
259 case CIM_UINT64:
260 *(UINT64 *)ptr = val;
261 break;
262 default:
263 FIXME( "unhandled column type %lu\n", type );
264 return WBEM_E_FAILED;
266 return S_OK;
269 HRESULT get_method( const struct table *table, const WCHAR *name, class_method **func )
271 UINT i, j;
273 for (i = 0; i < table->num_rows; i++)
275 for (j = 0; j < table->num_cols; j++)
277 if (table->columns[j].type & COL_FLAG_METHOD && !wcscmp( table->columns[j].name, name ))
279 HRESULT hr;
280 LONGLONG val;
282 if ((hr = get_value( table, i, j, &val )) != S_OK) return hr;
283 *func = (class_method *)(INT_PTR)val;
284 return S_OK;
288 return WBEM_E_INVALID_METHOD;
292 void free_row_values( const struct table *table, UINT row )
294 UINT i, type;
295 LONGLONG val;
297 for (i = 0; i < table->num_cols; i++)
299 if (!(table->columns[i].type & COL_FLAG_DYNAMIC)) continue;
301 type = table->columns[i].type & COL_TYPE_MASK;
302 if (type == CIM_STRING || type == CIM_DATETIME || type == CIM_REFERENCE)
304 if (get_value( table, row, i, &val ) == S_OK) free( (void *)(INT_PTR)val );
306 else if (type & CIM_FLAG_ARRAY)
308 if (get_value( table, row, i, &val ) == S_OK)
309 destroy_array( (void *)(INT_PTR)val, type & CIM_TYPE_MASK );
314 void clear_table( struct table *table )
316 UINT i;
318 if (!table->data) return;
320 for (i = 0; i < table->num_rows; i++) free_row_values( table, i );
321 if (table->fill)
323 table->num_rows = 0;
324 table->num_rows_allocated = 0;
325 free( table->data );
326 table->data = NULL;
330 void free_columns( struct column *columns, UINT num_cols )
332 UINT i;
334 for (i = 0; i < num_cols; i++) { free( (WCHAR *)columns[i].name ); }
335 free( columns );
338 void free_table( struct table *table )
340 if (!table) return;
341 assert( table->flags & TABLE_FLAG_DYNAMIC );
343 TRACE("destroying %p\n", table);
345 clear_table( table );
346 free( (WCHAR *)table->name );
347 free_columns( (struct column *)table->columns, table->num_cols );
348 free( table->data );
350 table->cs.DebugInfo->Spare[0] = 0;
351 DeleteCriticalSection( &table->cs );
352 free( table );
355 void release_table( struct table *table )
357 if (!--table->refs)
359 clear_table( table );
360 if (table->flags & TABLE_FLAG_DYNAMIC)
362 EnterCriticalSection( &table_list_cs );
363 list_remove( &table->entry );
364 table->removed = TRUE;
365 LeaveCriticalSection( &table_list_cs );
367 LeaveCriticalSection( &table->cs );
368 free_table( table );
369 return;
372 LeaveCriticalSection( &table->cs );
375 struct table *grab_table( struct table *table )
377 EnterCriticalSection( &table->cs );
378 if (table->removed)
380 LeaveCriticalSection( &table->cs );
381 return NULL;
383 table->refs++;
384 return table;
387 struct table *find_table( enum wbm_namespace ns, const WCHAR *name )
389 struct table *table;
391 if (ns == WBEMPROX_NAMESPACE_LAST) return NULL;
393 LIST_FOR_EACH_ENTRY( table, table_list[ns], struct table, entry )
395 if (name && !wcsicmp( table->name, name ))
397 TRACE("returning %p\n", table);
398 return grab_table( table );
401 return NULL;
404 struct table *create_table( const WCHAR *name, UINT num_cols, const struct column *columns,
405 UINT num_rows, UINT num_allocated, BYTE *data,
406 enum fill_status (*fill)(struct table *, const struct expr *cond) )
408 struct table *table;
410 if (!(table = malloc( sizeof(*table) ))) return NULL;
411 table->name = wcsdup( name );
412 table->num_cols = num_cols;
413 table->columns = columns;
414 table->num_rows = num_rows;
415 table->num_rows_allocated = num_allocated;
416 table->data = data;
417 table->fill = fill;
418 table->flags = TABLE_FLAG_DYNAMIC;
419 table->refs = 0;
420 table->removed = FALSE;
421 list_init( &table->entry );
422 InitializeCriticalSectionEx( &table->cs, 0, RTL_CRITICAL_SECTION_FLAG_FORCE_DEBUG_INFO );
423 table->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": table.cs");
424 return table;
427 BOOL add_table( enum wbm_namespace ns, struct table *table )
429 struct table *iter;
431 if (ns == WBEMPROX_NAMESPACE_LAST) return FALSE;
433 EnterCriticalSection( &table_list_cs );
434 LIST_FOR_EACH_ENTRY( iter, table_list[ns], struct table, entry )
436 if (!wcsicmp( iter->name, table->name ))
438 TRACE("table %s already exists\n", debugstr_w(table->name));
439 LeaveCriticalSection( &table_list_cs );
440 return FALSE;
443 list_add_tail( table_list[ns], &table->entry );
444 LeaveCriticalSection( &table_list_cs );
446 TRACE("added %p\n", table);
447 return TRUE;
450 BSTR get_method_name( enum wbm_namespace ns, const WCHAR *class, UINT index )
452 struct table *table;
453 UINT i, count = 0;
454 BSTR ret;
456 if (!(table = find_table( ns, class ))) return NULL;
458 for (i = 0; i < table->num_cols; i++)
460 if (table->columns[i].type & COL_FLAG_METHOD)
462 if (index == count)
464 ret = SysAllocString( table->columns[i].name );
465 release_table( table );
466 return ret;
468 count++;
471 release_table( table );
472 return NULL;
475 WCHAR *get_first_key_property( enum wbm_namespace ns, const WCHAR *class )
477 struct table *table;
478 WCHAR *ret = NULL;
479 UINT i;
481 if (!(table = find_table( ns, class ))) return NULL;
483 for (i = 0; i < table->num_cols; i++)
485 if (table->columns[i].type & COL_FLAG_KEY)
487 ret = wcsdup( table->columns[i].name );
488 break;
492 release_table( table );
493 return ret;