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
28 #include "wine/debug.h"
29 #include "wbemprox_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(wbemprox);
33 HRESULT get_column_index( const struct table *table, const WCHAR *name, UINT *column )
36 for (i = 0; i < table->num_cols; i++)
38 if (!strcmpiW( table->columns[i].name, name ))
44 return WBEM_E_INVALID_QUERY;
47 UINT get_type_size( CIMTYPE type )
49 if (type & CIM_FLAG_ARRAY) return sizeof(void *);
64 return sizeof(WCHAR *);
66 ERR("unhandled type %u\n", type);
69 return sizeof(LONGLONG);
72 static UINT get_column_size( const struct table *table, UINT column )
74 return get_type_size( table->columns[column].type & COL_TYPE_MASK );
77 static UINT get_column_offset( const struct table *table, UINT column )
80 for (i = 0; i < column; i++) offset += get_column_size( table, i );
84 static UINT get_row_size( const struct table *table )
86 return get_column_offset( table, table->num_cols - 1 ) + get_column_size( table, table->num_cols - 1 );
89 HRESULT get_value( const struct table *table, UINT row, UINT column, LONGLONG *val )
91 UINT col_offset, row_size;
94 col_offset = get_column_offset( table, column );
95 row_size = get_row_size( table );
96 ptr = table->data + row * row_size + col_offset;
98 if (table->columns[column].type & CIM_FLAG_ARRAY)
100 *val = (LONGLONG)(INT_PTR)*(const void **)ptr;
103 switch (table->columns[column].type & COL_TYPE_MASK)
107 *val = (LONGLONG)(INT_PTR)*(const WCHAR **)ptr;
110 *val = *(const INT16 *)ptr;
113 *val = *(const UINT16 *)ptr;
116 *val = *(const INT32 *)ptr;
119 *val = *(const UINT32 *)ptr;
122 *val = *(const INT64 *)ptr;
125 *val = *(const UINT64 *)ptr;
128 ERR("invalid column type %u\n", table->columns[column].type & COL_TYPE_MASK);
135 BSTR get_value_bstr( const struct table *table, UINT row, UINT column )
137 static const WCHAR fmt_signedW[] = {'%','d',0};
138 static const WCHAR fmt_unsignedW[] = {'%','u',0};
139 static const WCHAR fmt_signed64W[] = {'%','I','6','4','d',0};
140 static const WCHAR fmt_unsigned64W[] = {'%','I','6','4','u',0};
141 static const WCHAR fmt_strW[] = {'\"','%','s','\"',0};
147 if (table->columns[column].type & CIM_FLAG_ARRAY)
149 FIXME("array to string conversion not handled\n");
152 if (get_value( table, row, column, &val ) != S_OK) return NULL;
154 switch (table->columns[column].type & COL_TYPE_MASK)
158 len = strlenW( (const WCHAR *)(INT_PTR)val ) + 2;
159 if (!(ret = SysAllocStringLen( NULL, len ))) return NULL;
160 sprintfW( ret, fmt_strW, (const WCHAR *)(INT_PTR)val );
165 sprintfW( number, fmt_signedW, val );
166 return SysAllocString( number );
170 sprintfW( number, fmt_unsignedW, val );
171 return SysAllocString( number );
174 wsprintfW( number, fmt_signed64W, val );
175 return SysAllocString( number );
178 wsprintfW( number, fmt_unsigned64W, val );
179 return SysAllocString( number );
182 FIXME("unhandled column type %u\n", table->columns[column].type & COL_TYPE_MASK);
188 HRESULT set_value( const struct table *table, UINT row, UINT column, LONGLONG val,
191 UINT col_offset, row_size;
194 if ((table->columns[column].type & COL_TYPE_MASK) != type) return WBEM_E_TYPE_MISMATCH;
196 col_offset = get_column_offset( table, column );
197 row_size = get_row_size( table );
198 ptr = table->data + row * row_size + col_offset;
200 switch (table->columns[column].type & COL_TYPE_MASK)
204 *(WCHAR **)ptr = (WCHAR *)(INT_PTR)val;
210 *(UINT16 *)ptr = val;
216 *(UINT32 *)ptr = val;
222 *(UINT64 *)ptr = val;
225 FIXME("unhandled column type %u\n", type);
226 return WBEM_E_FAILED;
231 static void clear_table( struct table *table )
236 if (!table->data) return;
238 for (i = 0; i < table->num_rows; i++)
240 for (j = 0; j < table->num_cols; j++)
242 if (!(table->columns[j].type & COL_FLAG_DYNAMIC)) continue;
244 type = table->columns[j].type & COL_TYPE_MASK;
245 if (type == CIM_STRING || type == CIM_DATETIME || (type & CIM_FLAG_ARRAY))
247 if (get_value( table, i, j, &val ) == S_OK) heap_free( (void *)(INT_PTR)val );
254 heap_free( table->data );
259 void free_columns( struct column *columns, UINT num_cols )
263 for (i = 0; i < num_cols; i++)
265 heap_free( (WCHAR *)columns[i].name );
267 heap_free( columns );
270 void free_table( struct table *table )
274 clear_table( table );
275 if (table->flags & TABLE_FLAG_DYNAMIC)
277 heap_free( (WCHAR *)table->name );
278 free_columns( (struct column *)table->columns, table->num_cols );
283 struct table *get_table( const WCHAR *name )
287 LIST_FOR_EACH_ENTRY( table, table_list, struct table, entry )
289 if (!strcmpiW( table->name, name ))
291 if (table->fill && !table->data) table->fill( table );
298 struct table *create_table( const WCHAR *name, UINT num_cols, const struct column *columns,
299 UINT num_rows, BYTE *data, void (*fill)(struct table *) )
303 if (!(table = heap_alloc( sizeof(*table) ))) return NULL;
305 table->num_cols = num_cols;
306 table->columns = columns;
307 table->num_rows = num_rows;
310 table->flags = TABLE_FLAG_DYNAMIC;
314 BOOL add_table( struct table *table )
318 LIST_FOR_EACH_ENTRY( iter, table_list, struct table, entry )
320 if (!strcmpiW( iter->name, table->name ))
322 TRACE("table %s already exists\n", debugstr_w(table->name));
326 list_add_tail( table_list, &table->entry );