dinput: Fix printing NULL strings.
[wine] / dlls / propsys / propvar.c
1 /*
2  * PropVariant implementation
3  *
4  * Copyright 2008 James Hawkins for CodeWeavers
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define NONAMELESSUNION
25
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"
33
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(propsys);
38
39 static HRESULT PROPVAR_ConvertFILETIME(PROPVARIANT *ppropvarDest,
40                                        REFPROPVARIANT propvarSrc, VARTYPE vt)
41 {
42     SYSTEMTIME time;
43
44     FileTimeToSystemTime(&propvarSrc->u.filetime, &time);
45
46     switch (vt)
47     {
48         case VT_LPSTR:
49         {
50             static const char format[] = "%04d/%02d/%02d:%02d:%02d:%02d.%03d";
51
52             ppropvarDest->u.pszVal = HeapAlloc(GetProcessHeap(), 0,
53                                              lstrlenA(format) + 1);
54             if (!ppropvarDest->u.pszVal)
55                 return E_OUTOFMEMORY;
56
57             sprintf(ppropvarDest->u.pszVal, format, time.wYear, time.wMonth,
58                     time.wDay, time.wHour, time.wMinute,
59                     time.wSecond, time.wMilliseconds);
60
61             return S_OK;
62         }
63
64         default:
65             FIXME("Unhandled target type: %d\n", vt);
66     }
67
68     return E_FAIL;
69 }
70
71 /******************************************************************
72  *  PropVariantChangeType   (PROPSYS.@)
73  */
74 HRESULT WINAPI PropVariantChangeType(PROPVARIANT *ppropvarDest, REFPROPVARIANT propvarSrc,
75                                      PROPVAR_CHANGE_FLAGS flags, VARTYPE vt)
76 {
77     FIXME("(%p, %p, %d, %d, %d): semi-stub!\n", ppropvarDest, propvarSrc,
78           propvarSrc->vt, flags, vt);
79
80     switch (propvarSrc->vt)
81     {
82         case VT_FILETIME:
83             return PROPVAR_ConvertFILETIME(ppropvarDest, propvarSrc, vt);
84         default:
85             FIXME("Unhandled source type: %d\n", propvarSrc->vt);
86     }
87
88     return E_FAIL;
89 }
90
91 static void PROPVAR_GUIDToWSTR(REFGUID guid, WCHAR *str)
92 {
93     static const WCHAR format[] = {'{','%','0','8','X','-','%','0','4','X','-','%','0','4','X',
94         '-','%','0','2','X','%','0','2','X','-','%','0','2','X','%','0','2','X','%','0','2','X',
95         '%','0','2','X','%','0','2','X','%','0','2','X','}',0};
96
97     sprintfW(str, format, guid->Data1, guid->Data2, guid->Data3,
98             guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
99             guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
100 }
101
102 HRESULT WINAPI InitPropVariantFromGUIDAsString(REFGUID guid, PROPVARIANT *ppropvar)
103 {
104     TRACE("(%p %p)\n", guid, ppropvar);
105
106     if(!guid)
107         return E_FAIL;
108
109     ppropvar->vt = VT_LPWSTR;
110     ppropvar->u.pwszVal = CoTaskMemAlloc(39*sizeof(WCHAR));
111     if(!ppropvar->u.pwszVal)
112         return E_OUTOFMEMORY;
113
114     PROPVAR_GUIDToWSTR(guid, ppropvar->u.pwszVal);
115     return S_OK;
116 }
117
118 HRESULT WINAPI InitVariantFromGUIDAsString(REFGUID guid, VARIANT *pvar)
119 {
120     TRACE("(%p %p)\n", guid, pvar);
121
122     if(!guid) {
123         FIXME("guid == NULL\n");
124         return E_FAIL;
125     }
126
127     V_VT(pvar) = VT_BSTR;
128     V_BSTR(pvar) = SysAllocStringLen(NULL, 38);
129     if(!V_BSTR(pvar))
130         return E_OUTOFMEMORY;
131
132     PROPVAR_GUIDToWSTR(guid, V_BSTR(pvar));
133     return S_OK;
134 }
135
136 HRESULT WINAPI InitPropVariantFromBuffer(const VOID *pv, UINT cb, PROPVARIANT *ppropvar)
137 {
138     TRACE("(%p %u %p)\n", pv, cb, ppropvar);
139
140     ppropvar->u.caub.pElems = CoTaskMemAlloc(cb);
141     if(!ppropvar->u.caub.pElems)
142         return E_OUTOFMEMORY;
143
144     ppropvar->vt = VT_VECTOR|VT_UI1;
145     ppropvar->u.caub.cElems = cb;
146     memcpy(ppropvar->u.caub.pElems, pv, cb);
147     return S_OK;
148 }
149
150 HRESULT WINAPI InitVariantFromBuffer(const VOID *pv, UINT cb, VARIANT *pvar)
151 {
152     SAFEARRAY *arr;
153     void *data;
154     HRESULT hres;
155
156     TRACE("(%p %u %p)\n", pv, cb, pvar);
157
158     arr = SafeArrayCreateVector(VT_UI1, 0, cb);
159     if(!arr)
160         return E_OUTOFMEMORY;
161
162     hres = SafeArrayAccessData(arr, &data);
163     if(FAILED(hres)) {
164         SafeArrayDestroy(arr);
165         return hres;
166     }
167
168     memcpy(data, pv, cb);
169
170     hres = SafeArrayUnaccessData(arr);
171     if(FAILED(hres)) {
172         SafeArrayDestroy(arr);
173         return hres;
174     }
175
176     V_VT(pvar) = VT_ARRAY|VT_UI1;
177     V_ARRAY(pvar) = arr;
178     return S_OK;
179 }