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 #include "wbemprox_private.h"
23 HRESULT
get_column_index( const struct table
*table
, const WCHAR
*name
, UINT
*column
)
26 for (i
= 0; i
< table
->num_cols
; i
++)
28 if (!strcmpiW( table
->columns
[i
].name
, name
))
34 return WBEM_E_INVALID_QUERY
;
37 UINT
get_type_size( CIMTYPE type
)
39 if (type
& CIM_FLAG_ARRAY
) return sizeof(void *);
56 return sizeof(WCHAR
*);
58 ERR("unhandled type %u\n", type
);
61 return sizeof(LONGLONG
);
64 static UINT
get_column_size( const struct table
*table
, UINT column
)
66 return get_type_size( table
->columns
[column
].type
& COL_TYPE_MASK
);
69 static UINT
get_column_offset( const struct table
*table
, UINT column
)
72 for (i
= 0; i
< column
; i
++) offset
+= get_column_size( table
, i
);
76 static UINT
get_row_size( const struct table
*table
)
78 return get_column_offset( table
, table
->num_cols
- 1 ) + get_column_size( table
, table
->num_cols
- 1 );
81 HRESULT
get_value( const struct table
*table
, UINT row
, UINT column
, LONGLONG
*val
)
83 UINT col_offset
, row_size
;
86 col_offset
= get_column_offset( table
, column
);
87 row_size
= get_row_size( table
);
88 ptr
= table
->data
+ row
* row_size
+ col_offset
;
90 if (table
->columns
[column
].type
& CIM_FLAG_ARRAY
)
92 *val
= (INT_PTR
)*(const void **)ptr
;
95 switch (table
->columns
[column
].type
& COL_TYPE_MASK
)
98 *val
= *(const int *)ptr
;
102 *val
= (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};
137 static const WCHAR trueW
[] = {'T','R','U','E',0};
138 static const WCHAR falseW
[] = {'F','A','L','S','E',0};
144 if (table
->columns
[column
].type
& CIM_FLAG_ARRAY
)
146 FIXME("array to string conversion not handled\n");
149 if (get_value( table
, row
, column
, &val
) != S_OK
) return NULL
;
151 switch (table
->columns
[column
].type
& COL_TYPE_MASK
)
154 if (val
) return SysAllocString( trueW
);
155 else return SysAllocString( falseW
);
159 if (!val
) return NULL
;
160 len
= strlenW( (const WCHAR
*)(INT_PTR
)val
) + 2;
161 if (!(ret
= SysAllocStringLen( NULL
, len
))) return NULL
;
162 sprintfW( ret
, fmt_strW
, (const WCHAR
*)(INT_PTR
)val
);
167 sprintfW( number
, fmt_signedW
, val
);
168 return SysAllocString( number
);
172 sprintfW( number
, fmt_unsignedW
, val
);
173 return SysAllocString( number
);
176 wsprintfW( number
, fmt_signed64W
, val
);
177 return SysAllocString( number
);
180 wsprintfW( number
, fmt_unsigned64W
, val
);
181 return SysAllocString( number
);
184 FIXME("unhandled column type %u\n", table
->columns
[column
].type
& COL_TYPE_MASK
);
190 HRESULT
set_value( const struct table
*table
, UINT row
, UINT column
, LONGLONG val
,
193 UINT col_offset
, row_size
;
196 if ((table
->columns
[column
].type
& COL_TYPE_MASK
) != type
) return WBEM_E_TYPE_MISMATCH
;
198 col_offset
= get_column_offset( table
, column
);
199 row_size
= get_row_size( table
);
200 ptr
= table
->data
+ row
* row_size
+ col_offset
;
202 switch (table
->columns
[column
].type
& COL_TYPE_MASK
)
206 *(WCHAR
**)ptr
= (WCHAR
*)(INT_PTR
)val
;
212 *(UINT16
*)ptr
= val
;
218 *(UINT32
*)ptr
= val
;
224 *(UINT64
*)ptr
= val
;
227 FIXME("unhandled column type %u\n", type
);
228 return WBEM_E_FAILED
;
233 HRESULT
get_method( const struct table
*table
, const WCHAR
*name
, class_method
**func
)
237 for (i
= 0; i
< table
->num_rows
; i
++)
239 for (j
= 0; j
< table
->num_cols
; j
++)
241 if (table
->columns
[j
].type
& COL_FLAG_METHOD
&& !strcmpW( table
->columns
[j
].name
, name
))
246 if ((hr
= get_value( table
, i
, j
, &val
)) != S_OK
) return hr
;
247 *func
= (class_method
*)(INT_PTR
)val
;
252 return WBEM_E_INVALID_METHOD
;
256 void free_row_values( const struct table
*table
, UINT row
)
261 for (i
= 0; i
< table
->num_cols
; i
++)
263 if (!(table
->columns
[i
].type
& COL_FLAG_DYNAMIC
)) continue;
265 type
= table
->columns
[i
].type
& COL_TYPE_MASK
;
266 if (type
== CIM_STRING
|| type
== CIM_DATETIME
|| (type
& CIM_FLAG_ARRAY
))
268 if (get_value( table
, row
, i
, &val
) == S_OK
) heap_free( (void *)(INT_PTR
)val
);
273 void clear_table( struct table
*table
)
277 if (!table
->data
) return;
279 for (i
= 0; i
< table
->num_rows
; i
++) free_row_values( table
, i
);
283 table
->num_rows_allocated
= 0;
284 heap_free( table
->data
);
289 void free_columns( struct column
*columns
, UINT num_cols
)
293 for (i
= 0; i
< num_cols
; i
++) { heap_free( (WCHAR
*)columns
[i
].name
); }
294 heap_free( columns
);
297 void free_table( struct table
*table
)
301 clear_table( table
);
302 if (table
->flags
& TABLE_FLAG_DYNAMIC
)
304 TRACE("destroying %p\n", table
);
305 heap_free( (WCHAR
*)table
->name
);
306 free_columns( (struct column
*)table
->columns
, table
->num_cols
);
307 list_remove( &table
->entry
);
312 void release_table( struct table
*table
)
314 if (!InterlockedDecrement( &table
->refs
)) free_table( table
);
317 struct table
*addref_table( struct table
*table
)
319 InterlockedIncrement( &table
->refs
);
323 struct table
*grab_table( const WCHAR
*name
)
327 LIST_FOR_EACH_ENTRY( table
, table_list
, struct table
, entry
)
329 if (!strcmpiW( table
->name
, name
))
331 TRACE("returning %p\n", table
);
332 return addref_table( table
);
338 struct table
*create_table( const WCHAR
*name
, UINT num_cols
, const struct column
*columns
,
339 UINT num_rows
, UINT num_allocated
, BYTE
*data
,
340 enum fill_status (*fill
)(struct table
*, const struct expr
*cond
) )
344 if (!(table
= heap_alloc( sizeof(*table
) ))) return NULL
;
345 table
->name
= heap_strdupW( name
);
346 table
->num_cols
= num_cols
;
347 table
->columns
= columns
;
348 table
->num_rows
= num_rows
;
349 table
->num_rows_allocated
= num_allocated
;
352 table
->flags
= TABLE_FLAG_DYNAMIC
;
354 list_init( &table
->entry
);
358 BOOL
add_table( struct table
*table
)
362 LIST_FOR_EACH_ENTRY( iter
, table_list
, struct table
, entry
)
364 if (!strcmpiW( iter
->name
, table
->name
))
366 TRACE("table %s already exists\n", debugstr_w(table
->name
));
370 list_add_tail( table_list
, &table
->entry
);
371 TRACE("added %p\n", table
);
375 BSTR
get_method_name( const WCHAR
*class, UINT index
)
381 if (!(table
= grab_table( class ))) return NULL
;
383 for (i
= 0; i
< table
->num_cols
; i
++)
385 if (table
->columns
[i
].type
& COL_FLAG_METHOD
)
389 ret
= SysAllocString( table
->columns
[i
].name
);
390 release_table( table
);
396 release_table( table
);
400 BSTR
get_property_name( const WCHAR
*class, UINT index
)
406 if (!(table
= grab_table( class ))) return NULL
;
408 for (i
= 0; i
< table
->num_cols
; i
++)
410 if (!(table
->columns
[i
].type
& COL_FLAG_METHOD
))
414 ret
= SysAllocString( table
->columns
[i
].name
);
415 release_table( table
);
421 release_table( table
);