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 *);
66 return sizeof(WCHAR
*);
68 ERR("unhandled type %u\n", type
);
71 return sizeof(LONGLONG
);
74 static UINT
get_column_size( const struct table
*table
, UINT column
)
76 return get_type_size( table
->columns
[column
].type
& COL_TYPE_MASK
);
79 static UINT
get_column_offset( const struct table
*table
, UINT column
)
82 for (i
= 0; i
< column
; i
++) offset
+= get_column_size( table
, i
);
86 static UINT
get_row_size( const struct table
*table
)
88 return get_column_offset( table
, table
->num_cols
- 1 ) + get_column_size( table
, table
->num_cols
- 1 );
91 HRESULT
get_value( const struct table
*table
, UINT row
, UINT column
, LONGLONG
*val
)
93 UINT col_offset
, row_size
;
96 col_offset
= get_column_offset( table
, column
);
97 row_size
= get_row_size( table
);
98 ptr
= table
->data
+ row
* row_size
+ col_offset
;
100 if (table
->columns
[column
].type
& CIM_FLAG_ARRAY
)
102 *val
= (INT_PTR
)*(const void **)ptr
;
105 switch (table
->columns
[column
].type
& COL_TYPE_MASK
)
108 *val
= *(const int *)ptr
;
112 *val
= (INT_PTR
)*(const WCHAR
**)ptr
;
115 *val
= *(const INT16
*)ptr
;
118 *val
= *(const UINT16
*)ptr
;
121 *val
= *(const INT32
*)ptr
;
124 *val
= *(const UINT32
*)ptr
;
127 *val
= *(const INT64
*)ptr
;
130 *val
= *(const UINT64
*)ptr
;
133 ERR("invalid column type %u\n", table
->columns
[column
].type
& COL_TYPE_MASK
);
140 BSTR
get_value_bstr( const struct table
*table
, UINT row
, UINT column
)
142 static const WCHAR fmt_signedW
[] = {'%','d',0};
143 static const WCHAR fmt_unsignedW
[] = {'%','u',0};
144 static const WCHAR fmt_signed64W
[] = {'%','I','6','4','d',0};
145 static const WCHAR fmt_unsigned64W
[] = {'%','I','6','4','u',0};
146 static const WCHAR fmt_strW
[] = {'\"','%','s','\"',0};
147 static const WCHAR trueW
[] = {'T','R','U','E',0};
148 static const WCHAR falseW
[] = {'F','A','L','S','E',0};
154 if (table
->columns
[column
].type
& CIM_FLAG_ARRAY
)
156 FIXME("array to string conversion not handled\n");
159 if (get_value( table
, row
, column
, &val
) != S_OK
) return NULL
;
161 switch (table
->columns
[column
].type
& COL_TYPE_MASK
)
164 if (val
) return SysAllocString( trueW
);
165 else return SysAllocString( falseW
);
169 if (!val
) return NULL
;
170 len
= strlenW( (const WCHAR
*)(INT_PTR
)val
) + 2;
171 if (!(ret
= SysAllocStringLen( NULL
, len
))) return NULL
;
172 sprintfW( ret
, fmt_strW
, (const WCHAR
*)(INT_PTR
)val
);
177 sprintfW( number
, fmt_signedW
, val
);
178 return SysAllocString( number
);
182 sprintfW( number
, fmt_unsignedW
, val
);
183 return SysAllocString( number
);
186 wsprintfW( number
, fmt_signed64W
, val
);
187 return SysAllocString( number
);
190 wsprintfW( number
, fmt_unsigned64W
, val
);
191 return SysAllocString( number
);
194 FIXME("unhandled column type %u\n", table
->columns
[column
].type
& COL_TYPE_MASK
);
200 HRESULT
set_value( const struct table
*table
, UINT row
, UINT column
, LONGLONG val
,
203 UINT col_offset
, row_size
;
206 if ((table
->columns
[column
].type
& COL_TYPE_MASK
) != type
) return WBEM_E_TYPE_MISMATCH
;
208 col_offset
= get_column_offset( table
, column
);
209 row_size
= get_row_size( table
);
210 ptr
= table
->data
+ row
* row_size
+ col_offset
;
212 switch (table
->columns
[column
].type
& COL_TYPE_MASK
)
216 *(WCHAR
**)ptr
= (WCHAR
*)(INT_PTR
)val
;
222 *(UINT16
*)ptr
= val
;
228 *(UINT32
*)ptr
= val
;
234 *(UINT64
*)ptr
= val
;
237 FIXME("unhandled column type %u\n", type
);
238 return WBEM_E_FAILED
;
243 HRESULT
get_method( const struct table
*table
, const WCHAR
*name
, class_method
**func
)
247 for (i
= 0; i
< table
->num_rows
; i
++)
249 for (j
= 0; j
< table
->num_cols
; j
++)
251 if (table
->columns
[j
].type
& COL_FLAG_METHOD
&& !strcmpW( table
->columns
[j
].name
, name
))
256 if ((hr
= get_value( table
, i
, j
, &val
)) != S_OK
) return hr
;
257 *func
= (class_method
*)(INT_PTR
)val
;
262 return WBEM_E_INVALID_METHOD
;
266 void free_row_values( const struct table
*table
, UINT row
)
271 for (i
= 0; i
< table
->num_cols
; i
++)
273 if (!(table
->columns
[i
].type
& COL_FLAG_DYNAMIC
)) continue;
275 type
= table
->columns
[i
].type
& COL_TYPE_MASK
;
276 if (type
== CIM_STRING
|| type
== CIM_DATETIME
|| (type
& CIM_FLAG_ARRAY
))
278 if (get_value( table
, row
, i
, &val
) == S_OK
) heap_free( (void *)(INT_PTR
)val
);
283 void clear_table( struct table
*table
)
287 if (!table
->data
) return;
289 for (i
= 0; i
< table
->num_rows
; i
++) free_row_values( table
, i
);
293 table
->num_rows_allocated
= 0;
294 heap_free( table
->data
);
299 void free_columns( struct column
*columns
, UINT num_cols
)
303 for (i
= 0; i
< num_cols
; i
++) { heap_free( (WCHAR
*)columns
[i
].name
); }
304 heap_free( columns
);
307 void free_table( struct table
*table
)
311 clear_table( table
);
312 if (table
->flags
& TABLE_FLAG_DYNAMIC
)
314 TRACE("destroying %p\n", table
);
315 heap_free( (WCHAR
*)table
->name
);
316 free_columns( (struct column
*)table
->columns
, table
->num_cols
);
317 list_remove( &table
->entry
);
322 void release_table( struct table
*table
)
324 if (!InterlockedDecrement( &table
->refs
)) free_table( table
);
327 struct table
*addref_table( struct table
*table
)
329 InterlockedIncrement( &table
->refs
);
333 struct table
*grab_table( const WCHAR
*name
)
337 LIST_FOR_EACH_ENTRY( table
, table_list
, struct table
, entry
)
339 if (!strcmpiW( table
->name
, name
))
341 TRACE("returning %p\n", table
);
342 return addref_table( table
);
348 struct table
*create_table( const WCHAR
*name
, UINT num_cols
, const struct column
*columns
,
349 UINT num_rows
, UINT num_allocated
, BYTE
*data
,
350 enum fill_status (*fill
)(struct table
*, const struct expr
*cond
) )
354 if (!(table
= heap_alloc( sizeof(*table
) ))) return NULL
;
355 table
->name
= heap_strdupW( name
);
356 table
->num_cols
= num_cols
;
357 table
->columns
= columns
;
358 table
->num_rows
= num_rows
;
359 table
->num_rows_allocated
= num_allocated
;
362 table
->flags
= TABLE_FLAG_DYNAMIC
;
364 list_init( &table
->entry
);
368 BOOL
add_table( struct table
*table
)
372 LIST_FOR_EACH_ENTRY( iter
, table_list
, struct table
, entry
)
374 if (!strcmpiW( iter
->name
, table
->name
))
376 TRACE("table %s already exists\n", debugstr_w(table
->name
));
380 list_add_tail( table_list
, &table
->entry
);
381 TRACE("added %p\n", table
);
385 BSTR
get_method_name( const WCHAR
*class, UINT index
)
391 if (!(table
= grab_table( class ))) return NULL
;
393 for (i
= 0; i
< table
->num_cols
; i
++)
395 if (table
->columns
[i
].type
& COL_FLAG_METHOD
)
399 ret
= SysAllocString( table
->columns
[i
].name
);
400 release_table( table
);
406 release_table( table
);
410 BSTR
get_property_name( const WCHAR
*class, UINT index
)
416 if (!(table
= grab_table( class ))) return NULL
;
418 for (i
= 0; i
< table
->num_cols
; i
++)
420 if (!(table
->columns
[i
].type
& COL_FLAG_METHOD
))
424 ret
= SysAllocString( table
->columns
[i
].name
);
425 release_table( table
);
431 release_table( table
);