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 #define WIN32_NO_STATUS
21 #define COM_NO_WINDOWS_H
35 #include "wine/debug.h"
36 #include "wbemprox_private.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(wbemprox
);
40 HRESULT
get_column_index( const struct table
*table
, const WCHAR
*name
, UINT
*column
)
43 for (i
= 0; i
< table
->num_cols
; i
++)
45 if (!strcmpiW( table
->columns
[i
].name
, name
))
51 return WBEM_E_INVALID_QUERY
;
54 UINT
get_type_size( CIMTYPE type
)
56 if (type
& CIM_FLAG_ARRAY
) return sizeof(void *);
73 return sizeof(WCHAR
*);
75 ERR("unhandled type %u\n", type
);
78 return sizeof(LONGLONG
);
81 static UINT
get_column_size( const struct table
*table
, UINT column
)
83 return get_type_size( table
->columns
[column
].type
& COL_TYPE_MASK
);
86 static UINT
get_column_offset( const struct table
*table
, UINT column
)
89 for (i
= 0; i
< column
; i
++) offset
+= get_column_size( table
, i
);
93 static UINT
get_row_size( const struct table
*table
)
95 return get_column_offset( table
, table
->num_cols
- 1 ) + get_column_size( table
, table
->num_cols
- 1 );
98 HRESULT
get_value( const struct table
*table
, UINT row
, UINT column
, LONGLONG
*val
)
100 UINT col_offset
, row_size
;
103 col_offset
= get_column_offset( table
, column
);
104 row_size
= get_row_size( table
);
105 ptr
= table
->data
+ row
* row_size
+ col_offset
;
107 if (table
->columns
[column
].type
& CIM_FLAG_ARRAY
)
109 *val
= (INT_PTR
)*(const void **)ptr
;
112 switch (table
->columns
[column
].type
& COL_TYPE_MASK
)
115 *val
= *(const int *)ptr
;
119 *val
= (INT_PTR
)*(const WCHAR
**)ptr
;
122 *val
= *(const INT16
*)ptr
;
125 *val
= *(const UINT16
*)ptr
;
128 *val
= *(const INT32
*)ptr
;
131 *val
= *(const UINT32
*)ptr
;
134 *val
= *(const INT64
*)ptr
;
137 *val
= *(const UINT64
*)ptr
;
140 ERR("invalid column type %u\n", table
->columns
[column
].type
& COL_TYPE_MASK
);
147 BSTR
get_value_bstr( const struct table
*table
, UINT row
, UINT column
)
149 static const WCHAR fmt_signedW
[] = {'%','d',0};
150 static const WCHAR fmt_unsignedW
[] = {'%','u',0};
151 static const WCHAR fmt_signed64W
[] = {'%','I','6','4','d',0};
152 static const WCHAR fmt_unsigned64W
[] = {'%','I','6','4','u',0};
153 static const WCHAR fmt_strW
[] = {'\"','%','s','\"',0};
154 static const WCHAR trueW
[] = {'T','R','U','E',0};
155 static const WCHAR falseW
[] = {'F','A','L','S','E',0};
161 if (table
->columns
[column
].type
& CIM_FLAG_ARRAY
)
163 FIXME("array to string conversion not handled\n");
166 if (get_value( table
, row
, column
, &val
) != S_OK
) return NULL
;
168 switch (table
->columns
[column
].type
& COL_TYPE_MASK
)
171 if (val
) return SysAllocString( trueW
);
172 else return SysAllocString( falseW
);
176 if (!val
) return NULL
;
177 len
= strlenW( (const WCHAR
*)(INT_PTR
)val
) + 2;
178 if (!(ret
= SysAllocStringLen( NULL
, len
))) return NULL
;
179 sprintfW( ret
, fmt_strW
, (const WCHAR
*)(INT_PTR
)val
);
184 sprintfW( number
, fmt_signedW
, val
);
185 return SysAllocString( number
);
189 sprintfW( number
, fmt_unsignedW
, val
);
190 return SysAllocString( number
);
193 wsprintfW( number
, fmt_signed64W
, val
);
194 return SysAllocString( number
);
197 wsprintfW( number
, fmt_unsigned64W
, val
);
198 return SysAllocString( number
);
201 FIXME("unhandled column type %u\n", table
->columns
[column
].type
& COL_TYPE_MASK
);
207 HRESULT
set_value( const struct table
*table
, UINT row
, UINT column
, LONGLONG val
,
210 UINT col_offset
, row_size
;
213 if ((table
->columns
[column
].type
& COL_TYPE_MASK
) != type
) return WBEM_E_TYPE_MISMATCH
;
215 col_offset
= get_column_offset( table
, column
);
216 row_size
= get_row_size( table
);
217 ptr
= table
->data
+ row
* row_size
+ col_offset
;
219 switch (table
->columns
[column
].type
& COL_TYPE_MASK
)
223 *(WCHAR
**)ptr
= (WCHAR
*)(INT_PTR
)val
;
229 *(UINT16
*)ptr
= val
;
235 *(UINT32
*)ptr
= val
;
241 *(UINT64
*)ptr
= val
;
244 FIXME("unhandled column type %u\n", type
);
245 return WBEM_E_FAILED
;
250 HRESULT
get_method( const struct table
*table
, const WCHAR
*name
, class_method
**func
)
254 for (i
= 0; i
< table
->num_rows
; i
++)
256 for (j
= 0; j
< table
->num_cols
; j
++)
258 if (table
->columns
[j
].type
& COL_FLAG_METHOD
&& !strcmpW( table
->columns
[j
].name
, name
))
263 if ((hr
= get_value( table
, i
, j
, &val
)) != S_OK
) return hr
;
264 *func
= (class_method
*)(INT_PTR
)val
;
269 return WBEM_E_INVALID_METHOD
;
273 static void clear_table( struct table
*table
)
278 if (!table
->data
) return;
280 for (i
= 0; i
< table
->num_rows
; i
++)
282 for (j
= 0; j
< table
->num_cols
; j
++)
284 if (!(table
->columns
[j
].type
& COL_FLAG_DYNAMIC
)) continue;
286 type
= table
->columns
[j
].type
& COL_TYPE_MASK
;
287 if (type
== CIM_STRING
|| type
== CIM_DATETIME
|| (type
& CIM_FLAG_ARRAY
))
289 if (get_value( table
, i
, j
, &val
) == S_OK
) heap_free( (void *)(INT_PTR
)val
);
296 heap_free( table
->data
);
301 void free_columns( struct column
*columns
, UINT num_cols
)
305 for (i
= 0; i
< num_cols
; i
++)
307 heap_free( (WCHAR
*)columns
[i
].name
);
309 heap_free( columns
);
312 void free_table( struct table
*table
)
316 clear_table( table
);
317 if (table
->flags
& TABLE_FLAG_DYNAMIC
)
319 TRACE("destroying %p\n", table
);
320 heap_free( (WCHAR
*)table
->name
);
321 free_columns( (struct column
*)table
->columns
, table
->num_cols
);
322 list_remove( &table
->entry
);
327 void release_table( struct table
*table
)
329 if (!InterlockedDecrement( &table
->refs
)) free_table( table
);
332 struct table
*addref_table( struct table
*table
)
334 InterlockedIncrement( &table
->refs
);
338 struct table
*grab_table( const WCHAR
*name
)
342 LIST_FOR_EACH_ENTRY( table
, table_list
, struct table
, entry
)
344 if (!strcmpiW( table
->name
, name
))
346 if (table
->fill
&& !table
->data
) table
->fill( table
);
347 TRACE("returning %p\n", table
);
348 return addref_table( table
);
354 struct table
*create_table( const WCHAR
*name
, UINT num_cols
, const struct column
*columns
,
355 UINT num_rows
, BYTE
*data
, void (*fill
)(struct table
*) )
359 if (!(table
= heap_alloc( sizeof(*table
) ))) return NULL
;
360 table
->name
= heap_strdupW( name
);
361 table
->num_cols
= num_cols
;
362 table
->columns
= columns
;
363 table
->num_rows
= num_rows
;
366 table
->flags
= TABLE_FLAG_DYNAMIC
;
368 list_init( &table
->entry
);
372 BOOL
add_table( struct table
*table
)
376 LIST_FOR_EACH_ENTRY( iter
, table_list
, struct table
, entry
)
378 if (!strcmpiW( iter
->name
, table
->name
))
380 TRACE("table %s already exists\n", debugstr_w(table
->name
));
384 list_add_tail( table_list
, &table
->entry
);
385 TRACE("added %p\n", table
);
389 BSTR
get_method_name( const WCHAR
*class, UINT index
)
395 if (!(table
= grab_table( class ))) return NULL
;
397 for (i
= 0; i
< table
->num_cols
; i
++)
399 if (table
->columns
[i
].type
& COL_FLAG_METHOD
)
403 ret
= SysAllocString( table
->columns
[i
].name
);
404 release_table( table
);
410 release_table( table
);
414 BSTR
get_property_name( const WCHAR
*class, UINT index
)
420 if (!(table
= grab_table( class ))) return NULL
;
422 for (i
= 0; i
< table
->num_cols
; i
++)
424 if (!(table
->columns
[i
].type
& COL_FLAG_METHOD
))
428 ret
= SysAllocString( table
->columns
[i
].name
);
429 release_table( table
);
435 release_table( table
);