4 * Copyright 1998 Jean-Claude Cote
5 * Copyright 2003 Jon Griffiths
6 * Copyright 2005 Daniel Remenak
8 * The alorithm for conversion from Julian days to day/month/year is based on
9 * that devised by Henry Fliegel, as implemented in PostgreSQL, which is
10 * Copyright 1994-7 Regents of the University of California
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
39 #include "wine/unicode.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(variant
);
46 const char* wine_vtypes
[VT_CLSID
] =
48 "VT_EMPTY","VT_NULL","VT_I2","VT_I4","VT_R4","VT_R8","VT_CY","VT_DATE",
49 "VT_BSTR","VT_DISPATCH","VT_ERROR","VT_BOOL","VT_VARIANT","VT_UNKNOWN",
50 "VT_DECIMAL","15","VT_I1","VT_UI1","VT_UI2","VT_UI4","VT_I8","VT_UI8",
51 "VT_INT","VT_UINT","VT_VOID","VT_HRESULT","VT_PTR","VT_SAFEARRAY",
52 "VT_CARRAY","VT_USERDEFINED","VT_LPSTR","VT_LPWSTR""32","33","34","35",
53 "VT_RECORD","VT_INT_PTR","VT_UINT_PTR","39","40","41","42","43","44","45",
54 "46","47","48","49","50","51","52","53","54","55","56","57","58","59","60",
55 "61","62","63","VT_FILETIME","VT_BLOB","VT_STREAM","VT_STORAGE",
56 "VT_STREAMED_OBJECT","VT_STORED_OBJECT","VT_BLOB_OBJECT","VT_CF","VT_CLSID"
59 const char* wine_vflags
[16] =
64 "|VT_VECTOR|VT_ARRAY",
66 "|VT_VECTOR|VT_ARRAY",
68 "|VT_VECTOR|VT_ARRAY|VT_BYREF",
70 "|VT_VECTOR|VT_HARDTYPE",
71 "|VT_ARRAY|VT_HARDTYPE",
72 "|VT_VECTOR|VT_ARRAY|VT_HARDTYPE",
73 "|VT_BYREF|VT_HARDTYPE",
74 "|VT_VECTOR|VT_ARRAY|VT_HARDTYPE",
75 "|VT_ARRAY|VT_BYREF|VT_HARDTYPE",
76 "|VT_VECTOR|VT_ARRAY|VT_BYREF|VT_HARDTYPE",
79 /* Convert a variant from one type to another */
80 static inline HRESULT
VARIANT_Coerce(VARIANTARG
* pd
, LCID lcid
, USHORT wFlags
,
81 VARIANTARG
* ps
, VARTYPE vt
)
83 HRESULT res
= DISP_E_TYPEMISMATCH
;
84 VARTYPE vtFrom
= V_TYPE(ps
);
85 BOOL bIgnoreOverflow
= FALSE
;
88 TRACE("(%p->(%s%s),0x%08lx,0x%04x,%p->(%s%s),%s%s)\n", pd
, debugstr_VT(pd
),
89 debugstr_VF(pd
), lcid
, wFlags
, ps
, debugstr_VT(ps
), debugstr_VF(ps
),
90 debugstr_vt(vt
), debugstr_vf(vt
));
92 if (vt
== VT_BSTR
|| vtFrom
== VT_BSTR
)
94 /* All flags passed to low level function are only used for
95 * changing to or from strings. Map these here.
97 if (wFlags
& VARIANT_LOCALBOOL
)
98 dwFlags
|= VAR_LOCALBOOL
;
99 if (wFlags
& VARIANT_CALENDAR_HIJRI
)
100 dwFlags
|= VAR_CALENDAR_HIJRI
;
101 if (wFlags
& VARIANT_CALENDAR_THAI
)
102 dwFlags
|= VAR_CALENDAR_THAI
;
103 if (wFlags
& VARIANT_CALENDAR_GREGORIAN
)
104 dwFlags
|= VAR_CALENDAR_GREGORIAN
;
105 if (wFlags
& VARIANT_NOUSEROVERRIDE
)
106 dwFlags
|= LOCALE_NOUSEROVERRIDE
;
107 if (wFlags
& VARIANT_USE_NLS
)
108 dwFlags
|= LOCALE_USE_NLS
;
111 /* Map int/uint to i4/ui4 */
114 else if (vt
== VT_UINT
)
117 if (vtFrom
== VT_INT
)
119 else if (vtFrom
== VT_UINT
)
123 bIgnoreOverflow
= TRUE
;
127 return VariantCopy(pd
, ps
);
129 if (wFlags
& VARIANT_NOVALUEPROP
&& vtFrom
== VT_DISPATCH
&& vt
!= VT_UNKNOWN
)
131 /* VARIANT_NOVALUEPROP prevents IDispatch objects from being coerced by
132 * accessing the default object property.
134 return DISP_E_TYPEMISMATCH
;
140 if (vtFrom
== VT_NULL
)
141 return DISP_E_TYPEMISMATCH
;
142 /* ... Fall through */
144 if (vtFrom
<= VT_UINT
&& vtFrom
!= (VARTYPE
)15 && vtFrom
!= VT_ERROR
)
146 res
= VariantClear( pd
);
147 if (vt
== VT_NULL
&& SUCCEEDED(res
))
155 case VT_EMPTY
: V_I1(pd
) = 0; return S_OK
;
156 case VT_I2
: return VarI1FromI2(V_I2(ps
), &V_I1(pd
));
157 case VT_I4
: return VarI1FromI4(V_I4(ps
), &V_I1(pd
));
158 case VT_UI1
: return VarI1FromUI1(V_UI1(ps
), &V_I1(pd
));
159 case VT_UI2
: return VarI1FromUI2(V_UI2(ps
), &V_I1(pd
));
160 case VT_UI4
: return VarI1FromUI4(V_UI4(ps
), &V_I1(pd
));
161 case VT_I8
: return VarI1FromI8(V_I8(ps
), &V_I1(pd
));
162 case VT_UI8
: return VarI1FromUI8(V_UI8(ps
), &V_I1(pd
));
163 case VT_R4
: return VarI1FromR4(V_R4(ps
), &V_I1(pd
));
164 case VT_R8
: return VarI1FromR8(V_R8(ps
), &V_I1(pd
));
165 case VT_DATE
: return VarI1FromDate(V_DATE(ps
), &V_I1(pd
));
166 case VT_BOOL
: return VarI1FromBool(V_BOOL(ps
), &V_I1(pd
));
167 case VT_CY
: return VarI1FromCy(V_CY(ps
), &V_I1(pd
));
168 case VT_DECIMAL
: return VarI1FromDec(&V_DECIMAL(ps
), &V_I1(pd
) );
169 case VT_DISPATCH
: return VarI1FromDisp(V_DISPATCH(ps
), lcid
, &V_I1(pd
) );
170 case VT_BSTR
: return VarI1FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I1(pd
) );
177 case VT_EMPTY
: V_I2(pd
) = 0; return S_OK
;
178 case VT_I1
: return VarI2FromI1(V_I1(ps
), &V_I2(pd
));
179 case VT_I4
: return VarI2FromI4(V_I4(ps
), &V_I2(pd
));
180 case VT_UI1
: return VarI2FromUI1(V_UI1(ps
), &V_I2(pd
));
181 case VT_UI2
: return VarI2FromUI2(V_UI2(ps
), &V_I2(pd
));
182 case VT_UI4
: return VarI2FromUI4(V_UI4(ps
), &V_I2(pd
));
183 case VT_I8
: return VarI2FromI8(V_I8(ps
), &V_I2(pd
));
184 case VT_UI8
: return VarI2FromUI8(V_UI8(ps
), &V_I2(pd
));
185 case VT_R4
: return VarI2FromR4(V_R4(ps
), &V_I2(pd
));
186 case VT_R8
: return VarI2FromR8(V_R8(ps
), &V_I2(pd
));
187 case VT_DATE
: return VarI2FromDate(V_DATE(ps
), &V_I2(pd
));
188 case VT_BOOL
: return VarI2FromBool(V_BOOL(ps
), &V_I2(pd
));
189 case VT_CY
: return VarI2FromCy(V_CY(ps
), &V_I2(pd
));
190 case VT_DECIMAL
: return VarI2FromDec(&V_DECIMAL(ps
), &V_I2(pd
));
191 case VT_DISPATCH
: return VarI2FromDisp(V_DISPATCH(ps
), lcid
, &V_I2(pd
));
192 case VT_BSTR
: return VarI2FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I2(pd
));
199 case VT_EMPTY
: V_I4(pd
) = 0; return S_OK
;
200 case VT_I1
: return VarI4FromI1(V_I1(ps
), &V_I4(pd
));
201 case VT_I2
: return VarI4FromI2(V_I2(ps
), &V_I4(pd
));
202 case VT_UI1
: return VarI4FromUI1(V_UI1(ps
), &V_I4(pd
));
203 case VT_UI2
: return VarI4FromUI2(V_UI2(ps
), &V_I4(pd
));
211 return VarI4FromUI4(V_UI4(ps
), &V_I4(pd
));
212 case VT_I8
: return VarI4FromI8(V_I8(ps
), &V_I4(pd
));
213 case VT_UI8
: return VarI4FromUI8(V_UI8(ps
), &V_I4(pd
));
214 case VT_R4
: return VarI4FromR4(V_R4(ps
), &V_I4(pd
));
215 case VT_R8
: return VarI4FromR8(V_R8(ps
), &V_I4(pd
));
216 case VT_DATE
: return VarI4FromDate(V_DATE(ps
), &V_I4(pd
));
217 case VT_BOOL
: return VarI4FromBool(V_BOOL(ps
), &V_I4(pd
));
218 case VT_CY
: return VarI4FromCy(V_CY(ps
), &V_I4(pd
));
219 case VT_DECIMAL
: return VarI4FromDec(&V_DECIMAL(ps
), &V_I4(pd
));
220 case VT_DISPATCH
: return VarI4FromDisp(V_DISPATCH(ps
), lcid
, &V_I4(pd
));
221 case VT_BSTR
: return VarI4FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I4(pd
));
228 case VT_EMPTY
: V_UI1(pd
) = 0; return S_OK
;
229 case VT_I1
: return VarUI1FromI1(V_I1(ps
), &V_UI1(pd
));
230 case VT_I2
: return VarUI1FromI2(V_I2(ps
), &V_UI1(pd
));
231 case VT_I4
: return VarUI1FromI4(V_I4(ps
), &V_UI1(pd
));
232 case VT_UI2
: return VarUI1FromUI2(V_UI2(ps
), &V_UI1(pd
));
233 case VT_UI4
: return VarUI1FromUI4(V_UI4(ps
), &V_UI1(pd
));
234 case VT_I8
: return VarUI1FromI8(V_I8(ps
), &V_UI1(pd
));
235 case VT_UI8
: return VarUI1FromUI8(V_UI8(ps
), &V_UI1(pd
));
236 case VT_R4
: return VarUI1FromR4(V_R4(ps
), &V_UI1(pd
));
237 case VT_R8
: return VarUI1FromR8(V_R8(ps
), &V_UI1(pd
));
238 case VT_DATE
: return VarUI1FromDate(V_DATE(ps
), &V_UI1(pd
));
239 case VT_BOOL
: return VarUI1FromBool(V_BOOL(ps
), &V_UI1(pd
));
240 case VT_CY
: return VarUI1FromCy(V_CY(ps
), &V_UI1(pd
));
241 case VT_DECIMAL
: return VarUI1FromDec(&V_DECIMAL(ps
), &V_UI1(pd
));
242 case VT_DISPATCH
: return VarUI1FromDisp(V_DISPATCH(ps
), lcid
, &V_UI1(pd
));
243 case VT_BSTR
: return VarUI1FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI1(pd
));
250 case VT_EMPTY
: V_UI2(pd
) = 0; return S_OK
;
251 case VT_I1
: return VarUI2FromI1(V_I1(ps
), &V_UI2(pd
));
252 case VT_I2
: return VarUI2FromI2(V_I2(ps
), &V_UI2(pd
));
253 case VT_I4
: return VarUI2FromI4(V_I4(ps
), &V_UI2(pd
));
254 case VT_UI1
: return VarUI2FromUI1(V_UI1(ps
), &V_UI2(pd
));
255 case VT_UI4
: return VarUI2FromUI4(V_UI4(ps
), &V_UI2(pd
));
256 case VT_I8
: return VarUI4FromI8(V_I8(ps
), &V_UI4(pd
));
257 case VT_UI8
: return VarUI4FromUI8(V_UI8(ps
), &V_UI4(pd
));
258 case VT_R4
: return VarUI2FromR4(V_R4(ps
), &V_UI2(pd
));
259 case VT_R8
: return VarUI2FromR8(V_R8(ps
), &V_UI2(pd
));
260 case VT_DATE
: return VarUI2FromDate(V_DATE(ps
), &V_UI2(pd
));
261 case VT_BOOL
: return VarUI2FromBool(V_BOOL(ps
), &V_UI2(pd
));
262 case VT_CY
: return VarUI2FromCy(V_CY(ps
), &V_UI2(pd
));
263 case VT_DECIMAL
: return VarUI2FromDec(&V_DECIMAL(ps
), &V_UI2(pd
));
264 case VT_DISPATCH
: return VarUI2FromDisp(V_DISPATCH(ps
), lcid
, &V_UI2(pd
));
265 case VT_BSTR
: return VarUI2FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI2(pd
));
272 case VT_EMPTY
: V_UI4(pd
) = 0; return S_OK
;
273 case VT_I1
: return VarUI4FromI1(V_I1(ps
), &V_UI4(pd
));
274 case VT_I2
: return VarUI4FromI2(V_I2(ps
), &V_UI4(pd
));
275 case VT_I4
: return VarUI4FromI4(V_I4(ps
), &V_UI4(pd
));
276 case VT_UI1
: return VarUI4FromUI1(V_UI1(ps
), &V_UI4(pd
));
277 case VT_UI2
: return VarUI4FromUI2(V_UI2(ps
), &V_UI4(pd
));
278 case VT_I8
: return VarUI4FromI8(V_I8(ps
), &V_UI4(pd
));
279 case VT_UI8
: return VarUI4FromUI8(V_UI8(ps
), &V_UI4(pd
));
280 case VT_R4
: return VarUI4FromR4(V_R4(ps
), &V_UI4(pd
));
281 case VT_R8
: return VarUI4FromR8(V_R8(ps
), &V_UI4(pd
));
282 case VT_DATE
: return VarUI4FromDate(V_DATE(ps
), &V_UI4(pd
));
283 case VT_BOOL
: return VarUI4FromBool(V_BOOL(ps
), &V_UI4(pd
));
284 case VT_CY
: return VarUI4FromCy(V_CY(ps
), &V_UI4(pd
));
285 case VT_DECIMAL
: return VarUI4FromDec(&V_DECIMAL(ps
), &V_UI4(pd
));
286 case VT_DISPATCH
: return VarUI4FromDisp(V_DISPATCH(ps
), lcid
, &V_UI4(pd
));
287 case VT_BSTR
: return VarUI4FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI4(pd
));
294 case VT_EMPTY
: V_UI8(pd
) = 0; return S_OK
;
295 case VT_I4
: if (V_I4(ps
) < 0) return DISP_E_OVERFLOW
; V_UI8(pd
) = V_I4(ps
); return S_OK
;
296 case VT_I1
: return VarUI8FromI1(V_I1(ps
), &V_UI8(pd
));
297 case VT_I2
: return VarUI8FromI2(V_I2(ps
), &V_UI8(pd
));
298 case VT_UI1
: return VarUI8FromUI1(V_UI1(ps
), &V_UI8(pd
));
299 case VT_UI2
: return VarUI8FromUI2(V_UI2(ps
), &V_UI8(pd
));
300 case VT_UI4
: return VarUI8FromUI4(V_UI4(ps
), &V_UI8(pd
));
301 case VT_I8
: return VarUI8FromI8(V_I8(ps
), &V_UI8(pd
));
302 case VT_R4
: return VarUI8FromR4(V_R4(ps
), &V_UI8(pd
));
303 case VT_R8
: return VarUI8FromR8(V_R8(ps
), &V_UI8(pd
));
304 case VT_DATE
: return VarUI8FromDate(V_DATE(ps
), &V_UI8(pd
));
305 case VT_BOOL
: return VarUI8FromBool(V_BOOL(ps
), &V_UI8(pd
));
306 case VT_CY
: return VarUI8FromCy(V_CY(ps
), &V_UI8(pd
));
307 case VT_DECIMAL
: return VarUI8FromDec(&V_DECIMAL(ps
), &V_UI8(pd
));
308 case VT_DISPATCH
: return VarUI8FromDisp(V_DISPATCH(ps
), lcid
, &V_UI8(pd
));
309 case VT_BSTR
: return VarUI8FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI8(pd
));
316 case VT_EMPTY
: V_I8(pd
) = 0; return S_OK
;
317 case VT_I4
: V_I8(pd
) = V_I4(ps
); return S_OK
;
318 case VT_I1
: return VarI8FromI1(V_I1(ps
), &V_I8(pd
));
319 case VT_I2
: return VarI8FromI2(V_I2(ps
), &V_I8(pd
));
320 case VT_UI1
: return VarI8FromUI1(V_UI1(ps
), &V_I8(pd
));
321 case VT_UI2
: return VarI8FromUI2(V_UI2(ps
), &V_I8(pd
));
322 case VT_UI4
: return VarI8FromUI4(V_UI4(ps
), &V_I8(pd
));
323 case VT_UI8
: return VarI8FromUI8(V_I8(ps
), &V_I8(pd
));
324 case VT_R4
: return VarI8FromR4(V_R4(ps
), &V_I8(pd
));
325 case VT_R8
: return VarI8FromR8(V_R8(ps
), &V_I8(pd
));
326 case VT_DATE
: return VarI8FromDate(V_DATE(ps
), &V_I8(pd
));
327 case VT_BOOL
: return VarI8FromBool(V_BOOL(ps
), &V_I8(pd
));
328 case VT_CY
: return VarI8FromCy(V_CY(ps
), &V_I8(pd
));
329 case VT_DECIMAL
: return VarI8FromDec(&V_DECIMAL(ps
), &V_I8(pd
));
330 case VT_DISPATCH
: return VarI8FromDisp(V_DISPATCH(ps
), lcid
, &V_I8(pd
));
331 case VT_BSTR
: return VarI8FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I8(pd
));
338 case VT_EMPTY
: V_R4(pd
) = 0.0f
; return S_OK
;
339 case VT_I1
: return VarR4FromI1(V_I1(ps
), &V_R4(pd
));
340 case VT_I2
: return VarR4FromI2(V_I2(ps
), &V_R4(pd
));
341 case VT_I4
: return VarR4FromI4(V_I4(ps
), &V_R4(pd
));
342 case VT_UI1
: return VarR4FromUI1(V_UI1(ps
), &V_R4(pd
));
343 case VT_UI2
: return VarR4FromUI2(V_UI2(ps
), &V_R4(pd
));
344 case VT_UI4
: return VarR4FromUI4(V_UI4(ps
), &V_R4(pd
));
345 case VT_I8
: return VarR4FromI8(V_I8(ps
), &V_R4(pd
));
346 case VT_UI8
: return VarR4FromUI8(V_UI8(ps
), &V_R4(pd
));
347 case VT_R8
: return VarR4FromR8(V_R8(ps
), &V_R4(pd
));
348 case VT_DATE
: return VarR4FromDate(V_DATE(ps
), &V_R4(pd
));
349 case VT_BOOL
: return VarR4FromBool(V_BOOL(ps
), &V_R4(pd
));
350 case VT_CY
: return VarR4FromCy(V_CY(ps
), &V_R4(pd
));
351 case VT_DECIMAL
: return VarR4FromDec(&V_DECIMAL(ps
), &V_R4(pd
));
352 case VT_DISPATCH
: return VarR4FromDisp(V_DISPATCH(ps
), lcid
, &V_R4(pd
));
353 case VT_BSTR
: return VarR4FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_R4(pd
));
360 case VT_EMPTY
: V_R8(pd
) = 0.0; return S_OK
;
361 case VT_I1
: return VarR8FromI1(V_I1(ps
), &V_R8(pd
));
362 case VT_I2
: return VarR8FromI2(V_I2(ps
), &V_R8(pd
));
363 case VT_I4
: return VarR8FromI4(V_I4(ps
), &V_R8(pd
));
364 case VT_UI1
: return VarR8FromUI1(V_UI1(ps
), &V_R8(pd
));
365 case VT_UI2
: return VarR8FromUI2(V_UI2(ps
), &V_R8(pd
));
366 case VT_UI4
: return VarR8FromUI4(V_UI4(ps
), &V_R8(pd
));
367 case VT_I8
: return VarR8FromI8(V_I8(ps
), &V_R8(pd
));
368 case VT_UI8
: return VarR8FromUI8(V_UI8(ps
), &V_R8(pd
));
369 case VT_R4
: return VarR8FromR4(V_R4(ps
), &V_R8(pd
));
370 case VT_DATE
: return VarR8FromDate(V_DATE(ps
), &V_R8(pd
));
371 case VT_BOOL
: return VarR8FromBool(V_BOOL(ps
), &V_R8(pd
));
372 case VT_CY
: return VarR8FromCy(V_CY(ps
), &V_R8(pd
));
373 case VT_DECIMAL
: return VarR8FromDec(&V_DECIMAL(ps
), &V_R8(pd
));
374 case VT_DISPATCH
: return VarR8FromDisp(V_DISPATCH(ps
), lcid
, &V_R8(pd
));
375 case VT_BSTR
: return VarR8FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_R8(pd
));
382 case VT_EMPTY
: V_DATE(pd
) = 0.0; return S_OK
;
383 case VT_I1
: return VarDateFromI1(V_I1(ps
), &V_DATE(pd
));
384 case VT_I2
: return VarDateFromI2(V_I2(ps
), &V_DATE(pd
));
385 case VT_I4
: return VarDateFromI4(V_I4(ps
), &V_DATE(pd
));
386 case VT_UI1
: return VarDateFromUI1(V_UI1(ps
), &V_DATE(pd
));
387 case VT_UI2
: return VarDateFromUI2(V_UI2(ps
), &V_DATE(pd
));
388 case VT_UI4
: return VarDateFromUI4(V_UI4(ps
), &V_DATE(pd
));
389 case VT_I8
: return VarDateFromI8(V_I8(ps
), &V_DATE(pd
));
390 case VT_UI8
: return VarDateFromUI8(V_UI8(ps
), &V_DATE(pd
));
391 case VT_R4
: return VarDateFromR4(V_R4(ps
), &V_DATE(pd
));
392 case VT_R8
: return VarDateFromR8(V_R8(ps
), &V_DATE(pd
));
393 case VT_BOOL
: return VarDateFromBool(V_BOOL(ps
), &V_DATE(pd
));
394 case VT_CY
: return VarDateFromCy(V_CY(ps
), &V_DATE(pd
));
395 case VT_DECIMAL
: return VarDateFromDec(&V_DECIMAL(ps
), &V_DATE(pd
));
396 case VT_DISPATCH
: return VarDateFromDisp(V_DISPATCH(ps
), lcid
, &V_DATE(pd
));
397 case VT_BSTR
: return VarDateFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_DATE(pd
));
404 case VT_EMPTY
: V_BOOL(pd
) = 0; return S_OK
;
405 case VT_I1
: return VarBoolFromI1(V_I1(ps
), &V_BOOL(pd
));
406 case VT_I2
: return VarBoolFromI2(V_I2(ps
), &V_BOOL(pd
));
407 case VT_I4
: return VarBoolFromI4(V_I4(ps
), &V_BOOL(pd
));
408 case VT_UI1
: return VarBoolFromUI1(V_UI1(ps
), &V_BOOL(pd
));
409 case VT_UI2
: return VarBoolFromUI2(V_UI2(ps
), &V_BOOL(pd
));
410 case VT_UI4
: return VarBoolFromUI4(V_UI4(ps
), &V_BOOL(pd
));
411 case VT_I8
: return VarBoolFromI8(V_I8(ps
), &V_BOOL(pd
));
412 case VT_UI8
: return VarBoolFromUI8(V_UI8(ps
), &V_BOOL(pd
));
413 case VT_R4
: return VarBoolFromR4(V_R4(ps
), &V_BOOL(pd
));
414 case VT_R8
: return VarBoolFromR8(V_R8(ps
), &V_BOOL(pd
));
415 case VT_DATE
: return VarBoolFromDate(V_DATE(ps
), &V_BOOL(pd
));
416 case VT_CY
: return VarBoolFromCy(V_CY(ps
), &V_BOOL(pd
));
417 case VT_DECIMAL
: return VarBoolFromDec(&V_DECIMAL(ps
), &V_BOOL(pd
));
418 case VT_DISPATCH
: return VarBoolFromDisp(V_DISPATCH(ps
), lcid
, &V_BOOL(pd
));
419 case VT_BSTR
: return VarBoolFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_BOOL(pd
));
427 V_BSTR(pd
) = SysAllocStringLen(NULL
, 0);
428 return V_BSTR(pd
) ? S_OK
: E_OUTOFMEMORY
;
430 if (wFlags
& (VARIANT_ALPHABOOL
|VARIANT_LOCALBOOL
))
431 return VarBstrFromBool(V_BOOL(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
432 return VarBstrFromI2(V_BOOL(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
433 case VT_I1
: return VarBstrFromI1(V_I1(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
434 case VT_I2
: return VarBstrFromI2(V_I2(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
435 case VT_I4
: return VarBstrFromI4(V_I4(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
436 case VT_UI1
: return VarBstrFromUI1(V_UI1(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
437 case VT_UI2
: return VarBstrFromUI2(V_UI2(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
438 case VT_UI4
: return VarBstrFromUI4(V_UI4(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
439 case VT_I8
: return VarBstrFromI8(V_I8(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
440 case VT_UI8
: return VarBstrFromUI8(V_UI8(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
441 case VT_R4
: return VarBstrFromR4(V_R4(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
442 case VT_R8
: return VarBstrFromR8(V_R8(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
443 case VT_DATE
: return VarBstrFromDate(V_DATE(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
444 case VT_CY
: return VarBstrFromCy(V_CY(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
445 case VT_DECIMAL
: return VarBstrFromDec(&V_DECIMAL(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
446 /* case VT_DISPATCH: return VarBstrFromDisp(V_DISPATCH(ps), lcid, dwFlags, &V_BSTR(pd)); */
453 case VT_EMPTY
: V_CY(pd
).int64
= 0; return S_OK
;
454 case VT_I1
: return VarCyFromI1(V_I1(ps
), &V_CY(pd
));
455 case VT_I2
: return VarCyFromI2(V_I2(ps
), &V_CY(pd
));
456 case VT_I4
: return VarCyFromI4(V_I4(ps
), &V_CY(pd
));
457 case VT_UI1
: return VarCyFromUI1(V_UI1(ps
), &V_CY(pd
));
458 case VT_UI2
: return VarCyFromUI2(V_UI2(ps
), &V_CY(pd
));
459 case VT_UI4
: return VarCyFromUI4(V_UI4(ps
), &V_CY(pd
));
460 case VT_I8
: return VarCyFromI8(V_I8(ps
), &V_CY(pd
));
461 case VT_UI8
: return VarCyFromUI8(V_UI8(ps
), &V_CY(pd
));
462 case VT_R4
: return VarCyFromR4(V_R4(ps
), &V_CY(pd
));
463 case VT_R8
: return VarCyFromR8(V_R8(ps
), &V_CY(pd
));
464 case VT_DATE
: return VarCyFromDate(V_DATE(ps
), &V_CY(pd
));
465 case VT_BOOL
: return VarCyFromBool(V_BOOL(ps
), &V_CY(pd
));
466 case VT_DECIMAL
: return VarCyFromDec(&V_DECIMAL(ps
), &V_CY(pd
));
467 case VT_DISPATCH
: return VarCyFromDisp(V_DISPATCH(ps
), lcid
, &V_CY(pd
));
468 case VT_BSTR
: return VarCyFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_CY(pd
));
477 DEC_SIGNSCALE(&V_DECIMAL(pd
)) = SIGNSCALE(DECIMAL_POS
,0);
478 DEC_HI32(&V_DECIMAL(pd
)) = 0;
479 DEC_MID32(&V_DECIMAL(pd
)) = 0;
480 /* VarDecFromBool() coerces to -1/0, ChangeTypeEx() coerces to 1/0.
481 * VT_NULL and VT_EMPTY always give a 0 value.
483 DEC_LO32(&V_DECIMAL(pd
)) = vtFrom
== VT_BOOL
&& V_BOOL(ps
) ? 1 : 0;
485 case VT_I1
: return VarDecFromI1(V_I1(ps
), &V_DECIMAL(pd
));
486 case VT_I2
: return VarDecFromI2(V_I2(ps
), &V_DECIMAL(pd
));
487 case VT_I4
: return VarDecFromI4(V_I4(ps
), &V_DECIMAL(pd
));
488 case VT_UI1
: return VarDecFromUI1(V_UI1(ps
), &V_DECIMAL(pd
));
489 case VT_UI2
: return VarDecFromUI2(V_UI2(ps
), &V_DECIMAL(pd
));
490 case VT_UI4
: return VarDecFromUI4(V_UI4(ps
), &V_DECIMAL(pd
));
491 case VT_I8
: return VarDecFromI8(V_I8(ps
), &V_DECIMAL(pd
));
492 case VT_UI8
: return VarDecFromUI8(V_UI8(ps
), &V_DECIMAL(pd
));
493 case VT_R4
: return VarDecFromR4(V_R4(ps
), &V_DECIMAL(pd
));
494 case VT_R8
: return VarDecFromR8(V_R8(ps
), &V_DECIMAL(pd
));
495 case VT_DATE
: return VarDecFromDate(V_DATE(ps
), &V_DECIMAL(pd
));
496 case VT_CY
: return VarDecFromCy(V_CY(ps
), &V_DECIMAL(pd
));
497 case VT_DISPATCH
: return VarDecFromDisp(V_DISPATCH(ps
), lcid
, &V_DECIMAL(pd
));
498 case VT_BSTR
: return VarDecFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_DECIMAL(pd
));
506 if (V_DISPATCH(ps
) == NULL
)
507 V_UNKNOWN(pd
) = NULL
;
509 res
= IDispatch_QueryInterface(V_DISPATCH(ps
), &IID_IUnknown
, (LPVOID
*)&V_UNKNOWN(pd
));
518 if (V_UNKNOWN(ps
) == NULL
)
519 V_DISPATCH(pd
) = NULL
;
521 res
= IUnknown_QueryInterface(V_UNKNOWN(ps
), &IID_IDispatch
, (LPVOID
*)&V_DISPATCH(pd
));
532 /* Coerce to/from an array */
533 static inline HRESULT
VARIANT_CoerceArray(VARIANTARG
* pd
, VARIANTARG
* ps
, VARTYPE vt
)
535 if (vt
== VT_BSTR
&& V_VT(ps
) == (VT_ARRAY
|VT_UI1
))
536 return BstrFromVector(V_ARRAY(ps
), &V_BSTR(pd
));
538 if (V_VT(ps
) == VT_BSTR
&& vt
== (VT_ARRAY
|VT_UI1
))
539 return VectorFromBstr(V_BSTR(ps
), &V_ARRAY(ps
));
542 return SafeArrayCopy(V_ARRAY(ps
), &V_ARRAY(pd
));
544 return DISP_E_TYPEMISMATCH
;
547 /******************************************************************************
548 * Check if a variants type is valid.
550 static inline HRESULT
VARIANT_ValidateType(VARTYPE vt
)
552 VARTYPE vtExtra
= vt
& VT_EXTRA_TYPE
;
556 if (!(vtExtra
& (VT_VECTOR
|VT_RESERVED
)))
558 if (vt
< VT_VOID
|| vt
== VT_RECORD
|| vt
== VT_CLSID
)
560 if ((vtExtra
& (VT_BYREF
|VT_ARRAY
)) && vt
<= VT_NULL
)
561 return DISP_E_BADVARTYPE
;
562 if (vt
!= (VARTYPE
)15)
566 return DISP_E_BADVARTYPE
;
569 /******************************************************************************
570 * VariantInit [OLEAUT32.8]
572 * Initialise a variant.
575 * pVarg [O] Variant to initialise
581 * This function simply sets the type of the variant to VT_EMPTY. It does not
582 * free any existing value, use VariantClear() for that.
584 void WINAPI
VariantInit(VARIANTARG
* pVarg
)
586 TRACE("(%p)\n", pVarg
);
588 V_VT(pVarg
) = VT_EMPTY
; /* Native doesn't set any other fields */
591 /******************************************************************************
592 * VariantClear [OLEAUT32.9]
597 * pVarg [I/O] Variant to clear
600 * Success: S_OK. Any previous value in pVarg is freed and its type is set to VT_EMPTY.
601 * Failure: DISP_E_BADVARTYPE, if the variant is a not a valid variant type.
603 HRESULT WINAPI
VariantClear(VARIANTARG
* pVarg
)
607 TRACE("(%p->(%s%s))\n", pVarg
, debugstr_VT(pVarg
), debugstr_VF(pVarg
));
609 hres
= VARIANT_ValidateType(V_VT(pVarg
));
613 if (!V_ISBYREF(pVarg
))
615 if (V_ISARRAY(pVarg
) || V_VT(pVarg
) == VT_SAFEARRAY
)
618 hres
= SafeArrayDestroy(V_ARRAY(pVarg
));
620 else if (V_VT(pVarg
) == VT_BSTR
)
623 SysFreeString(V_BSTR(pVarg
));
625 else if (V_VT(pVarg
) == VT_RECORD
)
627 struct __tagBRECORD
* pBr
= &V_UNION(pVarg
,brecVal
);
630 IRecordInfo_RecordClear(pBr
->pRecInfo
, pBr
->pvRecord
);
631 IRecordInfo_Release(pBr
->pRecInfo
);
634 else if (V_VT(pVarg
) == VT_DISPATCH
||
635 V_VT(pVarg
) == VT_UNKNOWN
)
637 if (V_UNKNOWN(pVarg
))
638 IUnknown_Release(V_UNKNOWN(pVarg
));
640 else if (V_VT(pVarg
) == VT_VARIANT
)
642 if (V_VARIANTREF(pVarg
))
643 VariantClear(V_VARIANTREF(pVarg
));
646 V_VT(pVarg
) = VT_EMPTY
;
651 /******************************************************************************
652 * Copy an IRecordInfo object contained in a variant.
654 static HRESULT
VARIANT_CopyIRecordInfo(struct __tagBRECORD
* pBr
)
662 hres
= IRecordInfo_GetSize(pBr
->pRecInfo
, &ulSize
);
665 PVOID pvRecord
= HeapAlloc(GetProcessHeap(), 0, ulSize
);
667 hres
= E_OUTOFMEMORY
;
670 memcpy(pvRecord
, pBr
->pvRecord
, ulSize
);
671 pBr
->pvRecord
= pvRecord
;
673 hres
= IRecordInfo_RecordCopy(pBr
->pRecInfo
, pvRecord
, pvRecord
);
675 IRecordInfo_AddRef(pBr
->pRecInfo
);
679 else if (pBr
->pvRecord
)
684 /******************************************************************************
685 * VariantCopy [OLEAUT32.10]
690 * pvargDest [O] Destination for copy
691 * pvargSrc [I] Source variant to copy
694 * Success: S_OK. pvargDest contains a copy of pvargSrc.
695 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid type.
696 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
697 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
698 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
701 * - If pvargSrc == pvargDest, this function does nothing, and succeeds if
702 * pvargSrc is valid. Otherwise, pvargDest is always cleared using
703 * VariantClear() before pvargSrc is copied to it. If clearing pvargDest
704 * fails, so does this function.
705 * - VT_CLSID is a valid type type for pvargSrc, but not for pvargDest.
706 * - For by-value non-intrinsic types, a deep copy is made, i.e. The whole value
707 * is copied rather than just any pointers to it.
708 * - For by-value object types the object pointer is copied and the objects
709 * reference count increased using IUnknown_AddRef().
710 * - For all by-reference types, only the referencing pointer is copied.
712 HRESULT WINAPI
VariantCopy(VARIANTARG
* pvargDest
, VARIANTARG
* pvargSrc
)
716 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest
, debugstr_VT(pvargDest
),
717 debugstr_VF(pvargDest
), pvargSrc
, debugstr_VT(pvargSrc
),
718 debugstr_VF(pvargSrc
));
720 if (V_TYPE(pvargSrc
) == VT_CLSID
|| /* VT_CLSID is a special case */
721 FAILED(VARIANT_ValidateType(V_VT(pvargSrc
))))
722 return DISP_E_BADVARTYPE
;
724 if (pvargSrc
!= pvargDest
&&
725 SUCCEEDED(hres
= VariantClear(pvargDest
)))
727 *pvargDest
= *pvargSrc
; /* Shallow copy the value */
729 if (!V_ISBYREF(pvargSrc
))
731 if (V_ISARRAY(pvargSrc
))
733 if (V_ARRAY(pvargSrc
))
734 hres
= SafeArrayCopy(V_ARRAY(pvargSrc
), &V_ARRAY(pvargDest
));
736 else if (V_VT(pvargSrc
) == VT_BSTR
)
738 if (V_BSTR(pvargSrc
))
740 V_BSTR(pvargDest
) = SysAllocStringByteLen((char*)V_BSTR(pvargSrc
), SysStringByteLen(V_BSTR(pvargSrc
)));
741 if (!V_BSTR(pvargDest
))
743 TRACE("!V_BSTR(pvargDest), SysAllocStringByteLen() failed to allocate %d bytes\n", SysStringByteLen(V_BSTR(pvargSrc
)));
744 hres
= E_OUTOFMEMORY
;
748 else if (V_VT(pvargSrc
) == VT_RECORD
)
750 hres
= VARIANT_CopyIRecordInfo(&V_UNION(pvargDest
,brecVal
));
752 else if (V_VT(pvargSrc
) == VT_DISPATCH
||
753 V_VT(pvargSrc
) == VT_UNKNOWN
)
755 if (V_UNKNOWN(pvargSrc
))
756 IUnknown_AddRef(V_UNKNOWN(pvargSrc
));
763 /* Return the byte size of a variants data */
764 static inline size_t VARIANT_DataSize(const VARIANT
* pv
)
769 case VT_UI1
: return sizeof(BYTE
);
771 case VT_UI2
: return sizeof(SHORT
);
775 case VT_UI4
: return sizeof(LONG
);
777 case VT_UI8
: return sizeof(LONGLONG
);
778 case VT_R4
: return sizeof(float);
779 case VT_R8
: return sizeof(double);
780 case VT_DATE
: return sizeof(DATE
);
781 case VT_BOOL
: return sizeof(VARIANT_BOOL
);
784 case VT_BSTR
: return sizeof(void*);
785 case VT_CY
: return sizeof(CY
);
786 case VT_ERROR
: return sizeof(SCODE
);
788 TRACE("Shouldn't be called for vt %s%s!\n", debugstr_VT(pv
), debugstr_VF(pv
));
792 /******************************************************************************
793 * VariantCopyInd [OLEAUT32.11]
795 * Copy a variant, dereferencing it it is by-reference.
798 * pvargDest [O] Destination for copy
799 * pvargSrc [I] Source variant to copy
802 * Success: S_OK. pvargDest contains a copy of pvargSrc.
803 * Failure: An HRESULT error code indicating the error.
806 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid by-value type.
807 * E_INVALIDARG, if pvargSrc is an invalid by-reference type.
808 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
809 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
810 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
813 * - If pvargSrc is by-value, this function behaves exactly as VariantCopy().
814 * - If pvargSrc is by-reference, the value copied to pvargDest is the pointed-to
816 * - if pvargSrc == pvargDest, this function dereferences in place. Otherwise,
817 * pvargDest is always cleared using VariantClear() before pvargSrc is copied
818 * to it. If clearing pvargDest fails, so does this function.
820 HRESULT WINAPI
VariantCopyInd(VARIANT
* pvargDest
, VARIANTARG
* pvargSrc
)
822 VARIANTARG vTmp
, *pSrc
= pvargSrc
;
826 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest
, debugstr_VT(pvargDest
),
827 debugstr_VF(pvargDest
), pvargSrc
, debugstr_VT(pvargSrc
),
828 debugstr_VF(pvargSrc
));
830 if (!V_ISBYREF(pvargSrc
))
831 return VariantCopy(pvargDest
, pvargSrc
);
833 /* Argument checking is more lax than VariantCopy()... */
834 vt
= V_TYPE(pvargSrc
);
835 if (V_ISARRAY(pvargSrc
) ||
836 (vt
> VT_NULL
&& vt
!= (VARTYPE
)15 && vt
< VT_VOID
&&
837 !(V_VT(pvargSrc
) & (VT_VECTOR
|VT_RESERVED
))))
842 return E_INVALIDARG
; /* ...And the return value for invalid types differs too */
844 if (pvargSrc
== pvargDest
)
846 /* In place copy. Use a shallow copy of pvargSrc & init pvargDest.
847 * This avoids an expensive VariantCopy() call - e.g. SafeArrayCopy().
851 V_VT(pvargDest
) = VT_EMPTY
;
855 /* Copy into another variant. Free the variant in pvargDest */
856 if (FAILED(hres
= VariantClear(pvargDest
)))
858 TRACE("VariantClear() of destination failed\n");
865 /* Native doesn't check that *V_ARRAYREF(pSrc) is valid */
866 hres
= SafeArrayCopy(*V_ARRAYREF(pSrc
), &V_ARRAY(pvargDest
));
868 else if (V_VT(pSrc
) == (VT_BSTR
|VT_BYREF
))
870 /* Native doesn't check that *V_BSTRREF(pSrc) is valid */
871 V_BSTR(pvargDest
) = SysAllocStringByteLen((char*)*V_BSTRREF(pSrc
), SysStringByteLen(*V_BSTRREF(pSrc
)));
873 else if (V_VT(pSrc
) == (VT_RECORD
|VT_BYREF
))
875 V_UNION(pvargDest
,brecVal
) = V_UNION(pvargSrc
,brecVal
);
876 hres
= VARIANT_CopyIRecordInfo(&V_UNION(pvargDest
,brecVal
));
878 else if (V_VT(pSrc
) == (VT_DISPATCH
|VT_BYREF
) ||
879 V_VT(pSrc
) == (VT_UNKNOWN
|VT_BYREF
))
881 /* Native doesn't check that *V_UNKNOWNREF(pSrc) is valid */
882 V_UNKNOWN(pvargDest
) = *V_UNKNOWNREF(pSrc
);
883 if (*V_UNKNOWNREF(pSrc
))
884 IUnknown_AddRef(*V_UNKNOWNREF(pSrc
));
886 else if (V_VT(pSrc
) == (VT_VARIANT
|VT_BYREF
))
888 /* Native doesn't check that *V_VARIANTREF(pSrc) is valid */
889 if (V_VT(V_VARIANTREF(pSrc
)) == (VT_VARIANT
|VT_BYREF
))
890 hres
= E_INVALIDARG
; /* Don't dereference more than one level */
892 hres
= VariantCopyInd(pvargDest
, V_VARIANTREF(pSrc
));
894 /* Use the dereferenced variants type value, not VT_VARIANT */
895 goto VariantCopyInd_Return
;
897 else if (V_VT(pSrc
) == (VT_DECIMAL
|VT_BYREF
))
899 memcpy(&DEC_SCALE(&V_DECIMAL(pvargDest
)), &DEC_SCALE(V_DECIMALREF(pSrc
)),
900 sizeof(DECIMAL
) - sizeof(USHORT
));
904 /* Copy the pointed to data into this variant */
905 memcpy(&V_BYREF(pvargDest
), V_BYREF(pSrc
), VARIANT_DataSize(pSrc
));
908 V_VT(pvargDest
) = V_VT(pSrc
) & ~VT_BYREF
;
910 VariantCopyInd_Return
:
912 if (pSrc
!= pvargSrc
)
915 TRACE("returning 0x%08lx, %p->(%s%s)\n", hres
, pvargDest
,
916 debugstr_VT(pvargDest
), debugstr_VF(pvargDest
));
920 /******************************************************************************
921 * VariantChangeType [OLEAUT32.12]
923 * Change the type of a variant.
926 * pvargDest [O] Destination for the converted variant
927 * pvargSrc [O] Source variant to change the type of
928 * wFlags [I] VARIANT_ flags from "oleauto.h"
929 * vt [I] Variant type to change pvargSrc into
932 * Success: S_OK. pvargDest contains the converted value.
933 * Failure: An HRESULT error code describing the failure.
936 * The LCID used for the conversion is LOCALE_USER_DEFAULT.
937 * See VariantChangeTypeEx.
939 HRESULT WINAPI
VariantChangeType(VARIANTARG
* pvargDest
, VARIANTARG
* pvargSrc
,
940 USHORT wFlags
, VARTYPE vt
)
942 return VariantChangeTypeEx( pvargDest
, pvargSrc
, LOCALE_USER_DEFAULT
, wFlags
, vt
);
945 /******************************************************************************
946 * VariantChangeTypeEx [OLEAUT32.147]
948 * Change the type of a variant.
951 * pvargDest [O] Destination for the converted variant
952 * pvargSrc [O] Source variant to change the type of
953 * lcid [I] LCID for the conversion
954 * wFlags [I] VARIANT_ flags from "oleauto.h"
955 * vt [I] Variant type to change pvargSrc into
958 * Success: S_OK. pvargDest contains the converted value.
959 * Failure: An HRESULT error code describing the failure.
962 * pvargDest and pvargSrc can point to the same variant to perform an in-place
963 * conversion. If the conversion is successful, pvargSrc will be freed.
965 HRESULT WINAPI
VariantChangeTypeEx(VARIANTARG
* pvargDest
, VARIANTARG
* pvargSrc
,
966 LCID lcid
, USHORT wFlags
, VARTYPE vt
)
970 TRACE("(%p->(%s%s),%p->(%s%s),0x%08lx,0x%04x,%s%s)\n", pvargDest
,
971 debugstr_VT(pvargDest
), debugstr_VF(pvargDest
), pvargSrc
,
972 debugstr_VT(pvargSrc
), debugstr_VF(pvargSrc
), lcid
, wFlags
,
973 debugstr_vt(vt
), debugstr_vf(vt
));
976 res
= DISP_E_BADVARTYPE
;
979 res
= VARIANT_ValidateType(V_VT(pvargSrc
));
983 res
= VARIANT_ValidateType(vt
);
987 VARIANTARG vTmp
, vSrcDeref
;
989 if(V_ISBYREF(pvargSrc
) && !V_BYREF(pvargSrc
))
990 res
= DISP_E_TYPEMISMATCH
;
993 V_VT(&vTmp
) = VT_EMPTY
;
994 V_VT(&vSrcDeref
) = VT_EMPTY
;
996 VariantClear(&vSrcDeref
);
1001 res
= VariantCopyInd(&vSrcDeref
, pvargSrc
);
1004 if (V_ISARRAY(&vSrcDeref
) || (vt
& VT_ARRAY
))
1005 res
= VARIANT_CoerceArray(&vTmp
, &vSrcDeref
, vt
);
1007 res
= VARIANT_Coerce(&vTmp
, lcid
, wFlags
, &vSrcDeref
, vt
);
1009 if (SUCCEEDED(res
)) {
1011 VariantCopy(pvargDest
, &vTmp
);
1013 VariantClear(&vTmp
);
1014 VariantClear(&vSrcDeref
);
1021 TRACE("returning 0x%08lx, %p->(%s%s)\n", res
, pvargDest
,
1022 debugstr_VT(pvargDest
), debugstr_VF(pvargDest
));
1026 /* Date Conversions */
1028 #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
1030 /* Convert a VT_DATE value to a Julian Date */
1031 static inline int VARIANT_JulianFromDate(int dateIn
)
1033 int julianDays
= dateIn
;
1035 julianDays
-= DATE_MIN
; /* Convert to + days from 1 Jan 100 AD */
1036 julianDays
+= 1757585; /* Convert to + days from 23 Nov 4713 BC (Julian) */
1040 /* Convert a Julian Date to a VT_DATE value */
1041 static inline int VARIANT_DateFromJulian(int dateIn
)
1043 int julianDays
= dateIn
;
1045 julianDays
-= 1757585; /* Convert to + days from 1 Jan 100 AD */
1046 julianDays
+= DATE_MIN
; /* Convert to +/- days from 1 Jan 1899 AD */
1050 /* Convert a Julian date to Day/Month/Year - from PostgreSQL */
1051 static inline void VARIANT_DMYFromJulian(int jd
, USHORT
*year
, USHORT
*month
, USHORT
*day
)
1057 l
-= (n
* 146097 + 3) / 4;
1058 i
= (4000 * (l
+ 1)) / 1461001;
1059 l
+= 31 - (i
* 1461) / 4;
1060 j
= (l
* 80) / 2447;
1061 *day
= l
- (j
* 2447) / 80;
1063 *month
= (j
+ 2) - (12 * l
);
1064 *year
= 100 * (n
- 49) + i
+ l
;
1067 /* Convert Day/Month/Year to a Julian date - from PostgreSQL */
1068 static inline double VARIANT_JulianFromDMY(USHORT year
, USHORT month
, USHORT day
)
1070 int m12
= (month
- 14) / 12;
1072 return ((1461 * (year
+ 4800 + m12
)) / 4 + (367 * (month
- 2 - 12 * m12
)) / 12 -
1073 (3 * ((year
+ 4900 + m12
) / 100)) / 4 + day
- 32075);
1076 /* Macros for accessing DOS format date/time fields */
1077 #define DOS_YEAR(x) (1980 + (x >> 9))
1078 #define DOS_MONTH(x) ((x >> 5) & 0xf)
1079 #define DOS_DAY(x) (x & 0x1f)
1080 #define DOS_HOUR(x) (x >> 11)
1081 #define DOS_MINUTE(x) ((x >> 5) & 0x3f)
1082 #define DOS_SECOND(x) ((x & 0x1f) << 1)
1083 /* Create a DOS format date/time */
1084 #define DOS_DATE(d,m,y) (d | (m << 5) | ((y-1980) << 9))
1085 #define DOS_TIME(h,m,s) ((s >> 1) | (m << 5) | (h << 11))
1087 /* Roll a date forwards or backwards to correct it */
1088 static HRESULT
VARIANT_RollUdate(UDATE
*lpUd
)
1090 static const BYTE days
[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1092 TRACE("Raw date: %d/%d/%d %d:%d:%d\n", lpUd
->st
.wDay
, lpUd
->st
.wMonth
,
1093 lpUd
->st
.wYear
, lpUd
->st
.wHour
, lpUd
->st
.wMinute
, lpUd
->st
.wSecond
);
1095 /* Years < 100 are treated as 1900 + year */
1096 if (lpUd
->st
.wYear
< 100)
1097 lpUd
->st
.wYear
+= 1900;
1099 if (!lpUd
->st
.wMonth
)
1101 /* Roll back to December of the previous year */
1102 lpUd
->st
.wMonth
= 12;
1105 else while (lpUd
->st
.wMonth
> 12)
1107 /* Roll forward the correct number of months */
1109 lpUd
->st
.wMonth
-= 12;
1112 if (lpUd
->st
.wYear
> 9999 || lpUd
->st
.wHour
> 23 ||
1113 lpUd
->st
.wMinute
> 59 || lpUd
->st
.wSecond
> 59)
1114 return E_INVALIDARG
; /* Invalid values */
1118 /* Roll back the date one day */
1119 if (lpUd
->st
.wMonth
== 1)
1121 /* Roll back to December 31 of the previous year */
1123 lpUd
->st
.wMonth
= 12;
1128 lpUd
->st
.wMonth
--; /* Previous month */
1129 if (lpUd
->st
.wMonth
== 2 && IsLeapYear(lpUd
->st
.wYear
))
1130 lpUd
->st
.wDay
= 29; /* Februaury has 29 days on leap years */
1132 lpUd
->st
.wDay
= days
[lpUd
->st
.wMonth
]; /* Last day of the month */
1135 else if (lpUd
->st
.wDay
> 28)
1137 int rollForward
= 0;
1139 /* Possibly need to roll the date forward */
1140 if (lpUd
->st
.wMonth
== 2 && IsLeapYear(lpUd
->st
.wYear
))
1141 rollForward
= lpUd
->st
.wDay
- 29; /* Februaury has 29 days on leap years */
1143 rollForward
= lpUd
->st
.wDay
- days
[lpUd
->st
.wMonth
];
1145 if (rollForward
> 0)
1147 lpUd
->st
.wDay
= rollForward
;
1149 if (lpUd
->st
.wMonth
> 12)
1151 lpUd
->st
.wMonth
= 1; /* Roll forward into January of the next year */
1156 TRACE("Rolled date: %d/%d/%d %d:%d:%d\n", lpUd
->st
.wDay
, lpUd
->st
.wMonth
,
1157 lpUd
->st
.wYear
, lpUd
->st
.wHour
, lpUd
->st
.wMinute
, lpUd
->st
.wSecond
);
1161 /**********************************************************************
1162 * DosDateTimeToVariantTime [OLEAUT32.14]
1164 * Convert a Dos format date and time into variant VT_DATE format.
1167 * wDosDate [I] Dos format date
1168 * wDosTime [I] Dos format time
1169 * pDateOut [O] Destination for VT_DATE format
1172 * Success: TRUE. pDateOut contains the converted time.
1173 * Failure: FALSE, if wDosDate or wDosTime are invalid (see notes).
1176 * - Dos format dates can only hold dates from 1-Jan-1980 to 31-Dec-2099.
1177 * - Dos format times are accurate to only 2 second precision.
1178 * - The format of a Dos Date is:
1179 *| Bits Values Meaning
1180 *| ---- ------ -------
1181 *| 0-4 1-31 Day of the week. 0 rolls back one day. A value greater than
1182 *| the days in the month rolls forward the extra days.
1183 *| 5-8 1-12 Month of the year. 0 rolls back to December of the previous
1184 *| year. 13-15 are invalid.
1185 *| 9-15 0-119 Year based from 1980 (Max 2099). 120-127 are invalid.
1186 * - The format of a Dos Time is:
1187 *| Bits Values Meaning
1188 *| ---- ------ -------
1189 *| 0-4 0-29 Seconds/2. 30 and 31 are invalid.
1190 *| 5-10 0-59 Minutes. 60-63 are invalid.
1191 *| 11-15 0-23 Hours (24 hour clock). 24-32 are invalid.
1193 INT WINAPI
DosDateTimeToVariantTime(USHORT wDosDate
, USHORT wDosTime
,
1198 TRACE("(0x%x(%d/%d/%d),0x%x(%d:%d:%d),%p)\n",
1199 wDosDate
, DOS_YEAR(wDosDate
), DOS_MONTH(wDosDate
), DOS_DAY(wDosDate
),
1200 wDosTime
, DOS_HOUR(wDosTime
), DOS_MINUTE(wDosTime
), DOS_SECOND(wDosTime
),
1203 ud
.st
.wYear
= DOS_YEAR(wDosDate
);
1204 ud
.st
.wMonth
= DOS_MONTH(wDosDate
);
1205 if (ud
.st
.wYear
> 2099 || ud
.st
.wMonth
> 12)
1207 ud
.st
.wDay
= DOS_DAY(wDosDate
);
1208 ud
.st
.wHour
= DOS_HOUR(wDosTime
);
1209 ud
.st
.wMinute
= DOS_MINUTE(wDosTime
);
1210 ud
.st
.wSecond
= DOS_SECOND(wDosTime
);
1211 ud
.st
.wDayOfWeek
= ud
.st
.wMilliseconds
= 0;
1213 return !VarDateFromUdate(&ud
, 0, pDateOut
);
1216 /**********************************************************************
1217 * VariantTimeToDosDateTime [OLEAUT32.13]
1219 * Convert a variant format date into a Dos format date and time.
1221 * dateIn [I] VT_DATE time format
1222 * pwDosDate [O] Destination for Dos format date
1223 * pwDosTime [O] Destination for Dos format time
1226 * Success: TRUE. pwDosDate and pwDosTime contains the converted values.
1227 * Failure: FALSE, if dateIn cannot be represented in Dos format.
1230 * See DosDateTimeToVariantTime() for Dos format details and bugs.
1232 INT WINAPI
VariantTimeToDosDateTime(double dateIn
, USHORT
*pwDosDate
, USHORT
*pwDosTime
)
1236 TRACE("(%g,%p,%p)\n", dateIn
, pwDosDate
, pwDosTime
);
1238 if (FAILED(VarUdateFromDate(dateIn
, 0, &ud
)))
1241 if (ud
.st
.wYear
< 1980 || ud
.st
.wYear
> 2099)
1244 *pwDosDate
= DOS_DATE(ud
.st
.wDay
, ud
.st
.wMonth
, ud
.st
.wYear
);
1245 *pwDosTime
= DOS_TIME(ud
.st
.wHour
, ud
.st
.wMinute
, ud
.st
.wSecond
);
1247 TRACE("Returning 0x%x(%d/%d/%d), 0x%x(%d:%d:%d)\n",
1248 *pwDosDate
, DOS_YEAR(*pwDosDate
), DOS_MONTH(*pwDosDate
), DOS_DAY(*pwDosDate
),
1249 *pwDosTime
, DOS_HOUR(*pwDosTime
), DOS_MINUTE(*pwDosTime
), DOS_SECOND(*pwDosTime
));
1253 /***********************************************************************
1254 * SystemTimeToVariantTime [OLEAUT32.184]
1256 * Convert a System format date and time into variant VT_DATE format.
1259 * lpSt [I] System format date and time
1260 * pDateOut [O] Destination for VT_DATE format date
1263 * Success: TRUE. *pDateOut contains the converted value.
1264 * Failure: FALSE, if lpSt cannot be represented in VT_DATE format.
1266 INT WINAPI
SystemTimeToVariantTime(LPSYSTEMTIME lpSt
, double *pDateOut
)
1270 TRACE("(%p->%d/%d/%d %d:%d:%d,%p)\n", lpSt
, lpSt
->wDay
, lpSt
->wMonth
,
1271 lpSt
->wYear
, lpSt
->wHour
, lpSt
->wMinute
, lpSt
->wSecond
, pDateOut
);
1273 if (lpSt
->wMonth
> 12)
1276 memcpy(&ud
.st
, lpSt
, sizeof(ud
.st
));
1277 return !VarDateFromUdate(&ud
, 0, pDateOut
);
1280 /***********************************************************************
1281 * VariantTimeToSystemTime [OLEAUT32.185]
1283 * Convert a variant VT_DATE into a System format date and time.
1286 * datein [I] Variant VT_DATE format date
1287 * lpSt [O] Destination for System format date and time
1290 * Success: TRUE. *lpSt contains the converted value.
1291 * Failure: FALSE, if dateIn is too large or small.
1293 INT WINAPI
VariantTimeToSystemTime(double dateIn
, LPSYSTEMTIME lpSt
)
1297 TRACE("(%g,%p)\n", dateIn
, lpSt
);
1299 if (FAILED(VarUdateFromDate(dateIn
, 0, &ud
)))
1302 memcpy(lpSt
, &ud
.st
, sizeof(ud
.st
));
1306 /***********************************************************************
1307 * VarDateFromUdateEx [OLEAUT32.319]
1309 * Convert an unpacked format date and time to a variant VT_DATE.
1312 * pUdateIn [I] Unpacked format date and time to convert
1313 * lcid [I] Locale identifier for the conversion
1314 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1315 * pDateOut [O] Destination for variant VT_DATE.
1318 * Success: S_OK. *pDateOut contains the converted value.
1319 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1321 HRESULT WINAPI
VarDateFromUdateEx(UDATE
*pUdateIn
, LCID lcid
, ULONG dwFlags
, DATE
*pDateOut
)
1326 TRACE("(%p->%d/%d/%d %d:%d:%d:%d %d %d,0x%08lx,0x%08lx,%p)\n", pUdateIn
,
1327 pUdateIn
->st
.wMonth
, pUdateIn
->st
.wDay
, pUdateIn
->st
.wYear
,
1328 pUdateIn
->st
.wHour
, pUdateIn
->st
.wMinute
, pUdateIn
->st
.wSecond
,
1329 pUdateIn
->st
.wMilliseconds
, pUdateIn
->st
.wDayOfWeek
,
1330 pUdateIn
->wDayOfYear
, lcid
, dwFlags
, pDateOut
);
1332 if (lcid
!= MAKELCID(MAKELANGID(LANG_ENGLISH
, SUBLANG_ENGLISH_US
), SORT_DEFAULT
))
1333 FIXME("lcid possibly not handled, treating as en-us\n");
1335 memcpy(&ud
, pUdateIn
, sizeof(ud
));
1337 if (dwFlags
& VAR_VALIDDATE
)
1338 WARN("Ignoring VAR_VALIDDATE\n");
1340 if (FAILED(VARIANT_RollUdate(&ud
)))
1341 return E_INVALIDARG
;
1344 dateVal
= VARIANT_DateFromJulian(VARIANT_JulianFromDMY(ud
.st
.wYear
, ud
.st
.wMonth
, ud
.st
.wDay
));
1347 dateVal
+= ud
.st
.wHour
/ 24.0;
1348 dateVal
+= ud
.st
.wMinute
/ 1440.0;
1349 dateVal
+= ud
.st
.wSecond
/ 86400.0;
1350 dateVal
+= ud
.st
.wMilliseconds
/ 86400000.0;
1352 TRACE("Returning %g\n", dateVal
);
1353 *pDateOut
= dateVal
;
1357 /***********************************************************************
1358 * VarDateFromUdate [OLEAUT32.330]
1360 * Convert an unpacked format date and time to a variant VT_DATE.
1363 * pUdateIn [I] Unpacked format date and time to convert
1364 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1365 * pDateOut [O] Destination for variant VT_DATE.
1368 * Success: S_OK. *pDateOut contains the converted value.
1369 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1372 * This function uses the United States English locale for the conversion. Use
1373 * VarDateFromUdateEx() for alternate locales.
1375 HRESULT WINAPI
VarDateFromUdate(UDATE
*pUdateIn
, ULONG dwFlags
, DATE
*pDateOut
)
1377 LCID lcid
= MAKELCID(MAKELANGID(LANG_ENGLISH
, SUBLANG_ENGLISH_US
), SORT_DEFAULT
);
1379 return VarDateFromUdateEx(pUdateIn
, lcid
, dwFlags
, pDateOut
);
1382 /***********************************************************************
1383 * VarUdateFromDate [OLEAUT32.331]
1385 * Convert a variant VT_DATE into an unpacked format date and time.
1388 * datein [I] Variant VT_DATE format date
1389 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1390 * lpUdate [O] Destination for unpacked format date and time
1393 * Success: S_OK. *lpUdate contains the converted value.
1394 * Failure: E_INVALIDARG, if dateIn is too large or small.
1396 HRESULT WINAPI
VarUdateFromDate(DATE dateIn
, ULONG dwFlags
, UDATE
*lpUdate
)
1398 /* Cumulative totals of days per month */
1399 static const USHORT cumulativeDays
[] =
1401 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
1403 double datePart
, timePart
;
1406 TRACE("(%g,0x%08lx,%p)\n", dateIn
, dwFlags
, lpUdate
);
1408 if (dateIn
<= (DATE_MIN
- 1.0) || dateIn
>= (DATE_MAX
+ 1.0))
1409 return E_INVALIDARG
;
1411 datePart
= dateIn
< 0.0 ? ceil(dateIn
) : floor(dateIn
);
1412 /* Compensate for int truncation (always downwards) */
1413 timePart
= dateIn
- datePart
+ 0.00000000001;
1414 if (timePart
>= 1.0)
1415 timePart
-= 0.00000000001;
1418 julianDays
= VARIANT_JulianFromDate(dateIn
);
1419 VARIANT_DMYFromJulian(julianDays
, &lpUdate
->st
.wYear
, &lpUdate
->st
.wMonth
,
1422 datePart
= (datePart
+ 1.5) / 7.0;
1423 lpUdate
->st
.wDayOfWeek
= (datePart
- floor(datePart
)) * 7;
1424 if (lpUdate
->st
.wDayOfWeek
== 0)
1425 lpUdate
->st
.wDayOfWeek
= 5;
1426 else if (lpUdate
->st
.wDayOfWeek
== 1)
1427 lpUdate
->st
.wDayOfWeek
= 6;
1429 lpUdate
->st
.wDayOfWeek
-= 2;
1431 if (lpUdate
->st
.wMonth
> 2 && IsLeapYear(lpUdate
->st
.wYear
))
1432 lpUdate
->wDayOfYear
= 1; /* After February, in a leap year */
1434 lpUdate
->wDayOfYear
= 0;
1436 lpUdate
->wDayOfYear
+= cumulativeDays
[lpUdate
->st
.wMonth
];
1437 lpUdate
->wDayOfYear
+= lpUdate
->st
.wDay
;
1441 lpUdate
->st
.wHour
= timePart
;
1442 timePart
-= lpUdate
->st
.wHour
;
1444 lpUdate
->st
.wMinute
= timePart
;
1445 timePart
-= lpUdate
->st
.wMinute
;
1447 lpUdate
->st
.wSecond
= timePart
;
1448 timePart
-= lpUdate
->st
.wSecond
;
1449 lpUdate
->st
.wMilliseconds
= 0;
1452 /* Round the milliseconds, adjusting the time/date forward if needed */
1453 if (lpUdate
->st
.wSecond
< 59)
1454 lpUdate
->st
.wSecond
++;
1457 lpUdate
->st
.wSecond
= 0;
1458 if (lpUdate
->st
.wMinute
< 59)
1459 lpUdate
->st
.wMinute
++;
1462 lpUdate
->st
.wMinute
= 0;
1463 if (lpUdate
->st
.wHour
< 23)
1464 lpUdate
->st
.wHour
++;
1467 lpUdate
->st
.wHour
= 0;
1468 /* Roll over a whole day */
1469 if (++lpUdate
->st
.wDay
> 28)
1470 VARIANT_RollUdate(lpUdate
);
1478 #define GET_NUMBER_TEXT(fld,name) \
1480 if (!GetLocaleInfoW(lcid, lctype|fld, buff, 2)) \
1481 WARN("buffer too small for " #fld "\n"); \
1483 if (buff[0]) lpChars->name = buff[0]; \
1484 TRACE("lcid 0x%lx, " #name "=%d '%c'\n", lcid, lpChars->name, lpChars->name)
1486 /* Get the valid number characters for an lcid */
1487 void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS
*lpChars
, LCID lcid
, DWORD dwFlags
)
1489 static const VARIANT_NUMBER_CHARS defaultChars
= { '-','+','.',',','$',0,'.',',' };
1490 LCTYPE lctype
= dwFlags
& LOCALE_NOUSEROVERRIDE
;
1493 memcpy(lpChars
, &defaultChars
, sizeof(defaultChars
));
1494 GET_NUMBER_TEXT(LOCALE_SNEGATIVESIGN
, cNegativeSymbol
);
1495 GET_NUMBER_TEXT(LOCALE_SPOSITIVESIGN
, cPositiveSymbol
);
1496 GET_NUMBER_TEXT(LOCALE_SDECIMAL
, cDecimalPoint
);
1497 GET_NUMBER_TEXT(LOCALE_STHOUSAND
, cDigitSeperator
);
1498 GET_NUMBER_TEXT(LOCALE_SMONDECIMALSEP
, cCurrencyDecimalPoint
);
1499 GET_NUMBER_TEXT(LOCALE_SMONTHOUSANDSEP
, cCurrencyDigitSeperator
);
1501 /* Local currency symbols are often 2 characters */
1502 lpChars
->cCurrencyLocal2
= '\0';
1503 switch(GetLocaleInfoW(lcid
, lctype
|LOCALE_SCURRENCY
, buff
, sizeof(buff
)/sizeof(WCHAR
)))
1505 case 3: lpChars
->cCurrencyLocal2
= buff
[1]; /* Fall through */
1506 case 2: lpChars
->cCurrencyLocal
= buff
[0];
1508 default: WARN("buffer too small for LOCALE_SCURRENCY\n");
1510 TRACE("lcid 0x%lx, cCurrencyLocal =%d,%d '%c','%c'\n", lcid
, lpChars
->cCurrencyLocal
,
1511 lpChars
->cCurrencyLocal2
, lpChars
->cCurrencyLocal
, lpChars
->cCurrencyLocal2
);
1514 /* Number Parsing States */
1515 #define B_PROCESSING_EXPONENT 0x1
1516 #define B_NEGATIVE_EXPONENT 0x2
1517 #define B_EXPONENT_START 0x4
1518 #define B_INEXACT_ZEROS 0x8
1519 #define B_LEADING_ZERO 0x10
1520 #define B_PROCESSING_HEX 0x20
1521 #define B_PROCESSING_OCT 0x40
1523 /**********************************************************************
1524 * VarParseNumFromStr [OLEAUT32.46]
1526 * Parse a string containing a number into a NUMPARSE structure.
1529 * lpszStr [I] String to parse number from
1530 * lcid [I] Locale Id for the conversion
1531 * dwFlags [I] 0, or LOCALE_NOUSEROVERRIDE to use system default number chars
1532 * pNumprs [I/O] Destination for parsed number
1533 * rgbDig [O] Destination for digits read in
1536 * Success: S_OK. pNumprs and rgbDig contain the parsed representation of
1538 * Failure: E_INVALIDARG, if any parameter is invalid.
1539 * DISP_E_TYPEMISMATCH, if the string is not a number or is formatted
1541 * DISP_E_OVERFLOW, if rgbDig is too small to hold the number.
1544 * pNumprs must have the following fields set:
1545 * cDig: Set to the size of rgbDig.
1546 * dwInFlags: Set to the allowable syntax of the number using NUMPRS_ flags
1550 * - I am unsure if this function should parse non-arabic (e.g. Thai)
1551 * numerals, so this has not been implemented.
1553 HRESULT WINAPI
VarParseNumFromStr(OLECHAR
*lpszStr
, LCID lcid
, ULONG dwFlags
,
1554 NUMPARSE
*pNumprs
, BYTE
*rgbDig
)
1556 VARIANT_NUMBER_CHARS chars
;
1558 DWORD dwState
= B_EXPONENT_START
|B_INEXACT_ZEROS
;
1559 int iMaxDigits
= sizeof(rgbTmp
) / sizeof(BYTE
);
1562 TRACE("(%s,%ld,0x%08lx,%p,%p)\n", debugstr_w(lpszStr
), lcid
, dwFlags
, pNumprs
, rgbDig
);
1564 if (!pNumprs
|| !rgbDig
)
1565 return E_INVALIDARG
;
1567 if (pNumprs
->cDig
< iMaxDigits
)
1568 iMaxDigits
= pNumprs
->cDig
;
1571 pNumprs
->dwOutFlags
= 0;
1572 pNumprs
->cchUsed
= 0;
1573 pNumprs
->nBaseShift
= 0;
1574 pNumprs
->nPwr10
= 0;
1577 return DISP_E_TYPEMISMATCH
;
1579 VARIANT_GetLocalisedNumberChars(&chars
, lcid
, dwFlags
);
1581 /* First consume all the leading symbols and space from the string */
1584 if (pNumprs
->dwInFlags
& NUMPRS_LEADING_WHITE
&& isspaceW(*lpszStr
))
1586 pNumprs
->dwOutFlags
|= NUMPRS_LEADING_WHITE
;
1591 } while (isspaceW(*lpszStr
));
1593 else if (pNumprs
->dwInFlags
& NUMPRS_LEADING_PLUS
&&
1594 *lpszStr
== chars
.cPositiveSymbol
&&
1595 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_PLUS
))
1597 pNumprs
->dwOutFlags
|= NUMPRS_LEADING_PLUS
;
1601 else if (pNumprs
->dwInFlags
& NUMPRS_LEADING_MINUS
&&
1602 *lpszStr
== chars
.cNegativeSymbol
&&
1603 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_MINUS
))
1605 pNumprs
->dwOutFlags
|= (NUMPRS_LEADING_MINUS
|NUMPRS_NEG
);
1609 else if (pNumprs
->dwInFlags
& NUMPRS_CURRENCY
&&
1610 !(pNumprs
->dwOutFlags
& NUMPRS_CURRENCY
) &&
1611 *lpszStr
== chars
.cCurrencyLocal
&&
1612 (!chars
.cCurrencyLocal2
|| lpszStr
[1] == chars
.cCurrencyLocal2
))
1614 pNumprs
->dwOutFlags
|= NUMPRS_CURRENCY
;
1617 /* Only accept currency characters */
1618 chars
.cDecimalPoint
= chars
.cCurrencyDecimalPoint
;
1619 chars
.cDigitSeperator
= chars
.cCurrencyDigitSeperator
;
1621 else if (pNumprs
->dwInFlags
& NUMPRS_PARENS
&& *lpszStr
== '(' &&
1622 !(pNumprs
->dwOutFlags
& NUMPRS_PARENS
))
1624 pNumprs
->dwOutFlags
|= NUMPRS_PARENS
;
1632 if (!(pNumprs
->dwOutFlags
& NUMPRS_CURRENCY
))
1634 /* Only accept non-currency characters */
1635 chars
.cCurrencyDecimalPoint
= chars
.cDecimalPoint
;
1636 chars
.cCurrencyDigitSeperator
= chars
.cDigitSeperator
;
1639 if ((*lpszStr
== '&' && (*(lpszStr
+1) == 'H' || *(lpszStr
+1) == 'h')) &&
1640 pNumprs
->dwInFlags
& NUMPRS_HEX_OCT
)
1642 dwState
|= B_PROCESSING_HEX
;
1643 pNumprs
->dwOutFlags
|= NUMPRS_HEX_OCT
;
1647 else if ((*lpszStr
== '&' && (*(lpszStr
+1) == 'O' || *(lpszStr
+1) == 'o')) &&
1648 pNumprs
->dwInFlags
& NUMPRS_HEX_OCT
)
1650 dwState
|= B_PROCESSING_OCT
;
1651 pNumprs
->dwOutFlags
|= NUMPRS_HEX_OCT
;
1656 /* Strip Leading zeros */
1657 while (*lpszStr
== '0')
1659 dwState
|= B_LEADING_ZERO
;
1666 if (isdigitW(*lpszStr
))
1668 if (dwState
& B_PROCESSING_EXPONENT
)
1670 int exponentSize
= 0;
1671 if (dwState
& B_EXPONENT_START
)
1673 if (!isdigitW(*lpszStr
))
1674 break; /* No exponent digits - invalid */
1675 while (*lpszStr
== '0')
1677 /* Skip leading zero's in the exponent */
1683 while (isdigitW(*lpszStr
))
1686 exponentSize
+= *lpszStr
- '0';
1690 if (dwState
& B_NEGATIVE_EXPONENT
)
1691 exponentSize
= -exponentSize
;
1692 /* Add the exponent into the powers of 10 */
1693 pNumprs
->nPwr10
+= exponentSize
;
1694 dwState
&= ~(B_PROCESSING_EXPONENT
|B_EXPONENT_START
);
1695 lpszStr
--; /* back up to allow processing of next char */
1699 if ((pNumprs
->cDig
>= iMaxDigits
) && !(dwState
& B_PROCESSING_HEX
)
1700 && !(dwState
& B_PROCESSING_OCT
))
1702 pNumprs
->dwOutFlags
|= NUMPRS_INEXACT
;
1704 if (*lpszStr
!= '0')
1705 dwState
&= ~B_INEXACT_ZEROS
; /* Inexact number with non-trailing zeros */
1707 /* This digit can't be represented, but count it in nPwr10 */
1708 if (pNumprs
->dwOutFlags
& NUMPRS_DECIMAL
)
1715 if ((dwState
& B_PROCESSING_OCT
) && ((*lpszStr
== '8') || (*lpszStr
== '9'))) {
1716 return DISP_E_TYPEMISMATCH
;
1719 if (pNumprs
->dwOutFlags
& NUMPRS_DECIMAL
)
1720 pNumprs
->nPwr10
--; /* Count decimal points in nPwr10 */
1722 rgbTmp
[pNumprs
->cDig
] = *lpszStr
- '0';
1728 else if (*lpszStr
== chars
.cDigitSeperator
&& pNumprs
->dwInFlags
& NUMPRS_THOUSANDS
)
1730 pNumprs
->dwOutFlags
|= NUMPRS_THOUSANDS
;
1733 else if (*lpszStr
== chars
.cDecimalPoint
&&
1734 pNumprs
->dwInFlags
& NUMPRS_DECIMAL
&&
1735 !(pNumprs
->dwOutFlags
& (NUMPRS_DECIMAL
|NUMPRS_EXPONENT
)))
1737 pNumprs
->dwOutFlags
|= NUMPRS_DECIMAL
;
1740 /* If we have no digits so far, skip leading zeros */
1743 while (lpszStr
[1] == '0')
1745 dwState
|= B_LEADING_ZERO
;
1752 else if ((*lpszStr
== 'e' || *lpszStr
== 'E') &&
1753 pNumprs
->dwInFlags
& NUMPRS_EXPONENT
&&
1754 !(pNumprs
->dwOutFlags
& NUMPRS_EXPONENT
))
1756 dwState
|= B_PROCESSING_EXPONENT
;
1757 pNumprs
->dwOutFlags
|= NUMPRS_EXPONENT
;
1760 else if (dwState
& B_PROCESSING_EXPONENT
&& *lpszStr
== chars
.cPositiveSymbol
)
1762 cchUsed
++; /* Ignore positive exponent */
1764 else if (dwState
& B_PROCESSING_EXPONENT
&& *lpszStr
== chars
.cNegativeSymbol
)
1766 dwState
|= B_NEGATIVE_EXPONENT
;
1769 else if (((*lpszStr
>= 'a' && *lpszStr
<= 'f') ||
1770 (*lpszStr
>= 'A' && *lpszStr
<= 'F')) &&
1771 dwState
& B_PROCESSING_HEX
)
1773 if (pNumprs
->cDig
>= iMaxDigits
)
1775 return DISP_E_OVERFLOW
;
1779 if (*lpszStr
>= 'a')
1780 rgbTmp
[pNumprs
->cDig
] = *lpszStr
- 'a' + 10;
1782 rgbTmp
[pNumprs
->cDig
] = *lpszStr
- 'A' + 10;
1788 break; /* Stop at an unrecognised character */
1793 if (!pNumprs
->cDig
&& dwState
& B_LEADING_ZERO
)
1795 /* Ensure a 0 on its own gets stored */
1800 if (pNumprs
->dwOutFlags
& NUMPRS_EXPONENT
&& dwState
& B_PROCESSING_EXPONENT
)
1802 pNumprs
->cchUsed
= cchUsed
;
1803 return DISP_E_TYPEMISMATCH
; /* Failed to completely parse the exponent */
1806 if (pNumprs
->dwOutFlags
& NUMPRS_INEXACT
)
1808 if (dwState
& B_INEXACT_ZEROS
)
1809 pNumprs
->dwOutFlags
&= ~NUMPRS_INEXACT
; /* All zeros doesn't set NUMPRS_INEXACT */
1810 } else if(pNumprs
->dwInFlags
& NUMPRS_HEX_OCT
)
1812 /* copy all of the digits into the output digit buffer */
1813 /* this is exactly what windows does although it also returns */
1814 /* cDig of X and writes X+Y where Y>=0 number of digits to rgbDig */
1815 memcpy(rgbDig
, rgbTmp
, pNumprs
->cDig
* sizeof(BYTE
));
1817 if (dwState
& B_PROCESSING_HEX
) {
1818 /* hex numbers have always the same format */
1820 pNumprs
->nBaseShift
=4;
1822 if (dwState
& B_PROCESSING_OCT
) {
1823 /* oct numbers have always the same format */
1825 pNumprs
->nBaseShift
=3;
1827 while (pNumprs
->cDig
> 1 && !rgbTmp
[pNumprs
->cDig
- 1])
1836 /* Remove trailing zeros from the last (whole number or decimal) part */
1837 while (pNumprs
->cDig
> 1 && !rgbTmp
[pNumprs
->cDig
- 1])
1844 if (pNumprs
->cDig
<= iMaxDigits
)
1845 pNumprs
->dwOutFlags
&= ~NUMPRS_INEXACT
; /* Ignore stripped zeros for NUMPRS_INEXACT */
1847 pNumprs
->cDig
= iMaxDigits
; /* Only return iMaxDigits worth of digits */
1849 /* Copy the digits we processed into rgbDig */
1850 memcpy(rgbDig
, rgbTmp
, pNumprs
->cDig
* sizeof(BYTE
));
1852 /* Consume any trailing symbols and space */
1855 if ((pNumprs
->dwInFlags
& NUMPRS_TRAILING_WHITE
) && isspaceW(*lpszStr
))
1857 pNumprs
->dwOutFlags
|= NUMPRS_TRAILING_WHITE
;
1862 } while (isspaceW(*lpszStr
));
1864 else if (pNumprs
->dwInFlags
& NUMPRS_TRAILING_PLUS
&&
1865 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_PLUS
) &&
1866 *lpszStr
== chars
.cPositiveSymbol
)
1868 pNumprs
->dwOutFlags
|= NUMPRS_TRAILING_PLUS
;
1872 else if (pNumprs
->dwInFlags
& NUMPRS_TRAILING_MINUS
&&
1873 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_MINUS
) &&
1874 *lpszStr
== chars
.cNegativeSymbol
)
1876 pNumprs
->dwOutFlags
|= (NUMPRS_TRAILING_MINUS
|NUMPRS_NEG
);
1880 else if (pNumprs
->dwInFlags
& NUMPRS_PARENS
&& *lpszStr
== ')' &&
1881 pNumprs
->dwOutFlags
& NUMPRS_PARENS
)
1885 pNumprs
->dwOutFlags
|= NUMPRS_NEG
;
1891 if (pNumprs
->dwOutFlags
& NUMPRS_PARENS
&& !(pNumprs
->dwOutFlags
& NUMPRS_NEG
))
1893 pNumprs
->cchUsed
= cchUsed
;
1894 return DISP_E_TYPEMISMATCH
; /* Opening parenthesis not matched */
1897 if (pNumprs
->dwInFlags
& NUMPRS_USE_ALL
&& *lpszStr
!= '\0')
1898 return DISP_E_TYPEMISMATCH
; /* Not all chars were consumed */
1901 return DISP_E_TYPEMISMATCH
; /* No Number found */
1903 pNumprs
->cchUsed
= cchUsed
;
1907 /* VTBIT flags indicating an integer value */
1908 #define INTEGER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8)
1909 /* VTBIT flags indicating a real number value */
1910 #define REAL_VTBITS (VTBIT_R4|VTBIT_R8|VTBIT_CY)
1912 /* Helper macros to check whether bit pattern fits in VARIANT (x is a ULONG64 ) */
1913 #define FITS_AS_I1(x) ((x) >> 8 == 0)
1914 #define FITS_AS_I2(x) ((x) >> 16 == 0)
1915 #define FITS_AS_I4(x) ((x) >> 32 == 0)
1917 /**********************************************************************
1918 * VarNumFromParseNum [OLEAUT32.47]
1920 * Convert a NUMPARSE structure into a numeric Variant type.
1923 * pNumprs [I] Source for parsed number. cDig must be set to the size of rgbDig
1924 * rgbDig [I] Source for the numbers digits
1925 * dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
1926 * pVarDst [O] Destination for the converted Variant value.
1929 * Success: S_OK. pVarDst contains the converted value.
1930 * Failure: E_INVALIDARG, if any parameter is invalid.
1931 * DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
1934 * - The smallest favoured type present in dwVtBits that can represent the
1935 * number in pNumprs without losing precision is used.
1936 * - Signed types are preferrred over unsigned types of the same size.
1937 * - Preferred types in order are: integer, float, double, currency then decimal.
1938 * - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
1939 * for details of the rounding method.
1940 * - pVarDst is not cleared before the result is stored in it.
1942 HRESULT WINAPI
VarNumFromParseNum(NUMPARSE
*pNumprs
, BYTE
*rgbDig
,
1943 ULONG dwVtBits
, VARIANT
*pVarDst
)
1945 /* Scale factors and limits for double arithmetic */
1946 static const double dblMultipliers
[11] = {
1947 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
1948 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
1950 static const double dblMinimums
[11] = {
1951 R8_MIN
, R8_MIN
*10.0, R8_MIN
*100.0, R8_MIN
*1000.0, R8_MIN
*10000.0,
1952 R8_MIN
*100000.0, R8_MIN
*1000000.0, R8_MIN
*10000000.0,
1953 R8_MIN
*100000000.0, R8_MIN
*1000000000.0, R8_MIN
*10000000000.0
1955 static const double dblMaximums
[11] = {
1956 R8_MAX
, R8_MAX
/10.0, R8_MAX
/100.0, R8_MAX
/1000.0, R8_MAX
/10000.0,
1957 R8_MAX
/100000.0, R8_MAX
/1000000.0, R8_MAX
/10000000.0,
1958 R8_MAX
/100000000.0, R8_MAX
/1000000000.0, R8_MAX
/10000000000.0
1961 int wholeNumberDigits
, fractionalDigits
, divisor10
= 0, multiplier10
= 0;
1963 TRACE("(%p,%p,0x%lx,%p)\n", pNumprs
, rgbDig
, dwVtBits
, pVarDst
);
1965 if (pNumprs
->nBaseShift
)
1967 /* nBaseShift indicates a hex or octal number */
1972 /* Convert the hex or octal number string into a UI64 */
1973 for (i
= 0; i
< pNumprs
->cDig
; i
++)
1975 if (ul64
> ((UI8_MAX
>>pNumprs
->nBaseShift
) - rgbDig
[i
]))
1977 TRACE("Overflow multiplying digits\n");
1978 return DISP_E_OVERFLOW
;
1980 ul64
= (ul64
<<pNumprs
->nBaseShift
) + rgbDig
[i
];
1983 /* also make a negative representation */
1986 /* Try signed and unsigned types in size order */
1987 if (dwVtBits
& VTBIT_I1
&& FITS_AS_I1(ul64
))
1989 V_VT(pVarDst
) = VT_I1
;
1990 V_I1(pVarDst
) = ul64
;
1993 else if (dwVtBits
& VTBIT_UI1
&& FITS_AS_I1(ul64
))
1995 V_VT(pVarDst
) = VT_UI1
;
1996 V_UI1(pVarDst
) = ul64
;
1999 else if (dwVtBits
& VTBIT_I2
&& FITS_AS_I2(ul64
))
2001 V_VT(pVarDst
) = VT_I2
;
2002 V_I2(pVarDst
) = ul64
;
2005 else if (dwVtBits
& VTBIT_UI2
&& FITS_AS_I2(ul64
))
2007 V_VT(pVarDst
) = VT_UI2
;
2008 V_UI2(pVarDst
) = ul64
;
2011 else if (dwVtBits
& VTBIT_I4
&& FITS_AS_I4(ul64
))
2013 V_VT(pVarDst
) = VT_I4
;
2014 V_I4(pVarDst
) = ul64
;
2017 else if (dwVtBits
& VTBIT_UI4
&& FITS_AS_I4(ul64
))
2019 V_VT(pVarDst
) = VT_UI4
;
2020 V_UI4(pVarDst
) = ul64
;
2023 else if (dwVtBits
& VTBIT_I8
&& ((ul64
<= I8_MAX
)||(l64
>=I8_MIN
)))
2025 V_VT(pVarDst
) = VT_I8
;
2026 V_I8(pVarDst
) = ul64
;
2029 else if (dwVtBits
& VTBIT_UI8
)
2031 V_VT(pVarDst
) = VT_UI8
;
2032 V_UI8(pVarDst
) = ul64
;
2035 else if ((dwVtBits
& REAL_VTBITS
) == VTBIT_DECIMAL
)
2037 V_VT(pVarDst
) = VT_DECIMAL
;
2038 DEC_SIGNSCALE(&V_DECIMAL(pVarDst
)) = SIGNSCALE(DECIMAL_POS
,0);
2039 DEC_HI32(&V_DECIMAL(pVarDst
)) = 0;
2040 DEC_LO64(&V_DECIMAL(pVarDst
)) = ul64
;
2043 else if (dwVtBits
& VTBIT_R4
&& ((ul64
<= I4_MAX
)||(l64
>= I4_MIN
)))
2045 V_VT(pVarDst
) = VT_R4
;
2047 V_R4(pVarDst
) = ul64
;
2049 V_R4(pVarDst
) = l64
;
2052 else if (dwVtBits
& VTBIT_R8
&& ((ul64
<= I4_MAX
)||(l64
>= I4_MIN
)))
2054 V_VT(pVarDst
) = VT_R8
;
2056 V_R8(pVarDst
) = ul64
;
2058 V_R8(pVarDst
) = l64
;
2062 TRACE("Overflow: possible return types: 0x%lx, value: %s\n", dwVtBits
, wine_dbgstr_longlong(ul64
));
2063 return DISP_E_OVERFLOW
;
2066 /* Count the number of relevant fractional and whole digits stored,
2067 * And compute the divisor/multiplier to scale the number by.
2069 if (pNumprs
->nPwr10
< 0)
2071 if (-pNumprs
->nPwr10
>= pNumprs
->cDig
)
2073 /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
2074 wholeNumberDigits
= 0;
2075 fractionalDigits
= pNumprs
->cDig
;
2076 divisor10
= -pNumprs
->nPwr10
;
2080 /* An exactly represented real number e.g. 1.024 */
2081 wholeNumberDigits
= pNumprs
->cDig
+ pNumprs
->nPwr10
;
2082 fractionalDigits
= pNumprs
->cDig
- wholeNumberDigits
;
2083 divisor10
= pNumprs
->cDig
- wholeNumberDigits
;
2086 else if (pNumprs
->nPwr10
== 0)
2088 /* An exactly represented whole number e.g. 1024 */
2089 wholeNumberDigits
= pNumprs
->cDig
;
2090 fractionalDigits
= 0;
2092 else /* pNumprs->nPwr10 > 0 */
2094 /* A whole number followed by nPwr10 0's e.g. 102400 */
2095 wholeNumberDigits
= pNumprs
->cDig
;
2096 fractionalDigits
= 0;
2097 multiplier10
= pNumprs
->nPwr10
;
2100 TRACE("cDig %d; nPwr10 %d, whole %d, frac %d ", pNumprs
->cDig
,
2101 pNumprs
->nPwr10
, wholeNumberDigits
, fractionalDigits
);
2102 TRACE("mult %d; div %d\n", multiplier10
, divisor10
);
2104 if (dwVtBits
& (INTEGER_VTBITS
|VTBIT_DECIMAL
) &&
2105 (!fractionalDigits
|| !(dwVtBits
& (REAL_VTBITS
|VTBIT_CY
|VTBIT_DECIMAL
))))
2107 /* We have one or more integer output choices, and either:
2108 * 1) An integer input value, or
2109 * 2) A real number input value but no floating output choices.
2110 * Alternately, we have a DECIMAL output available and an integer input.
2112 * So, place the integer value into pVarDst, using the smallest type
2113 * possible and preferring signed over unsigned types.
2115 BOOL bOverflow
= FALSE
, bNegative
;
2119 /* Convert the integer part of the number into a UI8 */
2120 for (i
= 0; i
< wholeNumberDigits
; i
++)
2122 if (ul64
> (UI8_MAX
/ 10 - rgbDig
[i
]))
2124 TRACE("Overflow multiplying digits\n");
2128 ul64
= ul64
* 10 + rgbDig
[i
];
2131 /* Account for the scale of the number */
2132 if (!bOverflow
&& multiplier10
)
2134 for (i
= 0; i
< multiplier10
; i
++)
2136 if (ul64
> (UI8_MAX
/ 10))
2138 TRACE("Overflow scaling number\n");
2146 /* If we have any fractional digits, round the value.
2147 * Note we don't have to do this if divisor10 is < 1,
2148 * because this means the fractional part must be < 0.5
2150 if (!bOverflow
&& fractionalDigits
&& divisor10
> 0)
2152 const BYTE
* fracDig
= rgbDig
+ wholeNumberDigits
;
2153 BOOL bAdjust
= FALSE
;
2155 TRACE("first decimal value is %d\n", *fracDig
);
2158 bAdjust
= TRUE
; /* > 0.5 */
2159 else if (*fracDig
== 5)
2161 for (i
= 1; i
< fractionalDigits
; i
++)
2165 bAdjust
= TRUE
; /* > 0.5 */
2169 /* If exactly 0.5, round only odd values */
2170 if (i
== fractionalDigits
&& (ul64
& 1))
2176 if (ul64
== UI8_MAX
)
2178 TRACE("Overflow after rounding\n");
2185 /* Zero is not a negative number */
2186 bNegative
= pNumprs
->dwOutFlags
& NUMPRS_NEG
&& ul64
? TRUE
: FALSE
;
2188 TRACE("Integer value is %lld, bNeg %d\n", ul64
, bNegative
);
2190 /* For negative integers, try the signed types in size order */
2191 if (!bOverflow
&& bNegative
)
2193 if (dwVtBits
& (VTBIT_I1
|VTBIT_I2
|VTBIT_I4
|VTBIT_I8
))
2195 if (dwVtBits
& VTBIT_I1
&& ul64
<= -I1_MIN
)
2197 V_VT(pVarDst
) = VT_I1
;
2198 V_I1(pVarDst
) = -ul64
;
2201 else if (dwVtBits
& VTBIT_I2
&& ul64
<= -I2_MIN
)
2203 V_VT(pVarDst
) = VT_I2
;
2204 V_I2(pVarDst
) = -ul64
;
2207 else if (dwVtBits
& VTBIT_I4
&& ul64
<= -((LONGLONG
)I4_MIN
))
2209 V_VT(pVarDst
) = VT_I4
;
2210 V_I4(pVarDst
) = -ul64
;
2213 else if (dwVtBits
& VTBIT_I8
&& ul64
<= (ULONGLONG
)I8_MAX
+ 1)
2215 V_VT(pVarDst
) = VT_I8
;
2216 V_I8(pVarDst
) = -ul64
;
2219 else if ((dwVtBits
& REAL_VTBITS
) == VTBIT_DECIMAL
)
2221 /* Decimal is only output choice left - fast path */
2222 V_VT(pVarDst
) = VT_DECIMAL
;
2223 DEC_SIGNSCALE(&V_DECIMAL(pVarDst
)) = SIGNSCALE(DECIMAL_NEG
,0);
2224 DEC_HI32(&V_DECIMAL(pVarDst
)) = 0;
2225 DEC_LO64(&V_DECIMAL(pVarDst
)) = -ul64
;
2230 else if (!bOverflow
)
2232 /* For positive integers, try signed then unsigned types in size order */
2233 if (dwVtBits
& VTBIT_I1
&& ul64
<= I1_MAX
)
2235 V_VT(pVarDst
) = VT_I1
;
2236 V_I1(pVarDst
) = ul64
;
2239 else if (dwVtBits
& VTBIT_UI1
&& ul64
<= UI1_MAX
)
2241 V_VT(pVarDst
) = VT_UI1
;
2242 V_UI1(pVarDst
) = ul64
;
2245 else if (dwVtBits
& VTBIT_I2
&& ul64
<= I2_MAX
)
2247 V_VT(pVarDst
) = VT_I2
;
2248 V_I2(pVarDst
) = ul64
;
2251 else if (dwVtBits
& VTBIT_UI2
&& ul64
<= UI2_MAX
)
2253 V_VT(pVarDst
) = VT_UI2
;
2254 V_UI2(pVarDst
) = ul64
;
2257 else if (dwVtBits
& VTBIT_I4
&& ul64
<= I4_MAX
)
2259 V_VT(pVarDst
) = VT_I4
;
2260 V_I4(pVarDst
) = ul64
;
2263 else if (dwVtBits
& VTBIT_UI4
&& ul64
<= UI4_MAX
)
2265 V_VT(pVarDst
) = VT_UI4
;
2266 V_UI4(pVarDst
) = ul64
;
2269 else if (dwVtBits
& VTBIT_I8
&& ul64
<= I8_MAX
)
2271 V_VT(pVarDst
) = VT_I8
;
2272 V_I8(pVarDst
) = ul64
;
2275 else if (dwVtBits
& VTBIT_UI8
)
2277 V_VT(pVarDst
) = VT_UI8
;
2278 V_UI8(pVarDst
) = ul64
;
2281 else if ((dwVtBits
& REAL_VTBITS
) == VTBIT_DECIMAL
)
2283 /* Decimal is only output choice left - fast path */
2284 V_VT(pVarDst
) = VT_DECIMAL
;
2285 DEC_SIGNSCALE(&V_DECIMAL(pVarDst
)) = SIGNSCALE(DECIMAL_POS
,0);
2286 DEC_HI32(&V_DECIMAL(pVarDst
)) = 0;
2287 DEC_LO64(&V_DECIMAL(pVarDst
)) = ul64
;
2293 if (dwVtBits
& REAL_VTBITS
)
2295 /* Try to put the number into a float or real */
2296 BOOL bOverflow
= FALSE
, bNegative
= pNumprs
->dwOutFlags
& NUMPRS_NEG
;
2300 /* Convert the number into a double */
2301 for (i
= 0; i
< pNumprs
->cDig
; i
++)
2302 whole
= whole
* 10.0 + rgbDig
[i
];
2304 TRACE("Whole double value is %16.16g\n", whole
);
2306 /* Account for the scale */
2307 while (multiplier10
> 10)
2309 if (whole
> dblMaximums
[10])
2311 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
);
2315 whole
= whole
* dblMultipliers
[10];
2320 if (whole
> dblMaximums
[multiplier10
])
2322 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
);
2326 whole
= whole
* dblMultipliers
[multiplier10
];
2329 TRACE("Scaled double value is %16.16g\n", whole
);
2331 while (divisor10
> 10)
2333 if (whole
< dblMinimums
[10] && whole
!= 0)
2335 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
); /* Underflow */
2339 whole
= whole
/ dblMultipliers
[10];
2344 if (whole
< dblMinimums
[divisor10
] && whole
!= 0)
2346 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
); /* Underflow */
2350 whole
= whole
/ dblMultipliers
[divisor10
];
2353 TRACE("Final double value is %16.16g\n", whole
);
2355 if (dwVtBits
& VTBIT_R4
&&
2356 ((whole
<= R4_MAX
&& whole
>= R4_MIN
) || whole
== 0.0))
2358 TRACE("Set R4 to final value\n");
2359 V_VT(pVarDst
) = VT_R4
; /* Fits into a float */
2360 V_R4(pVarDst
) = pNumprs
->dwOutFlags
& NUMPRS_NEG
? -whole
: whole
;
2364 if (dwVtBits
& VTBIT_R8
)
2366 TRACE("Set R8 to final value\n");
2367 V_VT(pVarDst
) = VT_R8
; /* Fits into a double */
2368 V_R8(pVarDst
) = pNumprs
->dwOutFlags
& NUMPRS_NEG
? -whole
: whole
;
2372 if (dwVtBits
& VTBIT_CY
)
2374 if (SUCCEEDED(VarCyFromR8(bNegative
? -whole
: whole
, &V_CY(pVarDst
))))
2376 V_VT(pVarDst
) = VT_CY
; /* Fits into a currency */
2377 TRACE("Set CY to final value\n");
2380 TRACE("Value Overflows CY\n");
2384 if (dwVtBits
& VTBIT_DECIMAL
)
2389 DECIMAL
* pDec
= &V_DECIMAL(pVarDst
);
2391 DECIMAL_SETZERO(*pDec
);
2394 if (pNumprs
->dwOutFlags
& NUMPRS_NEG
)
2395 DEC_SIGN(pDec
) = DECIMAL_NEG
;
2397 DEC_SIGN(pDec
) = DECIMAL_POS
;
2399 /* Factor the significant digits */
2400 for (i
= 0; i
< pNumprs
->cDig
; i
++)
2402 tmp
= (ULONG64
)DEC_LO32(pDec
) * 10 + rgbDig
[i
];
2403 carry
= (ULONG
)(tmp
>> 32);
2404 DEC_LO32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2405 tmp
= (ULONG64
)DEC_MID32(pDec
) * 10 + carry
;
2406 carry
= (ULONG
)(tmp
>> 32);
2407 DEC_MID32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2408 tmp
= (ULONG64
)DEC_HI32(pDec
) * 10 + carry
;
2409 DEC_HI32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2411 if (tmp
>> 32 & UI4_MAX
)
2413 VarNumFromParseNum_DecOverflow
:
2414 TRACE("Overflow\n");
2415 DEC_LO32(pDec
) = DEC_MID32(pDec
) = DEC_HI32(pDec
) = UI4_MAX
;
2416 return DISP_E_OVERFLOW
;
2420 /* Account for the scale of the number */
2421 while (multiplier10
> 0)
2423 tmp
= (ULONG64
)DEC_LO32(pDec
) * 10;
2424 carry
= (ULONG
)(tmp
>> 32);
2425 DEC_LO32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2426 tmp
= (ULONG64
)DEC_MID32(pDec
) * 10 + carry
;
2427 carry
= (ULONG
)(tmp
>> 32);
2428 DEC_MID32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2429 tmp
= (ULONG64
)DEC_HI32(pDec
) * 10 + carry
;
2430 DEC_HI32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2432 if (tmp
>> 32 & UI4_MAX
)
2433 goto VarNumFromParseNum_DecOverflow
;
2436 DEC_SCALE(pDec
) = divisor10
;
2438 V_VT(pVarDst
) = VT_DECIMAL
;
2441 return DISP_E_OVERFLOW
; /* No more output choices */
2444 /**********************************************************************
2445 * VarCat [OLEAUT32.318]
2447 HRESULT WINAPI
VarCat(LPVARIANT left
, LPVARIANT right
, LPVARIANT out
)
2449 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
),
2450 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
), out
);
2452 /* Should we VariantClear out? */
2453 /* Can we handle array, vector, by ref etc. */
2454 if ((V_VT(left
)&VT_TYPEMASK
) == VT_NULL
&&
2455 (V_VT(right
)&VT_TYPEMASK
) == VT_NULL
)
2457 V_VT(out
) = VT_NULL
;
2461 if (V_VT(left
) == VT_BSTR
&& V_VT(right
) == VT_BSTR
)
2463 V_VT(out
) = VT_BSTR
;
2464 VarBstrCat (V_BSTR(left
), V_BSTR(right
), &V_BSTR(out
));
2467 if (V_VT(left
) == VT_BSTR
) {
2471 V_VT(out
) = VT_BSTR
;
2472 VariantInit(&bstrvar
);
2473 hres
= VariantChangeTypeEx(&bstrvar
,right
,0,0,VT_BSTR
);
2475 FIXME("Failed to convert right side from vt %d to VT_BSTR?\n",V_VT(right
));
2478 VarBstrCat (V_BSTR(left
), V_BSTR(&bstrvar
), &V_BSTR(out
));
2481 if (V_VT(right
) == VT_BSTR
) {
2485 V_VT(out
) = VT_BSTR
;
2486 VariantInit(&bstrvar
);
2487 hres
= VariantChangeTypeEx(&bstrvar
,left
,0,0,VT_BSTR
);
2489 FIXME("Failed to convert right side from vt %d to VT_BSTR?\n",V_VT(right
));
2492 VarBstrCat (V_BSTR(&bstrvar
), V_BSTR(right
), &V_BSTR(out
));
2495 FIXME ("types %d / %d not supported\n",V_VT(left
)&VT_TYPEMASK
, V_VT(right
)&VT_TYPEMASK
);
2499 /**********************************************************************
2500 * VarCmp [OLEAUT32.176]
2503 * NORM_IGNORECASE, NORM_IGNORENONSPACE, NORM_IGNORESYMBOLS
2504 * NORM_IGNOREWIDTH, NORM_IGNOREKANATYPE, NORM_IGNOREKASHIDA
2507 HRESULT WINAPI
VarCmp(LPVARIANT left
, LPVARIANT right
, LCID lcid
, DWORD flags
)
2517 TRACE("(%p->(%s%s),%p->(%s%s),0x%08lx,0x%08lx)\n", left
, debugstr_VT(left
),
2518 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
), lcid
, flags
);
2520 VariantInit(&lv
);VariantInit(&rv
);
2521 V_VT(right
) &= ~0x8000; /* hack since we sometime get this flag. */
2522 V_VT(left
) &= ~0x8000; /* hack since we sometime get this flag. */
2524 /* If either are null, then return VARCMP_NULL */
2525 if ((V_VT(left
)&VT_TYPEMASK
) == VT_NULL
||
2526 (V_VT(right
)&VT_TYPEMASK
) == VT_NULL
)
2529 /* Strings - use VarBstrCmp */
2530 if ((V_VT(left
)&VT_TYPEMASK
) == VT_BSTR
&&
2531 (V_VT(right
)&VT_TYPEMASK
) == VT_BSTR
) {
2532 return VarBstrCmp(V_BSTR(left
), V_BSTR(right
), lcid
, flags
);
2535 xmask
= (1<<(V_VT(left
)&VT_TYPEMASK
))|(1<<(V_VT(right
)&VT_TYPEMASK
));
2536 if (xmask
& VTBIT_R8
) {
2537 rc
= VariantChangeType(&lv
,left
,0,VT_R8
);
2538 if (FAILED(rc
)) return rc
;
2539 rc
= VariantChangeType(&rv
,right
,0,VT_R8
);
2540 if (FAILED(rc
)) return rc
;
2542 if (V_R8(&lv
) == V_R8(&rv
)) return VARCMP_EQ
;
2543 if (V_R8(&lv
) < V_R8(&rv
)) return VARCMP_LT
;
2544 if (V_R8(&lv
) > V_R8(&rv
)) return VARCMP_GT
;
2545 return E_FAIL
; /* can't get here */
2547 if (xmask
& VTBIT_R4
) {
2548 rc
= VariantChangeType(&lv
,left
,0,VT_R4
);
2549 if (FAILED(rc
)) return rc
;
2550 rc
= VariantChangeType(&rv
,right
,0,VT_R4
);
2551 if (FAILED(rc
)) return rc
;
2553 if (V_R4(&lv
) == V_R4(&rv
)) return VARCMP_EQ
;
2554 if (V_R4(&lv
) < V_R4(&rv
)) return VARCMP_LT
;
2555 if (V_R4(&lv
) > V_R4(&rv
)) return VARCMP_GT
;
2556 return E_FAIL
; /* can't get here */
2559 /* Integers - Ideally like to use VarDecCmp, but no Dec support yet
2560 Use LONGLONG to maximize ranges */
2562 switch (V_VT(left
)&VT_TYPEMASK
) {
2563 case VT_I1
: lVal
= V_I1(left
); break;
2564 case VT_I2
: lVal
= V_I2(left
); break;
2566 case VT_INT
: lVal
= V_I4(left
); break;
2567 case VT_UI1
: lVal
= V_UI1(left
); break;
2568 case VT_UI2
: lVal
= V_UI2(left
); break;
2570 case VT_UINT
: lVal
= V_UI4(left
); break;
2571 case VT_BOOL
: lVal
= V_BOOL(left
); break;
2572 default: lOk
= FALSE
;
2576 switch (V_VT(right
)&VT_TYPEMASK
) {
2577 case VT_I1
: rVal
= V_I1(right
); break;
2578 case VT_I2
: rVal
= V_I2(right
); break;
2580 case VT_INT
: rVal
= V_I4(right
); break;
2581 case VT_UI1
: rVal
= V_UI1(right
); break;
2582 case VT_UI2
: rVal
= V_UI2(right
); break;
2584 case VT_UINT
: rVal
= V_UI4(right
); break;
2585 case VT_BOOL
: rVal
= V_BOOL(right
); break;
2586 default: rOk
= FALSE
;
2592 } else if (lVal
> rVal
) {
2599 /* Strings - use VarBstrCmp */
2600 if ((V_VT(left
)&VT_TYPEMASK
) == VT_DATE
&&
2601 (V_VT(right
)&VT_TYPEMASK
) == VT_DATE
) {
2603 if (floor(V_DATE(left
)) == floor(V_DATE(right
))) {
2604 /* Due to floating point rounding errors, calculate varDate in whole numbers) */
2605 double wholePart
= 0.0;
2609 /* Get the fraction * 24*60*60 to make it into whole seconds */
2610 wholePart
= (double) floor( V_DATE(left
) );
2611 if (wholePart
== 0) wholePart
= 1;
2612 leftR
= floor(fmod( V_DATE(left
), wholePart
) * (24*60*60));
2614 wholePart
= (double) floor( V_DATE(right
) );
2615 if (wholePart
== 0) wholePart
= 1;
2616 rightR
= floor(fmod( V_DATE(right
), wholePart
) * (24*60*60));
2618 if (leftR
< rightR
) {
2620 } else if (leftR
> rightR
) {
2626 } else if (V_DATE(left
) < V_DATE(right
)) {
2628 } else if (V_DATE(left
) > V_DATE(right
)) {
2632 FIXME("VarCmp partial implementation, doesn't support vt 0x%x / 0x%x\n",V_VT(left
), V_VT(right
));
2636 /**********************************************************************
2637 * VarAnd [OLEAUT32.142]
2640 HRESULT WINAPI
VarAnd(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
2642 HRESULT rc
= E_FAIL
;
2644 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
),
2645 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
), result
);
2647 if ((V_VT(left
)&VT_TYPEMASK
) == VT_BOOL
&&
2648 (V_VT(right
)&VT_TYPEMASK
) == VT_BOOL
) {
2650 V_VT(result
) = VT_BOOL
;
2651 if (V_BOOL(left
) && V_BOOL(right
)) {
2652 V_BOOL(result
) = VARIANT_TRUE
;
2654 V_BOOL(result
) = VARIANT_FALSE
;
2665 int resT
= 0; /* Testing has shown I2 & I2 == I2, all else
2666 becomes I4, even unsigned ints (incl. UI2) */
2669 switch (V_VT(left
)&VT_TYPEMASK
) {
2670 case VT_I1
: lVal
= V_I1(left
); resT
=VT_I4
; break;
2671 case VT_I2
: lVal
= V_I2(left
); resT
=VT_I2
; break;
2673 case VT_INT
: lVal
= V_I4(left
); resT
=VT_I4
; break;
2674 case VT_UI1
: lVal
= V_UI1(left
); resT
=VT_I4
; break;
2675 case VT_UI2
: lVal
= V_UI2(left
); resT
=VT_I4
; break;
2677 case VT_UINT
: lVal
= V_UI4(left
); resT
=VT_I4
; break;
2678 case VT_BOOL
: rVal
= V_BOOL(left
); resT
=VT_I4
; break;
2679 default: lOk
= FALSE
;
2683 switch (V_VT(right
)&VT_TYPEMASK
) {
2684 case VT_I1
: rVal
= V_I1(right
); resT
=VT_I4
; break;
2685 case VT_I2
: rVal
= V_I2(right
); resT
=max(VT_I2
, resT
); break;
2687 case VT_INT
: rVal
= V_I4(right
); resT
=VT_I4
; break;
2688 case VT_UI1
: rVal
= V_UI1(right
); resT
=VT_I4
; break;
2689 case VT_UI2
: rVal
= V_UI2(right
); resT
=VT_I4
; break;
2691 case VT_UINT
: rVal
= V_UI4(right
); resT
=VT_I4
; break;
2692 case VT_BOOL
: rVal
= V_BOOL(right
); resT
=VT_I4
; break;
2693 default: rOk
= FALSE
;
2697 res
= (lVal
& rVal
);
2698 V_VT(result
) = resT
;
2700 case VT_I2
: V_I2(result
) = res
; break;
2701 case VT_I4
: V_I4(result
) = res
; break;
2703 FIXME("Unexpected result variant type %x\n", resT
);
2709 FIXME("VarAnd stub\n");
2713 TRACE("returning 0x%8lx (%s%s),%ld\n", rc
, debugstr_VT(result
),
2714 debugstr_VF(result
), V_VT(result
) == VT_I4
? V_I4(result
) : V_I2(result
));
2718 /**********************************************************************
2719 * VarAdd [OLEAUT32.141]
2720 * FIXME: From MSDN: If ... Then
2721 * Both expressions are of the string type Concatenated.
2722 * One expression is a string type and the other a character Addition.
2723 * One expression is numeric and the other is a string Addition.
2724 * Both expressions are numeric Addition.
2725 * Either expression is NULL NULL is returned.
2726 * Both expressions are empty Integer subtype is returned.
2729 HRESULT WINAPI
VarAdd(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
2731 HRESULT rc
= E_FAIL
;
2733 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
),
2734 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
), result
);
2736 if ((V_VT(left
)&VT_TYPEMASK
) == VT_EMPTY
)
2737 return VariantCopy(result
,right
);
2739 if ((V_VT(right
)&VT_TYPEMASK
) == VT_EMPTY
)
2740 return VariantCopy(result
,left
);
2742 /* check if we add doubles */
2743 if (((V_VT(left
)&VT_TYPEMASK
) == VT_R8
) || ((V_VT(right
)&VT_TYPEMASK
) == VT_R8
)) {
2751 switch (V_VT(left
)&VT_TYPEMASK
) {
2752 case VT_I1
: lVal
= V_I1(left
); break;
2753 case VT_I2
: lVal
= V_I2(left
); break;
2755 case VT_INT
: lVal
= V_I4(left
); break;
2756 case VT_UI1
: lVal
= V_UI1(left
); break;
2757 case VT_UI2
: lVal
= V_UI2(left
); break;
2759 case VT_UINT
: lVal
= V_UI4(left
); break;
2760 case VT_R4
: lVal
= V_R4(left
); break;
2761 case VT_R8
: lVal
= V_R8(left
); break;
2762 case VT_NULL
: lVal
= 0.0; break;
2763 default: lOk
= FALSE
;
2767 switch (V_VT(right
)&VT_TYPEMASK
) {
2768 case VT_I1
: rVal
= V_I1(right
); break;
2769 case VT_I2
: rVal
= V_I2(right
); break;
2771 case VT_INT
: rVal
= V_I4(right
); break;
2772 case VT_UI1
: rVal
= V_UI1(right
); break;
2773 case VT_UI2
: rVal
= V_UI2(right
); break;
2775 case VT_UINT
: rVal
= V_UI4(right
); break;
2776 case VT_R4
: rVal
= V_R4(right
);break;
2777 case VT_R8
: rVal
= V_R8(right
);break;
2778 case VT_NULL
: rVal
= 0.0; break;
2779 default: rOk
= FALSE
;
2783 res
= (lVal
+ rVal
);
2784 V_VT(result
) = VT_R8
;
2788 FIXME("Unhandled type pair %d / %d in double addition.\n",
2789 (V_VT(left
)&VT_TYPEMASK
),
2790 (V_VT(right
)&VT_TYPEMASK
)
2796 /* now check if we add floats. VT_R8 can no longer happen here! */
2797 if (((V_VT(left
)&VT_TYPEMASK
) == VT_R4
) || ((V_VT(right
)&VT_TYPEMASK
) == VT_R4
)) {
2805 switch (V_VT(left
)&VT_TYPEMASK
) {
2806 case VT_I1
: lVal
= V_I1(left
); break;
2807 case VT_I2
: lVal
= V_I2(left
); break;
2809 case VT_INT
: lVal
= V_I4(left
); break;
2810 case VT_UI1
: lVal
= V_UI1(left
); break;
2811 case VT_UI2
: lVal
= V_UI2(left
); break;
2813 case VT_UINT
: lVal
= V_UI4(left
); break;
2814 case VT_R4
: lVal
= V_R4(left
); break;
2815 case VT_NULL
: lVal
= 0.0; break;
2816 default: lOk
= FALSE
;
2820 switch (V_VT(right
)&VT_TYPEMASK
) {
2821 case VT_I1
: rVal
= V_I1(right
); break;
2822 case VT_I2
: rVal
= V_I2(right
); break;
2824 case VT_INT
: rVal
= V_I4(right
); break;
2825 case VT_UI1
: rVal
= V_UI1(right
); break;
2826 case VT_UI2
: rVal
= V_UI2(right
); break;
2828 case VT_UINT
: rVal
= V_UI4(right
); break;
2829 case VT_R4
: rVal
= V_R4(right
);break;
2830 case VT_NULL
: rVal
= 0.0; break;
2831 default: rOk
= FALSE
;
2835 res
= (lVal
+ rVal
);
2836 V_VT(result
) = VT_R4
;
2840 FIXME("Unhandled type pair %d / %d in float addition.\n",
2841 (V_VT(left
)&VT_TYPEMASK
),
2842 (V_VT(right
)&VT_TYPEMASK
)
2848 /* Handle strings as concat */
2849 if ((V_VT(left
)&VT_TYPEMASK
) == VT_BSTR
&&
2850 (V_VT(right
)&VT_TYPEMASK
) == VT_BSTR
) {
2851 V_VT(result
) = VT_BSTR
;
2852 return VarBstrCat(V_BSTR(left
), V_BSTR(right
), &V_BSTR(result
));
2861 int resT
= 0; /* Testing has shown I2 + I2 == I2, all else
2865 switch (V_VT(left
)&VT_TYPEMASK
) {
2866 case VT_I1
: lVal
= V_I1(left
); resT
=VT_I4
; break;
2867 case VT_I2
: lVal
= V_I2(left
); resT
=VT_I2
; break;
2869 case VT_INT
: lVal
= V_I4(left
); resT
=VT_I4
; break;
2870 case VT_UI1
: lVal
= V_UI1(left
); resT
=VT_I4
; break;
2871 case VT_UI2
: lVal
= V_UI2(left
); resT
=VT_I4
; break;
2873 case VT_UINT
: lVal
= V_UI4(left
); resT
=VT_I4
; break;
2874 case VT_NULL
: lVal
= 0; resT
= VT_I4
; break;
2875 default: lOk
= FALSE
;
2879 switch (V_VT(right
)&VT_TYPEMASK
) {
2880 case VT_I1
: rVal
= V_I1(right
); resT
=VT_I4
; break;
2881 case VT_I2
: rVal
= V_I2(right
); resT
=max(VT_I2
, resT
); break;
2883 case VT_INT
: rVal
= V_I4(right
); resT
=VT_I4
; break;
2884 case VT_UI1
: rVal
= V_UI1(right
); resT
=VT_I4
; break;
2885 case VT_UI2
: rVal
= V_UI2(right
); resT
=VT_I4
; break;
2887 case VT_UINT
: rVal
= V_UI4(right
); resT
=VT_I4
; break;
2888 case VT_NULL
: rVal
= 0; resT
=VT_I4
; break;
2889 default: rOk
= FALSE
;
2893 res
= (lVal
+ rVal
);
2894 V_VT(result
) = resT
;
2896 case VT_I2
: V_I2(result
) = res
; break;
2897 case VT_I4
: V_I4(result
) = res
; break;
2899 FIXME("Unexpected result variant type %x\n", resT
);
2905 FIXME("unimplemented part (0x%x + 0x%x)\n",V_VT(left
), V_VT(right
));
2909 TRACE("returning 0x%8lx (%s%s),%ld\n", rc
, debugstr_VT(result
),
2910 debugstr_VF(result
), V_VT(result
) == VT_I4
? V_I4(result
) : V_I2(result
));
2914 /**********************************************************************
2915 * VarMul [OLEAUT32.156]
2917 * Multiply two variants.
2920 * left [I] First variant
2921 * right [I] Second variant
2922 * result [O] Result variant
2926 * Failure: An HRESULT error code indicating the error.
2929 * Native VarMul up to and including WinXP dosn't like as input variants
2930 * I1, UI2, UI4, UI8, INT and UINT. But it can multiply apples with oranges.
2932 * Native VarMul dosn't check for NULL in/out pointers and crashes. We do the
2936 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
2939 HRESULT WINAPI
VarMul(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
2942 VARTYPE lvt
, rvt
, resvt
, tvt
;
2946 /* Variant priority for coercion. Sorted from lowest to highest.
2947 VT_ERROR shows an invalid input variant type. */
2948 enum coerceprio
{ vt_UI1
= 0, vt_I2
, vt_I4
, vt_I8
, vt_CY
, vt_R4
, vt_R8
,
2949 vt_DECIMAL
, vt_NULL
, vt_ERROR
};
2950 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
2951 VARTYPE prio2vt
[] = { VT_UI1
, VT_I2
, VT_I4
, VT_I8
, VT_CY
, VT_R4
, VT_R8
,
2952 VT_DECIMAL
, VT_NULL
, VT_ERROR
};
2954 /* Mapping for coercion from input variant to priority of result variant. */
2955 static VARTYPE coerce
[] = {
2956 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
2957 vt_UI1
, vt_NULL
, vt_I2
, vt_I4
, vt_R4
,
2958 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
2959 vt_R8
, vt_CY
, vt_R8
, vt_R8
, vt_ERROR
,
2960 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
2961 vt_ERROR
, vt_I2
, vt_ERROR
, vt_ERROR
, vt_DECIMAL
,
2962 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
2963 vt_ERROR
, vt_ERROR
, vt_UI1
, vt_ERROR
, vt_ERROR
, vt_I8
2966 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
),
2967 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
),
2973 lvt
= V_VT(left
)&VT_TYPEMASK
;
2974 rvt
= V_VT(right
)&VT_TYPEMASK
;
2976 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
2977 Same for any input variant type > VT_I8 */
2978 if (V_VT(left
) & ~VT_TYPEMASK
|| V_VT(right
) & ~VT_TYPEMASK
||
2979 lvt
> VT_I8
|| rvt
> VT_I8
) {
2980 hres
= DISP_E_BADVARTYPE
;
2984 /* Determine the variant type to coerce to. */
2985 if (coerce
[lvt
] > coerce
[rvt
]) {
2986 resvt
= prio2vt
[coerce
[lvt
]];
2987 tvt
= prio2vt
[coerce
[rvt
]];
2989 resvt
= prio2vt
[coerce
[rvt
]];
2990 tvt
= prio2vt
[coerce
[lvt
]];
2993 /* Special cases where the result variant type is defined by both
2994 input variants and not only that with the highest priority */
2995 if (resvt
== VT_R4
&& (tvt
== VT_CY
|| tvt
== VT_I8
|| tvt
== VT_I4
))
2997 if (lvt
== VT_EMPTY
&& rvt
== VT_EMPTY
)
3000 /* For overflow detection use the biggest compatible type for the
3004 hres
= DISP_E_BADVARTYPE
;
3008 V_VT(result
) = VT_NULL
;
3023 /* Now coerce the variants */
3024 hres
= VariantChangeType(&lv
, left
, 0, tvt
);
3027 hres
= VariantChangeType(&rv
, right
, 0, tvt
);
3034 V_VT(result
) = resvt
;
3037 hres
= VarDecMul(&V_DECIMAL(&lv
), &V_DECIMAL(&rv
),
3038 &V_DECIMAL(result
));
3041 hres
= VarCyMul(V_CY(&lv
), V_CY(&rv
), &V_CY(result
));
3044 /* Overflow detection */
3045 r8res
= (double)V_I8(&lv
) * (double)V_I8(&rv
);
3046 if (r8res
> (double)I8_MAX
|| r8res
< (double)I8_MIN
) {
3047 V_VT(result
) = VT_R8
;
3048 V_R8(result
) = r8res
;
3051 V_I8(&tv
) = V_I8(&lv
) * V_I8(&rv
);
3054 /* FIXME: overflow detection */
3055 V_R8(&tv
) = V_R8(&lv
) * V_R8(&rv
);
3058 ERR("We shouldn't get here! tvt = %d!\n", tvt
);
3062 while ((hres
= VariantChangeType(result
, &tv
, 0, resvt
)) != S_OK
) {
3063 /* Overflow! Change to the vartype with the next higher priority */
3064 resvt
= prio2vt
[coerce
[resvt
] + 1];
3067 hres
= VariantCopy(result
, &tv
);
3071 V_VT(result
) = VT_EMPTY
;
3072 V_I4(result
) = 0; /* No V_EMPTY */
3077 TRACE("returning 0x%8lx (variant type %s)\n", hres
, debugstr_VT(result
));
3081 /**********************************************************************
3082 * VarDiv [OLEAUT32.143]
3085 HRESULT WINAPI
VarDiv(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3087 HRESULT rc
= E_FAIL
;
3088 VARTYPE lvt
,rvt
,resvt
;
3092 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
),
3093 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
), result
);
3095 VariantInit(&lv
);VariantInit(&rv
);
3096 lvt
= V_VT(left
)&VT_TYPEMASK
;
3097 rvt
= V_VT(right
)&VT_TYPEMASK
;
3098 found
= FALSE
;resvt
= VT_VOID
;
3099 if (((1<<lvt
) | (1<<rvt
)) & (VTBIT_R4
|VTBIT_R8
)) {
3103 if (!found
&& (((1<<lvt
) | (1<<rvt
)) & (VTBIT_I1
|VTBIT_I2
|VTBIT_UI1
|VTBIT_UI2
|VTBIT_I4
|VTBIT_UI4
|(1<<VT_INT
)|(1<<VT_UINT
)))) {
3108 FIXME("can't expand vt %d vs %d to a target type.\n",lvt
,rvt
);
3111 rc
= VariantChangeType(&lv
, left
, 0, resvt
);
3113 FIXME("Could not convert 0x%x to %d?\n",V_VT(left
),resvt
);
3116 rc
= VariantChangeType(&rv
, right
, 0, resvt
);
3118 FIXME("Could not convert 0x%x to %d?\n",V_VT(right
),resvt
);
3123 if (V_R8(&rv
) == 0) return DISP_E_DIVBYZERO
;
3124 V_VT(result
) = resvt
;
3125 V_R8(result
) = V_R8(&lv
) / V_R8(&rv
);
3129 if (V_I4(&rv
) == 0) return DISP_E_DIVBYZERO
;
3130 V_VT(result
) = resvt
;
3131 V_I4(result
) = V_I4(&lv
) / V_I4(&rv
);
3135 TRACE("returning 0x%8lx (%s%s),%g\n", rc
, debugstr_VT(result
),
3136 debugstr_VF(result
), V_VT(result
) == VT_R8
? V_R8(result
) : (double)V_I4(result
));
3140 /**********************************************************************
3141 * VarSub [OLEAUT32.159]
3144 HRESULT WINAPI
VarSub(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3146 HRESULT rc
= E_FAIL
;
3147 VARTYPE lvt
,rvt
,resvt
;
3151 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
),
3152 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
), result
);
3154 VariantInit(&lv
);VariantInit(&rv
);
3155 lvt
= V_VT(left
)&VT_TYPEMASK
;
3156 rvt
= V_VT(right
)&VT_TYPEMASK
;
3157 found
= FALSE
;resvt
= VT_VOID
;
3158 if (((1<<lvt
) | (1<<rvt
)) & ((1<<VT_DATE
)|(1<<VT_R4
)|(1<<VT_R8
))) {
3162 if (!found
&& (((1<<lvt
) | (1<<rvt
)) & (VTBIT_I1
|VTBIT_I2
|VTBIT_UI1
|VTBIT_UI2
|VTBIT_I4
|VTBIT_UI4
|(1<<VT_INT
)|(1<<VT_UINT
)))) {
3167 FIXME("can't expand vt %d vs %d to a target type.\n",lvt
,rvt
);
3170 rc
= VariantChangeType(&lv
, left
, 0, resvt
);
3172 FIXME("Could not convert 0x%x to %d?\n",V_VT(left
),resvt
);
3175 rc
= VariantChangeType(&rv
, right
, 0, resvt
);
3177 FIXME("Could not convert 0x%x to %d?\n",V_VT(right
),resvt
);
3182 V_VT(result
) = resvt
;
3183 V_R8(result
) = V_R8(&lv
) - V_R8(&rv
);
3187 V_VT(result
) = resvt
;
3188 V_I4(result
) = V_I4(&lv
) - V_I4(&rv
);
3192 TRACE("returning 0x%8lx (%s%s),%g\n", rc
, debugstr_VT(result
),
3193 debugstr_VF(result
), V_VT(result
) == VT_R8
? V_R8(result
) : (double)V_I4(result
));
3197 /**********************************************************************
3198 * VarOr [OLEAUT32.157]
3200 * Perform a logical or (OR) operation on two variants.
3203 * pVarLeft [I] First variant
3204 * pVarRight [I] Variant to OR with pVarLeft
3205 * pVarOut [O] Destination for OR result
3208 * Success: S_OK. pVarOut contains the result of the operation with its type
3209 * taken from the table listed under VarXor().
3210 * Failure: An HRESULT error code indicating the error.
3213 * See the Notes section of VarXor() for further information.
3215 HRESULT WINAPI
VarOr(LPVARIANT pVarLeft
, LPVARIANT pVarRight
, LPVARIANT pVarOut
)
3218 VARIANT varLeft
, varRight
, varStr
;
3221 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft
, debugstr_VT(pVarLeft
),
3222 debugstr_VF(pVarLeft
), pVarRight
, debugstr_VT(pVarRight
),
3223 debugstr_VF(pVarRight
), pVarOut
);
3225 if (V_EXTRA_TYPE(pVarLeft
) || V_EXTRA_TYPE(pVarRight
) ||
3226 V_VT(pVarLeft
) == VT_UNKNOWN
|| V_VT(pVarRight
) == VT_UNKNOWN
||
3227 V_VT(pVarLeft
) == VT_DISPATCH
|| V_VT(pVarRight
) == VT_DISPATCH
||
3228 V_VT(pVarLeft
) == VT_RECORD
|| V_VT(pVarRight
) == VT_RECORD
)
3229 return DISP_E_BADVARTYPE
;
3231 V_VT(&varLeft
) = V_VT(&varRight
) = V_VT(&varStr
) = VT_EMPTY
;
3233 if (V_VT(pVarLeft
) == VT_NULL
|| V_VT(pVarRight
) == VT_NULL
)
3235 /* NULL OR Zero is NULL, NULL OR value is value */
3236 if (V_VT(pVarLeft
) == VT_NULL
)
3237 pVarLeft
= pVarRight
; /* point to the non-NULL var */
3239 V_VT(pVarOut
) = VT_NULL
;
3242 switch (V_VT(pVarLeft
))
3244 case VT_DATE
: case VT_R8
:
3249 if (V_BOOL(pVarLeft
))
3250 *pVarOut
= *pVarLeft
;
3252 case VT_I2
: case VT_UI2
:
3261 if (V_UI1(pVarLeft
))
3262 *pVarOut
= *pVarLeft
;
3268 case VT_I4
: case VT_UI4
: case VT_INT
: case VT_UINT
:
3273 if (V_CY(pVarLeft
).int64
)
3276 case VT_I8
: case VT_UI8
:
3281 if (DEC_HI32(&V_DECIMAL(pVarLeft
)) || DEC_LO64(&V_DECIMAL(pVarLeft
)))
3288 if (!V_BSTR(pVarLeft
))
3289 return DISP_E_BADVARTYPE
;
3291 hRet
= VarBoolFromStr(V_BSTR(pVarLeft
), LOCALE_USER_DEFAULT
, VAR_LOCALBOOL
, &b
);
3292 if (SUCCEEDED(hRet
) && b
)
3294 V_VT(pVarOut
) = VT_BOOL
;
3295 V_BOOL(pVarOut
) = b
;
3299 case VT_NULL
: case VT_EMPTY
:
3300 V_VT(pVarOut
) = VT_NULL
;
3303 return DISP_E_BADVARTYPE
;
3307 if (V_VT(pVarLeft
) == VT_EMPTY
|| V_VT(pVarRight
) == VT_EMPTY
)
3309 if (V_VT(pVarLeft
) == VT_EMPTY
)
3310 pVarLeft
= pVarRight
; /* point to the non-EMPTY var */
3313 /* Since one argument is empty (0), OR'ing it with the other simply
3314 * gives the others value (as 0|x => x). So just convert the other
3315 * argument to the required result type.
3317 switch (V_VT(pVarLeft
))
3320 if (!V_BSTR(pVarLeft
))
3321 return DISP_E_BADVARTYPE
;
3323 hRet
= VariantCopy(&varStr
, pVarLeft
);
3327 hRet
= VariantChangeType(pVarLeft
, pVarLeft
, 0, VT_BOOL
);
3330 /* Fall Through ... */
3331 case VT_EMPTY
: case VT_UI1
: case VT_BOOL
: case VT_I2
:
3332 V_VT(pVarOut
) = VT_I2
;
3334 case VT_DATE
: case VT_CY
: case VT_DECIMAL
: case VT_R4
: case VT_R8
:
3335 case VT_I1
: case VT_UI2
: case VT_I4
: case VT_UI4
:
3336 case VT_INT
: case VT_UINT
: case VT_UI8
:
3337 V_VT(pVarOut
) = VT_I4
;
3340 V_VT(pVarOut
) = VT_I8
;
3343 return DISP_E_BADVARTYPE
;
3345 hRet
= VariantCopy(&varLeft
, pVarLeft
);
3348 pVarLeft
= &varLeft
;
3349 hRet
= VariantChangeType(pVarOut
, pVarLeft
, 0, V_VT(pVarOut
));
3353 if (V_VT(pVarLeft
) == VT_BOOL
&& V_VT(pVarRight
) == VT_BOOL
)
3355 V_VT(pVarOut
) = VT_BOOL
;
3356 V_BOOL(pVarOut
) = V_BOOL(pVarLeft
) | V_BOOL(pVarRight
);
3360 if (V_VT(pVarLeft
) == VT_UI1
&& V_VT(pVarRight
) == VT_UI1
)
3362 V_VT(pVarOut
) = VT_UI1
;
3363 V_UI1(pVarOut
) = V_UI1(pVarLeft
) | V_UI1(pVarRight
);
3367 if (V_VT(pVarLeft
) == VT_BSTR
)
3369 hRet
= VariantCopy(&varStr
, pVarLeft
);
3373 hRet
= VariantChangeType(pVarLeft
, pVarLeft
, 0, VT_BOOL
);
3378 if (V_VT(pVarLeft
) == VT_BOOL
&&
3379 (V_VT(pVarRight
) == VT_BOOL
|| V_VT(pVarRight
) == VT_BSTR
))
3383 else if ((V_VT(pVarLeft
) == VT_BOOL
|| V_VT(pVarLeft
) == VT_UI1
||
3384 V_VT(pVarLeft
) == VT_I2
|| V_VT(pVarLeft
) == VT_BSTR
) &&
3385 (V_VT(pVarRight
) == VT_BOOL
|| V_VT(pVarRight
) == VT_UI1
||
3386 V_VT(pVarRight
) == VT_I2
|| V_VT(pVarRight
) == VT_BSTR
))
3390 else if (V_VT(pVarLeft
) == VT_I8
|| V_VT(pVarRight
) == VT_I8
)
3392 if (V_VT(pVarLeft
) == VT_INT
|| V_VT(pVarRight
) == VT_INT
)
3393 return DISP_E_TYPEMISMATCH
;
3397 hRet
= VariantCopy(&varLeft
, pVarLeft
);
3401 hRet
= VariantCopy(&varRight
, pVarRight
);
3405 if (vt
== VT_I4
&& V_VT(&varLeft
) == VT_UI4
)
3406 V_VT(&varLeft
) = VT_I4
; /* Don't overflow */
3411 if (V_VT(&varLeft
) == VT_BSTR
&&
3412 FAILED(VarR8FromStr(V_BSTR(&varLeft
), LOCALE_USER_DEFAULT
, 0, &d
)))
3413 hRet
= VariantChangeType(&varLeft
, &varLeft
, VARIANT_LOCALBOOL
, VT_BOOL
);
3414 if (SUCCEEDED(hRet
) && V_VT(&varLeft
) != vt
)
3415 hRet
= VariantChangeType(&varLeft
, &varLeft
, 0, vt
);
3420 if (vt
== VT_I4
&& V_VT(&varRight
) == VT_UI4
)
3421 V_VT(&varRight
) = VT_I4
; /* Don't overflow */
3426 if (V_VT(&varRight
) == VT_BSTR
&&
3427 FAILED(VarR8FromStr(V_BSTR(&varRight
), LOCALE_USER_DEFAULT
, 0, &d
)))
3428 hRet
= VariantChangeType(&varRight
, &varRight
, VARIANT_LOCALBOOL
, VT_BOOL
);
3429 if (SUCCEEDED(hRet
) && V_VT(&varRight
) != vt
)
3430 hRet
= VariantChangeType(&varRight
, &varRight
, 0, vt
);
3438 V_I8(pVarOut
) = V_I8(&varLeft
) | V_I8(&varRight
);
3440 else if (vt
== VT_I4
)
3442 V_I4(pVarOut
) = V_I4(&varLeft
) | V_I4(&varRight
);
3446 V_I2(pVarOut
) = V_I2(&varLeft
) | V_I2(&varRight
);
3450 VariantClear(&varStr
);
3451 VariantClear(&varLeft
);
3452 VariantClear(&varRight
);
3456 /**********************************************************************
3457 * VarAbs [OLEAUT32.168]
3459 * Convert a variant to its absolute value.
3462 * pVarIn [I] Source variant
3463 * pVarOut [O] Destination for converted value
3466 * Success: S_OK. pVarOut contains the absolute value of pVarIn.
3467 * Failure: An HRESULT error code indicating the error.
3470 * - This function does not process by-reference variants.
3471 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3472 * according to the following table:
3473 *| Input Type Output Type
3474 *| ---------- -----------
3477 *| (All others) Unchanged
3479 HRESULT WINAPI
VarAbs(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
3482 HRESULT hRet
= S_OK
;
3484 TRACE("(%p->(%s%s),%p)\n", pVarIn
, debugstr_VT(pVarIn
),
3485 debugstr_VF(pVarIn
), pVarOut
);
3487 if (V_ISARRAY(pVarIn
) || V_VT(pVarIn
) == VT_UNKNOWN
||
3488 V_VT(pVarIn
) == VT_DISPATCH
|| V_VT(pVarIn
) == VT_RECORD
||
3489 V_VT(pVarIn
) == VT_ERROR
)
3490 return DISP_E_TYPEMISMATCH
;
3492 *pVarOut
= *pVarIn
; /* Shallow copy the value, and invert it if needed */
3494 #define ABS_CASE(typ,min) \
3495 case VT_##typ: if (V_##typ(pVarIn) == min) hRet = DISP_E_OVERFLOW; \
3496 else if (V_##typ(pVarIn) < 0) V_##typ(pVarOut) = -V_##typ(pVarIn); \
3499 switch (V_VT(pVarIn
))
3501 ABS_CASE(I1
,I1_MIN
);
3503 V_VT(pVarOut
) = VT_I2
;
3504 /* BOOL->I2, Fall through ... */
3505 ABS_CASE(I2
,I2_MIN
);
3507 ABS_CASE(I4
,I4_MIN
);
3508 ABS_CASE(I8
,I8_MIN
);
3509 ABS_CASE(R4
,R4_MIN
);
3511 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(&varIn
));
3514 V_VT(pVarOut
) = VT_R8
;
3516 /* Fall through ... */
3518 ABS_CASE(R8
,R8_MIN
);
3520 hRet
= VarCyAbs(V_CY(pVarIn
), & V_CY(pVarOut
));
3523 DEC_SIGN(&V_DECIMAL(pVarOut
)) &= ~DECIMAL_NEG
;
3533 V_VT(pVarOut
) = VT_I2
;
3538 hRet
= DISP_E_BADVARTYPE
;
3544 /**********************************************************************
3545 * VarFix [OLEAUT32.169]
3547 * Truncate a variants value to a whole number.
3550 * pVarIn [I] Source variant
3551 * pVarOut [O] Destination for converted value
3554 * Success: S_OK. pVarOut contains the converted value.
3555 * Failure: An HRESULT error code indicating the error.
3558 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3559 * according to the following table:
3560 *| Input Type Output Type
3561 *| ---------- -----------
3565 *| All Others Unchanged
3566 * - The difference between this function and VarInt() is that VarInt() rounds
3567 * negative numbers away from 0, while this function rounds them towards zero.
3569 HRESULT WINAPI
VarFix(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
3571 HRESULT hRet
= S_OK
;
3573 TRACE("(%p->(%s%s),%p)\n", pVarIn
, debugstr_VT(pVarIn
),
3574 debugstr_VF(pVarIn
), pVarOut
);
3576 V_VT(pVarOut
) = V_VT(pVarIn
);
3578 switch (V_VT(pVarIn
))
3581 V_UI1(pVarOut
) = V_UI1(pVarIn
);
3584 V_VT(pVarOut
) = VT_I2
;
3587 V_I2(pVarOut
) = V_I2(pVarIn
);
3590 V_I4(pVarOut
) = V_I4(pVarIn
);
3593 V_I8(pVarOut
) = V_I8(pVarIn
);
3596 if (V_R4(pVarIn
) < 0.0f
)
3597 V_R4(pVarOut
) = (float)ceil(V_R4(pVarIn
));
3599 V_R4(pVarOut
) = (float)floor(V_R4(pVarIn
));
3602 V_VT(pVarOut
) = VT_R8
;
3603 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(pVarOut
));
3608 if (V_R8(pVarIn
) < 0.0)
3609 V_R8(pVarOut
) = ceil(V_R8(pVarIn
));
3611 V_R8(pVarOut
) = floor(V_R8(pVarIn
));
3614 hRet
= VarCyFix(V_CY(pVarIn
), &V_CY(pVarOut
));
3617 hRet
= VarDecFix(&V_DECIMAL(pVarIn
), &V_DECIMAL(pVarOut
));
3620 V_VT(pVarOut
) = VT_I2
;
3627 if (V_TYPE(pVarIn
) == VT_CLSID
|| /* VT_CLSID is a special case */
3628 FAILED(VARIANT_ValidateType(V_VT(pVarIn
))))
3629 hRet
= DISP_E_BADVARTYPE
;
3631 hRet
= DISP_E_TYPEMISMATCH
;
3634 V_VT(pVarOut
) = VT_EMPTY
;
3639 /**********************************************************************
3640 * VarInt [OLEAUT32.172]
3642 * Truncate a variants value to a whole number.
3645 * pVarIn [I] Source variant
3646 * pVarOut [O] Destination for converted value
3649 * Success: S_OK. pVarOut contains the converted value.
3650 * Failure: An HRESULT error code indicating the error.
3653 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3654 * according to the following table:
3655 *| Input Type Output Type
3656 *| ---------- -----------
3660 *| All Others Unchanged
3661 * - The difference between this function and VarFix() is that VarFix() rounds
3662 * negative numbers towards 0, while this function rounds them away from zero.
3664 HRESULT WINAPI
VarInt(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
3666 HRESULT hRet
= S_OK
;
3668 TRACE("(%p->(%s%s),%p)\n", pVarIn
, debugstr_VT(pVarIn
),
3669 debugstr_VF(pVarIn
), pVarOut
);
3671 V_VT(pVarOut
) = V_VT(pVarIn
);
3673 switch (V_VT(pVarIn
))
3676 V_R4(pVarOut
) = (float)floor(V_R4(pVarIn
));
3679 V_VT(pVarOut
) = VT_R8
;
3680 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(pVarOut
));
3685 V_R8(pVarOut
) = floor(V_R8(pVarIn
));
3688 hRet
= VarCyInt(V_CY(pVarIn
), &V_CY(pVarOut
));
3691 hRet
= VarDecInt(&V_DECIMAL(pVarIn
), &V_DECIMAL(pVarOut
));
3694 return VarFix(pVarIn
, pVarOut
);
3700 /**********************************************************************
3701 * VarXor [OLEAUT32.167]
3703 * Perform a logical exclusive-or (XOR) operation on two variants.
3706 * pVarLeft [I] First variant
3707 * pVarRight [I] Variant to XOR with pVarLeft
3708 * pVarOut [O] Destination for XOR result
3711 * Success: S_OK. pVarOut contains the result of the operation with its type
3712 * taken from the table below).
3713 * Failure: An HRESULT error code indicating the error.
3716 * - Neither pVarLeft or pVarRight are modified by this function.
3717 * - This function does not process by-reference variants.
3718 * - Input types of VT_BSTR may be numeric strings or boolean text.
3719 * - The type of result stored in pVarOut depends on the types of pVarLeft
3720 * and pVarRight, and will be one of VT_UI1, VT_I2, VT_I4, VT_I8, VT_BOOL,
3721 * or VT_NULL if the function succeeds.
3722 * - Type promotion is inconsistent and as a result certain combinations of
3723 * values will return DISP_E_OVERFLOW even when they could be represented.
3724 * This matches the behaviour of native oleaut32.
3726 HRESULT WINAPI
VarXor(LPVARIANT pVarLeft
, LPVARIANT pVarRight
, LPVARIANT pVarOut
)
3729 VARIANT varLeft
, varRight
;
3733 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft
, debugstr_VT(pVarLeft
),
3734 debugstr_VF(pVarLeft
), pVarRight
, debugstr_VT(pVarRight
),
3735 debugstr_VF(pVarRight
), pVarOut
);
3737 if (V_EXTRA_TYPE(pVarLeft
) || V_EXTRA_TYPE(pVarRight
) ||
3738 V_VT(pVarLeft
) > VT_UINT
|| V_VT(pVarRight
) > VT_UINT
||
3739 V_VT(pVarLeft
) == VT_VARIANT
|| V_VT(pVarRight
) == VT_VARIANT
||
3740 V_VT(pVarLeft
) == VT_UNKNOWN
|| V_VT(pVarRight
) == VT_UNKNOWN
||
3741 V_VT(pVarLeft
) == (VARTYPE
)15 || V_VT(pVarRight
) == (VARTYPE
)15 ||
3742 V_VT(pVarLeft
) == VT_ERROR
|| V_VT(pVarRight
) == VT_ERROR
)
3743 return DISP_E_BADVARTYPE
;
3745 if (V_VT(pVarLeft
) == VT_NULL
|| V_VT(pVarRight
) == VT_NULL
)
3747 /* NULL XOR anything valid is NULL */
3748 V_VT(pVarOut
) = VT_NULL
;
3752 /* Copy our inputs so we don't disturb anything */
3753 V_VT(&varLeft
) = V_VT(&varRight
) = VT_EMPTY
;
3755 hRet
= VariantCopy(&varLeft
, pVarLeft
);
3759 hRet
= VariantCopy(&varRight
, pVarRight
);
3763 /* Try any strings first as numbers, then as VT_BOOL */
3764 if (V_VT(&varLeft
) == VT_BSTR
)
3766 hRet
= VarR8FromStr(V_BSTR(&varLeft
), LOCALE_USER_DEFAULT
, 0, &d
);
3767 hRet
= VariantChangeType(&varLeft
, &varLeft
, VARIANT_LOCALBOOL
,
3768 FAILED(hRet
) ? VT_BOOL
: VT_I4
);
3773 if (V_VT(&varRight
) == VT_BSTR
)
3775 hRet
= VarR8FromStr(V_BSTR(&varRight
), LOCALE_USER_DEFAULT
, 0, &d
);
3776 hRet
= VariantChangeType(&varRight
, &varRight
, VARIANT_LOCALBOOL
,
3777 FAILED(hRet
) ? VT_BOOL
: VT_I4
);
3782 /* Determine the result type */
3783 if (V_VT(&varLeft
) == VT_I8
|| V_VT(&varRight
) == VT_I8
)
3785 if (V_VT(pVarLeft
) == VT_INT
|| V_VT(pVarRight
) == VT_INT
)
3786 return DISP_E_TYPEMISMATCH
;
3791 switch ((V_VT(&varLeft
) << 16) | V_VT(&varRight
))
3793 case (VT_BOOL
<< 16) | VT_BOOL
:
3796 case (VT_UI1
<< 16) | VT_UI1
:
3799 case (VT_EMPTY
<< 16) | VT_EMPTY
:
3800 case (VT_EMPTY
<< 16) | VT_UI1
:
3801 case (VT_EMPTY
<< 16) | VT_I2
:
3802 case (VT_EMPTY
<< 16) | VT_BOOL
:
3803 case (VT_UI1
<< 16) | VT_EMPTY
:
3804 case (VT_UI1
<< 16) | VT_I2
:
3805 case (VT_UI1
<< 16) | VT_BOOL
:
3806 case (VT_I2
<< 16) | VT_EMPTY
:
3807 case (VT_I2
<< 16) | VT_UI1
:
3808 case (VT_I2
<< 16) | VT_I2
:
3809 case (VT_I2
<< 16) | VT_BOOL
:
3810 case (VT_BOOL
<< 16) | VT_EMPTY
:
3811 case (VT_BOOL
<< 16) | VT_UI1
:
3812 case (VT_BOOL
<< 16) | VT_I2
:
3821 /* VT_UI4 does not overflow */
3824 if (V_VT(&varLeft
) == VT_UI4
)
3825 V_VT(&varLeft
) = VT_I4
;
3826 if (V_VT(&varRight
) == VT_UI4
)
3827 V_VT(&varRight
) = VT_I4
;
3830 /* Convert our input copies to the result type */
3831 if (V_VT(&varLeft
) != vt
)
3832 hRet
= VariantChangeType(&varLeft
, &varLeft
, 0, vt
);
3836 if (V_VT(&varRight
) != vt
)
3837 hRet
= VariantChangeType(&varRight
, &varRight
, 0, vt
);
3843 /* Calculate the result */
3847 V_I8(pVarOut
) = V_I8(&varLeft
) ^ V_I8(&varRight
);
3850 V_I4(pVarOut
) = V_I4(&varLeft
) ^ V_I4(&varRight
);
3854 V_I2(pVarOut
) = V_I2(&varLeft
) ^ V_I2(&varRight
);
3857 V_UI1(pVarOut
) = V_UI1(&varLeft
) ^ V_UI1(&varRight
);
3862 VariantClear(&varLeft
);
3863 VariantClear(&varRight
);
3867 /**********************************************************************
3868 * VarEqv [OLEAUT32.172]
3870 * Determine if two variants contain the same value.
3873 * pVarLeft [I] First variant to compare
3874 * pVarRight [I] Variant to compare to pVarLeft
3875 * pVarOut [O] Destination for comparison result
3878 * Success: S_OK. pVarOut contains the result of the comparison (VARIANT_TRUE
3879 * if equivalent or non-zero otherwise.
3880 * Failure: An HRESULT error code indicating the error.
3883 * - This function simply calls VarXor() on pVarLeft and pVarRight and inverts
3886 HRESULT WINAPI
VarEqv(LPVARIANT pVarLeft
, LPVARIANT pVarRight
, LPVARIANT pVarOut
)
3890 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft
, debugstr_VT(pVarLeft
),
3891 debugstr_VF(pVarLeft
), pVarRight
, debugstr_VT(pVarRight
),
3892 debugstr_VF(pVarRight
), pVarOut
);
3894 hRet
= VarXor(pVarLeft
, pVarRight
, pVarOut
);
3895 if (SUCCEEDED(hRet
))
3897 if (V_VT(pVarOut
) == VT_I8
)
3898 V_I8(pVarOut
) = ~V_I8(pVarOut
);
3900 V_UI4(pVarOut
) = ~V_UI4(pVarOut
);
3905 /**********************************************************************
3906 * VarNeg [OLEAUT32.173]
3908 * Negate the value of a variant.
3911 * pVarIn [I] Source variant
3912 * pVarOut [O] Destination for converted value
3915 * Success: S_OK. pVarOut contains the converted value.
3916 * Failure: An HRESULT error code indicating the error.
3919 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3920 * according to the following table:
3921 *| Input Type Output Type
3922 *| ---------- -----------
3927 *| All Others Unchanged (unless promoted)
3928 * - Where the negated value of a variant does not fit in its base type, the type
3929 * is promoted according to the following table:
3930 *| Input Type Promoted To
3931 *| ---------- -----------
3935 * - The native version of this function returns DISP_E_BADVARTYPE for valid
3936 * variant types that cannot be negated, and returns DISP_E_TYPEMISMATCH
3937 * for types which are not valid. Since this is in contravention of the
3938 * meaning of those error codes and unlikely to be relied on by applications,
3939 * this implementation returns errors consistent with the other high level
3940 * variant math functions.
3942 HRESULT WINAPI
VarNeg(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
3944 HRESULT hRet
= S_OK
;
3946 TRACE("(%p->(%s%s),%p)\n", pVarIn
, debugstr_VT(pVarIn
),
3947 debugstr_VF(pVarIn
), pVarOut
);
3949 V_VT(pVarOut
) = V_VT(pVarIn
);
3951 switch (V_VT(pVarIn
))
3954 V_VT(pVarOut
) = VT_I2
;
3955 V_I2(pVarOut
) = -V_UI1(pVarIn
);
3958 V_VT(pVarOut
) = VT_I2
;
3961 if (V_I2(pVarIn
) == I2_MIN
)
3963 V_VT(pVarOut
) = VT_I4
;
3964 V_I4(pVarOut
) = -(int)V_I2(pVarIn
);
3967 V_I2(pVarOut
) = -V_I2(pVarIn
);
3970 if (V_I4(pVarIn
) == I4_MIN
)
3972 V_VT(pVarOut
) = VT_R8
;
3973 V_R8(pVarOut
) = -(double)V_I4(pVarIn
);
3976 V_I4(pVarOut
) = -V_I4(pVarIn
);
3979 if (V_I8(pVarIn
) == I8_MIN
)
3981 V_VT(pVarOut
) = VT_R8
;
3982 hRet
= VarR8FromI8(V_I8(pVarIn
), &V_R8(pVarOut
));
3983 V_R8(pVarOut
) *= -1.0;
3986 V_I8(pVarOut
) = -V_I8(pVarIn
);
3989 V_R4(pVarOut
) = -V_R4(pVarIn
);
3993 V_R8(pVarOut
) = -V_R8(pVarIn
);
3996 hRet
= VarCyNeg(V_CY(pVarIn
), &V_CY(pVarOut
));
3999 hRet
= VarDecNeg(&V_DECIMAL(pVarIn
), &V_DECIMAL(pVarOut
));
4002 V_VT(pVarOut
) = VT_R8
;
4003 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(pVarOut
));
4004 V_R8(pVarOut
) = -V_R8(pVarOut
);
4007 V_VT(pVarOut
) = VT_I2
;
4014 if (V_TYPE(pVarIn
) == VT_CLSID
|| /* VT_CLSID is a special case */
4015 FAILED(VARIANT_ValidateType(V_VT(pVarIn
))))
4016 hRet
= DISP_E_BADVARTYPE
;
4018 hRet
= DISP_E_TYPEMISMATCH
;
4021 V_VT(pVarOut
) = VT_EMPTY
;
4026 /**********************************************************************
4027 * VarNot [OLEAUT32.174]
4029 * Perform a not operation on a variant.
4032 * pVarIn [I] Source variant
4033 * pVarOut [O] Destination for converted value
4036 * Success: S_OK. pVarOut contains the converted value.
4037 * Failure: An HRESULT error code indicating the error.
4040 * - Strictly speaking, this function performs a bitwise ones complement
4041 * on the variants value (after possibly converting to VT_I4, see below).
4042 * This only behaves like a boolean not operation if the value in
4043 * pVarIn is either VARIANT_TRUE or VARIANT_FALSE and the type is signed.
4044 * - To perform a genuine not operation, convert the variant to a VT_BOOL
4045 * before calling this function.
4046 * - This function does not process by-reference variants.
4047 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4048 * according to the following table:
4049 *| Input Type Output Type
4050 *| ---------- -----------
4057 *| (All others) Unchanged
4059 HRESULT WINAPI
VarNot(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
4062 HRESULT hRet
= S_OK
;
4064 TRACE("(%p->(%s%s),%p)\n", pVarIn
, debugstr_VT(pVarIn
),
4065 debugstr_VF(pVarIn
), pVarOut
);
4067 V_VT(pVarOut
) = V_VT(pVarIn
);
4069 switch (V_VT(pVarIn
))
4072 V_I4(pVarOut
) = ~V_I1(pVarIn
);
4073 V_VT(pVarOut
) = VT_I4
;
4075 case VT_UI1
: V_UI1(pVarOut
) = ~V_UI1(pVarIn
); break;
4077 case VT_I2
: V_I2(pVarOut
) = ~V_I2(pVarIn
); break;
4079 V_I4(pVarOut
) = ~V_UI2(pVarIn
);
4080 V_VT(pVarOut
) = VT_I4
;
4083 hRet
= VarI4FromDec(&V_DECIMAL(pVarIn
), &V_I4(&varIn
));
4087 /* Fall through ... */
4089 V_VT(pVarOut
) = VT_I4
;
4090 /* Fall through ... */
4091 case VT_I4
: V_I4(pVarOut
) = ~V_I4(pVarIn
); break;
4094 V_I4(pVarOut
) = ~V_UI4(pVarIn
);
4095 V_VT(pVarOut
) = VT_I4
;
4097 case VT_I8
: V_I8(pVarOut
) = ~V_I8(pVarIn
); break;
4099 V_I4(pVarOut
) = ~V_UI8(pVarIn
);
4100 V_VT(pVarOut
) = VT_I4
;
4103 hRet
= VarI4FromR4(V_R4(pVarIn
), &V_I4(pVarOut
));
4104 V_I4(pVarOut
) = ~V_I4(pVarOut
);
4105 V_VT(pVarOut
) = VT_I4
;
4108 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(&varIn
));
4112 /* Fall through ... */
4115 hRet
= VarI4FromR8(V_R8(pVarIn
), &V_I4(pVarOut
));
4116 V_I4(pVarOut
) = ~V_I4(pVarOut
);
4117 V_VT(pVarOut
) = VT_I4
;
4120 hRet
= VarI4FromCy(V_CY(pVarIn
), &V_I4(pVarOut
));
4121 V_I4(pVarOut
) = ~V_I4(pVarOut
);
4122 V_VT(pVarOut
) = VT_I4
;
4126 V_VT(pVarOut
) = VT_I2
;
4132 if (V_TYPE(pVarIn
) == VT_CLSID
|| /* VT_CLSID is a special case */
4133 FAILED(VARIANT_ValidateType(V_VT(pVarIn
))))
4134 hRet
= DISP_E_BADVARTYPE
;
4136 hRet
= DISP_E_TYPEMISMATCH
;
4139 V_VT(pVarOut
) = VT_EMPTY
;
4144 /**********************************************************************
4145 * VarRound [OLEAUT32.175]
4147 * Perform a round operation on a variant.
4150 * pVarIn [I] Source variant
4151 * deci [I] Number of decimals to round to
4152 * pVarOut [O] Destination for converted value
4155 * Success: S_OK. pVarOut contains the converted value.
4156 * Failure: An HRESULT error code indicating the error.
4159 * - Floating point values are rounded to the desired number of decimals.
4160 * - Some integer types are just copied to the return variable.
4161 * - Some other integer types are not handled and fail.
4163 HRESULT WINAPI
VarRound(LPVARIANT pVarIn
, int deci
, LPVARIANT pVarOut
)
4166 HRESULT hRet
= S_OK
;
4169 TRACE("(%p->(%s%s),%d)\n", pVarIn
, debugstr_VT(pVarIn
), debugstr_VF(pVarIn
), deci
);
4171 switch (V_VT(pVarIn
))
4173 /* cases that fail on windows */
4178 hRet
= DISP_E_BADVARTYPE
;
4181 /* cases just copying in to out */
4183 V_VT(pVarOut
) = V_VT(pVarIn
);
4184 V_UI1(pVarOut
) = V_UI1(pVarIn
);
4187 V_VT(pVarOut
) = V_VT(pVarIn
);
4188 V_I2(pVarOut
) = V_I2(pVarIn
);
4191 V_VT(pVarOut
) = V_VT(pVarIn
);
4192 V_I4(pVarOut
) = V_I4(pVarIn
);
4195 V_VT(pVarOut
) = V_VT(pVarIn
);
4196 /* value unchanged */
4199 /* cases that change type */
4201 V_VT(pVarOut
) = VT_I2
;
4205 V_VT(pVarOut
) = VT_I2
;
4206 V_I2(pVarOut
) = V_BOOL(pVarIn
);
4209 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(&varIn
));
4214 /* Fall through ... */
4216 /* cases we need to do math */
4218 if (V_R8(pVarIn
)>0) {
4219 V_R8(pVarOut
)=floor(V_R8(pVarIn
)*pow(10, deci
)+0.5)/pow(10, deci
);
4221 V_R8(pVarOut
)=ceil(V_R8(pVarIn
)*pow(10, deci
)-0.5)/pow(10, deci
);
4223 V_VT(pVarOut
) = V_VT(pVarIn
);
4226 if (V_R4(pVarIn
)>0) {
4227 V_R4(pVarOut
)=floor(V_R4(pVarIn
)*pow(10, deci
)+0.5)/pow(10, deci
);
4229 V_R4(pVarOut
)=ceil(V_R4(pVarIn
)*pow(10, deci
)-0.5)/pow(10, deci
);
4231 V_VT(pVarOut
) = V_VT(pVarIn
);
4234 if (V_DATE(pVarIn
)>0) {
4235 V_DATE(pVarOut
)=floor(V_DATE(pVarIn
)*pow(10, deci
)+0.5)/pow(10, deci
);
4237 V_DATE(pVarOut
)=ceil(V_DATE(pVarIn
)*pow(10, deci
)-0.5)/pow(10, deci
);
4239 V_VT(pVarOut
) = V_VT(pVarIn
);
4245 factor
=pow(10, 4-deci
);
4247 if (V_CY(pVarIn
).int64
>0) {
4248 V_CY(pVarOut
).int64
=floor(V_CY(pVarIn
).int64
/factor
)*factor
;
4250 V_CY(pVarOut
).int64
=ceil(V_CY(pVarIn
).int64
/factor
)*factor
;
4252 V_VT(pVarOut
) = V_VT(pVarIn
);
4255 /* cases we don't know yet */
4257 FIXME("unimplemented part, V_VT(pVarIn) == 0x%X, deci == %d\n",
4258 V_VT(pVarIn
) & VT_TYPEMASK
, deci
);
4259 hRet
= DISP_E_BADVARTYPE
;
4263 V_VT(pVarOut
) = VT_EMPTY
;
4265 TRACE("returning 0x%08lx (%s%s),%f\n", hRet
, debugstr_VT(pVarOut
),
4266 debugstr_VF(pVarOut
), (V_VT(pVarOut
) == VT_R4
) ? V_R4(pVarOut
) :
4267 (V_VT(pVarOut
) == VT_R8
) ? V_R8(pVarOut
) : 0);
4272 /**********************************************************************
4273 * VarIdiv [OLEAUT32.153]
4275 * Converts input variants to integers and divides them.
4278 * left [I] Left hand variant
4279 * right [I] Right hand variant
4280 * result [O] Destination for quotient
4283 * Success: S_OK. result contains the quotient.
4284 * Failure: An HRESULT error code indicating the error.
4287 * If either expression is null, null is returned, as per MSDN
4289 HRESULT WINAPI
VarIdiv(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
4297 if ((V_VT(left
) == VT_NULL
) || (V_VT(right
) == VT_NULL
)) {
4298 hr
= VariantChangeType(result
, result
, 0, VT_NULL
);
4300 /* This should never happen */
4301 FIXME("Failed to convert return value to VT_NULL.\n");
4307 hr
= VariantChangeType(&lv
, left
, 0, VT_I4
);
4311 hr
= VariantChangeType(&rv
, right
, 0, VT_I4
);
4316 hr
= VarDiv(&lv
, &rv
, result
);
4321 /**********************************************************************
4322 * VarMod [OLEAUT32.155]
4324 * Perform the modulus operation of the right hand variant on the left
4327 * left [I] Left hand variant
4328 * right [I] Right hand variant
4329 * result [O] Destination for converted value
4332 * Success: S_OK. result contains the remainder.
4333 * Failure: An HRESULT error code indicating the error.
4336 * If an error occurs the type of result will be modified but the value will not be.
4337 * Doesn't support arrays or any special flags yet.
4339 HRESULT WINAPI
VarMod(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
4343 HRESULT rc
= E_FAIL
;
4350 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
),
4351 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
), result
);
4353 /* check for invalid inputs */
4355 switch (V_VT(left
) & VT_TYPEMASK
) {
4376 V_VT(result
) = VT_EMPTY
;
4377 return DISP_E_TYPEMISMATCH
;
4379 V_VT(result
) = VT_EMPTY
;
4380 return DISP_E_OVERFLOW
;
4382 return DISP_E_TYPEMISMATCH
;
4384 V_VT(result
) = VT_EMPTY
;
4385 return DISP_E_TYPEMISMATCH
;
4389 V_VT(result
) = VT_EMPTY
;
4390 return DISP_E_BADVARTYPE
;
4395 switch (V_VT(right
) & VT_TYPEMASK
) {
4401 if((V_VT(left
) == VT_INT
) && (V_VT(right
) == VT_I8
))
4403 V_VT(result
) = VT_EMPTY
;
4404 return DISP_E_TYPEMISMATCH
;
4407 if((V_VT(right
) == VT_INT
) && (V_VT(left
) == VT_I8
))
4409 V_VT(result
) = VT_EMPTY
;
4410 return DISP_E_TYPEMISMATCH
;
4420 if(V_VT(left
) == VT_EMPTY
)
4422 V_VT(result
) = VT_I4
;
4428 if(V_VT(left
) == VT_NULL
)
4430 V_VT(result
) = VT_NULL
;
4436 V_VT(result
) = VT_EMPTY
;
4437 return DISP_E_BADVARTYPE
;
4439 if(V_VT(left
) == VT_VOID
)
4441 V_VT(result
) = VT_EMPTY
;
4442 return DISP_E_BADVARTYPE
;
4443 } else if((V_VT(left
) == VT_NULL
) || (V_VT(left
) == VT_EMPTY
) || (V_VT(left
) == VT_ERROR
) ||
4446 V_VT(result
) = VT_NULL
;
4450 V_VT(result
) = VT_NULL
;
4451 return DISP_E_BADVARTYPE
;
4455 V_VT(result
) = VT_EMPTY
;
4456 return DISP_E_TYPEMISMATCH
;
4458 if(V_VT(left
) == VT_ERROR
)
4460 V_VT(result
) = VT_EMPTY
;
4461 return DISP_E_TYPEMISMATCH
;
4464 V_VT(result
) = VT_EMPTY
;
4465 return DISP_E_OVERFLOW
;
4468 return DISP_E_TYPEMISMATCH
;
4470 if((V_VT(left
) == 15) || ((V_VT(left
) >= 24) && (V_VT(left
) <= 35)) || !lOk
)
4472 V_VT(result
) = VT_EMPTY
;
4473 return DISP_E_BADVARTYPE
;
4476 V_VT(result
) = VT_EMPTY
;
4477 return DISP_E_TYPEMISMATCH
;
4480 V_VT(result
) = VT_EMPTY
;
4481 return DISP_E_BADVARTYPE
;
4484 /* determine the result type */
4485 if((V_VT(left
) == VT_I8
) || (V_VT(right
) == VT_I8
)) resT
= VT_I8
;
4486 else if((V_VT(left
) == VT_UI1
) && (V_VT(right
) == VT_BOOL
)) resT
= VT_I2
;
4487 else if((V_VT(left
) == VT_UI1
) && (V_VT(right
) == VT_UI1
)) resT
= VT_UI1
;
4488 else if((V_VT(left
) == VT_UI1
) && (V_VT(right
) == VT_I2
)) resT
= VT_I2
;
4489 else if((V_VT(left
) == VT_I2
) && (V_VT(right
) == VT_BOOL
)) resT
= VT_I2
;
4490 else if((V_VT(left
) == VT_I2
) && (V_VT(right
) == VT_UI1
)) resT
= VT_I2
;
4491 else if((V_VT(left
) == VT_I2
) && (V_VT(right
) == VT_I2
)) resT
= VT_I2
;
4492 else if((V_VT(left
) == VT_BOOL
) && (V_VT(right
) == VT_BOOL
)) resT
= VT_I2
;
4493 else if((V_VT(left
) == VT_BOOL
) && (V_VT(right
) == VT_UI1
)) resT
= VT_I2
;
4494 else if((V_VT(left
) == VT_BOOL
) && (V_VT(right
) == VT_I2
)) resT
= VT_I2
;
4495 else resT
= VT_I4
; /* most outputs are I4 */
4497 /* convert to I8 for the modulo */
4498 rc
= VariantChangeType(&lv
, left
, 0, VT_I8
);
4501 FIXME("Could not convert left type %d to %d? rc == 0x%lX\n", V_VT(left
), VT_I8
, rc
);
4505 rc
= VariantChangeType(&rv
, right
, 0, VT_I8
);
4508 FIXME("Could not convert right type %d to %d? rc == 0x%lX\n", V_VT(right
), VT_I8
, rc
);
4512 /* if right is zero set VT_EMPTY and return divide by zero */
4515 V_VT(result
) = VT_EMPTY
;
4516 return DISP_E_DIVBYZERO
;
4519 /* perform the modulo operation */
4520 V_VT(result
) = VT_I8
;
4521 V_I8(result
) = V_I8(&lv
) % V_I8(&rv
);
4523 TRACE("V_I8(left) == %ld, V_I8(right) == %ld, V_I8(result) == %ld\n", (long)V_I8(&lv
), (long)V_I8(&rv
), (long)V_I8(result
));
4525 /* convert left and right to the destination type */
4526 rc
= VariantChangeType(result
, result
, 0, resT
);
4529 FIXME("Could not convert 0x%x to %d?\n", V_VT(result
), resT
);
4536 /**********************************************************************
4537 * VarPow [OLEAUT32.158]
4540 HRESULT WINAPI
VarPow(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
4545 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
), debugstr_VF(left
),
4546 right
, debugstr_VT(right
), debugstr_VF(right
), result
);
4548 hr
= VariantChangeType(&dl
,left
,0,VT_R8
);
4549 if (!SUCCEEDED(hr
)) {
4550 ERR("Could not change passed left argument to VT_R8, handle it differently.\n");
4553 hr
= VariantChangeType(&dr
,right
,0,VT_R8
);
4554 if (!SUCCEEDED(hr
)) {
4555 ERR("Could not change passed right argument to VT_R8, handle it differently.\n");
4558 V_VT(result
) = VT_R8
;
4559 V_R8(result
) = pow(V_R8(&dl
),V_R8(&dr
));