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 static UINT get_column_size( const struct table *table, UINT column )
49 if (table->columns[column].type & CIM_FLAG_ARRAY) return sizeof(void *);
51 switch (table->columns[column].type & COL_TYPE_MASK)
64 return sizeof(WCHAR *);
66 ERR("unknown column type %u\n", table->columns[column].type & COL_TYPE_MASK);
72 static UINT get_column_offset( const struct table *table, UINT column )
75 for (i = 0; i < column; i++) offset += get_column_size( table, i );
79 static UINT get_row_size( const struct table *table )
81 return get_column_offset( table, table->num_cols - 1 ) + get_column_size( table, table->num_cols - 1 );
84 HRESULT get_value( const struct table *table, UINT row, UINT column, LONGLONG *val )
86 UINT col_offset, row_size;
89 col_offset = get_column_offset( table, column );
90 row_size = get_row_size( table );
91 ptr = table->data + row * row_size + col_offset;
93 if (table->columns[column].type & CIM_FLAG_ARRAY)
95 *val = (LONGLONG)(INT_PTR)*(const void **)ptr;
98 switch (table->columns[column].type & COL_TYPE_MASK)
102 *val = (LONGLONG)(INT_PTR)*(const WCHAR **)ptr;
105 *val = *(const INT16 *)ptr;
108 *val = *(const UINT16 *)ptr;
111 *val = *(const INT32 *)ptr;
114 *val = *(const UINT32 *)ptr;
117 *val = *(const INT64 *)ptr;
120 *val = *(const UINT64 *)ptr;
123 ERR("invalid column type %u\n", table->columns[column].type & COL_TYPE_MASK);
130 BSTR get_value_bstr( const struct table *table, UINT row, UINT column )
132 static const WCHAR fmt_signedW[] = {'%','d',0};
133 static const WCHAR fmt_unsignedW[] = {'%','u',0};
134 static const WCHAR fmt_signed64W[] = {'%','I','6','4','d',0};
135 static const WCHAR fmt_unsigned64W[] = {'%','I','6','4','u',0};
136 static const WCHAR fmt_strW[] = {'\"','%','s','\"',0};
142 if (table->columns[column].type & CIM_FLAG_ARRAY)
144 FIXME("array to string conversion not handled\n");
147 if (get_value( table, row, column, &val ) != S_OK) return NULL;
149 switch (table->columns[column].type & COL_TYPE_MASK)
153 len = strlenW( (const WCHAR *)(INT_PTR)val ) + 2;
154 if (!(ret = SysAllocStringLen( NULL, len ))) return NULL;
155 sprintfW( ret, fmt_strW, (const WCHAR *)(INT_PTR)val );
160 sprintfW( number, fmt_signedW, val );
161 return SysAllocString( number );
165 sprintfW( number, fmt_unsignedW, val );
166 return SysAllocString( number );
169 wsprintfW( number, fmt_signed64W, val );
170 return SysAllocString( number );
173 wsprintfW( number, fmt_unsigned64W, val );
174 return SysAllocString( number );
177 FIXME("unhandled column type %u\n", table->columns[column].type & COL_TYPE_MASK);
183 static void clear_table( struct table *table )
188 if (!table->data) return;
190 for (i = 0; i < table->num_rows; i++)
192 for (j = 0; j < table->num_cols; j++)
194 if (!(table->columns[j].type & COL_FLAG_DYNAMIC)) continue;
196 type = table->columns[j].type & COL_TYPE_MASK;
197 if (type == CIM_STRING || type == CIM_DATETIME || (type & CIM_FLAG_ARRAY))
199 if (get_value( table, i, j, &val ) == S_OK) heap_free( (void *)(INT_PTR)val );
206 heap_free( table->data );
211 void free_columns( struct column *columns, UINT num_cols )
215 for (i = 0; i < num_cols; i++)
217 heap_free( (WCHAR *)columns[i].name );
219 heap_free( columns );
222 void free_table( struct table *table )
226 clear_table( table );
227 if (table->flags & TABLE_FLAG_DYNAMIC)
229 heap_free( (WCHAR *)table->name );
230 free_columns( (struct column *)table->columns, table->num_cols );
235 struct table *get_table( const WCHAR *name )
239 LIST_FOR_EACH_ENTRY( table, table_list, struct table, entry )
241 if (!strcmpiW( table->name, name ))
243 if (table->fill && !table->data) table->fill( table );
250 struct table *create_table( const WCHAR *name, UINT num_cols, const struct column *columns,
251 UINT num_rows, BYTE *data, void (*fill)(struct table *) )
255 if (!(table = heap_alloc( sizeof(*table) ))) return NULL;
257 table->num_cols = num_cols;
258 table->columns = columns;
259 table->num_rows = num_rows;
262 table->flags = TABLE_FLAG_DYNAMIC;
266 BOOL add_table( struct table *table )
270 LIST_FOR_EACH_ENTRY( iter, table_list, struct table, entry )
272 if (!strcmpiW( iter->name, table->name ))
274 TRACE("table %s already exists\n", debugstr_w(table->name));
278 list_add_tail( table_list, &table->entry );