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
);
48 static CRITICAL_SECTION cache_cs
;
49 static CRITICAL_SECTION_DEBUG critsect_debug
=
52 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
53 0, 0, { (DWORD_PTR
)(__FILE__
": cache_cs") }
55 static CRITICAL_SECTION cache_cs
= { &critsect_debug
, -1, 0, 0, 0, 0 };
57 /* Convert a variant from one type to another */
58 static inline HRESULT
VARIANT_Coerce(VARIANTARG
* pd
, LCID lcid
, USHORT wFlags
,
59 VARIANTARG
* ps
, VARTYPE vt
)
61 HRESULT res
= DISP_E_TYPEMISMATCH
;
62 VARTYPE vtFrom
= V_TYPE(ps
);
65 TRACE("(%s,0x%08x,0x%04x,%s,%s)\n", debugstr_variant(pd
), lcid
, wFlags
,
66 debugstr_variant(ps
), debugstr_vt(vt
));
68 if (vt
== VT_BSTR
|| vtFrom
== VT_BSTR
)
70 /* All flags passed to low level function are only used for
71 * changing to or from strings. Map these here.
73 if (wFlags
& VARIANT_LOCALBOOL
)
74 dwFlags
|= VAR_LOCALBOOL
;
75 if (wFlags
& VARIANT_CALENDAR_HIJRI
)
76 dwFlags
|= VAR_CALENDAR_HIJRI
;
77 if (wFlags
& VARIANT_CALENDAR_THAI
)
78 dwFlags
|= VAR_CALENDAR_THAI
;
79 if (wFlags
& VARIANT_CALENDAR_GREGORIAN
)
80 dwFlags
|= VAR_CALENDAR_GREGORIAN
;
81 if (wFlags
& VARIANT_NOUSEROVERRIDE
)
82 dwFlags
|= LOCALE_NOUSEROVERRIDE
;
83 if (wFlags
& VARIANT_USE_NLS
)
84 dwFlags
|= LOCALE_USE_NLS
;
87 /* Map int/uint to i4/ui4 */
90 else if (vt
== VT_UINT
)
95 else if (vtFrom
== VT_UINT
)
99 return VariantCopy(pd
, ps
);
101 if (wFlags
& VARIANT_NOVALUEPROP
&& vtFrom
== VT_DISPATCH
&& vt
!= VT_UNKNOWN
)
103 /* VARIANT_NOVALUEPROP prevents IDispatch objects from being coerced by
104 * accessing the default object property.
106 return DISP_E_TYPEMISMATCH
;
112 if (vtFrom
== VT_NULL
)
113 return DISP_E_TYPEMISMATCH
;
114 /* ... Fall through */
116 if (vtFrom
<= VT_UINT
&& vtFrom
!= (VARTYPE
)15 && vtFrom
!= VT_ERROR
)
118 res
= VariantClear( pd
);
119 if (vt
== VT_NULL
&& SUCCEEDED(res
))
127 case VT_EMPTY
: V_I1(pd
) = 0; return S_OK
;
128 case VT_I2
: return VarI1FromI2(V_I2(ps
), &V_I1(pd
));
129 case VT_I4
: return VarI1FromI4(V_I4(ps
), &V_I1(pd
));
130 case VT_UI1
: V_I1(pd
) = V_UI1(ps
); return S_OK
;
131 case VT_UI2
: return VarI1FromUI2(V_UI2(ps
), &V_I1(pd
));
132 case VT_UI4
: return VarI1FromUI4(V_UI4(ps
), &V_I1(pd
));
133 case VT_I8
: return VarI1FromI8(V_I8(ps
), &V_I1(pd
));
134 case VT_UI8
: return VarI1FromUI8(V_UI8(ps
), &V_I1(pd
));
135 case VT_R4
: return VarI1FromR4(V_R4(ps
), &V_I1(pd
));
136 case VT_R8
: return VarI1FromR8(V_R8(ps
), &V_I1(pd
));
137 case VT_DATE
: return VarI1FromDate(V_DATE(ps
), &V_I1(pd
));
138 case VT_BOOL
: return VarI1FromBool(V_BOOL(ps
), &V_I1(pd
));
139 case VT_CY
: return VarI1FromCy(V_CY(ps
), &V_I1(pd
));
140 case VT_DECIMAL
: return VarI1FromDec(&V_DECIMAL(ps
), &V_I1(pd
) );
141 case VT_DISPATCH
: return VarI1FromDisp(V_DISPATCH(ps
), lcid
, &V_I1(pd
) );
142 case VT_BSTR
: return VarI1FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I1(pd
) );
149 case VT_EMPTY
: V_I2(pd
) = 0; return S_OK
;
150 case VT_I1
: return VarI2FromI1(V_I1(ps
), &V_I2(pd
));
151 case VT_I4
: return VarI2FromI4(V_I4(ps
), &V_I2(pd
));
152 case VT_UI1
: return VarI2FromUI1(V_UI1(ps
), &V_I2(pd
));
153 case VT_UI2
: V_I2(pd
) = V_UI2(ps
); return S_OK
;
154 case VT_UI4
: return VarI2FromUI4(V_UI4(ps
), &V_I2(pd
));
155 case VT_I8
: return VarI2FromI8(V_I8(ps
), &V_I2(pd
));
156 case VT_UI8
: return VarI2FromUI8(V_UI8(ps
), &V_I2(pd
));
157 case VT_R4
: return VarI2FromR4(V_R4(ps
), &V_I2(pd
));
158 case VT_R8
: return VarI2FromR8(V_R8(ps
), &V_I2(pd
));
159 case VT_DATE
: return VarI2FromDate(V_DATE(ps
), &V_I2(pd
));
160 case VT_BOOL
: return VarI2FromBool(V_BOOL(ps
), &V_I2(pd
));
161 case VT_CY
: return VarI2FromCy(V_CY(ps
), &V_I2(pd
));
162 case VT_DECIMAL
: return VarI2FromDec(&V_DECIMAL(ps
), &V_I2(pd
));
163 case VT_DISPATCH
: return VarI2FromDisp(V_DISPATCH(ps
), lcid
, &V_I2(pd
));
164 case VT_BSTR
: return VarI2FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I2(pd
));
171 case VT_EMPTY
: V_I4(pd
) = 0; return S_OK
;
172 case VT_I1
: return VarI4FromI1(V_I1(ps
), &V_I4(pd
));
173 case VT_I2
: return VarI4FromI2(V_I2(ps
), &V_I4(pd
));
174 case VT_UI1
: return VarI4FromUI1(V_UI1(ps
), &V_I4(pd
));
175 case VT_UI2
: return VarI4FromUI2(V_UI2(ps
), &V_I4(pd
));
176 case VT_UI4
: V_I4(pd
) = V_UI4(ps
); return S_OK
;
177 case VT_I8
: return VarI4FromI8(V_I8(ps
), &V_I4(pd
));
178 case VT_UI8
: return VarI4FromUI8(V_UI8(ps
), &V_I4(pd
));
179 case VT_R4
: return VarI4FromR4(V_R4(ps
), &V_I4(pd
));
180 case VT_R8
: return VarI4FromR8(V_R8(ps
), &V_I4(pd
));
181 case VT_DATE
: return VarI4FromDate(V_DATE(ps
), &V_I4(pd
));
182 case VT_BOOL
: return VarI4FromBool(V_BOOL(ps
), &V_I4(pd
));
183 case VT_CY
: return VarI4FromCy(V_CY(ps
), &V_I4(pd
));
184 case VT_DECIMAL
: return VarI4FromDec(&V_DECIMAL(ps
), &V_I4(pd
));
185 case VT_DISPATCH
: return VarI4FromDisp(V_DISPATCH(ps
), lcid
, &V_I4(pd
));
186 case VT_BSTR
: return VarI4FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I4(pd
));
193 case VT_EMPTY
: V_UI1(pd
) = 0; return S_OK
;
194 case VT_I1
: V_UI1(pd
) = V_I1(ps
); return S_OK
;
195 case VT_I2
: return VarUI1FromI2(V_I2(ps
), &V_UI1(pd
));
196 case VT_I4
: return VarUI1FromI4(V_I4(ps
), &V_UI1(pd
));
197 case VT_UI2
: return VarUI1FromUI2(V_UI2(ps
), &V_UI1(pd
));
198 case VT_UI4
: return VarUI1FromUI4(V_UI4(ps
), &V_UI1(pd
));
199 case VT_I8
: return VarUI1FromI8(V_I8(ps
), &V_UI1(pd
));
200 case VT_UI8
: return VarUI1FromUI8(V_UI8(ps
), &V_UI1(pd
));
201 case VT_R4
: return VarUI1FromR4(V_R4(ps
), &V_UI1(pd
));
202 case VT_R8
: return VarUI1FromR8(V_R8(ps
), &V_UI1(pd
));
203 case VT_DATE
: return VarUI1FromDate(V_DATE(ps
), &V_UI1(pd
));
204 case VT_BOOL
: return VarUI1FromBool(V_BOOL(ps
), &V_UI1(pd
));
205 case VT_CY
: return VarUI1FromCy(V_CY(ps
), &V_UI1(pd
));
206 case VT_DECIMAL
: return VarUI1FromDec(&V_DECIMAL(ps
), &V_UI1(pd
));
207 case VT_DISPATCH
: return VarUI1FromDisp(V_DISPATCH(ps
), lcid
, &V_UI1(pd
));
208 case VT_BSTR
: return VarUI1FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI1(pd
));
215 case VT_EMPTY
: V_UI2(pd
) = 0; return S_OK
;
216 case VT_I1
: return VarUI2FromI1(V_I1(ps
), &V_UI2(pd
));
217 case VT_I2
: V_UI2(pd
) = V_I2(ps
); return S_OK
;
218 case VT_I4
: return VarUI2FromI4(V_I4(ps
), &V_UI2(pd
));
219 case VT_UI1
: return VarUI2FromUI1(V_UI1(ps
), &V_UI2(pd
));
220 case VT_UI4
: return VarUI2FromUI4(V_UI4(ps
), &V_UI2(pd
));
221 case VT_I8
: return VarUI4FromI8(V_I8(ps
), &V_UI4(pd
));
222 case VT_UI8
: return VarUI4FromUI8(V_UI8(ps
), &V_UI4(pd
));
223 case VT_R4
: return VarUI2FromR4(V_R4(ps
), &V_UI2(pd
));
224 case VT_R8
: return VarUI2FromR8(V_R8(ps
), &V_UI2(pd
));
225 case VT_DATE
: return VarUI2FromDate(V_DATE(ps
), &V_UI2(pd
));
226 case VT_BOOL
: return VarUI2FromBool(V_BOOL(ps
), &V_UI2(pd
));
227 case VT_CY
: return VarUI2FromCy(V_CY(ps
), &V_UI2(pd
));
228 case VT_DECIMAL
: return VarUI2FromDec(&V_DECIMAL(ps
), &V_UI2(pd
));
229 case VT_DISPATCH
: return VarUI2FromDisp(V_DISPATCH(ps
), lcid
, &V_UI2(pd
));
230 case VT_BSTR
: return VarUI2FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI2(pd
));
237 case VT_EMPTY
: V_UI4(pd
) = 0; return S_OK
;
238 case VT_I1
: return VarUI4FromI1(V_I1(ps
), &V_UI4(pd
));
239 case VT_I2
: return VarUI4FromI2(V_I2(ps
), &V_UI4(pd
));
240 case VT_I4
: V_UI4(pd
) = V_I4(ps
); return S_OK
;
241 case VT_UI1
: return VarUI4FromUI1(V_UI1(ps
), &V_UI4(pd
));
242 case VT_UI2
: return VarUI4FromUI2(V_UI2(ps
), &V_UI4(pd
));
243 case VT_I8
: return VarUI4FromI8(V_I8(ps
), &V_UI4(pd
));
244 case VT_UI8
: return VarUI4FromUI8(V_UI8(ps
), &V_UI4(pd
));
245 case VT_R4
: return VarUI4FromR4(V_R4(ps
), &V_UI4(pd
));
246 case VT_R8
: return VarUI4FromR8(V_R8(ps
), &V_UI4(pd
));
247 case VT_DATE
: return VarUI4FromDate(V_DATE(ps
), &V_UI4(pd
));
248 case VT_BOOL
: return VarUI4FromBool(V_BOOL(ps
), &V_UI4(pd
));
249 case VT_CY
: return VarUI4FromCy(V_CY(ps
), &V_UI4(pd
));
250 case VT_DECIMAL
: return VarUI4FromDec(&V_DECIMAL(ps
), &V_UI4(pd
));
251 case VT_DISPATCH
: return VarUI4FromDisp(V_DISPATCH(ps
), lcid
, &V_UI4(pd
));
252 case VT_BSTR
: return VarUI4FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI4(pd
));
259 case VT_EMPTY
: V_UI8(pd
) = 0; return S_OK
;
260 case VT_I4
: if (V_I4(ps
) < 0) return DISP_E_OVERFLOW
; V_UI8(pd
) = V_I4(ps
); return S_OK
;
261 case VT_I1
: return VarUI8FromI1(V_I1(ps
), &V_UI8(pd
));
262 case VT_I2
: return VarUI8FromI2(V_I2(ps
), &V_UI8(pd
));
263 case VT_UI1
: return VarUI8FromUI1(V_UI1(ps
), &V_UI8(pd
));
264 case VT_UI2
: return VarUI8FromUI2(V_UI2(ps
), &V_UI8(pd
));
265 case VT_UI4
: return VarUI8FromUI4(V_UI4(ps
), &V_UI8(pd
));
266 case VT_I8
: V_UI8(pd
) = V_I8(ps
); return S_OK
;
267 case VT_R4
: return VarUI8FromR4(V_R4(ps
), &V_UI8(pd
));
268 case VT_R8
: return VarUI8FromR8(V_R8(ps
), &V_UI8(pd
));
269 case VT_DATE
: return VarUI8FromDate(V_DATE(ps
), &V_UI8(pd
));
270 case VT_BOOL
: return VarUI8FromBool(V_BOOL(ps
), &V_UI8(pd
));
271 case VT_CY
: return VarUI8FromCy(V_CY(ps
), &V_UI8(pd
));
272 case VT_DECIMAL
: return VarUI8FromDec(&V_DECIMAL(ps
), &V_UI8(pd
));
273 case VT_DISPATCH
: return VarUI8FromDisp(V_DISPATCH(ps
), lcid
, &V_UI8(pd
));
274 case VT_BSTR
: return VarUI8FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI8(pd
));
281 case VT_EMPTY
: V_I8(pd
) = 0; return S_OK
;
282 case VT_I4
: V_I8(pd
) = V_I4(ps
); return S_OK
;
283 case VT_I1
: return VarI8FromI1(V_I1(ps
), &V_I8(pd
));
284 case VT_I2
: return VarI8FromI2(V_I2(ps
), &V_I8(pd
));
285 case VT_UI1
: return VarI8FromUI1(V_UI1(ps
), &V_I8(pd
));
286 case VT_UI2
: return VarI8FromUI2(V_UI2(ps
), &V_I8(pd
));
287 case VT_UI4
: return VarI8FromUI4(V_UI4(ps
), &V_I8(pd
));
288 case VT_UI8
: V_I8(pd
) = V_UI8(ps
); return S_OK
;
289 case VT_R4
: return VarI8FromR4(V_R4(ps
), &V_I8(pd
));
290 case VT_R8
: return VarI8FromR8(V_R8(ps
), &V_I8(pd
));
291 case VT_DATE
: return VarI8FromDate(V_DATE(ps
), &V_I8(pd
));
292 case VT_BOOL
: return VarI8FromBool(V_BOOL(ps
), &V_I8(pd
));
293 case VT_CY
: return VarI8FromCy(V_CY(ps
), &V_I8(pd
));
294 case VT_DECIMAL
: return VarI8FromDec(&V_DECIMAL(ps
), &V_I8(pd
));
295 case VT_DISPATCH
: return VarI8FromDisp(V_DISPATCH(ps
), lcid
, &V_I8(pd
));
296 case VT_BSTR
: return VarI8FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I8(pd
));
303 case VT_EMPTY
: V_R4(pd
) = 0.0f
; return S_OK
;
304 case VT_I1
: return VarR4FromI1(V_I1(ps
), &V_R4(pd
));
305 case VT_I2
: return VarR4FromI2(V_I2(ps
), &V_R4(pd
));
306 case VT_I4
: return VarR4FromI4(V_I4(ps
), &V_R4(pd
));
307 case VT_UI1
: return VarR4FromUI1(V_UI1(ps
), &V_R4(pd
));
308 case VT_UI2
: return VarR4FromUI2(V_UI2(ps
), &V_R4(pd
));
309 case VT_UI4
: return VarR4FromUI4(V_UI4(ps
), &V_R4(pd
));
310 case VT_I8
: return VarR4FromI8(V_I8(ps
), &V_R4(pd
));
311 case VT_UI8
: return VarR4FromUI8(V_UI8(ps
), &V_R4(pd
));
312 case VT_R8
: return VarR4FromR8(V_R8(ps
), &V_R4(pd
));
313 case VT_DATE
: return VarR4FromDate(V_DATE(ps
), &V_R4(pd
));
314 case VT_BOOL
: return VarR4FromBool(V_BOOL(ps
), &V_R4(pd
));
315 case VT_CY
: return VarR4FromCy(V_CY(ps
), &V_R4(pd
));
316 case VT_DECIMAL
: return VarR4FromDec(&V_DECIMAL(ps
), &V_R4(pd
));
317 case VT_DISPATCH
: return VarR4FromDisp(V_DISPATCH(ps
), lcid
, &V_R4(pd
));
318 case VT_BSTR
: return VarR4FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_R4(pd
));
325 case VT_EMPTY
: V_R8(pd
) = 0.0; return S_OK
;
326 case VT_I1
: return VarR8FromI1(V_I1(ps
), &V_R8(pd
));
327 case VT_I2
: return VarR8FromI2(V_I2(ps
), &V_R8(pd
));
328 case VT_I4
: return VarR8FromI4(V_I4(ps
), &V_R8(pd
));
329 case VT_UI1
: return VarR8FromUI1(V_UI1(ps
), &V_R8(pd
));
330 case VT_UI2
: return VarR8FromUI2(V_UI2(ps
), &V_R8(pd
));
331 case VT_UI4
: return VarR8FromUI4(V_UI4(ps
), &V_R8(pd
));
332 case VT_I8
: return VarR8FromI8(V_I8(ps
), &V_R8(pd
));
333 case VT_UI8
: return VarR8FromUI8(V_UI8(ps
), &V_R8(pd
));
334 case VT_R4
: return VarR8FromR4(V_R4(ps
), &V_R8(pd
));
335 case VT_DATE
: return VarR8FromDate(V_DATE(ps
), &V_R8(pd
));
336 case VT_BOOL
: return VarR8FromBool(V_BOOL(ps
), &V_R8(pd
));
337 case VT_CY
: return VarR8FromCy(V_CY(ps
), &V_R8(pd
));
338 case VT_DECIMAL
: return VarR8FromDec(&V_DECIMAL(ps
), &V_R8(pd
));
339 case VT_DISPATCH
: return VarR8FromDisp(V_DISPATCH(ps
), lcid
, &V_R8(pd
));
340 case VT_BSTR
: return VarR8FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_R8(pd
));
347 case VT_EMPTY
: V_DATE(pd
) = 0.0; return S_OK
;
348 case VT_I1
: return VarDateFromI1(V_I1(ps
), &V_DATE(pd
));
349 case VT_I2
: return VarDateFromI2(V_I2(ps
), &V_DATE(pd
));
350 case VT_I4
: return VarDateFromI4(V_I4(ps
), &V_DATE(pd
));
351 case VT_UI1
: return VarDateFromUI1(V_UI1(ps
), &V_DATE(pd
));
352 case VT_UI2
: return VarDateFromUI2(V_UI2(ps
), &V_DATE(pd
));
353 case VT_UI4
: return VarDateFromUI4(V_UI4(ps
), &V_DATE(pd
));
354 case VT_I8
: return VarDateFromI8(V_I8(ps
), &V_DATE(pd
));
355 case VT_UI8
: return VarDateFromUI8(V_UI8(ps
), &V_DATE(pd
));
356 case VT_R4
: return VarDateFromR4(V_R4(ps
), &V_DATE(pd
));
357 case VT_R8
: return VarDateFromR8(V_R8(ps
), &V_DATE(pd
));
358 case VT_BOOL
: return VarDateFromBool(V_BOOL(ps
), &V_DATE(pd
));
359 case VT_CY
: return VarDateFromCy(V_CY(ps
), &V_DATE(pd
));
360 case VT_DECIMAL
: return VarDateFromDec(&V_DECIMAL(ps
), &V_DATE(pd
));
361 case VT_DISPATCH
: return VarDateFromDisp(V_DISPATCH(ps
), lcid
, &V_DATE(pd
));
362 case VT_BSTR
: return VarDateFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_DATE(pd
));
369 case VT_EMPTY
: V_BOOL(pd
) = 0; return S_OK
;
370 case VT_I1
: return VarBoolFromI1(V_I1(ps
), &V_BOOL(pd
));
371 case VT_I2
: return VarBoolFromI2(V_I2(ps
), &V_BOOL(pd
));
372 case VT_I4
: return VarBoolFromI4(V_I4(ps
), &V_BOOL(pd
));
373 case VT_UI1
: return VarBoolFromUI1(V_UI1(ps
), &V_BOOL(pd
));
374 case VT_UI2
: return VarBoolFromUI2(V_UI2(ps
), &V_BOOL(pd
));
375 case VT_UI4
: return VarBoolFromUI4(V_UI4(ps
), &V_BOOL(pd
));
376 case VT_I8
: return VarBoolFromI8(V_I8(ps
), &V_BOOL(pd
));
377 case VT_UI8
: return VarBoolFromUI8(V_UI8(ps
), &V_BOOL(pd
));
378 case VT_R4
: return VarBoolFromR4(V_R4(ps
), &V_BOOL(pd
));
379 case VT_R8
: return VarBoolFromR8(V_R8(ps
), &V_BOOL(pd
));
380 case VT_DATE
: return VarBoolFromDate(V_DATE(ps
), &V_BOOL(pd
));
381 case VT_CY
: return VarBoolFromCy(V_CY(ps
), &V_BOOL(pd
));
382 case VT_DECIMAL
: return VarBoolFromDec(&V_DECIMAL(ps
), &V_BOOL(pd
));
383 case VT_DISPATCH
: return VarBoolFromDisp(V_DISPATCH(ps
), lcid
, &V_BOOL(pd
));
384 case VT_BSTR
: return VarBoolFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_BOOL(pd
));
392 V_BSTR(pd
) = SysAllocStringLen(NULL
, 0);
393 return V_BSTR(pd
) ? S_OK
: E_OUTOFMEMORY
;
395 if (wFlags
& (VARIANT_ALPHABOOL
|VARIANT_LOCALBOOL
))
396 return VarBstrFromBool(V_BOOL(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
397 return VarBstrFromI2(V_BOOL(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
398 case VT_I1
: return VarBstrFromI1(V_I1(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
399 case VT_I2
: return VarBstrFromI2(V_I2(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
400 case VT_I4
: return VarBstrFromI4(V_I4(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
401 case VT_UI1
: return VarBstrFromUI1(V_UI1(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
402 case VT_UI2
: return VarBstrFromUI2(V_UI2(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
403 case VT_UI4
: return VarBstrFromUI4(V_UI4(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
404 case VT_I8
: return VarBstrFromI8(V_I8(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
405 case VT_UI8
: return VarBstrFromUI8(V_UI8(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
406 case VT_R4
: return VarBstrFromR4(V_R4(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
407 case VT_R8
: return VarBstrFromR8(V_R8(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
408 case VT_DATE
: return VarBstrFromDate(V_DATE(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
409 case VT_CY
: return VarBstrFromCy(V_CY(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
410 case VT_DECIMAL
: return VarBstrFromDec(&V_DECIMAL(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
411 case VT_DISPATCH
: return VarBstrFromDisp(V_DISPATCH(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
418 case VT_EMPTY
: V_CY(pd
).int64
= 0; return S_OK
;
419 case VT_I1
: return VarCyFromI1(V_I1(ps
), &V_CY(pd
));
420 case VT_I2
: return VarCyFromI2(V_I2(ps
), &V_CY(pd
));
421 case VT_I4
: return VarCyFromI4(V_I4(ps
), &V_CY(pd
));
422 case VT_UI1
: return VarCyFromUI1(V_UI1(ps
), &V_CY(pd
));
423 case VT_UI2
: return VarCyFromUI2(V_UI2(ps
), &V_CY(pd
));
424 case VT_UI4
: return VarCyFromUI4(V_UI4(ps
), &V_CY(pd
));
425 case VT_I8
: return VarCyFromI8(V_I8(ps
), &V_CY(pd
));
426 case VT_UI8
: return VarCyFromUI8(V_UI8(ps
), &V_CY(pd
));
427 case VT_R4
: return VarCyFromR4(V_R4(ps
), &V_CY(pd
));
428 case VT_R8
: return VarCyFromR8(V_R8(ps
), &V_CY(pd
));
429 case VT_DATE
: return VarCyFromDate(V_DATE(ps
), &V_CY(pd
));
430 case VT_BOOL
: return VarCyFromBool(V_BOOL(ps
), &V_CY(pd
));
431 case VT_DECIMAL
: return VarCyFromDec(&V_DECIMAL(ps
), &V_CY(pd
));
432 case VT_DISPATCH
: return VarCyFromDisp(V_DISPATCH(ps
), lcid
, &V_CY(pd
));
433 case VT_BSTR
: return VarCyFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_CY(pd
));
442 DEC_SIGNSCALE(&V_DECIMAL(pd
)) = SIGNSCALE(DECIMAL_POS
,0);
443 DEC_HI32(&V_DECIMAL(pd
)) = 0;
444 DEC_MID32(&V_DECIMAL(pd
)) = 0;
445 /* VarDecFromBool() coerces to -1/0, ChangeTypeEx() coerces to 1/0.
446 * VT_NULL and VT_EMPTY always give a 0 value.
448 DEC_LO32(&V_DECIMAL(pd
)) = vtFrom
== VT_BOOL
&& V_BOOL(ps
) ? 1 : 0;
450 case VT_I1
: return VarDecFromI1(V_I1(ps
), &V_DECIMAL(pd
));
451 case VT_I2
: return VarDecFromI2(V_I2(ps
), &V_DECIMAL(pd
));
452 case VT_I4
: return VarDecFromI4(V_I4(ps
), &V_DECIMAL(pd
));
453 case VT_UI1
: return VarDecFromUI1(V_UI1(ps
), &V_DECIMAL(pd
));
454 case VT_UI2
: return VarDecFromUI2(V_UI2(ps
), &V_DECIMAL(pd
));
455 case VT_UI4
: return VarDecFromUI4(V_UI4(ps
), &V_DECIMAL(pd
));
456 case VT_I8
: return VarDecFromI8(V_I8(ps
), &V_DECIMAL(pd
));
457 case VT_UI8
: return VarDecFromUI8(V_UI8(ps
), &V_DECIMAL(pd
));
458 case VT_R4
: return VarDecFromR4(V_R4(ps
), &V_DECIMAL(pd
));
459 case VT_R8
: return VarDecFromR8(V_R8(ps
), &V_DECIMAL(pd
));
460 case VT_DATE
: return VarDecFromDate(V_DATE(ps
), &V_DECIMAL(pd
));
461 case VT_CY
: return VarDecFromCy(V_CY(ps
), &V_DECIMAL(pd
));
462 case VT_DISPATCH
: return VarDecFromDisp(V_DISPATCH(ps
), lcid
, &V_DECIMAL(pd
));
463 case VT_BSTR
: return VarDecFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_DECIMAL(pd
));
471 if (V_DISPATCH(ps
) == NULL
)
473 V_UNKNOWN(pd
) = NULL
;
477 res
= IDispatch_QueryInterface(V_DISPATCH(ps
), &IID_IUnknown
, (LPVOID
*)&V_UNKNOWN(pd
));
486 if (V_UNKNOWN(ps
) == NULL
)
488 V_DISPATCH(pd
) = NULL
;
492 res
= IUnknown_QueryInterface(V_UNKNOWN(ps
), &IID_IDispatch
, (LPVOID
*)&V_DISPATCH(pd
));
503 /* Coerce to/from an array */
504 static inline HRESULT
VARIANT_CoerceArray(VARIANTARG
* pd
, VARIANTARG
* ps
, VARTYPE vt
)
506 if (vt
== VT_BSTR
&& V_VT(ps
) == (VT_ARRAY
|VT_UI1
))
507 return BstrFromVector(V_ARRAY(ps
), &V_BSTR(pd
));
509 if (V_VT(ps
) == VT_BSTR
&& vt
== (VT_ARRAY
|VT_UI1
))
510 return VectorFromBstr(V_BSTR(ps
), &V_ARRAY(pd
));
513 return SafeArrayCopy(V_ARRAY(ps
), &V_ARRAY(pd
));
515 return DISP_E_TYPEMISMATCH
;
518 static HRESULT
VARIANT_FetchDispatchValue(LPVARIANT pvDispatch
, LPVARIANT pValue
)
521 static DISPPARAMS emptyParams
= { NULL
, NULL
, 0, 0 };
523 if ((V_VT(pvDispatch
) & VT_TYPEMASK
) == VT_DISPATCH
) {
524 if (NULL
== V_DISPATCH(pvDispatch
)) return DISP_E_TYPEMISMATCH
;
525 hres
= IDispatch_Invoke(V_DISPATCH(pvDispatch
), DISPID_VALUE
, &IID_NULL
,
526 LOCALE_USER_DEFAULT
, DISPATCH_PROPERTYGET
, &emptyParams
, pValue
,
529 hres
= DISP_E_TYPEMISMATCH
;
534 /******************************************************************************
535 * Check if a variants type is valid.
537 static inline HRESULT
VARIANT_ValidateType(VARTYPE vt
)
539 VARTYPE vtExtra
= vt
& VT_EXTRA_TYPE
;
543 if (!(vtExtra
& (VT_VECTOR
|VT_RESERVED
)))
545 if (vt
< VT_VOID
|| vt
== VT_RECORD
|| vt
== VT_CLSID
)
547 if ((vtExtra
& (VT_BYREF
|VT_ARRAY
)) && vt
<= VT_NULL
)
548 return DISP_E_BADVARTYPE
;
549 if (vt
!= (VARTYPE
)15)
553 return DISP_E_BADVARTYPE
;
556 /******************************************************************************
557 * VariantInit [OLEAUT32.8]
559 * Initialise a variant.
562 * pVarg [O] Variant to initialise
568 * This function simply sets the type of the variant to VT_EMPTY. It does not
569 * free any existing value, use VariantClear() for that.
571 void WINAPI
VariantInit(VARIANTARG
* pVarg
)
573 TRACE("(%p)\n", pVarg
);
575 /* Win8.1 zeroes whole struct. Previous implementations don't set any other fields. */
576 V_VT(pVarg
) = VT_EMPTY
;
579 HRESULT
VARIANT_ClearInd(VARIANTARG
*pVarg
)
583 TRACE("(%s)\n", debugstr_variant(pVarg
));
585 hres
= VARIANT_ValidateType(V_VT(pVarg
));
593 if (V_UNKNOWN(pVarg
))
594 IUnknown_Release(V_UNKNOWN(pVarg
));
596 case VT_UNKNOWN
| VT_BYREF
:
597 case VT_DISPATCH
| VT_BYREF
:
598 if(*V_UNKNOWNREF(pVarg
))
599 IUnknown_Release(*V_UNKNOWNREF(pVarg
));
602 SysFreeString(V_BSTR(pVarg
));
604 case VT_BSTR
| VT_BYREF
:
605 SysFreeString(*V_BSTRREF(pVarg
));
607 case VT_VARIANT
| VT_BYREF
:
608 VariantClear(V_VARIANTREF(pVarg
));
611 case VT_RECORD
| VT_BYREF
:
613 struct __tagBRECORD
* pBr
= &V_UNION(pVarg
,brecVal
);
616 IRecordInfo_RecordClear(pBr
->pRecInfo
, pBr
->pvRecord
);
617 IRecordInfo_Release(pBr
->pRecInfo
);
622 if (V_ISARRAY(pVarg
) || (V_VT(pVarg
) & ~VT_BYREF
) == VT_SAFEARRAY
)
624 if (V_ISBYREF(pVarg
))
626 if (*V_ARRAYREF(pVarg
))
627 hres
= SafeArrayDestroy(*V_ARRAYREF(pVarg
));
629 else if (V_ARRAY(pVarg
))
630 hres
= SafeArrayDestroy(V_ARRAY(pVarg
));
635 V_VT(pVarg
) = VT_EMPTY
;
639 /******************************************************************************
640 * VariantClear [OLEAUT32.9]
645 * pVarg [I/O] Variant to clear
648 * Success: S_OK. Any previous value in pVarg is freed and its type is set to VT_EMPTY.
649 * Failure: DISP_E_BADVARTYPE, if the variant is not a valid variant type.
651 HRESULT WINAPI
VariantClear(VARIANTARG
* pVarg
)
655 TRACE("(%s)\n", debugstr_variant(pVarg
));
657 hres
= VARIANT_ValidateType(V_VT(pVarg
));
661 if (!V_ISBYREF(pVarg
))
663 if (V_ISARRAY(pVarg
) || V_VT(pVarg
) == VT_SAFEARRAY
)
665 hres
= SafeArrayDestroy(V_ARRAY(pVarg
));
667 else if (V_VT(pVarg
) == VT_BSTR
)
669 SysFreeString(V_BSTR(pVarg
));
671 else if (V_VT(pVarg
) == VT_RECORD
)
673 struct __tagBRECORD
* pBr
= &V_UNION(pVarg
,brecVal
);
676 IRecordInfo_RecordClear(pBr
->pRecInfo
, pBr
->pvRecord
);
677 IRecordInfo_Release(pBr
->pRecInfo
);
680 else if (V_VT(pVarg
) == VT_DISPATCH
||
681 V_VT(pVarg
) == VT_UNKNOWN
)
683 if (V_UNKNOWN(pVarg
))
684 IUnknown_Release(V_UNKNOWN(pVarg
));
687 V_VT(pVarg
) = VT_EMPTY
;
692 /******************************************************************************
693 * Copy an IRecordInfo object contained in a variant.
695 static HRESULT
VARIANT_CopyIRecordInfo(VARIANT
*dest
, VARIANT
*src
)
697 struct __tagBRECORD
*dest_rec
= &V_UNION(dest
, brecVal
);
698 struct __tagBRECORD
*src_rec
= &V_UNION(src
, brecVal
);
702 if (!src_rec
->pRecInfo
)
704 if (src_rec
->pvRecord
) return E_INVALIDARG
;
708 hr
= IRecordInfo_GetSize(src_rec
->pRecInfo
, &size
);
709 if (FAILED(hr
)) return hr
;
711 /* This could look cleaner if only RecordCreate() was used, but native doesn't use it.
712 Memory should be allocated in a same way as RecordCreate() does, so RecordDestroy()
713 could free it later. */
714 dest_rec
->pvRecord
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, size
);
715 if (!dest_rec
->pvRecord
) return E_OUTOFMEMORY
;
717 dest_rec
->pRecInfo
= src_rec
->pRecInfo
;
718 IRecordInfo_AddRef(src_rec
->pRecInfo
);
720 return IRecordInfo_RecordCopy(src_rec
->pRecInfo
, src_rec
->pvRecord
, dest_rec
->pvRecord
);
723 /******************************************************************************
724 * VariantCopy [OLEAUT32.10]
729 * pvargDest [O] Destination for copy
730 * pvargSrc [I] Source variant to copy
733 * Success: S_OK. pvargDest contains a copy of pvargSrc.
734 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid type.
735 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
736 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
737 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
740 * - If pvargSrc == pvargDest, this function does nothing, and succeeds if
741 * pvargSrc is valid. Otherwise, pvargDest is always cleared using
742 * VariantClear() before pvargSrc is copied to it. If clearing pvargDest
743 * fails, so does this function.
744 * - VT_CLSID is a valid type type for pvargSrc, but not for pvargDest.
745 * - For by-value non-intrinsic types, a deep copy is made, i.e. The whole value
746 * is copied rather than just any pointers to it.
747 * - For by-value object types the object pointer is copied and the objects
748 * reference count increased using IUnknown_AddRef().
749 * - For all by-reference types, only the referencing pointer is copied.
751 HRESULT WINAPI
VariantCopy(VARIANTARG
* pvargDest
, VARIANTARG
* pvargSrc
)
755 TRACE("(%s,%s)\n", debugstr_variant(pvargDest
), debugstr_variant(pvargSrc
));
757 if (V_TYPE(pvargSrc
) == VT_CLSID
|| /* VT_CLSID is a special case */
758 FAILED(VARIANT_ValidateType(V_VT(pvargSrc
))))
759 return DISP_E_BADVARTYPE
;
761 if (pvargSrc
!= pvargDest
&&
762 SUCCEEDED(hres
= VariantClear(pvargDest
)))
764 *pvargDest
= *pvargSrc
; /* Shallow copy the value */
766 if (!V_ISBYREF(pvargSrc
))
768 switch (V_VT(pvargSrc
))
771 V_BSTR(pvargDest
) = SysAllocStringByteLen((char*)V_BSTR(pvargSrc
), SysStringByteLen(V_BSTR(pvargSrc
)));
772 if (!V_BSTR(pvargDest
))
773 hres
= E_OUTOFMEMORY
;
776 hres
= VARIANT_CopyIRecordInfo(pvargDest
, pvargSrc
);
780 V_UNKNOWN(pvargDest
) = V_UNKNOWN(pvargSrc
);
781 if (V_UNKNOWN(pvargSrc
))
782 IUnknown_AddRef(V_UNKNOWN(pvargSrc
));
785 if (V_ISARRAY(pvargSrc
))
786 hres
= SafeArrayCopy(V_ARRAY(pvargSrc
), &V_ARRAY(pvargDest
));
793 /* Return the byte size of a variants data */
794 static inline size_t VARIANT_DataSize(const VARIANT
* pv
)
799 case VT_UI1
: return sizeof(BYTE
);
801 case VT_UI2
: return sizeof(SHORT
);
805 case VT_UI4
: return sizeof(LONG
);
807 case VT_UI8
: return sizeof(LONGLONG
);
808 case VT_R4
: return sizeof(float);
809 case VT_R8
: return sizeof(double);
810 case VT_DATE
: return sizeof(DATE
);
811 case VT_BOOL
: return sizeof(VARIANT_BOOL
);
814 case VT_BSTR
: return sizeof(void*);
815 case VT_CY
: return sizeof(CY
);
816 case VT_ERROR
: return sizeof(SCODE
);
818 TRACE("Shouldn't be called for variant %s!\n", debugstr_variant(pv
));
822 /******************************************************************************
823 * VariantCopyInd [OLEAUT32.11]
825 * Copy a variant, dereferencing it if it is by-reference.
828 * pvargDest [O] Destination for copy
829 * pvargSrc [I] Source variant to copy
832 * Success: S_OK. pvargDest contains a copy of pvargSrc.
833 * Failure: An HRESULT error code indicating the error.
836 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid by-value type.
837 * E_INVALIDARG, if pvargSrc is an invalid by-reference type.
838 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
839 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
840 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
843 * - If pvargSrc is by-value, this function behaves exactly as VariantCopy().
844 * - If pvargSrc is by-reference, the value copied to pvargDest is the pointed-to
846 * - if pvargSrc == pvargDest, this function dereferences in place. Otherwise,
847 * pvargDest is always cleared using VariantClear() before pvargSrc is copied
848 * to it. If clearing pvargDest fails, so does this function.
850 HRESULT WINAPI
VariantCopyInd(VARIANT
* pvargDest
, VARIANTARG
* pvargSrc
)
852 VARIANTARG vTmp
, *pSrc
= pvargSrc
;
856 TRACE("(%s,%s)\n", debugstr_variant(pvargDest
), debugstr_variant(pvargSrc
));
858 if (!V_ISBYREF(pvargSrc
))
859 return VariantCopy(pvargDest
, pvargSrc
);
861 /* Argument checking is more lax than VariantCopy()... */
862 vt
= V_TYPE(pvargSrc
);
863 if (V_ISARRAY(pvargSrc
) || (V_VT(pvargSrc
) == (VT_RECORD
|VT_BYREF
)) ||
864 (vt
> VT_NULL
&& vt
!= (VARTYPE
)15 && vt
< VT_VOID
&&
865 !(V_VT(pvargSrc
) & (VT_VECTOR
|VT_RESERVED
))))
870 return E_INVALIDARG
; /* ...And the return value for invalid types differs too */
872 if (pvargSrc
== pvargDest
)
874 /* In place copy. Use a shallow copy of pvargSrc & init pvargDest.
875 * This avoids an expensive VariantCopy() call - e.g. SafeArrayCopy().
879 V_VT(pvargDest
) = VT_EMPTY
;
883 /* Copy into another variant. Free the variant in pvargDest */
884 if (FAILED(hres
= VariantClear(pvargDest
)))
886 TRACE("VariantClear() of destination failed\n");
893 /* Native doesn't check that *V_ARRAYREF(pSrc) is valid */
894 hres
= SafeArrayCopy(*V_ARRAYREF(pSrc
), &V_ARRAY(pvargDest
));
896 else if (V_VT(pSrc
) == (VT_BSTR
|VT_BYREF
))
898 /* Native doesn't check that *V_BSTRREF(pSrc) is valid */
899 V_BSTR(pvargDest
) = SysAllocStringByteLen((char*)*V_BSTRREF(pSrc
), SysStringByteLen(*V_BSTRREF(pSrc
)));
901 else if (V_VT(pSrc
) == (VT_RECORD
|VT_BYREF
))
903 hres
= VARIANT_CopyIRecordInfo(pvargDest
, pvargSrc
);
905 else if (V_VT(pSrc
) == (VT_DISPATCH
|VT_BYREF
) ||
906 V_VT(pSrc
) == (VT_UNKNOWN
|VT_BYREF
))
908 /* Native doesn't check that *V_UNKNOWNREF(pSrc) is valid */
909 V_UNKNOWN(pvargDest
) = *V_UNKNOWNREF(pSrc
);
910 if (*V_UNKNOWNREF(pSrc
))
911 IUnknown_AddRef(*V_UNKNOWNREF(pSrc
));
913 else if (V_VT(pSrc
) == (VT_VARIANT
|VT_BYREF
))
915 /* Native doesn't check that *V_VARIANTREF(pSrc) is valid */
916 if (V_VT(V_VARIANTREF(pSrc
)) == (VT_VARIANT
|VT_BYREF
))
917 hres
= E_INVALIDARG
; /* Don't dereference more than one level */
919 hres
= VariantCopyInd(pvargDest
, V_VARIANTREF(pSrc
));
921 /* Use the dereferenced variants type value, not VT_VARIANT */
922 goto VariantCopyInd_Return
;
924 else if (V_VT(pSrc
) == (VT_DECIMAL
|VT_BYREF
))
926 memcpy(&DEC_SCALE(&V_DECIMAL(pvargDest
)), &DEC_SCALE(V_DECIMALREF(pSrc
)),
927 sizeof(DECIMAL
) - sizeof(USHORT
));
931 /* Copy the pointed to data into this variant */
932 memcpy(&V_BYREF(pvargDest
), V_BYREF(pSrc
), VARIANT_DataSize(pSrc
));
935 V_VT(pvargDest
) = V_VT(pSrc
) & ~VT_BYREF
;
937 VariantCopyInd_Return
:
939 if (pSrc
!= pvargSrc
)
942 TRACE("returning 0x%08x, %s\n", hres
, debugstr_variant(pvargDest
));
946 /******************************************************************************
947 * VariantChangeType [OLEAUT32.12]
949 * Change the type of a variant.
952 * pvargDest [O] Destination for the converted variant
953 * pvargSrc [O] Source variant to change the type of
954 * wFlags [I] VARIANT_ flags from "oleauto.h"
955 * vt [I] Variant type to change pvargSrc into
958 * Success: S_OK. pvargDest contains the converted value.
959 * Failure: An HRESULT error code describing the failure.
962 * The LCID used for the conversion is LOCALE_USER_DEFAULT.
963 * See VariantChangeTypeEx.
965 HRESULT WINAPI
VariantChangeType(VARIANTARG
* pvargDest
, VARIANTARG
* pvargSrc
,
966 USHORT wFlags
, VARTYPE vt
)
968 return VariantChangeTypeEx( pvargDest
, pvargSrc
, LOCALE_USER_DEFAULT
, wFlags
, vt
);
971 /******************************************************************************
972 * VariantChangeTypeEx [OLEAUT32.147]
974 * Change the type of a variant.
977 * pvargDest [O] Destination for the converted variant
978 * pvargSrc [O] Source variant to change the type of
979 * lcid [I] LCID for the conversion
980 * wFlags [I] VARIANT_ flags from "oleauto.h"
981 * vt [I] Variant type to change pvargSrc into
984 * Success: S_OK. pvargDest contains the converted value.
985 * Failure: An HRESULT error code describing the failure.
988 * pvargDest and pvargSrc can point to the same variant to perform an in-place
989 * conversion. If the conversion is successful, pvargSrc will be freed.
991 HRESULT WINAPI
VariantChangeTypeEx(VARIANTARG
* pvargDest
, VARIANTARG
* pvargSrc
,
992 LCID lcid
, USHORT wFlags
, VARTYPE vt
)
996 TRACE("(%s,%s,0x%08x,0x%04x,%s)\n", debugstr_variant(pvargDest
),
997 debugstr_variant(pvargSrc
), lcid
, wFlags
, debugstr_vt(vt
));
1000 res
= DISP_E_BADVARTYPE
;
1003 res
= VARIANT_ValidateType(V_VT(pvargSrc
));
1007 res
= VARIANT_ValidateType(vt
);
1011 VARIANTARG vTmp
, vSrcDeref
;
1013 if(V_ISBYREF(pvargSrc
) && !V_BYREF(pvargSrc
))
1014 res
= DISP_E_TYPEMISMATCH
;
1017 V_VT(&vTmp
) = VT_EMPTY
;
1018 V_VT(&vSrcDeref
) = VT_EMPTY
;
1019 VariantClear(&vTmp
);
1020 VariantClear(&vSrcDeref
);
1025 res
= VariantCopyInd(&vSrcDeref
, pvargSrc
);
1028 if (V_ISARRAY(&vSrcDeref
) || (vt
& VT_ARRAY
))
1029 res
= VARIANT_CoerceArray(&vTmp
, &vSrcDeref
, vt
);
1031 res
= VARIANT_Coerce(&vTmp
, lcid
, wFlags
, &vSrcDeref
, vt
);
1033 if (SUCCEEDED(res
)) {
1035 res
= VariantCopy(pvargDest
, &vTmp
);
1037 VariantClear(&vTmp
);
1038 VariantClear(&vSrcDeref
);
1045 TRACE("returning 0x%08x, %s\n", res
, debugstr_variant(pvargDest
));
1049 /* Date Conversions */
1051 #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
1053 /* Convert a VT_DATE value to a Julian Date */
1054 static inline int VARIANT_JulianFromDate(int dateIn
)
1056 int julianDays
= dateIn
;
1058 julianDays
-= DATE_MIN
; /* Convert to + days from 1 Jan 100 AD */
1059 julianDays
+= 1757585; /* Convert to + days from 23 Nov 4713 BC (Julian) */
1063 /* Convert a Julian Date to a VT_DATE value */
1064 static inline int VARIANT_DateFromJulian(int dateIn
)
1066 int julianDays
= dateIn
;
1068 julianDays
-= 1757585; /* Convert to + days from 1 Jan 100 AD */
1069 julianDays
+= DATE_MIN
; /* Convert to +/- days from 1 Jan 1899 AD */
1073 /* Convert a Julian date to Day/Month/Year - from PostgreSQL */
1074 static inline void VARIANT_DMYFromJulian(int jd
, USHORT
*year
, USHORT
*month
, USHORT
*day
)
1080 l
-= (n
* 146097 + 3) / 4;
1081 i
= (4000 * (l
+ 1)) / 1461001;
1082 l
+= 31 - (i
* 1461) / 4;
1083 j
= (l
* 80) / 2447;
1084 *day
= l
- (j
* 2447) / 80;
1086 *month
= (j
+ 2) - (12 * l
);
1087 *year
= 100 * (n
- 49) + i
+ l
;
1090 /* Convert Day/Month/Year to a Julian date - from PostgreSQL */
1091 static inline double VARIANT_JulianFromDMY(USHORT year
, USHORT month
, USHORT day
)
1093 int m12
= (month
- 14) / 12;
1095 return ((1461 * (year
+ 4800 + m12
)) / 4 + (367 * (month
- 2 - 12 * m12
)) / 12 -
1096 (3 * ((year
+ 4900 + m12
) / 100)) / 4 + day
- 32075);
1099 /* Macros for accessing DOS format date/time fields */
1100 #define DOS_YEAR(x) (1980 + (x >> 9))
1101 #define DOS_MONTH(x) ((x >> 5) & 0xf)
1102 #define DOS_DAY(x) (x & 0x1f)
1103 #define DOS_HOUR(x) (x >> 11)
1104 #define DOS_MINUTE(x) ((x >> 5) & 0x3f)
1105 #define DOS_SECOND(x) ((x & 0x1f) << 1)
1106 /* Create a DOS format date/time */
1107 #define DOS_DATE(d,m,y) (d | (m << 5) | ((y-1980) << 9))
1108 #define DOS_TIME(h,m,s) ((s >> 1) | (m << 5) | (h << 11))
1110 /* Roll a date forwards or backwards to correct it */
1111 static HRESULT
VARIANT_RollUdate(UDATE
*lpUd
)
1113 static const BYTE days
[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1114 short iYear
, iMonth
, iDay
, iHour
, iMinute
, iSecond
;
1116 /* interpret values signed */
1117 iYear
= lpUd
->st
.wYear
;
1118 iMonth
= lpUd
->st
.wMonth
;
1119 iDay
= lpUd
->st
.wDay
;
1120 iHour
= lpUd
->st
.wHour
;
1121 iMinute
= lpUd
->st
.wMinute
;
1122 iSecond
= lpUd
->st
.wSecond
;
1124 TRACE("Raw date: %d/%d/%d %d:%d:%d\n", iDay
, iMonth
,
1125 iYear
, iHour
, iMinute
, iSecond
);
1127 if (iYear
> 9999 || iYear
< -9999)
1128 return E_INVALIDARG
; /* Invalid value */
1129 /* Year 0 to 29 are treated as 2000 + year */
1130 if (iYear
>= 0 && iYear
< 30)
1132 /* Remaining years < 100 are treated as 1900 + year */
1133 else if (iYear
>= 30 && iYear
< 100)
1136 iMinute
+= iSecond
/ 60;
1137 iSecond
= iSecond
% 60;
1138 iHour
+= iMinute
/ 60;
1139 iMinute
= iMinute
% 60;
1142 iYear
+= iMonth
/ 12;
1143 iMonth
= iMonth
% 12;
1144 if (iMonth
<=0) {iMonth
+=12; iYear
--;}
1145 while (iDay
> days
[iMonth
])
1147 if (iMonth
== 2 && IsLeapYear(iYear
))
1150 iDay
-= days
[iMonth
];
1152 iYear
+= iMonth
/ 12;
1153 iMonth
= iMonth
% 12;
1158 if (iMonth
<=0) {iMonth
+=12; iYear
--;}
1159 if (iMonth
== 2 && IsLeapYear(iYear
))
1162 iDay
+= days
[iMonth
];
1165 if (iSecond
<0){iSecond
+=60; iMinute
--;}
1166 if (iMinute
<0){iMinute
+=60; iHour
--;}
1167 if (iHour
<0) {iHour
+=24; iDay
--;}
1168 if (iYear
<=0) iYear
+=2000;
1170 lpUd
->st
.wYear
= iYear
;
1171 lpUd
->st
.wMonth
= iMonth
;
1172 lpUd
->st
.wDay
= iDay
;
1173 lpUd
->st
.wHour
= iHour
;
1174 lpUd
->st
.wMinute
= iMinute
;
1175 lpUd
->st
.wSecond
= iSecond
;
1177 TRACE("Rolled date: %d/%d/%d %d:%d:%d\n", lpUd
->st
.wDay
, lpUd
->st
.wMonth
,
1178 lpUd
->st
.wYear
, lpUd
->st
.wHour
, lpUd
->st
.wMinute
, lpUd
->st
.wSecond
);
1182 /**********************************************************************
1183 * DosDateTimeToVariantTime [OLEAUT32.14]
1185 * Convert a Dos format date and time into variant VT_DATE format.
1188 * wDosDate [I] Dos format date
1189 * wDosTime [I] Dos format time
1190 * pDateOut [O] Destination for VT_DATE format
1193 * Success: TRUE. pDateOut contains the converted time.
1194 * Failure: FALSE, if wDosDate or wDosTime are invalid (see notes).
1197 * - Dos format dates can only hold dates from 1-Jan-1980 to 31-Dec-2099.
1198 * - Dos format times are accurate to only 2 second precision.
1199 * - The format of a Dos Date is:
1200 *| Bits Values Meaning
1201 *| ---- ------ -------
1202 *| 0-4 1-31 Day of the week. 0 rolls back one day. A value greater than
1203 *| the days in the month rolls forward the extra days.
1204 *| 5-8 1-12 Month of the year. 0 rolls back to December of the previous
1205 *| year. 13-15 are invalid.
1206 *| 9-15 0-119 Year based from 1980 (Max 2099). 120-127 are invalid.
1207 * - The format of a Dos Time is:
1208 *| Bits Values Meaning
1209 *| ---- ------ -------
1210 *| 0-4 0-29 Seconds/2. 30 and 31 are invalid.
1211 *| 5-10 0-59 Minutes. 60-63 are invalid.
1212 *| 11-15 0-23 Hours (24 hour clock). 24-32 are invalid.
1214 INT WINAPI
DosDateTimeToVariantTime(USHORT wDosDate
, USHORT wDosTime
,
1219 TRACE("(0x%x(%d/%d/%d),0x%x(%d:%d:%d),%p)\n",
1220 wDosDate
, DOS_YEAR(wDosDate
), DOS_MONTH(wDosDate
), DOS_DAY(wDosDate
),
1221 wDosTime
, DOS_HOUR(wDosTime
), DOS_MINUTE(wDosTime
), DOS_SECOND(wDosTime
),
1224 ud
.st
.wYear
= DOS_YEAR(wDosDate
);
1225 ud
.st
.wMonth
= DOS_MONTH(wDosDate
);
1226 if (ud
.st
.wYear
> 2099 || ud
.st
.wMonth
> 12)
1228 ud
.st
.wDay
= DOS_DAY(wDosDate
);
1229 ud
.st
.wHour
= DOS_HOUR(wDosTime
);
1230 ud
.st
.wMinute
= DOS_MINUTE(wDosTime
);
1231 ud
.st
.wSecond
= DOS_SECOND(wDosTime
);
1232 ud
.st
.wDayOfWeek
= ud
.st
.wMilliseconds
= 0;
1233 if (ud
.st
.wHour
> 23 || ud
.st
.wMinute
> 59 || ud
.st
.wSecond
> 59)
1234 return FALSE
; /* Invalid values in Dos*/
1236 return VarDateFromUdate(&ud
, 0, pDateOut
) == S_OK
;
1239 /**********************************************************************
1240 * VariantTimeToDosDateTime [OLEAUT32.13]
1242 * Convert a variant format date into a Dos format date and time.
1244 * dateIn [I] VT_DATE time format
1245 * pwDosDate [O] Destination for Dos format date
1246 * pwDosTime [O] Destination for Dos format time
1249 * Success: TRUE. pwDosDate and pwDosTime contains the converted values.
1250 * Failure: FALSE, if dateIn cannot be represented in Dos format.
1253 * See DosDateTimeToVariantTime() for Dos format details and bugs.
1255 INT WINAPI
VariantTimeToDosDateTime(double dateIn
, USHORT
*pwDosDate
, USHORT
*pwDosTime
)
1259 TRACE("(%g,%p,%p)\n", dateIn
, pwDosDate
, pwDosTime
);
1261 if (FAILED(VarUdateFromDate(dateIn
, 0, &ud
)))
1264 if (ud
.st
.wYear
< 1980 || ud
.st
.wYear
> 2099)
1267 *pwDosDate
= DOS_DATE(ud
.st
.wDay
, ud
.st
.wMonth
, ud
.st
.wYear
);
1268 *pwDosTime
= DOS_TIME(ud
.st
.wHour
, ud
.st
.wMinute
, ud
.st
.wSecond
);
1270 TRACE("Returning 0x%x(%d/%d/%d), 0x%x(%d:%d:%d)\n",
1271 *pwDosDate
, DOS_YEAR(*pwDosDate
), DOS_MONTH(*pwDosDate
), DOS_DAY(*pwDosDate
),
1272 *pwDosTime
, DOS_HOUR(*pwDosTime
), DOS_MINUTE(*pwDosTime
), DOS_SECOND(*pwDosTime
));
1276 /***********************************************************************
1277 * SystemTimeToVariantTime [OLEAUT32.184]
1279 * Convert a System format date and time into variant VT_DATE format.
1282 * lpSt [I] System format date and time
1283 * pDateOut [O] Destination for VT_DATE format date
1286 * Success: TRUE. *pDateOut contains the converted value.
1287 * Failure: FALSE, if lpSt cannot be represented in VT_DATE format.
1289 INT WINAPI
SystemTimeToVariantTime(LPSYSTEMTIME lpSt
, double *pDateOut
)
1293 TRACE("(%p->%d/%d/%d %d:%d:%d,%p)\n", lpSt
, lpSt
->wDay
, lpSt
->wMonth
,
1294 lpSt
->wYear
, lpSt
->wHour
, lpSt
->wMinute
, lpSt
->wSecond
, pDateOut
);
1296 if (lpSt
->wMonth
> 12)
1298 if (lpSt
->wDay
> 31)
1300 if ((short)lpSt
->wYear
< 0)
1304 return VarDateFromUdate(&ud
, 0, pDateOut
) == S_OK
;
1307 /***********************************************************************
1308 * VariantTimeToSystemTime [OLEAUT32.185]
1310 * Convert a variant VT_DATE into a System format date and time.
1313 * datein [I] Variant VT_DATE format date
1314 * lpSt [O] Destination for System format date and time
1317 * Success: TRUE. *lpSt contains the converted value.
1318 * Failure: FALSE, if dateIn is too large or small.
1320 INT WINAPI
VariantTimeToSystemTime(double dateIn
, LPSYSTEMTIME lpSt
)
1324 TRACE("(%g,%p)\n", dateIn
, lpSt
);
1326 if (FAILED(VarUdateFromDate(dateIn
, 0, &ud
)))
1333 /***********************************************************************
1334 * VarDateFromUdateEx [OLEAUT32.319]
1336 * Convert an unpacked format date and time to a variant VT_DATE.
1339 * pUdateIn [I] Unpacked format date and time to convert
1340 * lcid [I] Locale identifier for the conversion
1341 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1342 * pDateOut [O] Destination for variant VT_DATE.
1345 * Success: S_OK. *pDateOut contains the converted value.
1346 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1348 HRESULT WINAPI
VarDateFromUdateEx(UDATE
*pUdateIn
, LCID lcid
, ULONG dwFlags
, DATE
*pDateOut
)
1353 TRACE("(%p->%d/%d/%d %d:%d:%d:%d %d %d,0x%08x,0x%08x,%p)\n", pUdateIn
,
1354 pUdateIn
->st
.wMonth
, pUdateIn
->st
.wDay
, pUdateIn
->st
.wYear
,
1355 pUdateIn
->st
.wHour
, pUdateIn
->st
.wMinute
, pUdateIn
->st
.wSecond
,
1356 pUdateIn
->st
.wMilliseconds
, pUdateIn
->st
.wDayOfWeek
,
1357 pUdateIn
->wDayOfYear
, lcid
, dwFlags
, pDateOut
);
1359 if (lcid
!= MAKELCID(MAKELANGID(LANG_ENGLISH
, SUBLANG_ENGLISH_US
), SORT_DEFAULT
))
1360 FIXME("lcid possibly not handled, treating as en-us\n");
1361 if (dwFlags
& ~(VAR_TIMEVALUEONLY
|VAR_DATEVALUEONLY
))
1362 FIXME("unsupported flags: %x\n", dwFlags
);
1366 if (dwFlags
& VAR_VALIDDATE
)
1367 WARN("Ignoring VAR_VALIDDATE\n");
1369 if (FAILED(VARIANT_RollUdate(&ud
)))
1370 return E_INVALIDARG
;
1373 if (!(dwFlags
& VAR_TIMEVALUEONLY
))
1374 dateVal
= VARIANT_DateFromJulian(VARIANT_JulianFromDMY(ud
.st
.wYear
, ud
.st
.wMonth
, ud
.st
.wDay
));
1376 if ((dwFlags
& VAR_TIMEVALUEONLY
) || !(dwFlags
& VAR_DATEVALUEONLY
))
1378 double dateSign
= (dateVal
< 0.0) ? -1.0 : 1.0;
1381 dateVal
+= ud
.st
.wHour
/ 24.0 * dateSign
;
1382 dateVal
+= ud
.st
.wMinute
/ 1440.0 * dateSign
;
1383 dateVal
+= ud
.st
.wSecond
/ 86400.0 * dateSign
;
1386 TRACE("Returning %g\n", dateVal
);
1387 *pDateOut
= dateVal
;
1391 /***********************************************************************
1392 * VarDateFromUdate [OLEAUT32.330]
1394 * Convert an unpacked format date and time to a variant VT_DATE.
1397 * pUdateIn [I] Unpacked format date and time to convert
1398 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1399 * pDateOut [O] Destination for variant VT_DATE.
1402 * Success: S_OK. *pDateOut contains the converted value.
1403 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1406 * This function uses the United States English locale for the conversion. Use
1407 * VarDateFromUdateEx() for alternate locales.
1409 HRESULT WINAPI
VarDateFromUdate(UDATE
*pUdateIn
, ULONG dwFlags
, DATE
*pDateOut
)
1411 LCID lcid
= MAKELCID(MAKELANGID(LANG_ENGLISH
, SUBLANG_ENGLISH_US
), SORT_DEFAULT
);
1413 return VarDateFromUdateEx(pUdateIn
, lcid
, dwFlags
, pDateOut
);
1416 /***********************************************************************
1417 * VarUdateFromDate [OLEAUT32.331]
1419 * Convert a variant VT_DATE into an unpacked format date and time.
1422 * datein [I] Variant VT_DATE format date
1423 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1424 * lpUdate [O] Destination for unpacked format date and time
1427 * Success: S_OK. *lpUdate contains the converted value.
1428 * Failure: E_INVALIDARG, if dateIn is too large or small.
1430 HRESULT WINAPI
VarUdateFromDate(DATE dateIn
, ULONG dwFlags
, UDATE
*lpUdate
)
1432 /* Cumulative totals of days per month */
1433 static const USHORT cumulativeDays
[] =
1435 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
1437 double datePart
, timePart
;
1440 TRACE("(%g,0x%08x,%p)\n", dateIn
, dwFlags
, lpUdate
);
1442 if (dateIn
<= (DATE_MIN
- 1.0) || dateIn
>= (DATE_MAX
+ 1.0))
1443 return E_INVALIDARG
;
1445 datePart
= dateIn
< 0.0 ? ceil(dateIn
) : floor(dateIn
);
1446 /* Compensate for int truncation (always downwards) */
1447 timePart
= fabs(dateIn
- datePart
) + 0.00000000001;
1448 if (timePart
>= 1.0)
1449 timePart
-= 0.00000000001;
1452 julianDays
= VARIANT_JulianFromDate(dateIn
);
1453 VARIANT_DMYFromJulian(julianDays
, &lpUdate
->st
.wYear
, &lpUdate
->st
.wMonth
,
1456 datePart
= (datePart
+ 1.5) / 7.0;
1457 lpUdate
->st
.wDayOfWeek
= (datePart
- floor(datePart
)) * 7;
1458 if (lpUdate
->st
.wDayOfWeek
== 0)
1459 lpUdate
->st
.wDayOfWeek
= 5;
1460 else if (lpUdate
->st
.wDayOfWeek
== 1)
1461 lpUdate
->st
.wDayOfWeek
= 6;
1463 lpUdate
->st
.wDayOfWeek
-= 2;
1465 if (lpUdate
->st
.wMonth
> 2 && IsLeapYear(lpUdate
->st
.wYear
))
1466 lpUdate
->wDayOfYear
= 1; /* After February, in a leap year */
1468 lpUdate
->wDayOfYear
= 0;
1470 lpUdate
->wDayOfYear
+= cumulativeDays
[lpUdate
->st
.wMonth
];
1471 lpUdate
->wDayOfYear
+= lpUdate
->st
.wDay
;
1475 lpUdate
->st
.wHour
= timePart
;
1476 timePart
-= lpUdate
->st
.wHour
;
1478 lpUdate
->st
.wMinute
= timePart
;
1479 timePart
-= lpUdate
->st
.wMinute
;
1481 lpUdate
->st
.wSecond
= timePart
;
1482 timePart
-= lpUdate
->st
.wSecond
;
1483 lpUdate
->st
.wMilliseconds
= 0;
1486 /* Round the milliseconds, adjusting the time/date forward if needed */
1487 if (lpUdate
->st
.wSecond
< 59)
1488 lpUdate
->st
.wSecond
++;
1491 lpUdate
->st
.wSecond
= 0;
1492 if (lpUdate
->st
.wMinute
< 59)
1493 lpUdate
->st
.wMinute
++;
1496 lpUdate
->st
.wMinute
= 0;
1497 if (lpUdate
->st
.wHour
< 23)
1498 lpUdate
->st
.wHour
++;
1501 lpUdate
->st
.wHour
= 0;
1502 /* Roll over a whole day */
1503 if (++lpUdate
->st
.wDay
> 28)
1504 VARIANT_RollUdate(lpUdate
);
1512 #define GET_NUMBER_TEXT(fld,name) \
1514 if (!GetLocaleInfoW(lcid, lctype|fld, buff, 2)) \
1515 WARN("buffer too small for " #fld "\n"); \
1517 if (buff[0]) lpChars->name = buff[0]; \
1518 TRACE("lcid 0x%x, " #name "=%d '%c'\n", lcid, lpChars->name, lpChars->name)
1520 /* Get the valid number characters for an lcid */
1521 static void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS
*lpChars
, LCID lcid
, DWORD dwFlags
)
1523 static const VARIANT_NUMBER_CHARS defaultChars
= { '-','+','.',',','$',0,'.',',' };
1524 static VARIANT_NUMBER_CHARS lastChars
;
1525 static LCID lastLcid
= -1;
1526 static DWORD lastFlags
= 0;
1527 LCTYPE lctype
= dwFlags
& LOCALE_NOUSEROVERRIDE
;
1530 /* To make caching thread-safe, a critical section is needed */
1531 EnterCriticalSection(&cache_cs
);
1533 /* Asking for default locale entries is very expensive: It is a registry
1534 server call. So cache one locally, as Microsoft does it too */
1535 if(lcid
== lastLcid
&& dwFlags
== lastFlags
)
1537 memcpy(lpChars
, &lastChars
, sizeof(defaultChars
));
1538 LeaveCriticalSection(&cache_cs
);
1542 memcpy(lpChars
, &defaultChars
, sizeof(defaultChars
));
1543 GET_NUMBER_TEXT(LOCALE_SNEGATIVESIGN
, cNegativeSymbol
);
1544 GET_NUMBER_TEXT(LOCALE_SPOSITIVESIGN
, cPositiveSymbol
);
1545 GET_NUMBER_TEXT(LOCALE_SDECIMAL
, cDecimalPoint
);
1546 GET_NUMBER_TEXT(LOCALE_STHOUSAND
, cDigitSeparator
);
1547 GET_NUMBER_TEXT(LOCALE_SMONDECIMALSEP
, cCurrencyDecimalPoint
);
1548 GET_NUMBER_TEXT(LOCALE_SMONTHOUSANDSEP
, cCurrencyDigitSeparator
);
1550 /* Local currency symbols are often 2 characters */
1551 lpChars
->cCurrencyLocal2
= '\0';
1552 switch(GetLocaleInfoW(lcid
, lctype
|LOCALE_SCURRENCY
, buff
, sizeof(buff
)/sizeof(WCHAR
)))
1554 case 3: lpChars
->cCurrencyLocal2
= buff
[1]; /* Fall through */
1555 case 2: lpChars
->cCurrencyLocal
= buff
[0];
1557 default: WARN("buffer too small for LOCALE_SCURRENCY\n");
1559 TRACE("lcid 0x%x, cCurrencyLocal =%d,%d '%c','%c'\n", lcid
, lpChars
->cCurrencyLocal
,
1560 lpChars
->cCurrencyLocal2
, lpChars
->cCurrencyLocal
, lpChars
->cCurrencyLocal2
);
1562 memcpy(&lastChars
, lpChars
, sizeof(defaultChars
));
1564 lastFlags
= dwFlags
;
1565 LeaveCriticalSection(&cache_cs
);
1568 /* Number Parsing States */
1569 #define B_PROCESSING_EXPONENT 0x1
1570 #define B_NEGATIVE_EXPONENT 0x2
1571 #define B_EXPONENT_START 0x4
1572 #define B_INEXACT_ZEROS 0x8
1573 #define B_LEADING_ZERO 0x10
1574 #define B_PROCESSING_HEX 0x20
1575 #define B_PROCESSING_OCT 0x40
1577 /**********************************************************************
1578 * VarParseNumFromStr [OLEAUT32.46]
1580 * Parse a string containing a number into a NUMPARSE structure.
1583 * lpszStr [I] String to parse number from
1584 * lcid [I] Locale Id for the conversion
1585 * dwFlags [I] 0, or LOCALE_NOUSEROVERRIDE to use system default number chars
1586 * pNumprs [I/O] Destination for parsed number
1587 * rgbDig [O] Destination for digits read in
1590 * Success: S_OK. pNumprs and rgbDig contain the parsed representation of
1592 * Failure: E_INVALIDARG, if any parameter is invalid.
1593 * DISP_E_TYPEMISMATCH, if the string is not a number or is formatted
1595 * DISP_E_OVERFLOW, if rgbDig is too small to hold the number.
1598 * pNumprs must have the following fields set:
1599 * cDig: Set to the size of rgbDig.
1600 * dwInFlags: Set to the allowable syntax of the number using NUMPRS_ flags
1604 * - I am unsure if this function should parse non-Arabic (e.g. Thai)
1605 * numerals, so this has not been implemented.
1607 HRESULT WINAPI
VarParseNumFromStr(OLECHAR
*lpszStr
, LCID lcid
, ULONG dwFlags
,
1608 NUMPARSE
*pNumprs
, BYTE
*rgbDig
)
1610 VARIANT_NUMBER_CHARS chars
;
1612 DWORD dwState
= B_EXPONENT_START
|B_INEXACT_ZEROS
;
1613 int iMaxDigits
= sizeof(rgbTmp
) / sizeof(BYTE
);
1616 TRACE("(%s,%d,0x%08x,%p,%p)\n", debugstr_w(lpszStr
), lcid
, dwFlags
, pNumprs
, rgbDig
);
1618 if (!pNumprs
|| !rgbDig
)
1619 return E_INVALIDARG
;
1621 if (pNumprs
->cDig
< iMaxDigits
)
1622 iMaxDigits
= pNumprs
->cDig
;
1625 pNumprs
->dwOutFlags
= 0;
1626 pNumprs
->cchUsed
= 0;
1627 pNumprs
->nBaseShift
= 0;
1628 pNumprs
->nPwr10
= 0;
1631 return DISP_E_TYPEMISMATCH
;
1633 VARIANT_GetLocalisedNumberChars(&chars
, lcid
, dwFlags
);
1635 /* First consume all the leading symbols and space from the string */
1638 if (pNumprs
->dwInFlags
& NUMPRS_LEADING_WHITE
&& isspaceW(*lpszStr
))
1640 pNumprs
->dwOutFlags
|= NUMPRS_LEADING_WHITE
;
1645 } while (isspaceW(*lpszStr
));
1647 else if (pNumprs
->dwInFlags
& NUMPRS_LEADING_PLUS
&&
1648 *lpszStr
== chars
.cPositiveSymbol
&&
1649 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_PLUS
))
1651 pNumprs
->dwOutFlags
|= NUMPRS_LEADING_PLUS
;
1655 else if (pNumprs
->dwInFlags
& NUMPRS_LEADING_MINUS
&&
1656 *lpszStr
== chars
.cNegativeSymbol
&&
1657 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_MINUS
))
1659 pNumprs
->dwOutFlags
|= (NUMPRS_LEADING_MINUS
|NUMPRS_NEG
);
1663 else if (pNumprs
->dwInFlags
& NUMPRS_CURRENCY
&&
1664 !(pNumprs
->dwOutFlags
& NUMPRS_CURRENCY
) &&
1665 *lpszStr
== chars
.cCurrencyLocal
&&
1666 (!chars
.cCurrencyLocal2
|| lpszStr
[1] == chars
.cCurrencyLocal2
))
1668 pNumprs
->dwOutFlags
|= NUMPRS_CURRENCY
;
1671 /* Only accept currency characters */
1672 chars
.cDecimalPoint
= chars
.cCurrencyDecimalPoint
;
1673 chars
.cDigitSeparator
= chars
.cCurrencyDigitSeparator
;
1675 else if (pNumprs
->dwInFlags
& NUMPRS_PARENS
&& *lpszStr
== '(' &&
1676 !(pNumprs
->dwOutFlags
& NUMPRS_PARENS
))
1678 pNumprs
->dwOutFlags
|= NUMPRS_PARENS
;
1686 if (!(pNumprs
->dwOutFlags
& NUMPRS_CURRENCY
))
1688 /* Only accept non-currency characters */
1689 chars
.cCurrencyDecimalPoint
= chars
.cDecimalPoint
;
1690 chars
.cCurrencyDigitSeparator
= chars
.cDigitSeparator
;
1693 if ((*lpszStr
== '&' && (*(lpszStr
+1) == 'H' || *(lpszStr
+1) == 'h')) &&
1694 pNumprs
->dwInFlags
& NUMPRS_HEX_OCT
)
1696 dwState
|= B_PROCESSING_HEX
;
1697 pNumprs
->dwOutFlags
|= NUMPRS_HEX_OCT
;
1701 else if ((*lpszStr
== '&' && (*(lpszStr
+1) == 'O' || *(lpszStr
+1) == 'o')) &&
1702 pNumprs
->dwInFlags
& NUMPRS_HEX_OCT
)
1704 dwState
|= B_PROCESSING_OCT
;
1705 pNumprs
->dwOutFlags
|= NUMPRS_HEX_OCT
;
1710 /* Strip Leading zeros */
1711 while (*lpszStr
== '0')
1713 dwState
|= B_LEADING_ZERO
;
1720 if (isdigitW(*lpszStr
))
1722 if (dwState
& B_PROCESSING_EXPONENT
)
1724 int exponentSize
= 0;
1725 if (dwState
& B_EXPONENT_START
)
1727 if (!isdigitW(*lpszStr
))
1728 break; /* No exponent digits - invalid */
1729 while (*lpszStr
== '0')
1731 /* Skip leading zero's in the exponent */
1737 while (isdigitW(*lpszStr
))
1740 exponentSize
+= *lpszStr
- '0';
1744 if (dwState
& B_NEGATIVE_EXPONENT
)
1745 exponentSize
= -exponentSize
;
1746 /* Add the exponent into the powers of 10 */
1747 pNumprs
->nPwr10
+= exponentSize
;
1748 dwState
&= ~(B_PROCESSING_EXPONENT
|B_EXPONENT_START
);
1749 lpszStr
--; /* back up to allow processing of next char */
1753 if ((pNumprs
->cDig
>= iMaxDigits
) && !(dwState
& B_PROCESSING_HEX
)
1754 && !(dwState
& B_PROCESSING_OCT
))
1756 pNumprs
->dwOutFlags
|= NUMPRS_INEXACT
;
1758 if (*lpszStr
!= '0')
1759 dwState
&= ~B_INEXACT_ZEROS
; /* Inexact number with non-trailing zeros */
1761 /* This digit can't be represented, but count it in nPwr10 */
1762 if (pNumprs
->dwOutFlags
& NUMPRS_DECIMAL
)
1769 if ((dwState
& B_PROCESSING_OCT
) && ((*lpszStr
== '8') || (*lpszStr
== '9')))
1772 if (pNumprs
->dwOutFlags
& NUMPRS_DECIMAL
)
1773 pNumprs
->nPwr10
--; /* Count decimal points in nPwr10 */
1775 rgbTmp
[pNumprs
->cDig
] = *lpszStr
- '0';
1781 else if (*lpszStr
== chars
.cDigitSeparator
&& pNumprs
->dwInFlags
& NUMPRS_THOUSANDS
)
1783 pNumprs
->dwOutFlags
|= NUMPRS_THOUSANDS
;
1786 else if (*lpszStr
== chars
.cDecimalPoint
&&
1787 pNumprs
->dwInFlags
& NUMPRS_DECIMAL
&&
1788 !(pNumprs
->dwOutFlags
& (NUMPRS_DECIMAL
|NUMPRS_EXPONENT
)))
1790 pNumprs
->dwOutFlags
|= NUMPRS_DECIMAL
;
1793 /* If we have no digits so far, skip leading zeros */
1796 while (lpszStr
[1] == '0')
1798 dwState
|= B_LEADING_ZERO
;
1805 else if (((*lpszStr
>= 'a' && *lpszStr
<= 'f') ||
1806 (*lpszStr
>= 'A' && *lpszStr
<= 'F')) &&
1807 dwState
& B_PROCESSING_HEX
)
1809 if (pNumprs
->cDig
>= iMaxDigits
)
1811 return DISP_E_OVERFLOW
;
1815 if (*lpszStr
>= 'a')
1816 rgbTmp
[pNumprs
->cDig
] = *lpszStr
- 'a' + 10;
1818 rgbTmp
[pNumprs
->cDig
] = *lpszStr
- 'A' + 10;
1823 else if ((*lpszStr
== 'e' || *lpszStr
== 'E') &&
1824 pNumprs
->dwInFlags
& NUMPRS_EXPONENT
&&
1825 !(pNumprs
->dwOutFlags
& NUMPRS_EXPONENT
))
1827 dwState
|= B_PROCESSING_EXPONENT
;
1828 pNumprs
->dwOutFlags
|= NUMPRS_EXPONENT
;
1831 else if (dwState
& B_PROCESSING_EXPONENT
&& *lpszStr
== chars
.cPositiveSymbol
)
1833 cchUsed
++; /* Ignore positive exponent */
1835 else if (dwState
& B_PROCESSING_EXPONENT
&& *lpszStr
== chars
.cNegativeSymbol
)
1837 dwState
|= B_NEGATIVE_EXPONENT
;
1841 break; /* Stop at an unrecognised character */
1846 if (!pNumprs
->cDig
&& dwState
& B_LEADING_ZERO
)
1848 /* Ensure a 0 on its own gets stored */
1853 if (pNumprs
->dwOutFlags
& NUMPRS_EXPONENT
&& dwState
& B_PROCESSING_EXPONENT
)
1855 pNumprs
->cchUsed
= cchUsed
;
1856 WARN("didn't completely parse exponent\n");
1857 return DISP_E_TYPEMISMATCH
; /* Failed to completely parse the exponent */
1860 if (pNumprs
->dwOutFlags
& NUMPRS_INEXACT
)
1862 if (dwState
& B_INEXACT_ZEROS
)
1863 pNumprs
->dwOutFlags
&= ~NUMPRS_INEXACT
; /* All zeros doesn't set NUMPRS_INEXACT */
1864 } else if(pNumprs
->dwInFlags
& NUMPRS_HEX_OCT
)
1866 /* copy all of the digits into the output digit buffer */
1867 /* this is exactly what windows does although it also returns */
1868 /* cDig of X and writes X+Y where Y>=0 number of digits to rgbDig */
1869 memcpy(rgbDig
, rgbTmp
, pNumprs
->cDig
* sizeof(BYTE
));
1871 if (dwState
& B_PROCESSING_HEX
) {
1872 /* hex numbers have always the same format */
1874 pNumprs
->nBaseShift
=4;
1876 if (dwState
& B_PROCESSING_OCT
) {
1877 /* oct numbers have always the same format */
1879 pNumprs
->nBaseShift
=3;
1881 while (pNumprs
->cDig
> 1 && !rgbTmp
[pNumprs
->cDig
- 1])
1890 /* Remove trailing zeros from the last (whole number or decimal) part */
1891 while (pNumprs
->cDig
> 1 && !rgbTmp
[pNumprs
->cDig
- 1])
1898 if (pNumprs
->cDig
<= iMaxDigits
)
1899 pNumprs
->dwOutFlags
&= ~NUMPRS_INEXACT
; /* Ignore stripped zeros for NUMPRS_INEXACT */
1901 pNumprs
->cDig
= iMaxDigits
; /* Only return iMaxDigits worth of digits */
1903 /* Copy the digits we processed into rgbDig */
1904 memcpy(rgbDig
, rgbTmp
, pNumprs
->cDig
* sizeof(BYTE
));
1906 /* Consume any trailing symbols and space */
1909 if ((pNumprs
->dwInFlags
& NUMPRS_TRAILING_WHITE
) && isspaceW(*lpszStr
))
1911 pNumprs
->dwOutFlags
|= NUMPRS_TRAILING_WHITE
;
1916 } while (isspaceW(*lpszStr
));
1918 else if (pNumprs
->dwInFlags
& NUMPRS_TRAILING_PLUS
&&
1919 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_PLUS
) &&
1920 *lpszStr
== chars
.cPositiveSymbol
)
1922 pNumprs
->dwOutFlags
|= NUMPRS_TRAILING_PLUS
;
1926 else if (pNumprs
->dwInFlags
& NUMPRS_TRAILING_MINUS
&&
1927 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_MINUS
) &&
1928 *lpszStr
== chars
.cNegativeSymbol
)
1930 pNumprs
->dwOutFlags
|= (NUMPRS_TRAILING_MINUS
|NUMPRS_NEG
);
1934 else if (pNumprs
->dwInFlags
& NUMPRS_PARENS
&& *lpszStr
== ')' &&
1935 pNumprs
->dwOutFlags
& NUMPRS_PARENS
)
1939 pNumprs
->dwOutFlags
|= NUMPRS_NEG
;
1945 if (pNumprs
->dwOutFlags
& NUMPRS_PARENS
&& !(pNumprs
->dwOutFlags
& NUMPRS_NEG
))
1947 pNumprs
->cchUsed
= cchUsed
;
1948 return DISP_E_TYPEMISMATCH
; /* Opening parenthesis not matched */
1951 if (pNumprs
->dwInFlags
& NUMPRS_USE_ALL
&& *lpszStr
!= '\0')
1952 return DISP_E_TYPEMISMATCH
; /* Not all chars were consumed */
1955 return DISP_E_TYPEMISMATCH
; /* No Number found */
1957 pNumprs
->cchUsed
= cchUsed
;
1961 /* VTBIT flags indicating an integer value */
1962 #define INTEGER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8)
1963 /* VTBIT flags indicating a real number value */
1964 #define REAL_VTBITS (VTBIT_R4|VTBIT_R8|VTBIT_CY)
1966 /* Helper macros to check whether bit pattern fits in VARIANT (x is a ULONG64 ) */
1967 #define FITS_AS_I1(x) ((x) >> 8 == 0)
1968 #define FITS_AS_I2(x) ((x) >> 16 == 0)
1969 #define FITS_AS_I4(x) ((x) >> 32 == 0)
1971 /**********************************************************************
1972 * VarNumFromParseNum [OLEAUT32.47]
1974 * Convert a NUMPARSE structure into a numeric Variant type.
1977 * pNumprs [I] Source for parsed number. cDig must be set to the size of rgbDig
1978 * rgbDig [I] Source for the numbers digits
1979 * dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
1980 * pVarDst [O] Destination for the converted Variant value.
1983 * Success: S_OK. pVarDst contains the converted value.
1984 * Failure: E_INVALIDARG, if any parameter is invalid.
1985 * DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
1988 * - The smallest favoured type present in dwVtBits that can represent the
1989 * number in pNumprs without losing precision is used.
1990 * - Signed types are preferred over unsigned types of the same size.
1991 * - Preferred types in order are: integer, float, double, currency then decimal.
1992 * - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
1993 * for details of the rounding method.
1994 * - pVarDst is not cleared before the result is stored in it.
1995 * - WinXP and Win2003 support VTBIT_I8, VTBIT_UI8 but that's buggy (by
1996 * design?): If some other VTBIT's for integers are specified together
1997 * with VTBIT_I8 and the number will fit only in a VT_I8 Windows will "cast"
1998 * the number to the smallest requested integer truncating this way the
1999 * number. Wine doesn't implement this "feature" (yet?).
2001 HRESULT WINAPI
VarNumFromParseNum(NUMPARSE
*pNumprs
, BYTE
*rgbDig
,
2002 ULONG dwVtBits
, VARIANT
*pVarDst
)
2004 /* Scale factors and limits for double arithmetic */
2005 static const double dblMultipliers
[11] = {
2006 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
2007 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
2009 static const double dblMinimums
[11] = {
2010 R8_MIN
, R8_MIN
*10.0, R8_MIN
*100.0, R8_MIN
*1000.0, R8_MIN
*10000.0,
2011 R8_MIN
*100000.0, R8_MIN
*1000000.0, R8_MIN
*10000000.0,
2012 R8_MIN
*100000000.0, R8_MIN
*1000000000.0, R8_MIN
*10000000000.0
2014 static const double dblMaximums
[11] = {
2015 R8_MAX
, R8_MAX
/10.0, R8_MAX
/100.0, R8_MAX
/1000.0, R8_MAX
/10000.0,
2016 R8_MAX
/100000.0, R8_MAX
/1000000.0, R8_MAX
/10000000.0,
2017 R8_MAX
/100000000.0, R8_MAX
/1000000000.0, R8_MAX
/10000000000.0
2020 int wholeNumberDigits
, fractionalDigits
, divisor10
= 0, multiplier10
= 0;
2022 TRACE("(%p,%p,0x%x,%p)\n", pNumprs
, rgbDig
, dwVtBits
, pVarDst
);
2024 if (pNumprs
->nBaseShift
)
2026 /* nBaseShift indicates a hex or octal number */
2031 /* Convert the hex or octal number string into a UI64 */
2032 for (i
= 0; i
< pNumprs
->cDig
; i
++)
2034 if (ul64
> ((UI8_MAX
>>pNumprs
->nBaseShift
) - rgbDig
[i
]))
2036 TRACE("Overflow multiplying digits\n");
2037 return DISP_E_OVERFLOW
;
2039 ul64
= (ul64
<<pNumprs
->nBaseShift
) + rgbDig
[i
];
2042 /* also make a negative representation */
2045 /* Try signed and unsigned types in size order */
2046 if (dwVtBits
& VTBIT_I1
&& FITS_AS_I1(ul64
))
2048 V_VT(pVarDst
) = VT_I1
;
2049 V_I1(pVarDst
) = ul64
;
2052 else if (dwVtBits
& VTBIT_UI1
&& FITS_AS_I1(ul64
))
2054 V_VT(pVarDst
) = VT_UI1
;
2055 V_UI1(pVarDst
) = ul64
;
2058 else if (dwVtBits
& VTBIT_I2
&& FITS_AS_I2(ul64
))
2060 V_VT(pVarDst
) = VT_I2
;
2061 V_I2(pVarDst
) = ul64
;
2064 else if (dwVtBits
& VTBIT_UI2
&& FITS_AS_I2(ul64
))
2066 V_VT(pVarDst
) = VT_UI2
;
2067 V_UI2(pVarDst
) = ul64
;
2070 else if (dwVtBits
& VTBIT_I4
&& FITS_AS_I4(ul64
))
2072 V_VT(pVarDst
) = VT_I4
;
2073 V_I4(pVarDst
) = ul64
;
2076 else if (dwVtBits
& VTBIT_UI4
&& FITS_AS_I4(ul64
))
2078 V_VT(pVarDst
) = VT_UI4
;
2079 V_UI4(pVarDst
) = ul64
;
2082 else if (dwVtBits
& VTBIT_I8
&& ((ul64
<= I8_MAX
)||(l64
>=I8_MIN
)))
2084 V_VT(pVarDst
) = VT_I8
;
2085 V_I8(pVarDst
) = ul64
;
2088 else if (dwVtBits
& VTBIT_UI8
)
2090 V_VT(pVarDst
) = VT_UI8
;
2091 V_UI8(pVarDst
) = ul64
;
2094 else if ((dwVtBits
& VTBIT_DECIMAL
) == VTBIT_DECIMAL
)
2096 V_VT(pVarDst
) = VT_DECIMAL
;
2097 DEC_SIGNSCALE(&V_DECIMAL(pVarDst
)) = SIGNSCALE(DECIMAL_POS
,0);
2098 DEC_HI32(&V_DECIMAL(pVarDst
)) = 0;
2099 DEC_LO64(&V_DECIMAL(pVarDst
)) = ul64
;
2102 else if (dwVtBits
& VTBIT_R4
&& ((ul64
<= I4_MAX
)||(l64
>= I4_MIN
)))
2104 V_VT(pVarDst
) = VT_R4
;
2106 V_R4(pVarDst
) = ul64
;
2108 V_R4(pVarDst
) = l64
;
2111 else if (dwVtBits
& VTBIT_R8
&& ((ul64
<= I4_MAX
)||(l64
>= I4_MIN
)))
2113 V_VT(pVarDst
) = VT_R8
;
2115 V_R8(pVarDst
) = ul64
;
2117 V_R8(pVarDst
) = l64
;
2121 TRACE("Overflow: possible return types: 0x%x, value: %s\n", dwVtBits
, wine_dbgstr_longlong(ul64
));
2122 return DISP_E_OVERFLOW
;
2125 /* Count the number of relevant fractional and whole digits stored,
2126 * And compute the divisor/multiplier to scale the number by.
2128 if (pNumprs
->nPwr10
< 0)
2130 if (-pNumprs
->nPwr10
>= pNumprs
->cDig
)
2132 /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
2133 wholeNumberDigits
= 0;
2134 fractionalDigits
= pNumprs
->cDig
;
2135 divisor10
= -pNumprs
->nPwr10
;
2139 /* An exactly represented real number e.g. 1.024 */
2140 wholeNumberDigits
= pNumprs
->cDig
+ pNumprs
->nPwr10
;
2141 fractionalDigits
= pNumprs
->cDig
- wholeNumberDigits
;
2142 divisor10
= pNumprs
->cDig
- wholeNumberDigits
;
2145 else if (pNumprs
->nPwr10
== 0)
2147 /* An exactly represented whole number e.g. 1024 */
2148 wholeNumberDigits
= pNumprs
->cDig
;
2149 fractionalDigits
= 0;
2151 else /* pNumprs->nPwr10 > 0 */
2153 /* A whole number followed by nPwr10 0's e.g. 102400 */
2154 wholeNumberDigits
= pNumprs
->cDig
;
2155 fractionalDigits
= 0;
2156 multiplier10
= pNumprs
->nPwr10
;
2159 TRACE("cDig %d; nPwr10 %d, whole %d, frac %d mult %d; div %d\n",
2160 pNumprs
->cDig
, pNumprs
->nPwr10
, wholeNumberDigits
, fractionalDigits
,
2161 multiplier10
, divisor10
);
2163 if (dwVtBits
& (INTEGER_VTBITS
|VTBIT_DECIMAL
) &&
2164 (!fractionalDigits
|| !(dwVtBits
& (REAL_VTBITS
|VTBIT_CY
|VTBIT_DECIMAL
))))
2166 /* We have one or more integer output choices, and either:
2167 * 1) An integer input value, or
2168 * 2) A real number input value but no floating output choices.
2169 * Alternately, we have a DECIMAL output available and an integer input.
2171 * So, place the integer value into pVarDst, using the smallest type
2172 * possible and preferring signed over unsigned types.
2174 BOOL bOverflow
= FALSE
, bNegative
;
2178 /* Convert the integer part of the number into a UI8 */
2179 for (i
= 0; i
< wholeNumberDigits
; i
++)
2181 if (ul64
> UI8_MAX
/ 10 || (ul64
== UI8_MAX
/ 10 && rgbDig
[i
] > UI8_MAX
% 10))
2183 TRACE("Overflow multiplying digits\n");
2187 ul64
= ul64
* 10 + rgbDig
[i
];
2190 /* Account for the scale of the number */
2191 if (!bOverflow
&& multiplier10
)
2193 for (i
= 0; i
< multiplier10
; i
++)
2195 if (ul64
> (UI8_MAX
/ 10))
2197 TRACE("Overflow scaling number\n");
2205 /* If we have any fractional digits, round the value.
2206 * Note we don't have to do this if divisor10 is < 1,
2207 * because this means the fractional part must be < 0.5
2209 if (!bOverflow
&& fractionalDigits
&& divisor10
> 0)
2211 const BYTE
* fracDig
= rgbDig
+ wholeNumberDigits
;
2212 BOOL bAdjust
= FALSE
;
2214 TRACE("first decimal value is %d\n", *fracDig
);
2217 bAdjust
= TRUE
; /* > 0.5 */
2218 else if (*fracDig
== 5)
2220 for (i
= 1; i
< fractionalDigits
; i
++)
2224 bAdjust
= TRUE
; /* > 0.5 */
2228 /* If exactly 0.5, round only odd values */
2229 if (i
== fractionalDigits
&& (ul64
& 1))
2235 if (ul64
== UI8_MAX
)
2237 TRACE("Overflow after rounding\n");
2244 /* Zero is not a negative number */
2245 bNegative
= pNumprs
->dwOutFlags
& NUMPRS_NEG
&& ul64
;
2247 TRACE("Integer value is 0x%s, bNeg %d\n", wine_dbgstr_longlong(ul64
), bNegative
);
2249 /* For negative integers, try the signed types in size order */
2250 if (!bOverflow
&& bNegative
)
2252 if (dwVtBits
& (VTBIT_I1
|VTBIT_I2
|VTBIT_I4
|VTBIT_I8
))
2254 if (dwVtBits
& VTBIT_I1
&& ul64
<= -I1_MIN
)
2256 V_VT(pVarDst
) = VT_I1
;
2257 V_I1(pVarDst
) = -ul64
;
2260 else if (dwVtBits
& VTBIT_I2
&& ul64
<= -I2_MIN
)
2262 V_VT(pVarDst
) = VT_I2
;
2263 V_I2(pVarDst
) = -ul64
;
2266 else if (dwVtBits
& VTBIT_I4
&& ul64
<= -((LONGLONG
)I4_MIN
))
2268 V_VT(pVarDst
) = VT_I4
;
2269 V_I4(pVarDst
) = -ul64
;
2272 else if (dwVtBits
& VTBIT_I8
&& ul64
<= (ULONGLONG
)I8_MAX
+ 1)
2274 V_VT(pVarDst
) = VT_I8
;
2275 V_I8(pVarDst
) = -ul64
;
2278 else if ((dwVtBits
& REAL_VTBITS
) == VTBIT_DECIMAL
)
2280 /* Decimal is only output choice left - fast path */
2281 V_VT(pVarDst
) = VT_DECIMAL
;
2282 DEC_SIGNSCALE(&V_DECIMAL(pVarDst
)) = SIGNSCALE(DECIMAL_NEG
,0);
2283 DEC_HI32(&V_DECIMAL(pVarDst
)) = 0;
2284 DEC_LO64(&V_DECIMAL(pVarDst
)) = -ul64
;
2289 else if (!bOverflow
)
2291 /* For positive integers, try signed then unsigned types in size order */
2292 if (dwVtBits
& VTBIT_I1
&& ul64
<= I1_MAX
)
2294 V_VT(pVarDst
) = VT_I1
;
2295 V_I1(pVarDst
) = ul64
;
2298 else if (dwVtBits
& VTBIT_UI1
&& ul64
<= UI1_MAX
)
2300 V_VT(pVarDst
) = VT_UI1
;
2301 V_UI1(pVarDst
) = ul64
;
2304 else if (dwVtBits
& VTBIT_I2
&& ul64
<= I2_MAX
)
2306 V_VT(pVarDst
) = VT_I2
;
2307 V_I2(pVarDst
) = ul64
;
2310 else if (dwVtBits
& VTBIT_UI2
&& ul64
<= UI2_MAX
)
2312 V_VT(pVarDst
) = VT_UI2
;
2313 V_UI2(pVarDst
) = ul64
;
2316 else if (dwVtBits
& VTBIT_I4
&& ul64
<= I4_MAX
)
2318 V_VT(pVarDst
) = VT_I4
;
2319 V_I4(pVarDst
) = ul64
;
2322 else if (dwVtBits
& VTBIT_UI4
&& ul64
<= UI4_MAX
)
2324 V_VT(pVarDst
) = VT_UI4
;
2325 V_UI4(pVarDst
) = ul64
;
2328 else if (dwVtBits
& VTBIT_I8
&& ul64
<= I8_MAX
)
2330 V_VT(pVarDst
) = VT_I8
;
2331 V_I8(pVarDst
) = ul64
;
2334 else if (dwVtBits
& VTBIT_UI8
)
2336 V_VT(pVarDst
) = VT_UI8
;
2337 V_UI8(pVarDst
) = ul64
;
2340 else if ((dwVtBits
& REAL_VTBITS
) == VTBIT_DECIMAL
)
2342 /* Decimal is only output choice left - fast path */
2343 V_VT(pVarDst
) = VT_DECIMAL
;
2344 DEC_SIGNSCALE(&V_DECIMAL(pVarDst
)) = SIGNSCALE(DECIMAL_POS
,0);
2345 DEC_HI32(&V_DECIMAL(pVarDst
)) = 0;
2346 DEC_LO64(&V_DECIMAL(pVarDst
)) = ul64
;
2352 if (dwVtBits
& REAL_VTBITS
)
2354 /* Try to put the number into a float or real */
2355 BOOL bOverflow
= FALSE
, bNegative
= pNumprs
->dwOutFlags
& NUMPRS_NEG
;
2359 /* Convert the number into a double */
2360 for (i
= 0; i
< pNumprs
->cDig
; i
++)
2361 whole
= whole
* 10.0 + rgbDig
[i
];
2363 TRACE("Whole double value is %16.16g\n", whole
);
2365 /* Account for the scale */
2366 while (multiplier10
> 10)
2368 if (whole
> dblMaximums
[10])
2370 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
);
2374 whole
= whole
* dblMultipliers
[10];
2377 if (multiplier10
&& !bOverflow
)
2379 if (whole
> dblMaximums
[multiplier10
])
2381 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
);
2385 whole
= whole
* dblMultipliers
[multiplier10
];
2389 TRACE("Scaled double value is %16.16g\n", whole
);
2391 while (divisor10
> 10 && !bOverflow
)
2393 if (whole
< dblMinimums
[10] && whole
!= 0)
2395 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
); /* Underflow */
2399 whole
= whole
/ dblMultipliers
[10];
2402 if (divisor10
&& !bOverflow
)
2404 if (whole
< dblMinimums
[divisor10
] && whole
!= 0)
2406 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
); /* Underflow */
2410 whole
= whole
/ dblMultipliers
[divisor10
];
2413 TRACE("Final double value is %16.16g\n", whole
);
2415 if (dwVtBits
& VTBIT_R4
&&
2416 ((whole
<= R4_MAX
&& whole
>= R4_MIN
) || whole
== 0.0))
2418 TRACE("Set R4 to final value\n");
2419 V_VT(pVarDst
) = VT_R4
; /* Fits into a float */
2420 V_R4(pVarDst
) = pNumprs
->dwOutFlags
& NUMPRS_NEG
? -whole
: whole
;
2424 if (dwVtBits
& VTBIT_R8
)
2426 TRACE("Set R8 to final value\n");
2427 V_VT(pVarDst
) = VT_R8
; /* Fits into a double */
2428 V_R8(pVarDst
) = pNumprs
->dwOutFlags
& NUMPRS_NEG
? -whole
: whole
;
2432 if (dwVtBits
& VTBIT_CY
)
2434 if (SUCCEEDED(VarCyFromR8(bNegative
? -whole
: whole
, &V_CY(pVarDst
))))
2436 V_VT(pVarDst
) = VT_CY
; /* Fits into a currency */
2437 TRACE("Set CY to final value\n");
2440 TRACE("Value Overflows CY\n");
2444 if (dwVtBits
& VTBIT_DECIMAL
)
2449 DECIMAL
* pDec
= &V_DECIMAL(pVarDst
);
2451 DECIMAL_SETZERO(*pDec
);
2454 if (pNumprs
->dwOutFlags
& NUMPRS_NEG
)
2455 DEC_SIGN(pDec
) = DECIMAL_NEG
;
2457 DEC_SIGN(pDec
) = DECIMAL_POS
;
2459 /* Factor the significant digits */
2460 for (i
= 0; i
< pNumprs
->cDig
; i
++)
2462 tmp
= (ULONG64
)DEC_LO32(pDec
) * 10 + rgbDig
[i
];
2463 carry
= (ULONG
)(tmp
>> 32);
2464 DEC_LO32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2465 tmp
= (ULONG64
)DEC_MID32(pDec
) * 10 + carry
;
2466 carry
= (ULONG
)(tmp
>> 32);
2467 DEC_MID32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2468 tmp
= (ULONG64
)DEC_HI32(pDec
) * 10 + carry
;
2469 DEC_HI32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2471 if (tmp
>> 32 & UI4_MAX
)
2473 VarNumFromParseNum_DecOverflow
:
2474 TRACE("Overflow\n");
2475 DEC_LO32(pDec
) = DEC_MID32(pDec
) = DEC_HI32(pDec
) = UI4_MAX
;
2476 return DISP_E_OVERFLOW
;
2480 /* Account for the scale of the number */
2481 while (multiplier10
> 0)
2483 tmp
= (ULONG64
)DEC_LO32(pDec
) * 10;
2484 carry
= (ULONG
)(tmp
>> 32);
2485 DEC_LO32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2486 tmp
= (ULONG64
)DEC_MID32(pDec
) * 10 + carry
;
2487 carry
= (ULONG
)(tmp
>> 32);
2488 DEC_MID32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2489 tmp
= (ULONG64
)DEC_HI32(pDec
) * 10 + carry
;
2490 DEC_HI32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2492 if (tmp
>> 32 & UI4_MAX
)
2493 goto VarNumFromParseNum_DecOverflow
;
2496 DEC_SCALE(pDec
) = divisor10
;
2498 V_VT(pVarDst
) = VT_DECIMAL
;
2501 return DISP_E_OVERFLOW
; /* No more output choices */
2504 /**********************************************************************
2505 * VarCat [OLEAUT32.318]
2507 * Concatenates one variant onto another.
2510 * left [I] First variant
2511 * right [I] Second variant
2512 * result [O] Result variant
2516 * Failure: An HRESULT error code indicating the error.
2518 HRESULT WINAPI
VarCat(LPVARIANT left
, LPVARIANT right
, LPVARIANT out
)
2520 BSTR left_str
= NULL
, right_str
= NULL
;
2521 VARTYPE leftvt
, rightvt
;
2524 TRACE("%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), out
);
2526 leftvt
= V_VT(left
);
2527 rightvt
= V_VT(right
);
2529 /* when both left and right are NULL the result is NULL */
2530 if (leftvt
== VT_NULL
&& rightvt
== VT_NULL
)
2532 V_VT(out
) = VT_NULL
;
2536 /* There are many special case for errors and return types */
2537 if (leftvt
== VT_VARIANT
&& (rightvt
== VT_ERROR
||
2538 rightvt
== VT_DATE
|| rightvt
== VT_DECIMAL
))
2539 hres
= DISP_E_TYPEMISMATCH
;
2540 else if ((leftvt
== VT_I2
|| leftvt
== VT_I4
||
2541 leftvt
== VT_R4
|| leftvt
== VT_R8
||
2542 leftvt
== VT_CY
|| leftvt
== VT_BOOL
||
2543 leftvt
== VT_BSTR
|| leftvt
== VT_I1
||
2544 leftvt
== VT_UI1
|| leftvt
== VT_UI2
||
2545 leftvt
== VT_UI4
|| leftvt
== VT_I8
||
2546 leftvt
== VT_UI8
|| leftvt
== VT_INT
||
2547 leftvt
== VT_UINT
|| leftvt
== VT_EMPTY
||
2548 leftvt
== VT_NULL
|| leftvt
== VT_DATE
||
2549 leftvt
== VT_DECIMAL
|| leftvt
== VT_DISPATCH
)
2551 (rightvt
== VT_I2
|| rightvt
== VT_I4
||
2552 rightvt
== VT_R4
|| rightvt
== VT_R8
||
2553 rightvt
== VT_CY
|| rightvt
== VT_BOOL
||
2554 rightvt
== VT_BSTR
|| rightvt
== VT_I1
||
2555 rightvt
== VT_UI1
|| rightvt
== VT_UI2
||
2556 rightvt
== VT_UI4
|| rightvt
== VT_I8
||
2557 rightvt
== VT_UI8
|| rightvt
== VT_INT
||
2558 rightvt
== VT_UINT
|| rightvt
== VT_EMPTY
||
2559 rightvt
== VT_NULL
|| rightvt
== VT_DATE
||
2560 rightvt
== VT_DECIMAL
|| rightvt
== VT_DISPATCH
))
2562 else if (rightvt
== VT_ERROR
&& leftvt
< VT_VOID
)
2563 hres
= DISP_E_TYPEMISMATCH
;
2564 else if (leftvt
== VT_ERROR
&& (rightvt
== VT_DATE
||
2565 rightvt
== VT_ERROR
|| rightvt
== VT_DECIMAL
))
2566 hres
= DISP_E_TYPEMISMATCH
;
2567 else if (rightvt
== VT_DATE
|| rightvt
== VT_ERROR
||
2568 rightvt
== VT_DECIMAL
)
2569 hres
= DISP_E_BADVARTYPE
;
2570 else if (leftvt
== VT_ERROR
|| rightvt
== VT_ERROR
)
2571 hres
= DISP_E_TYPEMISMATCH
;
2572 else if (leftvt
== VT_VARIANT
)
2573 hres
= DISP_E_TYPEMISMATCH
;
2574 else if (rightvt
== VT_VARIANT
&& (leftvt
== VT_EMPTY
||
2575 leftvt
== VT_NULL
|| leftvt
== VT_I2
||
2576 leftvt
== VT_I4
|| leftvt
== VT_R4
||
2577 leftvt
== VT_R8
|| leftvt
== VT_CY
||
2578 leftvt
== VT_DATE
|| leftvt
== VT_BSTR
||
2579 leftvt
== VT_BOOL
|| leftvt
== VT_DECIMAL
||
2580 leftvt
== VT_I1
|| leftvt
== VT_UI1
||
2581 leftvt
== VT_UI2
|| leftvt
== VT_UI4
||
2582 leftvt
== VT_I8
|| leftvt
== VT_UI8
||
2583 leftvt
== VT_INT
|| leftvt
== VT_UINT
))
2584 hres
= DISP_E_TYPEMISMATCH
;
2586 hres
= DISP_E_BADVARTYPE
;
2588 /* if result type is not S_OK, then no need to go further */
2591 V_VT(out
) = VT_EMPTY
;
2595 if (leftvt
== VT_BSTR
)
2596 left_str
= V_BSTR(left
);
2599 VARIANT converted
, *tmp
= left
;
2601 VariantInit(&converted
);
2602 if(leftvt
== VT_DISPATCH
)
2604 hres
= VARIANT_FetchDispatchValue(left
, &converted
);
2611 hres
= VariantChangeTypeEx(&converted
, tmp
, 0, VARIANT_ALPHABOOL
|VARIANT_LOCALBOOL
, VT_BSTR
);
2612 if (SUCCEEDED(hres
))
2613 left_str
= V_BSTR(&converted
);
2614 else if (hres
!= DISP_E_TYPEMISMATCH
)
2616 VariantClear(&converted
);
2621 if (rightvt
== VT_BSTR
)
2622 right_str
= V_BSTR(right
);
2625 VARIANT converted
, *tmp
= right
;
2627 VariantInit(&converted
);
2628 if(rightvt
== VT_DISPATCH
)
2630 hres
= VARIANT_FetchDispatchValue(right
, &converted
);
2637 hres
= VariantChangeTypeEx(&converted
, tmp
, 0, VARIANT_ALPHABOOL
|VARIANT_LOCALBOOL
, VT_BSTR
);
2638 if (SUCCEEDED(hres
))
2639 right_str
= V_BSTR(&converted
);
2640 else if (hres
!= DISP_E_TYPEMISMATCH
)
2642 VariantClear(&converted
);
2648 V_VT(out
) = VT_BSTR
;
2649 hres
= VarBstrCat(left_str
, right_str
, &V_BSTR(out
));
2652 if(V_VT(left
) != VT_BSTR
)
2653 SysFreeString(left_str
);
2654 if(V_VT(right
) != VT_BSTR
)
2655 SysFreeString(right_str
);
2660 /* Wrapper around VariantChangeTypeEx() which permits changing a
2661 variant with VT_RESERVED flag set. Needed by VarCmp. */
2662 static HRESULT
_VarChangeTypeExWrap (VARIANTARG
* pvargDest
,
2663 VARIANTARG
* pvargSrc
, LCID lcid
, USHORT wFlags
, VARTYPE vt
)
2665 VARIANTARG vtmpsrc
= *pvargSrc
;
2667 V_VT(&vtmpsrc
) &= ~VT_RESERVED
;
2668 return VariantChangeTypeEx(pvargDest
,&vtmpsrc
,lcid
,wFlags
,vt
);
2671 /**********************************************************************
2672 * VarCmp [OLEAUT32.176]
2674 * Compare two variants.
2677 * left [I] First variant
2678 * right [I] Second variant
2679 * lcid [I] LCID (locale identifier) for the comparison
2680 * flags [I] Flags to be used in the comparison:
2681 * NORM_IGNORECASE, NORM_IGNORENONSPACE, NORM_IGNORESYMBOLS,
2682 * NORM_IGNOREWIDTH, NORM_IGNOREKANATYPE, NORM_IGNOREKASHIDA
2685 * VARCMP_LT: left variant is less than right variant.
2686 * VARCMP_EQ: input variants are equal.
2687 * VARCMP_GT: left variant is greater than right variant.
2688 * VARCMP_NULL: either one of the input variants is NULL.
2689 * Failure: An HRESULT error code indicating the error.
2692 * Native VarCmp up to and including WinXP doesn't like I1, UI2, VT_UI4,
2693 * UI8 and UINT as input variants. INT is accepted only as left variant.
2695 * If both input variants are ERROR then VARCMP_EQ will be returned, else
2696 * an ERROR variant will trigger an error.
2698 * Both input variants can have VT_RESERVED flag set which is ignored
2699 * unless one and only one of the variants is a BSTR and the other one
2700 * is not an EMPTY variant. All four VT_RESERVED combinations have a
2701 * different meaning:
2702 * - BSTR and other: BSTR is always greater than the other variant.
2703 * - BSTR|VT_RESERVED and other: a string comparison is performed.
2704 * - BSTR and other|VT_RESERVED: If the BSTR is a number a numeric
2705 * comparison will take place else the BSTR is always greater.
2706 * - BSTR|VT_RESERVED and other|VT_RESERVED: It seems that the other
2707 * variant is ignored and the return value depends only on the sign
2708 * of the BSTR if it is a number else the BSTR is always greater. A
2709 * positive BSTR is greater, a negative one is smaller than the other
2713 * VarBstrCmp for the lcid and flags usage.
2715 HRESULT WINAPI
VarCmp(LPVARIANT left
, LPVARIANT right
, LCID lcid
, DWORD flags
)
2717 VARTYPE lvt
, rvt
, vt
;
2722 TRACE("(%s,%s,0x%08x,0x%08x)\n", debugstr_variant(left
), debugstr_variant(right
), lcid
, flags
);
2724 lvt
= V_VT(left
) & VT_TYPEMASK
;
2725 rvt
= V_VT(right
) & VT_TYPEMASK
;
2726 xmask
= (1 << lvt
) | (1 << rvt
);
2728 /* If we have any flag set except VT_RESERVED bail out.
2729 Same for the left input variant type > VT_INT and for the
2730 right input variant type > VT_I8. Yes, VT_INT is only supported
2731 as left variant. Go figure */
2732 if (((V_VT(left
) | V_VT(right
)) & ~VT_TYPEMASK
& ~VT_RESERVED
) ||
2733 lvt
> VT_INT
|| rvt
> VT_I8
) {
2734 return DISP_E_BADVARTYPE
;
2737 /* Don't ask me why but native VarCmp cannot handle: VT_I1, VT_UI2, VT_UI4,
2738 VT_UINT and VT_UI8. Tested with DCOM98, Win2k, WinXP */
2739 if (rvt
== VT_INT
|| xmask
& (VTBIT_I1
| VTBIT_UI2
| VTBIT_UI4
| VTBIT_UI8
|
2740 VTBIT_DISPATCH
| VTBIT_VARIANT
| VTBIT_UNKNOWN
| VTBIT_15
))
2741 return DISP_E_TYPEMISMATCH
;
2743 /* If both variants are VT_ERROR return VARCMP_EQ */
2744 if (xmask
== VTBIT_ERROR
)
2746 else if (xmask
& VTBIT_ERROR
)
2747 return DISP_E_TYPEMISMATCH
;
2749 if (xmask
& VTBIT_NULL
)
2755 /* Two BSTRs, ignore VT_RESERVED */
2756 if (xmask
== VTBIT_BSTR
)
2757 return VarBstrCmp(V_BSTR(left
), V_BSTR(right
), lcid
, flags
);
2759 /* A BSTR and another variant; we have to take care of VT_RESERVED */
2760 if (xmask
& VTBIT_BSTR
) {
2761 VARIANT
*bstrv
, *nonbv
;
2765 /* Swap the variants so the BSTR is always on the left */
2766 if (lvt
== VT_BSTR
) {
2777 /* BSTR and EMPTY: ignore VT_RESERVED */
2778 if (nonbvt
== VT_EMPTY
)
2779 rc
= (!V_BSTR(bstrv
) || !*V_BSTR(bstrv
)) ? VARCMP_EQ
: VARCMP_GT
;
2781 VARTYPE breserv
= V_VT(bstrv
) & ~VT_TYPEMASK
;
2782 VARTYPE nreserv
= V_VT(nonbv
) & ~VT_TYPEMASK
;
2784 if (!breserv
&& !nreserv
)
2785 /* No VT_RESERVED set ==> BSTR always greater */
2787 else if (breserv
&& !nreserv
) {
2788 /* BSTR has VT_RESERVED set. Do a string comparison */
2789 rc
= VariantChangeTypeEx(&rv
,nonbv
,lcid
,0,VT_BSTR
);
2792 rc
= VarBstrCmp(V_BSTR(bstrv
), V_BSTR(&rv
), lcid
, flags
);
2794 } else if (V_BSTR(bstrv
) && *V_BSTR(bstrv
)) {
2795 /* Non NULL nor empty BSTR */
2796 /* If the BSTR is not a number the BSTR is greater */
2797 rc
= _VarChangeTypeExWrap(&lv
,bstrv
,lcid
,0,VT_R8
);
2800 else if (breserv
&& nreserv
)
2801 /* FIXME: This is strange: with both VT_RESERVED set it
2802 looks like the result depends only on the sign of
2804 rc
= (V_R8(&lv
) >= 0) ? VARCMP_GT
: VARCMP_LT
;
2806 /* Numeric comparison, will be handled below.
2807 VARCMP_NULL used only to break out. */
2812 /* Empty or NULL BSTR */
2815 /* Fixup the return code if we swapped left and right */
2817 if (rc
== VARCMP_GT
)
2819 else if (rc
== VARCMP_LT
)
2822 if (rc
!= VARCMP_NULL
)
2826 if (xmask
& VTBIT_DECIMAL
)
2828 else if (xmask
& VTBIT_BSTR
)
2830 else if (xmask
& VTBIT_R4
)
2832 else if (xmask
& (VTBIT_R8
| VTBIT_DATE
))
2834 else if (xmask
& VTBIT_CY
)
2840 /* Coerce the variants */
2841 rc
= _VarChangeTypeExWrap(&lv
,left
,lcid
,0,vt
);
2842 if (rc
== DISP_E_OVERFLOW
&& vt
!= VT_R8
) {
2843 /* Overflow, change to R8 */
2845 rc
= _VarChangeTypeExWrap(&lv
,left
,lcid
,0,vt
);
2849 rc
= _VarChangeTypeExWrap(&rv
,right
,lcid
,0,vt
);
2850 if (rc
== DISP_E_OVERFLOW
&& vt
!= VT_R8
) {
2851 /* Overflow, change to R8 */
2853 rc
= _VarChangeTypeExWrap(&lv
,left
,lcid
,0,vt
);
2856 rc
= _VarChangeTypeExWrap(&rv
,right
,lcid
,0,vt
);
2861 #define _VARCMP(a,b) \
2862 (((a) == (b)) ? VARCMP_EQ : (((a) < (b)) ? VARCMP_LT : VARCMP_GT))
2866 return VarCyCmp(V_CY(&lv
), V_CY(&rv
));
2868 return VarDecCmp(&V_DECIMAL(&lv
), &V_DECIMAL(&rv
));
2870 return _VARCMP(V_I8(&lv
), V_I8(&rv
));
2872 return _VARCMP(V_R4(&lv
), V_R4(&rv
));
2874 return _VARCMP(V_R8(&lv
), V_R8(&rv
));
2876 /* We should never get here */
2882 /**********************************************************************
2883 * VarAnd [OLEAUT32.142]
2885 * Computes the logical AND of two variants.
2888 * left [I] First variant
2889 * right [I] Second variant
2890 * result [O] Result variant
2894 * Failure: An HRESULT error code indicating the error.
2896 HRESULT WINAPI
VarAnd(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
2898 HRESULT hres
= S_OK
;
2899 VARTYPE resvt
= VT_EMPTY
;
2900 VARTYPE leftvt
,rightvt
;
2901 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
2902 VARIANT varLeft
, varRight
;
2903 VARIANT tempLeft
, tempRight
;
2905 VariantInit(&varLeft
);
2906 VariantInit(&varRight
);
2907 VariantInit(&tempLeft
);
2908 VariantInit(&tempRight
);
2910 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
2912 /* Handle VT_DISPATCH by storing and taking address of returned value */
2913 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
2915 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
2916 if (FAILED(hres
)) goto VarAnd_Exit
;
2919 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
2921 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
2922 if (FAILED(hres
)) goto VarAnd_Exit
;
2926 leftvt
= V_VT(left
)&VT_TYPEMASK
;
2927 rightvt
= V_VT(right
)&VT_TYPEMASK
;
2928 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
2929 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
2931 if (leftExtraFlags
!= rightExtraFlags
)
2933 hres
= DISP_E_BADVARTYPE
;
2936 ExtraFlags
= leftExtraFlags
;
2938 /* Native VarAnd always returns an error when using extra
2939 * flags or if the variant combination is I8 and INT.
2941 if ((leftvt
== VT_I8
&& rightvt
== VT_INT
) ||
2942 (leftvt
== VT_INT
&& rightvt
== VT_I8
) ||
2945 hres
= DISP_E_BADVARTYPE
;
2949 /* Determine return type */
2950 else if (leftvt
== VT_I8
|| rightvt
== VT_I8
)
2952 else if (leftvt
== VT_I4
|| rightvt
== VT_I4
||
2953 leftvt
== VT_UINT
|| rightvt
== VT_UINT
||
2954 leftvt
== VT_INT
|| rightvt
== VT_INT
||
2955 leftvt
== VT_R4
|| rightvt
== VT_R4
||
2956 leftvt
== VT_R8
|| rightvt
== VT_R8
||
2957 leftvt
== VT_CY
|| rightvt
== VT_CY
||
2958 leftvt
== VT_DATE
|| rightvt
== VT_DATE
||
2959 leftvt
== VT_I1
|| rightvt
== VT_I1
||
2960 leftvt
== VT_UI2
|| rightvt
== VT_UI2
||
2961 leftvt
== VT_UI4
|| rightvt
== VT_UI4
||
2962 leftvt
== VT_UI8
|| rightvt
== VT_UI8
||
2963 leftvt
== VT_DECIMAL
|| rightvt
== VT_DECIMAL
)
2965 else if (leftvt
== VT_UI1
|| rightvt
== VT_UI1
||
2966 leftvt
== VT_I2
|| rightvt
== VT_I2
||
2967 leftvt
== VT_EMPTY
|| rightvt
== VT_EMPTY
)
2968 if ((leftvt
== VT_NULL
&& rightvt
== VT_UI1
) ||
2969 (leftvt
== VT_UI1
&& rightvt
== VT_NULL
) ||
2970 (leftvt
== VT_UI1
&& rightvt
== VT_UI1
))
2974 else if (leftvt
== VT_BOOL
|| rightvt
== VT_BOOL
||
2975 (leftvt
== VT_BSTR
&& rightvt
== VT_BSTR
))
2977 else if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
||
2978 leftvt
== VT_BSTR
|| rightvt
== VT_BSTR
)
2982 hres
= DISP_E_BADVARTYPE
;
2986 if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
)
2989 * Special cases for when left variant is VT_NULL
2990 * (VT_NULL & 0 = VT_NULL, VT_NULL & value = value)
2992 if (leftvt
== VT_NULL
)
2997 case VT_I1
: if (V_I1(right
)) resvt
= VT_NULL
; break;
2998 case VT_UI1
: if (V_UI1(right
)) resvt
= VT_NULL
; break;
2999 case VT_I2
: if (V_I2(right
)) resvt
= VT_NULL
; break;
3000 case VT_UI2
: if (V_UI2(right
)) resvt
= VT_NULL
; break;
3001 case VT_I4
: if (V_I4(right
)) resvt
= VT_NULL
; break;
3002 case VT_UI4
: if (V_UI4(right
)) resvt
= VT_NULL
; break;
3003 case VT_I8
: if (V_I8(right
)) resvt
= VT_NULL
; break;
3004 case VT_UI8
: if (V_UI8(right
)) resvt
= VT_NULL
; break;
3005 case VT_INT
: if (V_INT(right
)) resvt
= VT_NULL
; break;
3006 case VT_UINT
: if (V_UINT(right
)) resvt
= VT_NULL
; break;
3007 case VT_BOOL
: if (V_BOOL(right
)) resvt
= VT_NULL
; break;
3008 case VT_R4
: if (V_R4(right
)) resvt
= VT_NULL
; break;
3009 case VT_R8
: if (V_R8(right
)) resvt
= VT_NULL
; break;
3011 if(V_CY(right
).int64
)
3015 if (DEC_HI32(&V_DECIMAL(right
)) ||
3016 DEC_LO64(&V_DECIMAL(right
)))
3020 hres
= VarBoolFromStr(V_BSTR(right
),
3021 LOCALE_USER_DEFAULT
, VAR_LOCALBOOL
, &b
);
3025 V_VT(result
) = VT_NULL
;
3028 V_VT(result
) = VT_BOOL
;
3034 V_VT(result
) = resvt
;
3038 hres
= VariantCopy(&varLeft
, left
);
3039 if (FAILED(hres
)) goto VarAnd_Exit
;
3041 hres
= VariantCopy(&varRight
, right
);
3042 if (FAILED(hres
)) goto VarAnd_Exit
;
3044 if (resvt
== VT_I4
&& V_VT(&varLeft
) == VT_UI4
)
3045 V_VT(&varLeft
) = VT_I4
; /* Don't overflow */
3050 if (V_VT(&varLeft
) == VT_BSTR
&&
3051 FAILED(VarR8FromStr(V_BSTR(&varLeft
),
3052 LOCALE_USER_DEFAULT
, 0, &d
)))
3053 hres
= VariantChangeType(&varLeft
,&varLeft
,
3054 VARIANT_LOCALBOOL
, VT_BOOL
);
3055 if (SUCCEEDED(hres
) && V_VT(&varLeft
) != resvt
)
3056 hres
= VariantChangeType(&varLeft
,&varLeft
,0,resvt
);
3057 if (FAILED(hres
)) goto VarAnd_Exit
;
3060 if (resvt
== VT_I4
&& V_VT(&varRight
) == VT_UI4
)
3061 V_VT(&varRight
) = VT_I4
; /* Don't overflow */
3066 if (V_VT(&varRight
) == VT_BSTR
&&
3067 FAILED(VarR8FromStr(V_BSTR(&varRight
),
3068 LOCALE_USER_DEFAULT
, 0, &d
)))
3069 hres
= VariantChangeType(&varRight
, &varRight
,
3070 VARIANT_LOCALBOOL
, VT_BOOL
);
3071 if (SUCCEEDED(hres
) && V_VT(&varRight
) != resvt
)
3072 hres
= VariantChangeType(&varRight
, &varRight
, 0, resvt
);
3073 if (FAILED(hres
)) goto VarAnd_Exit
;
3076 V_VT(result
) = resvt
;
3080 V_I8(result
) = V_I8(&varLeft
) & V_I8(&varRight
);
3083 V_I4(result
) = V_I4(&varLeft
) & V_I4(&varRight
);
3086 V_I2(result
) = V_I2(&varLeft
) & V_I2(&varRight
);
3089 V_UI1(result
) = V_UI1(&varLeft
) & V_UI1(&varRight
);
3092 V_BOOL(result
) = V_BOOL(&varLeft
) & V_BOOL(&varRight
);
3095 FIXME("Couldn't bitwise AND variant types %d,%d\n",
3100 VariantClear(&varLeft
);
3101 VariantClear(&varRight
);
3102 VariantClear(&tempLeft
);
3103 VariantClear(&tempRight
);
3108 /**********************************************************************
3109 * VarAdd [OLEAUT32.141]
3114 * left [I] First variant
3115 * right [I] Second variant
3116 * result [O] Result variant
3120 * Failure: An HRESULT error code indicating the error.
3123 * Native VarAdd up to and including WinXP doesn't like I1, UI2, UI4,
3124 * UI8, INT and UINT as input variants.
3126 * Native VarAdd doesn't check for NULL in/out pointers and crashes. We do the
3130 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3133 HRESULT WINAPI
VarAdd(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3136 VARTYPE lvt
, rvt
, resvt
, tvt
;
3138 VARIANT tempLeft
, tempRight
;
3141 /* Variant priority for coercion. Sorted from lowest to highest.
3142 VT_ERROR shows an invalid input variant type. */
3143 enum coerceprio
{ vt_EMPTY
, vt_UI1
, vt_I2
, vt_I4
, vt_I8
, vt_BSTR
,vt_R4
,
3144 vt_R8
, vt_CY
, vt_DATE
, vt_DECIMAL
, vt_DISPATCH
, vt_NULL
,
3146 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3147 static const VARTYPE prio2vt
[] = { VT_EMPTY
, VT_UI1
, VT_I2
, VT_I4
, VT_I8
, VT_BSTR
, VT_R4
,
3148 VT_R8
, VT_CY
, VT_DATE
, VT_DECIMAL
, VT_DISPATCH
,
3149 VT_NULL
, VT_ERROR
};
3151 /* Mapping for coercion from input variant to priority of result variant. */
3152 static const VARTYPE coerce
[] = {
3153 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3154 vt_EMPTY
, vt_NULL
, vt_I2
, vt_I4
, vt_R4
,
3155 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3156 vt_R8
, vt_CY
, vt_DATE
, vt_BSTR
, vt_DISPATCH
,
3157 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3158 vt_ERROR
, vt_I2
, vt_ERROR
, vt_ERROR
, vt_DECIMAL
,
3159 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3160 vt_ERROR
, vt_ERROR
, vt_UI1
, vt_ERROR
, vt_ERROR
, vt_I8
3163 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
3168 VariantInit(&tempLeft
);
3169 VariantInit(&tempRight
);
3171 /* Handle VT_DISPATCH by storing and taking address of returned value */
3172 if ((V_VT(left
) & VT_TYPEMASK
) != VT_NULL
&& (V_VT(right
) & VT_TYPEMASK
) != VT_NULL
)
3174 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
3176 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
3177 if (FAILED(hres
)) goto end
;
3180 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
3182 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
3183 if (FAILED(hres
)) goto end
;
3188 lvt
= V_VT(left
)&VT_TYPEMASK
;
3189 rvt
= V_VT(right
)&VT_TYPEMASK
;
3191 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3192 Same for any input variant type > VT_I8 */
3193 if (V_VT(left
) & ~VT_TYPEMASK
|| V_VT(right
) & ~VT_TYPEMASK
||
3194 lvt
> VT_I8
|| rvt
> VT_I8
) {
3195 hres
= DISP_E_BADVARTYPE
;
3199 /* Determine the variant type to coerce to. */
3200 if (coerce
[lvt
] > coerce
[rvt
]) {
3201 resvt
= prio2vt
[coerce
[lvt
]];
3202 tvt
= prio2vt
[coerce
[rvt
]];
3204 resvt
= prio2vt
[coerce
[rvt
]];
3205 tvt
= prio2vt
[coerce
[lvt
]];
3208 /* Special cases where the result variant type is defined by both
3209 input variants and not only that with the highest priority */
3210 if (resvt
== VT_BSTR
) {
3211 if (tvt
== VT_EMPTY
|| tvt
== VT_BSTR
)
3216 if (resvt
== VT_R4
&& (tvt
== VT_BSTR
|| tvt
== VT_I8
|| tvt
== VT_I4
))
3219 /* For overflow detection use the biggest compatible type for the
3223 hres
= DISP_E_BADVARTYPE
;
3227 V_VT(result
) = VT_NULL
;
3230 FIXME("cannot handle variant type VT_DISPATCH\n");
3231 hres
= DISP_E_TYPEMISMATCH
;
3250 /* Now coerce the variants */
3251 hres
= VariantChangeType(&lv
, left
, 0, tvt
);
3254 hres
= VariantChangeType(&rv
, right
, 0, tvt
);
3260 V_VT(result
) = resvt
;
3263 hres
= VarDecAdd(&V_DECIMAL(&lv
), &V_DECIMAL(&rv
),
3264 &V_DECIMAL(result
));
3267 hres
= VarCyAdd(V_CY(&lv
), V_CY(&rv
), &V_CY(result
));
3270 /* We do not add those, we concatenate them. */
3271 hres
= VarBstrCat(V_BSTR(&lv
), V_BSTR(&rv
), &V_BSTR(result
));
3274 /* Overflow detection */
3275 r8res
= (double)V_I8(&lv
) + (double)V_I8(&rv
);
3276 if (r8res
> (double)I8_MAX
|| r8res
< (double)I8_MIN
) {
3277 V_VT(result
) = VT_R8
;
3278 V_R8(result
) = r8res
;
3282 V_I8(&tv
) = V_I8(&lv
) + V_I8(&rv
);
3287 /* FIXME: overflow detection */
3288 V_R8(&tv
) = V_R8(&lv
) + V_R8(&rv
);
3291 ERR("We shouldn't get here! tvt = %d!\n", tvt
);
3295 if ((hres
= VariantChangeType(result
, &tv
, 0, resvt
)) != S_OK
) {
3296 /* Overflow! Change to the vartype with the next higher priority.
3297 With one exception: I4 ==> R8 even if it would fit in I8 */
3301 resvt
= prio2vt
[coerce
[resvt
] + 1];
3302 hres
= VariantChangeType(result
, &tv
, 0, resvt
);
3305 hres
= VariantCopy(result
, &tv
);
3309 V_VT(result
) = VT_EMPTY
;
3310 V_I4(result
) = 0; /* No V_EMPTY */
3315 VariantClear(&tempLeft
);
3316 VariantClear(&tempRight
);
3317 TRACE("returning 0x%8x %s\n", hres
, debugstr_variant(result
));
3321 /**********************************************************************
3322 * VarMul [OLEAUT32.156]
3324 * Multiply two variants.
3327 * left [I] First variant
3328 * right [I] Second variant
3329 * result [O] Result variant
3333 * Failure: An HRESULT error code indicating the error.
3336 * Native VarMul up to and including WinXP doesn't like I1, UI2, UI4,
3337 * UI8, INT and UINT as input variants. But it can multiply apples with oranges.
3339 * Native VarMul doesn't check for NULL in/out pointers and crashes. We do the
3343 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3346 HRESULT WINAPI
VarMul(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3349 VARTYPE lvt
, rvt
, resvt
, tvt
;
3351 VARIANT tempLeft
, tempRight
;
3354 /* Variant priority for coercion. Sorted from lowest to highest.
3355 VT_ERROR shows an invalid input variant type. */
3356 enum coerceprio
{ vt_UI1
= 0, vt_I2
, vt_I4
, vt_I8
, vt_CY
, vt_R4
, vt_R8
,
3357 vt_DECIMAL
, vt_NULL
, vt_ERROR
};
3358 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3359 static const VARTYPE prio2vt
[] = { VT_UI1
, VT_I2
, VT_I4
, VT_I8
, VT_CY
, VT_R4
, VT_R8
,
3360 VT_DECIMAL
, VT_NULL
, VT_ERROR
};
3362 /* Mapping for coercion from input variant to priority of result variant. */
3363 static const VARTYPE coerce
[] = {
3364 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3365 vt_UI1
, vt_NULL
, vt_I2
, vt_I4
, vt_R4
,
3366 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3367 vt_R8
, vt_CY
, vt_R8
, vt_R8
, vt_ERROR
,
3368 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3369 vt_ERROR
, vt_I2
, vt_ERROR
, vt_ERROR
, vt_DECIMAL
,
3370 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3371 vt_ERROR
, vt_ERROR
, vt_UI1
, vt_ERROR
, vt_ERROR
, vt_I8
3374 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
3379 VariantInit(&tempLeft
);
3380 VariantInit(&tempRight
);
3382 /* Handle VT_DISPATCH by storing and taking address of returned value */
3383 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
3385 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
3386 if (FAILED(hres
)) goto end
;
3389 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
3391 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
3392 if (FAILED(hres
)) goto end
;
3396 lvt
= V_VT(left
)&VT_TYPEMASK
;
3397 rvt
= V_VT(right
)&VT_TYPEMASK
;
3399 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3400 Same for any input variant type > VT_I8 */
3401 if (V_VT(left
) & ~VT_TYPEMASK
|| V_VT(right
) & ~VT_TYPEMASK
||
3402 lvt
> VT_I8
|| rvt
> VT_I8
) {
3403 hres
= DISP_E_BADVARTYPE
;
3407 /* Determine the variant type to coerce to. */
3408 if (coerce
[lvt
] > coerce
[rvt
]) {
3409 resvt
= prio2vt
[coerce
[lvt
]];
3410 tvt
= prio2vt
[coerce
[rvt
]];
3412 resvt
= prio2vt
[coerce
[rvt
]];
3413 tvt
= prio2vt
[coerce
[lvt
]];
3416 /* Special cases where the result variant type is defined by both
3417 input variants and not only that with the highest priority */
3418 if (resvt
== VT_R4
&& (tvt
== VT_CY
|| tvt
== VT_I8
|| tvt
== VT_I4
))
3420 if (lvt
== VT_EMPTY
&& rvt
== VT_EMPTY
)
3423 /* For overflow detection use the biggest compatible type for the
3427 hres
= DISP_E_BADVARTYPE
;
3431 V_VT(result
) = VT_NULL
;
3446 /* Now coerce the variants */
3447 hres
= VariantChangeType(&lv
, left
, 0, tvt
);
3450 hres
= VariantChangeType(&rv
, right
, 0, tvt
);
3457 V_VT(result
) = resvt
;
3460 hres
= VarDecMul(&V_DECIMAL(&lv
), &V_DECIMAL(&rv
),
3461 &V_DECIMAL(result
));
3464 hres
= VarCyMul(V_CY(&lv
), V_CY(&rv
), &V_CY(result
));
3467 /* Overflow detection */
3468 r8res
= (double)V_I8(&lv
) * (double)V_I8(&rv
);
3469 if (r8res
> (double)I8_MAX
|| r8res
< (double)I8_MIN
) {
3470 V_VT(result
) = VT_R8
;
3471 V_R8(result
) = r8res
;
3474 V_I8(&tv
) = V_I8(&lv
) * V_I8(&rv
);
3477 /* FIXME: overflow detection */
3478 V_R8(&tv
) = V_R8(&lv
) * V_R8(&rv
);
3481 ERR("We shouldn't get here! tvt = %d!\n", tvt
);
3485 while ((hres
= VariantChangeType(result
, &tv
, 0, resvt
)) != S_OK
) {
3486 /* Overflow! Change to the vartype with the next higher priority.
3487 With one exception: I4 ==> R8 even if it would fit in I8 */
3491 resvt
= prio2vt
[coerce
[resvt
] + 1];
3494 hres
= VariantCopy(result
, &tv
);
3498 V_VT(result
) = VT_EMPTY
;
3499 V_I4(result
) = 0; /* No V_EMPTY */
3504 VariantClear(&tempLeft
);
3505 VariantClear(&tempRight
);
3506 TRACE("returning 0x%8x %s\n", hres
, debugstr_variant(result
));
3510 /**********************************************************************
3511 * VarDiv [OLEAUT32.143]
3513 * Divides one variant with another.
3516 * left [I] First variant
3517 * right [I] Second variant
3518 * result [O] Result variant
3522 * Failure: An HRESULT error code indicating the error.
3524 HRESULT WINAPI
VarDiv(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3526 HRESULT hres
= S_OK
;
3527 VARTYPE resvt
= VT_EMPTY
;
3528 VARTYPE leftvt
,rightvt
;
3529 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
3531 VARIANT tempLeft
, tempRight
;
3533 VariantInit(&tempLeft
);
3534 VariantInit(&tempRight
);
3538 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
3540 /* Handle VT_DISPATCH by storing and taking address of returned value */
3541 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
3543 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
3544 if (FAILED(hres
)) goto end
;
3547 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
3549 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
3550 if (FAILED(hres
)) goto end
;
3554 leftvt
= V_VT(left
)&VT_TYPEMASK
;
3555 rightvt
= V_VT(right
)&VT_TYPEMASK
;
3556 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
3557 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
3559 if (leftExtraFlags
!= rightExtraFlags
)
3561 hres
= DISP_E_BADVARTYPE
;
3564 ExtraFlags
= leftExtraFlags
;
3566 /* Native VarDiv always returns an error when using extra flags */
3567 if (ExtraFlags
!= 0)
3569 hres
= DISP_E_BADVARTYPE
;
3573 /* Determine return type */
3574 if (rightvt
!= VT_EMPTY
)
3576 if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
)
3578 V_VT(result
) = VT_NULL
;
3582 else if (leftvt
== VT_DECIMAL
|| rightvt
== VT_DECIMAL
)
3584 else if (leftvt
== VT_I8
|| rightvt
== VT_I8
||
3585 leftvt
== VT_CY
|| rightvt
== VT_CY
||
3586 leftvt
== VT_DATE
|| rightvt
== VT_DATE
||
3587 leftvt
== VT_I4
|| rightvt
== VT_I4
||
3588 leftvt
== VT_BSTR
|| rightvt
== VT_BSTR
||
3589 leftvt
== VT_I2
|| rightvt
== VT_I2
||
3590 leftvt
== VT_BOOL
|| rightvt
== VT_BOOL
||
3591 leftvt
== VT_R8
|| rightvt
== VT_R8
||
3592 leftvt
== VT_UI1
|| rightvt
== VT_UI1
)
3594 if ((leftvt
== VT_UI1
&& rightvt
== VT_R4
) ||
3595 (leftvt
== VT_R4
&& rightvt
== VT_UI1
))
3597 else if ((leftvt
== VT_R4
&& (rightvt
== VT_BOOL
||
3598 rightvt
== VT_I2
)) || (rightvt
== VT_R4
&&
3599 (leftvt
== VT_BOOL
|| leftvt
== VT_I2
)))
3604 else if (leftvt
== VT_R4
|| rightvt
== VT_R4
)
3607 else if (leftvt
== VT_NULL
)
3609 V_VT(result
) = VT_NULL
;
3615 hres
= DISP_E_BADVARTYPE
;
3619 /* coerce to the result type */
3620 hres
= VariantChangeType(&lv
, left
, 0, resvt
);
3621 if (hres
!= S_OK
) goto end
;
3623 hres
= VariantChangeType(&rv
, right
, 0, resvt
);
3624 if (hres
!= S_OK
) goto end
;
3627 V_VT(result
) = resvt
;
3631 if (V_R4(&lv
) == 0.0 && V_R4(&rv
) == 0.0)
3633 hres
= DISP_E_OVERFLOW
;
3634 V_VT(result
) = VT_EMPTY
;
3636 else if (V_R4(&rv
) == 0.0)
3638 hres
= DISP_E_DIVBYZERO
;
3639 V_VT(result
) = VT_EMPTY
;
3642 V_R4(result
) = V_R4(&lv
) / V_R4(&rv
);
3645 if (V_R8(&lv
) == 0.0 && V_R8(&rv
) == 0.0)
3647 hres
= DISP_E_OVERFLOW
;
3648 V_VT(result
) = VT_EMPTY
;
3650 else if (V_R8(&rv
) == 0.0)
3652 hres
= DISP_E_DIVBYZERO
;
3653 V_VT(result
) = VT_EMPTY
;
3656 V_R8(result
) = V_R8(&lv
) / V_R8(&rv
);
3659 hres
= VarDecDiv(&(V_DECIMAL(&lv
)), &(V_DECIMAL(&rv
)), &(V_DECIMAL(result
)));
3666 VariantClear(&tempLeft
);
3667 VariantClear(&tempRight
);
3668 TRACE("returning 0x%8x %s\n", hres
, debugstr_variant(result
));
3672 /**********************************************************************
3673 * VarSub [OLEAUT32.159]
3675 * Subtract two variants.
3678 * left [I] First variant
3679 * right [I] Second variant
3680 * result [O] Result variant
3684 * Failure: An HRESULT error code indicating the error.
3686 HRESULT WINAPI
VarSub(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3688 HRESULT hres
= S_OK
;
3689 VARTYPE resvt
= VT_EMPTY
;
3690 VARTYPE leftvt
,rightvt
;
3691 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
3693 VARIANT tempLeft
, tempRight
;
3697 VariantInit(&tempLeft
);
3698 VariantInit(&tempRight
);
3700 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
3702 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
&&
3703 (V_VT(left
)&(~VT_TYPEMASK
)) == 0 &&
3704 (V_VT(right
) & VT_TYPEMASK
) != VT_NULL
)
3706 if (NULL
== V_DISPATCH(left
)) {
3707 if ((V_VT(right
) & VT_TYPEMASK
) >= VT_INT_PTR
)
3708 hres
= DISP_E_BADVARTYPE
;
3709 else if ((V_VT(right
) & VT_TYPEMASK
) >= VT_UI8
&&
3710 (V_VT(right
) & VT_TYPEMASK
) < VT_RECORD
)
3711 hres
= DISP_E_BADVARTYPE
;
3712 else switch (V_VT(right
) & VT_TYPEMASK
)
3720 hres
= DISP_E_BADVARTYPE
;
3722 if (FAILED(hres
)) goto end
;
3724 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
3725 if (FAILED(hres
)) goto end
;
3728 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
&&
3729 (V_VT(right
)&(~VT_TYPEMASK
)) == 0 &&
3730 (V_VT(left
) & VT_TYPEMASK
) != VT_NULL
)
3732 if (NULL
== V_DISPATCH(right
))
3734 if ((V_VT(left
) & VT_TYPEMASK
) >= VT_INT_PTR
)
3735 hres
= DISP_E_BADVARTYPE
;
3736 else if ((V_VT(left
) & VT_TYPEMASK
) >= VT_UI8
&&
3737 (V_VT(left
) & VT_TYPEMASK
) < VT_RECORD
)
3738 hres
= DISP_E_BADVARTYPE
;
3739 else switch (V_VT(left
) & VT_TYPEMASK
)
3747 hres
= DISP_E_BADVARTYPE
;
3749 if (FAILED(hres
)) goto end
;
3751 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
3752 if (FAILED(hres
)) goto end
;
3756 leftvt
= V_VT(left
)&VT_TYPEMASK
;
3757 rightvt
= V_VT(right
)&VT_TYPEMASK
;
3758 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
3759 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
3761 if (leftExtraFlags
!= rightExtraFlags
)
3763 hres
= DISP_E_BADVARTYPE
;
3766 ExtraFlags
= leftExtraFlags
;
3768 /* determine return type and return code */
3769 /* All extra flags produce errors */
3770 if (ExtraFlags
== (VT_VECTOR
|VT_BYREF
|VT_RESERVED
) ||
3771 ExtraFlags
== (VT_VECTOR
|VT_RESERVED
) ||
3772 ExtraFlags
== (VT_VECTOR
|VT_BYREF
) ||
3773 ExtraFlags
== (VT_BYREF
|VT_RESERVED
) ||
3774 ExtraFlags
== VT_VECTOR
||
3775 ExtraFlags
== VT_BYREF
||
3776 ExtraFlags
== VT_RESERVED
)
3778 hres
= DISP_E_BADVARTYPE
;
3781 else if (ExtraFlags
>= VT_ARRAY
)
3783 hres
= DISP_E_TYPEMISMATCH
;
3786 /* Native VarSub cannot handle: VT_I1, VT_UI2, VT_UI4,
3787 VT_INT, VT_UINT and VT_UI8. Tested with WinXP */
3788 else if (leftvt
== VT_CLSID
|| rightvt
== VT_CLSID
||
3789 leftvt
== VT_VARIANT
|| rightvt
== VT_VARIANT
||
3790 leftvt
== VT_I1
|| rightvt
== VT_I1
||
3791 leftvt
== VT_UI2
|| rightvt
== VT_UI2
||
3792 leftvt
== VT_UI4
|| rightvt
== VT_UI4
||
3793 leftvt
== VT_UI8
|| rightvt
== VT_UI8
||
3794 leftvt
== VT_INT
|| rightvt
== VT_INT
||
3795 leftvt
== VT_UINT
|| rightvt
== VT_UINT
||
3796 leftvt
== VT_UNKNOWN
|| rightvt
== VT_UNKNOWN
||
3797 leftvt
== VT_RECORD
|| rightvt
== VT_RECORD
)
3799 if (leftvt
== VT_RECORD
&& rightvt
== VT_I8
)
3800 hres
= DISP_E_TYPEMISMATCH
;
3801 else if (leftvt
< VT_UI1
&& rightvt
== VT_RECORD
)
3802 hres
= DISP_E_TYPEMISMATCH
;
3803 else if (leftvt
>= VT_UI1
&& rightvt
== VT_RECORD
)
3804 hres
= DISP_E_TYPEMISMATCH
;
3805 else if (leftvt
== VT_RECORD
&& rightvt
<= VT_UI1
)
3806 hres
= DISP_E_TYPEMISMATCH
;
3807 else if (leftvt
== VT_RECORD
&& rightvt
> VT_UI1
)
3808 hres
= DISP_E_BADVARTYPE
;
3810 hres
= DISP_E_BADVARTYPE
;
3813 /* The following flags/types are invalid for left variant */
3814 else if (!((leftvt
<= VT_LPWSTR
|| leftvt
== VT_RECORD
||
3815 leftvt
== VT_CLSID
) && leftvt
!= (VARTYPE
)15 /* undefined vt */ &&
3816 (leftvt
< VT_VOID
|| leftvt
> VT_LPWSTR
)))
3818 hres
= DISP_E_BADVARTYPE
;
3821 /* The following flags/types are invalid for right variant */
3822 else if (!((rightvt
<= VT_LPWSTR
|| rightvt
== VT_RECORD
||
3823 rightvt
== VT_CLSID
) && rightvt
!= (VARTYPE
)15 /* undefined vt */ &&
3824 (rightvt
< VT_VOID
|| rightvt
> VT_LPWSTR
)))
3826 hres
= DISP_E_BADVARTYPE
;
3829 else if ((leftvt
== VT_NULL
&& rightvt
== VT_DISPATCH
) ||
3830 (leftvt
== VT_DISPATCH
&& rightvt
== VT_NULL
))
3832 else if (leftvt
== VT_DISPATCH
|| rightvt
== VT_DISPATCH
||
3833 leftvt
== VT_ERROR
|| rightvt
== VT_ERROR
)
3835 hres
= DISP_E_TYPEMISMATCH
;
3838 else if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
)
3840 else if ((leftvt
== VT_EMPTY
&& rightvt
== VT_BSTR
) ||
3841 (leftvt
== VT_DATE
&& rightvt
== VT_DATE
) ||
3842 (leftvt
== VT_BSTR
&& rightvt
== VT_EMPTY
) ||
3843 (leftvt
== VT_BSTR
&& rightvt
== VT_BSTR
))
3845 else if (leftvt
== VT_DECIMAL
|| rightvt
== VT_DECIMAL
)
3847 else if (leftvt
== VT_DATE
|| rightvt
== VT_DATE
)
3849 else if (leftvt
== VT_CY
|| rightvt
== VT_CY
)
3851 else if (leftvt
== VT_R8
|| rightvt
== VT_R8
)
3853 else if (leftvt
== VT_BSTR
|| rightvt
== VT_BSTR
)
3855 else if (leftvt
== VT_R4
|| rightvt
== VT_R4
)
3857 if (leftvt
== VT_I4
|| rightvt
== VT_I4
||
3858 leftvt
== VT_I8
|| rightvt
== VT_I8
)
3863 else if (leftvt
== VT_I8
|| rightvt
== VT_I8
)
3865 else if (leftvt
== VT_I4
|| rightvt
== VT_I4
)
3867 else if (leftvt
== VT_I2
|| rightvt
== VT_I2
||
3868 leftvt
== VT_BOOL
|| rightvt
== VT_BOOL
||
3869 (leftvt
== VT_EMPTY
&& rightvt
== VT_EMPTY
))
3871 else if (leftvt
== VT_UI1
|| rightvt
== VT_UI1
)
3875 hres
= DISP_E_TYPEMISMATCH
;
3879 /* coerce to the result type */
3880 if (leftvt
== VT_BSTR
&& rightvt
== VT_DATE
)
3881 hres
= VariantChangeType(&lv
, left
, 0, VT_R8
);
3883 hres
= VariantChangeType(&lv
, left
, 0, resvt
);
3884 if (hres
!= S_OK
) goto end
;
3885 if (leftvt
== VT_DATE
&& rightvt
== VT_BSTR
)
3886 hres
= VariantChangeType(&rv
, right
, 0, VT_R8
);
3888 hres
= VariantChangeType(&rv
, right
, 0, resvt
);
3889 if (hres
!= S_OK
) goto end
;
3892 V_VT(result
) = resvt
;
3898 V_DATE(result
) = V_DATE(&lv
) - V_DATE(&rv
);
3901 hres
= VarCySub(V_CY(&lv
), V_CY(&rv
), &(V_CY(result
)));
3904 V_R4(result
) = V_R4(&lv
) - V_R4(&rv
);
3907 V_I8(result
) = V_I8(&lv
) - V_I8(&rv
);
3910 V_I4(result
) = V_I4(&lv
) - V_I4(&rv
);
3913 V_I2(result
) = V_I2(&lv
) - V_I2(&rv
);
3916 V_UI1(result
) = V_UI2(&lv
) - V_UI1(&rv
);
3919 V_R8(result
) = V_R8(&lv
) - V_R8(&rv
);
3922 hres
= VarDecSub(&(V_DECIMAL(&lv
)), &(V_DECIMAL(&rv
)), &(V_DECIMAL(result
)));
3929 VariantClear(&tempLeft
);
3930 VariantClear(&tempRight
);
3931 TRACE("returning 0x%8x %s\n", hres
, debugstr_variant(result
));
3936 /**********************************************************************
3937 * VarOr [OLEAUT32.157]
3939 * Perform a logical or (OR) operation on two variants.
3942 * pVarLeft [I] First variant
3943 * pVarRight [I] Variant to OR with pVarLeft
3944 * pVarOut [O] Destination for OR result
3947 * Success: S_OK. pVarOut contains the result of the operation with its type
3948 * taken from the table listed under VarXor().
3949 * Failure: An HRESULT error code indicating the error.
3952 * See the Notes section of VarXor() for further information.
3954 HRESULT WINAPI
VarOr(LPVARIANT pVarLeft
, LPVARIANT pVarRight
, LPVARIANT pVarOut
)
3957 VARIANT varLeft
, varRight
, varStr
;
3959 VARIANT tempLeft
, tempRight
;
3961 VariantInit(&tempLeft
);
3962 VariantInit(&tempRight
);
3963 VariantInit(&varLeft
);
3964 VariantInit(&varRight
);
3965 VariantInit(&varStr
);
3967 TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft
), debugstr_variant(pVarRight
), pVarOut
);
3969 /* Handle VT_DISPATCH by storing and taking address of returned value */
3970 if ((V_VT(pVarLeft
) & VT_TYPEMASK
) == VT_DISPATCH
)
3972 hRet
= VARIANT_FetchDispatchValue(pVarLeft
, &tempLeft
);
3973 if (FAILED(hRet
)) goto VarOr_Exit
;
3974 pVarLeft
= &tempLeft
;
3976 if ((V_VT(pVarRight
) & VT_TYPEMASK
) == VT_DISPATCH
)
3978 hRet
= VARIANT_FetchDispatchValue(pVarRight
, &tempRight
);
3979 if (FAILED(hRet
)) goto VarOr_Exit
;
3980 pVarRight
= &tempRight
;
3983 if (V_EXTRA_TYPE(pVarLeft
) || V_EXTRA_TYPE(pVarRight
) ||
3984 V_VT(pVarLeft
) == VT_UNKNOWN
|| V_VT(pVarRight
) == VT_UNKNOWN
||
3985 V_VT(pVarLeft
) == VT_DISPATCH
|| V_VT(pVarRight
) == VT_DISPATCH
||
3986 V_VT(pVarLeft
) == VT_RECORD
|| V_VT(pVarRight
) == VT_RECORD
)
3988 hRet
= DISP_E_BADVARTYPE
;
3992 V_VT(&varLeft
) = V_VT(&varRight
) = V_VT(&varStr
) = VT_EMPTY
;
3994 if (V_VT(pVarLeft
) == VT_NULL
|| V_VT(pVarRight
) == VT_NULL
)
3996 /* NULL OR Zero is NULL, NULL OR value is value */
3997 if (V_VT(pVarLeft
) == VT_NULL
)
3998 pVarLeft
= pVarRight
; /* point to the non-NULL var */
4000 V_VT(pVarOut
) = VT_NULL
;
4003 switch (V_VT(pVarLeft
))
4005 case VT_DATE
: case VT_R8
:
4011 if (V_BOOL(pVarLeft
))
4012 *pVarOut
= *pVarLeft
;
4015 case VT_I2
: case VT_UI2
:
4026 if (V_UI1(pVarLeft
))
4027 *pVarOut
= *pVarLeft
;
4035 case VT_I4
: case VT_UI4
: case VT_INT
: case VT_UINT
:
4041 if (V_CY(pVarLeft
).int64
)
4045 case VT_I8
: case VT_UI8
:
4051 if (DEC_HI32(&V_DECIMAL(pVarLeft
)) || DEC_LO64(&V_DECIMAL(pVarLeft
)))
4059 if (!V_BSTR(pVarLeft
))
4061 hRet
= DISP_E_BADVARTYPE
;
4065 hRet
= VarBoolFromStr(V_BSTR(pVarLeft
), LOCALE_USER_DEFAULT
, VAR_LOCALBOOL
, &b
);
4066 if (SUCCEEDED(hRet
) && b
)
4068 V_VT(pVarOut
) = VT_BOOL
;
4069 V_BOOL(pVarOut
) = b
;
4073 case VT_NULL
: case VT_EMPTY
:
4074 V_VT(pVarOut
) = VT_NULL
;
4078 hRet
= DISP_E_BADVARTYPE
;
4083 if (V_VT(pVarLeft
) == VT_EMPTY
|| V_VT(pVarRight
) == VT_EMPTY
)
4085 if (V_VT(pVarLeft
) == VT_EMPTY
)
4086 pVarLeft
= pVarRight
; /* point to the non-EMPTY var */
4089 /* Since one argument is empty (0), OR'ing it with the other simply
4090 * gives the others value (as 0|x => x). So just convert the other
4091 * argument to the required result type.
4093 switch (V_VT(pVarLeft
))
4096 if (!V_BSTR(pVarLeft
))
4098 hRet
= DISP_E_BADVARTYPE
;
4102 hRet
= VariantCopy(&varStr
, pVarLeft
);
4106 hRet
= VariantChangeType(pVarLeft
, pVarLeft
, 0, VT_BOOL
);
4109 /* Fall Through ... */
4110 case VT_EMPTY
: case VT_UI1
: case VT_BOOL
: case VT_I2
:
4111 V_VT(pVarOut
) = VT_I2
;
4113 case VT_DATE
: case VT_CY
: case VT_DECIMAL
: case VT_R4
: case VT_R8
:
4114 case VT_I1
: case VT_UI2
: case VT_I4
: case VT_UI4
:
4115 case VT_INT
: case VT_UINT
: case VT_UI8
:
4116 V_VT(pVarOut
) = VT_I4
;
4119 V_VT(pVarOut
) = VT_I8
;
4122 hRet
= DISP_E_BADVARTYPE
;
4125 hRet
= VariantCopy(&varLeft
, pVarLeft
);
4128 pVarLeft
= &varLeft
;
4129 hRet
= VariantChangeType(pVarOut
, pVarLeft
, 0, V_VT(pVarOut
));
4133 if (V_VT(pVarLeft
) == VT_BOOL
&& V_VT(pVarRight
) == VT_BOOL
)
4135 V_VT(pVarOut
) = VT_BOOL
;
4136 V_BOOL(pVarOut
) = V_BOOL(pVarLeft
) | V_BOOL(pVarRight
);
4141 if (V_VT(pVarLeft
) == VT_UI1
&& V_VT(pVarRight
) == VT_UI1
)
4143 V_VT(pVarOut
) = VT_UI1
;
4144 V_UI1(pVarOut
) = V_UI1(pVarLeft
) | V_UI1(pVarRight
);
4149 if (V_VT(pVarLeft
) == VT_BSTR
)
4151 hRet
= VariantCopy(&varStr
, pVarLeft
);
4155 hRet
= VariantChangeType(pVarLeft
, pVarLeft
, 0, VT_BOOL
);
4160 if (V_VT(pVarLeft
) == VT_BOOL
&&
4161 (V_VT(pVarRight
) == VT_BOOL
|| V_VT(pVarRight
) == VT_BSTR
))
4165 else if ((V_VT(pVarLeft
) == VT_BOOL
|| V_VT(pVarLeft
) == VT_UI1
||
4166 V_VT(pVarLeft
) == VT_I2
|| V_VT(pVarLeft
) == VT_BSTR
) &&
4167 (V_VT(pVarRight
) == VT_BOOL
|| V_VT(pVarRight
) == VT_UI1
||
4168 V_VT(pVarRight
) == VT_I2
|| V_VT(pVarRight
) == VT_BSTR
))
4172 else if (V_VT(pVarLeft
) == VT_I8
|| V_VT(pVarRight
) == VT_I8
)
4174 if (V_VT(pVarLeft
) == VT_INT
|| V_VT(pVarRight
) == VT_INT
)
4176 hRet
= DISP_E_TYPEMISMATCH
;
4182 hRet
= VariantCopy(&varLeft
, pVarLeft
);
4186 hRet
= VariantCopy(&varRight
, pVarRight
);
4190 if (vt
== VT_I4
&& V_VT(&varLeft
) == VT_UI4
)
4191 V_VT(&varLeft
) = VT_I4
; /* Don't overflow */
4196 if (V_VT(&varLeft
) == VT_BSTR
&&
4197 FAILED(VarR8FromStr(V_BSTR(&varLeft
), LOCALE_USER_DEFAULT
, 0, &d
)))
4198 hRet
= VariantChangeType(&varLeft
, &varLeft
, VARIANT_LOCALBOOL
, VT_BOOL
);
4199 if (SUCCEEDED(hRet
) && V_VT(&varLeft
) != vt
)
4200 hRet
= VariantChangeType(&varLeft
, &varLeft
, 0, vt
);
4205 if (vt
== VT_I4
&& V_VT(&varRight
) == VT_UI4
)
4206 V_VT(&varRight
) = VT_I4
; /* Don't overflow */
4211 if (V_VT(&varRight
) == VT_BSTR
&&
4212 FAILED(VarR8FromStr(V_BSTR(&varRight
), LOCALE_USER_DEFAULT
, 0, &d
)))
4213 hRet
= VariantChangeType(&varRight
, &varRight
, VARIANT_LOCALBOOL
, VT_BOOL
);
4214 if (SUCCEEDED(hRet
) && V_VT(&varRight
) != vt
)
4215 hRet
= VariantChangeType(&varRight
, &varRight
, 0, vt
);
4223 V_I8(pVarOut
) = V_I8(&varLeft
) | V_I8(&varRight
);
4225 else if (vt
== VT_I4
)
4227 V_I4(pVarOut
) = V_I4(&varLeft
) | V_I4(&varRight
);
4231 V_I2(pVarOut
) = V_I2(&varLeft
) | V_I2(&varRight
);
4235 VariantClear(&varStr
);
4236 VariantClear(&varLeft
);
4237 VariantClear(&varRight
);
4238 VariantClear(&tempLeft
);
4239 VariantClear(&tempRight
);
4243 /**********************************************************************
4244 * VarAbs [OLEAUT32.168]
4246 * Convert a variant to its absolute value.
4249 * pVarIn [I] Source variant
4250 * pVarOut [O] Destination for converted value
4253 * Success: S_OK. pVarOut contains the absolute value of pVarIn.
4254 * Failure: An HRESULT error code indicating the error.
4257 * - This function does not process by-reference variants.
4258 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4259 * according to the following table:
4260 *| Input Type Output Type
4261 *| ---------- -----------
4264 *| (All others) Unchanged
4266 HRESULT WINAPI
VarAbs(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
4269 HRESULT hRet
= S_OK
;
4274 TRACE("(%s,%p)\n", debugstr_variant(pVarIn
), pVarOut
);
4276 /* Handle VT_DISPATCH by storing and taking address of returned value */
4277 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
4279 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
4280 if (FAILED(hRet
)) goto VarAbs_Exit
;
4284 if (V_ISARRAY(pVarIn
) || V_VT(pVarIn
) == VT_UNKNOWN
||
4285 V_VT(pVarIn
) == VT_DISPATCH
|| V_VT(pVarIn
) == VT_RECORD
||
4286 V_VT(pVarIn
) == VT_ERROR
)
4288 hRet
= DISP_E_TYPEMISMATCH
;
4291 *pVarOut
= *pVarIn
; /* Shallow copy the value, and invert it if needed */
4293 #define ABS_CASE(typ,min) \
4294 case VT_##typ: if (V_##typ(pVarIn) == min) hRet = DISP_E_OVERFLOW; \
4295 else if (V_##typ(pVarIn) < 0) V_##typ(pVarOut) = -V_##typ(pVarIn); \
4298 switch (V_VT(pVarIn
))
4300 ABS_CASE(I1
,I1_MIN
);
4302 V_VT(pVarOut
) = VT_I2
;
4303 /* BOOL->I2, Fall through ... */
4304 ABS_CASE(I2
,I2_MIN
);
4306 ABS_CASE(I4
,I4_MIN
);
4307 ABS_CASE(I8
,I8_MIN
);
4308 ABS_CASE(R4
,R4_MIN
);
4310 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(&varIn
));
4313 V_VT(pVarOut
) = VT_R8
;
4315 /* Fall through ... */
4317 ABS_CASE(R8
,R8_MIN
);
4319 hRet
= VarCyAbs(V_CY(pVarIn
), & V_CY(pVarOut
));
4322 DEC_SIGN(&V_DECIMAL(pVarOut
)) &= ~DECIMAL_NEG
;
4332 V_VT(pVarOut
) = VT_I2
;
4337 hRet
= DISP_E_BADVARTYPE
;
4341 VariantClear(&temp
);
4345 /**********************************************************************
4346 * VarFix [OLEAUT32.169]
4348 * Truncate a variants value to a whole number.
4351 * pVarIn [I] Source variant
4352 * pVarOut [O] Destination for converted value
4355 * Success: S_OK. pVarOut contains the converted value.
4356 * Failure: An HRESULT error code indicating the error.
4359 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4360 * according to the following table:
4361 *| Input Type Output Type
4362 *| ---------- -----------
4366 *| All Others Unchanged
4367 * - The difference between this function and VarInt() is that VarInt() rounds
4368 * negative numbers away from 0, while this function rounds them towards zero.
4370 HRESULT WINAPI
VarFix(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
4372 HRESULT hRet
= S_OK
;
4377 TRACE("(%s,%p)\n", debugstr_variant(pVarIn
), pVarOut
);
4379 /* Handle VT_DISPATCH by storing and taking address of returned value */
4380 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
4382 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
4383 if (FAILED(hRet
)) goto VarFix_Exit
;
4386 V_VT(pVarOut
) = V_VT(pVarIn
);
4388 switch (V_VT(pVarIn
))
4391 V_UI1(pVarOut
) = V_UI1(pVarIn
);
4394 V_VT(pVarOut
) = VT_I2
;
4397 V_I2(pVarOut
) = V_I2(pVarIn
);
4400 V_I4(pVarOut
) = V_I4(pVarIn
);
4403 V_I8(pVarOut
) = V_I8(pVarIn
);
4406 if (V_R4(pVarIn
) < 0.0f
)
4407 V_R4(pVarOut
) = (float)ceil(V_R4(pVarIn
));
4409 V_R4(pVarOut
) = (float)floor(V_R4(pVarIn
));
4412 V_VT(pVarOut
) = VT_R8
;
4413 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(pVarOut
));
4418 if (V_R8(pVarIn
) < 0.0)
4419 V_R8(pVarOut
) = ceil(V_R8(pVarIn
));
4421 V_R8(pVarOut
) = floor(V_R8(pVarIn
));
4424 hRet
= VarCyFix(V_CY(pVarIn
), &V_CY(pVarOut
));
4427 hRet
= VarDecFix(&V_DECIMAL(pVarIn
), &V_DECIMAL(pVarOut
));
4430 V_VT(pVarOut
) = VT_I2
;
4437 if (V_TYPE(pVarIn
) == VT_CLSID
|| /* VT_CLSID is a special case */
4438 FAILED(VARIANT_ValidateType(V_VT(pVarIn
))))
4439 hRet
= DISP_E_BADVARTYPE
;
4441 hRet
= DISP_E_TYPEMISMATCH
;
4445 V_VT(pVarOut
) = VT_EMPTY
;
4446 VariantClear(&temp
);
4451 /**********************************************************************
4452 * VarInt [OLEAUT32.172]
4454 * Truncate a variants value to a whole number.
4457 * pVarIn [I] Source variant
4458 * pVarOut [O] Destination for converted value
4461 * Success: S_OK. pVarOut contains the converted value.
4462 * Failure: An HRESULT error code indicating the error.
4465 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4466 * according to the following table:
4467 *| Input Type Output Type
4468 *| ---------- -----------
4472 *| All Others Unchanged
4473 * - The difference between this function and VarFix() is that VarFix() rounds
4474 * negative numbers towards 0, while this function rounds them away from zero.
4476 HRESULT WINAPI
VarInt(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
4478 HRESULT hRet
= S_OK
;
4483 TRACE("(%s,%p)\n", debugstr_variant(pVarIn
), pVarOut
);
4485 /* Handle VT_DISPATCH by storing and taking address of returned value */
4486 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
4488 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
4489 if (FAILED(hRet
)) goto VarInt_Exit
;
4492 V_VT(pVarOut
) = V_VT(pVarIn
);
4494 switch (V_VT(pVarIn
))
4497 V_R4(pVarOut
) = (float)floor(V_R4(pVarIn
));
4500 V_VT(pVarOut
) = VT_R8
;
4501 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(pVarOut
));
4506 V_R8(pVarOut
) = floor(V_R8(pVarIn
));
4509 hRet
= VarCyInt(V_CY(pVarIn
), &V_CY(pVarOut
));
4512 hRet
= VarDecInt(&V_DECIMAL(pVarIn
), &V_DECIMAL(pVarOut
));
4515 hRet
= VarFix(pVarIn
, pVarOut
);
4518 VariantClear(&temp
);
4523 /**********************************************************************
4524 * VarXor [OLEAUT32.167]
4526 * Perform a logical exclusive-or (XOR) operation on two variants.
4529 * pVarLeft [I] First variant
4530 * pVarRight [I] Variant to XOR with pVarLeft
4531 * pVarOut [O] Destination for XOR result
4534 * Success: S_OK. pVarOut contains the result of the operation with its type
4535 * taken from the table below).
4536 * Failure: An HRESULT error code indicating the error.
4539 * - Neither pVarLeft or pVarRight are modified by this function.
4540 * - This function does not process by-reference variants.
4541 * - Input types of VT_BSTR may be numeric strings or boolean text.
4542 * - The type of result stored in pVarOut depends on the types of pVarLeft
4543 * and pVarRight, and will be one of VT_UI1, VT_I2, VT_I4, VT_I8, VT_BOOL,
4544 * or VT_NULL if the function succeeds.
4545 * - Type promotion is inconsistent and as a result certain combinations of
4546 * values will return DISP_E_OVERFLOW even when they could be represented.
4547 * This matches the behaviour of native oleaut32.
4549 HRESULT WINAPI
VarXor(LPVARIANT pVarLeft
, LPVARIANT pVarRight
, LPVARIANT pVarOut
)
4552 VARIANT varLeft
, varRight
;
4553 VARIANT tempLeft
, tempRight
;
4557 TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft
), debugstr_variant(pVarRight
), pVarOut
);
4559 if (V_EXTRA_TYPE(pVarLeft
) || V_EXTRA_TYPE(pVarRight
) ||
4560 V_VT(pVarLeft
) > VT_UINT
|| V_VT(pVarRight
) > VT_UINT
||
4561 V_VT(pVarLeft
) == VT_VARIANT
|| V_VT(pVarRight
) == VT_VARIANT
||
4562 V_VT(pVarLeft
) == VT_UNKNOWN
|| V_VT(pVarRight
) == VT_UNKNOWN
||
4563 V_VT(pVarLeft
) == (VARTYPE
)15 || V_VT(pVarRight
) == (VARTYPE
)15 ||
4564 V_VT(pVarLeft
) == VT_ERROR
|| V_VT(pVarRight
) == VT_ERROR
)
4565 return DISP_E_BADVARTYPE
;
4567 if (V_VT(pVarLeft
) == VT_NULL
|| V_VT(pVarRight
) == VT_NULL
)
4569 /* NULL XOR anything valid is NULL */
4570 V_VT(pVarOut
) = VT_NULL
;
4574 VariantInit(&tempLeft
);
4575 VariantInit(&tempRight
);
4577 /* Handle VT_DISPATCH by storing and taking address of returned value */
4578 if ((V_VT(pVarLeft
) & VT_TYPEMASK
) == VT_DISPATCH
)
4580 hRet
= VARIANT_FetchDispatchValue(pVarLeft
, &tempLeft
);
4581 if (FAILED(hRet
)) goto VarXor_Exit
;
4582 pVarLeft
= &tempLeft
;
4584 if ((V_VT(pVarRight
) & VT_TYPEMASK
) == VT_DISPATCH
)
4586 hRet
= VARIANT_FetchDispatchValue(pVarRight
, &tempRight
);
4587 if (FAILED(hRet
)) goto VarXor_Exit
;
4588 pVarRight
= &tempRight
;
4591 /* Copy our inputs so we don't disturb anything */
4592 V_VT(&varLeft
) = V_VT(&varRight
) = VT_EMPTY
;
4594 hRet
= VariantCopy(&varLeft
, pVarLeft
);
4598 hRet
= VariantCopy(&varRight
, pVarRight
);
4602 /* Try any strings first as numbers, then as VT_BOOL */
4603 if (V_VT(&varLeft
) == VT_BSTR
)
4605 hRet
= VarR8FromStr(V_BSTR(&varLeft
), LOCALE_USER_DEFAULT
, 0, &d
);
4606 hRet
= VariantChangeType(&varLeft
, &varLeft
, VARIANT_LOCALBOOL
,
4607 FAILED(hRet
) ? VT_BOOL
: VT_I4
);
4612 if (V_VT(&varRight
) == VT_BSTR
)
4614 hRet
= VarR8FromStr(V_BSTR(&varRight
), LOCALE_USER_DEFAULT
, 0, &d
);
4615 hRet
= VariantChangeType(&varRight
, &varRight
, VARIANT_LOCALBOOL
,
4616 FAILED(hRet
) ? VT_BOOL
: VT_I4
);
4621 /* Determine the result type */
4622 if (V_VT(&varLeft
) == VT_I8
|| V_VT(&varRight
) == VT_I8
)
4624 if (V_VT(pVarLeft
) == VT_INT
|| V_VT(pVarRight
) == VT_INT
)
4626 hRet
= DISP_E_TYPEMISMATCH
;
4633 switch ((V_VT(&varLeft
) << 16) | V_VT(&varRight
))
4635 case (VT_BOOL
<< 16) | VT_BOOL
:
4638 case (VT_UI1
<< 16) | VT_UI1
:
4641 case (VT_EMPTY
<< 16) | VT_EMPTY
:
4642 case (VT_EMPTY
<< 16) | VT_UI1
:
4643 case (VT_EMPTY
<< 16) | VT_I2
:
4644 case (VT_EMPTY
<< 16) | VT_BOOL
:
4645 case (VT_UI1
<< 16) | VT_EMPTY
:
4646 case (VT_UI1
<< 16) | VT_I2
:
4647 case (VT_UI1
<< 16) | VT_BOOL
:
4648 case (VT_I2
<< 16) | VT_EMPTY
:
4649 case (VT_I2
<< 16) | VT_UI1
:
4650 case (VT_I2
<< 16) | VT_I2
:
4651 case (VT_I2
<< 16) | VT_BOOL
:
4652 case (VT_BOOL
<< 16) | VT_EMPTY
:
4653 case (VT_BOOL
<< 16) | VT_UI1
:
4654 case (VT_BOOL
<< 16) | VT_I2
:
4663 /* VT_UI4 does not overflow */
4666 if (V_VT(&varLeft
) == VT_UI4
)
4667 V_VT(&varLeft
) = VT_I4
;
4668 if (V_VT(&varRight
) == VT_UI4
)
4669 V_VT(&varRight
) = VT_I4
;
4672 /* Convert our input copies to the result type */
4673 if (V_VT(&varLeft
) != vt
)
4674 hRet
= VariantChangeType(&varLeft
, &varLeft
, 0, vt
);
4678 if (V_VT(&varRight
) != vt
)
4679 hRet
= VariantChangeType(&varRight
, &varRight
, 0, vt
);
4685 /* Calculate the result */
4689 V_I8(pVarOut
) = V_I8(&varLeft
) ^ V_I8(&varRight
);
4692 V_I4(pVarOut
) = V_I4(&varLeft
) ^ V_I4(&varRight
);
4696 V_I2(pVarOut
) = V_I2(&varLeft
) ^ V_I2(&varRight
);
4699 V_UI1(pVarOut
) = V_UI1(&varLeft
) ^ V_UI1(&varRight
);
4704 VariantClear(&varLeft
);
4705 VariantClear(&varRight
);
4706 VariantClear(&tempLeft
);
4707 VariantClear(&tempRight
);
4711 /**********************************************************************
4712 * VarEqv [OLEAUT32.172]
4714 * Determine if two variants contain the same value.
4717 * pVarLeft [I] First variant to compare
4718 * pVarRight [I] Variant to compare to pVarLeft
4719 * pVarOut [O] Destination for comparison result
4722 * Success: S_OK. pVarOut contains the result of the comparison (VARIANT_TRUE
4723 * if equivalent or non-zero otherwise.
4724 * Failure: An HRESULT error code indicating the error.
4727 * - This function simply calls VarXor() on pVarLeft and pVarRight and inverts
4730 HRESULT WINAPI
VarEqv(LPVARIANT pVarLeft
, LPVARIANT pVarRight
, LPVARIANT pVarOut
)
4734 TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft
), debugstr_variant(pVarRight
), pVarOut
);
4736 hRet
= VarXor(pVarLeft
, pVarRight
, pVarOut
);
4737 if (SUCCEEDED(hRet
))
4739 if (V_VT(pVarOut
) == VT_I8
)
4740 V_I8(pVarOut
) = ~V_I8(pVarOut
);
4742 V_UI4(pVarOut
) = ~V_UI4(pVarOut
);
4747 /**********************************************************************
4748 * VarNeg [OLEAUT32.173]
4750 * Negate the value of a variant.
4753 * pVarIn [I] Source variant
4754 * pVarOut [O] Destination for converted value
4757 * Success: S_OK. pVarOut contains the converted value.
4758 * Failure: An HRESULT error code indicating the error.
4761 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4762 * according to the following table:
4763 *| Input Type Output Type
4764 *| ---------- -----------
4769 *| All Others Unchanged (unless promoted)
4770 * - Where the negated value of a variant does not fit in its base type, the type
4771 * is promoted according to the following table:
4772 *| Input Type Promoted To
4773 *| ---------- -----------
4777 * - The native version of this function returns DISP_E_BADVARTYPE for valid
4778 * variant types that cannot be negated, and returns DISP_E_TYPEMISMATCH
4779 * for types which are not valid. Since this is in contravention of the
4780 * meaning of those error codes and unlikely to be relied on by applications,
4781 * this implementation returns errors consistent with the other high level
4782 * variant math functions.
4784 HRESULT WINAPI
VarNeg(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
4786 HRESULT hRet
= S_OK
;
4791 TRACE("(%s,%p)\n", debugstr_variant(pVarIn
), pVarOut
);
4793 /* Handle VT_DISPATCH by storing and taking address of returned value */
4794 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
4796 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
4797 if (FAILED(hRet
)) goto VarNeg_Exit
;
4800 V_VT(pVarOut
) = V_VT(pVarIn
);
4802 switch (V_VT(pVarIn
))
4805 V_VT(pVarOut
) = VT_I2
;
4806 V_I2(pVarOut
) = -V_UI1(pVarIn
);
4809 V_VT(pVarOut
) = VT_I2
;
4812 if (V_I2(pVarIn
) == I2_MIN
)
4814 V_VT(pVarOut
) = VT_I4
;
4815 V_I4(pVarOut
) = -(int)V_I2(pVarIn
);
4818 V_I2(pVarOut
) = -V_I2(pVarIn
);
4821 if (V_I4(pVarIn
) == I4_MIN
)
4823 V_VT(pVarOut
) = VT_R8
;
4824 V_R8(pVarOut
) = -(double)V_I4(pVarIn
);
4827 V_I4(pVarOut
) = -V_I4(pVarIn
);
4830 if (V_I8(pVarIn
) == I8_MIN
)
4832 V_VT(pVarOut
) = VT_R8
;
4833 hRet
= VarR8FromI8(V_I8(pVarIn
), &V_R8(pVarOut
));
4834 V_R8(pVarOut
) *= -1.0;
4837 V_I8(pVarOut
) = -V_I8(pVarIn
);
4840 V_R4(pVarOut
) = -V_R4(pVarIn
);
4844 V_R8(pVarOut
) = -V_R8(pVarIn
);
4847 hRet
= VarCyNeg(V_CY(pVarIn
), &V_CY(pVarOut
));
4850 hRet
= VarDecNeg(&V_DECIMAL(pVarIn
), &V_DECIMAL(pVarOut
));
4853 V_VT(pVarOut
) = VT_R8
;
4854 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(pVarOut
));
4855 V_R8(pVarOut
) = -V_R8(pVarOut
);
4858 V_VT(pVarOut
) = VT_I2
;
4865 if (V_TYPE(pVarIn
) == VT_CLSID
|| /* VT_CLSID is a special case */
4866 FAILED(VARIANT_ValidateType(V_VT(pVarIn
))))
4867 hRet
= DISP_E_BADVARTYPE
;
4869 hRet
= DISP_E_TYPEMISMATCH
;
4873 V_VT(pVarOut
) = VT_EMPTY
;
4874 VariantClear(&temp
);
4879 /**********************************************************************
4880 * VarNot [OLEAUT32.174]
4882 * Perform a not operation on a variant.
4885 * pVarIn [I] Source variant
4886 * pVarOut [O] Destination for converted value
4889 * Success: S_OK. pVarOut contains the converted value.
4890 * Failure: An HRESULT error code indicating the error.
4893 * - Strictly speaking, this function performs a bitwise ones complement
4894 * on the variants value (after possibly converting to VT_I4, see below).
4895 * This only behaves like a boolean not operation if the value in
4896 * pVarIn is either VARIANT_TRUE or VARIANT_FALSE and the type is signed.
4897 * - To perform a genuine not operation, convert the variant to a VT_BOOL
4898 * before calling this function.
4899 * - This function does not process by-reference variants.
4900 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4901 * according to the following table:
4902 *| Input Type Output Type
4903 *| ---------- -----------
4910 *| (All others) Unchanged
4912 HRESULT WINAPI
VarNot(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
4915 HRESULT hRet
= S_OK
;
4920 TRACE("(%s,%p)\n", debugstr_variant(pVarIn
), pVarOut
);
4922 /* Handle VT_DISPATCH by storing and taking address of returned value */
4923 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
4925 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
4926 if (FAILED(hRet
)) goto VarNot_Exit
;
4930 if (V_VT(pVarIn
) == VT_BSTR
)
4932 V_VT(&varIn
) = VT_R8
;
4933 hRet
= VarR8FromStr( V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(&varIn
) );
4936 V_VT(&varIn
) = VT_BOOL
;
4937 hRet
= VarBoolFromStr( V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, VAR_LOCALBOOL
, &V_BOOL(&varIn
) );
4939 if (FAILED(hRet
)) goto VarNot_Exit
;
4943 V_VT(pVarOut
) = V_VT(pVarIn
);
4945 switch (V_VT(pVarIn
))
4948 V_I4(pVarOut
) = ~V_I1(pVarIn
);
4949 V_VT(pVarOut
) = VT_I4
;
4951 case VT_UI1
: V_UI1(pVarOut
) = ~V_UI1(pVarIn
); break;
4953 case VT_I2
: V_I2(pVarOut
) = ~V_I2(pVarIn
); break;
4955 V_I4(pVarOut
) = ~V_UI2(pVarIn
);
4956 V_VT(pVarOut
) = VT_I4
;
4959 hRet
= VarI4FromDec(&V_DECIMAL(pVarIn
), &V_I4(&varIn
));
4963 /* Fall through ... */
4965 V_VT(pVarOut
) = VT_I4
;
4966 /* Fall through ... */
4967 case VT_I4
: V_I4(pVarOut
) = ~V_I4(pVarIn
); break;
4970 V_I4(pVarOut
) = ~V_UI4(pVarIn
);
4971 V_VT(pVarOut
) = VT_I4
;
4973 case VT_I8
: V_I8(pVarOut
) = ~V_I8(pVarIn
); break;
4975 V_I4(pVarOut
) = ~V_UI8(pVarIn
);
4976 V_VT(pVarOut
) = VT_I4
;
4979 hRet
= VarI4FromR4(V_R4(pVarIn
), &V_I4(pVarOut
));
4980 V_I4(pVarOut
) = ~V_I4(pVarOut
);
4981 V_VT(pVarOut
) = VT_I4
;
4985 hRet
= VarI4FromR8(V_R8(pVarIn
), &V_I4(pVarOut
));
4986 V_I4(pVarOut
) = ~V_I4(pVarOut
);
4987 V_VT(pVarOut
) = VT_I4
;
4990 hRet
= VarI4FromCy(V_CY(pVarIn
), &V_I4(pVarOut
));
4991 V_I4(pVarOut
) = ~V_I4(pVarOut
);
4992 V_VT(pVarOut
) = VT_I4
;
4996 V_VT(pVarOut
) = VT_I2
;
5002 if (V_TYPE(pVarIn
) == VT_CLSID
|| /* VT_CLSID is a special case */
5003 FAILED(VARIANT_ValidateType(V_VT(pVarIn
))))
5004 hRet
= DISP_E_BADVARTYPE
;
5006 hRet
= DISP_E_TYPEMISMATCH
;
5010 V_VT(pVarOut
) = VT_EMPTY
;
5011 VariantClear(&temp
);
5016 /**********************************************************************
5017 * VarRound [OLEAUT32.175]
5019 * Perform a round operation on a variant.
5022 * pVarIn [I] Source variant
5023 * deci [I] Number of decimals to round to
5024 * pVarOut [O] Destination for converted value
5027 * Success: S_OK. pVarOut contains the converted value.
5028 * Failure: An HRESULT error code indicating the error.
5031 * - Floating point values are rounded to the desired number of decimals.
5032 * - Some integer types are just copied to the return variable.
5033 * - Some other integer types are not handled and fail.
5035 HRESULT WINAPI
VarRound(LPVARIANT pVarIn
, int deci
, LPVARIANT pVarOut
)
5038 HRESULT hRet
= S_OK
;
5044 TRACE("(%s,%d)\n", debugstr_variant(pVarIn
), deci
);
5046 /* Handle VT_DISPATCH by storing and taking address of returned value */
5047 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
5049 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
5050 if (FAILED(hRet
)) goto VarRound_Exit
;
5054 switch (V_VT(pVarIn
))
5056 /* cases that fail on windows */
5061 hRet
= DISP_E_BADVARTYPE
;
5064 /* cases just copying in to out */
5066 V_VT(pVarOut
) = V_VT(pVarIn
);
5067 V_UI1(pVarOut
) = V_UI1(pVarIn
);
5070 V_VT(pVarOut
) = V_VT(pVarIn
);
5071 V_I2(pVarOut
) = V_I2(pVarIn
);
5074 V_VT(pVarOut
) = V_VT(pVarIn
);
5075 V_I4(pVarOut
) = V_I4(pVarIn
);
5078 V_VT(pVarOut
) = V_VT(pVarIn
);
5079 /* value unchanged */
5082 /* cases that change type */
5084 V_VT(pVarOut
) = VT_I2
;
5088 V_VT(pVarOut
) = VT_I2
;
5089 V_I2(pVarOut
) = V_BOOL(pVarIn
);
5092 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(&varIn
));
5097 /* Fall through ... */
5099 /* cases we need to do math */
5101 if (V_R8(pVarIn
)>0) {
5102 V_R8(pVarOut
)=floor(V_R8(pVarIn
)*pow(10, deci
)+0.5)/pow(10, deci
);
5104 V_R8(pVarOut
)=ceil(V_R8(pVarIn
)*pow(10, deci
)-0.5)/pow(10, deci
);
5106 V_VT(pVarOut
) = V_VT(pVarIn
);
5109 if (V_R4(pVarIn
)>0) {
5110 V_R4(pVarOut
)=floor(V_R4(pVarIn
)*pow(10, deci
)+0.5)/pow(10, deci
);
5112 V_R4(pVarOut
)=ceil(V_R4(pVarIn
)*pow(10, deci
)-0.5)/pow(10, deci
);
5114 V_VT(pVarOut
) = V_VT(pVarIn
);
5117 if (V_DATE(pVarIn
)>0) {
5118 V_DATE(pVarOut
)=floor(V_DATE(pVarIn
)*pow(10, deci
)+0.5)/pow(10, deci
);
5120 V_DATE(pVarOut
)=ceil(V_DATE(pVarIn
)*pow(10, deci
)-0.5)/pow(10, deci
);
5122 V_VT(pVarOut
) = V_VT(pVarIn
);
5128 factor
=pow(10, 4-deci
);
5130 if (V_CY(pVarIn
).int64
>0) {
5131 V_CY(pVarOut
).int64
=floor(V_CY(pVarIn
).int64
/factor
)*factor
;
5133 V_CY(pVarOut
).int64
=ceil(V_CY(pVarIn
).int64
/factor
)*factor
;
5135 V_VT(pVarOut
) = V_VT(pVarIn
);
5138 /* cases we don't know yet */
5140 FIXME("unimplemented part, V_VT(pVarIn) == 0x%X, deci == %d\n",
5141 V_VT(pVarIn
) & VT_TYPEMASK
, deci
);
5142 hRet
= DISP_E_BADVARTYPE
;
5146 V_VT(pVarOut
) = VT_EMPTY
;
5147 VariantClear(&temp
);
5149 TRACE("returning 0x%08x %s\n", hRet
, debugstr_variant(pVarOut
));
5153 /**********************************************************************
5154 * VarIdiv [OLEAUT32.153]
5156 * Converts input variants to integers and divides them.
5159 * left [I] Left hand variant
5160 * right [I] Right hand variant
5161 * result [O] Destination for quotient
5164 * Success: S_OK. result contains the quotient.
5165 * Failure: An HRESULT error code indicating the error.
5168 * If either expression is null, null is returned, as per MSDN
5170 HRESULT WINAPI
VarIdiv(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
5172 HRESULT hres
= S_OK
;
5173 VARTYPE resvt
= VT_EMPTY
;
5174 VARTYPE leftvt
,rightvt
;
5175 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
5177 VARIANT tempLeft
, tempRight
;
5179 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
5183 VariantInit(&tempLeft
);
5184 VariantInit(&tempRight
);
5186 leftvt
= V_VT(left
)&VT_TYPEMASK
;
5187 rightvt
= V_VT(right
)&VT_TYPEMASK
;
5188 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
5189 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
5191 if (leftExtraFlags
!= rightExtraFlags
)
5193 hres
= DISP_E_BADVARTYPE
;
5196 ExtraFlags
= leftExtraFlags
;
5198 /* Native VarIdiv always returns an error when using extra
5199 * flags or if the variant combination is I8 and INT.
5201 if ((leftvt
== VT_I8
&& rightvt
== VT_INT
) ||
5202 (leftvt
== VT_INT
&& rightvt
== VT_I8
) ||
5203 (rightvt
== VT_EMPTY
&& leftvt
!= VT_NULL
) ||
5206 hres
= DISP_E_BADVARTYPE
;
5210 /* Determine variant type */
5211 else if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
)
5213 V_VT(result
) = VT_NULL
;
5217 else if (leftvt
== VT_I8
|| rightvt
== VT_I8
)
5219 else if (leftvt
== VT_I4
|| rightvt
== VT_I4
||
5220 leftvt
== VT_INT
|| rightvt
== VT_INT
||
5221 leftvt
== VT_UINT
|| rightvt
== VT_UINT
||
5222 leftvt
== VT_UI8
|| rightvt
== VT_UI8
||
5223 leftvt
== VT_UI4
|| rightvt
== VT_UI4
||
5224 leftvt
== VT_UI2
|| rightvt
== VT_UI2
||
5225 leftvt
== VT_I1
|| rightvt
== VT_I1
||
5226 leftvt
== VT_BSTR
|| rightvt
== VT_BSTR
||
5227 leftvt
== VT_DATE
|| rightvt
== VT_DATE
||
5228 leftvt
== VT_CY
|| rightvt
== VT_CY
||
5229 leftvt
== VT_DECIMAL
|| rightvt
== VT_DECIMAL
||
5230 leftvt
== VT_R8
|| rightvt
== VT_R8
||
5231 leftvt
== VT_R4
|| rightvt
== VT_R4
)
5233 else if (leftvt
== VT_I2
|| rightvt
== VT_I2
||
5234 leftvt
== VT_BOOL
|| rightvt
== VT_BOOL
||
5237 else if (leftvt
== VT_UI1
|| rightvt
== VT_UI1
)
5241 hres
= DISP_E_BADVARTYPE
;
5245 /* coerce to the result type */
5246 hres
= VariantChangeType(&lv
, left
, 0, resvt
);
5247 if (hres
!= S_OK
) goto end
;
5248 hres
= VariantChangeType(&rv
, right
, 0, resvt
);
5249 if (hres
!= S_OK
) goto end
;
5252 V_VT(result
) = resvt
;
5256 if (V_UI1(&rv
) == 0)
5258 hres
= DISP_E_DIVBYZERO
;
5259 V_VT(result
) = VT_EMPTY
;
5262 V_UI1(result
) = V_UI1(&lv
) / V_UI1(&rv
);
5267 hres
= DISP_E_DIVBYZERO
;
5268 V_VT(result
) = VT_EMPTY
;
5271 V_I2(result
) = V_I2(&lv
) / V_I2(&rv
);
5276 hres
= DISP_E_DIVBYZERO
;
5277 V_VT(result
) = VT_EMPTY
;
5280 V_I4(result
) = V_I4(&lv
) / V_I4(&rv
);
5285 hres
= DISP_E_DIVBYZERO
;
5286 V_VT(result
) = VT_EMPTY
;
5289 V_I8(result
) = V_I8(&lv
) / V_I8(&rv
);
5292 FIXME("Couldn't integer divide variant types %d,%d\n",
5299 VariantClear(&tempLeft
);
5300 VariantClear(&tempRight
);
5306 /**********************************************************************
5307 * VarMod [OLEAUT32.155]
5309 * Perform the modulus operation of the right hand variant on the left
5312 * left [I] Left hand variant
5313 * right [I] Right hand variant
5314 * result [O] Destination for converted value
5317 * Success: S_OK. result contains the remainder.
5318 * Failure: An HRESULT error code indicating the error.
5321 * If an error occurs the type of result will be modified but the value will not be.
5322 * Doesn't support arrays or any special flags yet.
5324 HRESULT WINAPI
VarMod(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
5327 HRESULT rc
= E_FAIL
;
5330 VARIANT tempLeft
, tempRight
;
5332 VariantInit(&tempLeft
);
5333 VariantInit(&tempRight
);
5337 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
5339 /* Handle VT_DISPATCH by storing and taking address of returned value */
5340 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
5342 rc
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
5343 if (FAILED(rc
)) goto end
;
5346 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
5348 rc
= VARIANT_FetchDispatchValue(right
, &tempRight
);
5349 if (FAILED(rc
)) goto end
;
5353 /* check for invalid inputs */
5355 switch (V_VT(left
) & VT_TYPEMASK
) {
5377 V_VT(result
) = VT_EMPTY
;
5378 rc
= DISP_E_TYPEMISMATCH
;
5381 rc
= DISP_E_TYPEMISMATCH
;
5384 V_VT(result
) = VT_EMPTY
;
5385 rc
= DISP_E_TYPEMISMATCH
;
5390 V_VT(result
) = VT_EMPTY
;
5391 rc
= DISP_E_BADVARTYPE
;
5396 switch (V_VT(right
) & VT_TYPEMASK
) {
5402 if((V_VT(left
) == VT_INT
) && (V_VT(right
) == VT_I8
))
5404 V_VT(result
) = VT_EMPTY
;
5405 rc
= DISP_E_TYPEMISMATCH
;
5409 if((V_VT(right
) == VT_INT
) && (V_VT(left
) == VT_I8
))
5411 V_VT(result
) = VT_EMPTY
;
5412 rc
= DISP_E_TYPEMISMATCH
;
5423 if(V_VT(left
) == VT_EMPTY
)
5425 V_VT(result
) = VT_I4
;
5432 if(V_VT(left
) == VT_ERROR
)
5434 V_VT(result
) = VT_EMPTY
;
5435 rc
= DISP_E_TYPEMISMATCH
;
5439 if(V_VT(left
) == VT_NULL
)
5441 V_VT(result
) = VT_NULL
;
5448 V_VT(result
) = VT_EMPTY
;
5449 rc
= DISP_E_BADVARTYPE
;
5452 if(V_VT(left
) == VT_VOID
)
5454 V_VT(result
) = VT_EMPTY
;
5455 rc
= DISP_E_BADVARTYPE
;
5456 } else if((V_VT(left
) == VT_NULL
) || (V_VT(left
) == VT_EMPTY
) || (V_VT(left
) == VT_ERROR
) ||
5459 V_VT(result
) = VT_NULL
;
5463 V_VT(result
) = VT_NULL
;
5464 rc
= DISP_E_BADVARTYPE
;
5469 V_VT(result
) = VT_EMPTY
;
5470 rc
= DISP_E_TYPEMISMATCH
;
5473 rc
= DISP_E_TYPEMISMATCH
;
5476 if((V_VT(left
) == 15) || ((V_VT(left
) >= 24) && (V_VT(left
) <= 35)) || !lOk
)
5478 V_VT(result
) = VT_EMPTY
;
5479 rc
= DISP_E_BADVARTYPE
;
5482 V_VT(result
) = VT_EMPTY
;
5483 rc
= DISP_E_TYPEMISMATCH
;
5487 V_VT(result
) = VT_EMPTY
;
5488 rc
= DISP_E_BADVARTYPE
;
5492 /* determine the result type */
5493 if((V_VT(left
) == VT_I8
) || (V_VT(right
) == VT_I8
)) resT
= VT_I8
;
5494 else if((V_VT(left
) == VT_UI1
) && (V_VT(right
) == VT_BOOL
)) resT
= VT_I2
;
5495 else if((V_VT(left
) == VT_UI1
) && (V_VT(right
) == VT_UI1
)) resT
= VT_UI1
;
5496 else if((V_VT(left
) == VT_UI1
) && (V_VT(right
) == VT_I2
)) resT
= VT_I2
;
5497 else if((V_VT(left
) == VT_I2
) && (V_VT(right
) == VT_BOOL
)) resT
= VT_I2
;
5498 else if((V_VT(left
) == VT_I2
) && (V_VT(right
) == VT_UI1
)) resT
= VT_I2
;
5499 else if((V_VT(left
) == VT_I2
) && (V_VT(right
) == VT_I2
)) resT
= VT_I2
;
5500 else if((V_VT(left
) == VT_BOOL
) && (V_VT(right
) == VT_BOOL
)) resT
= VT_I2
;
5501 else if((V_VT(left
) == VT_BOOL
) && (V_VT(right
) == VT_UI1
)) resT
= VT_I2
;
5502 else if((V_VT(left
) == VT_BOOL
) && (V_VT(right
) == VT_I2
)) resT
= VT_I2
;
5503 else resT
= VT_I4
; /* most outputs are I4 */
5505 /* convert to I8 for the modulo */
5506 rc
= VariantChangeType(&lv
, left
, 0, VT_I8
);
5509 FIXME("Could not convert left type %d to %d? rc == 0x%X\n", V_VT(left
), VT_I8
, rc
);
5513 rc
= VariantChangeType(&rv
, right
, 0, VT_I8
);
5516 FIXME("Could not convert right type %d to %d? rc == 0x%X\n", V_VT(right
), VT_I8
, rc
);
5520 /* if right is zero set VT_EMPTY and return divide by zero */
5523 V_VT(result
) = VT_EMPTY
;
5524 rc
= DISP_E_DIVBYZERO
;
5528 /* perform the modulo operation */
5529 V_VT(result
) = VT_I8
;
5530 V_I8(result
) = V_I8(&lv
) % V_I8(&rv
);
5532 TRACE("V_I8(left) == %s, V_I8(right) == %s, V_I8(result) == %s\n",
5533 wine_dbgstr_longlong(V_I8(&lv
)), wine_dbgstr_longlong(V_I8(&rv
)),
5534 wine_dbgstr_longlong(V_I8(result
)));
5536 /* convert left and right to the destination type */
5537 rc
= VariantChangeType(result
, result
, 0, resT
);
5540 FIXME("Could not convert 0x%x to %d?\n", V_VT(result
), resT
);
5541 /* fall to end of function */
5547 VariantClear(&tempLeft
);
5548 VariantClear(&tempRight
);
5552 /**********************************************************************
5553 * VarPow [OLEAUT32.158]
5555 * Computes the power of one variant to another variant.
5558 * left [I] First variant
5559 * right [I] Second variant
5560 * result [O] Result variant
5564 * Failure: An HRESULT error code indicating the error.
5566 HRESULT WINAPI
VarPow(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
5570 VARTYPE resvt
= VT_EMPTY
;
5571 VARTYPE leftvt
,rightvt
;
5572 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
5573 VARIANT tempLeft
, tempRight
;
5575 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
5579 VariantInit(&tempLeft
);
5580 VariantInit(&tempRight
);
5582 /* Handle VT_DISPATCH by storing and taking address of returned value */
5583 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
5585 hr
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
5586 if (FAILED(hr
)) goto end
;
5589 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
5591 hr
= VARIANT_FetchDispatchValue(right
, &tempRight
);
5592 if (FAILED(hr
)) goto end
;
5596 leftvt
= V_VT(left
)&VT_TYPEMASK
;
5597 rightvt
= V_VT(right
)&VT_TYPEMASK
;
5598 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
5599 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
5601 if (leftExtraFlags
!= rightExtraFlags
)
5603 hr
= DISP_E_BADVARTYPE
;
5606 ExtraFlags
= leftExtraFlags
;
5608 /* Native VarPow always returns an error when using extra flags */
5609 if (ExtraFlags
!= 0)
5611 hr
= DISP_E_BADVARTYPE
;
5615 /* Determine return type */
5616 else if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
) {
5617 V_VT(result
) = VT_NULL
;
5621 else if ((leftvt
== VT_EMPTY
|| leftvt
== VT_I2
||
5622 leftvt
== VT_I4
|| leftvt
== VT_R4
||
5623 leftvt
== VT_R8
|| leftvt
== VT_CY
||
5624 leftvt
== VT_DATE
|| leftvt
== VT_BSTR
||
5625 leftvt
== VT_BOOL
|| leftvt
== VT_DECIMAL
||
5626 (leftvt
>= VT_I1
&& leftvt
<= VT_UINT
)) &&
5627 (rightvt
== VT_EMPTY
|| rightvt
== VT_I2
||
5628 rightvt
== VT_I4
|| rightvt
== VT_R4
||
5629 rightvt
== VT_R8
|| rightvt
== VT_CY
||
5630 rightvt
== VT_DATE
|| rightvt
== VT_BSTR
||
5631 rightvt
== VT_BOOL
|| rightvt
== VT_DECIMAL
||
5632 (rightvt
>= VT_I1
&& rightvt
<= VT_UINT
)))
5636 hr
= DISP_E_BADVARTYPE
;
5640 hr
= VariantChangeType(&dl
,left
,0,resvt
);
5642 ERR("Could not change passed left argument to VT_R8, handle it differently.\n");
5647 hr
= VariantChangeType(&dr
,right
,0,resvt
);
5649 ERR("Could not change passed right argument to VT_R8, handle it differently.\n");
5654 V_VT(result
) = VT_R8
;
5655 V_R8(result
) = pow(V_R8(&dl
),V_R8(&dr
));
5660 VariantClear(&tempLeft
);
5661 VariantClear(&tempRight
);
5666 /**********************************************************************
5667 * VarImp [OLEAUT32.154]
5669 * Bitwise implication of two variants.
5672 * left [I] First variant
5673 * right [I] Second variant
5674 * result [O] Result variant
5678 * Failure: An HRESULT error code indicating the error.
5680 HRESULT WINAPI
VarImp(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
5682 HRESULT hres
= S_OK
;
5683 VARTYPE resvt
= VT_EMPTY
;
5684 VARTYPE leftvt
,rightvt
;
5685 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
5688 VARIANT tempLeft
, tempRight
;
5692 VariantInit(&tempLeft
);
5693 VariantInit(&tempRight
);
5695 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
5697 /* Handle VT_DISPATCH by storing and taking address of returned value */
5698 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
5700 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
5701 if (FAILED(hres
)) goto VarImp_Exit
;
5704 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
5706 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
5707 if (FAILED(hres
)) goto VarImp_Exit
;
5711 leftvt
= V_VT(left
)&VT_TYPEMASK
;
5712 rightvt
= V_VT(right
)&VT_TYPEMASK
;
5713 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
5714 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
5716 if (leftExtraFlags
!= rightExtraFlags
)
5718 hres
= DISP_E_BADVARTYPE
;
5721 ExtraFlags
= leftExtraFlags
;
5723 /* Native VarImp always returns an error when using extra
5724 * flags or if the variants are I8 and INT.
5726 if ((leftvt
== VT_I8
&& rightvt
== VT_INT
) ||
5729 hres
= DISP_E_BADVARTYPE
;
5733 /* Determine result type */
5734 else if ((leftvt
== VT_NULL
&& rightvt
== VT_NULL
) ||
5735 (leftvt
== VT_NULL
&& rightvt
== VT_EMPTY
))
5737 V_VT(result
) = VT_NULL
;
5741 else if (leftvt
== VT_I8
|| rightvt
== VT_I8
)
5743 else if (leftvt
== VT_I4
|| rightvt
== VT_I4
||
5744 leftvt
== VT_INT
|| rightvt
== VT_INT
||
5745 leftvt
== VT_UINT
|| rightvt
== VT_UINT
||
5746 leftvt
== VT_UI4
|| rightvt
== VT_UI4
||
5747 leftvt
== VT_UI8
|| rightvt
== VT_UI8
||
5748 leftvt
== VT_UI2
|| rightvt
== VT_UI2
||
5749 leftvt
== VT_DECIMAL
|| rightvt
== VT_DECIMAL
||
5750 leftvt
== VT_DATE
|| rightvt
== VT_DATE
||
5751 leftvt
== VT_CY
|| rightvt
== VT_CY
||
5752 leftvt
== VT_R8
|| rightvt
== VT_R8
||
5753 leftvt
== VT_R4
|| rightvt
== VT_R4
||
5754 leftvt
== VT_I1
|| rightvt
== VT_I1
)
5756 else if ((leftvt
== VT_UI1
&& rightvt
== VT_UI1
) ||
5757 (leftvt
== VT_UI1
&& rightvt
== VT_NULL
) ||
5758 (leftvt
== VT_NULL
&& rightvt
== VT_UI1
))
5760 else if (leftvt
== VT_EMPTY
|| rightvt
== VT_EMPTY
||
5761 leftvt
== VT_I2
|| rightvt
== VT_I2
||
5762 leftvt
== VT_UI1
|| rightvt
== VT_UI1
)
5764 else if (leftvt
== VT_BOOL
|| rightvt
== VT_BOOL
||
5765 leftvt
== VT_BSTR
|| rightvt
== VT_BSTR
)
5768 /* VT_NULL requires special handling for when the opposite
5769 * variant is equal to something other than -1.
5770 * (NULL Imp 0 = NULL, NULL Imp n = n)
5772 if (leftvt
== VT_NULL
)
5777 case VT_I1
: if (!V_I1(right
)) resvt
= VT_NULL
; break;
5778 case VT_UI1
: if (!V_UI1(right
)) resvt
= VT_NULL
; break;
5779 case VT_I2
: if (!V_I2(right
)) resvt
= VT_NULL
; break;
5780 case VT_UI2
: if (!V_UI2(right
)) resvt
= VT_NULL
; break;
5781 case VT_I4
: if (!V_I4(right
)) resvt
= VT_NULL
; break;
5782 case VT_UI4
: if (!V_UI4(right
)) resvt
= VT_NULL
; break;
5783 case VT_I8
: if (!V_I8(right
)) resvt
= VT_NULL
; break;
5784 case VT_UI8
: if (!V_UI8(right
)) resvt
= VT_NULL
; break;
5785 case VT_INT
: if (!V_INT(right
)) resvt
= VT_NULL
; break;
5786 case VT_UINT
: if (!V_UINT(right
)) resvt
= VT_NULL
; break;
5787 case VT_BOOL
: if (!V_BOOL(right
)) resvt
= VT_NULL
; break;
5788 case VT_R4
: if (!V_R4(right
)) resvt
= VT_NULL
; break;
5789 case VT_R8
: if (!V_R8(right
)) resvt
= VT_NULL
; break;
5790 case VT_DATE
: if (!V_DATE(right
)) resvt
= VT_NULL
; break;
5791 case VT_CY
: if (!V_CY(right
).int64
) resvt
= VT_NULL
; break;
5793 if (!(DEC_HI32(&V_DECIMAL(right
)) || DEC_LO64(&V_DECIMAL(right
))))
5797 hres
= VarBoolFromStr(V_BSTR(right
),LOCALE_USER_DEFAULT
, VAR_LOCALBOOL
, &b
);
5798 if (FAILED(hres
)) goto VarImp_Exit
;
5800 V_VT(result
) = VT_NULL
;
5803 V_VT(result
) = VT_BOOL
;
5808 if (resvt
== VT_NULL
)
5810 V_VT(result
) = resvt
;
5815 hres
= VariantChangeType(result
,right
,0,resvt
);
5820 /* Special handling is required when NULL is the right variant.
5821 * (-1 Imp NULL = NULL, n Imp NULL = n Imp 0)
5823 else if (rightvt
== VT_NULL
)
5828 case VT_I1
: if (V_I1(left
) == -1) resvt
= VT_NULL
; break;
5829 case VT_UI1
: if (V_UI1(left
) == 0xff) resvt
= VT_NULL
; break;
5830 case VT_I2
: if (V_I2(left
) == -1) resvt
= VT_NULL
; break;
5831 case VT_UI2
: if (V_UI2(left
) == 0xffff) resvt
= VT_NULL
; break;
5832 case VT_INT
: if (V_INT(left
) == -1) resvt
= VT_NULL
; break;
5833 case VT_UINT
: if (V_UINT(left
) == ~0u) resvt
= VT_NULL
; break;
5834 case VT_I4
: if (V_I4(left
) == -1) resvt
= VT_NULL
; break;
5835 case VT_UI4
: if (V_UI4(left
) == ~0u) resvt
= VT_NULL
; break;
5836 case VT_I8
: if (V_I8(left
) == -1) resvt
= VT_NULL
; break;
5837 case VT_UI8
: if (V_UI8(left
) == ~(ULONGLONG
)0) resvt
= VT_NULL
; break;
5838 case VT_BOOL
: if (V_BOOL(left
) == VARIANT_TRUE
) resvt
= VT_NULL
; break;
5839 case VT_R4
: if (V_R4(left
) == -1.0) resvt
= VT_NULL
; break;
5840 case VT_R8
: if (V_R8(left
) == -1.0) resvt
= VT_NULL
; break;
5841 case VT_CY
: if (V_CY(left
).int64
== -1) resvt
= VT_NULL
; break;
5843 if (DEC_HI32(&V_DECIMAL(left
)) == 0xffffffff)
5847 hres
= VarBoolFromStr(V_BSTR(left
),LOCALE_USER_DEFAULT
, VAR_LOCALBOOL
, &b
);
5848 if (FAILED(hres
)) goto VarImp_Exit
;
5849 else if (b
== VARIANT_TRUE
)
5852 if (resvt
== VT_NULL
)
5854 V_VT(result
) = resvt
;
5859 hres
= VariantCopy(&lv
, left
);
5860 if (FAILED(hres
)) goto VarImp_Exit
;
5862 if (rightvt
== VT_NULL
)
5864 memset( &rv
, 0, sizeof(rv
) );
5869 hres
= VariantCopy(&rv
, right
);
5870 if (FAILED(hres
)) goto VarImp_Exit
;
5873 if (V_VT(&lv
) == VT_BSTR
&&
5874 FAILED(VarR8FromStr(V_BSTR(&lv
),LOCALE_USER_DEFAULT
, 0, &d
)))
5875 hres
= VariantChangeType(&lv
,&lv
,VARIANT_LOCALBOOL
, VT_BOOL
);
5876 if (SUCCEEDED(hres
) && V_VT(&lv
) != resvt
)
5877 hres
= VariantChangeType(&lv
,&lv
,0,resvt
);
5878 if (FAILED(hres
)) goto VarImp_Exit
;
5880 if (V_VT(&rv
) == VT_BSTR
&&
5881 FAILED(VarR8FromStr(V_BSTR(&rv
),LOCALE_USER_DEFAULT
, 0, &d
)))
5882 hres
= VariantChangeType(&rv
, &rv
,VARIANT_LOCALBOOL
, VT_BOOL
);
5883 if (SUCCEEDED(hres
) && V_VT(&rv
) != resvt
)
5884 hres
= VariantChangeType(&rv
, &rv
, 0, resvt
);
5885 if (FAILED(hres
)) goto VarImp_Exit
;
5888 V_VT(result
) = resvt
;
5892 V_I8(result
) = (~V_I8(&lv
)) | V_I8(&rv
);
5895 V_I4(result
) = (~V_I4(&lv
)) | V_I4(&rv
);
5898 V_I2(result
) = (~V_I2(&lv
)) | V_I2(&rv
);
5901 V_UI1(result
) = (~V_UI1(&lv
)) | V_UI1(&rv
);
5904 V_BOOL(result
) = (~V_BOOL(&lv
)) | V_BOOL(&rv
);
5907 FIXME("Couldn't perform bitwise implication on variant types %d,%d\n",
5915 VariantClear(&tempLeft
);
5916 VariantClear(&tempRight
);