view src/win32/7zip/7z/CPP/Windows/PropVariant.cpp @ 5:8fe0c57e53d2

concentrating on lua first
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:39:40 -0600
parents f9f4f1b99eed
children
line wrap: on
line source
1 // Windows/PropVariant.cpp
3 #include "StdAfx.h"
5 #include "PropVariant.h"
7 #include "../Common/Defs.h"
9 namespace NWindows {
10 namespace NCOM {
12 CPropVariant::CPropVariant(const PROPVARIANT& varSrc)
13 {
14 vt = VT_EMPTY;
15 InternalCopy(&varSrc);
16 }
18 CPropVariant::CPropVariant(const CPropVariant& varSrc)
19 {
20 vt = VT_EMPTY;
21 InternalCopy(&varSrc);
22 }
24 CPropVariant::CPropVariant(BSTR bstrSrc)
25 {
26 vt = VT_EMPTY;
27 *this = bstrSrc;
28 }
30 CPropVariant::CPropVariant(LPCOLESTR lpszSrc)
31 {
32 vt = VT_EMPTY;
33 *this = lpszSrc;
34 }
36 CPropVariant& CPropVariant::operator=(const CPropVariant& varSrc)
37 {
38 InternalCopy(&varSrc);
39 return *this;
40 }
41 CPropVariant& CPropVariant::operator=(const PROPVARIANT& varSrc)
42 {
43 InternalCopy(&varSrc);
44 return *this;
45 }
47 CPropVariant& CPropVariant::operator=(BSTR bstrSrc)
48 {
49 *this = (LPCOLESTR)bstrSrc;
50 return *this;
51 }
53 CPropVariant& CPropVariant::operator=(LPCOLESTR lpszSrc)
54 {
55 InternalClear();
56 vt = VT_BSTR;
57 wReserved1 = 0;
58 bstrVal = ::SysAllocString(lpszSrc);
59 if (bstrVal == NULL && lpszSrc != NULL)
60 {
61 vt = VT_ERROR;
62 scode = E_OUTOFMEMORY;
63 }
64 return *this;
65 }
68 CPropVariant& CPropVariant::operator=(bool bSrc)
69 {
70 if (vt != VT_BOOL)
71 {
72 InternalClear();
73 vt = VT_BOOL;
74 }
75 boolVal = bSrc ? VARIANT_TRUE : VARIANT_FALSE;
76 return *this;
77 }
79 CPropVariant& CPropVariant::operator=(UInt32 value)
80 {
81 if (vt != VT_UI4)
82 {
83 InternalClear();
84 vt = VT_UI4;
85 }
86 ulVal = value;
87 return *this;
88 }
90 CPropVariant& CPropVariant::operator=(UInt64 value)
91 {
92 if (vt != VT_UI8)
93 {
94 InternalClear();
95 vt = VT_UI8;
96 }
97 uhVal.QuadPart = value;
98 return *this;
99 }
101 CPropVariant& CPropVariant::operator=(const FILETIME &value)
102 {
103 if (vt != VT_FILETIME)
104 {
105 InternalClear();
106 vt = VT_FILETIME;
107 }
108 filetime = value;
109 return *this;
110 }
112 CPropVariant& CPropVariant::operator=(Int32 value)
113 {
114 if (vt != VT_I4)
115 {
116 InternalClear();
117 vt = VT_I4;
118 }
119 lVal = value;
121 return *this;
122 }
124 CPropVariant& CPropVariant::operator=(Byte value)
125 {
126 if (vt != VT_UI1)
127 {
128 InternalClear();
129 vt = VT_UI1;
130 }
131 bVal = value;
132 return *this;
133 }
135 CPropVariant& CPropVariant::operator=(Int16 value)
136 {
137 if (vt != VT_I2)
138 {
139 InternalClear();
140 vt = VT_I2;
141 }
142 iVal = value;
143 return *this;
144 }
146 /*
147 CPropVariant& CPropVariant::operator=(LONG value)
148 {
149 if (vt != VT_I4)
150 {
151 InternalClear();
152 vt = VT_I4;
153 }
154 lVal = value;
155 return *this;
156 }
157 */
159 static HRESULT MyPropVariantClear(PROPVARIANT *propVariant)
160 {
161 switch(propVariant->vt)
162 {
163 case VT_UI1:
164 case VT_I1:
165 case VT_I2:
166 case VT_UI2:
167 case VT_BOOL:
168 case VT_I4:
169 case VT_UI4:
170 case VT_R4:
171 case VT_INT:
172 case VT_UINT:
173 case VT_ERROR:
174 case VT_FILETIME:
175 case VT_UI8:
176 case VT_R8:
177 case VT_CY:
178 case VT_DATE:
179 propVariant->vt = VT_EMPTY;
180 propVariant->wReserved1 = 0;
181 return S_OK;
182 }
183 return ::VariantClear((VARIANTARG *)propVariant);
184 }
186 HRESULT CPropVariant::Clear()
187 {
188 return MyPropVariantClear(this);
189 }
191 HRESULT CPropVariant::Copy(const PROPVARIANT* pSrc)
192 {
193 ::VariantClear((tagVARIANT *)this);
194 switch(pSrc->vt)
195 {
196 case VT_UI1:
197 case VT_I1:
198 case VT_I2:
199 case VT_UI2:
200 case VT_BOOL:
201 case VT_I4:
202 case VT_UI4:
203 case VT_R4:
204 case VT_INT:
205 case VT_UINT:
206 case VT_ERROR:
207 case VT_FILETIME:
208 case VT_UI8:
209 case VT_R8:
210 case VT_CY:
211 case VT_DATE:
212 memmove((PROPVARIANT*)this, pSrc, sizeof(PROPVARIANT));
213 return S_OK;
214 }
215 return ::VariantCopy((tagVARIANT *)this, (tagVARIANT *)(pSrc));
216 }
219 HRESULT CPropVariant::Attach(PROPVARIANT* pSrc)
220 {
221 HRESULT hr = Clear();
222 if (FAILED(hr))
223 return hr;
224 memcpy(this, pSrc, sizeof(PROPVARIANT));
225 pSrc->vt = VT_EMPTY;
226 return S_OK;
227 }
229 HRESULT CPropVariant::Detach(PROPVARIANT* pDest)
230 {
231 HRESULT hr = MyPropVariantClear(pDest);
232 if (FAILED(hr))
233 return hr;
234 memcpy(pDest, this, sizeof(PROPVARIANT));
235 vt = VT_EMPTY;
236 return S_OK;
237 }
239 HRESULT CPropVariant::InternalClear()
240 {
241 HRESULT hr = Clear();
242 if (FAILED(hr))
243 {
244 vt = VT_ERROR;
245 scode = hr;
246 }
247 return hr;
248 }
250 void CPropVariant::InternalCopy(const PROPVARIANT* pSrc)
251 {
252 HRESULT hr = Copy(pSrc);
253 if (FAILED(hr))
254 {
255 vt = VT_ERROR;
256 scode = hr;
257 }
258 }
260 int CPropVariant::Compare(const CPropVariant &a)
261 {
262 if (vt != a.vt)
263 return 0; // it's mean some bug
264 switch (vt)
265 {
266 case VT_EMPTY:
267 return 0;
269 /*
270 case VT_I1:
271 return MyCompare(cVal, a.cVal);
272 */
273 case VT_UI1:
274 return MyCompare(bVal, a.bVal);
276 case VT_I2:
277 return MyCompare(iVal, a.iVal);
278 case VT_UI2:
279 return MyCompare(uiVal, a.uiVal);
281 case VT_I4:
282 return MyCompare(lVal, a.lVal);
283 /*
284 case VT_INT:
285 return MyCompare(intVal, a.intVal);
286 */
287 case VT_UI4:
288 return MyCompare(ulVal, a.ulVal);
289 /*
290 case VT_UINT:
291 return MyCompare(uintVal, a.uintVal);
292 */
293 case VT_I8:
294 return MyCompare(hVal.QuadPart, a.hVal.QuadPart);
295 case VT_UI8:
296 return MyCompare(uhVal.QuadPart, a.uhVal.QuadPart);
298 case VT_BOOL:
299 return -MyCompare(boolVal, a.boolVal);
301 case VT_FILETIME:
302 return ::CompareFileTime(&filetime, &a.filetime);
303 case VT_BSTR:
304 return 0; // Not implemented
305 // return MyCompare(aPropVarint.cVal);
307 default:
308 return 0;
309 }
310 }
312 }}