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 /* Convert a variant from one type to another */
49 static inline HRESULT
VARIANT_Coerce(VARIANTARG
* pd
, LCID lcid
, USHORT wFlags
,
50 VARIANTARG
* ps
, VARTYPE vt
)
52 HRESULT res
= DISP_E_TYPEMISMATCH
;
53 VARTYPE vtFrom
= V_TYPE(ps
);
56 TRACE("(%s,0x%08x,0x%04x,%s,%s)\n", debugstr_variant(pd
), lcid
, wFlags
,
57 debugstr_variant(ps
), debugstr_vt(vt
));
59 if (vt
== VT_BSTR
|| vtFrom
== VT_BSTR
)
61 /* All flags passed to low level function are only used for
62 * changing to or from strings. Map these here.
64 if (wFlags
& VARIANT_LOCALBOOL
)
65 dwFlags
|= VAR_LOCALBOOL
;
66 if (wFlags
& VARIANT_CALENDAR_HIJRI
)
67 dwFlags
|= VAR_CALENDAR_HIJRI
;
68 if (wFlags
& VARIANT_CALENDAR_THAI
)
69 dwFlags
|= VAR_CALENDAR_THAI
;
70 if (wFlags
& VARIANT_CALENDAR_GREGORIAN
)
71 dwFlags
|= VAR_CALENDAR_GREGORIAN
;
72 if (wFlags
& VARIANT_NOUSEROVERRIDE
)
73 dwFlags
|= LOCALE_NOUSEROVERRIDE
;
74 if (wFlags
& VARIANT_USE_NLS
)
75 dwFlags
|= LOCALE_USE_NLS
;
78 /* Map int/uint to i4/ui4 */
81 else if (vt
== VT_UINT
)
86 else if (vtFrom
== VT_UINT
)
90 return VariantCopy(pd
, ps
);
92 if (wFlags
& VARIANT_NOVALUEPROP
&& vtFrom
== VT_DISPATCH
&& vt
!= VT_UNKNOWN
)
94 /* VARIANT_NOVALUEPROP prevents IDispatch objects from being coerced by
95 * accessing the default object property.
97 return DISP_E_TYPEMISMATCH
;
103 if (vtFrom
== VT_NULL
)
104 return DISP_E_TYPEMISMATCH
;
105 /* ... Fall through */
107 if (vtFrom
<= VT_UINT
&& vtFrom
!= (VARTYPE
)15 && vtFrom
!= VT_ERROR
)
109 res
= VariantClear( pd
);
110 if (vt
== VT_NULL
&& SUCCEEDED(res
))
118 case VT_EMPTY
: V_I1(pd
) = 0; return S_OK
;
119 case VT_I2
: return VarI1FromI2(V_I2(ps
), &V_I1(pd
));
120 case VT_I4
: return VarI1FromI4(V_I4(ps
), &V_I1(pd
));
121 case VT_UI1
: V_I1(pd
) = V_UI1(ps
); return S_OK
;
122 case VT_UI2
: return VarI1FromUI2(V_UI2(ps
), &V_I1(pd
));
123 case VT_UI4
: return VarI1FromUI4(V_UI4(ps
), &V_I1(pd
));
124 case VT_I8
: return VarI1FromI8(V_I8(ps
), &V_I1(pd
));
125 case VT_UI8
: return VarI1FromUI8(V_UI8(ps
), &V_I1(pd
));
126 case VT_R4
: return VarI1FromR4(V_R4(ps
), &V_I1(pd
));
127 case VT_R8
: return VarI1FromR8(V_R8(ps
), &V_I1(pd
));
128 case VT_DATE
: return VarI1FromDate(V_DATE(ps
), &V_I1(pd
));
129 case VT_BOOL
: return VarI1FromBool(V_BOOL(ps
), &V_I1(pd
));
130 case VT_CY
: return VarI1FromCy(V_CY(ps
), &V_I1(pd
));
131 case VT_DECIMAL
: return VarI1FromDec(&V_DECIMAL(ps
), &V_I1(pd
) );
132 case VT_DISPATCH
: return VarI1FromDisp(V_DISPATCH(ps
), lcid
, &V_I1(pd
) );
133 case VT_BSTR
: return VarI1FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I1(pd
) );
140 case VT_EMPTY
: V_I2(pd
) = 0; return S_OK
;
141 case VT_I1
: return VarI2FromI1(V_I1(ps
), &V_I2(pd
));
142 case VT_I4
: return VarI2FromI4(V_I4(ps
), &V_I2(pd
));
143 case VT_UI1
: return VarI2FromUI1(V_UI1(ps
), &V_I2(pd
));
144 case VT_UI2
: V_I2(pd
) = V_UI2(ps
); return S_OK
;
145 case VT_UI4
: return VarI2FromUI4(V_UI4(ps
), &V_I2(pd
));
146 case VT_I8
: return VarI2FromI8(V_I8(ps
), &V_I2(pd
));
147 case VT_UI8
: return VarI2FromUI8(V_UI8(ps
), &V_I2(pd
));
148 case VT_R4
: return VarI2FromR4(V_R4(ps
), &V_I2(pd
));
149 case VT_R8
: return VarI2FromR8(V_R8(ps
), &V_I2(pd
));
150 case VT_DATE
: return VarI2FromDate(V_DATE(ps
), &V_I2(pd
));
151 case VT_BOOL
: return VarI2FromBool(V_BOOL(ps
), &V_I2(pd
));
152 case VT_CY
: return VarI2FromCy(V_CY(ps
), &V_I2(pd
));
153 case VT_DECIMAL
: return VarI2FromDec(&V_DECIMAL(ps
), &V_I2(pd
));
154 case VT_DISPATCH
: return VarI2FromDisp(V_DISPATCH(ps
), lcid
, &V_I2(pd
));
155 case VT_BSTR
: return VarI2FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I2(pd
));
162 case VT_EMPTY
: V_I4(pd
) = 0; return S_OK
;
163 case VT_I1
: return VarI4FromI1(V_I1(ps
), &V_I4(pd
));
164 case VT_I2
: return VarI4FromI2(V_I2(ps
), &V_I4(pd
));
165 case VT_UI1
: return VarI4FromUI1(V_UI1(ps
), &V_I4(pd
));
166 case VT_UI2
: return VarI4FromUI2(V_UI2(ps
), &V_I4(pd
));
167 case VT_UI4
: V_I4(pd
) = V_UI4(ps
); return S_OK
;
168 case VT_I8
: return VarI4FromI8(V_I8(ps
), &V_I4(pd
));
169 case VT_UI8
: return VarI4FromUI8(V_UI8(ps
), &V_I4(pd
));
170 case VT_R4
: return VarI4FromR4(V_R4(ps
), &V_I4(pd
));
171 case VT_R8
: return VarI4FromR8(V_R8(ps
), &V_I4(pd
));
172 case VT_DATE
: return VarI4FromDate(V_DATE(ps
), &V_I4(pd
));
173 case VT_BOOL
: return VarI4FromBool(V_BOOL(ps
), &V_I4(pd
));
174 case VT_CY
: return VarI4FromCy(V_CY(ps
), &V_I4(pd
));
175 case VT_DECIMAL
: return VarI4FromDec(&V_DECIMAL(ps
), &V_I4(pd
));
176 case VT_DISPATCH
: return VarI4FromDisp(V_DISPATCH(ps
), lcid
, &V_I4(pd
));
177 case VT_BSTR
: return VarI4FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I4(pd
));
184 case VT_EMPTY
: V_UI1(pd
) = 0; return S_OK
;
185 case VT_I1
: V_UI1(pd
) = V_I1(ps
); return S_OK
;
186 case VT_I2
: return VarUI1FromI2(V_I2(ps
), &V_UI1(pd
));
187 case VT_I4
: return VarUI1FromI4(V_I4(ps
), &V_UI1(pd
));
188 case VT_UI2
: return VarUI1FromUI2(V_UI2(ps
), &V_UI1(pd
));
189 case VT_UI4
: return VarUI1FromUI4(V_UI4(ps
), &V_UI1(pd
));
190 case VT_I8
: return VarUI1FromI8(V_I8(ps
), &V_UI1(pd
));
191 case VT_UI8
: return VarUI1FromUI8(V_UI8(ps
), &V_UI1(pd
));
192 case VT_R4
: return VarUI1FromR4(V_R4(ps
), &V_UI1(pd
));
193 case VT_R8
: return VarUI1FromR8(V_R8(ps
), &V_UI1(pd
));
194 case VT_DATE
: return VarUI1FromDate(V_DATE(ps
), &V_UI1(pd
));
195 case VT_BOOL
: return VarUI1FromBool(V_BOOL(ps
), &V_UI1(pd
));
196 case VT_CY
: return VarUI1FromCy(V_CY(ps
), &V_UI1(pd
));
197 case VT_DECIMAL
: return VarUI1FromDec(&V_DECIMAL(ps
), &V_UI1(pd
));
198 case VT_DISPATCH
: return VarUI1FromDisp(V_DISPATCH(ps
), lcid
, &V_UI1(pd
));
199 case VT_BSTR
: return VarUI1FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI1(pd
));
206 case VT_EMPTY
: V_UI2(pd
) = 0; return S_OK
;
207 case VT_I1
: return VarUI2FromI1(V_I1(ps
), &V_UI2(pd
));
208 case VT_I2
: V_UI2(pd
) = V_I2(ps
); return S_OK
;
209 case VT_I4
: return VarUI2FromI4(V_I4(ps
), &V_UI2(pd
));
210 case VT_UI1
: return VarUI2FromUI1(V_UI1(ps
), &V_UI2(pd
));
211 case VT_UI4
: return VarUI2FromUI4(V_UI4(ps
), &V_UI2(pd
));
212 case VT_I8
: return VarUI4FromI8(V_I8(ps
), &V_UI4(pd
));
213 case VT_UI8
: return VarUI4FromUI8(V_UI8(ps
), &V_UI4(pd
));
214 case VT_R4
: return VarUI2FromR4(V_R4(ps
), &V_UI2(pd
));
215 case VT_R8
: return VarUI2FromR8(V_R8(ps
), &V_UI2(pd
));
216 case VT_DATE
: return VarUI2FromDate(V_DATE(ps
), &V_UI2(pd
));
217 case VT_BOOL
: return VarUI2FromBool(V_BOOL(ps
), &V_UI2(pd
));
218 case VT_CY
: return VarUI2FromCy(V_CY(ps
), &V_UI2(pd
));
219 case VT_DECIMAL
: return VarUI2FromDec(&V_DECIMAL(ps
), &V_UI2(pd
));
220 case VT_DISPATCH
: return VarUI2FromDisp(V_DISPATCH(ps
), lcid
, &V_UI2(pd
));
221 case VT_BSTR
: return VarUI2FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI2(pd
));
228 case VT_EMPTY
: V_UI4(pd
) = 0; return S_OK
;
229 case VT_I1
: return VarUI4FromI1(V_I1(ps
), &V_UI4(pd
));
230 case VT_I2
: return VarUI4FromI2(V_I2(ps
), &V_UI4(pd
));
231 case VT_I4
: V_UI4(pd
) = V_I4(ps
); return S_OK
;
232 case VT_UI1
: return VarUI4FromUI1(V_UI1(ps
), &V_UI4(pd
));
233 case VT_UI2
: return VarUI4FromUI2(V_UI2(ps
), &V_UI4(pd
));
234 case VT_I8
: return VarUI4FromI8(V_I8(ps
), &V_UI4(pd
));
235 case VT_UI8
: return VarUI4FromUI8(V_UI8(ps
), &V_UI4(pd
));
236 case VT_R4
: return VarUI4FromR4(V_R4(ps
), &V_UI4(pd
));
237 case VT_R8
: return VarUI4FromR8(V_R8(ps
), &V_UI4(pd
));
238 case VT_DATE
: return VarUI4FromDate(V_DATE(ps
), &V_UI4(pd
));
239 case VT_BOOL
: return VarUI4FromBool(V_BOOL(ps
), &V_UI4(pd
));
240 case VT_CY
: return VarUI4FromCy(V_CY(ps
), &V_UI4(pd
));
241 case VT_DECIMAL
: return VarUI4FromDec(&V_DECIMAL(ps
), &V_UI4(pd
));
242 case VT_DISPATCH
: return VarUI4FromDisp(V_DISPATCH(ps
), lcid
, &V_UI4(pd
));
243 case VT_BSTR
: return VarUI4FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI4(pd
));
250 case VT_EMPTY
: V_UI8(pd
) = 0; return S_OK
;
251 case VT_I4
: if (V_I4(ps
) < 0) return DISP_E_OVERFLOW
; V_UI8(pd
) = V_I4(ps
); return S_OK
;
252 case VT_I1
: return VarUI8FromI1(V_I1(ps
), &V_UI8(pd
));
253 case VT_I2
: return VarUI8FromI2(V_I2(ps
), &V_UI8(pd
));
254 case VT_UI1
: return VarUI8FromUI1(V_UI1(ps
), &V_UI8(pd
));
255 case VT_UI2
: return VarUI8FromUI2(V_UI2(ps
), &V_UI8(pd
));
256 case VT_UI4
: return VarUI8FromUI4(V_UI4(ps
), &V_UI8(pd
));
257 case VT_I8
: V_UI8(pd
) = V_I8(ps
); return S_OK
;
258 case VT_R4
: return VarUI8FromR4(V_R4(ps
), &V_UI8(pd
));
259 case VT_R8
: return VarUI8FromR8(V_R8(ps
), &V_UI8(pd
));
260 case VT_DATE
: return VarUI8FromDate(V_DATE(ps
), &V_UI8(pd
));
261 case VT_BOOL
: return VarUI8FromBool(V_BOOL(ps
), &V_UI8(pd
));
262 case VT_CY
: return VarUI8FromCy(V_CY(ps
), &V_UI8(pd
));
263 case VT_DECIMAL
: return VarUI8FromDec(&V_DECIMAL(ps
), &V_UI8(pd
));
264 case VT_DISPATCH
: return VarUI8FromDisp(V_DISPATCH(ps
), lcid
, &V_UI8(pd
));
265 case VT_BSTR
: return VarUI8FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI8(pd
));
272 case VT_EMPTY
: V_I8(pd
) = 0; return S_OK
;
273 case VT_I4
: V_I8(pd
) = V_I4(ps
); return S_OK
;
274 case VT_I1
: return VarI8FromI1(V_I1(ps
), &V_I8(pd
));
275 case VT_I2
: return VarI8FromI2(V_I2(ps
), &V_I8(pd
));
276 case VT_UI1
: return VarI8FromUI1(V_UI1(ps
), &V_I8(pd
));
277 case VT_UI2
: return VarI8FromUI2(V_UI2(ps
), &V_I8(pd
));
278 case VT_UI4
: return VarI8FromUI4(V_UI4(ps
), &V_I8(pd
));
279 case VT_UI8
: V_I8(pd
) = V_UI8(ps
); return S_OK
;
280 case VT_R4
: return VarI8FromR4(V_R4(ps
), &V_I8(pd
));
281 case VT_R8
: return VarI8FromR8(V_R8(ps
), &V_I8(pd
));
282 case VT_DATE
: return VarI8FromDate(V_DATE(ps
), &V_I8(pd
));
283 case VT_BOOL
: return VarI8FromBool(V_BOOL(ps
), &V_I8(pd
));
284 case VT_CY
: return VarI8FromCy(V_CY(ps
), &V_I8(pd
));
285 case VT_DECIMAL
: return VarI8FromDec(&V_DECIMAL(ps
), &V_I8(pd
));
286 case VT_DISPATCH
: return VarI8FromDisp(V_DISPATCH(ps
), lcid
, &V_I8(pd
));
287 case VT_BSTR
: return VarI8FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I8(pd
));
294 case VT_EMPTY
: V_R4(pd
) = 0.0f
; return S_OK
;
295 case VT_I1
: return VarR4FromI1(V_I1(ps
), &V_R4(pd
));
296 case VT_I2
: return VarR4FromI2(V_I2(ps
), &V_R4(pd
));
297 case VT_I4
: return VarR4FromI4(V_I4(ps
), &V_R4(pd
));
298 case VT_UI1
: return VarR4FromUI1(V_UI1(ps
), &V_R4(pd
));
299 case VT_UI2
: return VarR4FromUI2(V_UI2(ps
), &V_R4(pd
));
300 case VT_UI4
: return VarR4FromUI4(V_UI4(ps
), &V_R4(pd
));
301 case VT_I8
: return VarR4FromI8(V_I8(ps
), &V_R4(pd
));
302 case VT_UI8
: return VarR4FromUI8(V_UI8(ps
), &V_R4(pd
));
303 case VT_R8
: return VarR4FromR8(V_R8(ps
), &V_R4(pd
));
304 case VT_DATE
: return VarR4FromDate(V_DATE(ps
), &V_R4(pd
));
305 case VT_BOOL
: return VarR4FromBool(V_BOOL(ps
), &V_R4(pd
));
306 case VT_CY
: return VarR4FromCy(V_CY(ps
), &V_R4(pd
));
307 case VT_DECIMAL
: return VarR4FromDec(&V_DECIMAL(ps
), &V_R4(pd
));
308 case VT_DISPATCH
: return VarR4FromDisp(V_DISPATCH(ps
), lcid
, &V_R4(pd
));
309 case VT_BSTR
: return VarR4FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_R4(pd
));
316 case VT_EMPTY
: V_R8(pd
) = 0.0; return S_OK
;
317 case VT_I1
: return VarR8FromI1(V_I1(ps
), &V_R8(pd
));
318 case VT_I2
: return VarR8FromI2(V_I2(ps
), &V_R8(pd
));
319 case VT_I4
: return VarR8FromI4(V_I4(ps
), &V_R8(pd
));
320 case VT_UI1
: return VarR8FromUI1(V_UI1(ps
), &V_R8(pd
));
321 case VT_UI2
: return VarR8FromUI2(V_UI2(ps
), &V_R8(pd
));
322 case VT_UI4
: return VarR8FromUI4(V_UI4(ps
), &V_R8(pd
));
323 case VT_I8
: return VarR8FromI8(V_I8(ps
), &V_R8(pd
));
324 case VT_UI8
: return VarR8FromUI8(V_UI8(ps
), &V_R8(pd
));
325 case VT_R4
: return VarR8FromR4(V_R4(ps
), &V_R8(pd
));
326 case VT_DATE
: return VarR8FromDate(V_DATE(ps
), &V_R8(pd
));
327 case VT_BOOL
: return VarR8FromBool(V_BOOL(ps
), &V_R8(pd
));
328 case VT_CY
: return VarR8FromCy(V_CY(ps
), &V_R8(pd
));
329 case VT_DECIMAL
: return VarR8FromDec(&V_DECIMAL(ps
), &V_R8(pd
));
330 case VT_DISPATCH
: return VarR8FromDisp(V_DISPATCH(ps
), lcid
, &V_R8(pd
));
331 case VT_BSTR
: return VarR8FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_R8(pd
));
338 case VT_EMPTY
: V_DATE(pd
) = 0.0; return S_OK
;
339 case VT_I1
: return VarDateFromI1(V_I1(ps
), &V_DATE(pd
));
340 case VT_I2
: return VarDateFromI2(V_I2(ps
), &V_DATE(pd
));
341 case VT_I4
: return VarDateFromI4(V_I4(ps
), &V_DATE(pd
));
342 case VT_UI1
: return VarDateFromUI1(V_UI1(ps
), &V_DATE(pd
));
343 case VT_UI2
: return VarDateFromUI2(V_UI2(ps
), &V_DATE(pd
));
344 case VT_UI4
: return VarDateFromUI4(V_UI4(ps
), &V_DATE(pd
));
345 case VT_I8
: return VarDateFromI8(V_I8(ps
), &V_DATE(pd
));
346 case VT_UI8
: return VarDateFromUI8(V_UI8(ps
), &V_DATE(pd
));
347 case VT_R4
: return VarDateFromR4(V_R4(ps
), &V_DATE(pd
));
348 case VT_R8
: return VarDateFromR8(V_R8(ps
), &V_DATE(pd
));
349 case VT_BOOL
: return VarDateFromBool(V_BOOL(ps
), &V_DATE(pd
));
350 case VT_CY
: return VarDateFromCy(V_CY(ps
), &V_DATE(pd
));
351 case VT_DECIMAL
: return VarDateFromDec(&V_DECIMAL(ps
), &V_DATE(pd
));
352 case VT_DISPATCH
: return VarDateFromDisp(V_DISPATCH(ps
), lcid
, &V_DATE(pd
));
353 case VT_BSTR
: return VarDateFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_DATE(pd
));
360 case VT_EMPTY
: V_BOOL(pd
) = 0; return S_OK
;
361 case VT_I1
: return VarBoolFromI1(V_I1(ps
), &V_BOOL(pd
));
362 case VT_I2
: return VarBoolFromI2(V_I2(ps
), &V_BOOL(pd
));
363 case VT_I4
: return VarBoolFromI4(V_I4(ps
), &V_BOOL(pd
));
364 case VT_UI1
: return VarBoolFromUI1(V_UI1(ps
), &V_BOOL(pd
));
365 case VT_UI2
: return VarBoolFromUI2(V_UI2(ps
), &V_BOOL(pd
));
366 case VT_UI4
: return VarBoolFromUI4(V_UI4(ps
), &V_BOOL(pd
));
367 case VT_I8
: return VarBoolFromI8(V_I8(ps
), &V_BOOL(pd
));
368 case VT_UI8
: return VarBoolFromUI8(V_UI8(ps
), &V_BOOL(pd
));
369 case VT_R4
: return VarBoolFromR4(V_R4(ps
), &V_BOOL(pd
));
370 case VT_R8
: return VarBoolFromR8(V_R8(ps
), &V_BOOL(pd
));
371 case VT_DATE
: return VarBoolFromDate(V_DATE(ps
), &V_BOOL(pd
));
372 case VT_CY
: return VarBoolFromCy(V_CY(ps
), &V_BOOL(pd
));
373 case VT_DECIMAL
: return VarBoolFromDec(&V_DECIMAL(ps
), &V_BOOL(pd
));
374 case VT_DISPATCH
: return VarBoolFromDisp(V_DISPATCH(ps
), lcid
, &V_BOOL(pd
));
375 case VT_BSTR
: return VarBoolFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_BOOL(pd
));
383 V_BSTR(pd
) = SysAllocStringLen(NULL
, 0);
384 return V_BSTR(pd
) ? S_OK
: E_OUTOFMEMORY
;
386 if (wFlags
& (VARIANT_ALPHABOOL
|VARIANT_LOCALBOOL
))
387 return VarBstrFromBool(V_BOOL(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
388 return VarBstrFromI2(V_BOOL(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
389 case VT_I1
: return VarBstrFromI1(V_I1(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
390 case VT_I2
: return VarBstrFromI2(V_I2(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
391 case VT_I4
: return VarBstrFromI4(V_I4(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
392 case VT_UI1
: return VarBstrFromUI1(V_UI1(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
393 case VT_UI2
: return VarBstrFromUI2(V_UI2(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
394 case VT_UI4
: return VarBstrFromUI4(V_UI4(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
395 case VT_I8
: return VarBstrFromI8(V_I8(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
396 case VT_UI8
: return VarBstrFromUI8(V_UI8(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
397 case VT_R4
: return VarBstrFromR4(V_R4(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
398 case VT_R8
: return VarBstrFromR8(V_R8(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
399 case VT_DATE
: return VarBstrFromDate(V_DATE(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
400 case VT_CY
: return VarBstrFromCy(V_CY(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
401 case VT_DECIMAL
: return VarBstrFromDec(&V_DECIMAL(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
402 case VT_DISPATCH
: return VarBstrFromDisp(V_DISPATCH(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
409 case VT_EMPTY
: V_CY(pd
).int64
= 0; return S_OK
;
410 case VT_I1
: return VarCyFromI1(V_I1(ps
), &V_CY(pd
));
411 case VT_I2
: return VarCyFromI2(V_I2(ps
), &V_CY(pd
));
412 case VT_I4
: return VarCyFromI4(V_I4(ps
), &V_CY(pd
));
413 case VT_UI1
: return VarCyFromUI1(V_UI1(ps
), &V_CY(pd
));
414 case VT_UI2
: return VarCyFromUI2(V_UI2(ps
), &V_CY(pd
));
415 case VT_UI4
: return VarCyFromUI4(V_UI4(ps
), &V_CY(pd
));
416 case VT_I8
: return VarCyFromI8(V_I8(ps
), &V_CY(pd
));
417 case VT_UI8
: return VarCyFromUI8(V_UI8(ps
), &V_CY(pd
));
418 case VT_R4
: return VarCyFromR4(V_R4(ps
), &V_CY(pd
));
419 case VT_R8
: return VarCyFromR8(V_R8(ps
), &V_CY(pd
));
420 case VT_DATE
: return VarCyFromDate(V_DATE(ps
), &V_CY(pd
));
421 case VT_BOOL
: return VarCyFromBool(V_BOOL(ps
), &V_CY(pd
));
422 case VT_DECIMAL
: return VarCyFromDec(&V_DECIMAL(ps
), &V_CY(pd
));
423 case VT_DISPATCH
: return VarCyFromDisp(V_DISPATCH(ps
), lcid
, &V_CY(pd
));
424 case VT_BSTR
: return VarCyFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_CY(pd
));
433 DEC_SIGNSCALE(&V_DECIMAL(pd
)) = SIGNSCALE(DECIMAL_POS
,0);
434 DEC_HI32(&V_DECIMAL(pd
)) = 0;
435 DEC_MID32(&V_DECIMAL(pd
)) = 0;
436 /* VarDecFromBool() coerces to -1/0, ChangeTypeEx() coerces to 1/0.
437 * VT_NULL and VT_EMPTY always give a 0 value.
439 DEC_LO32(&V_DECIMAL(pd
)) = vtFrom
== VT_BOOL
&& V_BOOL(ps
) ? 1 : 0;
441 case VT_I1
: return VarDecFromI1(V_I1(ps
), &V_DECIMAL(pd
));
442 case VT_I2
: return VarDecFromI2(V_I2(ps
), &V_DECIMAL(pd
));
443 case VT_I4
: return VarDecFromI4(V_I4(ps
), &V_DECIMAL(pd
));
444 case VT_UI1
: return VarDecFromUI1(V_UI1(ps
), &V_DECIMAL(pd
));
445 case VT_UI2
: return VarDecFromUI2(V_UI2(ps
), &V_DECIMAL(pd
));
446 case VT_UI4
: return VarDecFromUI4(V_UI4(ps
), &V_DECIMAL(pd
));
447 case VT_I8
: return VarDecFromI8(V_I8(ps
), &V_DECIMAL(pd
));
448 case VT_UI8
: return VarDecFromUI8(V_UI8(ps
), &V_DECIMAL(pd
));
449 case VT_R4
: return VarDecFromR4(V_R4(ps
), &V_DECIMAL(pd
));
450 case VT_R8
: return VarDecFromR8(V_R8(ps
), &V_DECIMAL(pd
));
451 case VT_DATE
: return VarDecFromDate(V_DATE(ps
), &V_DECIMAL(pd
));
452 case VT_CY
: return VarDecFromCy(V_CY(ps
), &V_DECIMAL(pd
));
453 case VT_DISPATCH
: return VarDecFromDisp(V_DISPATCH(ps
), lcid
, &V_DECIMAL(pd
));
454 case VT_BSTR
: return VarDecFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_DECIMAL(pd
));
462 if (V_DISPATCH(ps
) == NULL
)
463 V_UNKNOWN(pd
) = NULL
;
465 res
= IDispatch_QueryInterface(V_DISPATCH(ps
), &IID_IUnknown
, (LPVOID
*)&V_UNKNOWN(pd
));
474 if (V_UNKNOWN(ps
) == NULL
)
475 V_DISPATCH(pd
) = NULL
;
477 res
= IUnknown_QueryInterface(V_UNKNOWN(ps
), &IID_IDispatch
, (LPVOID
*)&V_DISPATCH(pd
));
488 /* Coerce to/from an array */
489 static inline HRESULT
VARIANT_CoerceArray(VARIANTARG
* pd
, VARIANTARG
* ps
, VARTYPE vt
)
491 if (vt
== VT_BSTR
&& V_VT(ps
) == (VT_ARRAY
|VT_UI1
))
492 return BstrFromVector(V_ARRAY(ps
), &V_BSTR(pd
));
494 if (V_VT(ps
) == VT_BSTR
&& vt
== (VT_ARRAY
|VT_UI1
))
495 return VectorFromBstr(V_BSTR(ps
), &V_ARRAY(pd
));
498 return SafeArrayCopy(V_ARRAY(ps
), &V_ARRAY(pd
));
500 return DISP_E_TYPEMISMATCH
;
503 /******************************************************************************
504 * Check if a variants type is valid.
506 static inline HRESULT
VARIANT_ValidateType(VARTYPE vt
)
508 VARTYPE vtExtra
= vt
& VT_EXTRA_TYPE
;
512 if (!(vtExtra
& (VT_VECTOR
|VT_RESERVED
)))
514 if (vt
< VT_VOID
|| vt
== VT_RECORD
|| vt
== VT_CLSID
)
516 if ((vtExtra
& (VT_BYREF
|VT_ARRAY
)) && vt
<= VT_NULL
)
517 return DISP_E_BADVARTYPE
;
518 if (vt
!= (VARTYPE
)15)
522 return DISP_E_BADVARTYPE
;
525 /******************************************************************************
526 * VariantInit [OLEAUT32.8]
528 * Initialise a variant.
531 * pVarg [O] Variant to initialise
537 * This function simply sets the type of the variant to VT_EMPTY. It does not
538 * free any existing value, use VariantClear() for that.
540 void WINAPI
VariantInit(VARIANTARG
* pVarg
)
542 TRACE("(%p)\n", pVarg
);
544 /* Win8.1 zeroes whole struct. Previous implementations don't set any other fields. */
545 V_VT(pVarg
) = VT_EMPTY
;
548 HRESULT
VARIANT_ClearInd(VARIANTARG
*pVarg
)
552 TRACE("(%s)\n", debugstr_variant(pVarg
));
554 hres
= VARIANT_ValidateType(V_VT(pVarg
));
562 if (V_UNKNOWN(pVarg
))
563 IUnknown_Release(V_UNKNOWN(pVarg
));
565 case VT_UNKNOWN
| VT_BYREF
:
566 case VT_DISPATCH
| VT_BYREF
:
567 if(*V_UNKNOWNREF(pVarg
))
568 IUnknown_Release(*V_UNKNOWNREF(pVarg
));
571 SysFreeString(V_BSTR(pVarg
));
573 case VT_BSTR
| VT_BYREF
:
574 SysFreeString(*V_BSTRREF(pVarg
));
576 case VT_VARIANT
| VT_BYREF
:
577 VariantClear(V_VARIANTREF(pVarg
));
580 case VT_RECORD
| VT_BYREF
:
582 struct __tagBRECORD
* pBr
= &V_UNION(pVarg
,brecVal
);
585 IRecordInfo_RecordClear(pBr
->pRecInfo
, pBr
->pvRecord
);
586 IRecordInfo_Release(pBr
->pRecInfo
);
591 if (V_ISARRAY(pVarg
) || (V_VT(pVarg
) & ~VT_BYREF
) == VT_SAFEARRAY
)
593 if (V_ISBYREF(pVarg
))
595 if (*V_ARRAYREF(pVarg
))
596 hres
= SafeArrayDestroy(*V_ARRAYREF(pVarg
));
598 else if (V_ARRAY(pVarg
))
599 hres
= SafeArrayDestroy(V_ARRAY(pVarg
));
604 V_VT(pVarg
) = VT_EMPTY
;
608 /******************************************************************************
609 * VariantClear [OLEAUT32.9]
614 * pVarg [I/O] Variant to clear
617 * Success: S_OK. Any previous value in pVarg is freed and its type is set to VT_EMPTY.
618 * Failure: DISP_E_BADVARTYPE, if the variant is not a valid variant type.
620 HRESULT WINAPI
VariantClear(VARIANTARG
* pVarg
)
624 TRACE("(%s)\n", debugstr_variant(pVarg
));
626 hres
= VARIANT_ValidateType(V_VT(pVarg
));
630 if (!V_ISBYREF(pVarg
))
632 if (V_ISARRAY(pVarg
) || V_VT(pVarg
) == VT_SAFEARRAY
)
634 hres
= SafeArrayDestroy(V_ARRAY(pVarg
));
636 else if (V_VT(pVarg
) == VT_BSTR
)
638 SysFreeString(V_BSTR(pVarg
));
640 else if (V_VT(pVarg
) == VT_RECORD
)
642 struct __tagBRECORD
* pBr
= &V_UNION(pVarg
,brecVal
);
645 IRecordInfo_RecordClear(pBr
->pRecInfo
, pBr
->pvRecord
);
646 IRecordInfo_Release(pBr
->pRecInfo
);
649 else if (V_VT(pVarg
) == VT_DISPATCH
||
650 V_VT(pVarg
) == VT_UNKNOWN
)
652 if (V_UNKNOWN(pVarg
))
653 IUnknown_Release(V_UNKNOWN(pVarg
));
656 V_VT(pVarg
) = VT_EMPTY
;
661 /******************************************************************************
662 * Copy an IRecordInfo object contained in a variant.
664 static HRESULT
VARIANT_CopyIRecordInfo(VARIANT
*dest
, VARIANT
*src
)
666 struct __tagBRECORD
*dest_rec
= &V_UNION(dest
, brecVal
);
667 struct __tagBRECORD
*src_rec
= &V_UNION(src
, brecVal
);
671 if (!src_rec
->pRecInfo
)
673 if (src_rec
->pvRecord
) return E_INVALIDARG
;
677 hr
= IRecordInfo_GetSize(src_rec
->pRecInfo
, &size
);
678 if (FAILED(hr
)) return hr
;
680 /* This could look cleaner if only RecordCreate() was used, but native doesn't use it.
681 Memory should be allocated in a same way as RecordCreate() does, so RecordDestroy()
682 could free it later. */
683 dest_rec
->pvRecord
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, size
);
684 if (!dest_rec
->pvRecord
) return E_OUTOFMEMORY
;
686 dest_rec
->pRecInfo
= src_rec
->pRecInfo
;
687 IRecordInfo_AddRef(src_rec
->pRecInfo
);
689 return IRecordInfo_RecordCopy(src_rec
->pRecInfo
, src_rec
->pvRecord
, dest_rec
->pvRecord
);
692 /******************************************************************************
693 * VariantCopy [OLEAUT32.10]
698 * pvargDest [O] Destination for copy
699 * pvargSrc [I] Source variant to copy
702 * Success: S_OK. pvargDest contains a copy of pvargSrc.
703 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid type.
704 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
705 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
706 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
709 * - If pvargSrc == pvargDest, this function does nothing, and succeeds if
710 * pvargSrc is valid. Otherwise, pvargDest is always cleared using
711 * VariantClear() before pvargSrc is copied to it. If clearing pvargDest
712 * fails, so does this function.
713 * - VT_CLSID is a valid type type for pvargSrc, but not for pvargDest.
714 * - For by-value non-intrinsic types, a deep copy is made, i.e. The whole value
715 * is copied rather than just any pointers to it.
716 * - For by-value object types the object pointer is copied and the objects
717 * reference count increased using IUnknown_AddRef().
718 * - For all by-reference types, only the referencing pointer is copied.
720 HRESULT WINAPI
VariantCopy(VARIANTARG
* pvargDest
, VARIANTARG
* pvargSrc
)
724 TRACE("(%s,%s)\n", debugstr_variant(pvargDest
), debugstr_variant(pvargSrc
));
726 if (V_TYPE(pvargSrc
) == VT_CLSID
|| /* VT_CLSID is a special case */
727 FAILED(VARIANT_ValidateType(V_VT(pvargSrc
))))
728 return DISP_E_BADVARTYPE
;
730 if (pvargSrc
!= pvargDest
&&
731 SUCCEEDED(hres
= VariantClear(pvargDest
)))
733 *pvargDest
= *pvargSrc
; /* Shallow copy the value */
735 if (!V_ISBYREF(pvargSrc
))
737 switch (V_VT(pvargSrc
))
740 V_BSTR(pvargDest
) = SysAllocStringByteLen((char*)V_BSTR(pvargSrc
), SysStringByteLen(V_BSTR(pvargSrc
)));
741 if (!V_BSTR(pvargDest
))
742 hres
= E_OUTOFMEMORY
;
745 hres
= VARIANT_CopyIRecordInfo(pvargDest
, pvargSrc
);
749 V_UNKNOWN(pvargDest
) = V_UNKNOWN(pvargSrc
);
750 if (V_UNKNOWN(pvargSrc
))
751 IUnknown_AddRef(V_UNKNOWN(pvargSrc
));
754 if (V_ISARRAY(pvargSrc
))
755 hres
= SafeArrayCopy(V_ARRAY(pvargSrc
), &V_ARRAY(pvargDest
));
762 /* Return the byte size of a variants data */
763 static inline size_t VARIANT_DataSize(const VARIANT
* pv
)
768 case VT_UI1
: return sizeof(BYTE
);
770 case VT_UI2
: return sizeof(SHORT
);
774 case VT_UI4
: return sizeof(LONG
);
776 case VT_UI8
: return sizeof(LONGLONG
);
777 case VT_R4
: return sizeof(float);
778 case VT_R8
: return sizeof(double);
779 case VT_DATE
: return sizeof(DATE
);
780 case VT_BOOL
: return sizeof(VARIANT_BOOL
);
783 case VT_BSTR
: return sizeof(void*);
784 case VT_CY
: return sizeof(CY
);
785 case VT_ERROR
: return sizeof(SCODE
);
787 TRACE("Shouldn't be called for variant %s!\n", debugstr_variant(pv
));
791 /******************************************************************************
792 * VariantCopyInd [OLEAUT32.11]
794 * Copy a variant, dereferencing it if it is by-reference.
797 * pvargDest [O] Destination for copy
798 * pvargSrc [I] Source variant to copy
801 * Success: S_OK. pvargDest contains a copy of pvargSrc.
802 * Failure: An HRESULT error code indicating the error.
805 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid by-value type.
806 * E_INVALIDARG, if pvargSrc is an invalid by-reference type.
807 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
808 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
809 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
812 * - If pvargSrc is by-value, this function behaves exactly as VariantCopy().
813 * - If pvargSrc is by-reference, the value copied to pvargDest is the pointed-to
815 * - if pvargSrc == pvargDest, this function dereferences in place. Otherwise,
816 * pvargDest is always cleared using VariantClear() before pvargSrc is copied
817 * to it. If clearing pvargDest fails, so does this function.
819 HRESULT WINAPI
VariantCopyInd(VARIANT
* pvargDest
, VARIANTARG
* pvargSrc
)
821 VARIANTARG vTmp
, *pSrc
= pvargSrc
;
825 TRACE("(%s,%s)\n", debugstr_variant(pvargDest
), debugstr_variant(pvargSrc
));
827 if (!V_ISBYREF(pvargSrc
))
828 return VariantCopy(pvargDest
, pvargSrc
);
830 /* Argument checking is more lax than VariantCopy()... */
831 vt
= V_TYPE(pvargSrc
);
832 if (V_ISARRAY(pvargSrc
) || (V_VT(pvargSrc
) == (VT_RECORD
|VT_BYREF
)) ||
833 (vt
> VT_NULL
&& vt
!= (VARTYPE
)15 && vt
< VT_VOID
&&
834 !(V_VT(pvargSrc
) & (VT_VECTOR
|VT_RESERVED
))))
839 return E_INVALIDARG
; /* ...And the return value for invalid types differs too */
841 if (pvargSrc
== pvargDest
)
843 /* In place copy. Use a shallow copy of pvargSrc & init pvargDest.
844 * This avoids an expensive VariantCopy() call - e.g. SafeArrayCopy().
848 V_VT(pvargDest
) = VT_EMPTY
;
852 /* Copy into another variant. Free the variant in pvargDest */
853 if (FAILED(hres
= VariantClear(pvargDest
)))
855 TRACE("VariantClear() of destination failed\n");
862 /* Native doesn't check that *V_ARRAYREF(pSrc) is valid */
863 hres
= SafeArrayCopy(*V_ARRAYREF(pSrc
), &V_ARRAY(pvargDest
));
865 else if (V_VT(pSrc
) == (VT_BSTR
|VT_BYREF
))
867 /* Native doesn't check that *V_BSTRREF(pSrc) is valid */
868 V_BSTR(pvargDest
) = SysAllocStringByteLen((char*)*V_BSTRREF(pSrc
), SysStringByteLen(*V_BSTRREF(pSrc
)));
870 else if (V_VT(pSrc
) == (VT_RECORD
|VT_BYREF
))
872 hres
= VARIANT_CopyIRecordInfo(pvargDest
, pvargSrc
);
874 else if (V_VT(pSrc
) == (VT_DISPATCH
|VT_BYREF
) ||
875 V_VT(pSrc
) == (VT_UNKNOWN
|VT_BYREF
))
877 /* Native doesn't check that *V_UNKNOWNREF(pSrc) is valid */
878 V_UNKNOWN(pvargDest
) = *V_UNKNOWNREF(pSrc
);
879 if (*V_UNKNOWNREF(pSrc
))
880 IUnknown_AddRef(*V_UNKNOWNREF(pSrc
));
882 else if (V_VT(pSrc
) == (VT_VARIANT
|VT_BYREF
))
884 /* Native doesn't check that *V_VARIANTREF(pSrc) is valid */
885 if (V_VT(V_VARIANTREF(pSrc
)) == (VT_VARIANT
|VT_BYREF
))
886 hres
= E_INVALIDARG
; /* Don't dereference more than one level */
888 hres
= VariantCopyInd(pvargDest
, V_VARIANTREF(pSrc
));
890 /* Use the dereferenced variants type value, not VT_VARIANT */
891 goto VariantCopyInd_Return
;
893 else if (V_VT(pSrc
) == (VT_DECIMAL
|VT_BYREF
))
895 memcpy(&DEC_SCALE(&V_DECIMAL(pvargDest
)), &DEC_SCALE(V_DECIMALREF(pSrc
)),
896 sizeof(DECIMAL
) - sizeof(USHORT
));
900 /* Copy the pointed to data into this variant */
901 memcpy(&V_BYREF(pvargDest
), V_BYREF(pSrc
), VARIANT_DataSize(pSrc
));
904 V_VT(pvargDest
) = V_VT(pSrc
) & ~VT_BYREF
;
906 VariantCopyInd_Return
:
908 if (pSrc
!= pvargSrc
)
911 TRACE("returning 0x%08x, %s\n", hres
, debugstr_variant(pvargDest
));
915 /******************************************************************************
916 * VariantChangeType [OLEAUT32.12]
918 * Change the type of a variant.
921 * pvargDest [O] Destination for the converted variant
922 * pvargSrc [O] Source variant to change the type of
923 * wFlags [I] VARIANT_ flags from "oleauto.h"
924 * vt [I] Variant type to change pvargSrc into
927 * Success: S_OK. pvargDest contains the converted value.
928 * Failure: An HRESULT error code describing the failure.
931 * The LCID used for the conversion is LOCALE_USER_DEFAULT.
932 * See VariantChangeTypeEx.
934 HRESULT WINAPI
VariantChangeType(VARIANTARG
* pvargDest
, VARIANTARG
* pvargSrc
,
935 USHORT wFlags
, VARTYPE vt
)
937 return VariantChangeTypeEx( pvargDest
, pvargSrc
, LOCALE_USER_DEFAULT
, wFlags
, vt
);
940 /******************************************************************************
941 * VariantChangeTypeEx [OLEAUT32.147]
943 * Change the type of a variant.
946 * pvargDest [O] Destination for the converted variant
947 * pvargSrc [O] Source variant to change the type of
948 * lcid [I] LCID for the conversion
949 * wFlags [I] VARIANT_ flags from "oleauto.h"
950 * vt [I] Variant type to change pvargSrc into
953 * Success: S_OK. pvargDest contains the converted value.
954 * Failure: An HRESULT error code describing the failure.
957 * pvargDest and pvargSrc can point to the same variant to perform an in-place
958 * conversion. If the conversion is successful, pvargSrc will be freed.
960 HRESULT WINAPI
VariantChangeTypeEx(VARIANTARG
* pvargDest
, VARIANTARG
* pvargSrc
,
961 LCID lcid
, USHORT wFlags
, VARTYPE vt
)
965 TRACE("(%s,%s,0x%08x,0x%04x,%s)\n", debugstr_variant(pvargDest
),
966 debugstr_variant(pvargSrc
), lcid
, wFlags
, debugstr_vt(vt
));
969 res
= DISP_E_BADVARTYPE
;
972 res
= VARIANT_ValidateType(V_VT(pvargSrc
));
976 res
= VARIANT_ValidateType(vt
);
980 VARIANTARG vTmp
, vSrcDeref
;
982 if(V_ISBYREF(pvargSrc
) && !V_BYREF(pvargSrc
))
983 res
= DISP_E_TYPEMISMATCH
;
986 V_VT(&vTmp
) = VT_EMPTY
;
987 V_VT(&vSrcDeref
) = VT_EMPTY
;
989 VariantClear(&vSrcDeref
);
994 res
= VariantCopyInd(&vSrcDeref
, pvargSrc
);
997 if (V_ISARRAY(&vSrcDeref
) || (vt
& VT_ARRAY
))
998 res
= VARIANT_CoerceArray(&vTmp
, &vSrcDeref
, vt
);
1000 res
= VARIANT_Coerce(&vTmp
, lcid
, wFlags
, &vSrcDeref
, vt
);
1002 if (SUCCEEDED(res
)) {
1004 res
= VariantCopy(pvargDest
, &vTmp
);
1006 VariantClear(&vTmp
);
1007 VariantClear(&vSrcDeref
);
1014 TRACE("returning 0x%08x, %s\n", res
, debugstr_variant(pvargDest
));
1018 /* Date Conversions */
1020 #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
1022 /* Convert a VT_DATE value to a Julian Date */
1023 static inline int VARIANT_JulianFromDate(int dateIn
)
1025 int julianDays
= dateIn
;
1027 julianDays
-= DATE_MIN
; /* Convert to + days from 1 Jan 100 AD */
1028 julianDays
+= 1757585; /* Convert to + days from 23 Nov 4713 BC (Julian) */
1032 /* Convert a Julian Date to a VT_DATE value */
1033 static inline int VARIANT_DateFromJulian(int dateIn
)
1035 int julianDays
= dateIn
;
1037 julianDays
-= 1757585; /* Convert to + days from 1 Jan 100 AD */
1038 julianDays
+= DATE_MIN
; /* Convert to +/- days from 1 Jan 1899 AD */
1042 /* Convert a Julian date to Day/Month/Year - from PostgreSQL */
1043 static inline void VARIANT_DMYFromJulian(int jd
, USHORT
*year
, USHORT
*month
, USHORT
*day
)
1049 l
-= (n
* 146097 + 3) / 4;
1050 i
= (4000 * (l
+ 1)) / 1461001;
1051 l
+= 31 - (i
* 1461) / 4;
1052 j
= (l
* 80) / 2447;
1053 *day
= l
- (j
* 2447) / 80;
1055 *month
= (j
+ 2) - (12 * l
);
1056 *year
= 100 * (n
- 49) + i
+ l
;
1059 /* Convert Day/Month/Year to a Julian date - from PostgreSQL */
1060 static inline double VARIANT_JulianFromDMY(USHORT year
, USHORT month
, USHORT day
)
1062 int m12
= (month
- 14) / 12;
1064 return ((1461 * (year
+ 4800 + m12
)) / 4 + (367 * (month
- 2 - 12 * m12
)) / 12 -
1065 (3 * ((year
+ 4900 + m12
) / 100)) / 4 + day
- 32075);
1068 /* Macros for accessing DOS format date/time fields */
1069 #define DOS_YEAR(x) (1980 + (x >> 9))
1070 #define DOS_MONTH(x) ((x >> 5) & 0xf)
1071 #define DOS_DAY(x) (x & 0x1f)
1072 #define DOS_HOUR(x) (x >> 11)
1073 #define DOS_MINUTE(x) ((x >> 5) & 0x3f)
1074 #define DOS_SECOND(x) ((x & 0x1f) << 1)
1075 /* Create a DOS format date/time */
1076 #define DOS_DATE(d,m,y) (d | (m << 5) | ((y-1980) << 9))
1077 #define DOS_TIME(h,m,s) ((s >> 1) | (m << 5) | (h << 11))
1079 /* Roll a date forwards or backwards to correct it */
1080 static HRESULT
VARIANT_RollUdate(UDATE
*lpUd
)
1082 static const BYTE days
[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1083 short iYear
, iMonth
, iDay
, iHour
, iMinute
, iSecond
;
1085 /* interpret values signed */
1086 iYear
= lpUd
->st
.wYear
;
1087 iMonth
= lpUd
->st
.wMonth
;
1088 iDay
= lpUd
->st
.wDay
;
1089 iHour
= lpUd
->st
.wHour
;
1090 iMinute
= lpUd
->st
.wMinute
;
1091 iSecond
= lpUd
->st
.wSecond
;
1093 TRACE("Raw date: %d/%d/%d %d:%d:%d\n", iDay
, iMonth
,
1094 iYear
, iHour
, iMinute
, iSecond
);
1096 if (iYear
> 9999 || iYear
< -9999)
1097 return E_INVALIDARG
; /* Invalid value */
1098 /* Year 0 to 29 are treated as 2000 + year */
1099 if (iYear
>= 0 && iYear
< 30)
1101 /* Remaining years < 100 are treated as 1900 + year */
1102 else if (iYear
>= 30 && iYear
< 100)
1105 iMinute
+= iSecond
/ 60;
1106 iSecond
= iSecond
% 60;
1107 iHour
+= iMinute
/ 60;
1108 iMinute
= iMinute
% 60;
1111 iYear
+= iMonth
/ 12;
1112 iMonth
= iMonth
% 12;
1113 if (iMonth
<=0) {iMonth
+=12; iYear
--;}
1114 while (iDay
> days
[iMonth
])
1116 if (iMonth
== 2 && IsLeapYear(iYear
))
1119 iDay
-= days
[iMonth
];
1121 iYear
+= iMonth
/ 12;
1122 iMonth
= iMonth
% 12;
1127 if (iMonth
<=0) {iMonth
+=12; iYear
--;}
1128 if (iMonth
== 2 && IsLeapYear(iYear
))
1131 iDay
+= days
[iMonth
];
1134 if (iSecond
<0){iSecond
+=60; iMinute
--;}
1135 if (iMinute
<0){iMinute
+=60; iHour
--;}
1136 if (iHour
<0) {iHour
+=24; iDay
--;}
1137 if (iYear
<=0) iYear
+=2000;
1139 lpUd
->st
.wYear
= iYear
;
1140 lpUd
->st
.wMonth
= iMonth
;
1141 lpUd
->st
.wDay
= iDay
;
1142 lpUd
->st
.wHour
= iHour
;
1143 lpUd
->st
.wMinute
= iMinute
;
1144 lpUd
->st
.wSecond
= iSecond
;
1146 TRACE("Rolled date: %d/%d/%d %d:%d:%d\n", lpUd
->st
.wDay
, lpUd
->st
.wMonth
,
1147 lpUd
->st
.wYear
, lpUd
->st
.wHour
, lpUd
->st
.wMinute
, lpUd
->st
.wSecond
);
1151 /**********************************************************************
1152 * DosDateTimeToVariantTime [OLEAUT32.14]
1154 * Convert a Dos format date and time into variant VT_DATE format.
1157 * wDosDate [I] Dos format date
1158 * wDosTime [I] Dos format time
1159 * pDateOut [O] Destination for VT_DATE format
1162 * Success: TRUE. pDateOut contains the converted time.
1163 * Failure: FALSE, if wDosDate or wDosTime are invalid (see notes).
1166 * - Dos format dates can only hold dates from 1-Jan-1980 to 31-Dec-2099.
1167 * - Dos format times are accurate to only 2 second precision.
1168 * - The format of a Dos Date is:
1169 *| Bits Values Meaning
1170 *| ---- ------ -------
1171 *| 0-4 1-31 Day of the week. 0 rolls back one day. A value greater than
1172 *| the days in the month rolls forward the extra days.
1173 *| 5-8 1-12 Month of the year. 0 rolls back to December of the previous
1174 *| year. 13-15 are invalid.
1175 *| 9-15 0-119 Year based from 1980 (Max 2099). 120-127 are invalid.
1176 * - The format of a Dos Time is:
1177 *| Bits Values Meaning
1178 *| ---- ------ -------
1179 *| 0-4 0-29 Seconds/2. 30 and 31 are invalid.
1180 *| 5-10 0-59 Minutes. 60-63 are invalid.
1181 *| 11-15 0-23 Hours (24 hour clock). 24-32 are invalid.
1183 INT WINAPI
DosDateTimeToVariantTime(USHORT wDosDate
, USHORT wDosTime
,
1188 TRACE("(0x%x(%d/%d/%d),0x%x(%d:%d:%d),%p)\n",
1189 wDosDate
, DOS_YEAR(wDosDate
), DOS_MONTH(wDosDate
), DOS_DAY(wDosDate
),
1190 wDosTime
, DOS_HOUR(wDosTime
), DOS_MINUTE(wDosTime
), DOS_SECOND(wDosTime
),
1193 ud
.st
.wYear
= DOS_YEAR(wDosDate
);
1194 ud
.st
.wMonth
= DOS_MONTH(wDosDate
);
1195 if (ud
.st
.wYear
> 2099 || ud
.st
.wMonth
> 12)
1197 ud
.st
.wDay
= DOS_DAY(wDosDate
);
1198 ud
.st
.wHour
= DOS_HOUR(wDosTime
);
1199 ud
.st
.wMinute
= DOS_MINUTE(wDosTime
);
1200 ud
.st
.wSecond
= DOS_SECOND(wDosTime
);
1201 ud
.st
.wDayOfWeek
= ud
.st
.wMilliseconds
= 0;
1202 if (ud
.st
.wHour
> 23 || ud
.st
.wMinute
> 59 || ud
.st
.wSecond
> 59)
1203 return FALSE
; /* Invalid values in Dos*/
1205 return VarDateFromUdate(&ud
, 0, pDateOut
) == S_OK
;
1208 /**********************************************************************
1209 * VariantTimeToDosDateTime [OLEAUT32.13]
1211 * Convert a variant format date into a Dos format date and time.
1213 * dateIn [I] VT_DATE time format
1214 * pwDosDate [O] Destination for Dos format date
1215 * pwDosTime [O] Destination for Dos format time
1218 * Success: TRUE. pwDosDate and pwDosTime contains the converted values.
1219 * Failure: FALSE, if dateIn cannot be represented in Dos format.
1222 * See DosDateTimeToVariantTime() for Dos format details and bugs.
1224 INT WINAPI
VariantTimeToDosDateTime(double dateIn
, USHORT
*pwDosDate
, USHORT
*pwDosTime
)
1228 TRACE("(%g,%p,%p)\n", dateIn
, pwDosDate
, pwDosTime
);
1230 if (FAILED(VarUdateFromDate(dateIn
, 0, &ud
)))
1233 if (ud
.st
.wYear
< 1980 || ud
.st
.wYear
> 2099)
1236 *pwDosDate
= DOS_DATE(ud
.st
.wDay
, ud
.st
.wMonth
, ud
.st
.wYear
);
1237 *pwDosTime
= DOS_TIME(ud
.st
.wHour
, ud
.st
.wMinute
, ud
.st
.wSecond
);
1239 TRACE("Returning 0x%x(%d/%d/%d), 0x%x(%d:%d:%d)\n",
1240 *pwDosDate
, DOS_YEAR(*pwDosDate
), DOS_MONTH(*pwDosDate
), DOS_DAY(*pwDosDate
),
1241 *pwDosTime
, DOS_HOUR(*pwDosTime
), DOS_MINUTE(*pwDosTime
), DOS_SECOND(*pwDosTime
));
1245 /***********************************************************************
1246 * SystemTimeToVariantTime [OLEAUT32.184]
1248 * Convert a System format date and time into variant VT_DATE format.
1251 * lpSt [I] System format date and time
1252 * pDateOut [O] Destination for VT_DATE format date
1255 * Success: TRUE. *pDateOut contains the converted value.
1256 * Failure: FALSE, if lpSt cannot be represented in VT_DATE format.
1258 INT WINAPI
SystemTimeToVariantTime(LPSYSTEMTIME lpSt
, double *pDateOut
)
1262 TRACE("(%p->%d/%d/%d %d:%d:%d,%p)\n", lpSt
, lpSt
->wDay
, lpSt
->wMonth
,
1263 lpSt
->wYear
, lpSt
->wHour
, lpSt
->wMinute
, lpSt
->wSecond
, pDateOut
);
1265 if (lpSt
->wMonth
> 12)
1267 if (lpSt
->wDay
> 31)
1269 if ((short)lpSt
->wYear
< 0)
1273 return VarDateFromUdate(&ud
, 0, pDateOut
) == S_OK
;
1276 /***********************************************************************
1277 * VariantTimeToSystemTime [OLEAUT32.185]
1279 * Convert a variant VT_DATE into a System format date and time.
1282 * datein [I] Variant VT_DATE format date
1283 * lpSt [O] Destination for System format date and time
1286 * Success: TRUE. *lpSt contains the converted value.
1287 * Failure: FALSE, if dateIn is too large or small.
1289 INT WINAPI
VariantTimeToSystemTime(double dateIn
, LPSYSTEMTIME lpSt
)
1293 TRACE("(%g,%p)\n", dateIn
, lpSt
);
1295 if (FAILED(VarUdateFromDate(dateIn
, 0, &ud
)))
1302 /***********************************************************************
1303 * VarDateFromUdateEx [OLEAUT32.319]
1305 * Convert an unpacked format date and time to a variant VT_DATE.
1308 * pUdateIn [I] Unpacked format date and time to convert
1309 * lcid [I] Locale identifier for the conversion
1310 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1311 * pDateOut [O] Destination for variant VT_DATE.
1314 * Success: S_OK. *pDateOut contains the converted value.
1315 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1317 HRESULT WINAPI
VarDateFromUdateEx(UDATE
*pUdateIn
, LCID lcid
, ULONG dwFlags
, DATE
*pDateOut
)
1322 TRACE("(%p->%d/%d/%d %d:%d:%d:%d %d %d,0x%08x,0x%08x,%p)\n", pUdateIn
,
1323 pUdateIn
->st
.wMonth
, pUdateIn
->st
.wDay
, pUdateIn
->st
.wYear
,
1324 pUdateIn
->st
.wHour
, pUdateIn
->st
.wMinute
, pUdateIn
->st
.wSecond
,
1325 pUdateIn
->st
.wMilliseconds
, pUdateIn
->st
.wDayOfWeek
,
1326 pUdateIn
->wDayOfYear
, lcid
, dwFlags
, pDateOut
);
1328 if (lcid
!= MAKELCID(MAKELANGID(LANG_ENGLISH
, SUBLANG_ENGLISH_US
), SORT_DEFAULT
))
1329 FIXME("lcid possibly not handled, treating as en-us\n");
1330 if (dwFlags
& ~(VAR_TIMEVALUEONLY
|VAR_DATEVALUEONLY
))
1331 FIXME("unsupported flags: %x\n", dwFlags
);
1335 if (dwFlags
& VAR_VALIDDATE
)
1336 WARN("Ignoring VAR_VALIDDATE\n");
1338 if (FAILED(VARIANT_RollUdate(&ud
)))
1339 return E_INVALIDARG
;
1342 if (!(dwFlags
& VAR_TIMEVALUEONLY
))
1343 dateVal
= VARIANT_DateFromJulian(VARIANT_JulianFromDMY(ud
.st
.wYear
, ud
.st
.wMonth
, ud
.st
.wDay
));
1345 if ((dwFlags
& VAR_TIMEVALUEONLY
) || !(dwFlags
& VAR_DATEVALUEONLY
))
1347 double dateSign
= (dateVal
< 0.0) ? -1.0 : 1.0;
1350 dateVal
+= ud
.st
.wHour
/ 24.0 * dateSign
;
1351 dateVal
+= ud
.st
.wMinute
/ 1440.0 * dateSign
;
1352 dateVal
+= ud
.st
.wSecond
/ 86400.0 * dateSign
;
1355 TRACE("Returning %g\n", dateVal
);
1356 *pDateOut
= dateVal
;
1360 /***********************************************************************
1361 * VarDateFromUdate [OLEAUT32.330]
1363 * Convert an unpacked format date and time to a variant VT_DATE.
1366 * pUdateIn [I] Unpacked format date and time to convert
1367 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1368 * pDateOut [O] Destination for variant VT_DATE.
1371 * Success: S_OK. *pDateOut contains the converted value.
1372 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1375 * This function uses the United States English locale for the conversion. Use
1376 * VarDateFromUdateEx() for alternate locales.
1378 HRESULT WINAPI
VarDateFromUdate(UDATE
*pUdateIn
, ULONG dwFlags
, DATE
*pDateOut
)
1380 LCID lcid
= MAKELCID(MAKELANGID(LANG_ENGLISH
, SUBLANG_ENGLISH_US
), SORT_DEFAULT
);
1382 return VarDateFromUdateEx(pUdateIn
, lcid
, dwFlags
, pDateOut
);
1385 /***********************************************************************
1386 * VarUdateFromDate [OLEAUT32.331]
1388 * Convert a variant VT_DATE into an unpacked format date and time.
1391 * datein [I] Variant VT_DATE format date
1392 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1393 * lpUdate [O] Destination for unpacked format date and time
1396 * Success: S_OK. *lpUdate contains the converted value.
1397 * Failure: E_INVALIDARG, if dateIn is too large or small.
1399 HRESULT WINAPI
VarUdateFromDate(DATE dateIn
, ULONG dwFlags
, UDATE
*lpUdate
)
1401 /* Cumulative totals of days per month */
1402 static const USHORT cumulativeDays
[] =
1404 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
1406 double datePart
, timePart
;
1409 TRACE("(%g,0x%08x,%p)\n", dateIn
, dwFlags
, lpUdate
);
1411 if (dateIn
<= (DATE_MIN
- 1.0) || dateIn
>= (DATE_MAX
+ 1.0))
1412 return E_INVALIDARG
;
1414 datePart
= dateIn
< 0.0 ? ceil(dateIn
) : floor(dateIn
);
1415 /* Compensate for int truncation (always downwards) */
1416 timePart
= fabs(dateIn
- datePart
) + 0.00000000001;
1417 if (timePart
>= 1.0)
1418 timePart
-= 0.00000000001;
1421 julianDays
= VARIANT_JulianFromDate(dateIn
);
1422 VARIANT_DMYFromJulian(julianDays
, &lpUdate
->st
.wYear
, &lpUdate
->st
.wMonth
,
1425 datePart
= (datePart
+ 1.5) / 7.0;
1426 lpUdate
->st
.wDayOfWeek
= (datePart
- floor(datePart
)) * 7;
1427 if (lpUdate
->st
.wDayOfWeek
== 0)
1428 lpUdate
->st
.wDayOfWeek
= 5;
1429 else if (lpUdate
->st
.wDayOfWeek
== 1)
1430 lpUdate
->st
.wDayOfWeek
= 6;
1432 lpUdate
->st
.wDayOfWeek
-= 2;
1434 if (lpUdate
->st
.wMonth
> 2 && IsLeapYear(lpUdate
->st
.wYear
))
1435 lpUdate
->wDayOfYear
= 1; /* After February, in a leap year */
1437 lpUdate
->wDayOfYear
= 0;
1439 lpUdate
->wDayOfYear
+= cumulativeDays
[lpUdate
->st
.wMonth
];
1440 lpUdate
->wDayOfYear
+= lpUdate
->st
.wDay
;
1444 lpUdate
->st
.wHour
= timePart
;
1445 timePart
-= lpUdate
->st
.wHour
;
1447 lpUdate
->st
.wMinute
= timePart
;
1448 timePart
-= lpUdate
->st
.wMinute
;
1450 lpUdate
->st
.wSecond
= timePart
;
1451 timePart
-= lpUdate
->st
.wSecond
;
1452 lpUdate
->st
.wMilliseconds
= 0;
1455 /* Round the milliseconds, adjusting the time/date forward if needed */
1456 if (lpUdate
->st
.wSecond
< 59)
1457 lpUdate
->st
.wSecond
++;
1460 lpUdate
->st
.wSecond
= 0;
1461 if (lpUdate
->st
.wMinute
< 59)
1462 lpUdate
->st
.wMinute
++;
1465 lpUdate
->st
.wMinute
= 0;
1466 if (lpUdate
->st
.wHour
< 23)
1467 lpUdate
->st
.wHour
++;
1470 lpUdate
->st
.wHour
= 0;
1471 /* Roll over a whole day */
1472 if (++lpUdate
->st
.wDay
> 28)
1473 VARIANT_RollUdate(lpUdate
);
1481 #define GET_NUMBER_TEXT(fld,name) \
1483 if (!GetLocaleInfoW(lcid, lctype|fld, buff, 2)) \
1484 WARN("buffer too small for " #fld "\n"); \
1486 if (buff[0]) lpChars->name = buff[0]; \
1487 TRACE("lcid 0x%x, " #name "=%d '%c'\n", lcid, lpChars->name, lpChars->name)
1489 /* Get the valid number characters for an lcid */
1490 static void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS
*lpChars
, LCID lcid
, DWORD dwFlags
)
1492 static const VARIANT_NUMBER_CHARS defaultChars
= { '-','+','.',',','$',0,'.',',' };
1493 static CRITICAL_SECTION csLastChars
= { NULL
, -1, 0, 0, 0, 0 };
1494 static VARIANT_NUMBER_CHARS lastChars
;
1495 static LCID lastLcid
= -1;
1496 static DWORD lastFlags
= 0;
1497 LCTYPE lctype
= dwFlags
& LOCALE_NOUSEROVERRIDE
;
1500 /* To make caching thread-safe, a critical section is needed */
1501 EnterCriticalSection(&csLastChars
);
1503 /* Asking for default locale entries is very expensive: It is a registry
1504 server call. So cache one locally, as Microsoft does it too */
1505 if(lcid
== lastLcid
&& dwFlags
== lastFlags
)
1507 memcpy(lpChars
, &lastChars
, sizeof(defaultChars
));
1508 LeaveCriticalSection(&csLastChars
);
1512 memcpy(lpChars
, &defaultChars
, sizeof(defaultChars
));
1513 GET_NUMBER_TEXT(LOCALE_SNEGATIVESIGN
, cNegativeSymbol
);
1514 GET_NUMBER_TEXT(LOCALE_SPOSITIVESIGN
, cPositiveSymbol
);
1515 GET_NUMBER_TEXT(LOCALE_SDECIMAL
, cDecimalPoint
);
1516 GET_NUMBER_TEXT(LOCALE_STHOUSAND
, cDigitSeparator
);
1517 GET_NUMBER_TEXT(LOCALE_SMONDECIMALSEP
, cCurrencyDecimalPoint
);
1518 GET_NUMBER_TEXT(LOCALE_SMONTHOUSANDSEP
, cCurrencyDigitSeparator
);
1520 /* Local currency symbols are often 2 characters */
1521 lpChars
->cCurrencyLocal2
= '\0';
1522 switch(GetLocaleInfoW(lcid
, lctype
|LOCALE_SCURRENCY
, buff
, sizeof(buff
)/sizeof(WCHAR
)))
1524 case 3: lpChars
->cCurrencyLocal2
= buff
[1]; /* Fall through */
1525 case 2: lpChars
->cCurrencyLocal
= buff
[0];
1527 default: WARN("buffer too small for LOCALE_SCURRENCY\n");
1529 TRACE("lcid 0x%x, cCurrencyLocal =%d,%d '%c','%c'\n", lcid
, lpChars
->cCurrencyLocal
,
1530 lpChars
->cCurrencyLocal2
, lpChars
->cCurrencyLocal
, lpChars
->cCurrencyLocal2
);
1532 memcpy(&lastChars
, lpChars
, sizeof(defaultChars
));
1534 lastFlags
= dwFlags
;
1535 LeaveCriticalSection(&csLastChars
);
1538 /* Number Parsing States */
1539 #define B_PROCESSING_EXPONENT 0x1
1540 #define B_NEGATIVE_EXPONENT 0x2
1541 #define B_EXPONENT_START 0x4
1542 #define B_INEXACT_ZEROS 0x8
1543 #define B_LEADING_ZERO 0x10
1544 #define B_PROCESSING_HEX 0x20
1545 #define B_PROCESSING_OCT 0x40
1547 /**********************************************************************
1548 * VarParseNumFromStr [OLEAUT32.46]
1550 * Parse a string containing a number into a NUMPARSE structure.
1553 * lpszStr [I] String to parse number from
1554 * lcid [I] Locale Id for the conversion
1555 * dwFlags [I] 0, or LOCALE_NOUSEROVERRIDE to use system default number chars
1556 * pNumprs [I/O] Destination for parsed number
1557 * rgbDig [O] Destination for digits read in
1560 * Success: S_OK. pNumprs and rgbDig contain the parsed representation of
1562 * Failure: E_INVALIDARG, if any parameter is invalid.
1563 * DISP_E_TYPEMISMATCH, if the string is not a number or is formatted
1565 * DISP_E_OVERFLOW, if rgbDig is too small to hold the number.
1568 * pNumprs must have the following fields set:
1569 * cDig: Set to the size of rgbDig.
1570 * dwInFlags: Set to the allowable syntax of the number using NUMPRS_ flags
1574 * - I am unsure if this function should parse non-Arabic (e.g. Thai)
1575 * numerals, so this has not been implemented.
1577 HRESULT WINAPI
VarParseNumFromStr(OLECHAR
*lpszStr
, LCID lcid
, ULONG dwFlags
,
1578 NUMPARSE
*pNumprs
, BYTE
*rgbDig
)
1580 VARIANT_NUMBER_CHARS chars
;
1582 DWORD dwState
= B_EXPONENT_START
|B_INEXACT_ZEROS
;
1583 int iMaxDigits
= sizeof(rgbTmp
) / sizeof(BYTE
);
1586 TRACE("(%s,%d,0x%08x,%p,%p)\n", debugstr_w(lpszStr
), lcid
, dwFlags
, pNumprs
, rgbDig
);
1588 if (!pNumprs
|| !rgbDig
)
1589 return E_INVALIDARG
;
1591 if (pNumprs
->cDig
< iMaxDigits
)
1592 iMaxDigits
= pNumprs
->cDig
;
1595 pNumprs
->dwOutFlags
= 0;
1596 pNumprs
->cchUsed
= 0;
1597 pNumprs
->nBaseShift
= 0;
1598 pNumprs
->nPwr10
= 0;
1601 return DISP_E_TYPEMISMATCH
;
1603 VARIANT_GetLocalisedNumberChars(&chars
, lcid
, dwFlags
);
1605 /* First consume all the leading symbols and space from the string */
1608 if (pNumprs
->dwInFlags
& NUMPRS_LEADING_WHITE
&& isspaceW(*lpszStr
))
1610 pNumprs
->dwOutFlags
|= NUMPRS_LEADING_WHITE
;
1615 } while (isspaceW(*lpszStr
));
1617 else if (pNumprs
->dwInFlags
& NUMPRS_LEADING_PLUS
&&
1618 *lpszStr
== chars
.cPositiveSymbol
&&
1619 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_PLUS
))
1621 pNumprs
->dwOutFlags
|= NUMPRS_LEADING_PLUS
;
1625 else if (pNumprs
->dwInFlags
& NUMPRS_LEADING_MINUS
&&
1626 *lpszStr
== chars
.cNegativeSymbol
&&
1627 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_MINUS
))
1629 pNumprs
->dwOutFlags
|= (NUMPRS_LEADING_MINUS
|NUMPRS_NEG
);
1633 else if (pNumprs
->dwInFlags
& NUMPRS_CURRENCY
&&
1634 !(pNumprs
->dwOutFlags
& NUMPRS_CURRENCY
) &&
1635 *lpszStr
== chars
.cCurrencyLocal
&&
1636 (!chars
.cCurrencyLocal2
|| lpszStr
[1] == chars
.cCurrencyLocal2
))
1638 pNumprs
->dwOutFlags
|= NUMPRS_CURRENCY
;
1641 /* Only accept currency characters */
1642 chars
.cDecimalPoint
= chars
.cCurrencyDecimalPoint
;
1643 chars
.cDigitSeparator
= chars
.cCurrencyDigitSeparator
;
1645 else if (pNumprs
->dwInFlags
& NUMPRS_PARENS
&& *lpszStr
== '(' &&
1646 !(pNumprs
->dwOutFlags
& NUMPRS_PARENS
))
1648 pNumprs
->dwOutFlags
|= NUMPRS_PARENS
;
1656 if (!(pNumprs
->dwOutFlags
& NUMPRS_CURRENCY
))
1658 /* Only accept non-currency characters */
1659 chars
.cCurrencyDecimalPoint
= chars
.cDecimalPoint
;
1660 chars
.cCurrencyDigitSeparator
= chars
.cDigitSeparator
;
1663 if ((*lpszStr
== '&' && (*(lpszStr
+1) == 'H' || *(lpszStr
+1) == 'h')) &&
1664 pNumprs
->dwInFlags
& NUMPRS_HEX_OCT
)
1666 dwState
|= B_PROCESSING_HEX
;
1667 pNumprs
->dwOutFlags
|= NUMPRS_HEX_OCT
;
1671 else if ((*lpszStr
== '&' && (*(lpszStr
+1) == 'O' || *(lpszStr
+1) == 'o')) &&
1672 pNumprs
->dwInFlags
& NUMPRS_HEX_OCT
)
1674 dwState
|= B_PROCESSING_OCT
;
1675 pNumprs
->dwOutFlags
|= NUMPRS_HEX_OCT
;
1680 /* Strip Leading zeros */
1681 while (*lpszStr
== '0')
1683 dwState
|= B_LEADING_ZERO
;
1690 if (isdigitW(*lpszStr
))
1692 if (dwState
& B_PROCESSING_EXPONENT
)
1694 int exponentSize
= 0;
1695 if (dwState
& B_EXPONENT_START
)
1697 if (!isdigitW(*lpszStr
))
1698 break; /* No exponent digits - invalid */
1699 while (*lpszStr
== '0')
1701 /* Skip leading zero's in the exponent */
1707 while (isdigitW(*lpszStr
))
1710 exponentSize
+= *lpszStr
- '0';
1714 if (dwState
& B_NEGATIVE_EXPONENT
)
1715 exponentSize
= -exponentSize
;
1716 /* Add the exponent into the powers of 10 */
1717 pNumprs
->nPwr10
+= exponentSize
;
1718 dwState
&= ~(B_PROCESSING_EXPONENT
|B_EXPONENT_START
);
1719 lpszStr
--; /* back up to allow processing of next char */
1723 if ((pNumprs
->cDig
>= iMaxDigits
) && !(dwState
& B_PROCESSING_HEX
)
1724 && !(dwState
& B_PROCESSING_OCT
))
1726 pNumprs
->dwOutFlags
|= NUMPRS_INEXACT
;
1728 if (*lpszStr
!= '0')
1729 dwState
&= ~B_INEXACT_ZEROS
; /* Inexact number with non-trailing zeros */
1731 /* This digit can't be represented, but count it in nPwr10 */
1732 if (pNumprs
->dwOutFlags
& NUMPRS_DECIMAL
)
1739 if ((dwState
& B_PROCESSING_OCT
) && ((*lpszStr
== '8') || (*lpszStr
== '9')))
1742 if (pNumprs
->dwOutFlags
& NUMPRS_DECIMAL
)
1743 pNumprs
->nPwr10
--; /* Count decimal points in nPwr10 */
1745 rgbTmp
[pNumprs
->cDig
] = *lpszStr
- '0';
1751 else if (*lpszStr
== chars
.cDigitSeparator
&& pNumprs
->dwInFlags
& NUMPRS_THOUSANDS
)
1753 pNumprs
->dwOutFlags
|= NUMPRS_THOUSANDS
;
1756 else if (*lpszStr
== chars
.cDecimalPoint
&&
1757 pNumprs
->dwInFlags
& NUMPRS_DECIMAL
&&
1758 !(pNumprs
->dwOutFlags
& (NUMPRS_DECIMAL
|NUMPRS_EXPONENT
)))
1760 pNumprs
->dwOutFlags
|= NUMPRS_DECIMAL
;
1763 /* If we have no digits so far, skip leading zeros */
1766 while (lpszStr
[1] == '0')
1768 dwState
|= B_LEADING_ZERO
;
1775 else if (((*lpszStr
>= 'a' && *lpszStr
<= 'f') ||
1776 (*lpszStr
>= 'A' && *lpszStr
<= 'F')) &&
1777 dwState
& B_PROCESSING_HEX
)
1779 if (pNumprs
->cDig
>= iMaxDigits
)
1781 return DISP_E_OVERFLOW
;
1785 if (*lpszStr
>= 'a')
1786 rgbTmp
[pNumprs
->cDig
] = *lpszStr
- 'a' + 10;
1788 rgbTmp
[pNumprs
->cDig
] = *lpszStr
- 'A' + 10;
1793 else if ((*lpszStr
== 'e' || *lpszStr
== 'E') &&
1794 pNumprs
->dwInFlags
& NUMPRS_EXPONENT
&&
1795 !(pNumprs
->dwOutFlags
& NUMPRS_EXPONENT
))
1797 dwState
|= B_PROCESSING_EXPONENT
;
1798 pNumprs
->dwOutFlags
|= NUMPRS_EXPONENT
;
1801 else if (dwState
& B_PROCESSING_EXPONENT
&& *lpszStr
== chars
.cPositiveSymbol
)
1803 cchUsed
++; /* Ignore positive exponent */
1805 else if (dwState
& B_PROCESSING_EXPONENT
&& *lpszStr
== chars
.cNegativeSymbol
)
1807 dwState
|= B_NEGATIVE_EXPONENT
;
1811 break; /* Stop at an unrecognised character */
1816 if (!pNumprs
->cDig
&& dwState
& B_LEADING_ZERO
)
1818 /* Ensure a 0 on its own gets stored */
1823 if (pNumprs
->dwOutFlags
& NUMPRS_EXPONENT
&& dwState
& B_PROCESSING_EXPONENT
)
1825 pNumprs
->cchUsed
= cchUsed
;
1826 WARN("didn't completely parse exponent\n");
1827 return DISP_E_TYPEMISMATCH
; /* Failed to completely parse the exponent */
1830 if (pNumprs
->dwOutFlags
& NUMPRS_INEXACT
)
1832 if (dwState
& B_INEXACT_ZEROS
)
1833 pNumprs
->dwOutFlags
&= ~NUMPRS_INEXACT
; /* All zeros doesn't set NUMPRS_INEXACT */
1834 } else if(pNumprs
->dwInFlags
& NUMPRS_HEX_OCT
)
1836 /* copy all of the digits into the output digit buffer */
1837 /* this is exactly what windows does although it also returns */
1838 /* cDig of X and writes X+Y where Y>=0 number of digits to rgbDig */
1839 memcpy(rgbDig
, rgbTmp
, pNumprs
->cDig
* sizeof(BYTE
));
1841 if (dwState
& B_PROCESSING_HEX
) {
1842 /* hex numbers have always the same format */
1844 pNumprs
->nBaseShift
=4;
1846 if (dwState
& B_PROCESSING_OCT
) {
1847 /* oct numbers have always the same format */
1849 pNumprs
->nBaseShift
=3;
1851 while (pNumprs
->cDig
> 1 && !rgbTmp
[pNumprs
->cDig
- 1])
1860 /* Remove trailing zeros from the last (whole number or decimal) part */
1861 while (pNumprs
->cDig
> 1 && !rgbTmp
[pNumprs
->cDig
- 1])
1868 if (pNumprs
->cDig
<= iMaxDigits
)
1869 pNumprs
->dwOutFlags
&= ~NUMPRS_INEXACT
; /* Ignore stripped zeros for NUMPRS_INEXACT */
1871 pNumprs
->cDig
= iMaxDigits
; /* Only return iMaxDigits worth of digits */
1873 /* Copy the digits we processed into rgbDig */
1874 memcpy(rgbDig
, rgbTmp
, pNumprs
->cDig
* sizeof(BYTE
));
1876 /* Consume any trailing symbols and space */
1879 if ((pNumprs
->dwInFlags
& NUMPRS_TRAILING_WHITE
) && isspaceW(*lpszStr
))
1881 pNumprs
->dwOutFlags
|= NUMPRS_TRAILING_WHITE
;
1886 } while (isspaceW(*lpszStr
));
1888 else if (pNumprs
->dwInFlags
& NUMPRS_TRAILING_PLUS
&&
1889 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_PLUS
) &&
1890 *lpszStr
== chars
.cPositiveSymbol
)
1892 pNumprs
->dwOutFlags
|= NUMPRS_TRAILING_PLUS
;
1896 else if (pNumprs
->dwInFlags
& NUMPRS_TRAILING_MINUS
&&
1897 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_MINUS
) &&
1898 *lpszStr
== chars
.cNegativeSymbol
)
1900 pNumprs
->dwOutFlags
|= (NUMPRS_TRAILING_MINUS
|NUMPRS_NEG
);
1904 else if (pNumprs
->dwInFlags
& NUMPRS_PARENS
&& *lpszStr
== ')' &&
1905 pNumprs
->dwOutFlags
& NUMPRS_PARENS
)
1909 pNumprs
->dwOutFlags
|= NUMPRS_NEG
;
1915 if (pNumprs
->dwOutFlags
& NUMPRS_PARENS
&& !(pNumprs
->dwOutFlags
& NUMPRS_NEG
))
1917 pNumprs
->cchUsed
= cchUsed
;
1918 return DISP_E_TYPEMISMATCH
; /* Opening parenthesis not matched */
1921 if (pNumprs
->dwInFlags
& NUMPRS_USE_ALL
&& *lpszStr
!= '\0')
1922 return DISP_E_TYPEMISMATCH
; /* Not all chars were consumed */
1925 return DISP_E_TYPEMISMATCH
; /* No Number found */
1927 pNumprs
->cchUsed
= cchUsed
;
1931 /* VTBIT flags indicating an integer value */
1932 #define INTEGER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8)
1933 /* VTBIT flags indicating a real number value */
1934 #define REAL_VTBITS (VTBIT_R4|VTBIT_R8|VTBIT_CY)
1936 /* Helper macros to check whether bit pattern fits in VARIANT (x is a ULONG64 ) */
1937 #define FITS_AS_I1(x) ((x) >> 8 == 0)
1938 #define FITS_AS_I2(x) ((x) >> 16 == 0)
1939 #define FITS_AS_I4(x) ((x) >> 32 == 0)
1941 /**********************************************************************
1942 * VarNumFromParseNum [OLEAUT32.47]
1944 * Convert a NUMPARSE structure into a numeric Variant type.
1947 * pNumprs [I] Source for parsed number. cDig must be set to the size of rgbDig
1948 * rgbDig [I] Source for the numbers digits
1949 * dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
1950 * pVarDst [O] Destination for the converted Variant value.
1953 * Success: S_OK. pVarDst contains the converted value.
1954 * Failure: E_INVALIDARG, if any parameter is invalid.
1955 * DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
1958 * - The smallest favoured type present in dwVtBits that can represent the
1959 * number in pNumprs without losing precision is used.
1960 * - Signed types are preferred over unsigned types of the same size.
1961 * - Preferred types in order are: integer, float, double, currency then decimal.
1962 * - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
1963 * for details of the rounding method.
1964 * - pVarDst is not cleared before the result is stored in it.
1965 * - WinXP and Win2003 support VTBIT_I8, VTBIT_UI8 but that's buggy (by
1966 * design?): If some other VTBIT's for integers are specified together
1967 * with VTBIT_I8 and the number will fit only in a VT_I8 Windows will "cast"
1968 * the number to the smallest requested integer truncating this way the
1969 * number. Wine doesn't implement this "feature" (yet?).
1971 HRESULT WINAPI
VarNumFromParseNum(NUMPARSE
*pNumprs
, BYTE
*rgbDig
,
1972 ULONG dwVtBits
, VARIANT
*pVarDst
)
1974 /* Scale factors and limits for double arithmetic */
1975 static const double dblMultipliers
[11] = {
1976 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
1977 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
1979 static const double dblMinimums
[11] = {
1980 R8_MIN
, R8_MIN
*10.0, R8_MIN
*100.0, R8_MIN
*1000.0, R8_MIN
*10000.0,
1981 R8_MIN
*100000.0, R8_MIN
*1000000.0, R8_MIN
*10000000.0,
1982 R8_MIN
*100000000.0, R8_MIN
*1000000000.0, R8_MIN
*10000000000.0
1984 static const double dblMaximums
[11] = {
1985 R8_MAX
, R8_MAX
/10.0, R8_MAX
/100.0, R8_MAX
/1000.0, R8_MAX
/10000.0,
1986 R8_MAX
/100000.0, R8_MAX
/1000000.0, R8_MAX
/10000000.0,
1987 R8_MAX
/100000000.0, R8_MAX
/1000000000.0, R8_MAX
/10000000000.0
1990 int wholeNumberDigits
, fractionalDigits
, divisor10
= 0, multiplier10
= 0;
1992 TRACE("(%p,%p,0x%x,%p)\n", pNumprs
, rgbDig
, dwVtBits
, pVarDst
);
1994 if (pNumprs
->nBaseShift
)
1996 /* nBaseShift indicates a hex or octal number */
2001 /* Convert the hex or octal number string into a UI64 */
2002 for (i
= 0; i
< pNumprs
->cDig
; i
++)
2004 if (ul64
> ((UI8_MAX
>>pNumprs
->nBaseShift
) - rgbDig
[i
]))
2006 TRACE("Overflow multiplying digits\n");
2007 return DISP_E_OVERFLOW
;
2009 ul64
= (ul64
<<pNumprs
->nBaseShift
) + rgbDig
[i
];
2012 /* also make a negative representation */
2015 /* Try signed and unsigned types in size order */
2016 if (dwVtBits
& VTBIT_I1
&& FITS_AS_I1(ul64
))
2018 V_VT(pVarDst
) = VT_I1
;
2019 V_I1(pVarDst
) = ul64
;
2022 else if (dwVtBits
& VTBIT_UI1
&& FITS_AS_I1(ul64
))
2024 V_VT(pVarDst
) = VT_UI1
;
2025 V_UI1(pVarDst
) = ul64
;
2028 else if (dwVtBits
& VTBIT_I2
&& FITS_AS_I2(ul64
))
2030 V_VT(pVarDst
) = VT_I2
;
2031 V_I2(pVarDst
) = ul64
;
2034 else if (dwVtBits
& VTBIT_UI2
&& FITS_AS_I2(ul64
))
2036 V_VT(pVarDst
) = VT_UI2
;
2037 V_UI2(pVarDst
) = ul64
;
2040 else if (dwVtBits
& VTBIT_I4
&& FITS_AS_I4(ul64
))
2042 V_VT(pVarDst
) = VT_I4
;
2043 V_I4(pVarDst
) = ul64
;
2046 else if (dwVtBits
& VTBIT_UI4
&& FITS_AS_I4(ul64
))
2048 V_VT(pVarDst
) = VT_UI4
;
2049 V_UI4(pVarDst
) = ul64
;
2052 else if (dwVtBits
& VTBIT_I8
&& ((ul64
<= I8_MAX
)||(l64
>=I8_MIN
)))
2054 V_VT(pVarDst
) = VT_I8
;
2055 V_I8(pVarDst
) = ul64
;
2058 else if (dwVtBits
& VTBIT_UI8
)
2060 V_VT(pVarDst
) = VT_UI8
;
2061 V_UI8(pVarDst
) = ul64
;
2064 else if ((dwVtBits
& VTBIT_DECIMAL
) == VTBIT_DECIMAL
)
2066 V_VT(pVarDst
) = VT_DECIMAL
;
2067 DEC_SIGNSCALE(&V_DECIMAL(pVarDst
)) = SIGNSCALE(DECIMAL_POS
,0);
2068 DEC_HI32(&V_DECIMAL(pVarDst
)) = 0;
2069 DEC_LO64(&V_DECIMAL(pVarDst
)) = ul64
;
2072 else if (dwVtBits
& VTBIT_R4
&& ((ul64
<= I4_MAX
)||(l64
>= I4_MIN
)))
2074 V_VT(pVarDst
) = VT_R4
;
2076 V_R4(pVarDst
) = ul64
;
2078 V_R4(pVarDst
) = l64
;
2081 else if (dwVtBits
& VTBIT_R8
&& ((ul64
<= I4_MAX
)||(l64
>= I4_MIN
)))
2083 V_VT(pVarDst
) = VT_R8
;
2085 V_R8(pVarDst
) = ul64
;
2087 V_R8(pVarDst
) = l64
;
2091 TRACE("Overflow: possible return types: 0x%x, value: %s\n", dwVtBits
, wine_dbgstr_longlong(ul64
));
2092 return DISP_E_OVERFLOW
;
2095 /* Count the number of relevant fractional and whole digits stored,
2096 * And compute the divisor/multiplier to scale the number by.
2098 if (pNumprs
->nPwr10
< 0)
2100 if (-pNumprs
->nPwr10
>= pNumprs
->cDig
)
2102 /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
2103 wholeNumberDigits
= 0;
2104 fractionalDigits
= pNumprs
->cDig
;
2105 divisor10
= -pNumprs
->nPwr10
;
2109 /* An exactly represented real number e.g. 1.024 */
2110 wholeNumberDigits
= pNumprs
->cDig
+ pNumprs
->nPwr10
;
2111 fractionalDigits
= pNumprs
->cDig
- wholeNumberDigits
;
2112 divisor10
= pNumprs
->cDig
- wholeNumberDigits
;
2115 else if (pNumprs
->nPwr10
== 0)
2117 /* An exactly represented whole number e.g. 1024 */
2118 wholeNumberDigits
= pNumprs
->cDig
;
2119 fractionalDigits
= 0;
2121 else /* pNumprs->nPwr10 > 0 */
2123 /* A whole number followed by nPwr10 0's e.g. 102400 */
2124 wholeNumberDigits
= pNumprs
->cDig
;
2125 fractionalDigits
= 0;
2126 multiplier10
= pNumprs
->nPwr10
;
2129 TRACE("cDig %d; nPwr10 %d, whole %d, frac %d mult %d; div %d\n",
2130 pNumprs
->cDig
, pNumprs
->nPwr10
, wholeNumberDigits
, fractionalDigits
,
2131 multiplier10
, divisor10
);
2133 if (dwVtBits
& (INTEGER_VTBITS
|VTBIT_DECIMAL
) &&
2134 (!fractionalDigits
|| !(dwVtBits
& (REAL_VTBITS
|VTBIT_CY
|VTBIT_DECIMAL
))))
2136 /* We have one or more integer output choices, and either:
2137 * 1) An integer input value, or
2138 * 2) A real number input value but no floating output choices.
2139 * Alternately, we have a DECIMAL output available and an integer input.
2141 * So, place the integer value into pVarDst, using the smallest type
2142 * possible and preferring signed over unsigned types.
2144 BOOL bOverflow
= FALSE
, bNegative
;
2148 /* Convert the integer part of the number into a UI8 */
2149 for (i
= 0; i
< wholeNumberDigits
; i
++)
2151 if (ul64
> UI8_MAX
/ 10 || (ul64
== UI8_MAX
/ 10 && rgbDig
[i
] > UI8_MAX
% 10))
2153 TRACE("Overflow multiplying digits\n");
2157 ul64
= ul64
* 10 + rgbDig
[i
];
2160 /* Account for the scale of the number */
2161 if (!bOverflow
&& multiplier10
)
2163 for (i
= 0; i
< multiplier10
; i
++)
2165 if (ul64
> (UI8_MAX
/ 10))
2167 TRACE("Overflow scaling number\n");
2175 /* If we have any fractional digits, round the value.
2176 * Note we don't have to do this if divisor10 is < 1,
2177 * because this means the fractional part must be < 0.5
2179 if (!bOverflow
&& fractionalDigits
&& divisor10
> 0)
2181 const BYTE
* fracDig
= rgbDig
+ wholeNumberDigits
;
2182 BOOL bAdjust
= FALSE
;
2184 TRACE("first decimal value is %d\n", *fracDig
);
2187 bAdjust
= TRUE
; /* > 0.5 */
2188 else if (*fracDig
== 5)
2190 for (i
= 1; i
< fractionalDigits
; i
++)
2194 bAdjust
= TRUE
; /* > 0.5 */
2198 /* If exactly 0.5, round only odd values */
2199 if (i
== fractionalDigits
&& (ul64
& 1))
2205 if (ul64
== UI8_MAX
)
2207 TRACE("Overflow after rounding\n");
2214 /* Zero is not a negative number */
2215 bNegative
= pNumprs
->dwOutFlags
& NUMPRS_NEG
&& ul64
;
2217 TRACE("Integer value is 0x%s, bNeg %d\n", wine_dbgstr_longlong(ul64
), bNegative
);
2219 /* For negative integers, try the signed types in size order */
2220 if (!bOverflow
&& bNegative
)
2222 if (dwVtBits
& (VTBIT_I1
|VTBIT_I2
|VTBIT_I4
|VTBIT_I8
))
2224 if (dwVtBits
& VTBIT_I1
&& ul64
<= -I1_MIN
)
2226 V_VT(pVarDst
) = VT_I1
;
2227 V_I1(pVarDst
) = -ul64
;
2230 else if (dwVtBits
& VTBIT_I2
&& ul64
<= -I2_MIN
)
2232 V_VT(pVarDst
) = VT_I2
;
2233 V_I2(pVarDst
) = -ul64
;
2236 else if (dwVtBits
& VTBIT_I4
&& ul64
<= -((LONGLONG
)I4_MIN
))
2238 V_VT(pVarDst
) = VT_I4
;
2239 V_I4(pVarDst
) = -ul64
;
2242 else if (dwVtBits
& VTBIT_I8
&& ul64
<= (ULONGLONG
)I8_MAX
+ 1)
2244 V_VT(pVarDst
) = VT_I8
;
2245 V_I8(pVarDst
) = -ul64
;
2248 else if ((dwVtBits
& REAL_VTBITS
) == VTBIT_DECIMAL
)
2250 /* Decimal is only output choice left - fast path */
2251 V_VT(pVarDst
) = VT_DECIMAL
;
2252 DEC_SIGNSCALE(&V_DECIMAL(pVarDst
)) = SIGNSCALE(DECIMAL_NEG
,0);
2253 DEC_HI32(&V_DECIMAL(pVarDst
)) = 0;
2254 DEC_LO64(&V_DECIMAL(pVarDst
)) = -ul64
;
2259 else if (!bOverflow
)
2261 /* For positive integers, try signed then unsigned types in size order */
2262 if (dwVtBits
& VTBIT_I1
&& ul64
<= I1_MAX
)
2264 V_VT(pVarDst
) = VT_I1
;
2265 V_I1(pVarDst
) = ul64
;
2268 else if (dwVtBits
& VTBIT_UI1
&& ul64
<= UI1_MAX
)
2270 V_VT(pVarDst
) = VT_UI1
;
2271 V_UI1(pVarDst
) = ul64
;
2274 else if (dwVtBits
& VTBIT_I2
&& ul64
<= I2_MAX
)
2276 V_VT(pVarDst
) = VT_I2
;
2277 V_I2(pVarDst
) = ul64
;
2280 else if (dwVtBits
& VTBIT_UI2
&& ul64
<= UI2_MAX
)
2282 V_VT(pVarDst
) = VT_UI2
;
2283 V_UI2(pVarDst
) = ul64
;
2286 else if (dwVtBits
& VTBIT_I4
&& ul64
<= I4_MAX
)
2288 V_VT(pVarDst
) = VT_I4
;
2289 V_I4(pVarDst
) = ul64
;
2292 else if (dwVtBits
& VTBIT_UI4
&& ul64
<= UI4_MAX
)
2294 V_VT(pVarDst
) = VT_UI4
;
2295 V_UI4(pVarDst
) = ul64
;
2298 else if (dwVtBits
& VTBIT_I8
&& ul64
<= I8_MAX
)
2300 V_VT(pVarDst
) = VT_I8
;
2301 V_I8(pVarDst
) = ul64
;
2304 else if (dwVtBits
& VTBIT_UI8
)
2306 V_VT(pVarDst
) = VT_UI8
;
2307 V_UI8(pVarDst
) = ul64
;
2310 else if ((dwVtBits
& REAL_VTBITS
) == VTBIT_DECIMAL
)
2312 /* Decimal is only output choice left - fast path */
2313 V_VT(pVarDst
) = VT_DECIMAL
;
2314 DEC_SIGNSCALE(&V_DECIMAL(pVarDst
)) = SIGNSCALE(DECIMAL_POS
,0);
2315 DEC_HI32(&V_DECIMAL(pVarDst
)) = 0;
2316 DEC_LO64(&V_DECIMAL(pVarDst
)) = ul64
;
2322 if (dwVtBits
& REAL_VTBITS
)
2324 /* Try to put the number into a float or real */
2325 BOOL bOverflow
= FALSE
, bNegative
= pNumprs
->dwOutFlags
& NUMPRS_NEG
;
2329 /* Convert the number into a double */
2330 for (i
= 0; i
< pNumprs
->cDig
; i
++)
2331 whole
= whole
* 10.0 + rgbDig
[i
];
2333 TRACE("Whole double value is %16.16g\n", whole
);
2335 /* Account for the scale */
2336 while (multiplier10
> 10)
2338 if (whole
> dblMaximums
[10])
2340 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
);
2344 whole
= whole
* dblMultipliers
[10];
2347 if (multiplier10
&& !bOverflow
)
2349 if (whole
> dblMaximums
[multiplier10
])
2351 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
);
2355 whole
= whole
* dblMultipliers
[multiplier10
];
2359 TRACE("Scaled double value is %16.16g\n", whole
);
2361 while (divisor10
> 10 && !bOverflow
)
2363 if (whole
< dblMinimums
[10] && whole
!= 0)
2365 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
); /* Underflow */
2369 whole
= whole
/ dblMultipliers
[10];
2372 if (divisor10
&& !bOverflow
)
2374 if (whole
< dblMinimums
[divisor10
] && whole
!= 0)
2376 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
); /* Underflow */
2380 whole
= whole
/ dblMultipliers
[divisor10
];
2383 TRACE("Final double value is %16.16g\n", whole
);
2385 if (dwVtBits
& VTBIT_R4
&&
2386 ((whole
<= R4_MAX
&& whole
>= R4_MIN
) || whole
== 0.0))
2388 TRACE("Set R4 to final value\n");
2389 V_VT(pVarDst
) = VT_R4
; /* Fits into a float */
2390 V_R4(pVarDst
) = pNumprs
->dwOutFlags
& NUMPRS_NEG
? -whole
: whole
;
2394 if (dwVtBits
& VTBIT_R8
)
2396 TRACE("Set R8 to final value\n");
2397 V_VT(pVarDst
) = VT_R8
; /* Fits into a double */
2398 V_R8(pVarDst
) = pNumprs
->dwOutFlags
& NUMPRS_NEG
? -whole
: whole
;
2402 if (dwVtBits
& VTBIT_CY
)
2404 if (SUCCEEDED(VarCyFromR8(bNegative
? -whole
: whole
, &V_CY(pVarDst
))))
2406 V_VT(pVarDst
) = VT_CY
; /* Fits into a currency */
2407 TRACE("Set CY to final value\n");
2410 TRACE("Value Overflows CY\n");
2414 if (dwVtBits
& VTBIT_DECIMAL
)
2419 DECIMAL
* pDec
= &V_DECIMAL(pVarDst
);
2421 DECIMAL_SETZERO(*pDec
);
2424 if (pNumprs
->dwOutFlags
& NUMPRS_NEG
)
2425 DEC_SIGN(pDec
) = DECIMAL_NEG
;
2427 DEC_SIGN(pDec
) = DECIMAL_POS
;
2429 /* Factor the significant digits */
2430 for (i
= 0; i
< pNumprs
->cDig
; i
++)
2432 tmp
= (ULONG64
)DEC_LO32(pDec
) * 10 + rgbDig
[i
];
2433 carry
= (ULONG
)(tmp
>> 32);
2434 DEC_LO32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2435 tmp
= (ULONG64
)DEC_MID32(pDec
) * 10 + carry
;
2436 carry
= (ULONG
)(tmp
>> 32);
2437 DEC_MID32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2438 tmp
= (ULONG64
)DEC_HI32(pDec
) * 10 + carry
;
2439 DEC_HI32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2441 if (tmp
>> 32 & UI4_MAX
)
2443 VarNumFromParseNum_DecOverflow
:
2444 TRACE("Overflow\n");
2445 DEC_LO32(pDec
) = DEC_MID32(pDec
) = DEC_HI32(pDec
) = UI4_MAX
;
2446 return DISP_E_OVERFLOW
;
2450 /* Account for the scale of the number */
2451 while (multiplier10
> 0)
2453 tmp
= (ULONG64
)DEC_LO32(pDec
) * 10;
2454 carry
= (ULONG
)(tmp
>> 32);
2455 DEC_LO32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2456 tmp
= (ULONG64
)DEC_MID32(pDec
) * 10 + carry
;
2457 carry
= (ULONG
)(tmp
>> 32);
2458 DEC_MID32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2459 tmp
= (ULONG64
)DEC_HI32(pDec
) * 10 + carry
;
2460 DEC_HI32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2462 if (tmp
>> 32 & UI4_MAX
)
2463 goto VarNumFromParseNum_DecOverflow
;
2466 DEC_SCALE(pDec
) = divisor10
;
2468 V_VT(pVarDst
) = VT_DECIMAL
;
2471 return DISP_E_OVERFLOW
; /* No more output choices */
2474 /**********************************************************************
2475 * VarCat [OLEAUT32.318]
2477 * Concatenates one variant onto another.
2480 * left [I] First variant
2481 * right [I] Second variant
2482 * result [O] Result variant
2486 * Failure: An HRESULT error code indicating the error.
2488 HRESULT WINAPI
VarCat(LPVARIANT left
, LPVARIANT right
, LPVARIANT out
)
2490 VARTYPE leftvt
,rightvt
,resultvt
;
2492 static WCHAR str_true
[32];
2493 static WCHAR str_false
[32];
2494 static const WCHAR sz_empty
[] = {'\0'};
2495 leftvt
= V_VT(left
);
2496 rightvt
= V_VT(right
);
2498 TRACE("%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), out
);
2501 VARIANT_GetLocalisedText(LOCALE_USER_DEFAULT
, IDS_FALSE
, str_false
);
2502 VARIANT_GetLocalisedText(LOCALE_USER_DEFAULT
, IDS_TRUE
, str_true
);
2505 /* when both left and right are NULL the result is NULL */
2506 if (leftvt
== VT_NULL
&& rightvt
== VT_NULL
)
2508 V_VT(out
) = VT_NULL
;
2513 resultvt
= VT_EMPTY
;
2515 /* There are many special case for errors and return types */
2516 if (leftvt
== VT_VARIANT
&& (rightvt
== VT_ERROR
||
2517 rightvt
== VT_DATE
|| rightvt
== VT_DECIMAL
))
2518 hres
= DISP_E_TYPEMISMATCH
;
2519 else if ((leftvt
== VT_I2
|| leftvt
== VT_I4
||
2520 leftvt
== VT_R4
|| leftvt
== VT_R8
||
2521 leftvt
== VT_CY
|| leftvt
== VT_BOOL
||
2522 leftvt
== VT_BSTR
|| leftvt
== VT_I1
||
2523 leftvt
== VT_UI1
|| leftvt
== VT_UI2
||
2524 leftvt
== VT_UI4
|| leftvt
== VT_I8
||
2525 leftvt
== VT_UI8
|| leftvt
== VT_INT
||
2526 leftvt
== VT_UINT
|| leftvt
== VT_EMPTY
||
2527 leftvt
== VT_NULL
|| leftvt
== VT_DATE
||
2528 leftvt
== VT_DECIMAL
|| leftvt
== VT_DISPATCH
)
2530 (rightvt
== VT_I2
|| rightvt
== VT_I4
||
2531 rightvt
== VT_R4
|| rightvt
== VT_R8
||
2532 rightvt
== VT_CY
|| rightvt
== VT_BOOL
||
2533 rightvt
== VT_BSTR
|| rightvt
== VT_I1
||
2534 rightvt
== VT_UI1
|| rightvt
== VT_UI2
||
2535 rightvt
== VT_UI4
|| rightvt
== VT_I8
||
2536 rightvt
== VT_UI8
|| rightvt
== VT_INT
||
2537 rightvt
== VT_UINT
|| rightvt
== VT_EMPTY
||
2538 rightvt
== VT_NULL
|| rightvt
== VT_DATE
||
2539 rightvt
== VT_DECIMAL
|| rightvt
== VT_DISPATCH
))
2541 else if (rightvt
== VT_ERROR
&& leftvt
< VT_VOID
)
2542 hres
= DISP_E_TYPEMISMATCH
;
2543 else if (leftvt
== VT_ERROR
&& (rightvt
== VT_DATE
||
2544 rightvt
== VT_ERROR
|| rightvt
== VT_DECIMAL
))
2545 hres
= DISP_E_TYPEMISMATCH
;
2546 else if (rightvt
== VT_DATE
|| rightvt
== VT_ERROR
||
2547 rightvt
== VT_DECIMAL
)
2548 hres
= DISP_E_BADVARTYPE
;
2549 else if (leftvt
== VT_ERROR
|| rightvt
== VT_ERROR
)
2550 hres
= DISP_E_TYPEMISMATCH
;
2551 else if (leftvt
== VT_VARIANT
)
2552 hres
= DISP_E_TYPEMISMATCH
;
2553 else if (rightvt
== VT_VARIANT
&& (leftvt
== VT_EMPTY
||
2554 leftvt
== VT_NULL
|| leftvt
== VT_I2
||
2555 leftvt
== VT_I4
|| leftvt
== VT_R4
||
2556 leftvt
== VT_R8
|| leftvt
== VT_CY
||
2557 leftvt
== VT_DATE
|| leftvt
== VT_BSTR
||
2558 leftvt
== VT_BOOL
|| leftvt
== VT_DECIMAL
||
2559 leftvt
== VT_I1
|| leftvt
== VT_UI1
||
2560 leftvt
== VT_UI2
|| leftvt
== VT_UI4
||
2561 leftvt
== VT_I8
|| leftvt
== VT_UI8
||
2562 leftvt
== VT_INT
|| leftvt
== VT_UINT
))
2563 hres
= DISP_E_TYPEMISMATCH
;
2565 hres
= DISP_E_BADVARTYPE
;
2567 /* if result type is not S_OK, then no need to go further */
2570 V_VT(out
) = resultvt
;
2573 /* Else proceed with formatting inputs to strings */
2576 VARIANT bstrvar_left
, bstrvar_right
;
2577 V_VT(out
) = VT_BSTR
;
2579 VariantInit(&bstrvar_left
);
2580 VariantInit(&bstrvar_right
);
2582 /* Convert left side variant to string */
2583 if (leftvt
!= VT_BSTR
)
2585 if (leftvt
== VT_BOOL
)
2587 /* Bools are handled as localized True/False strings instead of 0/-1 as in MSDN */
2588 V_VT(&bstrvar_left
) = VT_BSTR
;
2590 V_BSTR(&bstrvar_left
) = SysAllocString(str_true
);
2592 V_BSTR(&bstrvar_left
) = SysAllocString(str_false
);
2594 /* Fill with empty string for later concat with right side */
2595 else if (leftvt
== VT_NULL
)
2597 V_VT(&bstrvar_left
) = VT_BSTR
;
2598 V_BSTR(&bstrvar_left
) = SysAllocString(sz_empty
);
2602 hres
= VariantChangeTypeEx(&bstrvar_left
,left
,0,0,VT_BSTR
);
2604 VariantClear(&bstrvar_left
);
2605 VariantClear(&bstrvar_right
);
2606 if (leftvt
== VT_NULL
&& (rightvt
== VT_EMPTY
||
2607 rightvt
== VT_NULL
|| rightvt
== VT_I2
||
2608 rightvt
== VT_I4
|| rightvt
== VT_R4
||
2609 rightvt
== VT_R8
|| rightvt
== VT_CY
||
2610 rightvt
== VT_DATE
|| rightvt
== VT_BSTR
||
2611 rightvt
== VT_BOOL
|| rightvt
== VT_DECIMAL
||
2612 rightvt
== VT_I1
|| rightvt
== VT_UI1
||
2613 rightvt
== VT_UI2
|| rightvt
== VT_UI4
||
2614 rightvt
== VT_I8
|| rightvt
== VT_UI8
||
2615 rightvt
== VT_INT
|| rightvt
== VT_UINT
))
2616 return DISP_E_BADVARTYPE
;
2622 /* convert right side variant to string */
2623 if (rightvt
!= VT_BSTR
)
2625 if (rightvt
== VT_BOOL
)
2627 /* Bools are handled as localized True/False strings instead of 0/-1 as in MSDN */
2628 V_VT(&bstrvar_right
) = VT_BSTR
;
2630 V_BSTR(&bstrvar_right
) = SysAllocString(str_true
);
2632 V_BSTR(&bstrvar_right
) = SysAllocString(str_false
);
2634 /* Fill with empty string for later concat with right side */
2635 else if (rightvt
== VT_NULL
)
2637 V_VT(&bstrvar_right
) = VT_BSTR
;
2638 V_BSTR(&bstrvar_right
) = SysAllocString(sz_empty
);
2642 hres
= VariantChangeTypeEx(&bstrvar_right
,right
,0,0,VT_BSTR
);
2644 VariantClear(&bstrvar_left
);
2645 VariantClear(&bstrvar_right
);
2646 if (rightvt
== VT_NULL
&& (leftvt
== VT_EMPTY
||
2647 leftvt
== VT_NULL
|| leftvt
== VT_I2
||
2648 leftvt
== VT_I4
|| leftvt
== VT_R4
||
2649 leftvt
== VT_R8
|| leftvt
== VT_CY
||
2650 leftvt
== VT_DATE
|| leftvt
== VT_BSTR
||
2651 leftvt
== VT_BOOL
|| leftvt
== VT_DECIMAL
||
2652 leftvt
== VT_I1
|| leftvt
== VT_UI1
||
2653 leftvt
== VT_UI2
|| leftvt
== VT_UI4
||
2654 leftvt
== VT_I8
|| leftvt
== VT_UI8
||
2655 leftvt
== VT_INT
|| leftvt
== VT_UINT
))
2656 return DISP_E_BADVARTYPE
;
2662 /* Concat the resulting strings together */
2663 if (leftvt
== VT_BSTR
&& rightvt
== VT_BSTR
)
2664 VarBstrCat (V_BSTR(left
), V_BSTR(right
), &V_BSTR(out
));
2665 else if (leftvt
!= VT_BSTR
&& rightvt
!= VT_BSTR
)
2666 VarBstrCat (V_BSTR(&bstrvar_left
), V_BSTR(&bstrvar_right
), &V_BSTR(out
));
2667 else if (leftvt
!= VT_BSTR
&& rightvt
== VT_BSTR
)
2668 VarBstrCat (V_BSTR(&bstrvar_left
), V_BSTR(right
), &V_BSTR(out
));
2669 else if (leftvt
== VT_BSTR
&& rightvt
!= VT_BSTR
)
2670 VarBstrCat (V_BSTR(left
), V_BSTR(&bstrvar_right
), &V_BSTR(out
));
2672 VariantClear(&bstrvar_left
);
2673 VariantClear(&bstrvar_right
);
2679 /* Wrapper around VariantChangeTypeEx() which permits changing a
2680 variant with VT_RESERVED flag set. Needed by VarCmp. */
2681 static HRESULT
_VarChangeTypeExWrap (VARIANTARG
* pvargDest
,
2682 VARIANTARG
* pvargSrc
, LCID lcid
, USHORT wFlags
, VARTYPE vt
)
2684 VARIANTARG vtmpsrc
= *pvargSrc
;
2686 V_VT(&vtmpsrc
) &= ~VT_RESERVED
;
2687 return VariantChangeTypeEx(pvargDest
,&vtmpsrc
,lcid
,wFlags
,vt
);
2690 /**********************************************************************
2691 * VarCmp [OLEAUT32.176]
2693 * Compare two variants.
2696 * left [I] First variant
2697 * right [I] Second variant
2698 * lcid [I] LCID (locale identifier) for the comparison
2699 * flags [I] Flags to be used in the comparison:
2700 * NORM_IGNORECASE, NORM_IGNORENONSPACE, NORM_IGNORESYMBOLS,
2701 * NORM_IGNOREWIDTH, NORM_IGNOREKANATYPE, NORM_IGNOREKASHIDA
2704 * VARCMP_LT: left variant is less than right variant.
2705 * VARCMP_EQ: input variants are equal.
2706 * VARCMP_GT: left variant is greater than right variant.
2707 * VARCMP_NULL: either one of the input variants is NULL.
2708 * Failure: An HRESULT error code indicating the error.
2711 * Native VarCmp up to and including WinXP doesn't like I1, UI2, VT_UI4,
2712 * UI8 and UINT as input variants. INT is accepted only as left variant.
2714 * If both input variants are ERROR then VARCMP_EQ will be returned, else
2715 * an ERROR variant will trigger an error.
2717 * Both input variants can have VT_RESERVED flag set which is ignored
2718 * unless one and only one of the variants is a BSTR and the other one
2719 * is not an EMPTY variant. All four VT_RESERVED combinations have a
2720 * different meaning:
2721 * - BSTR and other: BSTR is always greater than the other variant.
2722 * - BSTR|VT_RESERVED and other: a string comparison is performed.
2723 * - BSTR and other|VT_RESERVED: If the BSTR is a number a numeric
2724 * comparison will take place else the BSTR is always greater.
2725 * - BSTR|VT_RESERVED and other|VT_RESERVED: It seems that the other
2726 * variant is ignored and the return value depends only on the sign
2727 * of the BSTR if it is a number else the BSTR is always greater. A
2728 * positive BSTR is greater, a negative one is smaller than the other
2732 * VarBstrCmp for the lcid and flags usage.
2734 HRESULT WINAPI
VarCmp(LPVARIANT left
, LPVARIANT right
, LCID lcid
, DWORD flags
)
2736 VARTYPE lvt
, rvt
, vt
;
2741 TRACE("(%s,%s,0x%08x,0x%08x)\n", debugstr_variant(left
), debugstr_variant(right
), lcid
, flags
);
2743 lvt
= V_VT(left
) & VT_TYPEMASK
;
2744 rvt
= V_VT(right
) & VT_TYPEMASK
;
2745 xmask
= (1 << lvt
) | (1 << rvt
);
2747 /* If we have any flag set except VT_RESERVED bail out.
2748 Same for the left input variant type > VT_INT and for the
2749 right input variant type > VT_I8. Yes, VT_INT is only supported
2750 as left variant. Go figure */
2751 if (((V_VT(left
) | V_VT(right
)) & ~VT_TYPEMASK
& ~VT_RESERVED
) ||
2752 lvt
> VT_INT
|| rvt
> VT_I8
) {
2753 return DISP_E_BADVARTYPE
;
2756 /* Don't ask me why but native VarCmp cannot handle: VT_I1, VT_UI2, VT_UI4,
2757 VT_UINT and VT_UI8. Tested with DCOM98, Win2k, WinXP */
2758 if (rvt
== VT_INT
|| xmask
& (VTBIT_I1
| VTBIT_UI2
| VTBIT_UI4
| VTBIT_UI8
|
2759 VTBIT_DISPATCH
| VTBIT_VARIANT
| VTBIT_UNKNOWN
| VTBIT_15
))
2760 return DISP_E_TYPEMISMATCH
;
2762 /* If both variants are VT_ERROR return VARCMP_EQ */
2763 if (xmask
== VTBIT_ERROR
)
2765 else if (xmask
& VTBIT_ERROR
)
2766 return DISP_E_TYPEMISMATCH
;
2768 if (xmask
& VTBIT_NULL
)
2774 /* Two BSTRs, ignore VT_RESERVED */
2775 if (xmask
== VTBIT_BSTR
)
2776 return VarBstrCmp(V_BSTR(left
), V_BSTR(right
), lcid
, flags
);
2778 /* A BSTR and another variant; we have to take care of VT_RESERVED */
2779 if (xmask
& VTBIT_BSTR
) {
2780 VARIANT
*bstrv
, *nonbv
;
2784 /* Swap the variants so the BSTR is always on the left */
2785 if (lvt
== VT_BSTR
) {
2796 /* BSTR and EMPTY: ignore VT_RESERVED */
2797 if (nonbvt
== VT_EMPTY
)
2798 rc
= (!V_BSTR(bstrv
) || !*V_BSTR(bstrv
)) ? VARCMP_EQ
: VARCMP_GT
;
2800 VARTYPE breserv
= V_VT(bstrv
) & ~VT_TYPEMASK
;
2801 VARTYPE nreserv
= V_VT(nonbv
) & ~VT_TYPEMASK
;
2803 if (!breserv
&& !nreserv
)
2804 /* No VT_RESERVED set ==> BSTR always greater */
2806 else if (breserv
&& !nreserv
) {
2807 /* BSTR has VT_RESERVED set. Do a string comparison */
2808 rc
= VariantChangeTypeEx(&rv
,nonbv
,lcid
,0,VT_BSTR
);
2811 rc
= VarBstrCmp(V_BSTR(bstrv
), V_BSTR(&rv
), lcid
, flags
);
2813 } else if (V_BSTR(bstrv
) && *V_BSTR(bstrv
)) {
2814 /* Non NULL nor empty BSTR */
2815 /* If the BSTR is not a number the BSTR is greater */
2816 rc
= _VarChangeTypeExWrap(&lv
,bstrv
,lcid
,0,VT_R8
);
2819 else if (breserv
&& nreserv
)
2820 /* FIXME: This is strange: with both VT_RESERVED set it
2821 looks like the result depends only on the sign of
2823 rc
= (V_R8(&lv
) >= 0) ? VARCMP_GT
: VARCMP_LT
;
2825 /* Numeric comparison, will be handled below.
2826 VARCMP_NULL used only to break out. */
2831 /* Empty or NULL BSTR */
2834 /* Fixup the return code if we swapped left and right */
2836 if (rc
== VARCMP_GT
)
2838 else if (rc
== VARCMP_LT
)
2841 if (rc
!= VARCMP_NULL
)
2845 if (xmask
& VTBIT_DECIMAL
)
2847 else if (xmask
& VTBIT_BSTR
)
2849 else if (xmask
& VTBIT_R4
)
2851 else if (xmask
& (VTBIT_R8
| VTBIT_DATE
))
2853 else if (xmask
& VTBIT_CY
)
2859 /* Coerce the variants */
2860 rc
= _VarChangeTypeExWrap(&lv
,left
,lcid
,0,vt
);
2861 if (rc
== DISP_E_OVERFLOW
&& vt
!= VT_R8
) {
2862 /* Overflow, change to R8 */
2864 rc
= _VarChangeTypeExWrap(&lv
,left
,lcid
,0,vt
);
2868 rc
= _VarChangeTypeExWrap(&rv
,right
,lcid
,0,vt
);
2869 if (rc
== DISP_E_OVERFLOW
&& vt
!= VT_R8
) {
2870 /* Overflow, change to R8 */
2872 rc
= _VarChangeTypeExWrap(&lv
,left
,lcid
,0,vt
);
2875 rc
= _VarChangeTypeExWrap(&rv
,right
,lcid
,0,vt
);
2880 #define _VARCMP(a,b) \
2881 (((a) == (b)) ? VARCMP_EQ : (((a) < (b)) ? VARCMP_LT : VARCMP_GT))
2885 return VarCyCmp(V_CY(&lv
), V_CY(&rv
));
2887 return VarDecCmp(&V_DECIMAL(&lv
), &V_DECIMAL(&rv
));
2889 return _VARCMP(V_I8(&lv
), V_I8(&rv
));
2891 return _VARCMP(V_R4(&lv
), V_R4(&rv
));
2893 return _VARCMP(V_R8(&lv
), V_R8(&rv
));
2895 /* We should never get here */
2901 static HRESULT
VARIANT_FetchDispatchValue(LPVARIANT pvDispatch
, LPVARIANT pValue
)
2904 static DISPPARAMS emptyParams
= { NULL
, NULL
, 0, 0 };
2906 if ((V_VT(pvDispatch
) & VT_TYPEMASK
) == VT_DISPATCH
) {
2907 if (NULL
== V_DISPATCH(pvDispatch
)) return DISP_E_TYPEMISMATCH
;
2908 hres
= IDispatch_Invoke(V_DISPATCH(pvDispatch
), DISPID_VALUE
, &IID_NULL
,
2909 LOCALE_USER_DEFAULT
, DISPATCH_PROPERTYGET
, &emptyParams
, pValue
,
2912 hres
= DISP_E_TYPEMISMATCH
;
2917 /**********************************************************************
2918 * VarAnd [OLEAUT32.142]
2920 * Computes the logical AND of two variants.
2923 * left [I] First variant
2924 * right [I] Second variant
2925 * result [O] Result variant
2929 * Failure: An HRESULT error code indicating the error.
2931 HRESULT WINAPI
VarAnd(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
2933 HRESULT hres
= S_OK
;
2934 VARTYPE resvt
= VT_EMPTY
;
2935 VARTYPE leftvt
,rightvt
;
2936 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
2937 VARIANT varLeft
, varRight
;
2938 VARIANT tempLeft
, tempRight
;
2940 VariantInit(&varLeft
);
2941 VariantInit(&varRight
);
2942 VariantInit(&tempLeft
);
2943 VariantInit(&tempRight
);
2945 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
2947 /* Handle VT_DISPATCH by storing and taking address of returned value */
2948 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
2950 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
2951 if (FAILED(hres
)) goto VarAnd_Exit
;
2954 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
2956 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
2957 if (FAILED(hres
)) goto VarAnd_Exit
;
2961 leftvt
= V_VT(left
)&VT_TYPEMASK
;
2962 rightvt
= V_VT(right
)&VT_TYPEMASK
;
2963 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
2964 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
2966 if (leftExtraFlags
!= rightExtraFlags
)
2968 hres
= DISP_E_BADVARTYPE
;
2971 ExtraFlags
= leftExtraFlags
;
2973 /* Native VarAnd always returns an error when using extra
2974 * flags or if the variant combination is I8 and INT.
2976 if ((leftvt
== VT_I8
&& rightvt
== VT_INT
) ||
2977 (leftvt
== VT_INT
&& rightvt
== VT_I8
) ||
2980 hres
= DISP_E_BADVARTYPE
;
2984 /* Determine return type */
2985 else if (leftvt
== VT_I8
|| rightvt
== VT_I8
)
2987 else if (leftvt
== VT_I4
|| rightvt
== VT_I4
||
2988 leftvt
== VT_UINT
|| rightvt
== VT_UINT
||
2989 leftvt
== VT_INT
|| rightvt
== VT_INT
||
2990 leftvt
== VT_R4
|| rightvt
== VT_R4
||
2991 leftvt
== VT_R8
|| rightvt
== VT_R8
||
2992 leftvt
== VT_CY
|| rightvt
== VT_CY
||
2993 leftvt
== VT_DATE
|| rightvt
== VT_DATE
||
2994 leftvt
== VT_I1
|| rightvt
== VT_I1
||
2995 leftvt
== VT_UI2
|| rightvt
== VT_UI2
||
2996 leftvt
== VT_UI4
|| rightvt
== VT_UI4
||
2997 leftvt
== VT_UI8
|| rightvt
== VT_UI8
||
2998 leftvt
== VT_DECIMAL
|| rightvt
== VT_DECIMAL
)
3000 else if (leftvt
== VT_UI1
|| rightvt
== VT_UI1
||
3001 leftvt
== VT_I2
|| rightvt
== VT_I2
||
3002 leftvt
== VT_EMPTY
|| rightvt
== VT_EMPTY
)
3003 if ((leftvt
== VT_NULL
&& rightvt
== VT_UI1
) ||
3004 (leftvt
== VT_UI1
&& rightvt
== VT_NULL
) ||
3005 (leftvt
== VT_UI1
&& rightvt
== VT_UI1
))
3009 else if (leftvt
== VT_BOOL
|| rightvt
== VT_BOOL
||
3010 (leftvt
== VT_BSTR
&& rightvt
== VT_BSTR
))
3012 else if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
||
3013 leftvt
== VT_BSTR
|| rightvt
== VT_BSTR
)
3017 hres
= DISP_E_BADVARTYPE
;
3021 if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
)
3024 * Special cases for when left variant is VT_NULL
3025 * (VT_NULL & 0 = VT_NULL, VT_NULL & value = value)
3027 if (leftvt
== VT_NULL
)
3032 case VT_I1
: if (V_I1(right
)) resvt
= VT_NULL
; break;
3033 case VT_UI1
: if (V_UI1(right
)) resvt
= VT_NULL
; break;
3034 case VT_I2
: if (V_I2(right
)) resvt
= VT_NULL
; break;
3035 case VT_UI2
: if (V_UI2(right
)) resvt
= VT_NULL
; break;
3036 case VT_I4
: if (V_I4(right
)) resvt
= VT_NULL
; break;
3037 case VT_UI4
: if (V_UI4(right
)) resvt
= VT_NULL
; break;
3038 case VT_I8
: if (V_I8(right
)) resvt
= VT_NULL
; break;
3039 case VT_UI8
: if (V_UI8(right
)) resvt
= VT_NULL
; break;
3040 case VT_INT
: if (V_INT(right
)) resvt
= VT_NULL
; break;
3041 case VT_UINT
: if (V_UINT(right
)) resvt
= VT_NULL
; break;
3042 case VT_BOOL
: if (V_BOOL(right
)) resvt
= VT_NULL
; break;
3043 case VT_R4
: if (V_R4(right
)) resvt
= VT_NULL
; break;
3044 case VT_R8
: if (V_R8(right
)) resvt
= VT_NULL
; break;
3046 if(V_CY(right
).int64
)
3050 if (DEC_HI32(&V_DECIMAL(right
)) ||
3051 DEC_LO64(&V_DECIMAL(right
)))
3055 hres
= VarBoolFromStr(V_BSTR(right
),
3056 LOCALE_USER_DEFAULT
, VAR_LOCALBOOL
, &b
);
3060 V_VT(result
) = VT_NULL
;
3063 V_VT(result
) = VT_BOOL
;
3069 V_VT(result
) = resvt
;
3073 hres
= VariantCopy(&varLeft
, left
);
3074 if (FAILED(hres
)) goto VarAnd_Exit
;
3076 hres
= VariantCopy(&varRight
, right
);
3077 if (FAILED(hres
)) goto VarAnd_Exit
;
3079 if (resvt
== VT_I4
&& V_VT(&varLeft
) == VT_UI4
)
3080 V_VT(&varLeft
) = VT_I4
; /* Don't overflow */
3085 if (V_VT(&varLeft
) == VT_BSTR
&&
3086 FAILED(VarR8FromStr(V_BSTR(&varLeft
),
3087 LOCALE_USER_DEFAULT
, 0, &d
)))
3088 hres
= VariantChangeType(&varLeft
,&varLeft
,
3089 VARIANT_LOCALBOOL
, VT_BOOL
);
3090 if (SUCCEEDED(hres
) && V_VT(&varLeft
) != resvt
)
3091 hres
= VariantChangeType(&varLeft
,&varLeft
,0,resvt
);
3092 if (FAILED(hres
)) goto VarAnd_Exit
;
3095 if (resvt
== VT_I4
&& V_VT(&varRight
) == VT_UI4
)
3096 V_VT(&varRight
) = VT_I4
; /* Don't overflow */
3101 if (V_VT(&varRight
) == VT_BSTR
&&
3102 FAILED(VarR8FromStr(V_BSTR(&varRight
),
3103 LOCALE_USER_DEFAULT
, 0, &d
)))
3104 hres
= VariantChangeType(&varRight
, &varRight
,
3105 VARIANT_LOCALBOOL
, VT_BOOL
);
3106 if (SUCCEEDED(hres
) && V_VT(&varRight
) != resvt
)
3107 hres
= VariantChangeType(&varRight
, &varRight
, 0, resvt
);
3108 if (FAILED(hres
)) goto VarAnd_Exit
;
3111 V_VT(result
) = resvt
;
3115 V_I8(result
) = V_I8(&varLeft
) & V_I8(&varRight
);
3118 V_I4(result
) = V_I4(&varLeft
) & V_I4(&varRight
);
3121 V_I2(result
) = V_I2(&varLeft
) & V_I2(&varRight
);
3124 V_UI1(result
) = V_UI1(&varLeft
) & V_UI1(&varRight
);
3127 V_BOOL(result
) = V_BOOL(&varLeft
) & V_BOOL(&varRight
);
3130 FIXME("Couldn't bitwise AND variant types %d,%d\n",
3135 VariantClear(&varLeft
);
3136 VariantClear(&varRight
);
3137 VariantClear(&tempLeft
);
3138 VariantClear(&tempRight
);
3143 /**********************************************************************
3144 * VarAdd [OLEAUT32.141]
3149 * left [I] First variant
3150 * right [I] Second variant
3151 * result [O] Result variant
3155 * Failure: An HRESULT error code indicating the error.
3158 * Native VarAdd up to and including WinXP doesn't like I1, UI2, UI4,
3159 * UI8, INT and UINT as input variants.
3161 * Native VarAdd doesn't check for NULL in/out pointers and crashes. We do the
3165 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3168 HRESULT WINAPI
VarAdd(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3171 VARTYPE lvt
, rvt
, resvt
, tvt
;
3173 VARIANT tempLeft
, tempRight
;
3176 /* Variant priority for coercion. Sorted from lowest to highest.
3177 VT_ERROR shows an invalid input variant type. */
3178 enum coerceprio
{ vt_EMPTY
, vt_UI1
, vt_I2
, vt_I4
, vt_I8
, vt_BSTR
,vt_R4
,
3179 vt_R8
, vt_CY
, vt_DATE
, vt_DECIMAL
, vt_DISPATCH
, vt_NULL
,
3181 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3182 static const VARTYPE prio2vt
[] = { VT_EMPTY
, VT_UI1
, VT_I2
, VT_I4
, VT_I8
, VT_BSTR
, VT_R4
,
3183 VT_R8
, VT_CY
, VT_DATE
, VT_DECIMAL
, VT_DISPATCH
,
3184 VT_NULL
, VT_ERROR
};
3186 /* Mapping for coercion from input variant to priority of result variant. */
3187 static const VARTYPE coerce
[] = {
3188 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3189 vt_EMPTY
, vt_NULL
, vt_I2
, vt_I4
, vt_R4
,
3190 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3191 vt_R8
, vt_CY
, vt_DATE
, vt_BSTR
, vt_DISPATCH
,
3192 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3193 vt_ERROR
, vt_I2
, vt_ERROR
, vt_ERROR
, vt_DECIMAL
,
3194 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3195 vt_ERROR
, vt_ERROR
, vt_UI1
, vt_ERROR
, vt_ERROR
, vt_I8
3198 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
3203 VariantInit(&tempLeft
);
3204 VariantInit(&tempRight
);
3206 /* Handle VT_DISPATCH by storing and taking address of returned value */
3207 if ((V_VT(left
) & VT_TYPEMASK
) != VT_NULL
&& (V_VT(right
) & VT_TYPEMASK
) != VT_NULL
)
3209 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
3211 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
3212 if (FAILED(hres
)) goto end
;
3215 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
3217 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
3218 if (FAILED(hres
)) goto end
;
3223 lvt
= V_VT(left
)&VT_TYPEMASK
;
3224 rvt
= V_VT(right
)&VT_TYPEMASK
;
3226 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3227 Same for any input variant type > VT_I8 */
3228 if (V_VT(left
) & ~VT_TYPEMASK
|| V_VT(right
) & ~VT_TYPEMASK
||
3229 lvt
> VT_I8
|| rvt
> VT_I8
) {
3230 hres
= DISP_E_BADVARTYPE
;
3234 /* Determine the variant type to coerce to. */
3235 if (coerce
[lvt
] > coerce
[rvt
]) {
3236 resvt
= prio2vt
[coerce
[lvt
]];
3237 tvt
= prio2vt
[coerce
[rvt
]];
3239 resvt
= prio2vt
[coerce
[rvt
]];
3240 tvt
= prio2vt
[coerce
[lvt
]];
3243 /* Special cases where the result variant type is defined by both
3244 input variants and not only that with the highest priority */
3245 if (resvt
== VT_BSTR
) {
3246 if (tvt
== VT_EMPTY
|| tvt
== VT_BSTR
)
3251 if (resvt
== VT_R4
&& (tvt
== VT_BSTR
|| tvt
== VT_I8
|| tvt
== VT_I4
))
3254 /* For overflow detection use the biggest compatible type for the
3258 hres
= DISP_E_BADVARTYPE
;
3262 V_VT(result
) = VT_NULL
;
3265 FIXME("cannot handle variant type VT_DISPATCH\n");
3266 hres
= DISP_E_TYPEMISMATCH
;
3285 /* Now coerce the variants */
3286 hres
= VariantChangeType(&lv
, left
, 0, tvt
);
3289 hres
= VariantChangeType(&rv
, right
, 0, tvt
);
3295 V_VT(result
) = resvt
;
3298 hres
= VarDecAdd(&V_DECIMAL(&lv
), &V_DECIMAL(&rv
),
3299 &V_DECIMAL(result
));
3302 hres
= VarCyAdd(V_CY(&lv
), V_CY(&rv
), &V_CY(result
));
3305 /* We do not add those, we concatenate them. */
3306 hres
= VarBstrCat(V_BSTR(&lv
), V_BSTR(&rv
), &V_BSTR(result
));
3309 /* Overflow detection */
3310 r8res
= (double)V_I8(&lv
) + (double)V_I8(&rv
);
3311 if (r8res
> (double)I8_MAX
|| r8res
< (double)I8_MIN
) {
3312 V_VT(result
) = VT_R8
;
3313 V_R8(result
) = r8res
;
3317 V_I8(&tv
) = V_I8(&lv
) + V_I8(&rv
);
3322 /* FIXME: overflow detection */
3323 V_R8(&tv
) = V_R8(&lv
) + V_R8(&rv
);
3326 ERR("We shouldn't get here! tvt = %d!\n", tvt
);
3330 if ((hres
= VariantChangeType(result
, &tv
, 0, resvt
)) != S_OK
) {
3331 /* Overflow! Change to the vartype with the next higher priority.
3332 With one exception: I4 ==> R8 even if it would fit in I8 */
3336 resvt
= prio2vt
[coerce
[resvt
] + 1];
3337 hres
= VariantChangeType(result
, &tv
, 0, resvt
);
3340 hres
= VariantCopy(result
, &tv
);
3344 V_VT(result
) = VT_EMPTY
;
3345 V_I4(result
) = 0; /* No V_EMPTY */
3350 VariantClear(&tempLeft
);
3351 VariantClear(&tempRight
);
3352 TRACE("returning 0x%8x %s\n", hres
, debugstr_variant(result
));
3356 /**********************************************************************
3357 * VarMul [OLEAUT32.156]
3359 * Multiply two variants.
3362 * left [I] First variant
3363 * right [I] Second variant
3364 * result [O] Result variant
3368 * Failure: An HRESULT error code indicating the error.
3371 * Native VarMul up to and including WinXP doesn't like I1, UI2, UI4,
3372 * UI8, INT and UINT as input variants. But it can multiply apples with oranges.
3374 * Native VarMul doesn't check for NULL in/out pointers and crashes. We do the
3378 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3381 HRESULT WINAPI
VarMul(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3384 VARTYPE lvt
, rvt
, resvt
, tvt
;
3386 VARIANT tempLeft
, tempRight
;
3389 /* Variant priority for coercion. Sorted from lowest to highest.
3390 VT_ERROR shows an invalid input variant type. */
3391 enum coerceprio
{ vt_UI1
= 0, vt_I2
, vt_I4
, vt_I8
, vt_CY
, vt_R4
, vt_R8
,
3392 vt_DECIMAL
, vt_NULL
, vt_ERROR
};
3393 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3394 static const VARTYPE prio2vt
[] = { VT_UI1
, VT_I2
, VT_I4
, VT_I8
, VT_CY
, VT_R4
, VT_R8
,
3395 VT_DECIMAL
, VT_NULL
, VT_ERROR
};
3397 /* Mapping for coercion from input variant to priority of result variant. */
3398 static const VARTYPE coerce
[] = {
3399 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3400 vt_UI1
, vt_NULL
, vt_I2
, vt_I4
, vt_R4
,
3401 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3402 vt_R8
, vt_CY
, vt_R8
, vt_R8
, vt_ERROR
,
3403 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3404 vt_ERROR
, vt_I2
, vt_ERROR
, vt_ERROR
, vt_DECIMAL
,
3405 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3406 vt_ERROR
, vt_ERROR
, vt_UI1
, vt_ERROR
, vt_ERROR
, vt_I8
3409 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
3414 VariantInit(&tempLeft
);
3415 VariantInit(&tempRight
);
3417 /* Handle VT_DISPATCH by storing and taking address of returned value */
3418 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
3420 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
3421 if (FAILED(hres
)) goto end
;
3424 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
3426 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
3427 if (FAILED(hres
)) goto end
;
3431 lvt
= V_VT(left
)&VT_TYPEMASK
;
3432 rvt
= V_VT(right
)&VT_TYPEMASK
;
3434 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3435 Same for any input variant type > VT_I8 */
3436 if (V_VT(left
) & ~VT_TYPEMASK
|| V_VT(right
) & ~VT_TYPEMASK
||
3437 lvt
> VT_I8
|| rvt
> VT_I8
) {
3438 hres
= DISP_E_BADVARTYPE
;
3442 /* Determine the variant type to coerce to. */
3443 if (coerce
[lvt
] > coerce
[rvt
]) {
3444 resvt
= prio2vt
[coerce
[lvt
]];
3445 tvt
= prio2vt
[coerce
[rvt
]];
3447 resvt
= prio2vt
[coerce
[rvt
]];
3448 tvt
= prio2vt
[coerce
[lvt
]];
3451 /* Special cases where the result variant type is defined by both
3452 input variants and not only that with the highest priority */
3453 if (resvt
== VT_R4
&& (tvt
== VT_CY
|| tvt
== VT_I8
|| tvt
== VT_I4
))
3455 if (lvt
== VT_EMPTY
&& rvt
== VT_EMPTY
)
3458 /* For overflow detection use the biggest compatible type for the
3462 hres
= DISP_E_BADVARTYPE
;
3466 V_VT(result
) = VT_NULL
;
3481 /* Now coerce the variants */
3482 hres
= VariantChangeType(&lv
, left
, 0, tvt
);
3485 hres
= VariantChangeType(&rv
, right
, 0, tvt
);
3492 V_VT(result
) = resvt
;
3495 hres
= VarDecMul(&V_DECIMAL(&lv
), &V_DECIMAL(&rv
),
3496 &V_DECIMAL(result
));
3499 hres
= VarCyMul(V_CY(&lv
), V_CY(&rv
), &V_CY(result
));
3502 /* Overflow detection */
3503 r8res
= (double)V_I8(&lv
) * (double)V_I8(&rv
);
3504 if (r8res
> (double)I8_MAX
|| r8res
< (double)I8_MIN
) {
3505 V_VT(result
) = VT_R8
;
3506 V_R8(result
) = r8res
;
3509 V_I8(&tv
) = V_I8(&lv
) * V_I8(&rv
);
3512 /* FIXME: overflow detection */
3513 V_R8(&tv
) = V_R8(&lv
) * V_R8(&rv
);
3516 ERR("We shouldn't get here! tvt = %d!\n", tvt
);
3520 while ((hres
= VariantChangeType(result
, &tv
, 0, resvt
)) != S_OK
) {
3521 /* Overflow! Change to the vartype with the next higher priority.
3522 With one exception: I4 ==> R8 even if it would fit in I8 */
3526 resvt
= prio2vt
[coerce
[resvt
] + 1];
3529 hres
= VariantCopy(result
, &tv
);
3533 V_VT(result
) = VT_EMPTY
;
3534 V_I4(result
) = 0; /* No V_EMPTY */
3539 VariantClear(&tempLeft
);
3540 VariantClear(&tempRight
);
3541 TRACE("returning 0x%8x %s\n", hres
, debugstr_variant(result
));
3545 /**********************************************************************
3546 * VarDiv [OLEAUT32.143]
3548 * Divides one variant with another.
3551 * left [I] First variant
3552 * right [I] Second variant
3553 * result [O] Result variant
3557 * Failure: An HRESULT error code indicating the error.
3559 HRESULT WINAPI
VarDiv(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3561 HRESULT hres
= S_OK
;
3562 VARTYPE resvt
= VT_EMPTY
;
3563 VARTYPE leftvt
,rightvt
;
3564 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
3566 VARIANT tempLeft
, tempRight
;
3568 VariantInit(&tempLeft
);
3569 VariantInit(&tempRight
);
3573 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
3575 /* Handle VT_DISPATCH by storing and taking address of returned value */
3576 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
3578 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
3579 if (FAILED(hres
)) goto end
;
3582 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
3584 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
3585 if (FAILED(hres
)) goto end
;
3589 leftvt
= V_VT(left
)&VT_TYPEMASK
;
3590 rightvt
= V_VT(right
)&VT_TYPEMASK
;
3591 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
3592 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
3594 if (leftExtraFlags
!= rightExtraFlags
)
3596 hres
= DISP_E_BADVARTYPE
;
3599 ExtraFlags
= leftExtraFlags
;
3601 /* Native VarDiv always returns an error when using extra flags */
3602 if (ExtraFlags
!= 0)
3604 hres
= DISP_E_BADVARTYPE
;
3608 /* Determine return type */
3609 if (!(rightvt
== VT_EMPTY
))
3611 if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
)
3613 V_VT(result
) = VT_NULL
;
3617 else if (leftvt
== VT_DECIMAL
|| rightvt
== VT_DECIMAL
)
3619 else if (leftvt
== VT_I8
|| rightvt
== VT_I8
||
3620 leftvt
== VT_CY
|| rightvt
== VT_CY
||
3621 leftvt
== VT_DATE
|| rightvt
== VT_DATE
||
3622 leftvt
== VT_I4
|| rightvt
== VT_I4
||
3623 leftvt
== VT_BSTR
|| rightvt
== VT_BSTR
||
3624 leftvt
== VT_I2
|| rightvt
== VT_I2
||
3625 leftvt
== VT_BOOL
|| rightvt
== VT_BOOL
||
3626 leftvt
== VT_R8
|| rightvt
== VT_R8
||
3627 leftvt
== VT_UI1
|| rightvt
== VT_UI1
)
3629 if ((leftvt
== VT_UI1
&& rightvt
== VT_R4
) ||
3630 (leftvt
== VT_R4
&& rightvt
== VT_UI1
))
3632 else if ((leftvt
== VT_R4
&& (rightvt
== VT_BOOL
||
3633 rightvt
== VT_I2
)) || (rightvt
== VT_R4
&&
3634 (leftvt
== VT_BOOL
|| leftvt
== VT_I2
)))
3639 else if (leftvt
== VT_R4
|| rightvt
== VT_R4
)
3642 else if (leftvt
== VT_NULL
&& rightvt
== VT_EMPTY
)
3644 V_VT(result
) = VT_NULL
;
3650 hres
= DISP_E_BADVARTYPE
;
3654 /* coerce to the result type */
3655 hres
= VariantChangeType(&lv
, left
, 0, resvt
);
3656 if (hres
!= S_OK
) goto end
;
3658 hres
= VariantChangeType(&rv
, right
, 0, resvt
);
3659 if (hres
!= S_OK
) goto end
;
3662 V_VT(result
) = resvt
;
3666 if (V_R4(&lv
) == 0.0 && V_R4(&rv
) == 0.0)
3668 hres
= DISP_E_OVERFLOW
;
3669 V_VT(result
) = VT_EMPTY
;
3671 else if (V_R4(&rv
) == 0.0)
3673 hres
= DISP_E_DIVBYZERO
;
3674 V_VT(result
) = VT_EMPTY
;
3677 V_R4(result
) = V_R4(&lv
) / V_R4(&rv
);
3680 if (V_R8(&lv
) == 0.0 && V_R8(&rv
) == 0.0)
3682 hres
= DISP_E_OVERFLOW
;
3683 V_VT(result
) = VT_EMPTY
;
3685 else if (V_R8(&rv
) == 0.0)
3687 hres
= DISP_E_DIVBYZERO
;
3688 V_VT(result
) = VT_EMPTY
;
3691 V_R8(result
) = V_R8(&lv
) / V_R8(&rv
);
3694 hres
= VarDecDiv(&(V_DECIMAL(&lv
)), &(V_DECIMAL(&rv
)), &(V_DECIMAL(result
)));
3701 VariantClear(&tempLeft
);
3702 VariantClear(&tempRight
);
3703 TRACE("returning 0x%8x %s\n", hres
, debugstr_variant(result
));
3707 /**********************************************************************
3708 * VarSub [OLEAUT32.159]
3710 * Subtract two variants.
3713 * left [I] First variant
3714 * right [I] Second variant
3715 * result [O] Result variant
3719 * Failure: An HRESULT error code indicating the error.
3721 HRESULT WINAPI
VarSub(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3723 HRESULT hres
= S_OK
;
3724 VARTYPE resvt
= VT_EMPTY
;
3725 VARTYPE leftvt
,rightvt
;
3726 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
3728 VARIANT tempLeft
, tempRight
;
3732 VariantInit(&tempLeft
);
3733 VariantInit(&tempRight
);
3735 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
3737 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
&&
3738 (V_VT(left
)&(~VT_TYPEMASK
)) == 0 &&
3739 (V_VT(right
) & VT_TYPEMASK
) != VT_NULL
)
3741 if (NULL
== V_DISPATCH(left
)) {
3742 if ((V_VT(right
) & VT_TYPEMASK
) >= VT_INT_PTR
)
3743 hres
= DISP_E_BADVARTYPE
;
3744 else if ((V_VT(right
) & VT_TYPEMASK
) >= VT_UI8
&&
3745 (V_VT(right
) & VT_TYPEMASK
) < VT_RECORD
)
3746 hres
= DISP_E_BADVARTYPE
;
3747 else switch (V_VT(right
) & VT_TYPEMASK
)
3755 hres
= DISP_E_BADVARTYPE
;
3757 if (FAILED(hres
)) goto end
;
3759 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
3760 if (FAILED(hres
)) goto end
;
3763 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
&&
3764 (V_VT(right
)&(~VT_TYPEMASK
)) == 0 &&
3765 (V_VT(left
) & VT_TYPEMASK
) != VT_NULL
)
3767 if (NULL
== V_DISPATCH(right
))
3769 if ((V_VT(left
) & VT_TYPEMASK
) >= VT_INT_PTR
)
3770 hres
= DISP_E_BADVARTYPE
;
3771 else if ((V_VT(left
) & VT_TYPEMASK
) >= VT_UI8
&&
3772 (V_VT(left
) & VT_TYPEMASK
) < VT_RECORD
)
3773 hres
= DISP_E_BADVARTYPE
;
3774 else switch (V_VT(left
) & VT_TYPEMASK
)
3782 hres
= DISP_E_BADVARTYPE
;
3784 if (FAILED(hres
)) goto end
;
3786 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
3787 if (FAILED(hres
)) goto end
;
3791 leftvt
= V_VT(left
)&VT_TYPEMASK
;
3792 rightvt
= V_VT(right
)&VT_TYPEMASK
;
3793 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
3794 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
3796 if (leftExtraFlags
!= rightExtraFlags
)
3798 hres
= DISP_E_BADVARTYPE
;
3801 ExtraFlags
= leftExtraFlags
;
3803 /* determine return type and return code */
3804 /* All extra flags produce errors */
3805 if (ExtraFlags
== (VT_VECTOR
|VT_BYREF
|VT_RESERVED
) ||
3806 ExtraFlags
== (VT_VECTOR
|VT_RESERVED
) ||
3807 ExtraFlags
== (VT_VECTOR
|VT_BYREF
) ||
3808 ExtraFlags
== (VT_BYREF
|VT_RESERVED
) ||
3809 ExtraFlags
== VT_VECTOR
||
3810 ExtraFlags
== VT_BYREF
||
3811 ExtraFlags
== VT_RESERVED
)
3813 hres
= DISP_E_BADVARTYPE
;
3816 else if (ExtraFlags
>= VT_ARRAY
)
3818 hres
= DISP_E_TYPEMISMATCH
;
3821 /* Native VarSub cannot handle: VT_I1, VT_UI2, VT_UI4,
3822 VT_INT, VT_UINT and VT_UI8. Tested with WinXP */
3823 else if (leftvt
== VT_CLSID
|| rightvt
== VT_CLSID
||
3824 leftvt
== VT_VARIANT
|| rightvt
== VT_VARIANT
||
3825 leftvt
== VT_I1
|| rightvt
== VT_I1
||
3826 leftvt
== VT_UI2
|| rightvt
== VT_UI2
||
3827 leftvt
== VT_UI4
|| rightvt
== VT_UI4
||
3828 leftvt
== VT_UI8
|| rightvt
== VT_UI8
||
3829 leftvt
== VT_INT
|| rightvt
== VT_INT
||
3830 leftvt
== VT_UINT
|| rightvt
== VT_UINT
||
3831 leftvt
== VT_UNKNOWN
|| rightvt
== VT_UNKNOWN
||
3832 leftvt
== VT_RECORD
|| rightvt
== VT_RECORD
)
3834 if (leftvt
== VT_RECORD
&& rightvt
== VT_I8
)
3835 hres
= DISP_E_TYPEMISMATCH
;
3836 else if (leftvt
< VT_UI1
&& rightvt
== VT_RECORD
)
3837 hres
= DISP_E_TYPEMISMATCH
;
3838 else if (leftvt
>= VT_UI1
&& rightvt
== VT_RECORD
)
3839 hres
= DISP_E_TYPEMISMATCH
;
3840 else if (leftvt
== VT_RECORD
&& rightvt
<= VT_UI1
)
3841 hres
= DISP_E_TYPEMISMATCH
;
3842 else if (leftvt
== VT_RECORD
&& rightvt
> VT_UI1
)
3843 hres
= DISP_E_BADVARTYPE
;
3845 hres
= DISP_E_BADVARTYPE
;
3848 /* The following flags/types are invalid for left variant */
3849 else if (!((leftvt
<= VT_LPWSTR
|| leftvt
== VT_RECORD
||
3850 leftvt
== VT_CLSID
) && leftvt
!= (VARTYPE
)15 /* undefined vt */ &&
3851 (leftvt
< VT_VOID
|| leftvt
> VT_LPWSTR
)))
3853 hres
= DISP_E_BADVARTYPE
;
3856 /* The following flags/types are invalid for right variant */
3857 else if (!((rightvt
<= VT_LPWSTR
|| rightvt
== VT_RECORD
||
3858 rightvt
== VT_CLSID
) && rightvt
!= (VARTYPE
)15 /* undefined vt */ &&
3859 (rightvt
< VT_VOID
|| rightvt
> VT_LPWSTR
)))
3861 hres
= DISP_E_BADVARTYPE
;
3864 else if ((leftvt
== VT_NULL
&& rightvt
== VT_DISPATCH
) ||
3865 (leftvt
== VT_DISPATCH
&& rightvt
== VT_NULL
))
3867 else if (leftvt
== VT_DISPATCH
|| rightvt
== VT_DISPATCH
||
3868 leftvt
== VT_ERROR
|| rightvt
== VT_ERROR
)
3870 hres
= DISP_E_TYPEMISMATCH
;
3873 else if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
)
3875 else if ((leftvt
== VT_EMPTY
&& rightvt
== VT_BSTR
) ||
3876 (leftvt
== VT_DATE
&& rightvt
== VT_DATE
) ||
3877 (leftvt
== VT_BSTR
&& rightvt
== VT_EMPTY
) ||
3878 (leftvt
== VT_BSTR
&& rightvt
== VT_BSTR
))
3880 else if (leftvt
== VT_DECIMAL
|| rightvt
== VT_DECIMAL
)
3882 else if (leftvt
== VT_DATE
|| rightvt
== VT_DATE
)
3884 else if (leftvt
== VT_CY
|| rightvt
== VT_CY
)
3886 else if (leftvt
== VT_R8
|| rightvt
== VT_R8
)
3888 else if (leftvt
== VT_BSTR
|| rightvt
== VT_BSTR
)
3890 else if (leftvt
== VT_R4
|| rightvt
== VT_R4
)
3892 if (leftvt
== VT_I4
|| rightvt
== VT_I4
||
3893 leftvt
== VT_I8
|| rightvt
== VT_I8
)
3898 else if (leftvt
== VT_I8
|| rightvt
== VT_I8
)
3900 else if (leftvt
== VT_I4
|| rightvt
== VT_I4
)
3902 else if (leftvt
== VT_I2
|| rightvt
== VT_I2
||
3903 leftvt
== VT_BOOL
|| rightvt
== VT_BOOL
||
3904 (leftvt
== VT_EMPTY
&& rightvt
== VT_EMPTY
))
3906 else if (leftvt
== VT_UI1
|| rightvt
== VT_UI1
)
3910 hres
= DISP_E_TYPEMISMATCH
;
3914 /* coerce to the result type */
3915 if (leftvt
== VT_BSTR
&& rightvt
== VT_DATE
)
3916 hres
= VariantChangeType(&lv
, left
, 0, VT_R8
);
3918 hres
= VariantChangeType(&lv
, left
, 0, resvt
);
3919 if (hres
!= S_OK
) goto end
;
3920 if (leftvt
== VT_DATE
&& rightvt
== VT_BSTR
)
3921 hres
= VariantChangeType(&rv
, right
, 0, VT_R8
);
3923 hres
= VariantChangeType(&rv
, right
, 0, resvt
);
3924 if (hres
!= S_OK
) goto end
;
3927 V_VT(result
) = resvt
;
3933 V_DATE(result
) = V_DATE(&lv
) - V_DATE(&rv
);
3936 hres
= VarCySub(V_CY(&lv
), V_CY(&rv
), &(V_CY(result
)));
3939 V_R4(result
) = V_R4(&lv
) - V_R4(&rv
);
3942 V_I8(result
) = V_I8(&lv
) - V_I8(&rv
);
3945 V_I4(result
) = V_I4(&lv
) - V_I4(&rv
);
3948 V_I2(result
) = V_I2(&lv
) - V_I2(&rv
);
3951 V_I1(result
) = V_I1(&lv
) - V_I1(&rv
);
3954 V_UI1(result
) = V_UI2(&lv
) - V_UI1(&rv
);
3957 V_R8(result
) = V_R8(&lv
) - V_R8(&rv
);
3960 hres
= VarDecSub(&(V_DECIMAL(&lv
)), &(V_DECIMAL(&rv
)), &(V_DECIMAL(result
)));
3967 VariantClear(&tempLeft
);
3968 VariantClear(&tempRight
);
3969 TRACE("returning 0x%8x %s\n", hres
, debugstr_variant(result
));
3974 /**********************************************************************
3975 * VarOr [OLEAUT32.157]
3977 * Perform a logical or (OR) operation on two variants.
3980 * pVarLeft [I] First variant
3981 * pVarRight [I] Variant to OR with pVarLeft
3982 * pVarOut [O] Destination for OR result
3985 * Success: S_OK. pVarOut contains the result of the operation with its type
3986 * taken from the table listed under VarXor().
3987 * Failure: An HRESULT error code indicating the error.
3990 * See the Notes section of VarXor() for further information.
3992 HRESULT WINAPI
VarOr(LPVARIANT pVarLeft
, LPVARIANT pVarRight
, LPVARIANT pVarOut
)
3995 VARIANT varLeft
, varRight
, varStr
;
3997 VARIANT tempLeft
, tempRight
;
3999 VariantInit(&tempLeft
);
4000 VariantInit(&tempRight
);
4001 VariantInit(&varLeft
);
4002 VariantInit(&varRight
);
4003 VariantInit(&varStr
);
4005 TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft
), debugstr_variant(pVarRight
), pVarOut
);
4007 /* Handle VT_DISPATCH by storing and taking address of returned value */
4008 if ((V_VT(pVarLeft
) & VT_TYPEMASK
) == VT_DISPATCH
)
4010 hRet
= VARIANT_FetchDispatchValue(pVarLeft
, &tempLeft
);
4011 if (FAILED(hRet
)) goto VarOr_Exit
;
4012 pVarLeft
= &tempLeft
;
4014 if ((V_VT(pVarRight
) & VT_TYPEMASK
) == VT_DISPATCH
)
4016 hRet
= VARIANT_FetchDispatchValue(pVarRight
, &tempRight
);
4017 if (FAILED(hRet
)) goto VarOr_Exit
;
4018 pVarRight
= &tempRight
;
4021 if (V_EXTRA_TYPE(pVarLeft
) || V_EXTRA_TYPE(pVarRight
) ||
4022 V_VT(pVarLeft
) == VT_UNKNOWN
|| V_VT(pVarRight
) == VT_UNKNOWN
||
4023 V_VT(pVarLeft
) == VT_DISPATCH
|| V_VT(pVarRight
) == VT_DISPATCH
||
4024 V_VT(pVarLeft
) == VT_RECORD
|| V_VT(pVarRight
) == VT_RECORD
)
4026 hRet
= DISP_E_BADVARTYPE
;
4030 V_VT(&varLeft
) = V_VT(&varRight
) = V_VT(&varStr
) = VT_EMPTY
;
4032 if (V_VT(pVarLeft
) == VT_NULL
|| V_VT(pVarRight
) == VT_NULL
)
4034 /* NULL OR Zero is NULL, NULL OR value is value */
4035 if (V_VT(pVarLeft
) == VT_NULL
)
4036 pVarLeft
= pVarRight
; /* point to the non-NULL var */
4038 V_VT(pVarOut
) = VT_NULL
;
4041 switch (V_VT(pVarLeft
))
4043 case VT_DATE
: case VT_R8
:
4049 if (V_BOOL(pVarLeft
))
4050 *pVarOut
= *pVarLeft
;
4053 case VT_I2
: case VT_UI2
:
4064 if (V_UI1(pVarLeft
))
4065 *pVarOut
= *pVarLeft
;
4073 case VT_I4
: case VT_UI4
: case VT_INT
: case VT_UINT
:
4079 if (V_CY(pVarLeft
).int64
)
4083 case VT_I8
: case VT_UI8
:
4089 if (DEC_HI32(&V_DECIMAL(pVarLeft
)) || DEC_LO64(&V_DECIMAL(pVarLeft
)))
4097 if (!V_BSTR(pVarLeft
))
4099 hRet
= DISP_E_BADVARTYPE
;
4103 hRet
= VarBoolFromStr(V_BSTR(pVarLeft
), LOCALE_USER_DEFAULT
, VAR_LOCALBOOL
, &b
);
4104 if (SUCCEEDED(hRet
) && b
)
4106 V_VT(pVarOut
) = VT_BOOL
;
4107 V_BOOL(pVarOut
) = b
;
4111 case VT_NULL
: case VT_EMPTY
:
4112 V_VT(pVarOut
) = VT_NULL
;
4116 hRet
= DISP_E_BADVARTYPE
;
4121 if (V_VT(pVarLeft
) == VT_EMPTY
|| V_VT(pVarRight
) == VT_EMPTY
)
4123 if (V_VT(pVarLeft
) == VT_EMPTY
)
4124 pVarLeft
= pVarRight
; /* point to the non-EMPTY var */
4127 /* Since one argument is empty (0), OR'ing it with the other simply
4128 * gives the others value (as 0|x => x). So just convert the other
4129 * argument to the required result type.
4131 switch (V_VT(pVarLeft
))
4134 if (!V_BSTR(pVarLeft
))
4136 hRet
= DISP_E_BADVARTYPE
;
4140 hRet
= VariantCopy(&varStr
, pVarLeft
);
4144 hRet
= VariantChangeType(pVarLeft
, pVarLeft
, 0, VT_BOOL
);
4147 /* Fall Through ... */
4148 case VT_EMPTY
: case VT_UI1
: case VT_BOOL
: case VT_I2
:
4149 V_VT(pVarOut
) = VT_I2
;
4151 case VT_DATE
: case VT_CY
: case VT_DECIMAL
: case VT_R4
: case VT_R8
:
4152 case VT_I1
: case VT_UI2
: case VT_I4
: case VT_UI4
:
4153 case VT_INT
: case VT_UINT
: case VT_UI8
:
4154 V_VT(pVarOut
) = VT_I4
;
4157 V_VT(pVarOut
) = VT_I8
;
4160 hRet
= DISP_E_BADVARTYPE
;
4163 hRet
= VariantCopy(&varLeft
, pVarLeft
);
4166 pVarLeft
= &varLeft
;
4167 hRet
= VariantChangeType(pVarOut
, pVarLeft
, 0, V_VT(pVarOut
));
4171 if (V_VT(pVarLeft
) == VT_BOOL
&& V_VT(pVarRight
) == VT_BOOL
)
4173 V_VT(pVarOut
) = VT_BOOL
;
4174 V_BOOL(pVarOut
) = V_BOOL(pVarLeft
) | V_BOOL(pVarRight
);
4179 if (V_VT(pVarLeft
) == VT_UI1
&& V_VT(pVarRight
) == VT_UI1
)
4181 V_VT(pVarOut
) = VT_UI1
;
4182 V_UI1(pVarOut
) = V_UI1(pVarLeft
) | V_UI1(pVarRight
);
4187 if (V_VT(pVarLeft
) == VT_BSTR
)
4189 hRet
= VariantCopy(&varStr
, pVarLeft
);
4193 hRet
= VariantChangeType(pVarLeft
, pVarLeft
, 0, VT_BOOL
);
4198 if (V_VT(pVarLeft
) == VT_BOOL
&&
4199 (V_VT(pVarRight
) == VT_BOOL
|| V_VT(pVarRight
) == VT_BSTR
))
4203 else if ((V_VT(pVarLeft
) == VT_BOOL
|| V_VT(pVarLeft
) == VT_UI1
||
4204 V_VT(pVarLeft
) == VT_I2
|| V_VT(pVarLeft
) == VT_BSTR
) &&
4205 (V_VT(pVarRight
) == VT_BOOL
|| V_VT(pVarRight
) == VT_UI1
||
4206 V_VT(pVarRight
) == VT_I2
|| V_VT(pVarRight
) == VT_BSTR
))
4210 else if (V_VT(pVarLeft
) == VT_I8
|| V_VT(pVarRight
) == VT_I8
)
4212 if (V_VT(pVarLeft
) == VT_INT
|| V_VT(pVarRight
) == VT_INT
)
4214 hRet
= DISP_E_TYPEMISMATCH
;
4220 hRet
= VariantCopy(&varLeft
, pVarLeft
);
4224 hRet
= VariantCopy(&varRight
, pVarRight
);
4228 if (vt
== VT_I4
&& V_VT(&varLeft
) == VT_UI4
)
4229 V_VT(&varLeft
) = VT_I4
; /* Don't overflow */
4234 if (V_VT(&varLeft
) == VT_BSTR
&&
4235 FAILED(VarR8FromStr(V_BSTR(&varLeft
), LOCALE_USER_DEFAULT
, 0, &d
)))
4236 hRet
= VariantChangeType(&varLeft
, &varLeft
, VARIANT_LOCALBOOL
, VT_BOOL
);
4237 if (SUCCEEDED(hRet
) && V_VT(&varLeft
) != vt
)
4238 hRet
= VariantChangeType(&varLeft
, &varLeft
, 0, vt
);
4243 if (vt
== VT_I4
&& V_VT(&varRight
) == VT_UI4
)
4244 V_VT(&varRight
) = VT_I4
; /* Don't overflow */
4249 if (V_VT(&varRight
) == VT_BSTR
&&
4250 FAILED(VarR8FromStr(V_BSTR(&varRight
), LOCALE_USER_DEFAULT
, 0, &d
)))
4251 hRet
= VariantChangeType(&varRight
, &varRight
, VARIANT_LOCALBOOL
, VT_BOOL
);
4252 if (SUCCEEDED(hRet
) && V_VT(&varRight
) != vt
)
4253 hRet
= VariantChangeType(&varRight
, &varRight
, 0, vt
);
4261 V_I8(pVarOut
) = V_I8(&varLeft
) | V_I8(&varRight
);
4263 else if (vt
== VT_I4
)
4265 V_I4(pVarOut
) = V_I4(&varLeft
) | V_I4(&varRight
);
4269 V_I2(pVarOut
) = V_I2(&varLeft
) | V_I2(&varRight
);
4273 VariantClear(&varStr
);
4274 VariantClear(&varLeft
);
4275 VariantClear(&varRight
);
4276 VariantClear(&tempLeft
);
4277 VariantClear(&tempRight
);
4281 /**********************************************************************
4282 * VarAbs [OLEAUT32.168]
4284 * Convert a variant to its absolute value.
4287 * pVarIn [I] Source variant
4288 * pVarOut [O] Destination for converted value
4291 * Success: S_OK. pVarOut contains the absolute value of pVarIn.
4292 * Failure: An HRESULT error code indicating the error.
4295 * - This function does not process by-reference variants.
4296 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4297 * according to the following table:
4298 *| Input Type Output Type
4299 *| ---------- -----------
4302 *| (All others) Unchanged
4304 HRESULT WINAPI
VarAbs(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
4307 HRESULT hRet
= S_OK
;
4312 TRACE("(%s,%p)\n", debugstr_variant(pVarIn
), pVarOut
);
4314 /* Handle VT_DISPATCH by storing and taking address of returned value */
4315 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
4317 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
4318 if (FAILED(hRet
)) goto VarAbs_Exit
;
4322 if (V_ISARRAY(pVarIn
) || V_VT(pVarIn
) == VT_UNKNOWN
||
4323 V_VT(pVarIn
) == VT_DISPATCH
|| V_VT(pVarIn
) == VT_RECORD
||
4324 V_VT(pVarIn
) == VT_ERROR
)
4326 hRet
= DISP_E_TYPEMISMATCH
;
4329 *pVarOut
= *pVarIn
; /* Shallow copy the value, and invert it if needed */
4331 #define ABS_CASE(typ,min) \
4332 case VT_##typ: if (V_##typ(pVarIn) == min) hRet = DISP_E_OVERFLOW; \
4333 else if (V_##typ(pVarIn) < 0) V_##typ(pVarOut) = -V_##typ(pVarIn); \
4336 switch (V_VT(pVarIn
))
4338 ABS_CASE(I1
,I1_MIN
);
4340 V_VT(pVarOut
) = VT_I2
;
4341 /* BOOL->I2, Fall through ... */
4342 ABS_CASE(I2
,I2_MIN
);
4344 ABS_CASE(I4
,I4_MIN
);
4345 ABS_CASE(I8
,I8_MIN
);
4346 ABS_CASE(R4
,R4_MIN
);
4348 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(&varIn
));
4351 V_VT(pVarOut
) = VT_R8
;
4353 /* Fall through ... */
4355 ABS_CASE(R8
,R8_MIN
);
4357 hRet
= VarCyAbs(V_CY(pVarIn
), & V_CY(pVarOut
));
4360 DEC_SIGN(&V_DECIMAL(pVarOut
)) &= ~DECIMAL_NEG
;
4370 V_VT(pVarOut
) = VT_I2
;
4375 hRet
= DISP_E_BADVARTYPE
;
4379 VariantClear(&temp
);
4383 /**********************************************************************
4384 * VarFix [OLEAUT32.169]
4386 * Truncate a variants value to a whole number.
4389 * pVarIn [I] Source variant
4390 * pVarOut [O] Destination for converted value
4393 * Success: S_OK. pVarOut contains the converted value.
4394 * Failure: An HRESULT error code indicating the error.
4397 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4398 * according to the following table:
4399 *| Input Type Output Type
4400 *| ---------- -----------
4404 *| All Others Unchanged
4405 * - The difference between this function and VarInt() is that VarInt() rounds
4406 * negative numbers away from 0, while this function rounds them towards zero.
4408 HRESULT WINAPI
VarFix(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
4410 HRESULT hRet
= S_OK
;
4415 TRACE("(%s,%p)\n", debugstr_variant(pVarIn
), pVarOut
);
4417 /* Handle VT_DISPATCH by storing and taking address of returned value */
4418 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
4420 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
4421 if (FAILED(hRet
)) goto VarFix_Exit
;
4424 V_VT(pVarOut
) = V_VT(pVarIn
);
4426 switch (V_VT(pVarIn
))
4429 V_UI1(pVarOut
) = V_UI1(pVarIn
);
4432 V_VT(pVarOut
) = VT_I2
;
4435 V_I2(pVarOut
) = V_I2(pVarIn
);
4438 V_I4(pVarOut
) = V_I4(pVarIn
);
4441 V_I8(pVarOut
) = V_I8(pVarIn
);
4444 if (V_R4(pVarIn
) < 0.0f
)
4445 V_R4(pVarOut
) = (float)ceil(V_R4(pVarIn
));
4447 V_R4(pVarOut
) = (float)floor(V_R4(pVarIn
));
4450 V_VT(pVarOut
) = VT_R8
;
4451 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(pVarOut
));
4456 if (V_R8(pVarIn
) < 0.0)
4457 V_R8(pVarOut
) = ceil(V_R8(pVarIn
));
4459 V_R8(pVarOut
) = floor(V_R8(pVarIn
));
4462 hRet
= VarCyFix(V_CY(pVarIn
), &V_CY(pVarOut
));
4465 hRet
= VarDecFix(&V_DECIMAL(pVarIn
), &V_DECIMAL(pVarOut
));
4468 V_VT(pVarOut
) = VT_I2
;
4475 if (V_TYPE(pVarIn
) == VT_CLSID
|| /* VT_CLSID is a special case */
4476 FAILED(VARIANT_ValidateType(V_VT(pVarIn
))))
4477 hRet
= DISP_E_BADVARTYPE
;
4479 hRet
= DISP_E_TYPEMISMATCH
;
4483 V_VT(pVarOut
) = VT_EMPTY
;
4484 VariantClear(&temp
);
4489 /**********************************************************************
4490 * VarInt [OLEAUT32.172]
4492 * Truncate a variants value to a whole number.
4495 * pVarIn [I] Source variant
4496 * pVarOut [O] Destination for converted value
4499 * Success: S_OK. pVarOut contains the converted value.
4500 * Failure: An HRESULT error code indicating the error.
4503 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4504 * according to the following table:
4505 *| Input Type Output Type
4506 *| ---------- -----------
4510 *| All Others Unchanged
4511 * - The difference between this function and VarFix() is that VarFix() rounds
4512 * negative numbers towards 0, while this function rounds them away from zero.
4514 HRESULT WINAPI
VarInt(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
4516 HRESULT hRet
= S_OK
;
4521 TRACE("(%s,%p)\n", debugstr_variant(pVarIn
), pVarOut
);
4523 /* Handle VT_DISPATCH by storing and taking address of returned value */
4524 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
4526 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
4527 if (FAILED(hRet
)) goto VarInt_Exit
;
4530 V_VT(pVarOut
) = V_VT(pVarIn
);
4532 switch (V_VT(pVarIn
))
4535 V_R4(pVarOut
) = (float)floor(V_R4(pVarIn
));
4538 V_VT(pVarOut
) = VT_R8
;
4539 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(pVarOut
));
4544 V_R8(pVarOut
) = floor(V_R8(pVarIn
));
4547 hRet
= VarCyInt(V_CY(pVarIn
), &V_CY(pVarOut
));
4550 hRet
= VarDecInt(&V_DECIMAL(pVarIn
), &V_DECIMAL(pVarOut
));
4553 hRet
= VarFix(pVarIn
, pVarOut
);
4556 VariantClear(&temp
);
4561 /**********************************************************************
4562 * VarXor [OLEAUT32.167]
4564 * Perform a logical exclusive-or (XOR) operation on two variants.
4567 * pVarLeft [I] First variant
4568 * pVarRight [I] Variant to XOR with pVarLeft
4569 * pVarOut [O] Destination for XOR result
4572 * Success: S_OK. pVarOut contains the result of the operation with its type
4573 * taken from the table below).
4574 * Failure: An HRESULT error code indicating the error.
4577 * - Neither pVarLeft or pVarRight are modified by this function.
4578 * - This function does not process by-reference variants.
4579 * - Input types of VT_BSTR may be numeric strings or boolean text.
4580 * - The type of result stored in pVarOut depends on the types of pVarLeft
4581 * and pVarRight, and will be one of VT_UI1, VT_I2, VT_I4, VT_I8, VT_BOOL,
4582 * or VT_NULL if the function succeeds.
4583 * - Type promotion is inconsistent and as a result certain combinations of
4584 * values will return DISP_E_OVERFLOW even when they could be represented.
4585 * This matches the behaviour of native oleaut32.
4587 HRESULT WINAPI
VarXor(LPVARIANT pVarLeft
, LPVARIANT pVarRight
, LPVARIANT pVarOut
)
4590 VARIANT varLeft
, varRight
;
4591 VARIANT tempLeft
, tempRight
;
4595 TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft
), debugstr_variant(pVarRight
), pVarOut
);
4597 if (V_EXTRA_TYPE(pVarLeft
) || V_EXTRA_TYPE(pVarRight
) ||
4598 V_VT(pVarLeft
) > VT_UINT
|| V_VT(pVarRight
) > VT_UINT
||
4599 V_VT(pVarLeft
) == VT_VARIANT
|| V_VT(pVarRight
) == VT_VARIANT
||
4600 V_VT(pVarLeft
) == VT_UNKNOWN
|| V_VT(pVarRight
) == VT_UNKNOWN
||
4601 V_VT(pVarLeft
) == (VARTYPE
)15 || V_VT(pVarRight
) == (VARTYPE
)15 ||
4602 V_VT(pVarLeft
) == VT_ERROR
|| V_VT(pVarRight
) == VT_ERROR
)
4603 return DISP_E_BADVARTYPE
;
4605 if (V_VT(pVarLeft
) == VT_NULL
|| V_VT(pVarRight
) == VT_NULL
)
4607 /* NULL XOR anything valid is NULL */
4608 V_VT(pVarOut
) = VT_NULL
;
4612 VariantInit(&tempLeft
);
4613 VariantInit(&tempRight
);
4615 /* Handle VT_DISPATCH by storing and taking address of returned value */
4616 if ((V_VT(pVarLeft
) & VT_TYPEMASK
) == VT_DISPATCH
)
4618 hRet
= VARIANT_FetchDispatchValue(pVarLeft
, &tempLeft
);
4619 if (FAILED(hRet
)) goto VarXor_Exit
;
4620 pVarLeft
= &tempLeft
;
4622 if ((V_VT(pVarRight
) & VT_TYPEMASK
) == VT_DISPATCH
)
4624 hRet
= VARIANT_FetchDispatchValue(pVarRight
, &tempRight
);
4625 if (FAILED(hRet
)) goto VarXor_Exit
;
4626 pVarRight
= &tempRight
;
4629 /* Copy our inputs so we don't disturb anything */
4630 V_VT(&varLeft
) = V_VT(&varRight
) = VT_EMPTY
;
4632 hRet
= VariantCopy(&varLeft
, pVarLeft
);
4636 hRet
= VariantCopy(&varRight
, pVarRight
);
4640 /* Try any strings first as numbers, then as VT_BOOL */
4641 if (V_VT(&varLeft
) == VT_BSTR
)
4643 hRet
= VarR8FromStr(V_BSTR(&varLeft
), LOCALE_USER_DEFAULT
, 0, &d
);
4644 hRet
= VariantChangeType(&varLeft
, &varLeft
, VARIANT_LOCALBOOL
,
4645 FAILED(hRet
) ? VT_BOOL
: VT_I4
);
4650 if (V_VT(&varRight
) == VT_BSTR
)
4652 hRet
= VarR8FromStr(V_BSTR(&varRight
), LOCALE_USER_DEFAULT
, 0, &d
);
4653 hRet
= VariantChangeType(&varRight
, &varRight
, VARIANT_LOCALBOOL
,
4654 FAILED(hRet
) ? VT_BOOL
: VT_I4
);
4659 /* Determine the result type */
4660 if (V_VT(&varLeft
) == VT_I8
|| V_VT(&varRight
) == VT_I8
)
4662 if (V_VT(pVarLeft
) == VT_INT
|| V_VT(pVarRight
) == VT_INT
)
4664 hRet
= DISP_E_TYPEMISMATCH
;
4671 switch ((V_VT(&varLeft
) << 16) | V_VT(&varRight
))
4673 case (VT_BOOL
<< 16) | VT_BOOL
:
4676 case (VT_UI1
<< 16) | VT_UI1
:
4679 case (VT_EMPTY
<< 16) | VT_EMPTY
:
4680 case (VT_EMPTY
<< 16) | VT_UI1
:
4681 case (VT_EMPTY
<< 16) | VT_I2
:
4682 case (VT_EMPTY
<< 16) | VT_BOOL
:
4683 case (VT_UI1
<< 16) | VT_EMPTY
:
4684 case (VT_UI1
<< 16) | VT_I2
:
4685 case (VT_UI1
<< 16) | VT_BOOL
:
4686 case (VT_I2
<< 16) | VT_EMPTY
:
4687 case (VT_I2
<< 16) | VT_UI1
:
4688 case (VT_I2
<< 16) | VT_I2
:
4689 case (VT_I2
<< 16) | VT_BOOL
:
4690 case (VT_BOOL
<< 16) | VT_EMPTY
:
4691 case (VT_BOOL
<< 16) | VT_UI1
:
4692 case (VT_BOOL
<< 16) | VT_I2
:
4701 /* VT_UI4 does not overflow */
4704 if (V_VT(&varLeft
) == VT_UI4
)
4705 V_VT(&varLeft
) = VT_I4
;
4706 if (V_VT(&varRight
) == VT_UI4
)
4707 V_VT(&varRight
) = VT_I4
;
4710 /* Convert our input copies to the result type */
4711 if (V_VT(&varLeft
) != vt
)
4712 hRet
= VariantChangeType(&varLeft
, &varLeft
, 0, vt
);
4716 if (V_VT(&varRight
) != vt
)
4717 hRet
= VariantChangeType(&varRight
, &varRight
, 0, vt
);
4723 /* Calculate the result */
4727 V_I8(pVarOut
) = V_I8(&varLeft
) ^ V_I8(&varRight
);
4730 V_I4(pVarOut
) = V_I4(&varLeft
) ^ V_I4(&varRight
);
4734 V_I2(pVarOut
) = V_I2(&varLeft
) ^ V_I2(&varRight
);
4737 V_UI1(pVarOut
) = V_UI1(&varLeft
) ^ V_UI1(&varRight
);
4742 VariantClear(&varLeft
);
4743 VariantClear(&varRight
);
4744 VariantClear(&tempLeft
);
4745 VariantClear(&tempRight
);
4749 /**********************************************************************
4750 * VarEqv [OLEAUT32.172]
4752 * Determine if two variants contain the same value.
4755 * pVarLeft [I] First variant to compare
4756 * pVarRight [I] Variant to compare to pVarLeft
4757 * pVarOut [O] Destination for comparison result
4760 * Success: S_OK. pVarOut contains the result of the comparison (VARIANT_TRUE
4761 * if equivalent or non-zero otherwise.
4762 * Failure: An HRESULT error code indicating the error.
4765 * - This function simply calls VarXor() on pVarLeft and pVarRight and inverts
4768 HRESULT WINAPI
VarEqv(LPVARIANT pVarLeft
, LPVARIANT pVarRight
, LPVARIANT pVarOut
)
4772 TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft
), debugstr_variant(pVarRight
), pVarOut
);
4774 hRet
= VarXor(pVarLeft
, pVarRight
, pVarOut
);
4775 if (SUCCEEDED(hRet
))
4777 if (V_VT(pVarOut
) == VT_I8
)
4778 V_I8(pVarOut
) = ~V_I8(pVarOut
);
4780 V_UI4(pVarOut
) = ~V_UI4(pVarOut
);
4785 /**********************************************************************
4786 * VarNeg [OLEAUT32.173]
4788 * Negate the value of a variant.
4791 * pVarIn [I] Source variant
4792 * pVarOut [O] Destination for converted value
4795 * Success: S_OK. pVarOut contains the converted value.
4796 * Failure: An HRESULT error code indicating the error.
4799 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4800 * according to the following table:
4801 *| Input Type Output Type
4802 *| ---------- -----------
4807 *| All Others Unchanged (unless promoted)
4808 * - Where the negated value of a variant does not fit in its base type, the type
4809 * is promoted according to the following table:
4810 *| Input Type Promoted To
4811 *| ---------- -----------
4815 * - The native version of this function returns DISP_E_BADVARTYPE for valid
4816 * variant types that cannot be negated, and returns DISP_E_TYPEMISMATCH
4817 * for types which are not valid. Since this is in contravention of the
4818 * meaning of those error codes and unlikely to be relied on by applications,
4819 * this implementation returns errors consistent with the other high level
4820 * variant math functions.
4822 HRESULT WINAPI
VarNeg(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
4824 HRESULT hRet
= S_OK
;
4829 TRACE("(%s,%p)\n", debugstr_variant(pVarIn
), pVarOut
);
4831 /* Handle VT_DISPATCH by storing and taking address of returned value */
4832 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
4834 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
4835 if (FAILED(hRet
)) goto VarNeg_Exit
;
4838 V_VT(pVarOut
) = V_VT(pVarIn
);
4840 switch (V_VT(pVarIn
))
4843 V_VT(pVarOut
) = VT_I2
;
4844 V_I2(pVarOut
) = -V_UI1(pVarIn
);
4847 V_VT(pVarOut
) = VT_I2
;
4850 if (V_I2(pVarIn
) == I2_MIN
)
4852 V_VT(pVarOut
) = VT_I4
;
4853 V_I4(pVarOut
) = -(int)V_I2(pVarIn
);
4856 V_I2(pVarOut
) = -V_I2(pVarIn
);
4859 if (V_I4(pVarIn
) == I4_MIN
)
4861 V_VT(pVarOut
) = VT_R8
;
4862 V_R8(pVarOut
) = -(double)V_I4(pVarIn
);
4865 V_I4(pVarOut
) = -V_I4(pVarIn
);
4868 if (V_I8(pVarIn
) == I8_MIN
)
4870 V_VT(pVarOut
) = VT_R8
;
4871 hRet
= VarR8FromI8(V_I8(pVarIn
), &V_R8(pVarOut
));
4872 V_R8(pVarOut
) *= -1.0;
4875 V_I8(pVarOut
) = -V_I8(pVarIn
);
4878 V_R4(pVarOut
) = -V_R4(pVarIn
);
4882 V_R8(pVarOut
) = -V_R8(pVarIn
);
4885 hRet
= VarCyNeg(V_CY(pVarIn
), &V_CY(pVarOut
));
4888 hRet
= VarDecNeg(&V_DECIMAL(pVarIn
), &V_DECIMAL(pVarOut
));
4891 V_VT(pVarOut
) = VT_R8
;
4892 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(pVarOut
));
4893 V_R8(pVarOut
) = -V_R8(pVarOut
);
4896 V_VT(pVarOut
) = VT_I2
;
4903 if (V_TYPE(pVarIn
) == VT_CLSID
|| /* VT_CLSID is a special case */
4904 FAILED(VARIANT_ValidateType(V_VT(pVarIn
))))
4905 hRet
= DISP_E_BADVARTYPE
;
4907 hRet
= DISP_E_TYPEMISMATCH
;
4911 V_VT(pVarOut
) = VT_EMPTY
;
4912 VariantClear(&temp
);
4917 /**********************************************************************
4918 * VarNot [OLEAUT32.174]
4920 * Perform a not operation on a variant.
4923 * pVarIn [I] Source variant
4924 * pVarOut [O] Destination for converted value
4927 * Success: S_OK. pVarOut contains the converted value.
4928 * Failure: An HRESULT error code indicating the error.
4931 * - Strictly speaking, this function performs a bitwise ones complement
4932 * on the variants value (after possibly converting to VT_I4, see below).
4933 * This only behaves like a boolean not operation if the value in
4934 * pVarIn is either VARIANT_TRUE or VARIANT_FALSE and the type is signed.
4935 * - To perform a genuine not operation, convert the variant to a VT_BOOL
4936 * before calling this function.
4937 * - This function does not process by-reference variants.
4938 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4939 * according to the following table:
4940 *| Input Type Output Type
4941 *| ---------- -----------
4948 *| (All others) Unchanged
4950 HRESULT WINAPI
VarNot(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
4953 HRESULT hRet
= S_OK
;
4958 TRACE("(%s,%p)\n", debugstr_variant(pVarIn
), pVarOut
);
4960 /* Handle VT_DISPATCH by storing and taking address of returned value */
4961 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
4963 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
4964 if (FAILED(hRet
)) goto VarNot_Exit
;
4968 if (V_VT(pVarIn
) == VT_BSTR
)
4970 V_VT(&varIn
) = VT_R8
;
4971 hRet
= VarR8FromStr( V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(&varIn
) );
4974 V_VT(&varIn
) = VT_BOOL
;
4975 hRet
= VarBoolFromStr( V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, VAR_LOCALBOOL
, &V_BOOL(&varIn
) );
4977 if (FAILED(hRet
)) goto VarNot_Exit
;
4981 V_VT(pVarOut
) = V_VT(pVarIn
);
4983 switch (V_VT(pVarIn
))
4986 V_I4(pVarOut
) = ~V_I1(pVarIn
);
4987 V_VT(pVarOut
) = VT_I4
;
4989 case VT_UI1
: V_UI1(pVarOut
) = ~V_UI1(pVarIn
); break;
4991 case VT_I2
: V_I2(pVarOut
) = ~V_I2(pVarIn
); break;
4993 V_I4(pVarOut
) = ~V_UI2(pVarIn
);
4994 V_VT(pVarOut
) = VT_I4
;
4997 hRet
= VarI4FromDec(&V_DECIMAL(pVarIn
), &V_I4(&varIn
));
5001 /* Fall through ... */
5003 V_VT(pVarOut
) = VT_I4
;
5004 /* Fall through ... */
5005 case VT_I4
: V_I4(pVarOut
) = ~V_I4(pVarIn
); break;
5008 V_I4(pVarOut
) = ~V_UI4(pVarIn
);
5009 V_VT(pVarOut
) = VT_I4
;
5011 case VT_I8
: V_I8(pVarOut
) = ~V_I8(pVarIn
); break;
5013 V_I4(pVarOut
) = ~V_UI8(pVarIn
);
5014 V_VT(pVarOut
) = VT_I4
;
5017 hRet
= VarI4FromR4(V_R4(pVarIn
), &V_I4(pVarOut
));
5018 V_I4(pVarOut
) = ~V_I4(pVarOut
);
5019 V_VT(pVarOut
) = VT_I4
;
5023 hRet
= VarI4FromR8(V_R8(pVarIn
), &V_I4(pVarOut
));
5024 V_I4(pVarOut
) = ~V_I4(pVarOut
);
5025 V_VT(pVarOut
) = VT_I4
;
5028 hRet
= VarI4FromCy(V_CY(pVarIn
), &V_I4(pVarOut
));
5029 V_I4(pVarOut
) = ~V_I4(pVarOut
);
5030 V_VT(pVarOut
) = VT_I4
;
5034 V_VT(pVarOut
) = VT_I2
;
5040 if (V_TYPE(pVarIn
) == VT_CLSID
|| /* VT_CLSID is a special case */
5041 FAILED(VARIANT_ValidateType(V_VT(pVarIn
))))
5042 hRet
= DISP_E_BADVARTYPE
;
5044 hRet
= DISP_E_TYPEMISMATCH
;
5048 V_VT(pVarOut
) = VT_EMPTY
;
5049 VariantClear(&temp
);
5054 /**********************************************************************
5055 * VarRound [OLEAUT32.175]
5057 * Perform a round operation on a variant.
5060 * pVarIn [I] Source variant
5061 * deci [I] Number of decimals to round to
5062 * pVarOut [O] Destination for converted value
5065 * Success: S_OK. pVarOut contains the converted value.
5066 * Failure: An HRESULT error code indicating the error.
5069 * - Floating point values are rounded to the desired number of decimals.
5070 * - Some integer types are just copied to the return variable.
5071 * - Some other integer types are not handled and fail.
5073 HRESULT WINAPI
VarRound(LPVARIANT pVarIn
, int deci
, LPVARIANT pVarOut
)
5076 HRESULT hRet
= S_OK
;
5082 TRACE("(%s,%d)\n", debugstr_variant(pVarIn
), deci
);
5084 /* Handle VT_DISPATCH by storing and taking address of returned value */
5085 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
5087 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
5088 if (FAILED(hRet
)) goto VarRound_Exit
;
5092 switch (V_VT(pVarIn
))
5094 /* cases that fail on windows */
5099 hRet
= DISP_E_BADVARTYPE
;
5102 /* cases just copying in to out */
5104 V_VT(pVarOut
) = V_VT(pVarIn
);
5105 V_UI1(pVarOut
) = V_UI1(pVarIn
);
5108 V_VT(pVarOut
) = V_VT(pVarIn
);
5109 V_I2(pVarOut
) = V_I2(pVarIn
);
5112 V_VT(pVarOut
) = V_VT(pVarIn
);
5113 V_I4(pVarOut
) = V_I4(pVarIn
);
5116 V_VT(pVarOut
) = V_VT(pVarIn
);
5117 /* value unchanged */
5120 /* cases that change type */
5122 V_VT(pVarOut
) = VT_I2
;
5126 V_VT(pVarOut
) = VT_I2
;
5127 V_I2(pVarOut
) = V_BOOL(pVarIn
);
5130 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(&varIn
));
5135 /* Fall through ... */
5137 /* cases we need to do math */
5139 if (V_R8(pVarIn
)>0) {
5140 V_R8(pVarOut
)=floor(V_R8(pVarIn
)*pow(10, deci
)+0.5)/pow(10, deci
);
5142 V_R8(pVarOut
)=ceil(V_R8(pVarIn
)*pow(10, deci
)-0.5)/pow(10, deci
);
5144 V_VT(pVarOut
) = V_VT(pVarIn
);
5147 if (V_R4(pVarIn
)>0) {
5148 V_R4(pVarOut
)=floor(V_R4(pVarIn
)*pow(10, deci
)+0.5)/pow(10, deci
);
5150 V_R4(pVarOut
)=ceil(V_R4(pVarIn
)*pow(10, deci
)-0.5)/pow(10, deci
);
5152 V_VT(pVarOut
) = V_VT(pVarIn
);
5155 if (V_DATE(pVarIn
)>0) {
5156 V_DATE(pVarOut
)=floor(V_DATE(pVarIn
)*pow(10, deci
)+0.5)/pow(10, deci
);
5158 V_DATE(pVarOut
)=ceil(V_DATE(pVarIn
)*pow(10, deci
)-0.5)/pow(10, deci
);
5160 V_VT(pVarOut
) = V_VT(pVarIn
);
5166 factor
=pow(10, 4-deci
);
5168 if (V_CY(pVarIn
).int64
>0) {
5169 V_CY(pVarOut
).int64
=floor(V_CY(pVarIn
).int64
/factor
)*factor
;
5171 V_CY(pVarOut
).int64
=ceil(V_CY(pVarIn
).int64
/factor
)*factor
;
5173 V_VT(pVarOut
) = V_VT(pVarIn
);
5176 /* cases we don't know yet */
5178 FIXME("unimplemented part, V_VT(pVarIn) == 0x%X, deci == %d\n",
5179 V_VT(pVarIn
) & VT_TYPEMASK
, deci
);
5180 hRet
= DISP_E_BADVARTYPE
;
5184 V_VT(pVarOut
) = VT_EMPTY
;
5185 VariantClear(&temp
);
5187 TRACE("returning 0x%08x %s\n", hRet
, debugstr_variant(pVarOut
));
5191 /**********************************************************************
5192 * VarIdiv [OLEAUT32.153]
5194 * Converts input variants to integers and divides them.
5197 * left [I] Left hand variant
5198 * right [I] Right hand variant
5199 * result [O] Destination for quotient
5202 * Success: S_OK. result contains the quotient.
5203 * Failure: An HRESULT error code indicating the error.
5206 * If either expression is null, null is returned, as per MSDN
5208 HRESULT WINAPI
VarIdiv(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
5210 HRESULT hres
= S_OK
;
5211 VARTYPE resvt
= VT_EMPTY
;
5212 VARTYPE leftvt
,rightvt
;
5213 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
5215 VARIANT tempLeft
, tempRight
;
5217 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
5221 VariantInit(&tempLeft
);
5222 VariantInit(&tempRight
);
5224 leftvt
= V_VT(left
)&VT_TYPEMASK
;
5225 rightvt
= V_VT(right
)&VT_TYPEMASK
;
5226 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
5227 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
5229 if (leftExtraFlags
!= rightExtraFlags
)
5231 hres
= DISP_E_BADVARTYPE
;
5234 ExtraFlags
= leftExtraFlags
;
5236 /* Native VarIdiv always returns an error when using extra
5237 * flags or if the variant combination is I8 and INT.
5239 if ((leftvt
== VT_I8
&& rightvt
== VT_INT
) ||
5240 (leftvt
== VT_INT
&& rightvt
== VT_I8
) ||
5241 (rightvt
== VT_EMPTY
&& leftvt
!= VT_NULL
) ||
5244 hres
= DISP_E_BADVARTYPE
;
5248 /* Determine variant type */
5249 else if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
)
5251 V_VT(result
) = VT_NULL
;
5255 else if (leftvt
== VT_I8
|| rightvt
== VT_I8
)
5257 else if (leftvt
== VT_I4
|| rightvt
== VT_I4
||
5258 leftvt
== VT_INT
|| rightvt
== VT_INT
||
5259 leftvt
== VT_UINT
|| rightvt
== VT_UINT
||
5260 leftvt
== VT_UI8
|| rightvt
== VT_UI8
||
5261 leftvt
== VT_UI4
|| rightvt
== VT_UI4
||
5262 leftvt
== VT_UI2
|| rightvt
== VT_UI2
||
5263 leftvt
== VT_I1
|| rightvt
== VT_I1
||
5264 leftvt
== VT_BSTR
|| rightvt
== VT_BSTR
||
5265 leftvt
== VT_DATE
|| rightvt
== VT_DATE
||
5266 leftvt
== VT_CY
|| rightvt
== VT_CY
||
5267 leftvt
== VT_DECIMAL
|| rightvt
== VT_DECIMAL
||
5268 leftvt
== VT_R8
|| rightvt
== VT_R8
||
5269 leftvt
== VT_R4
|| rightvt
== VT_R4
)
5271 else if (leftvt
== VT_I2
|| rightvt
== VT_I2
||
5272 leftvt
== VT_BOOL
|| rightvt
== VT_BOOL
||
5275 else if (leftvt
== VT_UI1
|| rightvt
== VT_UI1
)
5279 hres
= DISP_E_BADVARTYPE
;
5283 /* coerce to the result type */
5284 hres
= VariantChangeType(&lv
, left
, 0, resvt
);
5285 if (hres
!= S_OK
) goto end
;
5286 hres
= VariantChangeType(&rv
, right
, 0, resvt
);
5287 if (hres
!= S_OK
) goto end
;
5290 V_VT(result
) = resvt
;
5294 if (V_UI1(&rv
) == 0)
5296 hres
= DISP_E_DIVBYZERO
;
5297 V_VT(result
) = VT_EMPTY
;
5300 V_UI1(result
) = V_UI1(&lv
) / V_UI1(&rv
);
5305 hres
= DISP_E_DIVBYZERO
;
5306 V_VT(result
) = VT_EMPTY
;
5309 V_I2(result
) = V_I2(&lv
) / V_I2(&rv
);
5314 hres
= DISP_E_DIVBYZERO
;
5315 V_VT(result
) = VT_EMPTY
;
5318 V_I4(result
) = V_I4(&lv
) / V_I4(&rv
);
5323 hres
= DISP_E_DIVBYZERO
;
5324 V_VT(result
) = VT_EMPTY
;
5327 V_I8(result
) = V_I8(&lv
) / V_I8(&rv
);
5330 FIXME("Couldn't integer divide variant types %d,%d\n",
5337 VariantClear(&tempLeft
);
5338 VariantClear(&tempRight
);
5344 /**********************************************************************
5345 * VarMod [OLEAUT32.155]
5347 * Perform the modulus operation of the right hand variant on the left
5350 * left [I] Left hand variant
5351 * right [I] Right hand variant
5352 * result [O] Destination for converted value
5355 * Success: S_OK. result contains the remainder.
5356 * Failure: An HRESULT error code indicating the error.
5359 * If an error occurs the type of result will be modified but the value will not be.
5360 * Doesn't support arrays or any special flags yet.
5362 HRESULT WINAPI
VarMod(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
5365 HRESULT rc
= E_FAIL
;
5368 VARIANT tempLeft
, tempRight
;
5370 VariantInit(&tempLeft
);
5371 VariantInit(&tempRight
);
5375 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
5377 /* Handle VT_DISPATCH by storing and taking address of returned value */
5378 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
5380 rc
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
5381 if (FAILED(rc
)) goto end
;
5384 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
5386 rc
= VARIANT_FetchDispatchValue(right
, &tempRight
);
5387 if (FAILED(rc
)) goto end
;
5391 /* check for invalid inputs */
5393 switch (V_VT(left
) & VT_TYPEMASK
) {
5415 V_VT(result
) = VT_EMPTY
;
5416 rc
= DISP_E_TYPEMISMATCH
;
5419 rc
= DISP_E_TYPEMISMATCH
;
5422 V_VT(result
) = VT_EMPTY
;
5423 rc
= DISP_E_TYPEMISMATCH
;
5428 V_VT(result
) = VT_EMPTY
;
5429 rc
= DISP_E_BADVARTYPE
;
5434 switch (V_VT(right
) & VT_TYPEMASK
) {
5440 if((V_VT(left
) == VT_INT
) && (V_VT(right
) == VT_I8
))
5442 V_VT(result
) = VT_EMPTY
;
5443 rc
= DISP_E_TYPEMISMATCH
;
5447 if((V_VT(right
) == VT_INT
) && (V_VT(left
) == VT_I8
))
5449 V_VT(result
) = VT_EMPTY
;
5450 rc
= DISP_E_TYPEMISMATCH
;
5461 if(V_VT(left
) == VT_EMPTY
)
5463 V_VT(result
) = VT_I4
;
5470 if(V_VT(left
) == VT_ERROR
)
5472 V_VT(result
) = VT_EMPTY
;
5473 rc
= DISP_E_TYPEMISMATCH
;
5477 if(V_VT(left
) == VT_NULL
)
5479 V_VT(result
) = VT_NULL
;
5486 V_VT(result
) = VT_EMPTY
;
5487 rc
= DISP_E_BADVARTYPE
;
5490 if(V_VT(left
) == VT_VOID
)
5492 V_VT(result
) = VT_EMPTY
;
5493 rc
= DISP_E_BADVARTYPE
;
5494 } else if((V_VT(left
) == VT_NULL
) || (V_VT(left
) == VT_EMPTY
) || (V_VT(left
) == VT_ERROR
) ||
5497 V_VT(result
) = VT_NULL
;
5501 V_VT(result
) = VT_NULL
;
5502 rc
= DISP_E_BADVARTYPE
;
5507 V_VT(result
) = VT_EMPTY
;
5508 rc
= DISP_E_TYPEMISMATCH
;
5511 rc
= DISP_E_TYPEMISMATCH
;
5514 if((V_VT(left
) == 15) || ((V_VT(left
) >= 24) && (V_VT(left
) <= 35)) || !lOk
)
5516 V_VT(result
) = VT_EMPTY
;
5517 rc
= DISP_E_BADVARTYPE
;
5520 V_VT(result
) = VT_EMPTY
;
5521 rc
= DISP_E_TYPEMISMATCH
;
5525 V_VT(result
) = VT_EMPTY
;
5526 rc
= DISP_E_BADVARTYPE
;
5530 /* determine the result type */
5531 if((V_VT(left
) == VT_I8
) || (V_VT(right
) == VT_I8
)) resT
= VT_I8
;
5532 else if((V_VT(left
) == VT_UI1
) && (V_VT(right
) == VT_BOOL
)) resT
= VT_I2
;
5533 else if((V_VT(left
) == VT_UI1
) && (V_VT(right
) == VT_UI1
)) resT
= VT_UI1
;
5534 else if((V_VT(left
) == VT_UI1
) && (V_VT(right
) == VT_I2
)) resT
= VT_I2
;
5535 else if((V_VT(left
) == VT_I2
) && (V_VT(right
) == VT_BOOL
)) resT
= VT_I2
;
5536 else if((V_VT(left
) == VT_I2
) && (V_VT(right
) == VT_UI1
)) resT
= VT_I2
;
5537 else if((V_VT(left
) == VT_I2
) && (V_VT(right
) == VT_I2
)) resT
= VT_I2
;
5538 else if((V_VT(left
) == VT_BOOL
) && (V_VT(right
) == VT_BOOL
)) resT
= VT_I2
;
5539 else if((V_VT(left
) == VT_BOOL
) && (V_VT(right
) == VT_UI1
)) resT
= VT_I2
;
5540 else if((V_VT(left
) == VT_BOOL
) && (V_VT(right
) == VT_I2
)) resT
= VT_I2
;
5541 else resT
= VT_I4
; /* most outputs are I4 */
5543 /* convert to I8 for the modulo */
5544 rc
= VariantChangeType(&lv
, left
, 0, VT_I8
);
5547 FIXME("Could not convert left type %d to %d? rc == 0x%X\n", V_VT(left
), VT_I8
, rc
);
5551 rc
= VariantChangeType(&rv
, right
, 0, VT_I8
);
5554 FIXME("Could not convert right type %d to %d? rc == 0x%X\n", V_VT(right
), VT_I8
, rc
);
5558 /* if right is zero set VT_EMPTY and return divide by zero */
5561 V_VT(result
) = VT_EMPTY
;
5562 rc
= DISP_E_DIVBYZERO
;
5566 /* perform the modulo operation */
5567 V_VT(result
) = VT_I8
;
5568 V_I8(result
) = V_I8(&lv
) % V_I8(&rv
);
5570 TRACE("V_I8(left) == %s, V_I8(right) == %s, V_I8(result) == %s\n",
5571 wine_dbgstr_longlong(V_I8(&lv
)), wine_dbgstr_longlong(V_I8(&rv
)),
5572 wine_dbgstr_longlong(V_I8(result
)));
5574 /* convert left and right to the destination type */
5575 rc
= VariantChangeType(result
, result
, 0, resT
);
5578 FIXME("Could not convert 0x%x to %d?\n", V_VT(result
), resT
);
5579 /* fall to end of function */
5585 VariantClear(&tempLeft
);
5586 VariantClear(&tempRight
);
5590 /**********************************************************************
5591 * VarPow [OLEAUT32.158]
5593 * Computes the power of one variant to another variant.
5596 * left [I] First variant
5597 * right [I] Second variant
5598 * result [O] Result variant
5602 * Failure: An HRESULT error code indicating the error.
5604 HRESULT WINAPI
VarPow(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
5608 VARTYPE resvt
= VT_EMPTY
;
5609 VARTYPE leftvt
,rightvt
;
5610 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
5611 VARIANT tempLeft
, tempRight
;
5613 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
5617 VariantInit(&tempLeft
);
5618 VariantInit(&tempRight
);
5620 /* Handle VT_DISPATCH by storing and taking address of returned value */
5621 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
5623 hr
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
5624 if (FAILED(hr
)) goto end
;
5627 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
5629 hr
= VARIANT_FetchDispatchValue(right
, &tempRight
);
5630 if (FAILED(hr
)) goto end
;
5634 leftvt
= V_VT(left
)&VT_TYPEMASK
;
5635 rightvt
= V_VT(right
)&VT_TYPEMASK
;
5636 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
5637 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
5639 if (leftExtraFlags
!= rightExtraFlags
)
5641 hr
= DISP_E_BADVARTYPE
;
5644 ExtraFlags
= leftExtraFlags
;
5646 /* Native VarPow always returns an error when using extra flags */
5647 if (ExtraFlags
!= 0)
5649 hr
= DISP_E_BADVARTYPE
;
5653 /* Determine return type */
5654 else if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
) {
5655 V_VT(result
) = VT_NULL
;
5659 else if ((leftvt
== VT_EMPTY
|| leftvt
== VT_I2
||
5660 leftvt
== VT_I4
|| leftvt
== VT_R4
||
5661 leftvt
== VT_R8
|| leftvt
== VT_CY
||
5662 leftvt
== VT_DATE
|| leftvt
== VT_BSTR
||
5663 leftvt
== VT_BOOL
|| leftvt
== VT_DECIMAL
||
5664 (leftvt
>= VT_I1
&& leftvt
<= VT_UINT
)) &&
5665 (rightvt
== VT_EMPTY
|| rightvt
== VT_I2
||
5666 rightvt
== VT_I4
|| rightvt
== VT_R4
||
5667 rightvt
== VT_R8
|| rightvt
== VT_CY
||
5668 rightvt
== VT_DATE
|| rightvt
== VT_BSTR
||
5669 rightvt
== VT_BOOL
|| rightvt
== VT_DECIMAL
||
5670 (rightvt
>= VT_I1
&& rightvt
<= VT_UINT
)))
5674 hr
= DISP_E_BADVARTYPE
;
5678 hr
= VariantChangeType(&dl
,left
,0,resvt
);
5680 ERR("Could not change passed left argument to VT_R8, handle it differently.\n");
5685 hr
= VariantChangeType(&dr
,right
,0,resvt
);
5687 ERR("Could not change passed right argument to VT_R8, handle it differently.\n");
5692 V_VT(result
) = VT_R8
;
5693 V_R8(result
) = pow(V_R8(&dl
),V_R8(&dr
));
5698 VariantClear(&tempLeft
);
5699 VariantClear(&tempRight
);
5704 /**********************************************************************
5705 * VarImp [OLEAUT32.154]
5707 * Bitwise implication of two variants.
5710 * left [I] First variant
5711 * right [I] Second variant
5712 * result [O] Result variant
5716 * Failure: An HRESULT error code indicating the error.
5718 HRESULT WINAPI
VarImp(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
5720 HRESULT hres
= S_OK
;
5721 VARTYPE resvt
= VT_EMPTY
;
5722 VARTYPE leftvt
,rightvt
;
5723 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
5726 VARIANT tempLeft
, tempRight
;
5730 VariantInit(&tempLeft
);
5731 VariantInit(&tempRight
);
5733 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
5735 /* Handle VT_DISPATCH by storing and taking address of returned value */
5736 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
5738 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
5739 if (FAILED(hres
)) goto VarImp_Exit
;
5742 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
5744 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
5745 if (FAILED(hres
)) goto VarImp_Exit
;
5749 leftvt
= V_VT(left
)&VT_TYPEMASK
;
5750 rightvt
= V_VT(right
)&VT_TYPEMASK
;
5751 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
5752 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
5754 if (leftExtraFlags
!= rightExtraFlags
)
5756 hres
= DISP_E_BADVARTYPE
;
5759 ExtraFlags
= leftExtraFlags
;
5761 /* Native VarImp always returns an error when using extra
5762 * flags or if the variants are I8 and INT.
5764 if ((leftvt
== VT_I8
&& rightvt
== VT_INT
) ||
5767 hres
= DISP_E_BADVARTYPE
;
5771 /* Determine result type */
5772 else if ((leftvt
== VT_NULL
&& rightvt
== VT_NULL
) ||
5773 (leftvt
== VT_NULL
&& rightvt
== VT_EMPTY
))
5775 V_VT(result
) = VT_NULL
;
5779 else if (leftvt
== VT_I8
|| rightvt
== VT_I8
)
5781 else if (leftvt
== VT_I4
|| rightvt
== VT_I4
||
5782 leftvt
== VT_INT
|| rightvt
== VT_INT
||
5783 leftvt
== VT_UINT
|| rightvt
== VT_UINT
||
5784 leftvt
== VT_UI4
|| rightvt
== VT_UI4
||
5785 leftvt
== VT_UI8
|| rightvt
== VT_UI8
||
5786 leftvt
== VT_UI2
|| rightvt
== VT_UI2
||
5787 leftvt
== VT_DECIMAL
|| rightvt
== VT_DECIMAL
||
5788 leftvt
== VT_DATE
|| rightvt
== VT_DATE
||
5789 leftvt
== VT_CY
|| rightvt
== VT_CY
||
5790 leftvt
== VT_R8
|| rightvt
== VT_R8
||
5791 leftvt
== VT_R4
|| rightvt
== VT_R4
||
5792 leftvt
== VT_I1
|| rightvt
== VT_I1
)
5794 else if ((leftvt
== VT_UI1
&& rightvt
== VT_UI1
) ||
5795 (leftvt
== VT_UI1
&& rightvt
== VT_NULL
) ||
5796 (leftvt
== VT_NULL
&& rightvt
== VT_UI1
))
5798 else if (leftvt
== VT_EMPTY
|| rightvt
== VT_EMPTY
||
5799 leftvt
== VT_I2
|| rightvt
== VT_I2
||
5800 leftvt
== VT_UI1
|| rightvt
== VT_UI1
)
5802 else if (leftvt
== VT_BOOL
|| rightvt
== VT_BOOL
||
5803 leftvt
== VT_BSTR
|| rightvt
== VT_BSTR
)
5806 /* VT_NULL requires special handling for when the opposite
5807 * variant is equal to something other than -1.
5808 * (NULL Imp 0 = NULL, NULL Imp n = n)
5810 if (leftvt
== VT_NULL
)
5815 case VT_I1
: if (!V_I1(right
)) resvt
= VT_NULL
; break;
5816 case VT_UI1
: if (!V_UI1(right
)) resvt
= VT_NULL
; break;
5817 case VT_I2
: if (!V_I2(right
)) resvt
= VT_NULL
; break;
5818 case VT_UI2
: if (!V_UI2(right
)) resvt
= VT_NULL
; break;
5819 case VT_I4
: if (!V_I4(right
)) resvt
= VT_NULL
; break;
5820 case VT_UI4
: if (!V_UI4(right
)) resvt
= VT_NULL
; break;
5821 case VT_I8
: if (!V_I8(right
)) resvt
= VT_NULL
; break;
5822 case VT_UI8
: if (!V_UI8(right
)) resvt
= VT_NULL
; break;
5823 case VT_INT
: if (!V_INT(right
)) resvt
= VT_NULL
; break;
5824 case VT_UINT
: if (!V_UINT(right
)) resvt
= VT_NULL
; break;
5825 case VT_BOOL
: if (!V_BOOL(right
)) resvt
= VT_NULL
; break;
5826 case VT_R4
: if (!V_R4(right
)) resvt
= VT_NULL
; break;
5827 case VT_R8
: if (!V_R8(right
)) resvt
= VT_NULL
; break;
5828 case VT_DATE
: if (!V_DATE(right
)) resvt
= VT_NULL
; break;
5829 case VT_CY
: if (!V_CY(right
).int64
) resvt
= VT_NULL
; break;
5831 if (!(DEC_HI32(&V_DECIMAL(right
)) || DEC_LO64(&V_DECIMAL(right
))))
5835 hres
= VarBoolFromStr(V_BSTR(right
),LOCALE_USER_DEFAULT
, VAR_LOCALBOOL
, &b
);
5836 if (FAILED(hres
)) goto VarImp_Exit
;
5838 V_VT(result
) = VT_NULL
;
5841 V_VT(result
) = VT_BOOL
;
5846 if (resvt
== VT_NULL
)
5848 V_VT(result
) = resvt
;
5853 hres
= VariantChangeType(result
,right
,0,resvt
);
5858 /* Special handling is required when NULL is the right variant.
5859 * (-1 Imp NULL = NULL, n Imp NULL = n Imp 0)
5861 else if (rightvt
== VT_NULL
)
5866 case VT_I1
: if (V_I1(left
) == -1) resvt
= VT_NULL
; break;
5867 case VT_UI1
: if (V_UI1(left
) == 0xff) resvt
= VT_NULL
; break;
5868 case VT_I2
: if (V_I2(left
) == -1) resvt
= VT_NULL
; break;
5869 case VT_UI2
: if (V_UI2(left
) == 0xffff) resvt
= VT_NULL
; break;
5870 case VT_INT
: if (V_INT(left
) == -1) resvt
= VT_NULL
; break;
5871 case VT_UINT
: if (V_UINT(left
) == ~0u) resvt
= VT_NULL
; break;
5872 case VT_I4
: if (V_I4(left
) == -1) resvt
= VT_NULL
; break;
5873 case VT_UI4
: if (V_UI4(left
) == ~0u) resvt
= VT_NULL
; break;
5874 case VT_I8
: if (V_I8(left
) == -1) resvt
= VT_NULL
; break;
5875 case VT_UI8
: if (V_UI8(left
) == ~(ULONGLONG
)0) resvt
= VT_NULL
; break;
5876 case VT_BOOL
: if (V_BOOL(left
) == VARIANT_TRUE
) resvt
= VT_NULL
; break;
5877 case VT_R4
: if (V_R4(left
) == -1.0) resvt
= VT_NULL
; break;
5878 case VT_R8
: if (V_R8(left
) == -1.0) resvt
= VT_NULL
; break;
5879 case VT_CY
: if (V_CY(left
).int64
== -1) resvt
= VT_NULL
; break;
5881 if (DEC_HI32(&V_DECIMAL(left
)) == 0xffffffff)
5885 hres
= VarBoolFromStr(V_BSTR(left
),LOCALE_USER_DEFAULT
, VAR_LOCALBOOL
, &b
);
5886 if (FAILED(hres
)) goto VarImp_Exit
;
5887 else if (b
== VARIANT_TRUE
)
5890 if (resvt
== VT_NULL
)
5892 V_VT(result
) = resvt
;
5897 hres
= VariantCopy(&lv
, left
);
5898 if (FAILED(hres
)) goto VarImp_Exit
;
5900 if (rightvt
== VT_NULL
)
5902 memset( &rv
, 0, sizeof(rv
) );
5907 hres
= VariantCopy(&rv
, right
);
5908 if (FAILED(hres
)) goto VarImp_Exit
;
5911 if (V_VT(&lv
) == VT_BSTR
&&
5912 FAILED(VarR8FromStr(V_BSTR(&lv
),LOCALE_USER_DEFAULT
, 0, &d
)))
5913 hres
= VariantChangeType(&lv
,&lv
,VARIANT_LOCALBOOL
, VT_BOOL
);
5914 if (SUCCEEDED(hres
) && V_VT(&lv
) != resvt
)
5915 hres
= VariantChangeType(&lv
,&lv
,0,resvt
);
5916 if (FAILED(hres
)) goto VarImp_Exit
;
5918 if (V_VT(&rv
) == VT_BSTR
&&
5919 FAILED(VarR8FromStr(V_BSTR(&rv
),LOCALE_USER_DEFAULT
, 0, &d
)))
5920 hres
= VariantChangeType(&rv
, &rv
,VARIANT_LOCALBOOL
, VT_BOOL
);
5921 if (SUCCEEDED(hres
) && V_VT(&rv
) != resvt
)
5922 hres
= VariantChangeType(&rv
, &rv
, 0, resvt
);
5923 if (FAILED(hres
)) goto VarImp_Exit
;
5926 V_VT(result
) = resvt
;
5930 V_I8(result
) = (~V_I8(&lv
)) | V_I8(&rv
);
5933 V_I4(result
) = (~V_I4(&lv
)) | V_I4(&rv
);
5936 V_I2(result
) = (~V_I2(&lv
)) | V_I2(&rv
);
5939 V_UI1(result
) = (~V_UI1(&lv
)) | V_UI1(&rv
);
5942 V_BOOL(result
) = (~V_BOOL(&lv
)) | V_BOOL(&rv
);
5945 FIXME("Couldn't perform bitwise implication on variant types %d,%d\n",
5953 VariantClear(&tempLeft
);
5954 VariantClear(&tempRight
);