4 * Copyright 1998 Jean-Claude Cote
5 * Copyright 2003 Jon Griffiths
6 * Copyright 2005 Daniel Remenak
7 * Copyright 2006 Google (Benjamin Arai)
9 * The algorithm for conversion from Julian days to day/month/year is based on
10 * that devised by Henry Fliegel, as implemented in PostgreSQL, which is
11 * Copyright 1994-7 Regents of the University of California
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
40 #include "wine/unicode.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(variant
);
47 static const char * const variant_types
[] =
49 "VT_EMPTY","VT_NULL","VT_I2","VT_I4","VT_R4","VT_R8","VT_CY","VT_DATE",
50 "VT_BSTR","VT_DISPATCH","VT_ERROR","VT_BOOL","VT_VARIANT","VT_UNKNOWN",
51 "VT_DECIMAL","15","VT_I1","VT_UI1","VT_UI2","VT_UI4","VT_I8","VT_UI8",
52 "VT_INT","VT_UINT","VT_VOID","VT_HRESULT","VT_PTR","VT_SAFEARRAY",
53 "VT_CARRAY","VT_USERDEFINED","VT_LPSTR","VT_LPWSTR","32","33","34","35",
54 "VT_RECORD","VT_INT_PTR","VT_UINT_PTR","39","40","41","42","43","44","45",
55 "46","47","48","49","50","51","52","53","54","55","56","57","58","59","60",
56 "61","62","63","VT_FILETIME","VT_BLOB","VT_STREAM","VT_STORAGE",
57 "VT_STREAMED_OBJECT","VT_STORED_OBJECT","VT_BLOB_OBJECT","VT_CF","VT_CLSID",
61 static const char * const variant_flags
[16] =
66 "|VT_VECTOR|VT_ARRAY",
68 "|VT_VECTOR|VT_ARRAY",
70 "|VT_VECTOR|VT_ARRAY|VT_BYREF",
72 "|VT_VECTOR|VT_RESERVED",
73 "|VT_ARRAY|VT_RESERVED",
74 "|VT_VECTOR|VT_ARRAY|VT_RESERVED",
75 "|VT_BYREF|VT_RESERVED",
76 "|VT_VECTOR|VT_ARRAY|VT_RESERVED",
77 "|VT_ARRAY|VT_BYREF|VT_RESERVED",
78 "|VT_VECTOR|VT_ARRAY|VT_BYREF|VT_RESERVED",
81 /* Convert a variant from one type to another */
82 static inline HRESULT
VARIANT_Coerce(VARIANTARG
* pd
, LCID lcid
, USHORT wFlags
,
83 VARIANTARG
* ps
, VARTYPE vt
)
85 HRESULT res
= DISP_E_TYPEMISMATCH
;
86 VARTYPE vtFrom
= V_TYPE(ps
);
89 TRACE("(%s,0x%08x,0x%04x,%s,%s)\n", debugstr_variant(pd
), lcid
, wFlags
,
90 debugstr_variant(ps
), debugstr_vt(vt
));
92 if (vt
== VT_BSTR
|| vtFrom
== VT_BSTR
)
94 /* All flags passed to low level function are only used for
95 * changing to or from strings. Map these here.
97 if (wFlags
& VARIANT_LOCALBOOL
)
98 dwFlags
|= VAR_LOCALBOOL
;
99 if (wFlags
& VARIANT_CALENDAR_HIJRI
)
100 dwFlags
|= VAR_CALENDAR_HIJRI
;
101 if (wFlags
& VARIANT_CALENDAR_THAI
)
102 dwFlags
|= VAR_CALENDAR_THAI
;
103 if (wFlags
& VARIANT_CALENDAR_GREGORIAN
)
104 dwFlags
|= VAR_CALENDAR_GREGORIAN
;
105 if (wFlags
& VARIANT_NOUSEROVERRIDE
)
106 dwFlags
|= LOCALE_NOUSEROVERRIDE
;
107 if (wFlags
& VARIANT_USE_NLS
)
108 dwFlags
|= LOCALE_USE_NLS
;
111 /* Map int/uint to i4/ui4 */
114 else if (vt
== VT_UINT
)
117 if (vtFrom
== VT_INT
)
119 else if (vtFrom
== VT_UINT
)
123 return VariantCopy(pd
, ps
);
125 if (wFlags
& VARIANT_NOVALUEPROP
&& vtFrom
== VT_DISPATCH
&& vt
!= VT_UNKNOWN
)
127 /* VARIANT_NOVALUEPROP prevents IDispatch objects from being coerced by
128 * accessing the default object property.
130 return DISP_E_TYPEMISMATCH
;
136 if (vtFrom
== VT_NULL
)
137 return DISP_E_TYPEMISMATCH
;
138 /* ... Fall through */
140 if (vtFrom
<= VT_UINT
&& vtFrom
!= (VARTYPE
)15 && vtFrom
!= VT_ERROR
)
142 res
= VariantClear( pd
);
143 if (vt
== VT_NULL
&& SUCCEEDED(res
))
151 case VT_EMPTY
: V_I1(pd
) = 0; return S_OK
;
152 case VT_I2
: return VarI1FromI2(V_I2(ps
), &V_I1(pd
));
153 case VT_I4
: return VarI1FromI4(V_I4(ps
), &V_I1(pd
));
154 case VT_UI1
: V_I1(pd
) = V_UI1(ps
); return S_OK
;
155 case VT_UI2
: return VarI1FromUI2(V_UI2(ps
), &V_I1(pd
));
156 case VT_UI4
: return VarI1FromUI4(V_UI4(ps
), &V_I1(pd
));
157 case VT_I8
: return VarI1FromI8(V_I8(ps
), &V_I1(pd
));
158 case VT_UI8
: return VarI1FromUI8(V_UI8(ps
), &V_I1(pd
));
159 case VT_R4
: return VarI1FromR4(V_R4(ps
), &V_I1(pd
));
160 case VT_R8
: return VarI1FromR8(V_R8(ps
), &V_I1(pd
));
161 case VT_DATE
: return VarI1FromDate(V_DATE(ps
), &V_I1(pd
));
162 case VT_BOOL
: return VarI1FromBool(V_BOOL(ps
), &V_I1(pd
));
163 case VT_CY
: return VarI1FromCy(V_CY(ps
), &V_I1(pd
));
164 case VT_DECIMAL
: return VarI1FromDec(&V_DECIMAL(ps
), &V_I1(pd
) );
165 case VT_DISPATCH
: return VarI1FromDisp(V_DISPATCH(ps
), lcid
, &V_I1(pd
) );
166 case VT_BSTR
: return VarI1FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I1(pd
) );
173 case VT_EMPTY
: V_I2(pd
) = 0; return S_OK
;
174 case VT_I1
: return VarI2FromI1(V_I1(ps
), &V_I2(pd
));
175 case VT_I4
: return VarI2FromI4(V_I4(ps
), &V_I2(pd
));
176 case VT_UI1
: return VarI2FromUI1(V_UI1(ps
), &V_I2(pd
));
177 case VT_UI2
: V_I2(pd
) = V_UI2(ps
); return S_OK
;
178 case VT_UI4
: return VarI2FromUI4(V_UI4(ps
), &V_I2(pd
));
179 case VT_I8
: return VarI2FromI8(V_I8(ps
), &V_I2(pd
));
180 case VT_UI8
: return VarI2FromUI8(V_UI8(ps
), &V_I2(pd
));
181 case VT_R4
: return VarI2FromR4(V_R4(ps
), &V_I2(pd
));
182 case VT_R8
: return VarI2FromR8(V_R8(ps
), &V_I2(pd
));
183 case VT_DATE
: return VarI2FromDate(V_DATE(ps
), &V_I2(pd
));
184 case VT_BOOL
: return VarI2FromBool(V_BOOL(ps
), &V_I2(pd
));
185 case VT_CY
: return VarI2FromCy(V_CY(ps
), &V_I2(pd
));
186 case VT_DECIMAL
: return VarI2FromDec(&V_DECIMAL(ps
), &V_I2(pd
));
187 case VT_DISPATCH
: return VarI2FromDisp(V_DISPATCH(ps
), lcid
, &V_I2(pd
));
188 case VT_BSTR
: return VarI2FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I2(pd
));
195 case VT_EMPTY
: V_I4(pd
) = 0; return S_OK
;
196 case VT_I1
: return VarI4FromI1(V_I1(ps
), &V_I4(pd
));
197 case VT_I2
: return VarI4FromI2(V_I2(ps
), &V_I4(pd
));
198 case VT_UI1
: return VarI4FromUI1(V_UI1(ps
), &V_I4(pd
));
199 case VT_UI2
: return VarI4FromUI2(V_UI2(ps
), &V_I4(pd
));
200 case VT_UI4
: V_I4(pd
) = V_UI4(ps
); return S_OK
;
201 case VT_I8
: return VarI4FromI8(V_I8(ps
), &V_I4(pd
));
202 case VT_UI8
: return VarI4FromUI8(V_UI8(ps
), &V_I4(pd
));
203 case VT_R4
: return VarI4FromR4(V_R4(ps
), &V_I4(pd
));
204 case VT_R8
: return VarI4FromR8(V_R8(ps
), &V_I4(pd
));
205 case VT_DATE
: return VarI4FromDate(V_DATE(ps
), &V_I4(pd
));
206 case VT_BOOL
: return VarI4FromBool(V_BOOL(ps
), &V_I4(pd
));
207 case VT_CY
: return VarI4FromCy(V_CY(ps
), &V_I4(pd
));
208 case VT_DECIMAL
: return VarI4FromDec(&V_DECIMAL(ps
), &V_I4(pd
));
209 case VT_DISPATCH
: return VarI4FromDisp(V_DISPATCH(ps
), lcid
, &V_I4(pd
));
210 case VT_BSTR
: return VarI4FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I4(pd
));
217 case VT_EMPTY
: V_UI1(pd
) = 0; return S_OK
;
218 case VT_I1
: V_UI1(pd
) = V_I1(ps
); return S_OK
;
219 case VT_I2
: return VarUI1FromI2(V_I2(ps
), &V_UI1(pd
));
220 case VT_I4
: return VarUI1FromI4(V_I4(ps
), &V_UI1(pd
));
221 case VT_UI2
: return VarUI1FromUI2(V_UI2(ps
), &V_UI1(pd
));
222 case VT_UI4
: return VarUI1FromUI4(V_UI4(ps
), &V_UI1(pd
));
223 case VT_I8
: return VarUI1FromI8(V_I8(ps
), &V_UI1(pd
));
224 case VT_UI8
: return VarUI1FromUI8(V_UI8(ps
), &V_UI1(pd
));
225 case VT_R4
: return VarUI1FromR4(V_R4(ps
), &V_UI1(pd
));
226 case VT_R8
: return VarUI1FromR8(V_R8(ps
), &V_UI1(pd
));
227 case VT_DATE
: return VarUI1FromDate(V_DATE(ps
), &V_UI1(pd
));
228 case VT_BOOL
: return VarUI1FromBool(V_BOOL(ps
), &V_UI1(pd
));
229 case VT_CY
: return VarUI1FromCy(V_CY(ps
), &V_UI1(pd
));
230 case VT_DECIMAL
: return VarUI1FromDec(&V_DECIMAL(ps
), &V_UI1(pd
));
231 case VT_DISPATCH
: return VarUI1FromDisp(V_DISPATCH(ps
), lcid
, &V_UI1(pd
));
232 case VT_BSTR
: return VarUI1FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI1(pd
));
239 case VT_EMPTY
: V_UI2(pd
) = 0; return S_OK
;
240 case VT_I1
: return VarUI2FromI1(V_I1(ps
), &V_UI2(pd
));
241 case VT_I2
: V_UI2(pd
) = V_I2(ps
); return S_OK
;
242 case VT_I4
: return VarUI2FromI4(V_I4(ps
), &V_UI2(pd
));
243 case VT_UI1
: return VarUI2FromUI1(V_UI1(ps
), &V_UI2(pd
));
244 case VT_UI4
: return VarUI2FromUI4(V_UI4(ps
), &V_UI2(pd
));
245 case VT_I8
: return VarUI4FromI8(V_I8(ps
), &V_UI4(pd
));
246 case VT_UI8
: return VarUI4FromUI8(V_UI8(ps
), &V_UI4(pd
));
247 case VT_R4
: return VarUI2FromR4(V_R4(ps
), &V_UI2(pd
));
248 case VT_R8
: return VarUI2FromR8(V_R8(ps
), &V_UI2(pd
));
249 case VT_DATE
: return VarUI2FromDate(V_DATE(ps
), &V_UI2(pd
));
250 case VT_BOOL
: return VarUI2FromBool(V_BOOL(ps
), &V_UI2(pd
));
251 case VT_CY
: return VarUI2FromCy(V_CY(ps
), &V_UI2(pd
));
252 case VT_DECIMAL
: return VarUI2FromDec(&V_DECIMAL(ps
), &V_UI2(pd
));
253 case VT_DISPATCH
: return VarUI2FromDisp(V_DISPATCH(ps
), lcid
, &V_UI2(pd
));
254 case VT_BSTR
: return VarUI2FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI2(pd
));
261 case VT_EMPTY
: V_UI4(pd
) = 0; return S_OK
;
262 case VT_I1
: return VarUI4FromI1(V_I1(ps
), &V_UI4(pd
));
263 case VT_I2
: return VarUI4FromI2(V_I2(ps
), &V_UI4(pd
));
264 case VT_I4
: V_UI4(pd
) = V_I4(ps
); return S_OK
;
265 case VT_UI1
: return VarUI4FromUI1(V_UI1(ps
), &V_UI4(pd
));
266 case VT_UI2
: return VarUI4FromUI2(V_UI2(ps
), &V_UI4(pd
));
267 case VT_I8
: return VarUI4FromI8(V_I8(ps
), &V_UI4(pd
));
268 case VT_UI8
: return VarUI4FromUI8(V_UI8(ps
), &V_UI4(pd
));
269 case VT_R4
: return VarUI4FromR4(V_R4(ps
), &V_UI4(pd
));
270 case VT_R8
: return VarUI4FromR8(V_R8(ps
), &V_UI4(pd
));
271 case VT_DATE
: return VarUI4FromDate(V_DATE(ps
), &V_UI4(pd
));
272 case VT_BOOL
: return VarUI4FromBool(V_BOOL(ps
), &V_UI4(pd
));
273 case VT_CY
: return VarUI4FromCy(V_CY(ps
), &V_UI4(pd
));
274 case VT_DECIMAL
: return VarUI4FromDec(&V_DECIMAL(ps
), &V_UI4(pd
));
275 case VT_DISPATCH
: return VarUI4FromDisp(V_DISPATCH(ps
), lcid
, &V_UI4(pd
));
276 case VT_BSTR
: return VarUI4FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI4(pd
));
283 case VT_EMPTY
: V_UI8(pd
) = 0; return S_OK
;
284 case VT_I4
: if (V_I4(ps
) < 0) return DISP_E_OVERFLOW
; V_UI8(pd
) = V_I4(ps
); return S_OK
;
285 case VT_I1
: return VarUI8FromI1(V_I1(ps
), &V_UI8(pd
));
286 case VT_I2
: return VarUI8FromI2(V_I2(ps
), &V_UI8(pd
));
287 case VT_UI1
: return VarUI8FromUI1(V_UI1(ps
), &V_UI8(pd
));
288 case VT_UI2
: return VarUI8FromUI2(V_UI2(ps
), &V_UI8(pd
));
289 case VT_UI4
: return VarUI8FromUI4(V_UI4(ps
), &V_UI8(pd
));
290 case VT_I8
: V_UI8(pd
) = V_I8(ps
); return S_OK
;
291 case VT_R4
: return VarUI8FromR4(V_R4(ps
), &V_UI8(pd
));
292 case VT_R8
: return VarUI8FromR8(V_R8(ps
), &V_UI8(pd
));
293 case VT_DATE
: return VarUI8FromDate(V_DATE(ps
), &V_UI8(pd
));
294 case VT_BOOL
: return VarUI8FromBool(V_BOOL(ps
), &V_UI8(pd
));
295 case VT_CY
: return VarUI8FromCy(V_CY(ps
), &V_UI8(pd
));
296 case VT_DECIMAL
: return VarUI8FromDec(&V_DECIMAL(ps
), &V_UI8(pd
));
297 case VT_DISPATCH
: return VarUI8FromDisp(V_DISPATCH(ps
), lcid
, &V_UI8(pd
));
298 case VT_BSTR
: return VarUI8FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI8(pd
));
305 case VT_EMPTY
: V_I8(pd
) = 0; return S_OK
;
306 case VT_I4
: V_I8(pd
) = V_I4(ps
); return S_OK
;
307 case VT_I1
: return VarI8FromI1(V_I1(ps
), &V_I8(pd
));
308 case VT_I2
: return VarI8FromI2(V_I2(ps
), &V_I8(pd
));
309 case VT_UI1
: return VarI8FromUI1(V_UI1(ps
), &V_I8(pd
));
310 case VT_UI2
: return VarI8FromUI2(V_UI2(ps
), &V_I8(pd
));
311 case VT_UI4
: return VarI8FromUI4(V_UI4(ps
), &V_I8(pd
));
312 case VT_UI8
: V_I8(pd
) = V_UI8(ps
); return S_OK
;
313 case VT_R4
: return VarI8FromR4(V_R4(ps
), &V_I8(pd
));
314 case VT_R8
: return VarI8FromR8(V_R8(ps
), &V_I8(pd
));
315 case VT_DATE
: return VarI8FromDate(V_DATE(ps
), &V_I8(pd
));
316 case VT_BOOL
: return VarI8FromBool(V_BOOL(ps
), &V_I8(pd
));
317 case VT_CY
: return VarI8FromCy(V_CY(ps
), &V_I8(pd
));
318 case VT_DECIMAL
: return VarI8FromDec(&V_DECIMAL(ps
), &V_I8(pd
));
319 case VT_DISPATCH
: return VarI8FromDisp(V_DISPATCH(ps
), lcid
, &V_I8(pd
));
320 case VT_BSTR
: return VarI8FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I8(pd
));
327 case VT_EMPTY
: V_R4(pd
) = 0.0f
; return S_OK
;
328 case VT_I1
: return VarR4FromI1(V_I1(ps
), &V_R4(pd
));
329 case VT_I2
: return VarR4FromI2(V_I2(ps
), &V_R4(pd
));
330 case VT_I4
: return VarR4FromI4(V_I4(ps
), &V_R4(pd
));
331 case VT_UI1
: return VarR4FromUI1(V_UI1(ps
), &V_R4(pd
));
332 case VT_UI2
: return VarR4FromUI2(V_UI2(ps
), &V_R4(pd
));
333 case VT_UI4
: return VarR4FromUI4(V_UI4(ps
), &V_R4(pd
));
334 case VT_I8
: return VarR4FromI8(V_I8(ps
), &V_R4(pd
));
335 case VT_UI8
: return VarR4FromUI8(V_UI8(ps
), &V_R4(pd
));
336 case VT_R8
: return VarR4FromR8(V_R8(ps
), &V_R4(pd
));
337 case VT_DATE
: return VarR4FromDate(V_DATE(ps
), &V_R4(pd
));
338 case VT_BOOL
: return VarR4FromBool(V_BOOL(ps
), &V_R4(pd
));
339 case VT_CY
: return VarR4FromCy(V_CY(ps
), &V_R4(pd
));
340 case VT_DECIMAL
: return VarR4FromDec(&V_DECIMAL(ps
), &V_R4(pd
));
341 case VT_DISPATCH
: return VarR4FromDisp(V_DISPATCH(ps
), lcid
, &V_R4(pd
));
342 case VT_BSTR
: return VarR4FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_R4(pd
));
349 case VT_EMPTY
: V_R8(pd
) = 0.0; return S_OK
;
350 case VT_I1
: return VarR8FromI1(V_I1(ps
), &V_R8(pd
));
351 case VT_I2
: return VarR8FromI2(V_I2(ps
), &V_R8(pd
));
352 case VT_I4
: return VarR8FromI4(V_I4(ps
), &V_R8(pd
));
353 case VT_UI1
: return VarR8FromUI1(V_UI1(ps
), &V_R8(pd
));
354 case VT_UI2
: return VarR8FromUI2(V_UI2(ps
), &V_R8(pd
));
355 case VT_UI4
: return VarR8FromUI4(V_UI4(ps
), &V_R8(pd
));
356 case VT_I8
: return VarR8FromI8(V_I8(ps
), &V_R8(pd
));
357 case VT_UI8
: return VarR8FromUI8(V_UI8(ps
), &V_R8(pd
));
358 case VT_R4
: return VarR8FromR4(V_R4(ps
), &V_R8(pd
));
359 case VT_DATE
: return VarR8FromDate(V_DATE(ps
), &V_R8(pd
));
360 case VT_BOOL
: return VarR8FromBool(V_BOOL(ps
), &V_R8(pd
));
361 case VT_CY
: return VarR8FromCy(V_CY(ps
), &V_R8(pd
));
362 case VT_DECIMAL
: return VarR8FromDec(&V_DECIMAL(ps
), &V_R8(pd
));
363 case VT_DISPATCH
: return VarR8FromDisp(V_DISPATCH(ps
), lcid
, &V_R8(pd
));
364 case VT_BSTR
: return VarR8FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_R8(pd
));
371 case VT_EMPTY
: V_DATE(pd
) = 0.0; return S_OK
;
372 case VT_I1
: return VarDateFromI1(V_I1(ps
), &V_DATE(pd
));
373 case VT_I2
: return VarDateFromI2(V_I2(ps
), &V_DATE(pd
));
374 case VT_I4
: return VarDateFromI4(V_I4(ps
), &V_DATE(pd
));
375 case VT_UI1
: return VarDateFromUI1(V_UI1(ps
), &V_DATE(pd
));
376 case VT_UI2
: return VarDateFromUI2(V_UI2(ps
), &V_DATE(pd
));
377 case VT_UI4
: return VarDateFromUI4(V_UI4(ps
), &V_DATE(pd
));
378 case VT_I8
: return VarDateFromI8(V_I8(ps
), &V_DATE(pd
));
379 case VT_UI8
: return VarDateFromUI8(V_UI8(ps
), &V_DATE(pd
));
380 case VT_R4
: return VarDateFromR4(V_R4(ps
), &V_DATE(pd
));
381 case VT_R8
: return VarDateFromR8(V_R8(ps
), &V_DATE(pd
));
382 case VT_BOOL
: return VarDateFromBool(V_BOOL(ps
), &V_DATE(pd
));
383 case VT_CY
: return VarDateFromCy(V_CY(ps
), &V_DATE(pd
));
384 case VT_DECIMAL
: return VarDateFromDec(&V_DECIMAL(ps
), &V_DATE(pd
));
385 case VT_DISPATCH
: return VarDateFromDisp(V_DISPATCH(ps
), lcid
, &V_DATE(pd
));
386 case VT_BSTR
: return VarDateFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_DATE(pd
));
393 case VT_EMPTY
: V_BOOL(pd
) = 0; return S_OK
;
394 case VT_I1
: return VarBoolFromI1(V_I1(ps
), &V_BOOL(pd
));
395 case VT_I2
: return VarBoolFromI2(V_I2(ps
), &V_BOOL(pd
));
396 case VT_I4
: return VarBoolFromI4(V_I4(ps
), &V_BOOL(pd
));
397 case VT_UI1
: return VarBoolFromUI1(V_UI1(ps
), &V_BOOL(pd
));
398 case VT_UI2
: return VarBoolFromUI2(V_UI2(ps
), &V_BOOL(pd
));
399 case VT_UI4
: return VarBoolFromUI4(V_UI4(ps
), &V_BOOL(pd
));
400 case VT_I8
: return VarBoolFromI8(V_I8(ps
), &V_BOOL(pd
));
401 case VT_UI8
: return VarBoolFromUI8(V_UI8(ps
), &V_BOOL(pd
));
402 case VT_R4
: return VarBoolFromR4(V_R4(ps
), &V_BOOL(pd
));
403 case VT_R8
: return VarBoolFromR8(V_R8(ps
), &V_BOOL(pd
));
404 case VT_DATE
: return VarBoolFromDate(V_DATE(ps
), &V_BOOL(pd
));
405 case VT_CY
: return VarBoolFromCy(V_CY(ps
), &V_BOOL(pd
));
406 case VT_DECIMAL
: return VarBoolFromDec(&V_DECIMAL(ps
), &V_BOOL(pd
));
407 case VT_DISPATCH
: return VarBoolFromDisp(V_DISPATCH(ps
), lcid
, &V_BOOL(pd
));
408 case VT_BSTR
: return VarBoolFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_BOOL(pd
));
416 V_BSTR(pd
) = SysAllocStringLen(NULL
, 0);
417 return V_BSTR(pd
) ? S_OK
: E_OUTOFMEMORY
;
419 if (wFlags
& (VARIANT_ALPHABOOL
|VARIANT_LOCALBOOL
))
420 return VarBstrFromBool(V_BOOL(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
421 return VarBstrFromI2(V_BOOL(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
422 case VT_I1
: return VarBstrFromI1(V_I1(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
423 case VT_I2
: return VarBstrFromI2(V_I2(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
424 case VT_I4
: return VarBstrFromI4(V_I4(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
425 case VT_UI1
: return VarBstrFromUI1(V_UI1(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
426 case VT_UI2
: return VarBstrFromUI2(V_UI2(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
427 case VT_UI4
: return VarBstrFromUI4(V_UI4(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
428 case VT_I8
: return VarBstrFromI8(V_I8(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
429 case VT_UI8
: return VarBstrFromUI8(V_UI8(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
430 case VT_R4
: return VarBstrFromR4(V_R4(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
431 case VT_R8
: return VarBstrFromR8(V_R8(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
432 case VT_DATE
: return VarBstrFromDate(V_DATE(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
433 case VT_CY
: return VarBstrFromCy(V_CY(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
434 case VT_DECIMAL
: return VarBstrFromDec(&V_DECIMAL(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
435 case VT_DISPATCH
: return VarBstrFromDisp(V_DISPATCH(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
442 case VT_EMPTY
: V_CY(pd
).int64
= 0; return S_OK
;
443 case VT_I1
: return VarCyFromI1(V_I1(ps
), &V_CY(pd
));
444 case VT_I2
: return VarCyFromI2(V_I2(ps
), &V_CY(pd
));
445 case VT_I4
: return VarCyFromI4(V_I4(ps
), &V_CY(pd
));
446 case VT_UI1
: return VarCyFromUI1(V_UI1(ps
), &V_CY(pd
));
447 case VT_UI2
: return VarCyFromUI2(V_UI2(ps
), &V_CY(pd
));
448 case VT_UI4
: return VarCyFromUI4(V_UI4(ps
), &V_CY(pd
));
449 case VT_I8
: return VarCyFromI8(V_I8(ps
), &V_CY(pd
));
450 case VT_UI8
: return VarCyFromUI8(V_UI8(ps
), &V_CY(pd
));
451 case VT_R4
: return VarCyFromR4(V_R4(ps
), &V_CY(pd
));
452 case VT_R8
: return VarCyFromR8(V_R8(ps
), &V_CY(pd
));
453 case VT_DATE
: return VarCyFromDate(V_DATE(ps
), &V_CY(pd
));
454 case VT_BOOL
: return VarCyFromBool(V_BOOL(ps
), &V_CY(pd
));
455 case VT_DECIMAL
: return VarCyFromDec(&V_DECIMAL(ps
), &V_CY(pd
));
456 case VT_DISPATCH
: return VarCyFromDisp(V_DISPATCH(ps
), lcid
, &V_CY(pd
));
457 case VT_BSTR
: return VarCyFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_CY(pd
));
466 DEC_SIGNSCALE(&V_DECIMAL(pd
)) = SIGNSCALE(DECIMAL_POS
,0);
467 DEC_HI32(&V_DECIMAL(pd
)) = 0;
468 DEC_MID32(&V_DECIMAL(pd
)) = 0;
469 /* VarDecFromBool() coerces to -1/0, ChangeTypeEx() coerces to 1/0.
470 * VT_NULL and VT_EMPTY always give a 0 value.
472 DEC_LO32(&V_DECIMAL(pd
)) = vtFrom
== VT_BOOL
&& V_BOOL(ps
) ? 1 : 0;
474 case VT_I1
: return VarDecFromI1(V_I1(ps
), &V_DECIMAL(pd
));
475 case VT_I2
: return VarDecFromI2(V_I2(ps
), &V_DECIMAL(pd
));
476 case VT_I4
: return VarDecFromI4(V_I4(ps
), &V_DECIMAL(pd
));
477 case VT_UI1
: return VarDecFromUI1(V_UI1(ps
), &V_DECIMAL(pd
));
478 case VT_UI2
: return VarDecFromUI2(V_UI2(ps
), &V_DECIMAL(pd
));
479 case VT_UI4
: return VarDecFromUI4(V_UI4(ps
), &V_DECIMAL(pd
));
480 case VT_I8
: return VarDecFromI8(V_I8(ps
), &V_DECIMAL(pd
));
481 case VT_UI8
: return VarDecFromUI8(V_UI8(ps
), &V_DECIMAL(pd
));
482 case VT_R4
: return VarDecFromR4(V_R4(ps
), &V_DECIMAL(pd
));
483 case VT_R8
: return VarDecFromR8(V_R8(ps
), &V_DECIMAL(pd
));
484 case VT_DATE
: return VarDecFromDate(V_DATE(ps
), &V_DECIMAL(pd
));
485 case VT_CY
: return VarDecFromCy(V_CY(ps
), &V_DECIMAL(pd
));
486 case VT_DISPATCH
: return VarDecFromDisp(V_DISPATCH(ps
), lcid
, &V_DECIMAL(pd
));
487 case VT_BSTR
: return VarDecFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_DECIMAL(pd
));
495 if (V_DISPATCH(ps
) == NULL
)
496 V_UNKNOWN(pd
) = NULL
;
498 res
= IDispatch_QueryInterface(V_DISPATCH(ps
), &IID_IUnknown
, (LPVOID
*)&V_UNKNOWN(pd
));
507 if (V_UNKNOWN(ps
) == NULL
)
508 V_DISPATCH(pd
) = NULL
;
510 res
= IUnknown_QueryInterface(V_UNKNOWN(ps
), &IID_IDispatch
, (LPVOID
*)&V_DISPATCH(pd
));
521 /* Coerce to/from an array */
522 static inline HRESULT
VARIANT_CoerceArray(VARIANTARG
* pd
, VARIANTARG
* ps
, VARTYPE vt
)
524 if (vt
== VT_BSTR
&& V_VT(ps
) == (VT_ARRAY
|VT_UI1
))
525 return BstrFromVector(V_ARRAY(ps
), &V_BSTR(pd
));
527 if (V_VT(ps
) == VT_BSTR
&& vt
== (VT_ARRAY
|VT_UI1
))
528 return VectorFromBstr(V_BSTR(ps
), &V_ARRAY(pd
));
531 return SafeArrayCopy(V_ARRAY(ps
), &V_ARRAY(pd
));
533 return DISP_E_TYPEMISMATCH
;
536 /******************************************************************************
537 * Check if a variants type is valid.
539 static inline HRESULT
VARIANT_ValidateType(VARTYPE vt
)
541 VARTYPE vtExtra
= vt
& VT_EXTRA_TYPE
;
545 if (!(vtExtra
& (VT_VECTOR
|VT_RESERVED
)))
547 if (vt
< VT_VOID
|| vt
== VT_RECORD
|| vt
== VT_CLSID
)
549 if ((vtExtra
& (VT_BYREF
|VT_ARRAY
)) && vt
<= VT_NULL
)
550 return DISP_E_BADVARTYPE
;
551 if (vt
!= (VARTYPE
)15)
555 return DISP_E_BADVARTYPE
;
558 /******************************************************************************
559 * VariantInit [OLEAUT32.8]
561 * Initialise a variant.
564 * pVarg [O] Variant to initialise
570 * This function simply sets the type of the variant to VT_EMPTY. It does not
571 * free any existing value, use VariantClear() for that.
573 void WINAPI
VariantInit(VARIANTARG
* pVarg
)
575 TRACE("(%p)\n", pVarg
);
577 /* Win8.1 zeroes whole struct. Previous implementations don't set any other fields. */
578 V_VT(pVarg
) = VT_EMPTY
;
581 HRESULT
VARIANT_ClearInd(VARIANTARG
*pVarg
)
585 TRACE("(%s)\n", debugstr_variant(pVarg
));
587 hres
= VARIANT_ValidateType(V_VT(pVarg
));
595 if (V_UNKNOWN(pVarg
))
596 IUnknown_Release(V_UNKNOWN(pVarg
));
598 case VT_UNKNOWN
| VT_BYREF
:
599 case VT_DISPATCH
| VT_BYREF
:
600 if(*V_UNKNOWNREF(pVarg
))
601 IUnknown_Release(*V_UNKNOWNREF(pVarg
));
604 SysFreeString(V_BSTR(pVarg
));
606 case VT_BSTR
| VT_BYREF
:
607 SysFreeString(*V_BSTRREF(pVarg
));
609 case VT_VARIANT
| VT_BYREF
:
610 VariantClear(V_VARIANTREF(pVarg
));
613 case VT_RECORD
| VT_BYREF
:
615 struct __tagBRECORD
* pBr
= &V_UNION(pVarg
,brecVal
);
618 IRecordInfo_RecordClear(pBr
->pRecInfo
, pBr
->pvRecord
);
619 IRecordInfo_Release(pBr
->pRecInfo
);
624 if (V_ISARRAY(pVarg
) || (V_VT(pVarg
) & ~VT_BYREF
) == VT_SAFEARRAY
)
626 if (V_ISBYREF(pVarg
))
628 if (*V_ARRAYREF(pVarg
))
629 hres
= SafeArrayDestroy(*V_ARRAYREF(pVarg
));
631 else if (V_ARRAY(pVarg
))
632 hres
= SafeArrayDestroy(V_ARRAY(pVarg
));
637 V_VT(pVarg
) = VT_EMPTY
;
641 /******************************************************************************
642 * VariantClear [OLEAUT32.9]
647 * pVarg [I/O] Variant to clear
650 * Success: S_OK. Any previous value in pVarg is freed and its type is set to VT_EMPTY.
651 * Failure: DISP_E_BADVARTYPE, if the variant is not a valid variant type.
653 HRESULT WINAPI
VariantClear(VARIANTARG
* pVarg
)
657 TRACE("(%s)\n", debugstr_variant(pVarg
));
659 hres
= VARIANT_ValidateType(V_VT(pVarg
));
663 if (!V_ISBYREF(pVarg
))
665 if (V_ISARRAY(pVarg
) || V_VT(pVarg
) == VT_SAFEARRAY
)
667 hres
= SafeArrayDestroy(V_ARRAY(pVarg
));
669 else if (V_VT(pVarg
) == VT_BSTR
)
671 SysFreeString(V_BSTR(pVarg
));
673 else if (V_VT(pVarg
) == VT_RECORD
)
675 struct __tagBRECORD
* pBr
= &V_UNION(pVarg
,brecVal
);
678 IRecordInfo_RecordClear(pBr
->pRecInfo
, pBr
->pvRecord
);
679 IRecordInfo_Release(pBr
->pRecInfo
);
682 else if (V_VT(pVarg
) == VT_DISPATCH
||
683 V_VT(pVarg
) == VT_UNKNOWN
)
685 if (V_UNKNOWN(pVarg
))
686 IUnknown_Release(V_UNKNOWN(pVarg
));
689 V_VT(pVarg
) = VT_EMPTY
;
694 /******************************************************************************
695 * Copy an IRecordInfo object contained in a variant.
697 static HRESULT
VARIANT_CopyIRecordInfo(VARIANT
*dest
, VARIANT
*src
)
699 struct __tagBRECORD
*dest_rec
= &V_UNION(dest
, brecVal
);
700 struct __tagBRECORD
*src_rec
= &V_UNION(src
, brecVal
);
704 if (!src_rec
->pRecInfo
)
706 if (src_rec
->pvRecord
) return E_INVALIDARG
;
710 hr
= IRecordInfo_GetSize(src_rec
->pRecInfo
, &size
);
711 if (FAILED(hr
)) return hr
;
713 /* This could look cleaner if only RecordCreate() was used, but native doesn't use it.
714 Memory should be allocated in a same way as RecordCreate() does, so RecordDestroy()
715 could free it later. */
716 dest_rec
->pvRecord
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, size
);
717 if (!dest_rec
->pvRecord
) return E_OUTOFMEMORY
;
719 dest_rec
->pRecInfo
= src_rec
->pRecInfo
;
720 IRecordInfo_AddRef(src_rec
->pRecInfo
);
722 return IRecordInfo_RecordCopy(src_rec
->pRecInfo
, src_rec
->pvRecord
, dest_rec
->pvRecord
);
725 /******************************************************************************
726 * VariantCopy [OLEAUT32.10]
731 * pvargDest [O] Destination for copy
732 * pvargSrc [I] Source variant to copy
735 * Success: S_OK. pvargDest contains a copy of pvargSrc.
736 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid type.
737 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
738 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
739 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
742 * - If pvargSrc == pvargDest, this function does nothing, and succeeds if
743 * pvargSrc is valid. Otherwise, pvargDest is always cleared using
744 * VariantClear() before pvargSrc is copied to it. If clearing pvargDest
745 * fails, so does this function.
746 * - VT_CLSID is a valid type type for pvargSrc, but not for pvargDest.
747 * - For by-value non-intrinsic types, a deep copy is made, i.e. The whole value
748 * is copied rather than just any pointers to it.
749 * - For by-value object types the object pointer is copied and the objects
750 * reference count increased using IUnknown_AddRef().
751 * - For all by-reference types, only the referencing pointer is copied.
753 HRESULT WINAPI
VariantCopy(VARIANTARG
* pvargDest
, VARIANTARG
* pvargSrc
)
757 TRACE("(%s,%s)\n", debugstr_variant(pvargDest
), debugstr_variant(pvargSrc
));
759 if (V_TYPE(pvargSrc
) == VT_CLSID
|| /* VT_CLSID is a special case */
760 FAILED(VARIANT_ValidateType(V_VT(pvargSrc
))))
761 return DISP_E_BADVARTYPE
;
763 if (pvargSrc
!= pvargDest
&&
764 SUCCEEDED(hres
= VariantClear(pvargDest
)))
766 *pvargDest
= *pvargSrc
; /* Shallow copy the value */
768 if (!V_ISBYREF(pvargSrc
))
770 switch (V_VT(pvargSrc
))
773 V_BSTR(pvargDest
) = SysAllocStringByteLen((char*)V_BSTR(pvargSrc
), SysStringByteLen(V_BSTR(pvargSrc
)));
774 if (!V_BSTR(pvargDest
))
775 hres
= E_OUTOFMEMORY
;
778 hres
= VARIANT_CopyIRecordInfo(pvargDest
, pvargSrc
);
782 V_UNKNOWN(pvargDest
) = V_UNKNOWN(pvargSrc
);
783 if (V_UNKNOWN(pvargSrc
))
784 IUnknown_AddRef(V_UNKNOWN(pvargSrc
));
787 if (V_ISARRAY(pvargSrc
))
788 hres
= SafeArrayCopy(V_ARRAY(pvargSrc
), &V_ARRAY(pvargDest
));
795 /* Return the byte size of a variants data */
796 static inline size_t VARIANT_DataSize(const VARIANT
* pv
)
801 case VT_UI1
: return sizeof(BYTE
);
803 case VT_UI2
: return sizeof(SHORT
);
807 case VT_UI4
: return sizeof(LONG
);
809 case VT_UI8
: return sizeof(LONGLONG
);
810 case VT_R4
: return sizeof(float);
811 case VT_R8
: return sizeof(double);
812 case VT_DATE
: return sizeof(DATE
);
813 case VT_BOOL
: return sizeof(VARIANT_BOOL
);
816 case VT_BSTR
: return sizeof(void*);
817 case VT_CY
: return sizeof(CY
);
818 case VT_ERROR
: return sizeof(SCODE
);
820 TRACE("Shouldn't be called for variant %s!\n", debugstr_variant(pv
));
824 /******************************************************************************
825 * VariantCopyInd [OLEAUT32.11]
827 * Copy a variant, dereferencing it if it is by-reference.
830 * pvargDest [O] Destination for copy
831 * pvargSrc [I] Source variant to copy
834 * Success: S_OK. pvargDest contains a copy of pvargSrc.
835 * Failure: An HRESULT error code indicating the error.
838 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid by-value type.
839 * E_INVALIDARG, if pvargSrc is an invalid by-reference type.
840 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
841 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
842 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
845 * - If pvargSrc is by-value, this function behaves exactly as VariantCopy().
846 * - If pvargSrc is by-reference, the value copied to pvargDest is the pointed-to
848 * - if pvargSrc == pvargDest, this function dereferences in place. Otherwise,
849 * pvargDest is always cleared using VariantClear() before pvargSrc is copied
850 * to it. If clearing pvargDest fails, so does this function.
852 HRESULT WINAPI
VariantCopyInd(VARIANT
* pvargDest
, VARIANTARG
* pvargSrc
)
854 VARIANTARG vTmp
, *pSrc
= pvargSrc
;
858 TRACE("(%s,%s)\n", debugstr_variant(pvargDest
), debugstr_variant(pvargSrc
));
860 if (!V_ISBYREF(pvargSrc
))
861 return VariantCopy(pvargDest
, pvargSrc
);
863 /* Argument checking is more lax than VariantCopy()... */
864 vt
= V_TYPE(pvargSrc
);
865 if (V_ISARRAY(pvargSrc
) || (V_VT(pvargSrc
) == (VT_RECORD
|VT_BYREF
)) ||
866 (vt
> VT_NULL
&& vt
!= (VARTYPE
)15 && vt
< VT_VOID
&&
867 !(V_VT(pvargSrc
) & (VT_VECTOR
|VT_RESERVED
))))
872 return E_INVALIDARG
; /* ...And the return value for invalid types differs too */
874 if (pvargSrc
== pvargDest
)
876 /* In place copy. Use a shallow copy of pvargSrc & init pvargDest.
877 * This avoids an expensive VariantCopy() call - e.g. SafeArrayCopy().
881 V_VT(pvargDest
) = VT_EMPTY
;
885 /* Copy into another variant. Free the variant in pvargDest */
886 if (FAILED(hres
= VariantClear(pvargDest
)))
888 TRACE("VariantClear() of destination failed\n");
895 /* Native doesn't check that *V_ARRAYREF(pSrc) is valid */
896 hres
= SafeArrayCopy(*V_ARRAYREF(pSrc
), &V_ARRAY(pvargDest
));
898 else if (V_VT(pSrc
) == (VT_BSTR
|VT_BYREF
))
900 /* Native doesn't check that *V_BSTRREF(pSrc) is valid */
901 V_BSTR(pvargDest
) = SysAllocStringByteLen((char*)*V_BSTRREF(pSrc
), SysStringByteLen(*V_BSTRREF(pSrc
)));
903 else if (V_VT(pSrc
) == (VT_RECORD
|VT_BYREF
))
905 hres
= VARIANT_CopyIRecordInfo(pvargDest
, pvargSrc
);
907 else if (V_VT(pSrc
) == (VT_DISPATCH
|VT_BYREF
) ||
908 V_VT(pSrc
) == (VT_UNKNOWN
|VT_BYREF
))
910 /* Native doesn't check that *V_UNKNOWNREF(pSrc) is valid */
911 V_UNKNOWN(pvargDest
) = *V_UNKNOWNREF(pSrc
);
912 if (*V_UNKNOWNREF(pSrc
))
913 IUnknown_AddRef(*V_UNKNOWNREF(pSrc
));
915 else if (V_VT(pSrc
) == (VT_VARIANT
|VT_BYREF
))
917 /* Native doesn't check that *V_VARIANTREF(pSrc) is valid */
918 if (V_VT(V_VARIANTREF(pSrc
)) == (VT_VARIANT
|VT_BYREF
))
919 hres
= E_INVALIDARG
; /* Don't dereference more than one level */
921 hres
= VariantCopyInd(pvargDest
, V_VARIANTREF(pSrc
));
923 /* Use the dereferenced variants type value, not VT_VARIANT */
924 goto VariantCopyInd_Return
;
926 else if (V_VT(pSrc
) == (VT_DECIMAL
|VT_BYREF
))
928 memcpy(&DEC_SCALE(&V_DECIMAL(pvargDest
)), &DEC_SCALE(V_DECIMALREF(pSrc
)),
929 sizeof(DECIMAL
) - sizeof(USHORT
));
933 /* Copy the pointed to data into this variant */
934 memcpy(&V_BYREF(pvargDest
), V_BYREF(pSrc
), VARIANT_DataSize(pSrc
));
937 V_VT(pvargDest
) = V_VT(pSrc
) & ~VT_BYREF
;
939 VariantCopyInd_Return
:
941 if (pSrc
!= pvargSrc
)
944 TRACE("returning 0x%08x, %s\n", hres
, debugstr_variant(pvargDest
));
948 /******************************************************************************
949 * VariantChangeType [OLEAUT32.12]
951 * Change the type of a variant.
954 * pvargDest [O] Destination for the converted variant
955 * pvargSrc [O] Source variant to change the type of
956 * wFlags [I] VARIANT_ flags from "oleauto.h"
957 * vt [I] Variant type to change pvargSrc into
960 * Success: S_OK. pvargDest contains the converted value.
961 * Failure: An HRESULT error code describing the failure.
964 * The LCID used for the conversion is LOCALE_USER_DEFAULT.
965 * See VariantChangeTypeEx.
967 HRESULT WINAPI
VariantChangeType(VARIANTARG
* pvargDest
, VARIANTARG
* pvargSrc
,
968 USHORT wFlags
, VARTYPE vt
)
970 return VariantChangeTypeEx( pvargDest
, pvargSrc
, LOCALE_USER_DEFAULT
, wFlags
, vt
);
973 /******************************************************************************
974 * VariantChangeTypeEx [OLEAUT32.147]
976 * Change the type of a variant.
979 * pvargDest [O] Destination for the converted variant
980 * pvargSrc [O] Source variant to change the type of
981 * lcid [I] LCID for the conversion
982 * wFlags [I] VARIANT_ flags from "oleauto.h"
983 * vt [I] Variant type to change pvargSrc into
986 * Success: S_OK. pvargDest contains the converted value.
987 * Failure: An HRESULT error code describing the failure.
990 * pvargDest and pvargSrc can point to the same variant to perform an in-place
991 * conversion. If the conversion is successful, pvargSrc will be freed.
993 HRESULT WINAPI
VariantChangeTypeEx(VARIANTARG
* pvargDest
, VARIANTARG
* pvargSrc
,
994 LCID lcid
, USHORT wFlags
, VARTYPE vt
)
998 TRACE("(%s,%s,0x%08x,0x%04x,%s)\n", debugstr_variant(pvargDest
),
999 debugstr_variant(pvargSrc
), lcid
, wFlags
, debugstr_vt(vt
));
1002 res
= DISP_E_BADVARTYPE
;
1005 res
= VARIANT_ValidateType(V_VT(pvargSrc
));
1009 res
= VARIANT_ValidateType(vt
);
1013 VARIANTARG vTmp
, vSrcDeref
;
1015 if(V_ISBYREF(pvargSrc
) && !V_BYREF(pvargSrc
))
1016 res
= DISP_E_TYPEMISMATCH
;
1019 V_VT(&vTmp
) = VT_EMPTY
;
1020 V_VT(&vSrcDeref
) = VT_EMPTY
;
1021 VariantClear(&vTmp
);
1022 VariantClear(&vSrcDeref
);
1027 res
= VariantCopyInd(&vSrcDeref
, pvargSrc
);
1030 if (V_ISARRAY(&vSrcDeref
) || (vt
& VT_ARRAY
))
1031 res
= VARIANT_CoerceArray(&vTmp
, &vSrcDeref
, vt
);
1033 res
= VARIANT_Coerce(&vTmp
, lcid
, wFlags
, &vSrcDeref
, vt
);
1035 if (SUCCEEDED(res
)) {
1037 res
= VariantCopy(pvargDest
, &vTmp
);
1039 VariantClear(&vTmp
);
1040 VariantClear(&vSrcDeref
);
1047 TRACE("returning 0x%08x, %s\n", res
, debugstr_variant(pvargDest
));
1051 /* Date Conversions */
1053 #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
1055 /* Convert a VT_DATE value to a Julian Date */
1056 static inline int VARIANT_JulianFromDate(int dateIn
)
1058 int julianDays
= dateIn
;
1060 julianDays
-= DATE_MIN
; /* Convert to + days from 1 Jan 100 AD */
1061 julianDays
+= 1757585; /* Convert to + days from 23 Nov 4713 BC (Julian) */
1065 /* Convert a Julian Date to a VT_DATE value */
1066 static inline int VARIANT_DateFromJulian(int dateIn
)
1068 int julianDays
= dateIn
;
1070 julianDays
-= 1757585; /* Convert to + days from 1 Jan 100 AD */
1071 julianDays
+= DATE_MIN
; /* Convert to +/- days from 1 Jan 1899 AD */
1075 /* Convert a Julian date to Day/Month/Year - from PostgreSQL */
1076 static inline void VARIANT_DMYFromJulian(int jd
, USHORT
*year
, USHORT
*month
, USHORT
*day
)
1082 l
-= (n
* 146097 + 3) / 4;
1083 i
= (4000 * (l
+ 1)) / 1461001;
1084 l
+= 31 - (i
* 1461) / 4;
1085 j
= (l
* 80) / 2447;
1086 *day
= l
- (j
* 2447) / 80;
1088 *month
= (j
+ 2) - (12 * l
);
1089 *year
= 100 * (n
- 49) + i
+ l
;
1092 /* Convert Day/Month/Year to a Julian date - from PostgreSQL */
1093 static inline double VARIANT_JulianFromDMY(USHORT year
, USHORT month
, USHORT day
)
1095 int m12
= (month
- 14) / 12;
1097 return ((1461 * (year
+ 4800 + m12
)) / 4 + (367 * (month
- 2 - 12 * m12
)) / 12 -
1098 (3 * ((year
+ 4900 + m12
) / 100)) / 4 + day
- 32075);
1101 /* Macros for accessing DOS format date/time fields */
1102 #define DOS_YEAR(x) (1980 + (x >> 9))
1103 #define DOS_MONTH(x) ((x >> 5) & 0xf)
1104 #define DOS_DAY(x) (x & 0x1f)
1105 #define DOS_HOUR(x) (x >> 11)
1106 #define DOS_MINUTE(x) ((x >> 5) & 0x3f)
1107 #define DOS_SECOND(x) ((x & 0x1f) << 1)
1108 /* Create a DOS format date/time */
1109 #define DOS_DATE(d,m,y) (d | (m << 5) | ((y-1980) << 9))
1110 #define DOS_TIME(h,m,s) ((s >> 1) | (m << 5) | (h << 11))
1112 /* Roll a date forwards or backwards to correct it */
1113 static HRESULT
VARIANT_RollUdate(UDATE
*lpUd
)
1115 static const BYTE days
[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1116 short iYear
, iMonth
, iDay
, iHour
, iMinute
, iSecond
;
1118 /* interpret values signed */
1119 iYear
= lpUd
->st
.wYear
;
1120 iMonth
= lpUd
->st
.wMonth
;
1121 iDay
= lpUd
->st
.wDay
;
1122 iHour
= lpUd
->st
.wHour
;
1123 iMinute
= lpUd
->st
.wMinute
;
1124 iSecond
= lpUd
->st
.wSecond
;
1126 TRACE("Raw date: %d/%d/%d %d:%d:%d\n", iDay
, iMonth
,
1127 iYear
, iHour
, iMinute
, iSecond
);
1129 if (iYear
> 9999 || iYear
< -9999)
1130 return E_INVALIDARG
; /* Invalid value */
1131 /* Year 0 to 29 are treated as 2000 + year */
1132 if (iYear
>= 0 && iYear
< 30)
1134 /* Remaining years < 100 are treated as 1900 + year */
1135 else if (iYear
>= 30 && iYear
< 100)
1138 iMinute
+= iSecond
/ 60;
1139 iSecond
= iSecond
% 60;
1140 iHour
+= iMinute
/ 60;
1141 iMinute
= iMinute
% 60;
1144 iYear
+= iMonth
/ 12;
1145 iMonth
= iMonth
% 12;
1146 if (iMonth
<=0) {iMonth
+=12; iYear
--;}
1147 while (iDay
> days
[iMonth
])
1149 if (iMonth
== 2 && IsLeapYear(iYear
))
1152 iDay
-= days
[iMonth
];
1154 iYear
+= iMonth
/ 12;
1155 iMonth
= iMonth
% 12;
1160 if (iMonth
<=0) {iMonth
+=12; iYear
--;}
1161 if (iMonth
== 2 && IsLeapYear(iYear
))
1164 iDay
+= days
[iMonth
];
1167 if (iSecond
<0){iSecond
+=60; iMinute
--;}
1168 if (iMinute
<0){iMinute
+=60; iHour
--;}
1169 if (iHour
<0) {iHour
+=24; iDay
--;}
1170 if (iYear
<=0) iYear
+=2000;
1172 lpUd
->st
.wYear
= iYear
;
1173 lpUd
->st
.wMonth
= iMonth
;
1174 lpUd
->st
.wDay
= iDay
;
1175 lpUd
->st
.wHour
= iHour
;
1176 lpUd
->st
.wMinute
= iMinute
;
1177 lpUd
->st
.wSecond
= iSecond
;
1179 TRACE("Rolled date: %d/%d/%d %d:%d:%d\n", lpUd
->st
.wDay
, lpUd
->st
.wMonth
,
1180 lpUd
->st
.wYear
, lpUd
->st
.wHour
, lpUd
->st
.wMinute
, lpUd
->st
.wSecond
);
1184 /**********************************************************************
1185 * DosDateTimeToVariantTime [OLEAUT32.14]
1187 * Convert a Dos format date and time into variant VT_DATE format.
1190 * wDosDate [I] Dos format date
1191 * wDosTime [I] Dos format time
1192 * pDateOut [O] Destination for VT_DATE format
1195 * Success: TRUE. pDateOut contains the converted time.
1196 * Failure: FALSE, if wDosDate or wDosTime are invalid (see notes).
1199 * - Dos format dates can only hold dates from 1-Jan-1980 to 31-Dec-2099.
1200 * - Dos format times are accurate to only 2 second precision.
1201 * - The format of a Dos Date is:
1202 *| Bits Values Meaning
1203 *| ---- ------ -------
1204 *| 0-4 1-31 Day of the week. 0 rolls back one day. A value greater than
1205 *| the days in the month rolls forward the extra days.
1206 *| 5-8 1-12 Month of the year. 0 rolls back to December of the previous
1207 *| year. 13-15 are invalid.
1208 *| 9-15 0-119 Year based from 1980 (Max 2099). 120-127 are invalid.
1209 * - The format of a Dos Time is:
1210 *| Bits Values Meaning
1211 *| ---- ------ -------
1212 *| 0-4 0-29 Seconds/2. 30 and 31 are invalid.
1213 *| 5-10 0-59 Minutes. 60-63 are invalid.
1214 *| 11-15 0-23 Hours (24 hour clock). 24-32 are invalid.
1216 INT WINAPI
DosDateTimeToVariantTime(USHORT wDosDate
, USHORT wDosTime
,
1221 TRACE("(0x%x(%d/%d/%d),0x%x(%d:%d:%d),%p)\n",
1222 wDosDate
, DOS_YEAR(wDosDate
), DOS_MONTH(wDosDate
), DOS_DAY(wDosDate
),
1223 wDosTime
, DOS_HOUR(wDosTime
), DOS_MINUTE(wDosTime
), DOS_SECOND(wDosTime
),
1226 ud
.st
.wYear
= DOS_YEAR(wDosDate
);
1227 ud
.st
.wMonth
= DOS_MONTH(wDosDate
);
1228 if (ud
.st
.wYear
> 2099 || ud
.st
.wMonth
> 12)
1230 ud
.st
.wDay
= DOS_DAY(wDosDate
);
1231 ud
.st
.wHour
= DOS_HOUR(wDosTime
);
1232 ud
.st
.wMinute
= DOS_MINUTE(wDosTime
);
1233 ud
.st
.wSecond
= DOS_SECOND(wDosTime
);
1234 ud
.st
.wDayOfWeek
= ud
.st
.wMilliseconds
= 0;
1235 if (ud
.st
.wHour
> 23 || ud
.st
.wMinute
> 59 || ud
.st
.wSecond
> 59)
1236 return FALSE
; /* Invalid values in Dos*/
1238 return VarDateFromUdate(&ud
, 0, pDateOut
) == S_OK
;
1241 /**********************************************************************
1242 * VariantTimeToDosDateTime [OLEAUT32.13]
1244 * Convert a variant format date into a Dos format date and time.
1246 * dateIn [I] VT_DATE time format
1247 * pwDosDate [O] Destination for Dos format date
1248 * pwDosTime [O] Destination for Dos format time
1251 * Success: TRUE. pwDosDate and pwDosTime contains the converted values.
1252 * Failure: FALSE, if dateIn cannot be represented in Dos format.
1255 * See DosDateTimeToVariantTime() for Dos format details and bugs.
1257 INT WINAPI
VariantTimeToDosDateTime(double dateIn
, USHORT
*pwDosDate
, USHORT
*pwDosTime
)
1261 TRACE("(%g,%p,%p)\n", dateIn
, pwDosDate
, pwDosTime
);
1263 if (FAILED(VarUdateFromDate(dateIn
, 0, &ud
)))
1266 if (ud
.st
.wYear
< 1980 || ud
.st
.wYear
> 2099)
1269 *pwDosDate
= DOS_DATE(ud
.st
.wDay
, ud
.st
.wMonth
, ud
.st
.wYear
);
1270 *pwDosTime
= DOS_TIME(ud
.st
.wHour
, ud
.st
.wMinute
, ud
.st
.wSecond
);
1272 TRACE("Returning 0x%x(%d/%d/%d), 0x%x(%d:%d:%d)\n",
1273 *pwDosDate
, DOS_YEAR(*pwDosDate
), DOS_MONTH(*pwDosDate
), DOS_DAY(*pwDosDate
),
1274 *pwDosTime
, DOS_HOUR(*pwDosTime
), DOS_MINUTE(*pwDosTime
), DOS_SECOND(*pwDosTime
));
1278 /***********************************************************************
1279 * SystemTimeToVariantTime [OLEAUT32.184]
1281 * Convert a System format date and time into variant VT_DATE format.
1284 * lpSt [I] System format date and time
1285 * pDateOut [O] Destination for VT_DATE format date
1288 * Success: TRUE. *pDateOut contains the converted value.
1289 * Failure: FALSE, if lpSt cannot be represented in VT_DATE format.
1291 INT WINAPI
SystemTimeToVariantTime(LPSYSTEMTIME lpSt
, double *pDateOut
)
1295 TRACE("(%p->%d/%d/%d %d:%d:%d,%p)\n", lpSt
, lpSt
->wDay
, lpSt
->wMonth
,
1296 lpSt
->wYear
, lpSt
->wHour
, lpSt
->wMinute
, lpSt
->wSecond
, pDateOut
);
1298 if (lpSt
->wMonth
> 12)
1300 if (lpSt
->wDay
> 31)
1302 if ((short)lpSt
->wYear
< 0)
1306 return VarDateFromUdate(&ud
, 0, pDateOut
) == S_OK
;
1309 /***********************************************************************
1310 * VariantTimeToSystemTime [OLEAUT32.185]
1312 * Convert a variant VT_DATE into a System format date and time.
1315 * datein [I] Variant VT_DATE format date
1316 * lpSt [O] Destination for System format date and time
1319 * Success: TRUE. *lpSt contains the converted value.
1320 * Failure: FALSE, if dateIn is too large or small.
1322 INT WINAPI
VariantTimeToSystemTime(double dateIn
, LPSYSTEMTIME lpSt
)
1326 TRACE("(%g,%p)\n", dateIn
, lpSt
);
1328 if (FAILED(VarUdateFromDate(dateIn
, 0, &ud
)))
1335 /***********************************************************************
1336 * VarDateFromUdateEx [OLEAUT32.319]
1338 * Convert an unpacked format date and time to a variant VT_DATE.
1341 * pUdateIn [I] Unpacked format date and time to convert
1342 * lcid [I] Locale identifier for the conversion
1343 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1344 * pDateOut [O] Destination for variant VT_DATE.
1347 * Success: S_OK. *pDateOut contains the converted value.
1348 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1350 HRESULT WINAPI
VarDateFromUdateEx(UDATE
*pUdateIn
, LCID lcid
, ULONG dwFlags
, DATE
*pDateOut
)
1353 double dateVal
, dateSign
;
1355 TRACE("(%p->%d/%d/%d %d:%d:%d:%d %d %d,0x%08x,0x%08x,%p)\n", pUdateIn
,
1356 pUdateIn
->st
.wMonth
, pUdateIn
->st
.wDay
, pUdateIn
->st
.wYear
,
1357 pUdateIn
->st
.wHour
, pUdateIn
->st
.wMinute
, pUdateIn
->st
.wSecond
,
1358 pUdateIn
->st
.wMilliseconds
, pUdateIn
->st
.wDayOfWeek
,
1359 pUdateIn
->wDayOfYear
, lcid
, dwFlags
, pDateOut
);
1361 if (lcid
!= MAKELCID(MAKELANGID(LANG_ENGLISH
, SUBLANG_ENGLISH_US
), SORT_DEFAULT
))
1362 FIXME("lcid possibly not handled, treating as en-us\n");
1366 if (dwFlags
& VAR_VALIDDATE
)
1367 WARN("Ignoring VAR_VALIDDATE\n");
1369 if (FAILED(VARIANT_RollUdate(&ud
)))
1370 return E_INVALIDARG
;
1373 dateVal
= VARIANT_DateFromJulian(VARIANT_JulianFromDMY(ud
.st
.wYear
, ud
.st
.wMonth
, ud
.st
.wDay
));
1376 dateSign
= (dateVal
< 0.0) ? -1.0 : 1.0;
1379 dateVal
+= ud
.st
.wHour
/ 24.0 * dateSign
;
1380 dateVal
+= ud
.st
.wMinute
/ 1440.0 * dateSign
;
1381 dateVal
+= ud
.st
.wSecond
/ 86400.0 * dateSign
;
1383 TRACE("Returning %g\n", dateVal
);
1384 *pDateOut
= dateVal
;
1388 /***********************************************************************
1389 * VarDateFromUdate [OLEAUT32.330]
1391 * Convert an unpacked format date and time to a variant VT_DATE.
1394 * pUdateIn [I] Unpacked format date and time to convert
1395 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1396 * pDateOut [O] Destination for variant VT_DATE.
1399 * Success: S_OK. *pDateOut contains the converted value.
1400 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1403 * This function uses the United States English locale for the conversion. Use
1404 * VarDateFromUdateEx() for alternate locales.
1406 HRESULT WINAPI
VarDateFromUdate(UDATE
*pUdateIn
, ULONG dwFlags
, DATE
*pDateOut
)
1408 LCID lcid
= MAKELCID(MAKELANGID(LANG_ENGLISH
, SUBLANG_ENGLISH_US
), SORT_DEFAULT
);
1410 return VarDateFromUdateEx(pUdateIn
, lcid
, dwFlags
, pDateOut
);
1413 /***********************************************************************
1414 * VarUdateFromDate [OLEAUT32.331]
1416 * Convert a variant VT_DATE into an unpacked format date and time.
1419 * datein [I] Variant VT_DATE format date
1420 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1421 * lpUdate [O] Destination for unpacked format date and time
1424 * Success: S_OK. *lpUdate contains the converted value.
1425 * Failure: E_INVALIDARG, if dateIn is too large or small.
1427 HRESULT WINAPI
VarUdateFromDate(DATE dateIn
, ULONG dwFlags
, UDATE
*lpUdate
)
1429 /* Cumulative totals of days per month */
1430 static const USHORT cumulativeDays
[] =
1432 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
1434 double datePart
, timePart
;
1437 TRACE("(%g,0x%08x,%p)\n", dateIn
, dwFlags
, lpUdate
);
1439 if (dateIn
<= (DATE_MIN
- 1.0) || dateIn
>= (DATE_MAX
+ 1.0))
1440 return E_INVALIDARG
;
1442 datePart
= dateIn
< 0.0 ? ceil(dateIn
) : floor(dateIn
);
1443 /* Compensate for int truncation (always downwards) */
1444 timePart
= fabs(dateIn
- datePart
) + 0.00000000001;
1445 if (timePart
>= 1.0)
1446 timePart
-= 0.00000000001;
1449 julianDays
= VARIANT_JulianFromDate(dateIn
);
1450 VARIANT_DMYFromJulian(julianDays
, &lpUdate
->st
.wYear
, &lpUdate
->st
.wMonth
,
1453 datePart
= (datePart
+ 1.5) / 7.0;
1454 lpUdate
->st
.wDayOfWeek
= (datePart
- floor(datePart
)) * 7;
1455 if (lpUdate
->st
.wDayOfWeek
== 0)
1456 lpUdate
->st
.wDayOfWeek
= 5;
1457 else if (lpUdate
->st
.wDayOfWeek
== 1)
1458 lpUdate
->st
.wDayOfWeek
= 6;
1460 lpUdate
->st
.wDayOfWeek
-= 2;
1462 if (lpUdate
->st
.wMonth
> 2 && IsLeapYear(lpUdate
->st
.wYear
))
1463 lpUdate
->wDayOfYear
= 1; /* After February, in a leap year */
1465 lpUdate
->wDayOfYear
= 0;
1467 lpUdate
->wDayOfYear
+= cumulativeDays
[lpUdate
->st
.wMonth
];
1468 lpUdate
->wDayOfYear
+= lpUdate
->st
.wDay
;
1472 lpUdate
->st
.wHour
= timePart
;
1473 timePart
-= lpUdate
->st
.wHour
;
1475 lpUdate
->st
.wMinute
= timePart
;
1476 timePart
-= lpUdate
->st
.wMinute
;
1478 lpUdate
->st
.wSecond
= timePart
;
1479 timePart
-= lpUdate
->st
.wSecond
;
1480 lpUdate
->st
.wMilliseconds
= 0;
1483 /* Round the milliseconds, adjusting the time/date forward if needed */
1484 if (lpUdate
->st
.wSecond
< 59)
1485 lpUdate
->st
.wSecond
++;
1488 lpUdate
->st
.wSecond
= 0;
1489 if (lpUdate
->st
.wMinute
< 59)
1490 lpUdate
->st
.wMinute
++;
1493 lpUdate
->st
.wMinute
= 0;
1494 if (lpUdate
->st
.wHour
< 23)
1495 lpUdate
->st
.wHour
++;
1498 lpUdate
->st
.wHour
= 0;
1499 /* Roll over a whole day */
1500 if (++lpUdate
->st
.wDay
> 28)
1501 VARIANT_RollUdate(lpUdate
);
1509 #define GET_NUMBER_TEXT(fld,name) \
1511 if (!GetLocaleInfoW(lcid, lctype|fld, buff, 2)) \
1512 WARN("buffer too small for " #fld "\n"); \
1514 if (buff[0]) lpChars->name = buff[0]; \
1515 TRACE("lcid 0x%x, " #name "=%d '%c'\n", lcid, lpChars->name, lpChars->name)
1517 /* Get the valid number characters for an lcid */
1518 static void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS
*lpChars
, LCID lcid
, DWORD dwFlags
)
1520 static const VARIANT_NUMBER_CHARS defaultChars
= { '-','+','.',',','$',0,'.',',' };
1521 static CRITICAL_SECTION csLastChars
= { NULL
, -1, 0, 0, 0, 0 };
1522 static VARIANT_NUMBER_CHARS lastChars
;
1523 static LCID lastLcid
= -1;
1524 static DWORD lastFlags
= 0;
1525 LCTYPE lctype
= dwFlags
& LOCALE_NOUSEROVERRIDE
;
1528 /* To make caching thread-safe, a critical section is needed */
1529 EnterCriticalSection(&csLastChars
);
1531 /* Asking for default locale entries is very expensive: It is a registry
1532 server call. So cache one locally, as Microsoft does it too */
1533 if(lcid
== lastLcid
&& dwFlags
== lastFlags
)
1535 memcpy(lpChars
, &lastChars
, sizeof(defaultChars
));
1536 LeaveCriticalSection(&csLastChars
);
1540 memcpy(lpChars
, &defaultChars
, sizeof(defaultChars
));
1541 GET_NUMBER_TEXT(LOCALE_SNEGATIVESIGN
, cNegativeSymbol
);
1542 GET_NUMBER_TEXT(LOCALE_SPOSITIVESIGN
, cPositiveSymbol
);
1543 GET_NUMBER_TEXT(LOCALE_SDECIMAL
, cDecimalPoint
);
1544 GET_NUMBER_TEXT(LOCALE_STHOUSAND
, cDigitSeparator
);
1545 GET_NUMBER_TEXT(LOCALE_SMONDECIMALSEP
, cCurrencyDecimalPoint
);
1546 GET_NUMBER_TEXT(LOCALE_SMONTHOUSANDSEP
, cCurrencyDigitSeparator
);
1548 /* Local currency symbols are often 2 characters */
1549 lpChars
->cCurrencyLocal2
= '\0';
1550 switch(GetLocaleInfoW(lcid
, lctype
|LOCALE_SCURRENCY
, buff
, sizeof(buff
)/sizeof(WCHAR
)))
1552 case 3: lpChars
->cCurrencyLocal2
= buff
[1]; /* Fall through */
1553 case 2: lpChars
->cCurrencyLocal
= buff
[0];
1555 default: WARN("buffer too small for LOCALE_SCURRENCY\n");
1557 TRACE("lcid 0x%x, cCurrencyLocal =%d,%d '%c','%c'\n", lcid
, lpChars
->cCurrencyLocal
,
1558 lpChars
->cCurrencyLocal2
, lpChars
->cCurrencyLocal
, lpChars
->cCurrencyLocal2
);
1560 memcpy(&lastChars
, lpChars
, sizeof(defaultChars
));
1562 lastFlags
= dwFlags
;
1563 LeaveCriticalSection(&csLastChars
);
1566 /* Number Parsing States */
1567 #define B_PROCESSING_EXPONENT 0x1
1568 #define B_NEGATIVE_EXPONENT 0x2
1569 #define B_EXPONENT_START 0x4
1570 #define B_INEXACT_ZEROS 0x8
1571 #define B_LEADING_ZERO 0x10
1572 #define B_PROCESSING_HEX 0x20
1573 #define B_PROCESSING_OCT 0x40
1575 /**********************************************************************
1576 * VarParseNumFromStr [OLEAUT32.46]
1578 * Parse a string containing a number into a NUMPARSE structure.
1581 * lpszStr [I] String to parse number from
1582 * lcid [I] Locale Id for the conversion
1583 * dwFlags [I] 0, or LOCALE_NOUSEROVERRIDE to use system default number chars
1584 * pNumprs [I/O] Destination for parsed number
1585 * rgbDig [O] Destination for digits read in
1588 * Success: S_OK. pNumprs and rgbDig contain the parsed representation of
1590 * Failure: E_INVALIDARG, if any parameter is invalid.
1591 * DISP_E_TYPEMISMATCH, if the string is not a number or is formatted
1593 * DISP_E_OVERFLOW, if rgbDig is too small to hold the number.
1596 * pNumprs must have the following fields set:
1597 * cDig: Set to the size of rgbDig.
1598 * dwInFlags: Set to the allowable syntax of the number using NUMPRS_ flags
1602 * - I am unsure if this function should parse non-Arabic (e.g. Thai)
1603 * numerals, so this has not been implemented.
1605 HRESULT WINAPI
VarParseNumFromStr(OLECHAR
*lpszStr
, LCID lcid
, ULONG dwFlags
,
1606 NUMPARSE
*pNumprs
, BYTE
*rgbDig
)
1608 VARIANT_NUMBER_CHARS chars
;
1610 DWORD dwState
= B_EXPONENT_START
|B_INEXACT_ZEROS
;
1611 int iMaxDigits
= sizeof(rgbTmp
) / sizeof(BYTE
);
1614 TRACE("(%s,%d,0x%08x,%p,%p)\n", debugstr_w(lpszStr
), lcid
, dwFlags
, pNumprs
, rgbDig
);
1616 if (!pNumprs
|| !rgbDig
)
1617 return E_INVALIDARG
;
1619 if (pNumprs
->cDig
< iMaxDigits
)
1620 iMaxDigits
= pNumprs
->cDig
;
1623 pNumprs
->dwOutFlags
= 0;
1624 pNumprs
->cchUsed
= 0;
1625 pNumprs
->nBaseShift
= 0;
1626 pNumprs
->nPwr10
= 0;
1629 return DISP_E_TYPEMISMATCH
;
1631 VARIANT_GetLocalisedNumberChars(&chars
, lcid
, dwFlags
);
1633 /* First consume all the leading symbols and space from the string */
1636 if (pNumprs
->dwInFlags
& NUMPRS_LEADING_WHITE
&& isspaceW(*lpszStr
))
1638 pNumprs
->dwOutFlags
|= NUMPRS_LEADING_WHITE
;
1643 } while (isspaceW(*lpszStr
));
1645 else if (pNumprs
->dwInFlags
& NUMPRS_LEADING_PLUS
&&
1646 *lpszStr
== chars
.cPositiveSymbol
&&
1647 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_PLUS
))
1649 pNumprs
->dwOutFlags
|= NUMPRS_LEADING_PLUS
;
1653 else if (pNumprs
->dwInFlags
& NUMPRS_LEADING_MINUS
&&
1654 *lpszStr
== chars
.cNegativeSymbol
&&
1655 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_MINUS
))
1657 pNumprs
->dwOutFlags
|= (NUMPRS_LEADING_MINUS
|NUMPRS_NEG
);
1661 else if (pNumprs
->dwInFlags
& NUMPRS_CURRENCY
&&
1662 !(pNumprs
->dwOutFlags
& NUMPRS_CURRENCY
) &&
1663 *lpszStr
== chars
.cCurrencyLocal
&&
1664 (!chars
.cCurrencyLocal2
|| lpszStr
[1] == chars
.cCurrencyLocal2
))
1666 pNumprs
->dwOutFlags
|= NUMPRS_CURRENCY
;
1669 /* Only accept currency characters */
1670 chars
.cDecimalPoint
= chars
.cCurrencyDecimalPoint
;
1671 chars
.cDigitSeparator
= chars
.cCurrencyDigitSeparator
;
1673 else if (pNumprs
->dwInFlags
& NUMPRS_PARENS
&& *lpszStr
== '(' &&
1674 !(pNumprs
->dwOutFlags
& NUMPRS_PARENS
))
1676 pNumprs
->dwOutFlags
|= NUMPRS_PARENS
;
1684 if (!(pNumprs
->dwOutFlags
& NUMPRS_CURRENCY
))
1686 /* Only accept non-currency characters */
1687 chars
.cCurrencyDecimalPoint
= chars
.cDecimalPoint
;
1688 chars
.cCurrencyDigitSeparator
= chars
.cDigitSeparator
;
1691 if ((*lpszStr
== '&' && (*(lpszStr
+1) == 'H' || *(lpszStr
+1) == 'h')) &&
1692 pNumprs
->dwInFlags
& NUMPRS_HEX_OCT
)
1694 dwState
|= B_PROCESSING_HEX
;
1695 pNumprs
->dwOutFlags
|= NUMPRS_HEX_OCT
;
1699 else if ((*lpszStr
== '&' && (*(lpszStr
+1) == 'O' || *(lpszStr
+1) == 'o')) &&
1700 pNumprs
->dwInFlags
& NUMPRS_HEX_OCT
)
1702 dwState
|= B_PROCESSING_OCT
;
1703 pNumprs
->dwOutFlags
|= NUMPRS_HEX_OCT
;
1708 /* Strip Leading zeros */
1709 while (*lpszStr
== '0')
1711 dwState
|= B_LEADING_ZERO
;
1718 if (isdigitW(*lpszStr
))
1720 if (dwState
& B_PROCESSING_EXPONENT
)
1722 int exponentSize
= 0;
1723 if (dwState
& B_EXPONENT_START
)
1725 if (!isdigitW(*lpszStr
))
1726 break; /* No exponent digits - invalid */
1727 while (*lpszStr
== '0')
1729 /* Skip leading zero's in the exponent */
1735 while (isdigitW(*lpszStr
))
1738 exponentSize
+= *lpszStr
- '0';
1742 if (dwState
& B_NEGATIVE_EXPONENT
)
1743 exponentSize
= -exponentSize
;
1744 /* Add the exponent into the powers of 10 */
1745 pNumprs
->nPwr10
+= exponentSize
;
1746 dwState
&= ~(B_PROCESSING_EXPONENT
|B_EXPONENT_START
);
1747 lpszStr
--; /* back up to allow processing of next char */
1751 if ((pNumprs
->cDig
>= iMaxDigits
) && !(dwState
& B_PROCESSING_HEX
)
1752 && !(dwState
& B_PROCESSING_OCT
))
1754 pNumprs
->dwOutFlags
|= NUMPRS_INEXACT
;
1756 if (*lpszStr
!= '0')
1757 dwState
&= ~B_INEXACT_ZEROS
; /* Inexact number with non-trailing zeros */
1759 /* This digit can't be represented, but count it in nPwr10 */
1760 if (pNumprs
->dwOutFlags
& NUMPRS_DECIMAL
)
1767 if ((dwState
& B_PROCESSING_OCT
) && ((*lpszStr
== '8') || (*lpszStr
== '9'))) {
1768 return DISP_E_TYPEMISMATCH
;
1771 if (pNumprs
->dwOutFlags
& NUMPRS_DECIMAL
)
1772 pNumprs
->nPwr10
--; /* Count decimal points in nPwr10 */
1774 rgbTmp
[pNumprs
->cDig
] = *lpszStr
- '0';
1780 else if (*lpszStr
== chars
.cDigitSeparator
&& pNumprs
->dwInFlags
& NUMPRS_THOUSANDS
)
1782 pNumprs
->dwOutFlags
|= NUMPRS_THOUSANDS
;
1785 else if (*lpszStr
== chars
.cDecimalPoint
&&
1786 pNumprs
->dwInFlags
& NUMPRS_DECIMAL
&&
1787 !(pNumprs
->dwOutFlags
& (NUMPRS_DECIMAL
|NUMPRS_EXPONENT
)))
1789 pNumprs
->dwOutFlags
|= NUMPRS_DECIMAL
;
1792 /* If we have no digits so far, skip leading zeros */
1795 while (lpszStr
[1] == '0')
1797 dwState
|= B_LEADING_ZERO
;
1804 else if (((*lpszStr
>= 'a' && *lpszStr
<= 'f') ||
1805 (*lpszStr
>= 'A' && *lpszStr
<= 'F')) &&
1806 dwState
& B_PROCESSING_HEX
)
1808 if (pNumprs
->cDig
>= iMaxDigits
)
1810 return DISP_E_OVERFLOW
;
1814 if (*lpszStr
>= 'a')
1815 rgbTmp
[pNumprs
->cDig
] = *lpszStr
- 'a' + 10;
1817 rgbTmp
[pNumprs
->cDig
] = *lpszStr
- 'A' + 10;
1822 else if ((*lpszStr
== 'e' || *lpszStr
== 'E') &&
1823 pNumprs
->dwInFlags
& NUMPRS_EXPONENT
&&
1824 !(pNumprs
->dwOutFlags
& NUMPRS_EXPONENT
))
1826 dwState
|= B_PROCESSING_EXPONENT
;
1827 pNumprs
->dwOutFlags
|= NUMPRS_EXPONENT
;
1830 else if (dwState
& B_PROCESSING_EXPONENT
&& *lpszStr
== chars
.cPositiveSymbol
)
1832 cchUsed
++; /* Ignore positive exponent */
1834 else if (dwState
& B_PROCESSING_EXPONENT
&& *lpszStr
== chars
.cNegativeSymbol
)
1836 dwState
|= B_NEGATIVE_EXPONENT
;
1840 break; /* Stop at an unrecognised character */
1845 if (!pNumprs
->cDig
&& dwState
& B_LEADING_ZERO
)
1847 /* Ensure a 0 on its own gets stored */
1852 if (pNumprs
->dwOutFlags
& NUMPRS_EXPONENT
&& dwState
& B_PROCESSING_EXPONENT
)
1854 pNumprs
->cchUsed
= cchUsed
;
1855 WARN("didn't completely parse exponent\n");
1856 return DISP_E_TYPEMISMATCH
; /* Failed to completely parse the exponent */
1859 if (pNumprs
->dwOutFlags
& NUMPRS_INEXACT
)
1861 if (dwState
& B_INEXACT_ZEROS
)
1862 pNumprs
->dwOutFlags
&= ~NUMPRS_INEXACT
; /* All zeros doesn't set NUMPRS_INEXACT */
1863 } else if(pNumprs
->dwInFlags
& NUMPRS_HEX_OCT
)
1865 /* copy all of the digits into the output digit buffer */
1866 /* this is exactly what windows does although it also returns */
1867 /* cDig of X and writes X+Y where Y>=0 number of digits to rgbDig */
1868 memcpy(rgbDig
, rgbTmp
, pNumprs
->cDig
* sizeof(BYTE
));
1870 if (dwState
& B_PROCESSING_HEX
) {
1871 /* hex numbers have always the same format */
1873 pNumprs
->nBaseShift
=4;
1875 if (dwState
& B_PROCESSING_OCT
) {
1876 /* oct numbers have always the same format */
1878 pNumprs
->nBaseShift
=3;
1880 while (pNumprs
->cDig
> 1 && !rgbTmp
[pNumprs
->cDig
- 1])
1889 /* Remove trailing zeros from the last (whole number or decimal) part */
1890 while (pNumprs
->cDig
> 1 && !rgbTmp
[pNumprs
->cDig
- 1])
1897 if (pNumprs
->cDig
<= iMaxDigits
)
1898 pNumprs
->dwOutFlags
&= ~NUMPRS_INEXACT
; /* Ignore stripped zeros for NUMPRS_INEXACT */
1900 pNumprs
->cDig
= iMaxDigits
; /* Only return iMaxDigits worth of digits */
1902 /* Copy the digits we processed into rgbDig */
1903 memcpy(rgbDig
, rgbTmp
, pNumprs
->cDig
* sizeof(BYTE
));
1905 /* Consume any trailing symbols and space */
1908 if ((pNumprs
->dwInFlags
& NUMPRS_TRAILING_WHITE
) && isspaceW(*lpszStr
))
1910 pNumprs
->dwOutFlags
|= NUMPRS_TRAILING_WHITE
;
1915 } while (isspaceW(*lpszStr
));
1917 else if (pNumprs
->dwInFlags
& NUMPRS_TRAILING_PLUS
&&
1918 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_PLUS
) &&
1919 *lpszStr
== chars
.cPositiveSymbol
)
1921 pNumprs
->dwOutFlags
|= NUMPRS_TRAILING_PLUS
;
1925 else if (pNumprs
->dwInFlags
& NUMPRS_TRAILING_MINUS
&&
1926 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_MINUS
) &&
1927 *lpszStr
== chars
.cNegativeSymbol
)
1929 pNumprs
->dwOutFlags
|= (NUMPRS_TRAILING_MINUS
|NUMPRS_NEG
);
1933 else if (pNumprs
->dwInFlags
& NUMPRS_PARENS
&& *lpszStr
== ')' &&
1934 pNumprs
->dwOutFlags
& NUMPRS_PARENS
)
1938 pNumprs
->dwOutFlags
|= NUMPRS_NEG
;
1944 if (pNumprs
->dwOutFlags
& NUMPRS_PARENS
&& !(pNumprs
->dwOutFlags
& NUMPRS_NEG
))
1946 pNumprs
->cchUsed
= cchUsed
;
1947 return DISP_E_TYPEMISMATCH
; /* Opening parenthesis not matched */
1950 if (pNumprs
->dwInFlags
& NUMPRS_USE_ALL
&& *lpszStr
!= '\0')
1951 return DISP_E_TYPEMISMATCH
; /* Not all chars were consumed */
1954 return DISP_E_TYPEMISMATCH
; /* No Number found */
1956 pNumprs
->cchUsed
= cchUsed
;
1960 /* VTBIT flags indicating an integer value */
1961 #define INTEGER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8)
1962 /* VTBIT flags indicating a real number value */
1963 #define REAL_VTBITS (VTBIT_R4|VTBIT_R8|VTBIT_CY)
1965 /* Helper macros to check whether bit pattern fits in VARIANT (x is a ULONG64 ) */
1966 #define FITS_AS_I1(x) ((x) >> 8 == 0)
1967 #define FITS_AS_I2(x) ((x) >> 16 == 0)
1968 #define FITS_AS_I4(x) ((x) >> 32 == 0)
1970 /**********************************************************************
1971 * VarNumFromParseNum [OLEAUT32.47]
1973 * Convert a NUMPARSE structure into a numeric Variant type.
1976 * pNumprs [I] Source for parsed number. cDig must be set to the size of rgbDig
1977 * rgbDig [I] Source for the numbers digits
1978 * dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
1979 * pVarDst [O] Destination for the converted Variant value.
1982 * Success: S_OK. pVarDst contains the converted value.
1983 * Failure: E_INVALIDARG, if any parameter is invalid.
1984 * DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
1987 * - The smallest favoured type present in dwVtBits that can represent the
1988 * number in pNumprs without losing precision is used.
1989 * - Signed types are preferred over unsigned types of the same size.
1990 * - Preferred types in order are: integer, float, double, currency then decimal.
1991 * - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
1992 * for details of the rounding method.
1993 * - pVarDst is not cleared before the result is stored in it.
1994 * - WinXP and Win2003 support VTBIT_I8, VTBIT_UI8 but that's buggy (by
1995 * design?): If some other VTBIT's for integers are specified together
1996 * with VTBIT_I8 and the number will fit only in a VT_I8 Windows will "cast"
1997 * the number to the smallest requested integer truncating this way the
1998 * number. Wine doesn't implement this "feature" (yet?).
2000 HRESULT WINAPI
VarNumFromParseNum(NUMPARSE
*pNumprs
, BYTE
*rgbDig
,
2001 ULONG dwVtBits
, VARIANT
*pVarDst
)
2003 /* Scale factors and limits for double arithmetic */
2004 static const double dblMultipliers
[11] = {
2005 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
2006 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
2008 static const double dblMinimums
[11] = {
2009 R8_MIN
, R8_MIN
*10.0, R8_MIN
*100.0, R8_MIN
*1000.0, R8_MIN
*10000.0,
2010 R8_MIN
*100000.0, R8_MIN
*1000000.0, R8_MIN
*10000000.0,
2011 R8_MIN
*100000000.0, R8_MIN
*1000000000.0, R8_MIN
*10000000000.0
2013 static const double dblMaximums
[11] = {
2014 R8_MAX
, R8_MAX
/10.0, R8_MAX
/100.0, R8_MAX
/1000.0, R8_MAX
/10000.0,
2015 R8_MAX
/100000.0, R8_MAX
/1000000.0, R8_MAX
/10000000.0,
2016 R8_MAX
/100000000.0, R8_MAX
/1000000000.0, R8_MAX
/10000000000.0
2019 int wholeNumberDigits
, fractionalDigits
, divisor10
= 0, multiplier10
= 0;
2021 TRACE("(%p,%p,0x%x,%p)\n", pNumprs
, rgbDig
, dwVtBits
, pVarDst
);
2023 if (pNumprs
->nBaseShift
)
2025 /* nBaseShift indicates a hex or octal number */
2030 /* Convert the hex or octal number string into a UI64 */
2031 for (i
= 0; i
< pNumprs
->cDig
; i
++)
2033 if (ul64
> ((UI8_MAX
>>pNumprs
->nBaseShift
) - rgbDig
[i
]))
2035 TRACE("Overflow multiplying digits\n");
2036 return DISP_E_OVERFLOW
;
2038 ul64
= (ul64
<<pNumprs
->nBaseShift
) + rgbDig
[i
];
2041 /* also make a negative representation */
2044 /* Try signed and unsigned types in size order */
2045 if (dwVtBits
& VTBIT_I1
&& FITS_AS_I1(ul64
))
2047 V_VT(pVarDst
) = VT_I1
;
2048 V_I1(pVarDst
) = ul64
;
2051 else if (dwVtBits
& VTBIT_UI1
&& FITS_AS_I1(ul64
))
2053 V_VT(pVarDst
) = VT_UI1
;
2054 V_UI1(pVarDst
) = ul64
;
2057 else if (dwVtBits
& VTBIT_I2
&& FITS_AS_I2(ul64
))
2059 V_VT(pVarDst
) = VT_I2
;
2060 V_I2(pVarDst
) = ul64
;
2063 else if (dwVtBits
& VTBIT_UI2
&& FITS_AS_I2(ul64
))
2065 V_VT(pVarDst
) = VT_UI2
;
2066 V_UI2(pVarDst
) = ul64
;
2069 else if (dwVtBits
& VTBIT_I4
&& FITS_AS_I4(ul64
))
2071 V_VT(pVarDst
) = VT_I4
;
2072 V_I4(pVarDst
) = ul64
;
2075 else if (dwVtBits
& VTBIT_UI4
&& FITS_AS_I4(ul64
))
2077 V_VT(pVarDst
) = VT_UI4
;
2078 V_UI4(pVarDst
) = ul64
;
2081 else if (dwVtBits
& VTBIT_I8
&& ((ul64
<= I8_MAX
)||(l64
>=I8_MIN
)))
2083 V_VT(pVarDst
) = VT_I8
;
2084 V_I8(pVarDst
) = ul64
;
2087 else if (dwVtBits
& VTBIT_UI8
)
2089 V_VT(pVarDst
) = VT_UI8
;
2090 V_UI8(pVarDst
) = ul64
;
2093 else if ((dwVtBits
& VTBIT_DECIMAL
) == VTBIT_DECIMAL
)
2095 V_VT(pVarDst
) = VT_DECIMAL
;
2096 DEC_SIGNSCALE(&V_DECIMAL(pVarDst
)) = SIGNSCALE(DECIMAL_POS
,0);
2097 DEC_HI32(&V_DECIMAL(pVarDst
)) = 0;
2098 DEC_LO64(&V_DECIMAL(pVarDst
)) = ul64
;
2101 else if (dwVtBits
& VTBIT_R4
&& ((ul64
<= I4_MAX
)||(l64
>= I4_MIN
)))
2103 V_VT(pVarDst
) = VT_R4
;
2105 V_R4(pVarDst
) = ul64
;
2107 V_R4(pVarDst
) = l64
;
2110 else if (dwVtBits
& VTBIT_R8
&& ((ul64
<= I4_MAX
)||(l64
>= I4_MIN
)))
2112 V_VT(pVarDst
) = VT_R8
;
2114 V_R8(pVarDst
) = ul64
;
2116 V_R8(pVarDst
) = l64
;
2120 TRACE("Overflow: possible return types: 0x%x, value: %s\n", dwVtBits
, wine_dbgstr_longlong(ul64
));
2121 return DISP_E_OVERFLOW
;
2124 /* Count the number of relevant fractional and whole digits stored,
2125 * And compute the divisor/multiplier to scale the number by.
2127 if (pNumprs
->nPwr10
< 0)
2129 if (-pNumprs
->nPwr10
>= pNumprs
->cDig
)
2131 /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
2132 wholeNumberDigits
= 0;
2133 fractionalDigits
= pNumprs
->cDig
;
2134 divisor10
= -pNumprs
->nPwr10
;
2138 /* An exactly represented real number e.g. 1.024 */
2139 wholeNumberDigits
= pNumprs
->cDig
+ pNumprs
->nPwr10
;
2140 fractionalDigits
= pNumprs
->cDig
- wholeNumberDigits
;
2141 divisor10
= pNumprs
->cDig
- wholeNumberDigits
;
2144 else if (pNumprs
->nPwr10
== 0)
2146 /* An exactly represented whole number e.g. 1024 */
2147 wholeNumberDigits
= pNumprs
->cDig
;
2148 fractionalDigits
= 0;
2150 else /* pNumprs->nPwr10 > 0 */
2152 /* A whole number followed by nPwr10 0's e.g. 102400 */
2153 wholeNumberDigits
= pNumprs
->cDig
;
2154 fractionalDigits
= 0;
2155 multiplier10
= pNumprs
->nPwr10
;
2158 TRACE("cDig %d; nPwr10 %d, whole %d, frac %d mult %d; div %d\n",
2159 pNumprs
->cDig
, pNumprs
->nPwr10
, wholeNumberDigits
, fractionalDigits
,
2160 multiplier10
, divisor10
);
2162 if (dwVtBits
& (INTEGER_VTBITS
|VTBIT_DECIMAL
) &&
2163 (!fractionalDigits
|| !(dwVtBits
& (REAL_VTBITS
|VTBIT_CY
|VTBIT_DECIMAL
))))
2165 /* We have one or more integer output choices, and either:
2166 * 1) An integer input value, or
2167 * 2) A real number input value but no floating output choices.
2168 * Alternately, we have a DECIMAL output available and an integer input.
2170 * So, place the integer value into pVarDst, using the smallest type
2171 * possible and preferring signed over unsigned types.
2173 BOOL bOverflow
= FALSE
, bNegative
;
2177 /* Convert the integer part of the number into a UI8 */
2178 for (i
= 0; i
< wholeNumberDigits
; i
++)
2180 if (ul64
> UI8_MAX
/ 10 || (ul64
== UI8_MAX
/ 10 && rgbDig
[i
] > UI8_MAX
% 10))
2182 TRACE("Overflow multiplying digits\n");
2186 ul64
= ul64
* 10 + rgbDig
[i
];
2189 /* Account for the scale of the number */
2190 if (!bOverflow
&& multiplier10
)
2192 for (i
= 0; i
< multiplier10
; i
++)
2194 if (ul64
> (UI8_MAX
/ 10))
2196 TRACE("Overflow scaling number\n");
2204 /* If we have any fractional digits, round the value.
2205 * Note we don't have to do this if divisor10 is < 1,
2206 * because this means the fractional part must be < 0.5
2208 if (!bOverflow
&& fractionalDigits
&& divisor10
> 0)
2210 const BYTE
* fracDig
= rgbDig
+ wholeNumberDigits
;
2211 BOOL bAdjust
= FALSE
;
2213 TRACE("first decimal value is %d\n", *fracDig
);
2216 bAdjust
= TRUE
; /* > 0.5 */
2217 else if (*fracDig
== 5)
2219 for (i
= 1; i
< fractionalDigits
; i
++)
2223 bAdjust
= TRUE
; /* > 0.5 */
2227 /* If exactly 0.5, round only odd values */
2228 if (i
== fractionalDigits
&& (ul64
& 1))
2234 if (ul64
== UI8_MAX
)
2236 TRACE("Overflow after rounding\n");
2243 /* Zero is not a negative number */
2244 bNegative
= pNumprs
->dwOutFlags
& NUMPRS_NEG
&& ul64
;
2246 TRACE("Integer value is 0x%s, bNeg %d\n", wine_dbgstr_longlong(ul64
), bNegative
);
2248 /* For negative integers, try the signed types in size order */
2249 if (!bOverflow
&& bNegative
)
2251 if (dwVtBits
& (VTBIT_I1
|VTBIT_I2
|VTBIT_I4
|VTBIT_I8
))
2253 if (dwVtBits
& VTBIT_I1
&& ul64
<= -I1_MIN
)
2255 V_VT(pVarDst
) = VT_I1
;
2256 V_I1(pVarDst
) = -ul64
;
2259 else if (dwVtBits
& VTBIT_I2
&& ul64
<= -I2_MIN
)
2261 V_VT(pVarDst
) = VT_I2
;
2262 V_I2(pVarDst
) = -ul64
;
2265 else if (dwVtBits
& VTBIT_I4
&& ul64
<= -((LONGLONG
)I4_MIN
))
2267 V_VT(pVarDst
) = VT_I4
;
2268 V_I4(pVarDst
) = -ul64
;
2271 else if (dwVtBits
& VTBIT_I8
&& ul64
<= (ULONGLONG
)I8_MAX
+ 1)
2273 V_VT(pVarDst
) = VT_I8
;
2274 V_I8(pVarDst
) = -ul64
;
2277 else if ((dwVtBits
& REAL_VTBITS
) == VTBIT_DECIMAL
)
2279 /* Decimal is only output choice left - fast path */
2280 V_VT(pVarDst
) = VT_DECIMAL
;
2281 DEC_SIGNSCALE(&V_DECIMAL(pVarDst
)) = SIGNSCALE(DECIMAL_NEG
,0);
2282 DEC_HI32(&V_DECIMAL(pVarDst
)) = 0;
2283 DEC_LO64(&V_DECIMAL(pVarDst
)) = -ul64
;
2288 else if (!bOverflow
)
2290 /* For positive integers, try signed then unsigned types in size order */
2291 if (dwVtBits
& VTBIT_I1
&& ul64
<= I1_MAX
)
2293 V_VT(pVarDst
) = VT_I1
;
2294 V_I1(pVarDst
) = ul64
;
2297 else if (dwVtBits
& VTBIT_UI1
&& ul64
<= UI1_MAX
)
2299 V_VT(pVarDst
) = VT_UI1
;
2300 V_UI1(pVarDst
) = ul64
;
2303 else if (dwVtBits
& VTBIT_I2
&& ul64
<= I2_MAX
)
2305 V_VT(pVarDst
) = VT_I2
;
2306 V_I2(pVarDst
) = ul64
;
2309 else if (dwVtBits
& VTBIT_UI2
&& ul64
<= UI2_MAX
)
2311 V_VT(pVarDst
) = VT_UI2
;
2312 V_UI2(pVarDst
) = ul64
;
2315 else if (dwVtBits
& VTBIT_I4
&& ul64
<= I4_MAX
)
2317 V_VT(pVarDst
) = VT_I4
;
2318 V_I4(pVarDst
) = ul64
;
2321 else if (dwVtBits
& VTBIT_UI4
&& ul64
<= UI4_MAX
)
2323 V_VT(pVarDst
) = VT_UI4
;
2324 V_UI4(pVarDst
) = ul64
;
2327 else if (dwVtBits
& VTBIT_I8
&& ul64
<= I8_MAX
)
2329 V_VT(pVarDst
) = VT_I8
;
2330 V_I8(pVarDst
) = ul64
;
2333 else if (dwVtBits
& VTBIT_UI8
)
2335 V_VT(pVarDst
) = VT_UI8
;
2336 V_UI8(pVarDst
) = ul64
;
2339 else if ((dwVtBits
& REAL_VTBITS
) == VTBIT_DECIMAL
)
2341 /* Decimal is only output choice left - fast path */
2342 V_VT(pVarDst
) = VT_DECIMAL
;
2343 DEC_SIGNSCALE(&V_DECIMAL(pVarDst
)) = SIGNSCALE(DECIMAL_POS
,0);
2344 DEC_HI32(&V_DECIMAL(pVarDst
)) = 0;
2345 DEC_LO64(&V_DECIMAL(pVarDst
)) = ul64
;
2351 if (dwVtBits
& REAL_VTBITS
)
2353 /* Try to put the number into a float or real */
2354 BOOL bOverflow
= FALSE
, bNegative
= pNumprs
->dwOutFlags
& NUMPRS_NEG
;
2358 /* Convert the number into a double */
2359 for (i
= 0; i
< pNumprs
->cDig
; i
++)
2360 whole
= whole
* 10.0 + rgbDig
[i
];
2362 TRACE("Whole double value is %16.16g\n", whole
);
2364 /* Account for the scale */
2365 while (multiplier10
> 10)
2367 if (whole
> dblMaximums
[10])
2369 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
);
2373 whole
= whole
* dblMultipliers
[10];
2376 if (multiplier10
&& !bOverflow
)
2378 if (whole
> dblMaximums
[multiplier10
])
2380 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
);
2384 whole
= whole
* dblMultipliers
[multiplier10
];
2388 TRACE("Scaled double value is %16.16g\n", whole
);
2390 while (divisor10
> 10 && !bOverflow
)
2392 if (whole
< dblMinimums
[10] && whole
!= 0)
2394 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
); /* Underflow */
2398 whole
= whole
/ dblMultipliers
[10];
2401 if (divisor10
&& !bOverflow
)
2403 if (whole
< dblMinimums
[divisor10
] && whole
!= 0)
2405 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
); /* Underflow */
2409 whole
= whole
/ dblMultipliers
[divisor10
];
2412 TRACE("Final double value is %16.16g\n", whole
);
2414 if (dwVtBits
& VTBIT_R4
&&
2415 ((whole
<= R4_MAX
&& whole
>= R4_MIN
) || whole
== 0.0))
2417 TRACE("Set R4 to final value\n");
2418 V_VT(pVarDst
) = VT_R4
; /* Fits into a float */
2419 V_R4(pVarDst
) = pNumprs
->dwOutFlags
& NUMPRS_NEG
? -whole
: whole
;
2423 if (dwVtBits
& VTBIT_R8
)
2425 TRACE("Set R8 to final value\n");
2426 V_VT(pVarDst
) = VT_R8
; /* Fits into a double */
2427 V_R8(pVarDst
) = pNumprs
->dwOutFlags
& NUMPRS_NEG
? -whole
: whole
;
2431 if (dwVtBits
& VTBIT_CY
)
2433 if (SUCCEEDED(VarCyFromR8(bNegative
? -whole
: whole
, &V_CY(pVarDst
))))
2435 V_VT(pVarDst
) = VT_CY
; /* Fits into a currency */
2436 TRACE("Set CY to final value\n");
2439 TRACE("Value Overflows CY\n");
2443 if (dwVtBits
& VTBIT_DECIMAL
)
2448 DECIMAL
* pDec
= &V_DECIMAL(pVarDst
);
2450 DECIMAL_SETZERO(*pDec
);
2453 if (pNumprs
->dwOutFlags
& NUMPRS_NEG
)
2454 DEC_SIGN(pDec
) = DECIMAL_NEG
;
2456 DEC_SIGN(pDec
) = DECIMAL_POS
;
2458 /* Factor the significant digits */
2459 for (i
= 0; i
< pNumprs
->cDig
; i
++)
2461 tmp
= (ULONG64
)DEC_LO32(pDec
) * 10 + rgbDig
[i
];
2462 carry
= (ULONG
)(tmp
>> 32);
2463 DEC_LO32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2464 tmp
= (ULONG64
)DEC_MID32(pDec
) * 10 + carry
;
2465 carry
= (ULONG
)(tmp
>> 32);
2466 DEC_MID32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2467 tmp
= (ULONG64
)DEC_HI32(pDec
) * 10 + carry
;
2468 DEC_HI32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2470 if (tmp
>> 32 & UI4_MAX
)
2472 VarNumFromParseNum_DecOverflow
:
2473 TRACE("Overflow\n");
2474 DEC_LO32(pDec
) = DEC_MID32(pDec
) = DEC_HI32(pDec
) = UI4_MAX
;
2475 return DISP_E_OVERFLOW
;
2479 /* Account for the scale of the number */
2480 while (multiplier10
> 0)
2482 tmp
= (ULONG64
)DEC_LO32(pDec
) * 10;
2483 carry
= (ULONG
)(tmp
>> 32);
2484 DEC_LO32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2485 tmp
= (ULONG64
)DEC_MID32(pDec
) * 10 + carry
;
2486 carry
= (ULONG
)(tmp
>> 32);
2487 DEC_MID32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2488 tmp
= (ULONG64
)DEC_HI32(pDec
) * 10 + carry
;
2489 DEC_HI32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2491 if (tmp
>> 32 & UI4_MAX
)
2492 goto VarNumFromParseNum_DecOverflow
;
2495 DEC_SCALE(pDec
) = divisor10
;
2497 V_VT(pVarDst
) = VT_DECIMAL
;
2500 return DISP_E_OVERFLOW
; /* No more output choices */
2503 /**********************************************************************
2504 * VarCat [OLEAUT32.318]
2506 * Concatenates one variant onto another.
2509 * left [I] First variant
2510 * right [I] Second variant
2511 * result [O] Result variant
2515 * Failure: An HRESULT error code indicating the error.
2517 HRESULT WINAPI
VarCat(LPVARIANT left
, LPVARIANT right
, LPVARIANT out
)
2519 VARTYPE leftvt
,rightvt
,resultvt
;
2521 static WCHAR str_true
[32];
2522 static WCHAR str_false
[32];
2523 static const WCHAR sz_empty
[] = {'\0'};
2524 leftvt
= V_VT(left
);
2525 rightvt
= V_VT(right
);
2527 TRACE("%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), out
);
2530 VARIANT_GetLocalisedText(LOCALE_USER_DEFAULT
, IDS_FALSE
, str_false
);
2531 VARIANT_GetLocalisedText(LOCALE_USER_DEFAULT
, IDS_TRUE
, str_true
);
2534 /* when both left and right are NULL the result is NULL */
2535 if (leftvt
== VT_NULL
&& rightvt
== VT_NULL
)
2537 V_VT(out
) = VT_NULL
;
2542 resultvt
= VT_EMPTY
;
2544 /* There are many special case for errors and return types */
2545 if (leftvt
== VT_VARIANT
&& (rightvt
== VT_ERROR
||
2546 rightvt
== VT_DATE
|| rightvt
== VT_DECIMAL
))
2547 hres
= DISP_E_TYPEMISMATCH
;
2548 else if ((leftvt
== VT_I2
|| leftvt
== VT_I4
||
2549 leftvt
== VT_R4
|| leftvt
== VT_R8
||
2550 leftvt
== VT_CY
|| leftvt
== VT_BOOL
||
2551 leftvt
== VT_BSTR
|| leftvt
== VT_I1
||
2552 leftvt
== VT_UI1
|| leftvt
== VT_UI2
||
2553 leftvt
== VT_UI4
|| leftvt
== VT_I8
||
2554 leftvt
== VT_UI8
|| leftvt
== VT_INT
||
2555 leftvt
== VT_UINT
|| leftvt
== VT_EMPTY
||
2556 leftvt
== VT_NULL
|| leftvt
== VT_DATE
||
2557 leftvt
== VT_DECIMAL
|| leftvt
== VT_DISPATCH
)
2559 (rightvt
== VT_I2
|| rightvt
== VT_I4
||
2560 rightvt
== VT_R4
|| rightvt
== VT_R8
||
2561 rightvt
== VT_CY
|| rightvt
== VT_BOOL
||
2562 rightvt
== VT_BSTR
|| rightvt
== VT_I1
||
2563 rightvt
== VT_UI1
|| rightvt
== VT_UI2
||
2564 rightvt
== VT_UI4
|| rightvt
== VT_I8
||
2565 rightvt
== VT_UI8
|| rightvt
== VT_INT
||
2566 rightvt
== VT_UINT
|| rightvt
== VT_EMPTY
||
2567 rightvt
== VT_NULL
|| rightvt
== VT_DATE
||
2568 rightvt
== VT_DECIMAL
|| rightvt
== VT_DISPATCH
))
2570 else if (rightvt
== VT_ERROR
&& leftvt
< VT_VOID
)
2571 hres
= DISP_E_TYPEMISMATCH
;
2572 else if (leftvt
== VT_ERROR
&& (rightvt
== VT_DATE
||
2573 rightvt
== VT_ERROR
|| rightvt
== VT_DECIMAL
))
2574 hres
= DISP_E_TYPEMISMATCH
;
2575 else if (rightvt
== VT_DATE
|| rightvt
== VT_ERROR
||
2576 rightvt
== VT_DECIMAL
)
2577 hres
= DISP_E_BADVARTYPE
;
2578 else if (leftvt
== VT_ERROR
|| rightvt
== VT_ERROR
)
2579 hres
= DISP_E_TYPEMISMATCH
;
2580 else if (leftvt
== VT_VARIANT
)
2581 hres
= DISP_E_TYPEMISMATCH
;
2582 else if (rightvt
== VT_VARIANT
&& (leftvt
== VT_EMPTY
||
2583 leftvt
== VT_NULL
|| leftvt
== VT_I2
||
2584 leftvt
== VT_I4
|| leftvt
== VT_R4
||
2585 leftvt
== VT_R8
|| leftvt
== VT_CY
||
2586 leftvt
== VT_DATE
|| leftvt
== VT_BSTR
||
2587 leftvt
== VT_BOOL
|| leftvt
== VT_DECIMAL
||
2588 leftvt
== VT_I1
|| leftvt
== VT_UI1
||
2589 leftvt
== VT_UI2
|| leftvt
== VT_UI4
||
2590 leftvt
== VT_I8
|| leftvt
== VT_UI8
||
2591 leftvt
== VT_INT
|| leftvt
== VT_UINT
))
2592 hres
= DISP_E_TYPEMISMATCH
;
2594 hres
= DISP_E_BADVARTYPE
;
2596 /* if result type is not S_OK, then no need to go further */
2599 V_VT(out
) = resultvt
;
2602 /* Else proceed with formatting inputs to strings */
2605 VARIANT bstrvar_left
, bstrvar_right
;
2606 V_VT(out
) = VT_BSTR
;
2608 VariantInit(&bstrvar_left
);
2609 VariantInit(&bstrvar_right
);
2611 /* Convert left side variant to string */
2612 if (leftvt
!= VT_BSTR
)
2614 if (leftvt
== VT_BOOL
)
2616 /* Bools are handled as localized True/False strings instead of 0/-1 as in MSDN */
2617 V_VT(&bstrvar_left
) = VT_BSTR
;
2619 V_BSTR(&bstrvar_left
) = SysAllocString(str_true
);
2621 V_BSTR(&bstrvar_left
) = SysAllocString(str_false
);
2623 /* Fill with empty string for later concat with right side */
2624 else if (leftvt
== VT_NULL
)
2626 V_VT(&bstrvar_left
) = VT_BSTR
;
2627 V_BSTR(&bstrvar_left
) = SysAllocString(sz_empty
);
2631 hres
= VariantChangeTypeEx(&bstrvar_left
,left
,0,0,VT_BSTR
);
2633 VariantClear(&bstrvar_left
);
2634 VariantClear(&bstrvar_right
);
2635 if (leftvt
== VT_NULL
&& (rightvt
== VT_EMPTY
||
2636 rightvt
== VT_NULL
|| rightvt
== VT_I2
||
2637 rightvt
== VT_I4
|| rightvt
== VT_R4
||
2638 rightvt
== VT_R8
|| rightvt
== VT_CY
||
2639 rightvt
== VT_DATE
|| rightvt
== VT_BSTR
||
2640 rightvt
== VT_BOOL
|| rightvt
== VT_DECIMAL
||
2641 rightvt
== VT_I1
|| rightvt
== VT_UI1
||
2642 rightvt
== VT_UI2
|| rightvt
== VT_UI4
||
2643 rightvt
== VT_I8
|| rightvt
== VT_UI8
||
2644 rightvt
== VT_INT
|| rightvt
== VT_UINT
))
2645 return DISP_E_BADVARTYPE
;
2651 /* convert right side variant to string */
2652 if (rightvt
!= VT_BSTR
)
2654 if (rightvt
== VT_BOOL
)
2656 /* Bools are handled as localized True/False strings instead of 0/-1 as in MSDN */
2657 V_VT(&bstrvar_right
) = VT_BSTR
;
2659 V_BSTR(&bstrvar_right
) = SysAllocString(str_true
);
2661 V_BSTR(&bstrvar_right
) = SysAllocString(str_false
);
2663 /* Fill with empty string for later concat with right side */
2664 else if (rightvt
== VT_NULL
)
2666 V_VT(&bstrvar_right
) = VT_BSTR
;
2667 V_BSTR(&bstrvar_right
) = SysAllocString(sz_empty
);
2671 hres
= VariantChangeTypeEx(&bstrvar_right
,right
,0,0,VT_BSTR
);
2673 VariantClear(&bstrvar_left
);
2674 VariantClear(&bstrvar_right
);
2675 if (rightvt
== VT_NULL
&& (leftvt
== VT_EMPTY
||
2676 leftvt
== VT_NULL
|| leftvt
== VT_I2
||
2677 leftvt
== VT_I4
|| leftvt
== VT_R4
||
2678 leftvt
== VT_R8
|| leftvt
== VT_CY
||
2679 leftvt
== VT_DATE
|| leftvt
== VT_BSTR
||
2680 leftvt
== VT_BOOL
|| leftvt
== VT_DECIMAL
||
2681 leftvt
== VT_I1
|| leftvt
== VT_UI1
||
2682 leftvt
== VT_UI2
|| leftvt
== VT_UI4
||
2683 leftvt
== VT_I8
|| leftvt
== VT_UI8
||
2684 leftvt
== VT_INT
|| leftvt
== VT_UINT
))
2685 return DISP_E_BADVARTYPE
;
2691 /* Concat the resulting strings together */
2692 if (leftvt
== VT_BSTR
&& rightvt
== VT_BSTR
)
2693 VarBstrCat (V_BSTR(left
), V_BSTR(right
), &V_BSTR(out
));
2694 else if (leftvt
!= VT_BSTR
&& rightvt
!= VT_BSTR
)
2695 VarBstrCat (V_BSTR(&bstrvar_left
), V_BSTR(&bstrvar_right
), &V_BSTR(out
));
2696 else if (leftvt
!= VT_BSTR
&& rightvt
== VT_BSTR
)
2697 VarBstrCat (V_BSTR(&bstrvar_left
), V_BSTR(right
), &V_BSTR(out
));
2698 else if (leftvt
== VT_BSTR
&& rightvt
!= VT_BSTR
)
2699 VarBstrCat (V_BSTR(left
), V_BSTR(&bstrvar_right
), &V_BSTR(out
));
2701 VariantClear(&bstrvar_left
);
2702 VariantClear(&bstrvar_right
);
2708 /* Wrapper around VariantChangeTypeEx() which permits changing a
2709 variant with VT_RESERVED flag set. Needed by VarCmp. */
2710 static HRESULT
_VarChangeTypeExWrap (VARIANTARG
* pvargDest
,
2711 VARIANTARG
* pvargSrc
, LCID lcid
, USHORT wFlags
, VARTYPE vt
)
2713 VARIANTARG vtmpsrc
= *pvargSrc
;
2715 V_VT(&vtmpsrc
) &= ~VT_RESERVED
;
2716 return VariantChangeTypeEx(pvargDest
,&vtmpsrc
,lcid
,wFlags
,vt
);
2719 /**********************************************************************
2720 * VarCmp [OLEAUT32.176]
2722 * Compare two variants.
2725 * left [I] First variant
2726 * right [I] Second variant
2727 * lcid [I] LCID (locale identifier) for the comparison
2728 * flags [I] Flags to be used in the comparison:
2729 * NORM_IGNORECASE, NORM_IGNORENONSPACE, NORM_IGNORESYMBOLS,
2730 * NORM_IGNOREWIDTH, NORM_IGNOREKANATYPE, NORM_IGNOREKASHIDA
2733 * VARCMP_LT: left variant is less than right variant.
2734 * VARCMP_EQ: input variants are equal.
2735 * VARCMP_GT: left variant is greater than right variant.
2736 * VARCMP_NULL: either one of the input variants is NULL.
2737 * Failure: An HRESULT error code indicating the error.
2740 * Native VarCmp up to and including WinXP doesn't like I1, UI2, VT_UI4,
2741 * UI8 and UINT as input variants. INT is accepted only as left variant.
2743 * If both input variants are ERROR then VARCMP_EQ will be returned, else
2744 * an ERROR variant will trigger an error.
2746 * Both input variants can have VT_RESERVED flag set which is ignored
2747 * unless one and only one of the variants is a BSTR and the other one
2748 * is not an EMPTY variant. All four VT_RESERVED combinations have a
2749 * different meaning:
2750 * - BSTR and other: BSTR is always greater than the other variant.
2751 * - BSTR|VT_RESERVED and other: a string comparison is performed.
2752 * - BSTR and other|VT_RESERVED: If the BSTR is a number a numeric
2753 * comparison will take place else the BSTR is always greater.
2754 * - BSTR|VT_RESERVED and other|VT_RESERVED: It seems that the other
2755 * variant is ignored and the return value depends only on the sign
2756 * of the BSTR if it is a number else the BSTR is always greater. A
2757 * positive BSTR is greater, a negative one is smaller than the other
2761 * VarBstrCmp for the lcid and flags usage.
2763 HRESULT WINAPI
VarCmp(LPVARIANT left
, LPVARIANT right
, LCID lcid
, DWORD flags
)
2765 VARTYPE lvt
, rvt
, vt
;
2770 TRACE("(%s,%s,0x%08x,0x%08x)\n", debugstr_variant(left
), debugstr_variant(right
), lcid
, flags
);
2772 lvt
= V_VT(left
) & VT_TYPEMASK
;
2773 rvt
= V_VT(right
) & VT_TYPEMASK
;
2774 xmask
= (1 << lvt
) | (1 << rvt
);
2776 /* If we have any flag set except VT_RESERVED bail out.
2777 Same for the left input variant type > VT_INT and for the
2778 right input variant type > VT_I8. Yes, VT_INT is only supported
2779 as left variant. Go figure */
2780 if (((V_VT(left
) | V_VT(right
)) & ~VT_TYPEMASK
& ~VT_RESERVED
) ||
2781 lvt
> VT_INT
|| rvt
> VT_I8
) {
2782 return DISP_E_BADVARTYPE
;
2785 /* Don't ask me why but native VarCmp cannot handle: VT_I1, VT_UI2, VT_UI4,
2786 VT_UINT and VT_UI8. Tested with DCOM98, Win2k, WinXP */
2787 if (rvt
== VT_INT
|| xmask
& (VTBIT_I1
| VTBIT_UI2
| VTBIT_UI4
| VTBIT_UI8
|
2788 VTBIT_DISPATCH
| VTBIT_VARIANT
| VTBIT_UNKNOWN
| VTBIT_15
))
2789 return DISP_E_TYPEMISMATCH
;
2791 /* If both variants are VT_ERROR return VARCMP_EQ */
2792 if (xmask
== VTBIT_ERROR
)
2794 else if (xmask
& VTBIT_ERROR
)
2795 return DISP_E_TYPEMISMATCH
;
2797 if (xmask
& VTBIT_NULL
)
2803 /* Two BSTRs, ignore VT_RESERVED */
2804 if (xmask
== VTBIT_BSTR
)
2805 return VarBstrCmp(V_BSTR(left
), V_BSTR(right
), lcid
, flags
);
2807 /* A BSTR and another variant; we have to take care of VT_RESERVED */
2808 if (xmask
& VTBIT_BSTR
) {
2809 VARIANT
*bstrv
, *nonbv
;
2813 /* Swap the variants so the BSTR is always on the left */
2814 if (lvt
== VT_BSTR
) {
2825 /* BSTR and EMPTY: ignore VT_RESERVED */
2826 if (nonbvt
== VT_EMPTY
)
2827 rc
= (!V_BSTR(bstrv
) || !*V_BSTR(bstrv
)) ? VARCMP_EQ
: VARCMP_GT
;
2829 VARTYPE breserv
= V_VT(bstrv
) & ~VT_TYPEMASK
;
2830 VARTYPE nreserv
= V_VT(nonbv
) & ~VT_TYPEMASK
;
2832 if (!breserv
&& !nreserv
)
2833 /* No VT_RESERVED set ==> BSTR always greater */
2835 else if (breserv
&& !nreserv
) {
2836 /* BSTR has VT_RESERVED set. Do a string comparison */
2837 rc
= VariantChangeTypeEx(&rv
,nonbv
,lcid
,0,VT_BSTR
);
2840 rc
= VarBstrCmp(V_BSTR(bstrv
), V_BSTR(&rv
), lcid
, flags
);
2842 } else if (V_BSTR(bstrv
) && *V_BSTR(bstrv
)) {
2843 /* Non NULL nor empty BSTR */
2844 /* If the BSTR is not a number the BSTR is greater */
2845 rc
= _VarChangeTypeExWrap(&lv
,bstrv
,lcid
,0,VT_R8
);
2848 else if (breserv
&& nreserv
)
2849 /* FIXME: This is strange: with both VT_RESERVED set it
2850 looks like the result depends only on the sign of
2852 rc
= (V_R8(&lv
) >= 0) ? VARCMP_GT
: VARCMP_LT
;
2854 /* Numeric comparison, will be handled below.
2855 VARCMP_NULL used only to break out. */
2860 /* Empty or NULL BSTR */
2863 /* Fixup the return code if we swapped left and right */
2865 if (rc
== VARCMP_GT
)
2867 else if (rc
== VARCMP_LT
)
2870 if (rc
!= VARCMP_NULL
)
2874 if (xmask
& VTBIT_DECIMAL
)
2876 else if (xmask
& VTBIT_BSTR
)
2878 else if (xmask
& VTBIT_R4
)
2880 else if (xmask
& (VTBIT_R8
| VTBIT_DATE
))
2882 else if (xmask
& VTBIT_CY
)
2888 /* Coerce the variants */
2889 rc
= _VarChangeTypeExWrap(&lv
,left
,lcid
,0,vt
);
2890 if (rc
== DISP_E_OVERFLOW
&& vt
!= VT_R8
) {
2891 /* Overflow, change to R8 */
2893 rc
= _VarChangeTypeExWrap(&lv
,left
,lcid
,0,vt
);
2897 rc
= _VarChangeTypeExWrap(&rv
,right
,lcid
,0,vt
);
2898 if (rc
== DISP_E_OVERFLOW
&& vt
!= VT_R8
) {
2899 /* Overflow, change to R8 */
2901 rc
= _VarChangeTypeExWrap(&lv
,left
,lcid
,0,vt
);
2904 rc
= _VarChangeTypeExWrap(&rv
,right
,lcid
,0,vt
);
2909 #define _VARCMP(a,b) \
2910 (((a) == (b)) ? VARCMP_EQ : (((a) < (b)) ? VARCMP_LT : VARCMP_GT))
2914 return VarCyCmp(V_CY(&lv
), V_CY(&rv
));
2916 return VarDecCmp(&V_DECIMAL(&lv
), &V_DECIMAL(&rv
));
2918 return _VARCMP(V_I8(&lv
), V_I8(&rv
));
2920 return _VARCMP(V_R4(&lv
), V_R4(&rv
));
2922 return _VARCMP(V_R8(&lv
), V_R8(&rv
));
2924 /* We should never get here */
2930 static HRESULT
VARIANT_FetchDispatchValue(LPVARIANT pvDispatch
, LPVARIANT pValue
)
2933 static DISPPARAMS emptyParams
= { NULL
, NULL
, 0, 0 };
2935 if ((V_VT(pvDispatch
) & VT_TYPEMASK
) == VT_DISPATCH
) {
2936 if (NULL
== V_DISPATCH(pvDispatch
)) return DISP_E_TYPEMISMATCH
;
2937 hres
= IDispatch_Invoke(V_DISPATCH(pvDispatch
), DISPID_VALUE
, &IID_NULL
,
2938 LOCALE_USER_DEFAULT
, DISPATCH_PROPERTYGET
, &emptyParams
, pValue
,
2941 hres
= DISP_E_TYPEMISMATCH
;
2946 /**********************************************************************
2947 * VarAnd [OLEAUT32.142]
2949 * Computes the logical AND of two variants.
2952 * left [I] First variant
2953 * right [I] Second variant
2954 * result [O] Result variant
2958 * Failure: An HRESULT error code indicating the error.
2960 HRESULT WINAPI
VarAnd(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
2962 HRESULT hres
= S_OK
;
2963 VARTYPE resvt
= VT_EMPTY
;
2964 VARTYPE leftvt
,rightvt
;
2965 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
2966 VARIANT varLeft
, varRight
;
2967 VARIANT tempLeft
, tempRight
;
2969 VariantInit(&varLeft
);
2970 VariantInit(&varRight
);
2971 VariantInit(&tempLeft
);
2972 VariantInit(&tempRight
);
2974 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
2976 /* Handle VT_DISPATCH by storing and taking address of returned value */
2977 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
2979 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
2980 if (FAILED(hres
)) goto VarAnd_Exit
;
2983 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
2985 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
2986 if (FAILED(hres
)) goto VarAnd_Exit
;
2990 leftvt
= V_VT(left
)&VT_TYPEMASK
;
2991 rightvt
= V_VT(right
)&VT_TYPEMASK
;
2992 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
2993 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
2995 if (leftExtraFlags
!= rightExtraFlags
)
2997 hres
= DISP_E_BADVARTYPE
;
3000 ExtraFlags
= leftExtraFlags
;
3002 /* Native VarAnd always returns an error when using extra
3003 * flags or if the variant combination is I8 and INT.
3005 if ((leftvt
== VT_I8
&& rightvt
== VT_INT
) ||
3006 (leftvt
== VT_INT
&& rightvt
== VT_I8
) ||
3009 hres
= DISP_E_BADVARTYPE
;
3013 /* Determine return type */
3014 else if (leftvt
== VT_I8
|| rightvt
== VT_I8
)
3016 else if (leftvt
== VT_I4
|| rightvt
== VT_I4
||
3017 leftvt
== VT_UINT
|| rightvt
== VT_UINT
||
3018 leftvt
== VT_INT
|| rightvt
== VT_INT
||
3019 leftvt
== VT_R4
|| rightvt
== VT_R4
||
3020 leftvt
== VT_R8
|| rightvt
== VT_R8
||
3021 leftvt
== VT_CY
|| rightvt
== VT_CY
||
3022 leftvt
== VT_DATE
|| rightvt
== VT_DATE
||
3023 leftvt
== VT_I1
|| rightvt
== VT_I1
||
3024 leftvt
== VT_UI2
|| rightvt
== VT_UI2
||
3025 leftvt
== VT_UI4
|| rightvt
== VT_UI4
||
3026 leftvt
== VT_UI8
|| rightvt
== VT_UI8
||
3027 leftvt
== VT_DECIMAL
|| rightvt
== VT_DECIMAL
)
3029 else if (leftvt
== VT_UI1
|| rightvt
== VT_UI1
||
3030 leftvt
== VT_I2
|| rightvt
== VT_I2
||
3031 leftvt
== VT_EMPTY
|| rightvt
== VT_EMPTY
)
3032 if ((leftvt
== VT_NULL
&& rightvt
== VT_UI1
) ||
3033 (leftvt
== VT_UI1
&& rightvt
== VT_NULL
) ||
3034 (leftvt
== VT_UI1
&& rightvt
== VT_UI1
))
3038 else if (leftvt
== VT_BOOL
|| rightvt
== VT_BOOL
||
3039 (leftvt
== VT_BSTR
&& rightvt
== VT_BSTR
))
3041 else if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
||
3042 leftvt
== VT_BSTR
|| rightvt
== VT_BSTR
)
3046 hres
= DISP_E_BADVARTYPE
;
3050 if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
)
3053 * Special cases for when left variant is VT_NULL
3054 * (VT_NULL & 0 = VT_NULL, VT_NULL & value = value)
3056 if (leftvt
== VT_NULL
)
3061 case VT_I1
: if (V_I1(right
)) resvt
= VT_NULL
; break;
3062 case VT_UI1
: if (V_UI1(right
)) resvt
= VT_NULL
; break;
3063 case VT_I2
: if (V_I2(right
)) resvt
= VT_NULL
; break;
3064 case VT_UI2
: if (V_UI2(right
)) resvt
= VT_NULL
; break;
3065 case VT_I4
: if (V_I4(right
)) resvt
= VT_NULL
; break;
3066 case VT_UI4
: if (V_UI4(right
)) resvt
= VT_NULL
; break;
3067 case VT_I8
: if (V_I8(right
)) resvt
= VT_NULL
; break;
3068 case VT_UI8
: if (V_UI8(right
)) resvt
= VT_NULL
; break;
3069 case VT_INT
: if (V_INT(right
)) resvt
= VT_NULL
; break;
3070 case VT_UINT
: if (V_UINT(right
)) resvt
= VT_NULL
; break;
3071 case VT_BOOL
: if (V_BOOL(right
)) resvt
= VT_NULL
; break;
3072 case VT_R4
: if (V_R4(right
)) resvt
= VT_NULL
; break;
3073 case VT_R8
: if (V_R8(right
)) resvt
= VT_NULL
; break;
3075 if(V_CY(right
).int64
)
3079 if (DEC_HI32(&V_DECIMAL(right
)) ||
3080 DEC_LO64(&V_DECIMAL(right
)))
3084 hres
= VarBoolFromStr(V_BSTR(right
),
3085 LOCALE_USER_DEFAULT
, VAR_LOCALBOOL
, &b
);
3089 V_VT(result
) = VT_NULL
;
3092 V_VT(result
) = VT_BOOL
;
3098 V_VT(result
) = resvt
;
3102 hres
= VariantCopy(&varLeft
, left
);
3103 if (FAILED(hres
)) goto VarAnd_Exit
;
3105 hres
= VariantCopy(&varRight
, right
);
3106 if (FAILED(hres
)) goto VarAnd_Exit
;
3108 if (resvt
== VT_I4
&& V_VT(&varLeft
) == VT_UI4
)
3109 V_VT(&varLeft
) = VT_I4
; /* Don't overflow */
3114 if (V_VT(&varLeft
) == VT_BSTR
&&
3115 FAILED(VarR8FromStr(V_BSTR(&varLeft
),
3116 LOCALE_USER_DEFAULT
, 0, &d
)))
3117 hres
= VariantChangeType(&varLeft
,&varLeft
,
3118 VARIANT_LOCALBOOL
, VT_BOOL
);
3119 if (SUCCEEDED(hres
) && V_VT(&varLeft
) != resvt
)
3120 hres
= VariantChangeType(&varLeft
,&varLeft
,0,resvt
);
3121 if (FAILED(hres
)) goto VarAnd_Exit
;
3124 if (resvt
== VT_I4
&& V_VT(&varRight
) == VT_UI4
)
3125 V_VT(&varRight
) = VT_I4
; /* Don't overflow */
3130 if (V_VT(&varRight
) == VT_BSTR
&&
3131 FAILED(VarR8FromStr(V_BSTR(&varRight
),
3132 LOCALE_USER_DEFAULT
, 0, &d
)))
3133 hres
= VariantChangeType(&varRight
, &varRight
,
3134 VARIANT_LOCALBOOL
, VT_BOOL
);
3135 if (SUCCEEDED(hres
) && V_VT(&varRight
) != resvt
)
3136 hres
= VariantChangeType(&varRight
, &varRight
, 0, resvt
);
3137 if (FAILED(hres
)) goto VarAnd_Exit
;
3140 V_VT(result
) = resvt
;
3144 V_I8(result
) = V_I8(&varLeft
) & V_I8(&varRight
);
3147 V_I4(result
) = V_I4(&varLeft
) & V_I4(&varRight
);
3150 V_I2(result
) = V_I2(&varLeft
) & V_I2(&varRight
);
3153 V_UI1(result
) = V_UI1(&varLeft
) & V_UI1(&varRight
);
3156 V_BOOL(result
) = V_BOOL(&varLeft
) & V_BOOL(&varRight
);
3159 FIXME("Couldn't bitwise AND variant types %d,%d\n",
3164 VariantClear(&varLeft
);
3165 VariantClear(&varRight
);
3166 VariantClear(&tempLeft
);
3167 VariantClear(&tempRight
);
3172 /**********************************************************************
3173 * VarAdd [OLEAUT32.141]
3178 * left [I] First variant
3179 * right [I] Second variant
3180 * result [O] Result variant
3184 * Failure: An HRESULT error code indicating the error.
3187 * Native VarAdd up to and including WinXP doesn't like I1, UI2, UI4,
3188 * UI8, INT and UINT as input variants.
3190 * Native VarAdd doesn't check for NULL in/out pointers and crashes. We do the
3194 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3197 HRESULT WINAPI
VarAdd(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3200 VARTYPE lvt
, rvt
, resvt
, tvt
;
3202 VARIANT tempLeft
, tempRight
;
3205 /* Variant priority for coercion. Sorted from lowest to highest.
3206 VT_ERROR shows an invalid input variant type. */
3207 enum coerceprio
{ vt_EMPTY
, vt_UI1
, vt_I2
, vt_I4
, vt_I8
, vt_BSTR
,vt_R4
,
3208 vt_R8
, vt_CY
, vt_DATE
, vt_DECIMAL
, vt_DISPATCH
, vt_NULL
,
3210 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3211 static const VARTYPE prio2vt
[] = { VT_EMPTY
, VT_UI1
, VT_I2
, VT_I4
, VT_I8
, VT_BSTR
, VT_R4
,
3212 VT_R8
, VT_CY
, VT_DATE
, VT_DECIMAL
, VT_DISPATCH
,
3213 VT_NULL
, VT_ERROR
};
3215 /* Mapping for coercion from input variant to priority of result variant. */
3216 static const VARTYPE coerce
[] = {
3217 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3218 vt_EMPTY
, vt_NULL
, vt_I2
, vt_I4
, vt_R4
,
3219 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3220 vt_R8
, vt_CY
, vt_DATE
, vt_BSTR
, vt_DISPATCH
,
3221 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3222 vt_ERROR
, vt_I2
, vt_ERROR
, vt_ERROR
, vt_DECIMAL
,
3223 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3224 vt_ERROR
, vt_ERROR
, vt_UI1
, vt_ERROR
, vt_ERROR
, vt_I8
3227 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
3232 VariantInit(&tempLeft
);
3233 VariantInit(&tempRight
);
3235 /* Handle VT_DISPATCH by storing and taking address of returned value */
3236 if ((V_VT(left
) & VT_TYPEMASK
) != VT_NULL
&& (V_VT(right
) & VT_TYPEMASK
) != VT_NULL
)
3238 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
3240 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
3241 if (FAILED(hres
)) goto end
;
3244 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
3246 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
3247 if (FAILED(hres
)) goto end
;
3252 lvt
= V_VT(left
)&VT_TYPEMASK
;
3253 rvt
= V_VT(right
)&VT_TYPEMASK
;
3255 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3256 Same for any input variant type > VT_I8 */
3257 if (V_VT(left
) & ~VT_TYPEMASK
|| V_VT(right
) & ~VT_TYPEMASK
||
3258 lvt
> VT_I8
|| rvt
> VT_I8
) {
3259 hres
= DISP_E_BADVARTYPE
;
3263 /* Determine the variant type to coerce to. */
3264 if (coerce
[lvt
] > coerce
[rvt
]) {
3265 resvt
= prio2vt
[coerce
[lvt
]];
3266 tvt
= prio2vt
[coerce
[rvt
]];
3268 resvt
= prio2vt
[coerce
[rvt
]];
3269 tvt
= prio2vt
[coerce
[lvt
]];
3272 /* Special cases where the result variant type is defined by both
3273 input variants and not only that with the highest priority */
3274 if (resvt
== VT_BSTR
) {
3275 if (tvt
== VT_EMPTY
|| tvt
== VT_BSTR
)
3280 if (resvt
== VT_R4
&& (tvt
== VT_BSTR
|| tvt
== VT_I8
|| tvt
== VT_I4
))
3283 /* For overflow detection use the biggest compatible type for the
3287 hres
= DISP_E_BADVARTYPE
;
3291 V_VT(result
) = VT_NULL
;
3294 FIXME("cannot handle variant type VT_DISPATCH\n");
3295 hres
= DISP_E_TYPEMISMATCH
;
3314 /* Now coerce the variants */
3315 hres
= VariantChangeType(&lv
, left
, 0, tvt
);
3318 hres
= VariantChangeType(&rv
, right
, 0, tvt
);
3324 V_VT(result
) = resvt
;
3327 hres
= VarDecAdd(&V_DECIMAL(&lv
), &V_DECIMAL(&rv
),
3328 &V_DECIMAL(result
));
3331 hres
= VarCyAdd(V_CY(&lv
), V_CY(&rv
), &V_CY(result
));
3334 /* We do not add those, we concatenate them. */
3335 hres
= VarBstrCat(V_BSTR(&lv
), V_BSTR(&rv
), &V_BSTR(result
));
3338 /* Overflow detection */
3339 r8res
= (double)V_I8(&lv
) + (double)V_I8(&rv
);
3340 if (r8res
> (double)I8_MAX
|| r8res
< (double)I8_MIN
) {
3341 V_VT(result
) = VT_R8
;
3342 V_R8(result
) = r8res
;
3346 V_I8(&tv
) = V_I8(&lv
) + V_I8(&rv
);
3351 /* FIXME: overflow detection */
3352 V_R8(&tv
) = V_R8(&lv
) + V_R8(&rv
);
3355 ERR("We shouldn't get here! tvt = %d!\n", tvt
);
3359 if ((hres
= VariantChangeType(result
, &tv
, 0, resvt
)) != S_OK
) {
3360 /* Overflow! Change to the vartype with the next higher priority.
3361 With one exception: I4 ==> R8 even if it would fit in I8 */
3365 resvt
= prio2vt
[coerce
[resvt
] + 1];
3366 hres
= VariantChangeType(result
, &tv
, 0, resvt
);
3369 hres
= VariantCopy(result
, &tv
);
3373 V_VT(result
) = VT_EMPTY
;
3374 V_I4(result
) = 0; /* No V_EMPTY */
3379 VariantClear(&tempLeft
);
3380 VariantClear(&tempRight
);
3381 TRACE("returning 0x%8x %s\n", hres
, debugstr_variant(result
));
3385 /**********************************************************************
3386 * VarMul [OLEAUT32.156]
3388 * Multiply two variants.
3391 * left [I] First variant
3392 * right [I] Second variant
3393 * result [O] Result variant
3397 * Failure: An HRESULT error code indicating the error.
3400 * Native VarMul up to and including WinXP doesn't like I1, UI2, UI4,
3401 * UI8, INT and UINT as input variants. But it can multiply apples with oranges.
3403 * Native VarMul doesn't check for NULL in/out pointers and crashes. We do the
3407 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3410 HRESULT WINAPI
VarMul(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3413 VARTYPE lvt
, rvt
, resvt
, tvt
;
3415 VARIANT tempLeft
, tempRight
;
3418 /* Variant priority for coercion. Sorted from lowest to highest.
3419 VT_ERROR shows an invalid input variant type. */
3420 enum coerceprio
{ vt_UI1
= 0, vt_I2
, vt_I4
, vt_I8
, vt_CY
, vt_R4
, vt_R8
,
3421 vt_DECIMAL
, vt_NULL
, vt_ERROR
};
3422 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3423 static const VARTYPE prio2vt
[] = { VT_UI1
, VT_I2
, VT_I4
, VT_I8
, VT_CY
, VT_R4
, VT_R8
,
3424 VT_DECIMAL
, VT_NULL
, VT_ERROR
};
3426 /* Mapping for coercion from input variant to priority of result variant. */
3427 static const VARTYPE coerce
[] = {
3428 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3429 vt_UI1
, vt_NULL
, vt_I2
, vt_I4
, vt_R4
,
3430 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3431 vt_R8
, vt_CY
, vt_R8
, vt_R8
, vt_ERROR
,
3432 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3433 vt_ERROR
, vt_I2
, vt_ERROR
, vt_ERROR
, vt_DECIMAL
,
3434 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3435 vt_ERROR
, vt_ERROR
, vt_UI1
, vt_ERROR
, vt_ERROR
, vt_I8
3438 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
3443 VariantInit(&tempLeft
);
3444 VariantInit(&tempRight
);
3446 /* Handle VT_DISPATCH by storing and taking address of returned value */
3447 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
3449 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
3450 if (FAILED(hres
)) goto end
;
3453 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
3455 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
3456 if (FAILED(hres
)) goto end
;
3460 lvt
= V_VT(left
)&VT_TYPEMASK
;
3461 rvt
= V_VT(right
)&VT_TYPEMASK
;
3463 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3464 Same for any input variant type > VT_I8 */
3465 if (V_VT(left
) & ~VT_TYPEMASK
|| V_VT(right
) & ~VT_TYPEMASK
||
3466 lvt
> VT_I8
|| rvt
> VT_I8
) {
3467 hres
= DISP_E_BADVARTYPE
;
3471 /* Determine the variant type to coerce to. */
3472 if (coerce
[lvt
] > coerce
[rvt
]) {
3473 resvt
= prio2vt
[coerce
[lvt
]];
3474 tvt
= prio2vt
[coerce
[rvt
]];
3476 resvt
= prio2vt
[coerce
[rvt
]];
3477 tvt
= prio2vt
[coerce
[lvt
]];
3480 /* Special cases where the result variant type is defined by both
3481 input variants and not only that with the highest priority */
3482 if (resvt
== VT_R4
&& (tvt
== VT_CY
|| tvt
== VT_I8
|| tvt
== VT_I4
))
3484 if (lvt
== VT_EMPTY
&& rvt
== VT_EMPTY
)
3487 /* For overflow detection use the biggest compatible type for the
3491 hres
= DISP_E_BADVARTYPE
;
3495 V_VT(result
) = VT_NULL
;
3510 /* Now coerce the variants */
3511 hres
= VariantChangeType(&lv
, left
, 0, tvt
);
3514 hres
= VariantChangeType(&rv
, right
, 0, tvt
);
3521 V_VT(result
) = resvt
;
3524 hres
= VarDecMul(&V_DECIMAL(&lv
), &V_DECIMAL(&rv
),
3525 &V_DECIMAL(result
));
3528 hres
= VarCyMul(V_CY(&lv
), V_CY(&rv
), &V_CY(result
));
3531 /* Overflow detection */
3532 r8res
= (double)V_I8(&lv
) * (double)V_I8(&rv
);
3533 if (r8res
> (double)I8_MAX
|| r8res
< (double)I8_MIN
) {
3534 V_VT(result
) = VT_R8
;
3535 V_R8(result
) = r8res
;
3538 V_I8(&tv
) = V_I8(&lv
) * V_I8(&rv
);
3541 /* FIXME: overflow detection */
3542 V_R8(&tv
) = V_R8(&lv
) * V_R8(&rv
);
3545 ERR("We shouldn't get here! tvt = %d!\n", tvt
);
3549 while ((hres
= VariantChangeType(result
, &tv
, 0, resvt
)) != S_OK
) {
3550 /* Overflow! Change to the vartype with the next higher priority.
3551 With one exception: I4 ==> R8 even if it would fit in I8 */
3555 resvt
= prio2vt
[coerce
[resvt
] + 1];
3558 hres
= VariantCopy(result
, &tv
);
3562 V_VT(result
) = VT_EMPTY
;
3563 V_I4(result
) = 0; /* No V_EMPTY */
3568 VariantClear(&tempLeft
);
3569 VariantClear(&tempRight
);
3570 TRACE("returning 0x%8x %s\n", hres
, debugstr_variant(result
));
3574 /**********************************************************************
3575 * VarDiv [OLEAUT32.143]
3577 * Divides one variant with another.
3580 * left [I] First variant
3581 * right [I] Second variant
3582 * result [O] Result variant
3586 * Failure: An HRESULT error code indicating the error.
3588 HRESULT WINAPI
VarDiv(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3590 HRESULT hres
= S_OK
;
3591 VARTYPE resvt
= VT_EMPTY
;
3592 VARTYPE leftvt
,rightvt
;
3593 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
3595 VARIANT tempLeft
, tempRight
;
3597 VariantInit(&tempLeft
);
3598 VariantInit(&tempRight
);
3602 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
3604 /* Handle VT_DISPATCH by storing and taking address of returned value */
3605 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
3607 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
3608 if (FAILED(hres
)) goto end
;
3611 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
3613 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
3614 if (FAILED(hres
)) goto end
;
3618 leftvt
= V_VT(left
)&VT_TYPEMASK
;
3619 rightvt
= V_VT(right
)&VT_TYPEMASK
;
3620 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
3621 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
3623 if (leftExtraFlags
!= rightExtraFlags
)
3625 hres
= DISP_E_BADVARTYPE
;
3628 ExtraFlags
= leftExtraFlags
;
3630 /* Native VarDiv always returns an error when using extra flags */
3631 if (ExtraFlags
!= 0)
3633 hres
= DISP_E_BADVARTYPE
;
3637 /* Determine return type */
3638 if (!(rightvt
== VT_EMPTY
))
3640 if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
)
3642 V_VT(result
) = VT_NULL
;
3646 else if (leftvt
== VT_DECIMAL
|| rightvt
== VT_DECIMAL
)
3648 else if (leftvt
== VT_I8
|| rightvt
== VT_I8
||
3649 leftvt
== VT_CY
|| rightvt
== VT_CY
||
3650 leftvt
== VT_DATE
|| rightvt
== VT_DATE
||
3651 leftvt
== VT_I4
|| rightvt
== VT_I4
||
3652 leftvt
== VT_BSTR
|| rightvt
== VT_BSTR
||
3653 leftvt
== VT_I2
|| rightvt
== VT_I2
||
3654 leftvt
== VT_BOOL
|| rightvt
== VT_BOOL
||
3655 leftvt
== VT_R8
|| rightvt
== VT_R8
||
3656 leftvt
== VT_UI1
|| rightvt
== VT_UI1
)
3658 if ((leftvt
== VT_UI1
&& rightvt
== VT_R4
) ||
3659 (leftvt
== VT_R4
&& rightvt
== VT_UI1
))
3661 else if ((leftvt
== VT_R4
&& (rightvt
== VT_BOOL
||
3662 rightvt
== VT_I2
)) || (rightvt
== VT_R4
&&
3663 (leftvt
== VT_BOOL
|| leftvt
== VT_I2
)))
3668 else if (leftvt
== VT_R4
|| rightvt
== VT_R4
)
3671 else if (leftvt
== VT_NULL
&& rightvt
== VT_EMPTY
)
3673 V_VT(result
) = VT_NULL
;
3679 hres
= DISP_E_BADVARTYPE
;
3683 /* coerce to the result type */
3684 hres
= VariantChangeType(&lv
, left
, 0, resvt
);
3685 if (hres
!= S_OK
) goto end
;
3687 hres
= VariantChangeType(&rv
, right
, 0, resvt
);
3688 if (hres
!= S_OK
) goto end
;
3691 V_VT(result
) = resvt
;
3695 if (V_R4(&lv
) == 0.0 && V_R4(&rv
) == 0.0)
3697 hres
= DISP_E_OVERFLOW
;
3698 V_VT(result
) = VT_EMPTY
;
3700 else if (V_R4(&rv
) == 0.0)
3702 hres
= DISP_E_DIVBYZERO
;
3703 V_VT(result
) = VT_EMPTY
;
3706 V_R4(result
) = V_R4(&lv
) / V_R4(&rv
);
3709 if (V_R8(&lv
) == 0.0 && V_R8(&rv
) == 0.0)
3711 hres
= DISP_E_OVERFLOW
;
3712 V_VT(result
) = VT_EMPTY
;
3714 else if (V_R8(&rv
) == 0.0)
3716 hres
= DISP_E_DIVBYZERO
;
3717 V_VT(result
) = VT_EMPTY
;
3720 V_R8(result
) = V_R8(&lv
) / V_R8(&rv
);
3723 hres
= VarDecDiv(&(V_DECIMAL(&lv
)), &(V_DECIMAL(&rv
)), &(V_DECIMAL(result
)));
3730 VariantClear(&tempLeft
);
3731 VariantClear(&tempRight
);
3732 TRACE("returning 0x%8x %s\n", hres
, debugstr_variant(result
));
3736 /**********************************************************************
3737 * VarSub [OLEAUT32.159]
3739 * Subtract two variants.
3742 * left [I] First variant
3743 * right [I] Second variant
3744 * result [O] Result variant
3748 * Failure: An HRESULT error code indicating the error.
3750 HRESULT WINAPI
VarSub(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3752 HRESULT hres
= S_OK
;
3753 VARTYPE resvt
= VT_EMPTY
;
3754 VARTYPE leftvt
,rightvt
;
3755 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
3757 VARIANT tempLeft
, tempRight
;
3761 VariantInit(&tempLeft
);
3762 VariantInit(&tempRight
);
3764 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
3766 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
&&
3767 (V_VT(left
)&(~VT_TYPEMASK
)) == 0 &&
3768 (V_VT(right
) & VT_TYPEMASK
) != VT_NULL
)
3770 if (NULL
== V_DISPATCH(left
)) {
3771 if ((V_VT(right
) & VT_TYPEMASK
) >= VT_INT_PTR
)
3772 hres
= DISP_E_BADVARTYPE
;
3773 else if ((V_VT(right
) & VT_TYPEMASK
) >= VT_UI8
&&
3774 (V_VT(right
) & VT_TYPEMASK
) < VT_RECORD
)
3775 hres
= DISP_E_BADVARTYPE
;
3776 else switch (V_VT(right
) & VT_TYPEMASK
)
3784 hres
= DISP_E_BADVARTYPE
;
3786 if (FAILED(hres
)) goto end
;
3788 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
3789 if (FAILED(hres
)) goto end
;
3792 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
&&
3793 (V_VT(right
)&(~VT_TYPEMASK
)) == 0 &&
3794 (V_VT(left
) & VT_TYPEMASK
) != VT_NULL
)
3796 if (NULL
== V_DISPATCH(right
))
3798 if ((V_VT(left
) & VT_TYPEMASK
) >= VT_INT_PTR
)
3799 hres
= DISP_E_BADVARTYPE
;
3800 else if ((V_VT(left
) & VT_TYPEMASK
) >= VT_UI8
&&
3801 (V_VT(left
) & VT_TYPEMASK
) < VT_RECORD
)
3802 hres
= DISP_E_BADVARTYPE
;
3803 else switch (V_VT(left
) & VT_TYPEMASK
)
3811 hres
= DISP_E_BADVARTYPE
;
3813 if (FAILED(hres
)) goto end
;
3815 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
3816 if (FAILED(hres
)) goto end
;
3820 leftvt
= V_VT(left
)&VT_TYPEMASK
;
3821 rightvt
= V_VT(right
)&VT_TYPEMASK
;
3822 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
3823 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
3825 if (leftExtraFlags
!= rightExtraFlags
)
3827 hres
= DISP_E_BADVARTYPE
;
3830 ExtraFlags
= leftExtraFlags
;
3832 /* determine return type and return code */
3833 /* All extra flags produce errors */
3834 if (ExtraFlags
== (VT_VECTOR
|VT_BYREF
|VT_RESERVED
) ||
3835 ExtraFlags
== (VT_VECTOR
|VT_RESERVED
) ||
3836 ExtraFlags
== (VT_VECTOR
|VT_BYREF
) ||
3837 ExtraFlags
== (VT_BYREF
|VT_RESERVED
) ||
3838 ExtraFlags
== VT_VECTOR
||
3839 ExtraFlags
== VT_BYREF
||
3840 ExtraFlags
== VT_RESERVED
)
3842 hres
= DISP_E_BADVARTYPE
;
3845 else if (ExtraFlags
>= VT_ARRAY
)
3847 hres
= DISP_E_TYPEMISMATCH
;
3850 /* Native VarSub cannot handle: VT_I1, VT_UI2, VT_UI4,
3851 VT_INT, VT_UINT and VT_UI8. Tested with WinXP */
3852 else if (leftvt
== VT_CLSID
|| rightvt
== VT_CLSID
||
3853 leftvt
== VT_VARIANT
|| rightvt
== VT_VARIANT
||
3854 leftvt
== VT_I1
|| rightvt
== VT_I1
||
3855 leftvt
== VT_UI2
|| rightvt
== VT_UI2
||
3856 leftvt
== VT_UI4
|| rightvt
== VT_UI4
||
3857 leftvt
== VT_UI8
|| rightvt
== VT_UI8
||
3858 leftvt
== VT_INT
|| rightvt
== VT_INT
||
3859 leftvt
== VT_UINT
|| rightvt
== VT_UINT
||
3860 leftvt
== VT_UNKNOWN
|| rightvt
== VT_UNKNOWN
||
3861 leftvt
== VT_RECORD
|| rightvt
== VT_RECORD
)
3863 if (leftvt
== VT_RECORD
&& rightvt
== VT_I8
)
3864 hres
= DISP_E_TYPEMISMATCH
;
3865 else if (leftvt
< VT_UI1
&& rightvt
== VT_RECORD
)
3866 hres
= DISP_E_TYPEMISMATCH
;
3867 else if (leftvt
>= VT_UI1
&& rightvt
== VT_RECORD
)
3868 hres
= DISP_E_TYPEMISMATCH
;
3869 else if (leftvt
== VT_RECORD
&& rightvt
<= VT_UI1
)
3870 hres
= DISP_E_TYPEMISMATCH
;
3871 else if (leftvt
== VT_RECORD
&& rightvt
> VT_UI1
)
3872 hres
= DISP_E_BADVARTYPE
;
3874 hres
= DISP_E_BADVARTYPE
;
3877 /* The following flags/types are invalid for left variant */
3878 else if (!((leftvt
<= VT_LPWSTR
|| leftvt
== VT_RECORD
||
3879 leftvt
== VT_CLSID
) && leftvt
!= (VARTYPE
)15 /* undefined vt */ &&
3880 (leftvt
< VT_VOID
|| leftvt
> VT_LPWSTR
)))
3882 hres
= DISP_E_BADVARTYPE
;
3885 /* The following flags/types are invalid for right variant */
3886 else if (!((rightvt
<= VT_LPWSTR
|| rightvt
== VT_RECORD
||
3887 rightvt
== VT_CLSID
) && rightvt
!= (VARTYPE
)15 /* undefined vt */ &&
3888 (rightvt
< VT_VOID
|| rightvt
> VT_LPWSTR
)))
3890 hres
= DISP_E_BADVARTYPE
;
3893 else if ((leftvt
== VT_NULL
&& rightvt
== VT_DISPATCH
) ||
3894 (leftvt
== VT_DISPATCH
&& rightvt
== VT_NULL
))
3896 else if (leftvt
== VT_DISPATCH
|| rightvt
== VT_DISPATCH
||
3897 leftvt
== VT_ERROR
|| rightvt
== VT_ERROR
)
3899 hres
= DISP_E_TYPEMISMATCH
;
3902 else if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
)
3904 else if ((leftvt
== VT_EMPTY
&& rightvt
== VT_BSTR
) ||
3905 (leftvt
== VT_DATE
&& rightvt
== VT_DATE
) ||
3906 (leftvt
== VT_BSTR
&& rightvt
== VT_EMPTY
) ||
3907 (leftvt
== VT_BSTR
&& rightvt
== VT_BSTR
))
3909 else if (leftvt
== VT_DECIMAL
|| rightvt
== VT_DECIMAL
)
3911 else if (leftvt
== VT_DATE
|| rightvt
== VT_DATE
)
3913 else if (leftvt
== VT_CY
|| rightvt
== VT_CY
)
3915 else if (leftvt
== VT_R8
|| rightvt
== VT_R8
)
3917 else if (leftvt
== VT_BSTR
|| rightvt
== VT_BSTR
)
3919 else if (leftvt
== VT_R4
|| rightvt
== VT_R4
)
3921 if (leftvt
== VT_I4
|| rightvt
== VT_I4
||
3922 leftvt
== VT_I8
|| rightvt
== VT_I8
)
3927 else if (leftvt
== VT_I8
|| rightvt
== VT_I8
)
3929 else if (leftvt
== VT_I4
|| rightvt
== VT_I4
)
3931 else if (leftvt
== VT_I2
|| rightvt
== VT_I2
||
3932 leftvt
== VT_BOOL
|| rightvt
== VT_BOOL
||
3933 (leftvt
== VT_EMPTY
&& rightvt
== VT_EMPTY
))
3935 else if (leftvt
== VT_UI1
|| rightvt
== VT_UI1
)
3939 hres
= DISP_E_TYPEMISMATCH
;
3943 /* coerce to the result type */
3944 if (leftvt
== VT_BSTR
&& rightvt
== VT_DATE
)
3945 hres
= VariantChangeType(&lv
, left
, 0, VT_R8
);
3947 hres
= VariantChangeType(&lv
, left
, 0, resvt
);
3948 if (hres
!= S_OK
) goto end
;
3949 if (leftvt
== VT_DATE
&& rightvt
== VT_BSTR
)
3950 hres
= VariantChangeType(&rv
, right
, 0, VT_R8
);
3952 hres
= VariantChangeType(&rv
, right
, 0, resvt
);
3953 if (hres
!= S_OK
) goto end
;
3956 V_VT(result
) = resvt
;
3962 V_DATE(result
) = V_DATE(&lv
) - V_DATE(&rv
);
3965 hres
= VarCySub(V_CY(&lv
), V_CY(&rv
), &(V_CY(result
)));
3968 V_R4(result
) = V_R4(&lv
) - V_R4(&rv
);
3971 V_I8(result
) = V_I8(&lv
) - V_I8(&rv
);
3974 V_I4(result
) = V_I4(&lv
) - V_I4(&rv
);
3977 V_I2(result
) = V_I2(&lv
) - V_I2(&rv
);
3980 V_I1(result
) = V_I1(&lv
) - V_I1(&rv
);
3983 V_UI1(result
) = V_UI2(&lv
) - V_UI1(&rv
);
3986 V_R8(result
) = V_R8(&lv
) - V_R8(&rv
);
3989 hres
= VarDecSub(&(V_DECIMAL(&lv
)), &(V_DECIMAL(&rv
)), &(V_DECIMAL(result
)));
3996 VariantClear(&tempLeft
);
3997 VariantClear(&tempRight
);
3998 TRACE("returning 0x%8x %s\n", hres
, debugstr_variant(result
));
4003 /**********************************************************************
4004 * VarOr [OLEAUT32.157]
4006 * Perform a logical or (OR) operation on two variants.
4009 * pVarLeft [I] First variant
4010 * pVarRight [I] Variant to OR with pVarLeft
4011 * pVarOut [O] Destination for OR result
4014 * Success: S_OK. pVarOut contains the result of the operation with its type
4015 * taken from the table listed under VarXor().
4016 * Failure: An HRESULT error code indicating the error.
4019 * See the Notes section of VarXor() for further information.
4021 HRESULT WINAPI
VarOr(LPVARIANT pVarLeft
, LPVARIANT pVarRight
, LPVARIANT pVarOut
)
4024 VARIANT varLeft
, varRight
, varStr
;
4026 VARIANT tempLeft
, tempRight
;
4028 VariantInit(&tempLeft
);
4029 VariantInit(&tempRight
);
4030 VariantInit(&varLeft
);
4031 VariantInit(&varRight
);
4032 VariantInit(&varStr
);
4034 TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft
), debugstr_variant(pVarRight
), pVarOut
);
4036 /* Handle VT_DISPATCH by storing and taking address of returned value */
4037 if ((V_VT(pVarLeft
) & VT_TYPEMASK
) == VT_DISPATCH
)
4039 hRet
= VARIANT_FetchDispatchValue(pVarLeft
, &tempLeft
);
4040 if (FAILED(hRet
)) goto VarOr_Exit
;
4041 pVarLeft
= &tempLeft
;
4043 if ((V_VT(pVarRight
) & VT_TYPEMASK
) == VT_DISPATCH
)
4045 hRet
= VARIANT_FetchDispatchValue(pVarRight
, &tempRight
);
4046 if (FAILED(hRet
)) goto VarOr_Exit
;
4047 pVarRight
= &tempRight
;
4050 if (V_EXTRA_TYPE(pVarLeft
) || V_EXTRA_TYPE(pVarRight
) ||
4051 V_VT(pVarLeft
) == VT_UNKNOWN
|| V_VT(pVarRight
) == VT_UNKNOWN
||
4052 V_VT(pVarLeft
) == VT_DISPATCH
|| V_VT(pVarRight
) == VT_DISPATCH
||
4053 V_VT(pVarLeft
) == VT_RECORD
|| V_VT(pVarRight
) == VT_RECORD
)
4055 hRet
= DISP_E_BADVARTYPE
;
4059 V_VT(&varLeft
) = V_VT(&varRight
) = V_VT(&varStr
) = VT_EMPTY
;
4061 if (V_VT(pVarLeft
) == VT_NULL
|| V_VT(pVarRight
) == VT_NULL
)
4063 /* NULL OR Zero is NULL, NULL OR value is value */
4064 if (V_VT(pVarLeft
) == VT_NULL
)
4065 pVarLeft
= pVarRight
; /* point to the non-NULL var */
4067 V_VT(pVarOut
) = VT_NULL
;
4070 switch (V_VT(pVarLeft
))
4072 case VT_DATE
: case VT_R8
:
4078 if (V_BOOL(pVarLeft
))
4079 *pVarOut
= *pVarLeft
;
4082 case VT_I2
: case VT_UI2
:
4093 if (V_UI1(pVarLeft
))
4094 *pVarOut
= *pVarLeft
;
4102 case VT_I4
: case VT_UI4
: case VT_INT
: case VT_UINT
:
4108 if (V_CY(pVarLeft
).int64
)
4112 case VT_I8
: case VT_UI8
:
4118 if (DEC_HI32(&V_DECIMAL(pVarLeft
)) || DEC_LO64(&V_DECIMAL(pVarLeft
)))
4126 if (!V_BSTR(pVarLeft
))
4128 hRet
= DISP_E_BADVARTYPE
;
4132 hRet
= VarBoolFromStr(V_BSTR(pVarLeft
), LOCALE_USER_DEFAULT
, VAR_LOCALBOOL
, &b
);
4133 if (SUCCEEDED(hRet
) && b
)
4135 V_VT(pVarOut
) = VT_BOOL
;
4136 V_BOOL(pVarOut
) = b
;
4140 case VT_NULL
: case VT_EMPTY
:
4141 V_VT(pVarOut
) = VT_NULL
;
4145 hRet
= DISP_E_BADVARTYPE
;
4150 if (V_VT(pVarLeft
) == VT_EMPTY
|| V_VT(pVarRight
) == VT_EMPTY
)
4152 if (V_VT(pVarLeft
) == VT_EMPTY
)
4153 pVarLeft
= pVarRight
; /* point to the non-EMPTY var */
4156 /* Since one argument is empty (0), OR'ing it with the other simply
4157 * gives the others value (as 0|x => x). So just convert the other
4158 * argument to the required result type.
4160 switch (V_VT(pVarLeft
))
4163 if (!V_BSTR(pVarLeft
))
4165 hRet
= DISP_E_BADVARTYPE
;
4169 hRet
= VariantCopy(&varStr
, pVarLeft
);
4173 hRet
= VariantChangeType(pVarLeft
, pVarLeft
, 0, VT_BOOL
);
4176 /* Fall Through ... */
4177 case VT_EMPTY
: case VT_UI1
: case VT_BOOL
: case VT_I2
:
4178 V_VT(pVarOut
) = VT_I2
;
4180 case VT_DATE
: case VT_CY
: case VT_DECIMAL
: case VT_R4
: case VT_R8
:
4181 case VT_I1
: case VT_UI2
: case VT_I4
: case VT_UI4
:
4182 case VT_INT
: case VT_UINT
: case VT_UI8
:
4183 V_VT(pVarOut
) = VT_I4
;
4186 V_VT(pVarOut
) = VT_I8
;
4189 hRet
= DISP_E_BADVARTYPE
;
4192 hRet
= VariantCopy(&varLeft
, pVarLeft
);
4195 pVarLeft
= &varLeft
;
4196 hRet
= VariantChangeType(pVarOut
, pVarLeft
, 0, V_VT(pVarOut
));
4200 if (V_VT(pVarLeft
) == VT_BOOL
&& V_VT(pVarRight
) == VT_BOOL
)
4202 V_VT(pVarOut
) = VT_BOOL
;
4203 V_BOOL(pVarOut
) = V_BOOL(pVarLeft
) | V_BOOL(pVarRight
);
4208 if (V_VT(pVarLeft
) == VT_UI1
&& V_VT(pVarRight
) == VT_UI1
)
4210 V_VT(pVarOut
) = VT_UI1
;
4211 V_UI1(pVarOut
) = V_UI1(pVarLeft
) | V_UI1(pVarRight
);
4216 if (V_VT(pVarLeft
) == VT_BSTR
)
4218 hRet
= VariantCopy(&varStr
, pVarLeft
);
4222 hRet
= VariantChangeType(pVarLeft
, pVarLeft
, 0, VT_BOOL
);
4227 if (V_VT(pVarLeft
) == VT_BOOL
&&
4228 (V_VT(pVarRight
) == VT_BOOL
|| V_VT(pVarRight
) == VT_BSTR
))
4232 else if ((V_VT(pVarLeft
) == VT_BOOL
|| V_VT(pVarLeft
) == VT_UI1
||
4233 V_VT(pVarLeft
) == VT_I2
|| V_VT(pVarLeft
) == VT_BSTR
) &&
4234 (V_VT(pVarRight
) == VT_BOOL
|| V_VT(pVarRight
) == VT_UI1
||
4235 V_VT(pVarRight
) == VT_I2
|| V_VT(pVarRight
) == VT_BSTR
))
4239 else if (V_VT(pVarLeft
) == VT_I8
|| V_VT(pVarRight
) == VT_I8
)
4241 if (V_VT(pVarLeft
) == VT_INT
|| V_VT(pVarRight
) == VT_INT
)
4243 hRet
= DISP_E_TYPEMISMATCH
;
4249 hRet
= VariantCopy(&varLeft
, pVarLeft
);
4253 hRet
= VariantCopy(&varRight
, pVarRight
);
4257 if (vt
== VT_I4
&& V_VT(&varLeft
) == VT_UI4
)
4258 V_VT(&varLeft
) = VT_I4
; /* Don't overflow */
4263 if (V_VT(&varLeft
) == VT_BSTR
&&
4264 FAILED(VarR8FromStr(V_BSTR(&varLeft
), LOCALE_USER_DEFAULT
, 0, &d
)))
4265 hRet
= VariantChangeType(&varLeft
, &varLeft
, VARIANT_LOCALBOOL
, VT_BOOL
);
4266 if (SUCCEEDED(hRet
) && V_VT(&varLeft
) != vt
)
4267 hRet
= VariantChangeType(&varLeft
, &varLeft
, 0, vt
);
4272 if (vt
== VT_I4
&& V_VT(&varRight
) == VT_UI4
)
4273 V_VT(&varRight
) = VT_I4
; /* Don't overflow */
4278 if (V_VT(&varRight
) == VT_BSTR
&&
4279 FAILED(VarR8FromStr(V_BSTR(&varRight
), LOCALE_USER_DEFAULT
, 0, &d
)))
4280 hRet
= VariantChangeType(&varRight
, &varRight
, VARIANT_LOCALBOOL
, VT_BOOL
);
4281 if (SUCCEEDED(hRet
) && V_VT(&varRight
) != vt
)
4282 hRet
= VariantChangeType(&varRight
, &varRight
, 0, vt
);
4290 V_I8(pVarOut
) = V_I8(&varLeft
) | V_I8(&varRight
);
4292 else if (vt
== VT_I4
)
4294 V_I4(pVarOut
) = V_I4(&varLeft
) | V_I4(&varRight
);
4298 V_I2(pVarOut
) = V_I2(&varLeft
) | V_I2(&varRight
);
4302 VariantClear(&varStr
);
4303 VariantClear(&varLeft
);
4304 VariantClear(&varRight
);
4305 VariantClear(&tempLeft
);
4306 VariantClear(&tempRight
);
4310 /**********************************************************************
4311 * VarAbs [OLEAUT32.168]
4313 * Convert a variant to its absolute value.
4316 * pVarIn [I] Source variant
4317 * pVarOut [O] Destination for converted value
4320 * Success: S_OK. pVarOut contains the absolute value of pVarIn.
4321 * Failure: An HRESULT error code indicating the error.
4324 * - This function does not process by-reference variants.
4325 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4326 * according to the following table:
4327 *| Input Type Output Type
4328 *| ---------- -----------
4331 *| (All others) Unchanged
4333 HRESULT WINAPI
VarAbs(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
4336 HRESULT hRet
= S_OK
;
4341 TRACE("(%s,%p)\n", debugstr_variant(pVarIn
), pVarOut
);
4343 /* Handle VT_DISPATCH by storing and taking address of returned value */
4344 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
4346 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
4347 if (FAILED(hRet
)) goto VarAbs_Exit
;
4351 if (V_ISARRAY(pVarIn
) || V_VT(pVarIn
) == VT_UNKNOWN
||
4352 V_VT(pVarIn
) == VT_DISPATCH
|| V_VT(pVarIn
) == VT_RECORD
||
4353 V_VT(pVarIn
) == VT_ERROR
)
4355 hRet
= DISP_E_TYPEMISMATCH
;
4358 *pVarOut
= *pVarIn
; /* Shallow copy the value, and invert it if needed */
4360 #define ABS_CASE(typ,min) \
4361 case VT_##typ: if (V_##typ(pVarIn) == min) hRet = DISP_E_OVERFLOW; \
4362 else if (V_##typ(pVarIn) < 0) V_##typ(pVarOut) = -V_##typ(pVarIn); \
4365 switch (V_VT(pVarIn
))
4367 ABS_CASE(I1
,I1_MIN
);
4369 V_VT(pVarOut
) = VT_I2
;
4370 /* BOOL->I2, Fall through ... */
4371 ABS_CASE(I2
,I2_MIN
);
4373 ABS_CASE(I4
,I4_MIN
);
4374 ABS_CASE(I8
,I8_MIN
);
4375 ABS_CASE(R4
,R4_MIN
);
4377 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(&varIn
));
4380 V_VT(pVarOut
) = VT_R8
;
4382 /* Fall through ... */
4384 ABS_CASE(R8
,R8_MIN
);
4386 hRet
= VarCyAbs(V_CY(pVarIn
), & V_CY(pVarOut
));
4389 DEC_SIGN(&V_DECIMAL(pVarOut
)) &= ~DECIMAL_NEG
;
4399 V_VT(pVarOut
) = VT_I2
;
4404 hRet
= DISP_E_BADVARTYPE
;
4408 VariantClear(&temp
);
4412 /**********************************************************************
4413 * VarFix [OLEAUT32.169]
4415 * Truncate a variants value to a whole number.
4418 * pVarIn [I] Source variant
4419 * pVarOut [O] Destination for converted value
4422 * Success: S_OK. pVarOut contains the converted value.
4423 * Failure: An HRESULT error code indicating the error.
4426 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4427 * according to the following table:
4428 *| Input Type Output Type
4429 *| ---------- -----------
4433 *| All Others Unchanged
4434 * - The difference between this function and VarInt() is that VarInt() rounds
4435 * negative numbers away from 0, while this function rounds them towards zero.
4437 HRESULT WINAPI
VarFix(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
4439 HRESULT hRet
= S_OK
;
4444 TRACE("(%s,%p)\n", debugstr_variant(pVarIn
), pVarOut
);
4446 /* Handle VT_DISPATCH by storing and taking address of returned value */
4447 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
4449 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
4450 if (FAILED(hRet
)) goto VarFix_Exit
;
4453 V_VT(pVarOut
) = V_VT(pVarIn
);
4455 switch (V_VT(pVarIn
))
4458 V_UI1(pVarOut
) = V_UI1(pVarIn
);
4461 V_VT(pVarOut
) = VT_I2
;
4464 V_I2(pVarOut
) = V_I2(pVarIn
);
4467 V_I4(pVarOut
) = V_I4(pVarIn
);
4470 V_I8(pVarOut
) = V_I8(pVarIn
);
4473 if (V_R4(pVarIn
) < 0.0f
)
4474 V_R4(pVarOut
) = (float)ceil(V_R4(pVarIn
));
4476 V_R4(pVarOut
) = (float)floor(V_R4(pVarIn
));
4479 V_VT(pVarOut
) = VT_R8
;
4480 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(pVarOut
));
4485 if (V_R8(pVarIn
) < 0.0)
4486 V_R8(pVarOut
) = ceil(V_R8(pVarIn
));
4488 V_R8(pVarOut
) = floor(V_R8(pVarIn
));
4491 hRet
= VarCyFix(V_CY(pVarIn
), &V_CY(pVarOut
));
4494 hRet
= VarDecFix(&V_DECIMAL(pVarIn
), &V_DECIMAL(pVarOut
));
4497 V_VT(pVarOut
) = VT_I2
;
4504 if (V_TYPE(pVarIn
) == VT_CLSID
|| /* VT_CLSID is a special case */
4505 FAILED(VARIANT_ValidateType(V_VT(pVarIn
))))
4506 hRet
= DISP_E_BADVARTYPE
;
4508 hRet
= DISP_E_TYPEMISMATCH
;
4512 V_VT(pVarOut
) = VT_EMPTY
;
4513 VariantClear(&temp
);
4518 /**********************************************************************
4519 * VarInt [OLEAUT32.172]
4521 * Truncate a variants value to a whole number.
4524 * pVarIn [I] Source variant
4525 * pVarOut [O] Destination for converted value
4528 * Success: S_OK. pVarOut contains the converted value.
4529 * Failure: An HRESULT error code indicating the error.
4532 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4533 * according to the following table:
4534 *| Input Type Output Type
4535 *| ---------- -----------
4539 *| All Others Unchanged
4540 * - The difference between this function and VarFix() is that VarFix() rounds
4541 * negative numbers towards 0, while this function rounds them away from zero.
4543 HRESULT WINAPI
VarInt(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
4545 HRESULT hRet
= S_OK
;
4550 TRACE("(%s,%p)\n", debugstr_variant(pVarIn
), pVarOut
);
4552 /* Handle VT_DISPATCH by storing and taking address of returned value */
4553 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
4555 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
4556 if (FAILED(hRet
)) goto VarInt_Exit
;
4559 V_VT(pVarOut
) = V_VT(pVarIn
);
4561 switch (V_VT(pVarIn
))
4564 V_R4(pVarOut
) = (float)floor(V_R4(pVarIn
));
4567 V_VT(pVarOut
) = VT_R8
;
4568 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(pVarOut
));
4573 V_R8(pVarOut
) = floor(V_R8(pVarIn
));
4576 hRet
= VarCyInt(V_CY(pVarIn
), &V_CY(pVarOut
));
4579 hRet
= VarDecInt(&V_DECIMAL(pVarIn
), &V_DECIMAL(pVarOut
));
4582 hRet
= VarFix(pVarIn
, pVarOut
);
4585 VariantClear(&temp
);
4590 /**********************************************************************
4591 * VarXor [OLEAUT32.167]
4593 * Perform a logical exclusive-or (XOR) operation on two variants.
4596 * pVarLeft [I] First variant
4597 * pVarRight [I] Variant to XOR with pVarLeft
4598 * pVarOut [O] Destination for XOR result
4601 * Success: S_OK. pVarOut contains the result of the operation with its type
4602 * taken from the table below).
4603 * Failure: An HRESULT error code indicating the error.
4606 * - Neither pVarLeft or pVarRight are modified by this function.
4607 * - This function does not process by-reference variants.
4608 * - Input types of VT_BSTR may be numeric strings or boolean text.
4609 * - The type of result stored in pVarOut depends on the types of pVarLeft
4610 * and pVarRight, and will be one of VT_UI1, VT_I2, VT_I4, VT_I8, VT_BOOL,
4611 * or VT_NULL if the function succeeds.
4612 * - Type promotion is inconsistent and as a result certain combinations of
4613 * values will return DISP_E_OVERFLOW even when they could be represented.
4614 * This matches the behaviour of native oleaut32.
4616 HRESULT WINAPI
VarXor(LPVARIANT pVarLeft
, LPVARIANT pVarRight
, LPVARIANT pVarOut
)
4619 VARIANT varLeft
, varRight
;
4620 VARIANT tempLeft
, tempRight
;
4624 TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft
), debugstr_variant(pVarRight
), pVarOut
);
4626 if (V_EXTRA_TYPE(pVarLeft
) || V_EXTRA_TYPE(pVarRight
) ||
4627 V_VT(pVarLeft
) > VT_UINT
|| V_VT(pVarRight
) > VT_UINT
||
4628 V_VT(pVarLeft
) == VT_VARIANT
|| V_VT(pVarRight
) == VT_VARIANT
||
4629 V_VT(pVarLeft
) == VT_UNKNOWN
|| V_VT(pVarRight
) == VT_UNKNOWN
||
4630 V_VT(pVarLeft
) == (VARTYPE
)15 || V_VT(pVarRight
) == (VARTYPE
)15 ||
4631 V_VT(pVarLeft
) == VT_ERROR
|| V_VT(pVarRight
) == VT_ERROR
)
4632 return DISP_E_BADVARTYPE
;
4634 if (V_VT(pVarLeft
) == VT_NULL
|| V_VT(pVarRight
) == VT_NULL
)
4636 /* NULL XOR anything valid is NULL */
4637 V_VT(pVarOut
) = VT_NULL
;
4641 VariantInit(&tempLeft
);
4642 VariantInit(&tempRight
);
4644 /* Handle VT_DISPATCH by storing and taking address of returned value */
4645 if ((V_VT(pVarLeft
) & VT_TYPEMASK
) == VT_DISPATCH
)
4647 hRet
= VARIANT_FetchDispatchValue(pVarLeft
, &tempLeft
);
4648 if (FAILED(hRet
)) goto VarXor_Exit
;
4649 pVarLeft
= &tempLeft
;
4651 if ((V_VT(pVarRight
) & VT_TYPEMASK
) == VT_DISPATCH
)
4653 hRet
= VARIANT_FetchDispatchValue(pVarRight
, &tempRight
);
4654 if (FAILED(hRet
)) goto VarXor_Exit
;
4655 pVarRight
= &tempRight
;
4658 /* Copy our inputs so we don't disturb anything */
4659 V_VT(&varLeft
) = V_VT(&varRight
) = VT_EMPTY
;
4661 hRet
= VariantCopy(&varLeft
, pVarLeft
);
4665 hRet
= VariantCopy(&varRight
, pVarRight
);
4669 /* Try any strings first as numbers, then as VT_BOOL */
4670 if (V_VT(&varLeft
) == VT_BSTR
)
4672 hRet
= VarR8FromStr(V_BSTR(&varLeft
), LOCALE_USER_DEFAULT
, 0, &d
);
4673 hRet
= VariantChangeType(&varLeft
, &varLeft
, VARIANT_LOCALBOOL
,
4674 FAILED(hRet
) ? VT_BOOL
: VT_I4
);
4679 if (V_VT(&varRight
) == VT_BSTR
)
4681 hRet
= VarR8FromStr(V_BSTR(&varRight
), LOCALE_USER_DEFAULT
, 0, &d
);
4682 hRet
= VariantChangeType(&varRight
, &varRight
, VARIANT_LOCALBOOL
,
4683 FAILED(hRet
) ? VT_BOOL
: VT_I4
);
4688 /* Determine the result type */
4689 if (V_VT(&varLeft
) == VT_I8
|| V_VT(&varRight
) == VT_I8
)
4691 if (V_VT(pVarLeft
) == VT_INT
|| V_VT(pVarRight
) == VT_INT
)
4693 hRet
= DISP_E_TYPEMISMATCH
;
4700 switch ((V_VT(&varLeft
) << 16) | V_VT(&varRight
))
4702 case (VT_BOOL
<< 16) | VT_BOOL
:
4705 case (VT_UI1
<< 16) | VT_UI1
:
4708 case (VT_EMPTY
<< 16) | VT_EMPTY
:
4709 case (VT_EMPTY
<< 16) | VT_UI1
:
4710 case (VT_EMPTY
<< 16) | VT_I2
:
4711 case (VT_EMPTY
<< 16) | VT_BOOL
:
4712 case (VT_UI1
<< 16) | VT_EMPTY
:
4713 case (VT_UI1
<< 16) | VT_I2
:
4714 case (VT_UI1
<< 16) | VT_BOOL
:
4715 case (VT_I2
<< 16) | VT_EMPTY
:
4716 case (VT_I2
<< 16) | VT_UI1
:
4717 case (VT_I2
<< 16) | VT_I2
:
4718 case (VT_I2
<< 16) | VT_BOOL
:
4719 case (VT_BOOL
<< 16) | VT_EMPTY
:
4720 case (VT_BOOL
<< 16) | VT_UI1
:
4721 case (VT_BOOL
<< 16) | VT_I2
:
4730 /* VT_UI4 does not overflow */
4733 if (V_VT(&varLeft
) == VT_UI4
)
4734 V_VT(&varLeft
) = VT_I4
;
4735 if (V_VT(&varRight
) == VT_UI4
)
4736 V_VT(&varRight
) = VT_I4
;
4739 /* Convert our input copies to the result type */
4740 if (V_VT(&varLeft
) != vt
)
4741 hRet
= VariantChangeType(&varLeft
, &varLeft
, 0, vt
);
4745 if (V_VT(&varRight
) != vt
)
4746 hRet
= VariantChangeType(&varRight
, &varRight
, 0, vt
);
4752 /* Calculate the result */
4756 V_I8(pVarOut
) = V_I8(&varLeft
) ^ V_I8(&varRight
);
4759 V_I4(pVarOut
) = V_I4(&varLeft
) ^ V_I4(&varRight
);
4763 V_I2(pVarOut
) = V_I2(&varLeft
) ^ V_I2(&varRight
);
4766 V_UI1(pVarOut
) = V_UI1(&varLeft
) ^ V_UI1(&varRight
);
4771 VariantClear(&varLeft
);
4772 VariantClear(&varRight
);
4773 VariantClear(&tempLeft
);
4774 VariantClear(&tempRight
);
4778 /**********************************************************************
4779 * VarEqv [OLEAUT32.172]
4781 * Determine if two variants contain the same value.
4784 * pVarLeft [I] First variant to compare
4785 * pVarRight [I] Variant to compare to pVarLeft
4786 * pVarOut [O] Destination for comparison result
4789 * Success: S_OK. pVarOut contains the result of the comparison (VARIANT_TRUE
4790 * if equivalent or non-zero otherwise.
4791 * Failure: An HRESULT error code indicating the error.
4794 * - This function simply calls VarXor() on pVarLeft and pVarRight and inverts
4797 HRESULT WINAPI
VarEqv(LPVARIANT pVarLeft
, LPVARIANT pVarRight
, LPVARIANT pVarOut
)
4801 TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft
), debugstr_variant(pVarRight
), pVarOut
);
4803 hRet
= VarXor(pVarLeft
, pVarRight
, pVarOut
);
4804 if (SUCCEEDED(hRet
))
4806 if (V_VT(pVarOut
) == VT_I8
)
4807 V_I8(pVarOut
) = ~V_I8(pVarOut
);
4809 V_UI4(pVarOut
) = ~V_UI4(pVarOut
);
4814 /**********************************************************************
4815 * VarNeg [OLEAUT32.173]
4817 * Negate the value of a variant.
4820 * pVarIn [I] Source variant
4821 * pVarOut [O] Destination for converted value
4824 * Success: S_OK. pVarOut contains the converted value.
4825 * Failure: An HRESULT error code indicating the error.
4828 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4829 * according to the following table:
4830 *| Input Type Output Type
4831 *| ---------- -----------
4836 *| All Others Unchanged (unless promoted)
4837 * - Where the negated value of a variant does not fit in its base type, the type
4838 * is promoted according to the following table:
4839 *| Input Type Promoted To
4840 *| ---------- -----------
4844 * - The native version of this function returns DISP_E_BADVARTYPE for valid
4845 * variant types that cannot be negated, and returns DISP_E_TYPEMISMATCH
4846 * for types which are not valid. Since this is in contravention of the
4847 * meaning of those error codes and unlikely to be relied on by applications,
4848 * this implementation returns errors consistent with the other high level
4849 * variant math functions.
4851 HRESULT WINAPI
VarNeg(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
4853 HRESULT hRet
= S_OK
;
4858 TRACE("(%s,%p)\n", debugstr_variant(pVarIn
), pVarOut
);
4860 /* Handle VT_DISPATCH by storing and taking address of returned value */
4861 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
4863 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
4864 if (FAILED(hRet
)) goto VarNeg_Exit
;
4867 V_VT(pVarOut
) = V_VT(pVarIn
);
4869 switch (V_VT(pVarIn
))
4872 V_VT(pVarOut
) = VT_I2
;
4873 V_I2(pVarOut
) = -V_UI1(pVarIn
);
4876 V_VT(pVarOut
) = VT_I2
;
4879 if (V_I2(pVarIn
) == I2_MIN
)
4881 V_VT(pVarOut
) = VT_I4
;
4882 V_I4(pVarOut
) = -(int)V_I2(pVarIn
);
4885 V_I2(pVarOut
) = -V_I2(pVarIn
);
4888 if (V_I4(pVarIn
) == I4_MIN
)
4890 V_VT(pVarOut
) = VT_R8
;
4891 V_R8(pVarOut
) = -(double)V_I4(pVarIn
);
4894 V_I4(pVarOut
) = -V_I4(pVarIn
);
4897 if (V_I8(pVarIn
) == I8_MIN
)
4899 V_VT(pVarOut
) = VT_R8
;
4900 hRet
= VarR8FromI8(V_I8(pVarIn
), &V_R8(pVarOut
));
4901 V_R8(pVarOut
) *= -1.0;
4904 V_I8(pVarOut
) = -V_I8(pVarIn
);
4907 V_R4(pVarOut
) = -V_R4(pVarIn
);
4911 V_R8(pVarOut
) = -V_R8(pVarIn
);
4914 hRet
= VarCyNeg(V_CY(pVarIn
), &V_CY(pVarOut
));
4917 hRet
= VarDecNeg(&V_DECIMAL(pVarIn
), &V_DECIMAL(pVarOut
));
4920 V_VT(pVarOut
) = VT_R8
;
4921 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(pVarOut
));
4922 V_R8(pVarOut
) = -V_R8(pVarOut
);
4925 V_VT(pVarOut
) = VT_I2
;
4932 if (V_TYPE(pVarIn
) == VT_CLSID
|| /* VT_CLSID is a special case */
4933 FAILED(VARIANT_ValidateType(V_VT(pVarIn
))))
4934 hRet
= DISP_E_BADVARTYPE
;
4936 hRet
= DISP_E_TYPEMISMATCH
;
4940 V_VT(pVarOut
) = VT_EMPTY
;
4941 VariantClear(&temp
);
4946 /**********************************************************************
4947 * VarNot [OLEAUT32.174]
4949 * Perform a not operation on a variant.
4952 * pVarIn [I] Source variant
4953 * pVarOut [O] Destination for converted value
4956 * Success: S_OK. pVarOut contains the converted value.
4957 * Failure: An HRESULT error code indicating the error.
4960 * - Strictly speaking, this function performs a bitwise ones complement
4961 * on the variants value (after possibly converting to VT_I4, see below).
4962 * This only behaves like a boolean not operation if the value in
4963 * pVarIn is either VARIANT_TRUE or VARIANT_FALSE and the type is signed.
4964 * - To perform a genuine not operation, convert the variant to a VT_BOOL
4965 * before calling this function.
4966 * - This function does not process by-reference variants.
4967 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4968 * according to the following table:
4969 *| Input Type Output Type
4970 *| ---------- -----------
4977 *| (All others) Unchanged
4979 HRESULT WINAPI
VarNot(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
4982 HRESULT hRet
= S_OK
;
4987 TRACE("(%s,%p)\n", debugstr_variant(pVarIn
), pVarOut
);
4989 /* Handle VT_DISPATCH by storing and taking address of returned value */
4990 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
4992 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
4993 if (FAILED(hRet
)) goto VarNot_Exit
;
4997 if (V_VT(pVarIn
) == VT_BSTR
)
4999 V_VT(&varIn
) = VT_R8
;
5000 hRet
= VarR8FromStr( V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(&varIn
) );
5003 V_VT(&varIn
) = VT_BOOL
;
5004 hRet
= VarBoolFromStr( V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, VAR_LOCALBOOL
, &V_BOOL(&varIn
) );
5006 if (FAILED(hRet
)) goto VarNot_Exit
;
5010 V_VT(pVarOut
) = V_VT(pVarIn
);
5012 switch (V_VT(pVarIn
))
5015 V_I4(pVarOut
) = ~V_I1(pVarIn
);
5016 V_VT(pVarOut
) = VT_I4
;
5018 case VT_UI1
: V_UI1(pVarOut
) = ~V_UI1(pVarIn
); break;
5020 case VT_I2
: V_I2(pVarOut
) = ~V_I2(pVarIn
); break;
5022 V_I4(pVarOut
) = ~V_UI2(pVarIn
);
5023 V_VT(pVarOut
) = VT_I4
;
5026 hRet
= VarI4FromDec(&V_DECIMAL(pVarIn
), &V_I4(&varIn
));
5030 /* Fall through ... */
5032 V_VT(pVarOut
) = VT_I4
;
5033 /* Fall through ... */
5034 case VT_I4
: V_I4(pVarOut
) = ~V_I4(pVarIn
); break;
5037 V_I4(pVarOut
) = ~V_UI4(pVarIn
);
5038 V_VT(pVarOut
) = VT_I4
;
5040 case VT_I8
: V_I8(pVarOut
) = ~V_I8(pVarIn
); break;
5042 V_I4(pVarOut
) = ~V_UI8(pVarIn
);
5043 V_VT(pVarOut
) = VT_I4
;
5046 hRet
= VarI4FromR4(V_R4(pVarIn
), &V_I4(pVarOut
));
5047 V_I4(pVarOut
) = ~V_I4(pVarOut
);
5048 V_VT(pVarOut
) = VT_I4
;
5052 hRet
= VarI4FromR8(V_R8(pVarIn
), &V_I4(pVarOut
));
5053 V_I4(pVarOut
) = ~V_I4(pVarOut
);
5054 V_VT(pVarOut
) = VT_I4
;
5057 hRet
= VarI4FromCy(V_CY(pVarIn
), &V_I4(pVarOut
));
5058 V_I4(pVarOut
) = ~V_I4(pVarOut
);
5059 V_VT(pVarOut
) = VT_I4
;
5063 V_VT(pVarOut
) = VT_I2
;
5069 if (V_TYPE(pVarIn
) == VT_CLSID
|| /* VT_CLSID is a special case */
5070 FAILED(VARIANT_ValidateType(V_VT(pVarIn
))))
5071 hRet
= DISP_E_BADVARTYPE
;
5073 hRet
= DISP_E_TYPEMISMATCH
;
5077 V_VT(pVarOut
) = VT_EMPTY
;
5078 VariantClear(&temp
);
5083 /**********************************************************************
5084 * VarRound [OLEAUT32.175]
5086 * Perform a round operation on a variant.
5089 * pVarIn [I] Source variant
5090 * deci [I] Number of decimals to round to
5091 * pVarOut [O] Destination for converted value
5094 * Success: S_OK. pVarOut contains the converted value.
5095 * Failure: An HRESULT error code indicating the error.
5098 * - Floating point values are rounded to the desired number of decimals.
5099 * - Some integer types are just copied to the return variable.
5100 * - Some other integer types are not handled and fail.
5102 HRESULT WINAPI
VarRound(LPVARIANT pVarIn
, int deci
, LPVARIANT pVarOut
)
5105 HRESULT hRet
= S_OK
;
5111 TRACE("(%s,%d)\n", debugstr_variant(pVarIn
), deci
);
5113 /* Handle VT_DISPATCH by storing and taking address of returned value */
5114 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
5116 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
5117 if (FAILED(hRet
)) goto VarRound_Exit
;
5121 switch (V_VT(pVarIn
))
5123 /* cases that fail on windows */
5128 hRet
= DISP_E_BADVARTYPE
;
5131 /* cases just copying in to out */
5133 V_VT(pVarOut
) = V_VT(pVarIn
);
5134 V_UI1(pVarOut
) = V_UI1(pVarIn
);
5137 V_VT(pVarOut
) = V_VT(pVarIn
);
5138 V_I2(pVarOut
) = V_I2(pVarIn
);
5141 V_VT(pVarOut
) = V_VT(pVarIn
);
5142 V_I4(pVarOut
) = V_I4(pVarIn
);
5145 V_VT(pVarOut
) = V_VT(pVarIn
);
5146 /* value unchanged */
5149 /* cases that change type */
5151 V_VT(pVarOut
) = VT_I2
;
5155 V_VT(pVarOut
) = VT_I2
;
5156 V_I2(pVarOut
) = V_BOOL(pVarIn
);
5159 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(&varIn
));
5164 /* Fall through ... */
5166 /* cases we need to do math */
5168 if (V_R8(pVarIn
)>0) {
5169 V_R8(pVarOut
)=floor(V_R8(pVarIn
)*pow(10, deci
)+0.5)/pow(10, deci
);
5171 V_R8(pVarOut
)=ceil(V_R8(pVarIn
)*pow(10, deci
)-0.5)/pow(10, deci
);
5173 V_VT(pVarOut
) = V_VT(pVarIn
);
5176 if (V_R4(pVarIn
)>0) {
5177 V_R4(pVarOut
)=floor(V_R4(pVarIn
)*pow(10, deci
)+0.5)/pow(10, deci
);
5179 V_R4(pVarOut
)=ceil(V_R4(pVarIn
)*pow(10, deci
)-0.5)/pow(10, deci
);
5181 V_VT(pVarOut
) = V_VT(pVarIn
);
5184 if (V_DATE(pVarIn
)>0) {
5185 V_DATE(pVarOut
)=floor(V_DATE(pVarIn
)*pow(10, deci
)+0.5)/pow(10, deci
);
5187 V_DATE(pVarOut
)=ceil(V_DATE(pVarIn
)*pow(10, deci
)-0.5)/pow(10, deci
);
5189 V_VT(pVarOut
) = V_VT(pVarIn
);
5195 factor
=pow(10, 4-deci
);
5197 if (V_CY(pVarIn
).int64
>0) {
5198 V_CY(pVarOut
).int64
=floor(V_CY(pVarIn
).int64
/factor
)*factor
;
5200 V_CY(pVarOut
).int64
=ceil(V_CY(pVarIn
).int64
/factor
)*factor
;
5202 V_VT(pVarOut
) = V_VT(pVarIn
);
5205 /* cases we don't know yet */
5207 FIXME("unimplemented part, V_VT(pVarIn) == 0x%X, deci == %d\n",
5208 V_VT(pVarIn
) & VT_TYPEMASK
, deci
);
5209 hRet
= DISP_E_BADVARTYPE
;
5213 V_VT(pVarOut
) = VT_EMPTY
;
5214 VariantClear(&temp
);
5216 TRACE("returning 0x%08x %s\n", hRet
, debugstr_variant(pVarOut
));
5220 /**********************************************************************
5221 * VarIdiv [OLEAUT32.153]
5223 * Converts input variants to integers and divides them.
5226 * left [I] Left hand variant
5227 * right [I] Right hand variant
5228 * result [O] Destination for quotient
5231 * Success: S_OK. result contains the quotient.
5232 * Failure: An HRESULT error code indicating the error.
5235 * If either expression is null, null is returned, as per MSDN
5237 HRESULT WINAPI
VarIdiv(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
5239 HRESULT hres
= S_OK
;
5240 VARTYPE resvt
= VT_EMPTY
;
5241 VARTYPE leftvt
,rightvt
;
5242 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
5244 VARIANT tempLeft
, tempRight
;
5246 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
5250 VariantInit(&tempLeft
);
5251 VariantInit(&tempRight
);
5253 leftvt
= V_VT(left
)&VT_TYPEMASK
;
5254 rightvt
= V_VT(right
)&VT_TYPEMASK
;
5255 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
5256 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
5258 if (leftExtraFlags
!= rightExtraFlags
)
5260 hres
= DISP_E_BADVARTYPE
;
5263 ExtraFlags
= leftExtraFlags
;
5265 /* Native VarIdiv always returns an error when using extra
5266 * flags or if the variant combination is I8 and INT.
5268 if ((leftvt
== VT_I8
&& rightvt
== VT_INT
) ||
5269 (leftvt
== VT_INT
&& rightvt
== VT_I8
) ||
5270 (rightvt
== VT_EMPTY
&& leftvt
!= VT_NULL
) ||
5273 hres
= DISP_E_BADVARTYPE
;
5277 /* Determine variant type */
5278 else if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
)
5280 V_VT(result
) = VT_NULL
;
5284 else if (leftvt
== VT_I8
|| rightvt
== VT_I8
)
5286 else if (leftvt
== VT_I4
|| rightvt
== VT_I4
||
5287 leftvt
== VT_INT
|| rightvt
== VT_INT
||
5288 leftvt
== VT_UINT
|| rightvt
== VT_UINT
||
5289 leftvt
== VT_UI8
|| rightvt
== VT_UI8
||
5290 leftvt
== VT_UI4
|| rightvt
== VT_UI4
||
5291 leftvt
== VT_UI2
|| rightvt
== VT_UI2
||
5292 leftvt
== VT_I1
|| rightvt
== VT_I1
||
5293 leftvt
== VT_BSTR
|| rightvt
== VT_BSTR
||
5294 leftvt
== VT_DATE
|| rightvt
== VT_DATE
||
5295 leftvt
== VT_CY
|| rightvt
== VT_CY
||
5296 leftvt
== VT_DECIMAL
|| rightvt
== VT_DECIMAL
||
5297 leftvt
== VT_R8
|| rightvt
== VT_R8
||
5298 leftvt
== VT_R4
|| rightvt
== VT_R4
)
5300 else if (leftvt
== VT_I2
|| rightvt
== VT_I2
||
5301 leftvt
== VT_BOOL
|| rightvt
== VT_BOOL
||
5304 else if (leftvt
== VT_UI1
|| rightvt
== VT_UI1
)
5308 hres
= DISP_E_BADVARTYPE
;
5312 /* coerce to the result type */
5313 hres
= VariantChangeType(&lv
, left
, 0, resvt
);
5314 if (hres
!= S_OK
) goto end
;
5315 hres
= VariantChangeType(&rv
, right
, 0, resvt
);
5316 if (hres
!= S_OK
) goto end
;
5319 V_VT(result
) = resvt
;
5323 if (V_UI1(&rv
) == 0)
5325 hres
= DISP_E_DIVBYZERO
;
5326 V_VT(result
) = VT_EMPTY
;
5329 V_UI1(result
) = V_UI1(&lv
) / V_UI1(&rv
);
5334 hres
= DISP_E_DIVBYZERO
;
5335 V_VT(result
) = VT_EMPTY
;
5338 V_I2(result
) = V_I2(&lv
) / V_I2(&rv
);
5343 hres
= DISP_E_DIVBYZERO
;
5344 V_VT(result
) = VT_EMPTY
;
5347 V_I4(result
) = V_I4(&lv
) / V_I4(&rv
);
5352 hres
= DISP_E_DIVBYZERO
;
5353 V_VT(result
) = VT_EMPTY
;
5356 V_I8(result
) = V_I8(&lv
) / V_I8(&rv
);
5359 FIXME("Couldn't integer divide variant types %d,%d\n",
5366 VariantClear(&tempLeft
);
5367 VariantClear(&tempRight
);
5373 /**********************************************************************
5374 * VarMod [OLEAUT32.155]
5376 * Perform the modulus operation of the right hand variant on the left
5379 * left [I] Left hand variant
5380 * right [I] Right hand variant
5381 * result [O] Destination for converted value
5384 * Success: S_OK. result contains the remainder.
5385 * Failure: An HRESULT error code indicating the error.
5388 * If an error occurs the type of result will be modified but the value will not be.
5389 * Doesn't support arrays or any special flags yet.
5391 HRESULT WINAPI
VarMod(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
5394 HRESULT rc
= E_FAIL
;
5397 VARIANT tempLeft
, tempRight
;
5399 VariantInit(&tempLeft
);
5400 VariantInit(&tempRight
);
5404 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
5406 /* Handle VT_DISPATCH by storing and taking address of returned value */
5407 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
5409 rc
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
5410 if (FAILED(rc
)) goto end
;
5413 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
5415 rc
= VARIANT_FetchDispatchValue(right
, &tempRight
);
5416 if (FAILED(rc
)) goto end
;
5420 /* check for invalid inputs */
5422 switch (V_VT(left
) & VT_TYPEMASK
) {
5444 V_VT(result
) = VT_EMPTY
;
5445 rc
= DISP_E_TYPEMISMATCH
;
5448 rc
= DISP_E_TYPEMISMATCH
;
5451 V_VT(result
) = VT_EMPTY
;
5452 rc
= DISP_E_TYPEMISMATCH
;
5457 V_VT(result
) = VT_EMPTY
;
5458 rc
= DISP_E_BADVARTYPE
;
5463 switch (V_VT(right
) & VT_TYPEMASK
) {
5469 if((V_VT(left
) == VT_INT
) && (V_VT(right
) == VT_I8
))
5471 V_VT(result
) = VT_EMPTY
;
5472 rc
= DISP_E_TYPEMISMATCH
;
5476 if((V_VT(right
) == VT_INT
) && (V_VT(left
) == VT_I8
))
5478 V_VT(result
) = VT_EMPTY
;
5479 rc
= DISP_E_TYPEMISMATCH
;
5490 if(V_VT(left
) == VT_EMPTY
)
5492 V_VT(result
) = VT_I4
;
5499 if(V_VT(left
) == VT_ERROR
)
5501 V_VT(result
) = VT_EMPTY
;
5502 rc
= DISP_E_TYPEMISMATCH
;
5506 if(V_VT(left
) == VT_NULL
)
5508 V_VT(result
) = VT_NULL
;
5515 V_VT(result
) = VT_EMPTY
;
5516 rc
= DISP_E_BADVARTYPE
;
5519 if(V_VT(left
) == VT_VOID
)
5521 V_VT(result
) = VT_EMPTY
;
5522 rc
= DISP_E_BADVARTYPE
;
5523 } else if((V_VT(left
) == VT_NULL
) || (V_VT(left
) == VT_EMPTY
) || (V_VT(left
) == VT_ERROR
) ||
5526 V_VT(result
) = VT_NULL
;
5530 V_VT(result
) = VT_NULL
;
5531 rc
= DISP_E_BADVARTYPE
;
5536 V_VT(result
) = VT_EMPTY
;
5537 rc
= DISP_E_TYPEMISMATCH
;
5540 rc
= DISP_E_TYPEMISMATCH
;
5543 if((V_VT(left
) == 15) || ((V_VT(left
) >= 24) && (V_VT(left
) <= 35)) || !lOk
)
5545 V_VT(result
) = VT_EMPTY
;
5546 rc
= DISP_E_BADVARTYPE
;
5549 V_VT(result
) = VT_EMPTY
;
5550 rc
= DISP_E_TYPEMISMATCH
;
5554 V_VT(result
) = VT_EMPTY
;
5555 rc
= DISP_E_BADVARTYPE
;
5559 /* determine the result type */
5560 if((V_VT(left
) == VT_I8
) || (V_VT(right
) == VT_I8
)) resT
= VT_I8
;
5561 else if((V_VT(left
) == VT_UI1
) && (V_VT(right
) == VT_BOOL
)) resT
= VT_I2
;
5562 else if((V_VT(left
) == VT_UI1
) && (V_VT(right
) == VT_UI1
)) resT
= VT_UI1
;
5563 else if((V_VT(left
) == VT_UI1
) && (V_VT(right
) == VT_I2
)) resT
= VT_I2
;
5564 else if((V_VT(left
) == VT_I2
) && (V_VT(right
) == VT_BOOL
)) resT
= VT_I2
;
5565 else if((V_VT(left
) == VT_I2
) && (V_VT(right
) == VT_UI1
)) resT
= VT_I2
;
5566 else if((V_VT(left
) == VT_I2
) && (V_VT(right
) == VT_I2
)) resT
= VT_I2
;
5567 else if((V_VT(left
) == VT_BOOL
) && (V_VT(right
) == VT_BOOL
)) resT
= VT_I2
;
5568 else if((V_VT(left
) == VT_BOOL
) && (V_VT(right
) == VT_UI1
)) resT
= VT_I2
;
5569 else if((V_VT(left
) == VT_BOOL
) && (V_VT(right
) == VT_I2
)) resT
= VT_I2
;
5570 else resT
= VT_I4
; /* most outputs are I4 */
5572 /* convert to I8 for the modulo */
5573 rc
= VariantChangeType(&lv
, left
, 0, VT_I8
);
5576 FIXME("Could not convert left type %d to %d? rc == 0x%X\n", V_VT(left
), VT_I8
, rc
);
5580 rc
= VariantChangeType(&rv
, right
, 0, VT_I8
);
5583 FIXME("Could not convert right type %d to %d? rc == 0x%X\n", V_VT(right
), VT_I8
, rc
);
5587 /* if right is zero set VT_EMPTY and return divide by zero */
5590 V_VT(result
) = VT_EMPTY
;
5591 rc
= DISP_E_DIVBYZERO
;
5595 /* perform the modulo operation */
5596 V_VT(result
) = VT_I8
;
5597 V_I8(result
) = V_I8(&lv
) % V_I8(&rv
);
5599 TRACE("V_I8(left) == %s, V_I8(right) == %s, V_I8(result) == %s\n",
5600 wine_dbgstr_longlong(V_I8(&lv
)), wine_dbgstr_longlong(V_I8(&rv
)),
5601 wine_dbgstr_longlong(V_I8(result
)));
5603 /* convert left and right to the destination type */
5604 rc
= VariantChangeType(result
, result
, 0, resT
);
5607 FIXME("Could not convert 0x%x to %d?\n", V_VT(result
), resT
);
5608 /* fall to end of function */
5614 VariantClear(&tempLeft
);
5615 VariantClear(&tempRight
);
5619 /**********************************************************************
5620 * VarPow [OLEAUT32.158]
5622 * Computes the power of one variant to another variant.
5625 * left [I] First variant
5626 * right [I] Second variant
5627 * result [O] Result variant
5631 * Failure: An HRESULT error code indicating the error.
5633 HRESULT WINAPI
VarPow(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
5637 VARTYPE resvt
= VT_EMPTY
;
5638 VARTYPE leftvt
,rightvt
;
5639 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
5640 VARIANT tempLeft
, tempRight
;
5642 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
5646 VariantInit(&tempLeft
);
5647 VariantInit(&tempRight
);
5649 /* Handle VT_DISPATCH by storing and taking address of returned value */
5650 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
5652 hr
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
5653 if (FAILED(hr
)) goto end
;
5656 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
5658 hr
= VARIANT_FetchDispatchValue(right
, &tempRight
);
5659 if (FAILED(hr
)) goto end
;
5663 leftvt
= V_VT(left
)&VT_TYPEMASK
;
5664 rightvt
= V_VT(right
)&VT_TYPEMASK
;
5665 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
5666 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
5668 if (leftExtraFlags
!= rightExtraFlags
)
5670 hr
= DISP_E_BADVARTYPE
;
5673 ExtraFlags
= leftExtraFlags
;
5675 /* Native VarPow always returns an error when using extra flags */
5676 if (ExtraFlags
!= 0)
5678 hr
= DISP_E_BADVARTYPE
;
5682 /* Determine return type */
5683 else if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
) {
5684 V_VT(result
) = VT_NULL
;
5688 else if ((leftvt
== VT_EMPTY
|| leftvt
== VT_I2
||
5689 leftvt
== VT_I4
|| leftvt
== VT_R4
||
5690 leftvt
== VT_R8
|| leftvt
== VT_CY
||
5691 leftvt
== VT_DATE
|| leftvt
== VT_BSTR
||
5692 leftvt
== VT_BOOL
|| leftvt
== VT_DECIMAL
||
5693 (leftvt
>= VT_I1
&& leftvt
<= VT_UINT
)) &&
5694 (rightvt
== VT_EMPTY
|| rightvt
== VT_I2
||
5695 rightvt
== VT_I4
|| rightvt
== VT_R4
||
5696 rightvt
== VT_R8
|| rightvt
== VT_CY
||
5697 rightvt
== VT_DATE
|| rightvt
== VT_BSTR
||
5698 rightvt
== VT_BOOL
|| rightvt
== VT_DECIMAL
||
5699 (rightvt
>= VT_I1
&& rightvt
<= VT_UINT
)))
5703 hr
= DISP_E_BADVARTYPE
;
5707 hr
= VariantChangeType(&dl
,left
,0,resvt
);
5709 ERR("Could not change passed left argument to VT_R8, handle it differently.\n");
5714 hr
= VariantChangeType(&dr
,right
,0,resvt
);
5716 ERR("Could not change passed right argument to VT_R8, handle it differently.\n");
5721 V_VT(result
) = VT_R8
;
5722 V_R8(result
) = pow(V_R8(&dl
),V_R8(&dr
));
5727 VariantClear(&tempLeft
);
5728 VariantClear(&tempRight
);
5733 /**********************************************************************
5734 * VarImp [OLEAUT32.154]
5736 * Bitwise implication of two variants.
5739 * left [I] First variant
5740 * right [I] Second variant
5741 * result [O] Result variant
5745 * Failure: An HRESULT error code indicating the error.
5747 HRESULT WINAPI
VarImp(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
5749 HRESULT hres
= S_OK
;
5750 VARTYPE resvt
= VT_EMPTY
;
5751 VARTYPE leftvt
,rightvt
;
5752 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
5755 VARIANT tempLeft
, tempRight
;
5759 VariantInit(&tempLeft
);
5760 VariantInit(&tempRight
);
5762 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
5764 /* Handle VT_DISPATCH by storing and taking address of returned value */
5765 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
5767 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
5768 if (FAILED(hres
)) goto VarImp_Exit
;
5771 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
5773 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
5774 if (FAILED(hres
)) goto VarImp_Exit
;
5778 leftvt
= V_VT(left
)&VT_TYPEMASK
;
5779 rightvt
= V_VT(right
)&VT_TYPEMASK
;
5780 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
5781 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
5783 if (leftExtraFlags
!= rightExtraFlags
)
5785 hres
= DISP_E_BADVARTYPE
;
5788 ExtraFlags
= leftExtraFlags
;
5790 /* Native VarImp always returns an error when using extra
5791 * flags or if the variants are I8 and INT.
5793 if ((leftvt
== VT_I8
&& rightvt
== VT_INT
) ||
5796 hres
= DISP_E_BADVARTYPE
;
5800 /* Determine result type */
5801 else if ((leftvt
== VT_NULL
&& rightvt
== VT_NULL
) ||
5802 (leftvt
== VT_NULL
&& rightvt
== VT_EMPTY
))
5804 V_VT(result
) = VT_NULL
;
5808 else if (leftvt
== VT_I8
|| rightvt
== VT_I8
)
5810 else if (leftvt
== VT_I4
|| rightvt
== VT_I4
||
5811 leftvt
== VT_INT
|| rightvt
== VT_INT
||
5812 leftvt
== VT_UINT
|| rightvt
== VT_UINT
||
5813 leftvt
== VT_UI4
|| rightvt
== VT_UI4
||
5814 leftvt
== VT_UI8
|| rightvt
== VT_UI8
||
5815 leftvt
== VT_UI2
|| rightvt
== VT_UI2
||
5816 leftvt
== VT_DECIMAL
|| rightvt
== VT_DECIMAL
||
5817 leftvt
== VT_DATE
|| rightvt
== VT_DATE
||
5818 leftvt
== VT_CY
|| rightvt
== VT_CY
||
5819 leftvt
== VT_R8
|| rightvt
== VT_R8
||
5820 leftvt
== VT_R4
|| rightvt
== VT_R4
||
5821 leftvt
== VT_I1
|| rightvt
== VT_I1
)
5823 else if ((leftvt
== VT_UI1
&& rightvt
== VT_UI1
) ||
5824 (leftvt
== VT_UI1
&& rightvt
== VT_NULL
) ||
5825 (leftvt
== VT_NULL
&& rightvt
== VT_UI1
))
5827 else if (leftvt
== VT_EMPTY
|| rightvt
== VT_EMPTY
||
5828 leftvt
== VT_I2
|| rightvt
== VT_I2
||
5829 leftvt
== VT_UI1
|| rightvt
== VT_UI1
)
5831 else if (leftvt
== VT_BOOL
|| rightvt
== VT_BOOL
||
5832 leftvt
== VT_BSTR
|| rightvt
== VT_BSTR
)
5835 /* VT_NULL requires special handling for when the opposite
5836 * variant is equal to something other than -1.
5837 * (NULL Imp 0 = NULL, NULL Imp n = n)
5839 if (leftvt
== VT_NULL
)
5844 case VT_I1
: if (!V_I1(right
)) resvt
= VT_NULL
; break;
5845 case VT_UI1
: if (!V_UI1(right
)) resvt
= VT_NULL
; break;
5846 case VT_I2
: if (!V_I2(right
)) resvt
= VT_NULL
; break;
5847 case VT_UI2
: if (!V_UI2(right
)) resvt
= VT_NULL
; break;
5848 case VT_I4
: if (!V_I4(right
)) resvt
= VT_NULL
; break;
5849 case VT_UI4
: if (!V_UI4(right
)) resvt
= VT_NULL
; break;
5850 case VT_I8
: if (!V_I8(right
)) resvt
= VT_NULL
; break;
5851 case VT_UI8
: if (!V_UI8(right
)) resvt
= VT_NULL
; break;
5852 case VT_INT
: if (!V_INT(right
)) resvt
= VT_NULL
; break;
5853 case VT_UINT
: if (!V_UINT(right
)) resvt
= VT_NULL
; break;
5854 case VT_BOOL
: if (!V_BOOL(right
)) resvt
= VT_NULL
; break;
5855 case VT_R4
: if (!V_R4(right
)) resvt
= VT_NULL
; break;
5856 case VT_R8
: if (!V_R8(right
)) resvt
= VT_NULL
; break;
5857 case VT_DATE
: if (!V_DATE(right
)) resvt
= VT_NULL
; break;
5858 case VT_CY
: if (!V_CY(right
).int64
) resvt
= VT_NULL
; break;
5860 if (!(DEC_HI32(&V_DECIMAL(right
)) || DEC_LO64(&V_DECIMAL(right
))))
5864 hres
= VarBoolFromStr(V_BSTR(right
),LOCALE_USER_DEFAULT
, VAR_LOCALBOOL
, &b
);
5865 if (FAILED(hres
)) goto VarImp_Exit
;
5867 V_VT(result
) = VT_NULL
;
5870 V_VT(result
) = VT_BOOL
;
5875 if (resvt
== VT_NULL
)
5877 V_VT(result
) = resvt
;
5882 hres
= VariantChangeType(result
,right
,0,resvt
);
5887 /* Special handling is required when NULL is the right variant.
5888 * (-1 Imp NULL = NULL, n Imp NULL = n Imp 0)
5890 else if (rightvt
== VT_NULL
)
5895 case VT_I1
: if (V_I1(left
) == -1) resvt
= VT_NULL
; break;
5896 case VT_UI1
: if (V_UI1(left
) == 0xff) resvt
= VT_NULL
; break;
5897 case VT_I2
: if (V_I2(left
) == -1) resvt
= VT_NULL
; break;
5898 case VT_UI2
: if (V_UI2(left
) == 0xffff) resvt
= VT_NULL
; break;
5899 case VT_INT
: if (V_INT(left
) == -1) resvt
= VT_NULL
; break;
5900 case VT_UINT
: if (V_UINT(left
) == ~0u) resvt
= VT_NULL
; break;
5901 case VT_I4
: if (V_I4(left
) == -1) resvt
= VT_NULL
; break;
5902 case VT_UI4
: if (V_UI4(left
) == ~0u) resvt
= VT_NULL
; break;
5903 case VT_I8
: if (V_I8(left
) == -1) resvt
= VT_NULL
; break;
5904 case VT_UI8
: if (V_UI8(left
) == ~(ULONGLONG
)0) resvt
= VT_NULL
; break;
5905 case VT_BOOL
: if (V_BOOL(left
) == VARIANT_TRUE
) resvt
= VT_NULL
; break;
5906 case VT_R4
: if (V_R4(left
) == -1.0) resvt
= VT_NULL
; break;
5907 case VT_R8
: if (V_R8(left
) == -1.0) resvt
= VT_NULL
; break;
5908 case VT_CY
: if (V_CY(left
).int64
== -1) resvt
= VT_NULL
; break;
5910 if (DEC_HI32(&V_DECIMAL(left
)) == 0xffffffff)
5914 hres
= VarBoolFromStr(V_BSTR(left
),LOCALE_USER_DEFAULT
, VAR_LOCALBOOL
, &b
);
5915 if (FAILED(hres
)) goto VarImp_Exit
;
5916 else if (b
== VARIANT_TRUE
)
5919 if (resvt
== VT_NULL
)
5921 V_VT(result
) = resvt
;
5926 hres
= VariantCopy(&lv
, left
);
5927 if (FAILED(hres
)) goto VarImp_Exit
;
5929 if (rightvt
== VT_NULL
)
5931 memset( &rv
, 0, sizeof(rv
) );
5936 hres
= VariantCopy(&rv
, right
);
5937 if (FAILED(hres
)) goto VarImp_Exit
;
5940 if (V_VT(&lv
) == VT_BSTR
&&
5941 FAILED(VarR8FromStr(V_BSTR(&lv
),LOCALE_USER_DEFAULT
, 0, &d
)))
5942 hres
= VariantChangeType(&lv
,&lv
,VARIANT_LOCALBOOL
, VT_BOOL
);
5943 if (SUCCEEDED(hres
) && V_VT(&lv
) != resvt
)
5944 hres
= VariantChangeType(&lv
,&lv
,0,resvt
);
5945 if (FAILED(hres
)) goto VarImp_Exit
;
5947 if (V_VT(&rv
) == VT_BSTR
&&
5948 FAILED(VarR8FromStr(V_BSTR(&rv
),LOCALE_USER_DEFAULT
, 0, &d
)))
5949 hres
= VariantChangeType(&rv
, &rv
,VARIANT_LOCALBOOL
, VT_BOOL
);
5950 if (SUCCEEDED(hres
) && V_VT(&rv
) != resvt
)
5951 hres
= VariantChangeType(&rv
, &rv
, 0, resvt
);
5952 if (FAILED(hres
)) goto VarImp_Exit
;
5955 V_VT(result
) = resvt
;
5959 V_I8(result
) = (~V_I8(&lv
)) | V_I8(&rv
);
5962 V_I4(result
) = (~V_I4(&lv
)) | V_I4(&rv
);
5965 V_I2(result
) = (~V_I2(&lv
)) | V_I2(&rv
);
5968 V_UI1(result
) = (~V_UI1(&lv
)) | V_UI1(&rv
);
5971 V_BOOL(result
) = (~V_BOOL(&lv
)) | V_BOOL(&rv
);
5974 FIXME("Couldn't perform bitwise implication on variant types %d,%d\n",
5982 VariantClear(&tempLeft
);
5983 VariantClear(&tempRight
);