xaudio2: Implement IXAudio2SourceVoice::Discontinuity.
[wine/multimedia.git] / dlls / propsys / propvar.c
blobfd22c9d175d0260779118adb6aff825d29c63622
1 /*
2 * PropVariant implementation
4 * Copyright 2008 James Hawkins for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <stdio.h>
24 #define NONAMELESSUNION
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "winreg.h"
30 #include "winuser.h"
31 #include "shlobj.h"
32 #include "propvarutil.h"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(propsys);
39 static HRESULT PROPVAR_ConvertFILETIME(PROPVARIANT *ppropvarDest,
40 REFPROPVARIANT propvarSrc, VARTYPE vt)
42 SYSTEMTIME time;
44 FileTimeToSystemTime(&propvarSrc->u.filetime, &time);
46 switch (vt)
48 case VT_LPSTR:
50 static const char format[] = "%04d/%02d/%02d:%02d:%02d:%02d.%03d";
52 ppropvarDest->u.pszVal = HeapAlloc(GetProcessHeap(), 0,
53 lstrlenA(format) + 1);
54 if (!ppropvarDest->u.pszVal)
55 return E_OUTOFMEMORY;
57 sprintf(ppropvarDest->u.pszVal, format, time.wYear, time.wMonth,
58 time.wDay, time.wHour, time.wMinute,
59 time.wSecond, time.wMilliseconds);
61 return S_OK;
64 default:
65 FIXME("Unhandled target type: %d\n", vt);
68 return E_FAIL;
71 static HRESULT PROPVAR_ConvertNumber(REFPROPVARIANT pv, int dest_bits,
72 BOOL dest_signed, LONGLONG *res)
74 BOOL src_signed;
76 switch (pv->vt)
78 case VT_I1:
79 src_signed = TRUE;
80 *res = pv->u.cVal;
81 break;
82 case VT_UI1:
83 src_signed = FALSE;
84 *res = pv->u.bVal;
85 break;
86 case VT_I2:
87 src_signed = TRUE;
88 *res = pv->u.iVal;
89 break;
90 case VT_UI2:
91 src_signed = FALSE;
92 *res = pv->u.uiVal;
93 break;
94 case VT_I4:
95 src_signed = TRUE;
96 *res = pv->u.lVal;
97 break;
98 case VT_UI4:
99 src_signed = FALSE;
100 *res = pv->u.ulVal;
101 break;
102 case VT_I8:
103 src_signed = TRUE;
104 *res = pv->u.hVal.QuadPart;
105 break;
106 case VT_UI8:
107 src_signed = FALSE;
108 *res = pv->u.uhVal.QuadPart;
109 break;
110 case VT_EMPTY:
111 src_signed = FALSE;
112 *res = 0;
113 break;
114 default:
115 FIXME("unhandled vt %d\n", pv->vt);
116 return E_NOTIMPL;
119 if (*res < 0 && src_signed != dest_signed)
120 return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
122 if (dest_bits < 64)
124 if (dest_signed)
126 if (*res >= ((LONGLONG)1 << (dest_bits-1)) ||
127 *res < ((LONGLONG)-1 << (dest_bits-1)))
128 return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
130 else
132 if ((ULONGLONG)(*res) >= ((ULONGLONG)1 << dest_bits))
133 return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
137 return S_OK;
140 HRESULT WINAPI PropVariantToInt16(REFPROPVARIANT propvarIn, SHORT *ret)
142 LONGLONG res;
143 HRESULT hr;
145 TRACE("%p,%p\n", propvarIn, ret);
147 hr = PROPVAR_ConvertNumber(propvarIn, 16, TRUE, &res);
148 if (SUCCEEDED(hr)) *ret = (SHORT)res;
149 return hr;
152 HRESULT WINAPI PropVariantToInt32(REFPROPVARIANT propvarIn, LONG *ret)
154 LONGLONG res;
155 HRESULT hr;
157 TRACE("%p,%p\n", propvarIn, ret);
159 hr = PROPVAR_ConvertNumber(propvarIn, 32, TRUE, &res);
160 if (SUCCEEDED(hr)) *ret = (LONG)res;
161 return hr;
164 HRESULT WINAPI PropVariantToInt64(REFPROPVARIANT propvarIn, LONGLONG *ret)
166 LONGLONG res;
167 HRESULT hr;
169 TRACE("%p,%p\n", propvarIn, ret);
171 hr = PROPVAR_ConvertNumber(propvarIn, 64, TRUE, &res);
172 if (SUCCEEDED(hr)) *ret = (LONGLONG)res;
173 return hr;
176 HRESULT WINAPI PropVariantToUInt16(REFPROPVARIANT propvarIn, USHORT *ret)
178 LONGLONG res;
179 HRESULT hr;
181 TRACE("%p,%p\n", propvarIn, ret);
183 hr = PROPVAR_ConvertNumber(propvarIn, 16, FALSE, &res);
184 if (SUCCEEDED(hr)) *ret = (USHORT)res;
185 return hr;
188 HRESULT WINAPI PropVariantToUInt32(REFPROPVARIANT propvarIn, ULONG *ret)
190 LONGLONG res;
191 HRESULT hr;
193 TRACE("%p,%p\n", propvarIn, ret);
195 hr = PROPVAR_ConvertNumber(propvarIn, 32, FALSE, &res);
196 if (SUCCEEDED(hr)) *ret = (ULONG)res;
197 return hr;
200 HRESULT WINAPI PropVariantToUInt64(REFPROPVARIANT propvarIn, ULONGLONG *ret)
202 LONGLONG res;
203 HRESULT hr;
205 TRACE("%p,%p\n", propvarIn, ret);
207 hr = PROPVAR_ConvertNumber(propvarIn, 64, FALSE, &res);
208 if (SUCCEEDED(hr)) *ret = (ULONGLONG)res;
209 return hr;
212 /******************************************************************
213 * PropVariantChangeType (PROPSYS.@)
215 HRESULT WINAPI PropVariantChangeType(PROPVARIANT *ppropvarDest, REFPROPVARIANT propvarSrc,
216 PROPVAR_CHANGE_FLAGS flags, VARTYPE vt)
218 HRESULT hr;
220 FIXME("(%p, %p, %d, %d, %d): semi-stub!\n", ppropvarDest, propvarSrc,
221 propvarSrc->vt, flags, vt);
223 switch (vt)
225 case VT_I2:
227 SHORT res;
228 hr = PropVariantToInt16(propvarSrc, &res);
229 if (SUCCEEDED(hr))
231 ppropvarDest->vt = VT_I2;
232 ppropvarDest->u.iVal = res;
234 return hr;
236 case VT_UI2:
238 USHORT res;
239 hr = PropVariantToUInt16(propvarSrc, &res);
240 if (SUCCEEDED(hr))
242 ppropvarDest->vt = VT_UI2;
243 ppropvarDest->u.uiVal = res;
245 return hr;
247 case VT_I4:
249 LONG res;
250 hr = PropVariantToInt32(propvarSrc, &res);
251 if (SUCCEEDED(hr))
253 ppropvarDest->vt = VT_I4;
254 ppropvarDest->u.lVal = res;
256 return hr;
258 case VT_UI4:
260 ULONG res;
261 hr = PropVariantToUInt32(propvarSrc, &res);
262 if (SUCCEEDED(hr))
264 ppropvarDest->vt = VT_UI4;
265 ppropvarDest->u.ulVal = res;
267 return hr;
269 case VT_I8:
271 LONGLONG res;
272 hr = PropVariantToInt64(propvarSrc, &res);
273 if (SUCCEEDED(hr))
275 ppropvarDest->vt = VT_I8;
276 ppropvarDest->u.hVal.QuadPart = res;
278 return hr;
280 case VT_UI8:
282 ULONGLONG res;
283 hr = PropVariantToUInt64(propvarSrc, &res);
284 if (SUCCEEDED(hr))
286 ppropvarDest->vt = VT_UI8;
287 ppropvarDest->u.uhVal.QuadPart = res;
289 return hr;
293 switch (propvarSrc->vt)
295 case VT_FILETIME:
296 return PROPVAR_ConvertFILETIME(ppropvarDest, propvarSrc, vt);
297 default:
298 FIXME("Unhandled source type: %d\n", propvarSrc->vt);
301 return E_FAIL;
304 static void PROPVAR_GUIDToWSTR(REFGUID guid, WCHAR *str)
306 static const WCHAR format[] = {'{','%','0','8','X','-','%','0','4','X','-','%','0','4','X',
307 '-','%','0','2','X','%','0','2','X','-','%','0','2','X','%','0','2','X','%','0','2','X',
308 '%','0','2','X','%','0','2','X','%','0','2','X','}',0};
310 sprintfW(str, format, guid->Data1, guid->Data2, guid->Data3,
311 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
312 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
315 HRESULT WINAPI InitPropVariantFromGUIDAsString(REFGUID guid, PROPVARIANT *ppropvar)
317 TRACE("(%p %p)\n", guid, ppropvar);
319 if(!guid)
320 return E_FAIL;
322 ppropvar->vt = VT_LPWSTR;
323 ppropvar->u.pwszVal = CoTaskMemAlloc(39*sizeof(WCHAR));
324 if(!ppropvar->u.pwszVal)
325 return E_OUTOFMEMORY;
327 PROPVAR_GUIDToWSTR(guid, ppropvar->u.pwszVal);
328 return S_OK;
331 HRESULT WINAPI InitVariantFromGUIDAsString(REFGUID guid, VARIANT *pvar)
333 TRACE("(%p %p)\n", guid, pvar);
335 if(!guid) {
336 FIXME("guid == NULL\n");
337 return E_FAIL;
340 V_VT(pvar) = VT_BSTR;
341 V_BSTR(pvar) = SysAllocStringLen(NULL, 38);
342 if(!V_BSTR(pvar))
343 return E_OUTOFMEMORY;
345 PROPVAR_GUIDToWSTR(guid, V_BSTR(pvar));
346 return S_OK;
349 HRESULT WINAPI InitPropVariantFromBuffer(const VOID *pv, UINT cb, PROPVARIANT *ppropvar)
351 TRACE("(%p %u %p)\n", pv, cb, ppropvar);
353 ppropvar->u.caub.pElems = CoTaskMemAlloc(cb);
354 if(!ppropvar->u.caub.pElems)
355 return E_OUTOFMEMORY;
357 ppropvar->vt = VT_VECTOR|VT_UI1;
358 ppropvar->u.caub.cElems = cb;
359 memcpy(ppropvar->u.caub.pElems, pv, cb);
360 return S_OK;
363 HRESULT WINAPI InitVariantFromBuffer(const VOID *pv, UINT cb, VARIANT *pvar)
365 SAFEARRAY *arr;
366 void *data;
367 HRESULT hres;
369 TRACE("(%p %u %p)\n", pv, cb, pvar);
371 arr = SafeArrayCreateVector(VT_UI1, 0, cb);
372 if(!arr)
373 return E_OUTOFMEMORY;
375 hres = SafeArrayAccessData(arr, &data);
376 if(FAILED(hres)) {
377 SafeArrayDestroy(arr);
378 return hres;
381 memcpy(data, pv, cb);
383 hres = SafeArrayUnaccessData(arr);
384 if(FAILED(hres)) {
385 SafeArrayDestroy(arr);
386 return hres;
389 V_VT(pvar) = VT_ARRAY|VT_UI1;
390 V_ARRAY(pvar) = arr;
391 return S_OK;
394 static inline DWORD PROPVAR_HexToNum(const WCHAR *hex)
396 DWORD ret;
398 if(hex[0]>='0' && hex[0]<='9')
399 ret = hex[0]-'0';
400 else if(hex[0]>='a' && hex[0]<='f')
401 ret = hex[0]-'a'+10;
402 else if(hex[0]>='A' && hex[0]<='F')
403 ret = hex[0]-'A'+10;
404 else
405 return -1;
407 ret <<= 4;
408 if(hex[1]>='0' && hex[1]<='9')
409 return ret + hex[1]-'0';
410 else if(hex[1]>='a' && hex[1]<='f')
411 return ret + hex[1]-'a'+10;
412 else if(hex[1]>='A' && hex[1]<='F')
413 return ret + hex[1]-'A'+10;
414 else
415 return -1;
418 static inline HRESULT PROPVAR_WCHARToGUID(const WCHAR *str, int len, GUID *guid)
420 DWORD i, val=0;
421 const WCHAR *p;
423 memset(guid, 0, sizeof(GUID));
425 if(len!=38 || str[0]!='{' || str[9]!='-' || str[14]!='-'
426 || str[19]!='-' || str[24]!='-' || str[37]!='}') {
427 WARN("Error parsing %s\n", debugstr_w(str));
428 return E_INVALIDARG;
431 p = str+1;
432 for(i=0; i<4 && val!=-1; i++) {
433 val = PROPVAR_HexToNum(p);
434 guid->Data1 = (guid->Data1<<8) + val;
435 p += 2;
437 p++;
438 for(i=0; i<2 && val!=-1; i++) {
439 val = PROPVAR_HexToNum(p);
440 guid->Data2 = (guid->Data2<<8) + val;
441 p += 2;
443 p++;
444 for(i=0; i<2 && val!=-1; i++) {
445 val = PROPVAR_HexToNum(p);
446 guid->Data3 = (guid->Data3<<8) + val;
447 p += 2;
449 p++;
450 for(i=0; i<8 && val!=-1; i++) {
451 if(i == 2)
452 p++;
454 val = guid->Data4[i] = PROPVAR_HexToNum(p);
455 p += 2;
458 if(val == -1) {
459 WARN("Error parsing %s\n", debugstr_w(str));
460 memset(guid, 0, sizeof(GUID));
461 return E_INVALIDARG;
463 return S_OK;
466 HRESULT WINAPI PropVariantToGUID(const PROPVARIANT *ppropvar, GUID *guid)
468 TRACE("%p %p)\n", ppropvar, guid);
470 switch(ppropvar->vt) {
471 case VT_BSTR:
472 return PROPVAR_WCHARToGUID(ppropvar->u.bstrVal, SysStringLen(ppropvar->u.bstrVal), guid);
473 case VT_LPWSTR:
474 return PROPVAR_WCHARToGUID(ppropvar->u.pwszVal, strlenW(ppropvar->u.pwszVal), guid);
476 default:
477 FIXME("unsupported vt: %d\n", ppropvar->vt);
478 return E_NOTIMPL;
482 HRESULT WINAPI VariantToGUID(const VARIANT *pvar, GUID *guid)
484 TRACE("(%p %p)\n", pvar, guid);
486 switch(V_VT(pvar)) {
487 case VT_BSTR: {
488 HRESULT hres = PROPVAR_WCHARToGUID(V_BSTR(pvar), SysStringLen(V_BSTR(pvar)), guid);
489 if(hres == E_INVALIDARG)
490 return E_FAIL;
491 return hres;
494 default:
495 FIXME("unsupported vt: %d\n", V_VT(pvar));
496 return E_NOTIMPL;
500 static BOOL isemptyornull(const PROPVARIANT *propvar)
502 if (propvar->vt == VT_EMPTY || propvar->vt == VT_NULL)
503 return TRUE;
504 if ((propvar->vt & VT_ARRAY) == VT_ARRAY)
506 int i;
507 for (i=0; i<propvar->u.parray->cDims; i++)
509 if (propvar->u.parray->rgsabound[i].cElements != 0)
510 break;
512 return i == propvar->u.parray->cDims;
514 /* FIXME: vectors, byrefs, errors? */
515 return FALSE;
518 INT WINAPI PropVariantCompareEx(REFPROPVARIANT propvar1, REFPROPVARIANT propvar2,
519 PROPVAR_COMPARE_UNIT unit, PROPVAR_COMPARE_FLAGS flags)
521 const PROPVARIANT *propvar2_converted;
522 PROPVARIANT propvar2_static;
523 HRESULT hr;
524 INT res=-1;
526 TRACE("%p,%p,%x,%x\n", propvar1, propvar2, unit, flags);
528 if (isemptyornull(propvar1))
530 if (isemptyornull(propvar2))
531 return 0;
532 return (flags & PVCF_TREATEMPTYASGREATERTHAN) ? 1 : -1;
535 if (isemptyornull(propvar2))
536 return (flags & PVCF_TREATEMPTYASGREATERTHAN) ? -1 : 1;
538 if (propvar1->vt != propvar2->vt)
540 hr = PropVariantChangeType(&propvar2_static, propvar2, 0, propvar1->vt);
542 if (FAILED(hr))
543 return -1;
545 propvar2_converted = &propvar2_static;
547 else
548 propvar2_converted = propvar2;
550 #define CMP_INT_VALUE(var) do { \
551 if (propvar1->u.var > propvar2_converted->u.var) \
552 res = 1; \
553 else if (propvar1->u.var < propvar2_converted->u.var) \
554 res = -1; \
555 else \
556 res = 0; \
557 } while (0)
559 switch (propvar1->vt)
561 case VT_I1:
562 CMP_INT_VALUE(cVal);
563 break;
564 case VT_UI1:
565 CMP_INT_VALUE(bVal);
566 break;
567 case VT_I2:
568 CMP_INT_VALUE(iVal);
569 break;
570 case VT_UI2:
571 CMP_INT_VALUE(uiVal);
572 break;
573 case VT_I4:
574 CMP_INT_VALUE(lVal);
575 break;
576 case VT_UI4:
577 CMP_INT_VALUE(uiVal);
578 break;
579 case VT_I8:
580 CMP_INT_VALUE(hVal.QuadPart);
581 break;
582 case VT_UI8:
583 CMP_INT_VALUE(uhVal.QuadPart);
584 break;
585 case VT_BSTR:
586 /* FIXME: Use string flags. */
587 res = lstrcmpW(propvar1->u.bstrVal, propvar2->u.bstrVal);
588 break;
589 default:
590 FIXME("vartype %d not handled\n", propvar1->vt);
591 res = -1;
592 break;
595 if (propvar2_converted == &propvar2_static)
596 PropVariantClear(&propvar2_static);
598 return res;