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
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
)
35 for (i
= 0; i
< table
->num_cols
; i
++)
37 if (!wcsicmp( table
->columns
[i
].name
, name
))
43 return WBEM_E_INVALID_QUERY
;
46 UINT
get_type_size( CIMTYPE type
)
48 if (type
& CIM_FLAG_ARRAY
) return sizeof(void *);
69 return sizeof(WCHAR
*);
73 ERR( "unhandled type %lu\n", type
);
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
)
87 for (i
= 0; i
< column
; i
++) offset
+= get_column_size( table
, i
);
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
;
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
;
110 switch (table
->columns
[column
].type
& COL_TYPE_MASK
)
113 *val
= *(const int *)ptr
;
118 *val
= (INT_PTR
)*(const WCHAR
**)ptr
;
121 *val
= *(const INT8
*)ptr
;
124 *val
= *(const UINT8
*)ptr
;
127 *val
= *(const INT16
*)ptr
;
130 *val
= *(const UINT16
*)ptr
;
133 *val
= *(const INT32
*)ptr
;
136 *val
= *(const UINT32
*)ptr
;
139 *val
= *(const INT64
*)ptr
;
142 *val
= *(const UINT64
*)ptr
;
145 memcpy( val
, ptr
, sizeof(FLOAT
) );
148 ERR("invalid column type %u\n", table
->columns
[column
].type
& COL_TYPE_MASK
);
155 BSTR
get_value_bstr( const struct table
*table
, UINT row
, UINT column
)
162 if (table
->columns
[column
].type
& CIM_FLAG_ARRAY
)
164 FIXME("array to string conversion not handled\n");
167 if (get_value( table
, row
, column
, &val
) != S_OK
) return NULL
;
169 switch (table
->columns
[column
].type
& COL_TYPE_MASK
)
172 if (val
) return SysAllocString( L
"TRUE" );
173 else return SysAllocString( L
"FALSE" );
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
+ 1, L
"\"%s\"", (const WCHAR
*)(INT_PTR
)val
);
186 swprintf( number
, ARRAY_SIZE( number
), L
"%d", val
);
187 return SysAllocString( number
);
191 swprintf( number
, ARRAY_SIZE( number
), L
"%u", val
);
192 return SysAllocString( number
);
195 wsprintfW( number
, L
"%I64d", val
);
196 return SysAllocString( number
);
199 wsprintfW( number
, L
"%I64u", val
);
200 return SysAllocString( number
);
203 FIXME("unhandled column type %u\n", table
->columns
[column
].type
& COL_TYPE_MASK
);
209 HRESULT
set_value( const struct table
*table
, UINT row
, UINT column
, LONGLONG val
,
212 UINT col_offset
, row_size
;
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
)
226 *(WCHAR
**)ptr
= (WCHAR
*)(INT_PTR
)val
;
238 *(UINT16
*)ptr
= val
;
244 *(UINT32
*)ptr
= val
;
250 *(UINT64
*)ptr
= val
;
253 FIXME( "unhandled column type %lu\n", type
);
254 return WBEM_E_FAILED
;
259 HRESULT
get_method( const struct table
*table
, const WCHAR
*name
, class_method
**func
)
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
))
272 if ((hr
= get_value( table
, i
, j
, &val
)) != S_OK
) return hr
;
273 *func
= (class_method
*)(INT_PTR
)val
;
278 return WBEM_E_INVALID_METHOD
;
282 void free_row_values( const struct table
*table
, UINT row
)
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
)
308 if (!table
->data
) return;
310 for (i
= 0; i
< table
->num_rows
; i
++) free_row_values( table
, i
);
314 table
->num_rows_allocated
= 0;
320 void free_columns( struct column
*columns
, UINT num_cols
)
324 for (i
= 0; i
< num_cols
; i
++) { free( (WCHAR
*)columns
[i
].name
); }
328 void free_table( struct table
*table
)
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
);
339 list_remove( &table
->entry
);
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
);
355 struct table
*grab_table( enum wbm_namespace ns
, const WCHAR
*name
)
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
);
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
) )
378 if (!(table
= malloc( sizeof(*table
) ))) return NULL
;
379 table
->name
= wcsdup( name
);
380 table
->num_cols
= num_cols
;
381 table
->columns
= columns
;
382 table
->num_rows
= num_rows
;
383 table
->num_rows_allocated
= num_allocated
;
386 table
->flags
= TABLE_FLAG_DYNAMIC
;
388 list_init( &table
->entry
);
392 BOOL
add_table( enum wbm_namespace ns
, struct table
*table
)
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
));
406 list_add_tail( table_list
[ns
], &table
->entry
);
407 TRACE("added %p\n", table
);
411 BSTR
get_method_name( enum wbm_namespace ns
, const WCHAR
*class, UINT index
)
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
)
425 ret
= SysAllocString( table
->columns
[i
].name
);
426 release_table( table
);
432 release_table( table
);