4 * Copyright 1998 Jean-Claude Cote
5 * Copyright 2003 Jon Griffiths
6 * The alorithm for conversion from Julian days to day/month/year is based on
7 * that devised by Henry Fliegel, as implemented in PostgreSQL, which is
8 * Copyright 1994-7 Regents of the University of California
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
37 #include "wine/unicode.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(variant);
44 const char* wine_vtypes[VT_CLSID] =
46 "VT_EMPTY","VT_NULL","VT_I2","VT_I4","VT_R4","VT_R8","VT_CY","VT_DATE",
47 "VT_BSTR","VT_DISPATCH","VT_ERROR","VT_BOOL","VT_VARIANT","VT_UNKNOWN",
48 "VT_DECIMAL","15","VT_I1","VT_UI1","VT_UI2","VT_UI4","VT_I8","VT_UI8",
49 "VT_INT","VT_UINT","VT_VOID","VT_HRESULT","VT_PTR","VT_SAFEARRAY",
50 "VT_CARRAY","VT_USERDEFINED","VT_LPSTR","VT_LPWSTR""32","33","34","35",
51 "VT_RECORD","VT_INT_PTR","VT_UINT_PTR","39","40","41","42","43","44","45",
52 "46","47","48","49","50","51","52","53","54","55","56","57","58","59","60",
53 "61","62","63","VT_FILETIME","VT_BLOB","VT_STREAM","VT_STORAGE",
54 "VT_STREAMED_OBJECT","VT_STORED_OBJECT","VT_BLOB_OBJECT","VT_CF","VT_CLSID"
57 const char* wine_vflags[16] =
62 "|VT_VECTOR|VT_ARRAY",
64 "|VT_VECTOR|VT_ARRAY",
66 "|VT_VECTOR|VT_ARRAY|VT_BYREF",
68 "|VT_VECTOR|VT_HARDTYPE",
69 "|VT_ARRAY|VT_HARDTYPE",
70 "|VT_VECTOR|VT_ARRAY|VT_HARDTYPE",
71 "|VT_BYREF|VT_HARDTYPE",
72 "|VT_VECTOR|VT_ARRAY|VT_HARDTYPE",
73 "|VT_ARRAY|VT_BYREF|VT_HARDTYPE",
74 "|VT_VECTOR|VT_ARRAY|VT_BYREF|VT_HARDTYPE",
77 /* Convert a variant from one type to another */
78 static inline HRESULT VARIANT_Coerce(VARIANTARG* pd, LCID lcid, USHORT wFlags,
79 VARIANTARG* ps, VARTYPE vt)
81 HRESULT res = DISP_E_TYPEMISMATCH;
82 VARTYPE vtFrom = V_TYPE(ps);
83 BOOL bIgnoreOverflow = FALSE;
86 TRACE("(%p->(%s%s),0x%08lx,0x%04x,%p->(%s%s),%s%s)\n", pd, debugstr_VT(pd),
87 debugstr_VF(pd), lcid, wFlags, ps, debugstr_VT(ps), debugstr_VF(ps),
88 debugstr_vt(vt), debugstr_vf(vt));
90 if (vt == VT_BSTR || vtFrom == VT_BSTR)
92 /* All flags passed to low level function are only used for
93 * changing to or from strings. Map these here.
95 if (wFlags & VARIANT_LOCALBOOL)
96 dwFlags |= VAR_LOCALBOOL;
97 if (wFlags & VARIANT_CALENDAR_HIJRI)
98 dwFlags |= VAR_CALENDAR_HIJRI;
99 if (wFlags & VARIANT_CALENDAR_THAI)
100 dwFlags |= VAR_CALENDAR_THAI;
101 if (wFlags & VARIANT_CALENDAR_GREGORIAN)
102 dwFlags |= VAR_CALENDAR_GREGORIAN;
103 if (wFlags & VARIANT_NOUSEROVERRIDE)
104 dwFlags |= LOCALE_NOUSEROVERRIDE;
105 if (wFlags & VARIANT_USE_NLS)
106 dwFlags |= LOCALE_USE_NLS;
109 /* Map int/uint to i4/ui4 */
112 else if (vt == VT_UINT)
115 if (vtFrom == VT_INT)
117 else if (vtFrom == VT_UINT)
121 bIgnoreOverflow = TRUE;
125 return VariantCopy(pd, ps);
127 if (wFlags & VARIANT_NOVALUEPROP && vtFrom == VT_DISPATCH && vt != VT_UNKNOWN)
129 /* VARIANT_NOVALUEPROP prevents IDispatch objects from being coerced by
130 * accessing the default object property.
132 return DISP_E_TYPEMISMATCH;
138 if (vtFrom == VT_NULL)
139 return DISP_E_TYPEMISMATCH;
140 /* ... Fall through */
142 if (vtFrom <= VT_UINT && vtFrom != (VARTYPE)15 && vtFrom != VT_ERROR)
144 res = VariantClear( pd );
145 if (vt == VT_NULL && SUCCEEDED(res))
153 case VT_EMPTY: V_I1(pd) = 0; return S_OK;
154 case VT_I2: return VarI1FromI2(V_I2(ps), &V_I1(pd));
155 case VT_I4: return VarI1FromI4(V_I4(ps), &V_I1(pd));
156 case VT_UI1: return VarI1FromUI1(V_UI1(ps), &V_I1(pd));
157 case VT_UI2: return VarI1FromUI2(V_UI2(ps), &V_I1(pd));
158 case VT_UI4: return VarI1FromUI4(V_UI4(ps), &V_I1(pd));
159 case VT_I8: return VarI1FromI8(V_I8(ps), &V_I1(pd));
160 case VT_UI8: return VarI1FromUI8(V_UI8(ps), &V_I1(pd));
161 case VT_R4: return VarI1FromR4(V_R4(ps), &V_I1(pd));
162 case VT_R8: return VarI1FromR8(V_R8(ps), &V_I1(pd));
163 case VT_DATE: return VarI1FromDate(V_DATE(ps), &V_I1(pd));
164 case VT_BOOL: return VarI1FromBool(V_BOOL(ps), &V_I1(pd));
165 case VT_CY: return VarI1FromCy(V_CY(ps), &V_I1(pd));
166 case VT_DECIMAL: return VarI1FromDec(&V_DECIMAL(ps), &V_I1(pd) );
167 case VT_DISPATCH: return VarI1FromDisp(V_DISPATCH(ps), lcid, &V_I1(pd) );
168 case VT_BSTR: return VarI1FromStr(V_BSTR(ps), lcid, dwFlags, &V_I1(pd) );
175 case VT_EMPTY: V_I2(pd) = 0; return S_OK;
176 case VT_I1: return VarI2FromI1(V_I1(ps), &V_I2(pd));
177 case VT_I4: return VarI2FromI4(V_I4(ps), &V_I2(pd));
178 case VT_UI1: return VarI2FromUI1(V_UI1(ps), &V_I2(pd));
179 case VT_UI2: return VarI2FromUI2(V_UI2(ps), &V_I2(pd));
180 case VT_UI4: return VarI2FromUI4(V_UI4(ps), &V_I2(pd));
181 case VT_I8: return VarI2FromI8(V_I8(ps), &V_I2(pd));
182 case VT_UI8: return VarI2FromUI8(V_UI8(ps), &V_I2(pd));
183 case VT_R4: return VarI2FromR4(V_R4(ps), &V_I2(pd));
184 case VT_R8: return VarI2FromR8(V_R8(ps), &V_I2(pd));
185 case VT_DATE: return VarI2FromDate(V_DATE(ps), &V_I2(pd));
186 case VT_BOOL: return VarI2FromBool(V_BOOL(ps), &V_I2(pd));
187 case VT_CY: return VarI2FromCy(V_CY(ps), &V_I2(pd));
188 case VT_DECIMAL: return VarI2FromDec(&V_DECIMAL(ps), &V_I2(pd));
189 case VT_DISPATCH: return VarI2FromDisp(V_DISPATCH(ps), lcid, &V_I2(pd));
190 case VT_BSTR: return VarI2FromStr(V_BSTR(ps), lcid, dwFlags, &V_I2(pd));
197 case VT_EMPTY: V_I4(pd) = 0; return S_OK;
198 case VT_I1: return VarI4FromI1(V_I1(ps), &V_I4(pd));
199 case VT_I2: return VarI4FromI2(V_I2(ps), &V_I4(pd));
200 case VT_UI1: return VarI4FromUI1(V_UI1(ps), &V_I4(pd));
201 case VT_UI2: return VarI4FromUI2(V_UI2(ps), &V_I4(pd));
209 return VarI4FromUI4(V_UI4(ps), &V_I4(pd));
210 case VT_I8: return VarI4FromI8(V_I8(ps), &V_I4(pd));
211 case VT_UI8: return VarI4FromUI8(V_UI8(ps), &V_I4(pd));
212 case VT_R4: return VarI4FromR4(V_R4(ps), &V_I4(pd));
213 case VT_R8: return VarI4FromR8(V_R8(ps), &V_I4(pd));
214 case VT_DATE: return VarI4FromDate(V_DATE(ps), &V_I4(pd));
215 case VT_BOOL: return VarI4FromBool(V_BOOL(ps), &V_I4(pd));
216 case VT_CY: return VarI4FromCy(V_CY(ps), &V_I4(pd));
217 case VT_DECIMAL: return VarI4FromDec(&V_DECIMAL(ps), &V_I4(pd));
218 case VT_DISPATCH: return VarI4FromDisp(V_DISPATCH(ps), lcid, &V_I4(pd));
219 case VT_BSTR: return VarI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_I4(pd));
226 case VT_EMPTY: V_UI1(pd) = 0; return S_OK;
227 case VT_I1: return VarUI1FromI1(V_I1(ps), &V_UI1(pd));
228 case VT_I2: return VarUI1FromI2(V_I2(ps), &V_UI1(pd));
229 case VT_I4: return VarUI1FromI4(V_I4(ps), &V_UI1(pd));
230 case VT_UI2: return VarUI1FromUI2(V_UI2(ps), &V_UI1(pd));
231 case VT_UI4: return VarUI1FromUI4(V_UI4(ps), &V_UI1(pd));
232 case VT_I8: return VarUI1FromI8(V_I8(ps), &V_UI1(pd));
233 case VT_UI8: return VarUI1FromUI8(V_UI8(ps), &V_UI1(pd));
234 case VT_R4: return VarUI1FromR4(V_R4(ps), &V_UI1(pd));
235 case VT_R8: return VarUI1FromR8(V_R8(ps), &V_UI1(pd));
236 case VT_DATE: return VarUI1FromDate(V_DATE(ps), &V_UI1(pd));
237 case VT_BOOL: return VarUI1FromBool(V_BOOL(ps), &V_UI1(pd));
238 case VT_CY: return VarUI1FromCy(V_CY(ps), &V_UI1(pd));
239 case VT_DECIMAL: return VarUI1FromDec(&V_DECIMAL(ps), &V_UI1(pd));
240 case VT_DISPATCH: return VarUI1FromDisp(V_DISPATCH(ps), lcid, &V_UI1(pd));
241 case VT_BSTR: return VarUI1FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI1(pd));
248 case VT_EMPTY: V_UI2(pd) = 0; return S_OK;
249 case VT_I1: return VarUI2FromI1(V_I1(ps), &V_UI2(pd));
250 case VT_I2: return VarUI2FromI2(V_I2(ps), &V_UI2(pd));
251 case VT_I4: return VarUI2FromI4(V_I4(ps), &V_UI2(pd));
252 case VT_UI1: return VarUI2FromUI1(V_UI1(ps), &V_UI2(pd));
253 case VT_UI4: return VarUI2FromUI4(V_UI4(ps), &V_UI2(pd));
254 case VT_I8: return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
255 case VT_UI8: return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
256 case VT_R4: return VarUI2FromR4(V_R4(ps), &V_UI2(pd));
257 case VT_R8: return VarUI2FromR8(V_R8(ps), &V_UI2(pd));
258 case VT_DATE: return VarUI2FromDate(V_DATE(ps), &V_UI2(pd));
259 case VT_BOOL: return VarUI2FromBool(V_BOOL(ps), &V_UI2(pd));
260 case VT_CY: return VarUI2FromCy(V_CY(ps), &V_UI2(pd));
261 case VT_DECIMAL: return VarUI2FromDec(&V_DECIMAL(ps), &V_UI2(pd));
262 case VT_DISPATCH: return VarUI2FromDisp(V_DISPATCH(ps), lcid, &V_UI2(pd));
263 case VT_BSTR: return VarUI2FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI2(pd));
270 case VT_EMPTY: V_UI4(pd) = 0; return S_OK;
271 case VT_I1: return VarUI4FromI1(V_I1(ps), &V_UI4(pd));
272 case VT_I2: return VarUI4FromI2(V_I2(ps), &V_UI4(pd));
273 case VT_I4: return VarUI4FromI4(V_I4(ps), &V_UI4(pd));
274 case VT_UI1: return VarUI4FromUI1(V_UI1(ps), &V_UI4(pd));
275 case VT_UI2: return VarUI4FromUI2(V_UI2(ps), &V_UI4(pd));
276 case VT_I8: return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
277 case VT_UI8: return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
278 case VT_R4: return VarUI4FromR4(V_R4(ps), &V_UI4(pd));
279 case VT_R8: return VarUI4FromR8(V_R8(ps), &V_UI4(pd));
280 case VT_DATE: return VarUI4FromDate(V_DATE(ps), &V_UI4(pd));
281 case VT_BOOL: return VarUI4FromBool(V_BOOL(ps), &V_UI4(pd));
282 case VT_CY: return VarUI4FromCy(V_CY(ps), &V_UI4(pd));
283 case VT_DECIMAL: return VarUI4FromDec(&V_DECIMAL(ps), &V_UI4(pd));
284 case VT_DISPATCH: return VarUI4FromDisp(V_DISPATCH(ps), lcid, &V_UI4(pd));
285 case VT_BSTR: return VarUI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI4(pd));
292 case VT_EMPTY: V_UI8(pd) = 0; return S_OK;
293 case VT_I4: if (V_I4(ps) < 0) return DISP_E_OVERFLOW; V_UI8(pd) = V_I4(ps); return S_OK;
294 case VT_I1: return VarUI8FromI1(V_I1(ps), &V_UI8(pd));
295 case VT_I2: return VarUI8FromI2(V_I2(ps), &V_UI8(pd));
296 case VT_UI1: return VarUI8FromUI1(V_UI1(ps), &V_UI8(pd));
297 case VT_UI2: return VarUI8FromUI2(V_UI2(ps), &V_UI8(pd));
298 case VT_UI4: return VarUI8FromUI4(V_UI4(ps), &V_UI8(pd));
299 case VT_I8: return VarUI8FromI8(V_I8(ps), &V_UI8(pd));
300 case VT_R4: return VarUI8FromR4(V_R4(ps), &V_UI8(pd));
301 case VT_R8: return VarUI8FromR8(V_R8(ps), &V_UI8(pd));
302 case VT_DATE: return VarUI8FromDate(V_DATE(ps), &V_UI8(pd));
303 case VT_BOOL: return VarUI8FromBool(V_BOOL(ps), &V_UI8(pd));
304 case VT_CY: return VarUI8FromCy(V_CY(ps), &V_UI8(pd));
305 case VT_DECIMAL: return VarUI8FromDec(&V_DECIMAL(ps), &V_UI8(pd));
306 case VT_DISPATCH: return VarUI8FromDisp(V_DISPATCH(ps), lcid, &V_UI8(pd));
307 case VT_BSTR: return VarUI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI8(pd));
314 case VT_EMPTY: V_I8(pd) = 0; return S_OK;
315 case VT_I4: V_I8(pd) = V_I4(ps); return S_OK;
316 case VT_I1: return VarI8FromI1(V_I1(ps), &V_I8(pd));
317 case VT_I2: return VarI8FromI2(V_I2(ps), &V_I8(pd));
318 case VT_UI1: return VarI8FromUI1(V_UI1(ps), &V_I8(pd));
319 case VT_UI2: return VarI8FromUI2(V_UI2(ps), &V_I8(pd));
320 case VT_UI4: return VarI8FromUI4(V_UI4(ps), &V_I8(pd));
321 case VT_UI8: return VarI8FromUI8(V_I8(ps), &V_I8(pd));
322 case VT_R4: return VarI8FromR4(V_R4(ps), &V_I8(pd));
323 case VT_R8: return VarI8FromR8(V_R8(ps), &V_I8(pd));
324 case VT_DATE: return VarI8FromDate(V_DATE(ps), &V_I8(pd));
325 case VT_BOOL: return VarI8FromBool(V_BOOL(ps), &V_I8(pd));
326 case VT_CY: return VarI8FromCy(V_CY(ps), &V_I8(pd));
327 case VT_DECIMAL: return VarI8FromDec(&V_DECIMAL(ps), &V_I8(pd));
328 case VT_DISPATCH: return VarI8FromDisp(V_DISPATCH(ps), lcid, &V_I8(pd));
329 case VT_BSTR: return VarI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_I8(pd));
336 case VT_EMPTY: V_R4(pd) = 0.0f; return S_OK;
337 case VT_I1: return VarR4FromI1(V_I1(ps), &V_R4(pd));
338 case VT_I2: return VarR4FromI2(V_I2(ps), &V_R4(pd));
339 case VT_I4: return VarR4FromI4(V_I4(ps), &V_R4(pd));
340 case VT_UI1: return VarR4FromUI1(V_UI1(ps), &V_R4(pd));
341 case VT_UI2: return VarR4FromUI2(V_UI2(ps), &V_R4(pd));
342 case VT_UI4: return VarR4FromUI4(V_UI4(ps), &V_R4(pd));
343 case VT_I8: return VarR4FromI8(V_I8(ps), &V_R4(pd));
344 case VT_UI8: return VarR4FromUI8(V_UI8(ps), &V_R4(pd));
345 case VT_R8: return VarR4FromR8(V_R8(ps), &V_R4(pd));
346 case VT_DATE: return VarR4FromDate(V_DATE(ps), &V_R4(pd));
347 case VT_BOOL: return VarR4FromBool(V_BOOL(ps), &V_R4(pd));
348 case VT_CY: return VarR4FromCy(V_CY(ps), &V_R4(pd));
349 case VT_DECIMAL: return VarR4FromDec(&V_DECIMAL(ps), &V_R4(pd));
350 case VT_DISPATCH: return VarR4FromDisp(V_DISPATCH(ps), lcid, &V_R4(pd));
351 case VT_BSTR: return VarR4FromStr(V_BSTR(ps), lcid, dwFlags, &V_R4(pd));
358 case VT_EMPTY: V_R8(pd) = 0.0; return S_OK;
359 case VT_I1: return VarR8FromI1(V_I1(ps), &V_R8(pd));
360 case VT_I2: return VarR8FromI2(V_I2(ps), &V_R8(pd));
361 case VT_I4: return VarR8FromI4(V_I4(ps), &V_R8(pd));
362 case VT_UI1: return VarR8FromUI1(V_UI1(ps), &V_R8(pd));
363 case VT_UI2: return VarR8FromUI2(V_UI2(ps), &V_R8(pd));
364 case VT_UI4: return VarR8FromUI4(V_UI4(ps), &V_R8(pd));
365 case VT_I8: return VarR8FromI8(V_I8(ps), &V_R8(pd));
366 case VT_UI8: return VarR8FromUI8(V_UI8(ps), &V_R8(pd));
367 case VT_R4: return VarR8FromR4(V_R4(ps), &V_R8(pd));
368 case VT_DATE: return VarR8FromDate(V_DATE(ps), &V_R8(pd));
369 case VT_BOOL: return VarR8FromBool(V_BOOL(ps), &V_R8(pd));
370 case VT_CY: return VarR8FromCy(V_CY(ps), &V_R8(pd));
371 case VT_DECIMAL: return VarR8FromDec(&V_DECIMAL(ps), &V_R8(pd));
372 case VT_DISPATCH: return VarR8FromDisp(V_DISPATCH(ps), lcid, &V_R8(pd));
373 case VT_BSTR: return VarR8FromStr(V_BSTR(ps), lcid, dwFlags, &V_R8(pd));
380 case VT_EMPTY: V_DATE(pd) = 0.0; return S_OK;
381 case VT_I1: return VarDateFromI1(V_I1(ps), &V_DATE(pd));
382 case VT_I2: return VarDateFromI2(V_I2(ps), &V_DATE(pd));
383 case VT_I4: return VarDateFromI4(V_I4(ps), &V_DATE(pd));
384 case VT_UI1: return VarDateFromUI1(V_UI1(ps), &V_DATE(pd));
385 case VT_UI2: return VarDateFromUI2(V_UI2(ps), &V_DATE(pd));
386 case VT_UI4: return VarDateFromUI4(V_UI4(ps), &V_DATE(pd));
387 case VT_I8: return VarDateFromI8(V_I8(ps), &V_DATE(pd));
388 case VT_UI8: return VarDateFromUI8(V_UI8(ps), &V_DATE(pd));
389 case VT_R4: return VarDateFromR4(V_R4(ps), &V_DATE(pd));
390 case VT_R8: return VarDateFromR8(V_R8(ps), &V_DATE(pd));
391 case VT_BOOL: return VarDateFromBool(V_BOOL(ps), &V_DATE(pd));
392 case VT_CY: return VarDateFromCy(V_CY(ps), &V_DATE(pd));
393 case VT_DECIMAL: return VarDateFromDec(&V_DECIMAL(ps), &V_DATE(pd));
394 case VT_DISPATCH: return VarDateFromDisp(V_DISPATCH(ps), lcid, &V_DATE(pd));
395 case VT_BSTR: return VarDateFromStr(V_BSTR(ps), lcid, dwFlags, &V_DATE(pd));
402 case VT_EMPTY: V_BOOL(pd) = 0; return S_OK;
403 case VT_I1: return VarBoolFromI1(V_I1(ps), &V_BOOL(pd));
404 case VT_I2: return VarBoolFromI2(V_I2(ps), &V_BOOL(pd));
405 case VT_I4: return VarBoolFromI4(V_I4(ps), &V_BOOL(pd));
406 case VT_UI1: return VarBoolFromUI1(V_UI1(ps), &V_BOOL(pd));
407 case VT_UI2: return VarBoolFromUI2(V_UI2(ps), &V_BOOL(pd));
408 case VT_UI4: return VarBoolFromUI4(V_UI4(ps), &V_BOOL(pd));
409 case VT_I8: return VarBoolFromI8(V_I8(ps), &V_BOOL(pd));
410 case VT_UI8: return VarBoolFromUI8(V_UI8(ps), &V_BOOL(pd));
411 case VT_R4: return VarBoolFromR4(V_R4(ps), &V_BOOL(pd));
412 case VT_R8: return VarBoolFromR8(V_R8(ps), &V_BOOL(pd));
413 case VT_DATE: return VarBoolFromDate(V_DATE(ps), &V_BOOL(pd));
414 case VT_CY: return VarBoolFromCy(V_CY(ps), &V_BOOL(pd));
415 case VT_DECIMAL: return VarBoolFromDec(&V_DECIMAL(ps), &V_BOOL(pd));
416 case VT_DISPATCH: return VarBoolFromDisp(V_DISPATCH(ps), lcid, &V_BOOL(pd));
417 case VT_BSTR: return VarBoolFromStr(V_BSTR(ps), lcid, dwFlags, &V_BOOL(pd));
425 V_BSTR(pd) = SysAllocStringLen(NULL, 0);
426 return V_BSTR(pd) ? S_OK : E_OUTOFMEMORY;
428 if (wFlags & (VARIANT_ALPHABOOL|VARIANT_LOCALBOOL))
429 return VarBstrFromBool(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
430 return VarBstrFromI2(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
431 case VT_I1: return VarBstrFromI1(V_I1(ps), lcid, dwFlags, &V_BSTR(pd));
432 case VT_I2: return VarBstrFromI2(V_I2(ps), lcid, dwFlags, &V_BSTR(pd));
433 case VT_I4: return VarBstrFromI4(V_I4(ps), lcid, dwFlags, &V_BSTR(pd));
434 case VT_UI1: return VarBstrFromUI1(V_UI1(ps), lcid, dwFlags, &V_BSTR(pd));
435 case VT_UI2: return VarBstrFromUI2(V_UI2(ps), lcid, dwFlags, &V_BSTR(pd));
436 case VT_UI4: return VarBstrFromUI4(V_UI4(ps), lcid, dwFlags, &V_BSTR(pd));
437 case VT_I8: return VarBstrFromI8(V_I8(ps), lcid, dwFlags, &V_BSTR(pd));
438 case VT_UI8: return VarBstrFromUI8(V_UI8(ps), lcid, dwFlags, &V_BSTR(pd));
439 case VT_R4: return VarBstrFromR4(V_R4(ps), lcid, dwFlags, &V_BSTR(pd));
440 case VT_R8: return VarBstrFromR8(V_R8(ps), lcid, dwFlags, &V_BSTR(pd));
441 case VT_DATE: return VarBstrFromDate(V_DATE(ps), lcid, dwFlags, &V_BSTR(pd));
442 case VT_CY: return VarBstrFromCy(V_CY(ps), lcid, dwFlags, &V_BSTR(pd));
443 case VT_DECIMAL: return VarBstrFromDec(&V_DECIMAL(ps), lcid, dwFlags, &V_BSTR(pd));
444 /* case VT_DISPATCH: return VarBstrFromDisp(V_DISPATCH(ps), lcid, dwFlags, &V_BSTR(pd)); */
451 case VT_EMPTY: V_CY(pd).int64 = 0; return S_OK;
452 case VT_I1: return VarCyFromI1(V_I1(ps), &V_CY(pd));
453 case VT_I2: return VarCyFromI2(V_I2(ps), &V_CY(pd));
454 case VT_I4: return VarCyFromI4(V_I4(ps), &V_CY(pd));
455 case VT_UI1: return VarCyFromUI1(V_UI1(ps), &V_CY(pd));
456 case VT_UI2: return VarCyFromUI2(V_UI2(ps), &V_CY(pd));
457 case VT_UI4: return VarCyFromUI4(V_UI4(ps), &V_CY(pd));
458 case VT_I8: return VarCyFromI8(V_I8(ps), &V_CY(pd));
459 case VT_UI8: return VarCyFromUI8(V_UI8(ps), &V_CY(pd));
460 case VT_R4: return VarCyFromR4(V_R4(ps), &V_CY(pd));
461 case VT_R8: return VarCyFromR8(V_R8(ps), &V_CY(pd));
462 case VT_DATE: return VarCyFromDate(V_DATE(ps), &V_CY(pd));
463 case VT_BOOL: return VarCyFromBool(V_BOOL(ps), &V_CY(pd));
464 case VT_DECIMAL: return VarCyFromDec(&V_DECIMAL(ps), &V_CY(pd));
465 case VT_DISPATCH: return VarCyFromDisp(V_DISPATCH(ps), lcid, &V_CY(pd));
466 case VT_BSTR: return VarCyFromStr(V_BSTR(ps), lcid, dwFlags, &V_CY(pd));
475 DEC_SIGNSCALE(&V_DECIMAL(pd)) = SIGNSCALE(DECIMAL_POS,0);
476 DEC_HI32(&V_DECIMAL(pd)) = 0;
477 DEC_MID32(&V_DECIMAL(pd)) = 0;
478 /* VarDecFromBool() coerces to -1/0, ChangeTypeEx() coerces to 1/0.
479 * VT_NULL and VT_EMPTY always give a 0 value.
481 DEC_LO32(&V_DECIMAL(pd)) = vtFrom == VT_BOOL && V_BOOL(ps) ? 1 : 0;
483 case VT_I1: return VarDecFromI1(V_I1(ps), &V_DECIMAL(pd));
484 case VT_I2: return VarDecFromI2(V_I2(ps), &V_DECIMAL(pd));
485 case VT_I4: return VarDecFromI4(V_I4(ps), &V_DECIMAL(pd));
486 case VT_UI1: return VarDecFromUI1(V_UI1(ps), &V_DECIMAL(pd));
487 case VT_UI2: return VarDecFromUI2(V_UI2(ps), &V_DECIMAL(pd));
488 case VT_UI4: return VarDecFromUI4(V_UI4(ps), &V_DECIMAL(pd));
489 case VT_I8: return VarDecFromI8(V_I8(ps), &V_DECIMAL(pd));
490 case VT_UI8: return VarDecFromUI8(V_UI8(ps), &V_DECIMAL(pd));
491 case VT_R4: return VarDecFromR4(V_R4(ps), &V_DECIMAL(pd));
492 case VT_R8: return VarDecFromR8(V_R8(ps), &V_DECIMAL(pd));
493 case VT_DATE: return VarDecFromDate(V_DATE(ps), &V_DECIMAL(pd));
494 case VT_CY: return VarDecFromCy(V_CY(pd), &V_DECIMAL(ps));
495 case VT_DISPATCH: return VarDecFromDisp(V_DISPATCH(ps), lcid, &V_DECIMAL(ps));
496 case VT_BSTR: return VarDecFromStr(V_BSTR(ps), lcid, dwFlags, &V_DECIMAL(pd));
504 if (V_DISPATCH(ps) == NULL)
505 V_UNKNOWN(pd) = NULL;
507 res = IDispatch_QueryInterface(V_DISPATCH(ps), &IID_IUnknown, (LPVOID*)&V_UNKNOWN(pd));
516 if (V_UNKNOWN(ps) == NULL)
517 V_DISPATCH(pd) = NULL;
519 res = IUnknown_QueryInterface(V_UNKNOWN(ps), &IID_IDispatch, (LPVOID*)&V_DISPATCH(pd));
530 /* Coerce to/from an array */
531 static inline HRESULT VARIANT_CoerceArray(VARIANTARG* pd, VARIANTARG* ps, VARTYPE vt)
533 if (vt == VT_BSTR && V_VT(ps) == (VT_ARRAY|VT_UI1))
534 return BstrFromVector(V_ARRAY(ps), &V_BSTR(pd));
536 if (V_VT(ps) == VT_BSTR && vt == (VT_ARRAY|VT_UI1))
537 return VectorFromBstr(V_BSTR(ps), &V_ARRAY(ps));
540 return SafeArrayCopy(V_ARRAY(ps), &V_ARRAY(pd));
542 return DISP_E_TYPEMISMATCH;
545 /******************************************************************************
546 * Check if a variants type is valid.
548 static inline HRESULT VARIANT_ValidateType(VARTYPE vt)
550 VARTYPE vtExtra = vt & VT_EXTRA_TYPE;
554 if (!(vtExtra & (VT_VECTOR|VT_RESERVED)))
556 if (vt < VT_VOID || vt == VT_RECORD || vt == VT_CLSID)
558 if ((vtExtra & (VT_BYREF|VT_ARRAY)) && vt <= VT_NULL)
559 return DISP_E_BADVARTYPE;
560 if (vt != (VARTYPE)15)
564 return DISP_E_BADVARTYPE;
567 /******************************************************************************
568 * VariantInit [OLEAUT32.8]
570 * Initialise a variant.
573 * pVarg [O] Variant to initialise
579 * This function simply sets the type of the variant to VT_EMPTY. It does not
580 * free any existing value, use VariantClear() for that.
582 void WINAPI VariantInit(VARIANTARG* pVarg)
584 TRACE("(%p)\n", pVarg);
586 V_VT(pVarg) = VT_EMPTY; /* Native doesn't set any other fields */
589 /******************************************************************************
590 * VariantClear [OLEAUT32.9]
595 * pVarg [I/O] Variant to clear
598 * Success: S_OK. Any previous value in pVarg is freed and its type is set to VT_EMPTY.
599 * Failure: DISP_E_BADVARTYPE, if the variant is a not a valid variant type.
601 HRESULT WINAPI VariantClear(VARIANTARG* pVarg)
605 TRACE("(%p->(%s%s))\n", pVarg, debugstr_VT(pVarg), debugstr_VF(pVarg));
607 hres = VARIANT_ValidateType(V_VT(pVarg));
611 if (!V_ISBYREF(pVarg))
613 if (V_ISARRAY(pVarg) || V_VT(pVarg) == VT_SAFEARRAY)
616 hres = SafeArrayDestroy(V_ARRAY(pVarg));
618 else if (V_VT(pVarg) == VT_BSTR)
621 SysFreeString(V_BSTR(pVarg));
623 else if (V_VT(pVarg) == VT_RECORD)
625 struct __tagBRECORD* pBr = &V_UNION(pVarg,brecVal);
628 IRecordInfo_RecordClear(pBr->pRecInfo, pBr->pvRecord);
629 IRecordInfo_Release(pBr->pRecInfo);
632 else if (V_VT(pVarg) == VT_DISPATCH ||
633 V_VT(pVarg) == VT_UNKNOWN)
635 if (V_UNKNOWN(pVarg))
636 IUnknown_Release(V_UNKNOWN(pVarg));
638 else if (V_VT(pVarg) == VT_VARIANT)
640 if (V_VARIANTREF(pVarg))
641 VariantClear(V_VARIANTREF(pVarg));
644 V_VT(pVarg) = VT_EMPTY;
649 /******************************************************************************
650 * Copy an IRecordInfo object contained in a variant.
652 static HRESULT VARIANT_CopyIRecordInfo(struct __tagBRECORD* pBr)
660 hres = IRecordInfo_GetSize(pBr->pRecInfo, &ulSize);
663 PVOID pvRecord = HeapAlloc(GetProcessHeap(), 0, ulSize);
665 hres = E_OUTOFMEMORY;
668 memcpy(pvRecord, pBr->pvRecord, ulSize);
669 pBr->pvRecord = pvRecord;
671 hres = IRecordInfo_RecordCopy(pBr->pRecInfo, pvRecord, pvRecord);
673 IRecordInfo_AddRef(pBr->pRecInfo);
677 else if (pBr->pvRecord)
682 /******************************************************************************
683 * VariantCopy [OLEAUT32.10]
688 * pvargDest [O] Destination for copy
689 * pvargSrc [I] Source variant to copy
692 * Success: S_OK. pvargDest contains a copy of pvargSrc.
693 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid type.
694 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
695 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
696 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
699 * - If pvargSrc == pvargDest, this function does nothing, and succeeds if
700 * pvargSrc is valid. Otherwise, pvargDest is always cleared using
701 * VariantClear() before pvargSrc is copied to it. If clearing pvargDest
702 * fails, so does this function.
703 * - VT_CLSID is a valid type type for pvargSrc, but not for pvargDest.
704 * - For by-value non-intrinsic types, a deep copy is made, i.e. The whole value
705 * is copied rather than just any pointers to it.
706 * - For by-value object types the object pointer is copied and the objects
707 * reference count increased using IUnknown_AddRef().
708 * - For all by-reference types, only the referencing pointer is copied.
710 HRESULT WINAPI VariantCopy(VARIANTARG* pvargDest, VARIANTARG* pvargSrc)
714 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
715 debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
716 debugstr_VF(pvargSrc));
718 if (V_TYPE(pvargSrc) == VT_CLSID || /* VT_CLSID is a special case */
719 FAILED(VARIANT_ValidateType(V_VT(pvargSrc))))
720 return DISP_E_BADVARTYPE;
722 if (pvargSrc != pvargDest &&
723 SUCCEEDED(hres = VariantClear(pvargDest)))
725 *pvargDest = *pvargSrc; /* Shallow copy the value */
727 if (!V_ISBYREF(pvargSrc))
729 if (V_ISARRAY(pvargSrc))
731 if (V_ARRAY(pvargSrc))
732 hres = SafeArrayCopy(V_ARRAY(pvargSrc), &V_ARRAY(pvargDest));
734 else if (V_VT(pvargSrc) == VT_BSTR)
736 if (V_BSTR(pvargSrc))
738 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)V_BSTR(pvargSrc), SysStringByteLen(V_BSTR(pvargSrc)));
739 if (!V_BSTR(pvargDest))
741 TRACE("!V_BSTR(pvargDest), SysAllocStringByteLen() failed to allocate %d bytes\n", SysStringByteLen(V_BSTR(pvargSrc)));
742 hres = E_OUTOFMEMORY;
746 else if (V_VT(pvargSrc) == VT_RECORD)
748 hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
750 else if (V_VT(pvargSrc) == VT_DISPATCH ||
751 V_VT(pvargSrc) == VT_UNKNOWN)
753 if (V_UNKNOWN(pvargSrc))
754 IUnknown_AddRef(V_UNKNOWN(pvargSrc));
761 /* Return the byte size of a variants data */
762 static inline size_t VARIANT_DataSize(const VARIANT* pv)
767 case VT_UI1: return sizeof(BYTE); break;
769 case VT_UI2: return sizeof(SHORT); break;
773 case VT_UI4: return sizeof(LONG); break;
775 case VT_UI8: return sizeof(LONGLONG); break;
776 case VT_R4: return sizeof(float); break;
777 case VT_R8: return sizeof(double); break;
778 case VT_DATE: return sizeof(DATE); break;
779 case VT_BOOL: return sizeof(VARIANT_BOOL); break;
782 case VT_BSTR: return sizeof(void*); break;
783 case VT_CY: return sizeof(CY); break;
784 case VT_ERROR: return sizeof(SCODE); break;
786 TRACE("Shouldn't be called for vt %s%s!\n", debugstr_VT(pv), debugstr_VF(pv));
790 /******************************************************************************
791 * VariantCopyInd [OLEAUT32.11]
793 * Copy a variant, dereferencing it it is by-reference.
796 * pvargDest [O] Destination for copy
797 * pvargSrc [I] Source variant to copy
800 * Success: S_OK. pvargDest contains a copy of pvargSrc.
801 * Failure: An HRESULT error code indicating the error.
804 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid by-value type.
805 * E_INVALIDARG, if pvargSrc is an invalid by-reference type.
806 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
807 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
808 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
811 * - If pvargSrc is by-value, this function behaves exactly as VariantCopy().
812 * - If pvargSrc is by-reference, the value copied to pvargDest is the pointed-to
814 * - if pvargSrc == pvargDest, this function dereferences in place. Otherwise,
815 * pvargDest is always cleared using VariantClear() before pvargSrc is copied
816 * to it. If clearing pvargDest fails, so does this function.
818 HRESULT WINAPI VariantCopyInd(VARIANT* pvargDest, VARIANTARG* pvargSrc)
820 VARIANTARG vTmp, *pSrc = pvargSrc;
824 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
825 debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
826 debugstr_VF(pvargSrc));
828 if (!V_ISBYREF(pvargSrc))
829 return VariantCopy(pvargDest, pvargSrc);
831 /* Argument checking is more lax than VariantCopy()... */
832 vt = V_TYPE(pvargSrc);
833 if (V_ISARRAY(pvargSrc) ||
834 (vt > VT_NULL && vt != (VARTYPE)15 && vt < VT_VOID &&
835 !(V_VT(pvargSrc) & (VT_VECTOR|VT_RESERVED))))
840 return E_INVALIDARG; /* ...And the return value for invalid types differs too */
842 if (pvargSrc == pvargDest)
844 /* In place copy. Use a shallow copy of pvargSrc & init pvargDest.
845 * This avoids an expensive VariantCopy() call - e.g. SafeArrayCopy().
849 V_VT(pvargDest) = VT_EMPTY;
853 /* Copy into another variant. Free the variant in pvargDest */
854 if (FAILED(hres = VariantClear(pvargDest)))
856 TRACE("VariantClear() of destination failed\n");
863 /* Native doesn't check that *V_ARRAYREF(pSrc) is valid */
864 hres = SafeArrayCopy(*V_ARRAYREF(pSrc), &V_ARRAY(pvargDest));
866 else if (V_VT(pSrc) == (VT_BSTR|VT_BYREF))
868 /* Native doesn't check that *V_BSTRREF(pSrc) is valid */
869 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)*V_BSTRREF(pSrc), SysStringByteLen(*V_BSTRREF(pSrc)));
871 else if (V_VT(pSrc) == (VT_RECORD|VT_BYREF))
873 V_UNION(pvargDest,brecVal) = V_UNION(pvargSrc,brecVal);
874 hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
876 else if (V_VT(pSrc) == (VT_DISPATCH|VT_BYREF) ||
877 V_VT(pSrc) == (VT_UNKNOWN|VT_BYREF))
879 /* Native doesn't check that *V_UNKNOWNREF(pSrc) is valid */
880 V_UNKNOWN(pvargDest) = *V_UNKNOWNREF(pSrc);
881 if (*V_UNKNOWNREF(pSrc))
882 IUnknown_AddRef(*V_UNKNOWNREF(pSrc));
884 else if (V_VT(pSrc) == (VT_VARIANT|VT_BYREF))
886 /* Native doesn't check that *V_VARIANTREF(pSrc) is valid */
887 if (V_VT(V_VARIANTREF(pSrc)) == (VT_VARIANT|VT_BYREF))
888 hres = E_INVALIDARG; /* Don't dereference more than one level */
890 hres = VariantCopyInd(pvargDest, V_VARIANTREF(pSrc));
892 /* Use the dereferenced variants type value, not VT_VARIANT */
893 goto VariantCopyInd_Return;
895 else if (V_VT(pSrc) == (VT_DECIMAL|VT_BYREF))
897 memcpy(&DEC_SCALE(&V_DECIMAL(pvargDest)), &DEC_SCALE(V_DECIMALREF(pSrc)),
898 sizeof(DECIMAL) - sizeof(USHORT));
902 /* Copy the pointed to data into this variant */
903 memcpy(&V_BYREF(pvargDest), V_BYREF(pSrc), VARIANT_DataSize(pSrc));
906 V_VT(pvargDest) = V_VT(pSrc) & ~VT_BYREF;
908 VariantCopyInd_Return:
910 if (pSrc != pvargSrc)
913 TRACE("returning 0x%08lx, %p->(%s%s)\n", hres, pvargDest,
914 debugstr_VT(pvargDest), debugstr_VF(pvargDest));
918 /******************************************************************************
919 * VariantChangeType [OLEAUT32.12]
921 * Change the type of a variant.
924 * pvargDest [O] Destination for the converted variant
925 * pvargSrc [O] Source variant to change the type of
926 * wFlags [I] VARIANT_ flags from "oleauto.h"
927 * vt [I] Variant type to change pvargSrc into
930 * Success: S_OK. pvargDest contains the converted value.
931 * Failure: An HRESULT error code describing the failure.
934 * The LCID used for the conversion is LOCALE_USER_DEFAULT.
935 * See VariantChangeTypeEx.
937 HRESULT WINAPI VariantChangeType(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
938 USHORT wFlags, VARTYPE vt)
940 return VariantChangeTypeEx( pvargDest, pvargSrc, LOCALE_USER_DEFAULT, wFlags, vt );
943 /******************************************************************************
944 * VariantChangeTypeEx [OLEAUT32.147]
946 * Change the type of a variant.
949 * pvargDest [O] Destination for the converted variant
950 * pvargSrc [O] Source variant to change the type of
951 * lcid [I] LCID for the conversion
952 * wFlags [I] VARIANT_ flags from "oleauto.h"
953 * vt [I] Variant type to change pvargSrc into
956 * Success: S_OK. pvargDest contains the converted value.
957 * Failure: An HRESULT error code describing the failure.
960 * pvargDest and pvargSrc can point to the same variant to perform an in-place
961 * conversion. If the conversion is successful, pvargSrc will be freed.
963 HRESULT WINAPI VariantChangeTypeEx(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
964 LCID lcid, USHORT wFlags, VARTYPE vt)
968 TRACE("(%p->(%s%s),%p->(%s%s),0x%08lx,0x%04x,%s%s)\n", pvargDest,
969 debugstr_VT(pvargDest), debugstr_VF(pvargDest), pvargSrc,
970 debugstr_VT(pvargSrc), debugstr_VF(pvargSrc), lcid, wFlags,
971 debugstr_vt(vt), debugstr_vf(vt));
974 res = DISP_E_BADVARTYPE;
977 res = VARIANT_ValidateType(V_VT(pvargSrc));
981 res = VARIANT_ValidateType(vt);
985 VARIANTARG vTmp, vSrcDeref;
987 if(V_VT(pvargSrc)&VT_BYREF && !V_BYREF(pvargSrc))
988 res = DISP_E_TYPEMISMATCH;
991 V_VT(&vTmp) = VT_EMPTY;
992 V_VT(&vSrcDeref) = VT_EMPTY;
994 VariantClear(&vSrcDeref);
999 res = VariantCopyInd(&vSrcDeref, pvargSrc);
1002 if (V_ISARRAY(&vSrcDeref) || (vt & VT_ARRAY))
1003 res = VARIANT_CoerceArray(&vTmp, &vSrcDeref, vt);
1005 res = VARIANT_Coerce(&vTmp, lcid, wFlags, &vSrcDeref, vt);
1007 if (SUCCEEDED(res)) {
1009 VariantCopy(pvargDest, &vTmp);
1011 VariantClear(&vTmp);
1012 VariantClear(&vSrcDeref);
1019 TRACE("returning 0x%08lx, %p->(%s%s)\n", res, pvargDest,
1020 debugstr_VT(pvargDest), debugstr_VF(pvargDest));
1024 /* Date Conversions */
1026 #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
1028 /* Convert a VT_DATE value to a Julian Date */
1029 static inline int VARIANT_JulianFromDate(int dateIn)
1031 int julianDays = dateIn;
1033 julianDays -= DATE_MIN; /* Convert to + days from 1 Jan 100 AD */
1034 julianDays += 1757585; /* Convert to + days from 23 Nov 4713 BC (Julian) */
1038 /* Convert a Julian Date to a VT_DATE value */
1039 static inline int VARIANT_DateFromJulian(int dateIn)
1041 int julianDays = dateIn;
1043 julianDays -= 1757585; /* Convert to + days from 1 Jan 100 AD */
1044 julianDays += DATE_MIN; /* Convert to +/- days from 1 Jan 1899 AD */
1048 /* Convert a Julian date to Day/Month/Year - from PostgreSQL */
1049 static inline void VARIANT_DMYFromJulian(int jd, USHORT *year, USHORT *month, USHORT *day)
1055 l -= (n * 146097 + 3) / 4;
1056 i = (4000 * (l + 1)) / 1461001;
1057 l += 31 - (i * 1461) / 4;
1058 j = (l * 80) / 2447;
1059 *day = l - (j * 2447) / 80;
1061 *month = (j + 2) - (12 * l);
1062 *year = 100 * (n - 49) + i + l;
1065 /* Convert Day/Month/Year to a Julian date - from PostgreSQL */
1066 static inline double VARIANT_JulianFromDMY(USHORT year, USHORT month, USHORT day)
1068 int m12 = (month - 14) / 12;
1070 return ((1461 * (year + 4800 + m12)) / 4 + (367 * (month - 2 - 12 * m12)) / 12 -
1071 (3 * ((year + 4900 + m12) / 100)) / 4 + day - 32075);
1074 /* Macros for accessing DOS format date/time fields */
1075 #define DOS_YEAR(x) (1980 + (x >> 9))
1076 #define DOS_MONTH(x) ((x >> 5) & 0xf)
1077 #define DOS_DAY(x) (x & 0x1f)
1078 #define DOS_HOUR(x) (x >> 11)
1079 #define DOS_MINUTE(x) ((x >> 5) & 0x3f)
1080 #define DOS_SECOND(x) ((x & 0x1f) << 1)
1081 /* Create a DOS format date/time */
1082 #define DOS_DATE(d,m,y) (d | (m << 5) | ((y-1980) << 9))
1083 #define DOS_TIME(h,m,s) ((s >> 1) | (m << 5) | (h << 11))
1085 /* Roll a date forwards or backwards to correct it */
1086 static HRESULT VARIANT_RollUdate(UDATE *lpUd)
1088 static const BYTE days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1090 TRACE("Raw date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
1091 lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
1093 /* Years < 100 are treated as 1900 + year */
1094 if (lpUd->st.wYear < 100)
1095 lpUd->st.wYear += 1900;
1097 if (!lpUd->st.wMonth)
1099 /* Roll back to December of the previous year */
1100 lpUd->st.wMonth = 12;
1103 else while (lpUd->st.wMonth > 12)
1105 /* Roll forward the correct number of months */
1107 lpUd->st.wMonth -= 12;
1110 if (lpUd->st.wYear > 9999 || lpUd->st.wHour > 23 ||
1111 lpUd->st.wMinute > 59 || lpUd->st.wSecond > 59)
1112 return E_INVALIDARG; /* Invalid values */
1116 /* Roll back the date one day */
1117 if (lpUd->st.wMonth == 1)
1119 /* Roll back to December 31 of the previous year */
1121 lpUd->st.wMonth = 12;
1126 lpUd->st.wMonth--; /* Previous month */
1127 if (lpUd->st.wMonth == 2 && IsLeapYear(lpUd->st.wYear))
1128 lpUd->st.wDay = 29; /* Februaury has 29 days on leap years */
1130 lpUd->st.wDay = days[lpUd->st.wMonth]; /* Last day of the month */
1133 else if (lpUd->st.wDay > 28)
1135 int rollForward = 0;
1137 /* Possibly need to roll the date forward */
1138 if (lpUd->st.wMonth == 2 && IsLeapYear(lpUd->st.wYear))
1139 rollForward = lpUd->st.wDay - 29; /* Februaury has 29 days on leap years */
1141 rollForward = lpUd->st.wDay - days[lpUd->st.wMonth];
1143 if (rollForward > 0)
1145 lpUd->st.wDay = rollForward;
1147 if (lpUd->st.wMonth > 12)
1149 lpUd->st.wMonth = 1; /* Roll forward into January of the next year */
1154 TRACE("Rolled date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
1155 lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
1159 /**********************************************************************
1160 * DosDateTimeToVariantTime [OLEAUT32.14]
1162 * Convert a Dos format date and time into variant VT_DATE format.
1165 * wDosDate [I] Dos format date
1166 * wDosTime [I] Dos format time
1167 * pDateOut [O] Destination for VT_DATE format
1170 * Success: TRUE. pDateOut contains the converted time.
1171 * Failure: FALSE, if wDosDate or wDosTime are invalid (see notes).
1174 * - Dos format dates can only hold dates from 1-Jan-1980 to 31-Dec-2099.
1175 * - Dos format times are accurate to only 2 second precision.
1176 * - The format of a Dos Date is:
1177 *| Bits Values Meaning
1178 *| ---- ------ -------
1179 *| 0-4 1-31 Day of the week. 0 rolls back one day. A value greater than
1180 *| the days in the month rolls forward the extra days.
1181 *| 5-8 1-12 Month of the year. 0 rolls back to December of the previous
1182 *| year. 13-15 are invalid.
1183 *| 9-15 0-119 Year based from 1980 (Max 2099). 120-127 are invalid.
1184 * - The format of a Dos Time is:
1185 *| Bits Values Meaning
1186 *| ---- ------ -------
1187 *| 0-4 0-29 Seconds/2. 30 and 31 are invalid.
1188 *| 5-10 0-59 Minutes. 60-63 are invalid.
1189 *| 11-15 0-23 Hours (24 hour clock). 24-32 are invalid.
1191 INT WINAPI DosDateTimeToVariantTime(USHORT wDosDate, USHORT wDosTime,
1196 TRACE("(0x%x(%d/%d/%d),0x%x(%d:%d:%d),%p)\n",
1197 wDosDate, DOS_YEAR(wDosDate), DOS_MONTH(wDosDate), DOS_DAY(wDosDate),
1198 wDosTime, DOS_HOUR(wDosTime), DOS_MINUTE(wDosTime), DOS_SECOND(wDosTime),
1201 ud.st.wYear = DOS_YEAR(wDosDate);
1202 ud.st.wMonth = DOS_MONTH(wDosDate);
1203 if (ud.st.wYear > 2099 || ud.st.wMonth > 12)
1205 ud.st.wDay = DOS_DAY(wDosDate);
1206 ud.st.wHour = DOS_HOUR(wDosTime);
1207 ud.st.wMinute = DOS_MINUTE(wDosTime);
1208 ud.st.wSecond = DOS_SECOND(wDosTime);
1209 ud.st.wDayOfWeek = ud.st.wMilliseconds = 0;
1211 return !VarDateFromUdate(&ud, 0, pDateOut);
1214 /**********************************************************************
1215 * VariantTimeToDosDateTime [OLEAUT32.13]
1217 * Convert a variant format date into a Dos format date and time.
1219 * dateIn [I] VT_DATE time format
1220 * pwDosDate [O] Destination for Dos format date
1221 * pwDosTime [O] Destination for Dos format time
1224 * Success: TRUE. pwDosDate and pwDosTime contains the converted values.
1225 * Failure: FALSE, if dateIn cannot be represented in Dos format.
1228 * See DosDateTimeToVariantTime() for Dos format details and bugs.
1230 INT WINAPI VariantTimeToDosDateTime(double dateIn, USHORT *pwDosDate, USHORT *pwDosTime)
1234 TRACE("(%g,%p,%p)\n", dateIn, pwDosDate, pwDosTime);
1236 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1239 if (ud.st.wYear < 1980 || ud.st.wYear > 2099)
1242 *pwDosDate = DOS_DATE(ud.st.wDay, ud.st.wMonth, ud.st.wYear);
1243 *pwDosTime = DOS_TIME(ud.st.wHour, ud.st.wMinute, ud.st.wSecond);
1245 TRACE("Returning 0x%x(%d/%d/%d), 0x%x(%d:%d:%d)\n",
1246 *pwDosDate, DOS_YEAR(*pwDosDate), DOS_MONTH(*pwDosDate), DOS_DAY(*pwDosDate),
1247 *pwDosTime, DOS_HOUR(*pwDosTime), DOS_MINUTE(*pwDosTime), DOS_SECOND(*pwDosTime));
1251 /***********************************************************************
1252 * SystemTimeToVariantTime [OLEAUT32.184]
1254 * Convert a System format date and time into variant VT_DATE format.
1257 * lpSt [I] System format date and time
1258 * pDateOut [O] Destination for VT_DATE format date
1261 * Success: TRUE. *pDateOut contains the converted value.
1262 * Failure: FALSE, if lpSt cannot be represented in VT_DATE format.
1264 INT WINAPI SystemTimeToVariantTime(LPSYSTEMTIME lpSt, double *pDateOut)
1268 TRACE("(%p->%d/%d/%d %d:%d:%d,%p)\n", lpSt, lpSt->wDay, lpSt->wMonth,
1269 lpSt->wYear, lpSt->wHour, lpSt->wMinute, lpSt->wSecond, pDateOut);
1271 if (lpSt->wMonth > 12)
1274 memcpy(&ud.st, lpSt, sizeof(ud.st));
1275 return !VarDateFromUdate(&ud, 0, pDateOut);
1278 /***********************************************************************
1279 * VariantTimeToSystemTime [OLEAUT32.185]
1281 * Convert a variant VT_DATE into a System format date and time.
1284 * datein [I] Variant VT_DATE format date
1285 * lpSt [O] Destination for System format date and time
1288 * Success: TRUE. *lpSt contains the converted value.
1289 * Failure: FALSE, if dateIn is too large or small.
1291 INT WINAPI VariantTimeToSystemTime(double dateIn, LPSYSTEMTIME lpSt)
1295 TRACE("(%g,%p)\n", dateIn, lpSt);
1297 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1300 memcpy(lpSt, &ud.st, sizeof(ud.st));
1304 /***********************************************************************
1305 * VarDateFromUdateEx [OLEAUT32.319]
1307 * Convert an unpacked format date and time to a variant VT_DATE.
1310 * pUdateIn [I] Unpacked format date and time to convert
1311 * lcid [I] Locale identifier for the conversion
1312 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1313 * pDateOut [O] Destination for variant VT_DATE.
1316 * Success: S_OK. *pDateOut contains the converted value.
1317 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1319 HRESULT WINAPI VarDateFromUdateEx(UDATE *pUdateIn, LCID lcid, ULONG dwFlags, DATE *pDateOut)
1324 TRACE("(%p->%d/%d/%d %d:%d:%d:%d %d %d,0x%08lx,0x%08lx,%p)\n", pUdateIn,
1325 pUdateIn->st.wMonth, pUdateIn->st.wDay, pUdateIn->st.wYear,
1326 pUdateIn->st.wHour, pUdateIn->st.wMinute, pUdateIn->st.wSecond,
1327 pUdateIn->st.wMilliseconds, pUdateIn->st.wDayOfWeek,
1328 pUdateIn->wDayOfYear, lcid, dwFlags, pDateOut);
1330 if (lcid != MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT))
1331 FIXME("lcid possibly not handled, treating as en-us\n");
1333 memcpy(&ud, pUdateIn, sizeof(ud));
1335 if (dwFlags & VAR_VALIDDATE)
1336 WARN("Ignoring VAR_VALIDDATE\n");
1338 if (FAILED(VARIANT_RollUdate(&ud)))
1339 return E_INVALIDARG;
1342 dateVal = VARIANT_DateFromJulian(VARIANT_JulianFromDMY(ud.st.wYear, ud.st.wMonth, ud.st.wDay));
1345 dateVal += ud.st.wHour / 24.0;
1346 dateVal += ud.st.wMinute / 1440.0;
1347 dateVal += ud.st.wSecond / 86400.0;
1348 dateVal += ud.st.wMilliseconds / 86400000.0;
1350 TRACE("Returning %g\n", dateVal);
1351 *pDateOut = dateVal;
1355 /***********************************************************************
1356 * VarDateFromUdate [OLEAUT32.330]
1358 * Convert an unpacked format date and time to a variant VT_DATE.
1361 * pUdateIn [I] Unpacked format date and time to convert
1362 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1363 * pDateOut [O] Destination for variant VT_DATE.
1366 * Success: S_OK. *pDateOut contains the converted value.
1367 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1370 * This function uses the United States English locale for the conversion. Use
1371 * VarDateFromUdateEx() for alternate locales.
1373 HRESULT WINAPI VarDateFromUdate(UDATE *pUdateIn, ULONG dwFlags, DATE *pDateOut)
1375 LCID lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
1377 return VarDateFromUdateEx(pUdateIn, lcid, dwFlags, pDateOut);
1380 /***********************************************************************
1381 * VarUdateFromDate [OLEAUT32.331]
1383 * Convert a variant VT_DATE into an unpacked format date and time.
1386 * datein [I] Variant VT_DATE format date
1387 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1388 * lpUdate [O] Destination for unpacked format date and time
1391 * Success: S_OK. *lpUdate contains the converted value.
1392 * Failure: E_INVALIDARG, if dateIn is too large or small.
1394 HRESULT WINAPI VarUdateFromDate(DATE dateIn, ULONG dwFlags, UDATE *lpUdate)
1396 /* Cumulative totals of days per month */
1397 static const USHORT cumulativeDays[] =
1399 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
1401 double datePart, timePart;
1404 TRACE("(%g,0x%08lx,%p)\n", dateIn, dwFlags, lpUdate);
1406 if (dateIn <= (DATE_MIN - 1.0) || dateIn >= (DATE_MAX + 1.0))
1407 return E_INVALIDARG;
1409 datePart = dateIn < 0.0 ? ceil(dateIn) : floor(dateIn);
1410 /* Compensate for int truncation (always downwards) */
1411 timePart = dateIn - datePart + 0.00000000001;
1412 if (timePart >= 1.0)
1413 timePart -= 0.00000000001;
1416 julianDays = VARIANT_JulianFromDate(dateIn);
1417 VARIANT_DMYFromJulian(julianDays, &lpUdate->st.wYear, &lpUdate->st.wMonth,
1420 datePart = (datePart + 1.5) / 7.0;
1421 lpUdate->st.wDayOfWeek = (datePart - floor(datePart)) * 7;
1422 if (lpUdate->st.wDayOfWeek == 0)
1423 lpUdate->st.wDayOfWeek = 5;
1424 else if (lpUdate->st.wDayOfWeek == 1)
1425 lpUdate->st.wDayOfWeek = 6;
1427 lpUdate->st.wDayOfWeek -= 2;
1429 if (lpUdate->st.wMonth > 2 && IsLeapYear(lpUdate->st.wYear))
1430 lpUdate->wDayOfYear = 1; /* After February, in a leap year */
1432 lpUdate->wDayOfYear = 0;
1434 lpUdate->wDayOfYear += cumulativeDays[lpUdate->st.wMonth];
1435 lpUdate->wDayOfYear += lpUdate->st.wDay;
1439 lpUdate->st.wHour = timePart;
1440 timePart -= lpUdate->st.wHour;
1442 lpUdate->st.wMinute = timePart;
1443 timePart -= lpUdate->st.wMinute;
1445 lpUdate->st.wSecond = timePart;
1446 timePart -= lpUdate->st.wSecond;
1447 lpUdate->st.wMilliseconds = 0;
1450 /* Round the milliseconds, adjusting the time/date forward if needed */
1451 if (lpUdate->st.wSecond < 59)
1452 lpUdate->st.wSecond++;
1455 lpUdate->st.wSecond = 0;
1456 if (lpUdate->st.wMinute < 59)
1457 lpUdate->st.wMinute++;
1460 lpUdate->st.wMinute = 0;
1461 if (lpUdate->st.wHour < 23)
1462 lpUdate->st.wHour++;
1465 lpUdate->st.wHour = 0;
1466 /* Roll over a whole day */
1467 if (++lpUdate->st.wDay > 28)
1468 VARIANT_RollUdate(lpUdate);
1476 #define GET_NUMBER_TEXT(fld,name) \
1478 if (!GetLocaleInfoW(lcid, lctype|fld, buff, 2)) \
1479 WARN("buffer too small for " #fld "\n"); \
1481 if (buff[0]) lpChars->name = buff[0]; \
1482 TRACE("lcid 0x%lx, " #name "=%d '%c'\n", lcid, lpChars->name, lpChars->name)
1484 /* Get the valid number characters for an lcid */
1485 void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS *lpChars, LCID lcid, DWORD dwFlags)
1487 static const VARIANT_NUMBER_CHARS defaultChars = { '-','+','.',',','$',0,'.',',' };
1488 LCTYPE lctype = dwFlags & LOCALE_NOUSEROVERRIDE;
1491 memcpy(lpChars, &defaultChars, sizeof(defaultChars));
1492 GET_NUMBER_TEXT(LOCALE_SNEGATIVESIGN, cNegativeSymbol);
1493 GET_NUMBER_TEXT(LOCALE_SPOSITIVESIGN, cPositiveSymbol);
1494 GET_NUMBER_TEXT(LOCALE_SDECIMAL, cDecimalPoint);
1495 GET_NUMBER_TEXT(LOCALE_STHOUSAND, cDigitSeperator);
1496 GET_NUMBER_TEXT(LOCALE_SMONDECIMALSEP, cCurrencyDecimalPoint);
1497 GET_NUMBER_TEXT(LOCALE_SMONTHOUSANDSEP, cCurrencyDigitSeperator);
1499 /* Local currency symbols are often 2 characters */
1500 lpChars->cCurrencyLocal2 = '\0';
1501 switch(GetLocaleInfoW(lcid, lctype|LOCALE_SCURRENCY, buff, sizeof(buff)/sizeof(WCHAR)))
1503 case 3: lpChars->cCurrencyLocal2 = buff[1]; /* Fall through */
1504 case 2: lpChars->cCurrencyLocal = buff[0];
1506 default: WARN("buffer too small for LOCALE_SCURRENCY\n");
1508 TRACE("lcid 0x%lx, cCurrencyLocal =%d,%d '%c','%c'\n", lcid, lpChars->cCurrencyLocal,
1509 lpChars->cCurrencyLocal2, lpChars->cCurrencyLocal, lpChars->cCurrencyLocal2);
1512 /* Number Parsing States */
1513 #define B_PROCESSING_EXPONENT 0x1
1514 #define B_NEGATIVE_EXPONENT 0x2
1515 #define B_EXPONENT_START 0x4
1516 #define B_INEXACT_ZEROS 0x8
1517 #define B_LEADING_ZERO 0x10
1518 #define B_PROCESSING_HEX 0x20
1519 #define B_PROCESSING_OCT 0x40
1521 /**********************************************************************
1522 * VarParseNumFromStr [OLEAUT32.46]
1524 * Parse a string containing a number into a NUMPARSE structure.
1527 * lpszStr [I] String to parse number from
1528 * lcid [I] Locale Id for the conversion
1529 * dwFlags [I] 0, or LOCALE_NOUSEROVERRIDE to use system default number chars
1530 * pNumprs [I/O] Destination for parsed number
1531 * rgbDig [O] Destination for digits read in
1534 * Success: S_OK. pNumprs and rgbDig contain the parsed representation of
1536 * Failure: E_INVALIDARG, if any parameter is invalid.
1537 * DISP_E_TYPEMISMATCH, if the string is not a number or is formatted
1539 * DISP_E_OVERFLOW, if rgbDig is too small to hold the number.
1542 * pNumprs must have the following fields set:
1543 * cDig: Set to the size of rgbDig.
1544 * dwInFlags: Set to the allowable syntax of the number using NUMPRS_ flags
1548 * - I am unsure if this function should parse non-arabic (e.g. Thai)
1549 * numerals, so this has not been implemented.
1551 HRESULT WINAPI VarParseNumFromStr(OLECHAR *lpszStr, LCID lcid, ULONG dwFlags,
1552 NUMPARSE *pNumprs, BYTE *rgbDig)
1554 VARIANT_NUMBER_CHARS chars;
1556 DWORD dwState = B_EXPONENT_START|B_INEXACT_ZEROS;
1557 int iMaxDigits = sizeof(rgbTmp) / sizeof(BYTE);
1560 TRACE("(%s,%ld,0x%08lx,%p,%p)\n", debugstr_w(lpszStr), lcid, dwFlags, pNumprs, rgbDig);
1562 if (!pNumprs || !rgbDig)
1563 return E_INVALIDARG;
1565 if (pNumprs->cDig < iMaxDigits)
1566 iMaxDigits = pNumprs->cDig;
1569 pNumprs->dwOutFlags = 0;
1570 pNumprs->cchUsed = 0;
1571 pNumprs->nBaseShift = 0;
1572 pNumprs->nPwr10 = 0;
1575 return DISP_E_TYPEMISMATCH;
1577 VARIANT_GetLocalisedNumberChars(&chars, lcid, dwFlags);
1579 /* First consume all the leading symbols and space from the string */
1582 if (pNumprs->dwInFlags & NUMPRS_LEADING_WHITE && isspaceW(*lpszStr))
1584 pNumprs->dwOutFlags |= NUMPRS_LEADING_WHITE;
1589 } while (isspaceW(*lpszStr));
1591 else if (pNumprs->dwInFlags & NUMPRS_LEADING_PLUS &&
1592 *lpszStr == chars.cPositiveSymbol &&
1593 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS))
1595 pNumprs->dwOutFlags |= NUMPRS_LEADING_PLUS;
1599 else if (pNumprs->dwInFlags & NUMPRS_LEADING_MINUS &&
1600 *lpszStr == chars.cNegativeSymbol &&
1601 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS))
1603 pNumprs->dwOutFlags |= (NUMPRS_LEADING_MINUS|NUMPRS_NEG);
1607 else if (pNumprs->dwInFlags & NUMPRS_CURRENCY &&
1608 !(pNumprs->dwOutFlags & NUMPRS_CURRENCY) &&
1609 *lpszStr == chars.cCurrencyLocal &&
1610 (!chars.cCurrencyLocal2 || lpszStr[1] == chars.cCurrencyLocal2))
1612 pNumprs->dwOutFlags |= NUMPRS_CURRENCY;
1615 /* Only accept currency characters */
1616 chars.cDecimalPoint = chars.cCurrencyDecimalPoint;
1617 chars.cDigitSeperator = chars.cCurrencyDigitSeperator;
1619 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == '(' &&
1620 !(pNumprs->dwOutFlags & NUMPRS_PARENS))
1622 pNumprs->dwOutFlags |= NUMPRS_PARENS;
1630 if (!(pNumprs->dwOutFlags & NUMPRS_CURRENCY))
1632 /* Only accept non-currency characters */
1633 chars.cCurrencyDecimalPoint = chars.cDecimalPoint;
1634 chars.cCurrencyDigitSeperator = chars.cDigitSeperator;
1637 if ((*lpszStr == '&' && (*(lpszStr+1) == 'H' || *(lpszStr+1) == 'h')) &&
1638 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1640 dwState |= B_PROCESSING_HEX;
1641 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1645 else if ((*lpszStr == '&' && (*(lpszStr+1) == 'O' || *(lpszStr+1) == 'o')) &&
1646 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1648 dwState |= B_PROCESSING_OCT;
1649 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1654 /* Strip Leading zeros */
1655 while (*lpszStr == '0')
1657 dwState |= B_LEADING_ZERO;
1664 if (isdigitW(*lpszStr))
1666 if (dwState & B_PROCESSING_EXPONENT)
1668 int exponentSize = 0;
1669 if (dwState & B_EXPONENT_START)
1671 if (!isdigitW(*lpszStr))
1672 break; /* No exponent digits - invalid */
1673 while (*lpszStr == '0')
1675 /* Skip leading zero's in the exponent */
1681 while (isdigitW(*lpszStr))
1684 exponentSize += *lpszStr - '0';
1688 if (dwState & B_NEGATIVE_EXPONENT)
1689 exponentSize = -exponentSize;
1690 /* Add the exponent into the powers of 10 */
1691 pNumprs->nPwr10 += exponentSize;
1692 dwState &= ~(B_PROCESSING_EXPONENT|B_EXPONENT_START);
1693 lpszStr--; /* back up to allow processing of next char */
1697 if ((pNumprs->cDig >= iMaxDigits) && !(dwState & B_PROCESSING_HEX)
1698 && !(dwState & B_PROCESSING_OCT))
1700 pNumprs->dwOutFlags |= NUMPRS_INEXACT;
1702 if (*lpszStr != '0')
1703 dwState &= ~B_INEXACT_ZEROS; /* Inexact number with non-trailing zeros */
1705 /* This digit can't be represented, but count it in nPwr10 */
1706 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1713 if ((dwState & B_PROCESSING_OCT) && ((*lpszStr == '8') || (*lpszStr == '9'))) {
1714 return DISP_E_TYPEMISMATCH;
1717 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1718 pNumprs->nPwr10--; /* Count decimal points in nPwr10 */
1720 rgbTmp[pNumprs->cDig] = *lpszStr - '0';
1726 else if (*lpszStr == chars.cDigitSeperator && pNumprs->dwInFlags & NUMPRS_THOUSANDS)
1728 pNumprs->dwOutFlags |= NUMPRS_THOUSANDS;
1731 else if (*lpszStr == chars.cDecimalPoint &&
1732 pNumprs->dwInFlags & NUMPRS_DECIMAL &&
1733 !(pNumprs->dwOutFlags & (NUMPRS_DECIMAL|NUMPRS_EXPONENT)))
1735 pNumprs->dwOutFlags |= NUMPRS_DECIMAL;
1738 /* If we have no digits so far, skip leading zeros */
1741 while (lpszStr[1] == '0')
1743 dwState |= B_LEADING_ZERO;
1750 else if ((*lpszStr == 'e' || *lpszStr == 'E') &&
1751 pNumprs->dwInFlags & NUMPRS_EXPONENT &&
1752 !(pNumprs->dwOutFlags & NUMPRS_EXPONENT))
1754 dwState |= B_PROCESSING_EXPONENT;
1755 pNumprs->dwOutFlags |= NUMPRS_EXPONENT;
1758 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cPositiveSymbol)
1760 cchUsed++; /* Ignore positive exponent */
1762 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cNegativeSymbol)
1764 dwState |= B_NEGATIVE_EXPONENT;
1767 else if (((*lpszStr >= 'a' && *lpszStr <= 'f') ||
1768 (*lpszStr >= 'A' && *lpszStr <= 'F')) &&
1769 dwState & B_PROCESSING_HEX)
1771 if (pNumprs->cDig >= iMaxDigits)
1773 return DISP_E_OVERFLOW;
1777 if (*lpszStr >= 'a')
1778 rgbTmp[pNumprs->cDig] = *lpszStr - 'a' + 10;
1780 rgbTmp[pNumprs->cDig] = *lpszStr - 'A' + 10;
1786 break; /* Stop at an unrecognised character */
1791 if (!pNumprs->cDig && dwState & B_LEADING_ZERO)
1793 /* Ensure a 0 on its own gets stored */
1798 if (pNumprs->dwOutFlags & NUMPRS_EXPONENT && dwState & B_PROCESSING_EXPONENT)
1800 pNumprs->cchUsed = cchUsed;
1801 return DISP_E_TYPEMISMATCH; /* Failed to completely parse the exponent */
1804 if (pNumprs->dwOutFlags & NUMPRS_INEXACT)
1806 if (dwState & B_INEXACT_ZEROS)
1807 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* All zeros doesn't set NUMPRS_INEXACT */
1808 } else if(pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1810 /* copy all of the digits into the output digit buffer */
1811 /* this is exactly what windows does although it also returns */
1812 /* cDig of X and writes X+Y where Y>=0 number of digits to rgbDig */
1813 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1815 if (dwState & B_PROCESSING_HEX) {
1816 /* hex numbers have always the same format */
1818 pNumprs->nBaseShift=4;
1820 if (dwState & B_PROCESSING_OCT) {
1821 /* oct numbers have always the same format */
1823 pNumprs->nBaseShift=3;
1825 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1834 /* Remove trailing zeros from the last (whole number or decimal) part */
1835 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1842 if (pNumprs->cDig <= iMaxDigits)
1843 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* Ignore stripped zeros for NUMPRS_INEXACT */
1845 pNumprs->cDig = iMaxDigits; /* Only return iMaxDigits worth of digits */
1847 /* Copy the digits we processed into rgbDig */
1848 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1850 /* Consume any trailing symbols and space */
1853 if ((pNumprs->dwInFlags & NUMPRS_TRAILING_WHITE) && isspaceW(*lpszStr))
1855 pNumprs->dwOutFlags |= NUMPRS_TRAILING_WHITE;
1860 } while (isspaceW(*lpszStr));
1862 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_PLUS &&
1863 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS) &&
1864 *lpszStr == chars.cPositiveSymbol)
1866 pNumprs->dwOutFlags |= NUMPRS_TRAILING_PLUS;
1870 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_MINUS &&
1871 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS) &&
1872 *lpszStr == chars.cNegativeSymbol)
1874 pNumprs->dwOutFlags |= (NUMPRS_TRAILING_MINUS|NUMPRS_NEG);
1878 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == ')' &&
1879 pNumprs->dwOutFlags & NUMPRS_PARENS)
1883 pNumprs->dwOutFlags |= NUMPRS_NEG;
1889 if (pNumprs->dwOutFlags & NUMPRS_PARENS && !(pNumprs->dwOutFlags & NUMPRS_NEG))
1891 pNumprs->cchUsed = cchUsed;
1892 return DISP_E_TYPEMISMATCH; /* Opening parenthesis not matched */
1895 if (pNumprs->dwInFlags & NUMPRS_USE_ALL && *lpszStr != '\0')
1896 return DISP_E_TYPEMISMATCH; /* Not all chars were consumed */
1899 return DISP_E_TYPEMISMATCH; /* No Number found */
1901 pNumprs->cchUsed = cchUsed;
1905 /* VTBIT flags indicating an integer value */
1906 #define INTEGER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8)
1907 /* VTBIT flags indicating a real number value */
1908 #define REAL_VTBITS (VTBIT_R4|VTBIT_R8|VTBIT_CY)
1910 /**********************************************************************
1911 * VarNumFromParseNum [OLEAUT32.47]
1913 * Convert a NUMPARSE structure into a numeric Variant type.
1916 * pNumprs [I] Source for parsed number. cDig must be set to the size of rgbDig
1917 * rgbDig [I] Source for the numbers digits
1918 * dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
1919 * pVarDst [O] Destination for the converted Variant value.
1922 * Success: S_OK. pVarDst contains the converted value.
1923 * Failure: E_INVALIDARG, if any parameter is invalid.
1924 * DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
1927 * - The smallest favoured type present in dwVtBits that can represent the
1928 * number in pNumprs without losing precision is used.
1929 * - Signed types are preferrred over unsigned types of the same size.
1930 * - Preferred types in order are: integer, float, double, currency then decimal.
1931 * - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
1932 * for details of the rounding method.
1933 * - pVarDst is not cleared before the result is stored in it.
1935 HRESULT WINAPI VarNumFromParseNum(NUMPARSE *pNumprs, BYTE *rgbDig,
1936 ULONG dwVtBits, VARIANT *pVarDst)
1938 /* Scale factors and limits for double arithmetic */
1939 static const double dblMultipliers[11] = {
1940 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
1941 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
1943 static const double dblMinimums[11] = {
1944 R8_MIN, R8_MIN*10.0, R8_MIN*100.0, R8_MIN*1000.0, R8_MIN*10000.0,
1945 R8_MIN*100000.0, R8_MIN*1000000.0, R8_MIN*10000000.0,
1946 R8_MIN*100000000.0, R8_MIN*1000000000.0, R8_MIN*10000000000.0
1948 static const double dblMaximums[11] = {
1949 R8_MAX, R8_MAX/10.0, R8_MAX/100.0, R8_MAX/1000.0, R8_MAX/10000.0,
1950 R8_MAX/100000.0, R8_MAX/1000000.0, R8_MAX/10000000.0,
1951 R8_MAX/100000000.0, R8_MAX/1000000000.0, R8_MAX/10000000000.0
1954 int wholeNumberDigits, fractionalDigits, divisor10 = 0, multiplier10 = 0;
1956 TRACE("(%p,%p,0x%lx,%p)\n", pNumprs, rgbDig, dwVtBits, pVarDst);
1958 if (pNumprs->nBaseShift)
1960 /* nBaseShift indicates a hex or octal number */
1965 /* Convert the hex or octal number string into a UI64 */
1966 for (i = 0; i < pNumprs->cDig; i++)
1968 if (ul64 > ((UI8_MAX>>pNumprs->nBaseShift) - rgbDig[i]))
1970 TRACE("Overflow multiplying digits\n");
1971 return DISP_E_OVERFLOW;
1973 ul64 = (ul64<<pNumprs->nBaseShift) + rgbDig[i];
1976 /* also make a negative representation */
1979 /* Try signed and unsigned types in size order */
1980 if (dwVtBits & VTBIT_I1 && ((ul64 <= I1_MAX)||(l64 >= I1_MIN)))
1982 V_VT(pVarDst) = VT_I1;
1984 V_I1(pVarDst) = ul64;
1986 V_I1(pVarDst) = l64;
1989 else if (dwVtBits & VTBIT_UI1 && ul64 <= UI1_MAX)
1991 V_VT(pVarDst) = VT_UI1;
1992 V_UI1(pVarDst) = ul64;
1995 else if (dwVtBits & VTBIT_I2 && ((ul64 <= I2_MAX)||(l64 >= I2_MIN)))
1997 V_VT(pVarDst) = VT_I2;
1999 V_I2(pVarDst) = ul64;
2001 V_I2(pVarDst) = l64;
2004 else if (dwVtBits & VTBIT_UI2 && ul64 <= UI2_MAX)
2006 V_VT(pVarDst) = VT_UI2;
2007 V_UI2(pVarDst) = ul64;
2010 else if (dwVtBits & VTBIT_I4 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2012 V_VT(pVarDst) = VT_I4;
2014 V_I4(pVarDst) = ul64;
2016 V_I4(pVarDst) = l64;
2019 else if (dwVtBits & VTBIT_UI4 && ul64 <= UI4_MAX)
2021 V_VT(pVarDst) = VT_UI4;
2022 V_UI4(pVarDst) = ul64;
2025 else if (dwVtBits & VTBIT_I8 && ((ul64 <= I4_MAX)||(l64>=I4_MIN)))
2027 V_VT(pVarDst) = VT_I8;
2028 V_I8(pVarDst) = ul64;
2031 else if (dwVtBits & VTBIT_UI8)
2033 V_VT(pVarDst) = VT_UI8;
2034 V_UI8(pVarDst) = ul64;
2037 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2039 V_VT(pVarDst) = VT_DECIMAL;
2040 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2041 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2042 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2045 else if (dwVtBits & VTBIT_R4 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2047 V_VT(pVarDst) = VT_R4;
2049 V_R4(pVarDst) = ul64;
2051 V_R4(pVarDst) = l64;
2054 else if (dwVtBits & VTBIT_R8 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2056 V_VT(pVarDst) = VT_R8;
2058 V_R8(pVarDst) = ul64;
2060 V_R8(pVarDst) = l64;
2064 TRACE("Overflow: possible return types: 0x%lx, value: %s\n", dwVtBits, wine_dbgstr_longlong(ul64));
2065 return DISP_E_OVERFLOW;
2068 /* Count the number of relevant fractional and whole digits stored,
2069 * And compute the divisor/multiplier to scale the number by.
2071 if (pNumprs->nPwr10 < 0)
2073 if (-pNumprs->nPwr10 >= pNumprs->cDig)
2075 /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
2076 wholeNumberDigits = 0;
2077 fractionalDigits = pNumprs->cDig;
2078 divisor10 = -pNumprs->nPwr10;
2082 /* An exactly represented real number e.g. 1.024 */
2083 wholeNumberDigits = pNumprs->cDig + pNumprs->nPwr10;
2084 fractionalDigits = pNumprs->cDig - wholeNumberDigits;
2085 divisor10 = pNumprs->cDig - wholeNumberDigits;
2088 else if (pNumprs->nPwr10 == 0)
2090 /* An exactly represented whole number e.g. 1024 */
2091 wholeNumberDigits = pNumprs->cDig;
2092 fractionalDigits = 0;
2094 else /* pNumprs->nPwr10 > 0 */
2096 /* A whole number followed by nPwr10 0's e.g. 102400 */
2097 wholeNumberDigits = pNumprs->cDig;
2098 fractionalDigits = 0;
2099 multiplier10 = pNumprs->nPwr10;
2102 TRACE("cDig %d; nPwr10 %d, whole %d, frac %d ", pNumprs->cDig,
2103 pNumprs->nPwr10, wholeNumberDigits, fractionalDigits);
2104 TRACE("mult %d; div %d\n", multiplier10, divisor10);
2106 if (dwVtBits & (INTEGER_VTBITS|VTBIT_DECIMAL) &&
2107 (!fractionalDigits || !(dwVtBits & (REAL_VTBITS|VTBIT_CY|VTBIT_DECIMAL))))
2109 /* We have one or more integer output choices, and either:
2110 * 1) An integer input value, or
2111 * 2) A real number input value but no floating output choices.
2112 * Alternately, we have a DECIMAL output available and an integer input.
2114 * So, place the integer value into pVarDst, using the smallest type
2115 * possible and preferring signed over unsigned types.
2117 BOOL bOverflow = FALSE, bNegative;
2121 /* Convert the integer part of the number into a UI8 */
2122 for (i = 0; i < wholeNumberDigits; i++)
2124 if (ul64 > (UI8_MAX / 10 - rgbDig[i]))
2126 TRACE("Overflow multiplying digits\n");
2130 ul64 = ul64 * 10 + rgbDig[i];
2133 /* Account for the scale of the number */
2134 if (!bOverflow && multiplier10)
2136 for (i = 0; i < multiplier10; i++)
2138 if (ul64 > (UI8_MAX / 10))
2140 TRACE("Overflow scaling number\n");
2148 /* If we have any fractional digits, round the value.
2149 * Note we don't have to do this if divisor10 is < 1,
2150 * because this means the fractional part must be < 0.5
2152 if (!bOverflow && fractionalDigits && divisor10 > 0)
2154 const BYTE* fracDig = rgbDig + wholeNumberDigits;
2155 BOOL bAdjust = FALSE;
2157 TRACE("first decimal value is %d\n", *fracDig);
2160 bAdjust = TRUE; /* > 0.5 */
2161 else if (*fracDig == 5)
2163 for (i = 1; i < fractionalDigits; i++)
2167 bAdjust = TRUE; /* > 0.5 */
2171 /* If exactly 0.5, round only odd values */
2172 if (i == fractionalDigits && (ul64 & 1))
2178 if (ul64 == UI8_MAX)
2180 TRACE("Overflow after rounding\n");
2187 /* Zero is not a negative number */
2188 bNegative = pNumprs->dwOutFlags & NUMPRS_NEG && ul64 ? TRUE : FALSE;
2190 TRACE("Integer value is %lld, bNeg %d\n", ul64, bNegative);
2192 /* For negative integers, try the signed types in size order */
2193 if (!bOverflow && bNegative)
2195 if (dwVtBits & (VTBIT_I1|VTBIT_I2|VTBIT_I4|VTBIT_I8))
2197 if (dwVtBits & VTBIT_I1 && ul64 <= -I1_MIN)
2199 V_VT(pVarDst) = VT_I1;
2200 V_I1(pVarDst) = -ul64;
2203 else if (dwVtBits & VTBIT_I2 && ul64 <= -I2_MIN)
2205 V_VT(pVarDst) = VT_I2;
2206 V_I2(pVarDst) = -ul64;
2209 else if (dwVtBits & VTBIT_I4 && ul64 <= -((LONGLONG)I4_MIN))
2211 V_VT(pVarDst) = VT_I4;
2212 V_I4(pVarDst) = -ul64;
2215 else if (dwVtBits & VTBIT_I8 && ul64 <= (ULONGLONG)I8_MAX + 1)
2217 V_VT(pVarDst) = VT_I8;
2218 V_I8(pVarDst) = -ul64;
2221 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2223 /* Decimal is only output choice left - fast path */
2224 V_VT(pVarDst) = VT_DECIMAL;
2225 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_NEG,0);
2226 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2227 DEC_LO64(&V_DECIMAL(pVarDst)) = -ul64;
2232 else if (!bOverflow)
2234 /* For positive integers, try signed then unsigned types in size order */
2235 if (dwVtBits & VTBIT_I1 && ul64 <= I1_MAX)
2237 V_VT(pVarDst) = VT_I1;
2238 V_I1(pVarDst) = ul64;
2241 else if (dwVtBits & VTBIT_UI1 && ul64 <= UI1_MAX)
2243 V_VT(pVarDst) = VT_UI1;
2244 V_UI1(pVarDst) = ul64;
2247 else if (dwVtBits & VTBIT_I2 && ul64 <= I2_MAX)
2249 V_VT(pVarDst) = VT_I2;
2250 V_I2(pVarDst) = ul64;
2253 else if (dwVtBits & VTBIT_UI2 && ul64 <= UI2_MAX)
2255 V_VT(pVarDst) = VT_UI2;
2256 V_UI2(pVarDst) = ul64;
2259 else if (dwVtBits & VTBIT_I4 && ul64 <= I4_MAX)
2261 V_VT(pVarDst) = VT_I4;
2262 V_I4(pVarDst) = ul64;
2265 else if (dwVtBits & VTBIT_UI4 && ul64 <= UI4_MAX)
2267 V_VT(pVarDst) = VT_UI4;
2268 V_UI4(pVarDst) = ul64;
2271 else if (dwVtBits & VTBIT_I8 && ul64 <= I8_MAX)
2273 V_VT(pVarDst) = VT_I8;
2274 V_I8(pVarDst) = ul64;
2277 else if (dwVtBits & VTBIT_UI8)
2279 V_VT(pVarDst) = VT_UI8;
2280 V_UI8(pVarDst) = ul64;
2283 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2285 /* Decimal is only output choice left - fast path */
2286 V_VT(pVarDst) = VT_DECIMAL;
2287 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2288 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2289 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2295 if (dwVtBits & REAL_VTBITS)
2297 /* Try to put the number into a float or real */
2298 BOOL bOverflow = FALSE, bNegative = pNumprs->dwOutFlags & NUMPRS_NEG;
2302 /* Convert the number into a double */
2303 for (i = 0; i < pNumprs->cDig; i++)
2304 whole = whole * 10.0 + rgbDig[i];
2306 TRACE("Whole double value is %16.16g\n", whole);
2308 /* Account for the scale */
2309 while (multiplier10 > 10)
2311 if (whole > dblMaximums[10])
2313 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2317 whole = whole * dblMultipliers[10];
2322 if (whole > dblMaximums[multiplier10])
2324 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2328 whole = whole * dblMultipliers[multiplier10];
2331 TRACE("Scaled double value is %16.16g\n", whole);
2333 while (divisor10 > 10)
2335 if (whole < dblMinimums[10] && whole != 0)
2337 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2341 whole = whole / dblMultipliers[10];
2346 if (whole < dblMinimums[divisor10] && whole != 0)
2348 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2352 whole = whole / dblMultipliers[divisor10];
2355 TRACE("Final double value is %16.16g\n", whole);
2357 if (dwVtBits & VTBIT_R4 &&
2358 ((whole <= R4_MAX && whole >= R4_MIN) || whole == 0.0))
2360 TRACE("Set R4 to final value\n");
2361 V_VT(pVarDst) = VT_R4; /* Fits into a float */
2362 V_R4(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2366 if (dwVtBits & VTBIT_R8)
2368 TRACE("Set R8 to final value\n");
2369 V_VT(pVarDst) = VT_R8; /* Fits into a double */
2370 V_R8(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2374 if (dwVtBits & VTBIT_CY)
2376 if (SUCCEEDED(VarCyFromR8(bNegative ? -whole : whole, &V_CY(pVarDst))))
2378 V_VT(pVarDst) = VT_CY; /* Fits into a currency */
2379 TRACE("Set CY to final value\n");
2382 TRACE("Value Overflows CY\n");
2386 if (dwVtBits & VTBIT_DECIMAL)
2391 DECIMAL* pDec = &V_DECIMAL(pVarDst);
2393 DECIMAL_SETZERO(pDec);
2396 if (pNumprs->dwOutFlags & NUMPRS_NEG)
2397 DEC_SIGN(pDec) = DECIMAL_NEG;
2399 DEC_SIGN(pDec) = DECIMAL_POS;
2401 /* Factor the significant digits */
2402 for (i = 0; i < pNumprs->cDig; i++)
2404 tmp = (ULONG64)DEC_LO32(pDec) * 10 + rgbDig[i];
2405 carry = (ULONG)(tmp >> 32);
2406 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2407 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2408 carry = (ULONG)(tmp >> 32);
2409 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2410 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2411 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2413 if (tmp >> 32 & UI4_MAX)
2415 VarNumFromParseNum_DecOverflow:
2416 TRACE("Overflow\n");
2417 DEC_LO32(pDec) = DEC_MID32(pDec) = DEC_HI32(pDec) = UI4_MAX;
2418 return DISP_E_OVERFLOW;
2422 /* Account for the scale of the number */
2423 while (multiplier10 > 0)
2425 tmp = (ULONG64)DEC_LO32(pDec) * 10;
2426 carry = (ULONG)(tmp >> 32);
2427 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2428 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2429 carry = (ULONG)(tmp >> 32);
2430 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2431 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2432 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2434 if (tmp >> 32 & UI4_MAX)
2435 goto VarNumFromParseNum_DecOverflow;
2438 DEC_SCALE(pDec) = divisor10;
2440 V_VT(pVarDst) = VT_DECIMAL;
2443 return DISP_E_OVERFLOW; /* No more output choices */
2446 /**********************************************************************
2447 * VarCat [OLEAUT32.318]
2449 HRESULT WINAPI VarCat(LPVARIANT left, LPVARIANT right, LPVARIANT out)
2451 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2452 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), out);
2454 /* Should we VariantClear out? */
2455 /* Can we handle array, vector, by ref etc. */
2456 if ((V_VT(left)&VT_TYPEMASK) == VT_NULL &&
2457 (V_VT(right)&VT_TYPEMASK) == VT_NULL)
2459 V_VT(out) = VT_NULL;
2463 if (V_VT(left) == VT_BSTR && V_VT(right) == VT_BSTR)
2465 V_VT(out) = VT_BSTR;
2466 VarBstrCat (V_BSTR(left), V_BSTR(right), &V_BSTR(out));
2469 if (V_VT(left) == VT_BSTR) {
2473 V_VT(out) = VT_BSTR;
2474 hres = VariantChangeTypeEx(&bstrvar,right,0,0,VT_BSTR);
2476 FIXME("Failed to convert right side from vt %d to VT_BSTR?\n",V_VT(right));
2479 VarBstrCat (V_BSTR(left), V_BSTR(&bstrvar), &V_BSTR(out));
2482 if (V_VT(right) == VT_BSTR) {
2486 V_VT(out) = VT_BSTR;
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 & (1<<VT_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 & (1<<VT_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_UNION(left,cVal); break;
2564 case VT_I2 : lVal = V_UNION(left,iVal); break;
2565 case VT_I4 : lVal = V_UNION(left,lVal); break;
2566 case VT_INT : lVal = V_UNION(left,lVal); break;
2567 case VT_UI1 : lVal = V_UNION(left,bVal); break;
2568 case VT_UI2 : lVal = V_UNION(left,uiVal); break;
2569 case VT_UI4 : lVal = V_UNION(left,ulVal); break;
2570 case VT_UINT : lVal = V_UNION(left,ulVal); break;
2571 case VT_BOOL : lVal = V_UNION(left,boolVal); break;
2572 default: lOk = FALSE;
2576 switch (V_VT(right)&VT_TYPEMASK) {
2577 case VT_I1 : rVal = V_UNION(right,cVal); break;
2578 case VT_I2 : rVal = V_UNION(right,iVal); break;
2579 case VT_I4 : rVal = V_UNION(right,lVal); break;
2580 case VT_INT : rVal = V_UNION(right,lVal); break;
2581 case VT_UI1 : rVal = V_UNION(right,bVal); break;
2582 case VT_UI2 : rVal = V_UNION(right,uiVal); break;
2583 case VT_UI4 : rVal = V_UNION(right,ulVal); break;
2584 case VT_UINT : rVal = V_UNION(right,ulVal); break;
2585 case VT_BOOL : rVal = V_UNION(right,boolVal); 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_UNION(left,date)) == floor(V_UNION(right,date))) {
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_UNION(left,date) );
2611 if (wholePart == 0) wholePart = 1;
2612 leftR = floor(fmod( V_UNION(left,date), wholePart ) * (24*60*60));
2614 wholePart = (double) floor( V_UNION(right,date) );
2615 if (wholePart == 0) wholePart = 1;
2616 rightR = floor(fmod( V_UNION(right,date), wholePart ) * (24*60*60));
2618 if (leftR < rightR) {
2620 } else if (leftR > rightR) {
2626 } else if (V_UNION(left,date) < V_UNION(right,date)) {
2628 } else if (V_UNION(left,date) > V_UNION(right,date)) {
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_UNION(left,cVal); resT=VT_I4; break;
2671 case VT_I2 : lVal = V_UNION(left,iVal); resT=VT_I2; break;
2672 case VT_I4 : lVal = V_UNION(left,lVal); resT=VT_I4; break;
2673 case VT_INT : lVal = V_UNION(left,lVal); resT=VT_I4; break;
2674 case VT_UI1 : lVal = V_UNION(left,bVal); resT=VT_I4; break;
2675 case VT_UI2 : lVal = V_UNION(left,uiVal); resT=VT_I4; break;
2676 case VT_UI4 : lVal = V_UNION(left,ulVal); resT=VT_I4; break;
2677 case VT_UINT : lVal = V_UNION(left,ulVal); resT=VT_I4; break;
2678 case VT_BOOL : rVal = V_UNION(left,boolVal); resT=VT_I4; break;
2679 default: lOk = FALSE;
2683 switch (V_VT(right)&VT_TYPEMASK) {
2684 case VT_I1 : rVal = V_UNION(right,cVal); resT=VT_I4; break;
2685 case VT_I2 : rVal = V_UNION(right,iVal); resT=max(VT_I2, resT); break;
2686 case VT_I4 : rVal = V_UNION(right,lVal); resT=VT_I4; break;
2687 case VT_INT : rVal = V_UNION(right,lVal); resT=VT_I4; break;
2688 case VT_UI1 : rVal = V_UNION(right,bVal); resT=VT_I4; break;
2689 case VT_UI2 : rVal = V_UNION(right,uiVal); resT=VT_I4; break;
2690 case VT_UI4 : rVal = V_UNION(right,ulVal); resT=VT_I4; break;
2691 case VT_UINT : rVal = V_UNION(right,ulVal); resT=VT_I4; break;
2692 case VT_BOOL : rVal = V_UNION(right,boolVal); resT=VT_I4; break;
2693 default: rOk = FALSE;
2697 res = (lVal & rVal);
2698 V_VT(result) = resT;
2700 case VT_I2 : V_UNION(result,iVal) = res; break;
2701 case VT_I4 : V_UNION(result,lVal) = res; break;
2703 FIXME("Unexpected result variant type %x\n", resT);
2704 V_UNION(result,lVal) = res;
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_UNION(left,cVal); break;
2753 case VT_I2 : lVal = V_UNION(left,iVal); break;
2754 case VT_I4 : lVal = V_UNION(left,lVal); break;
2755 case VT_INT : lVal = V_UNION(left,lVal); break;
2756 case VT_UI1 : lVal = V_UNION(left,bVal); break;
2757 case VT_UI2 : lVal = V_UNION(left,uiVal); break;
2758 case VT_UI4 : lVal = V_UNION(left,ulVal); break;
2759 case VT_UINT : lVal = V_UNION(left,ulVal); break;
2760 case VT_R4 : lVal = V_UNION(left,fltVal); break;
2761 case VT_R8 : lVal = V_UNION(left,dblVal); 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_UNION(right,cVal); break;
2769 case VT_I2 : rVal = V_UNION(right,iVal); break;
2770 case VT_I4 : rVal = V_UNION(right,lVal); break;
2771 case VT_INT : rVal = V_UNION(right,lVal); break;
2772 case VT_UI1 : rVal = V_UNION(right,bVal); break;
2773 case VT_UI2 : rVal = V_UNION(right,uiVal); break;
2774 case VT_UI4 : rVal = V_UNION(right,ulVal); break;
2775 case VT_UINT : rVal = V_UNION(right,ulVal); break;
2776 case VT_R4 : rVal = V_UNION(right,fltVal);break;
2777 case VT_R8 : rVal = V_UNION(right,dblVal);break;
2778 case VT_NULL : rVal = 0.0; break;
2779 default: rOk = FALSE;
2783 res = (lVal + rVal);
2784 V_VT(result) = VT_R8;
2785 V_UNION(result,dblVal) = res;
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_UNION(left,cVal); break;
2807 case VT_I2 : lVal = V_UNION(left,iVal); break;
2808 case VT_I4 : lVal = V_UNION(left,lVal); break;
2809 case VT_INT : lVal = V_UNION(left,lVal); break;
2810 case VT_UI1 : lVal = V_UNION(left,bVal); break;
2811 case VT_UI2 : lVal = V_UNION(left,uiVal); break;
2812 case VT_UI4 : lVal = V_UNION(left,ulVal); break;
2813 case VT_UINT : lVal = V_UNION(left,ulVal); break;
2814 case VT_R4 : lVal = V_UNION(left,fltVal); 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_UNION(right,cVal); break;
2822 case VT_I2 : rVal = V_UNION(right,iVal); break;
2823 case VT_I4 : rVal = V_UNION(right,lVal); break;
2824 case VT_INT : rVal = V_UNION(right,lVal); break;
2825 case VT_UI1 : rVal = V_UNION(right,bVal); break;
2826 case VT_UI2 : rVal = V_UNION(right,uiVal); break;
2827 case VT_UI4 : rVal = V_UNION(right,ulVal); break;
2828 case VT_UINT : rVal = V_UNION(right,ulVal); break;
2829 case VT_R4 : rVal = V_UNION(right,fltVal);break;
2830 case VT_NULL : rVal = 0.0; break;
2831 default: rOk = FALSE;
2835 res = (lVal + rVal);
2836 V_VT(result) = VT_R4;
2837 V_UNION(result,fltVal) = res;
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_UNION(left,cVal); resT=VT_I4; break;
2867 case VT_I2 : lVal = V_UNION(left,iVal); resT=VT_I2; break;
2868 case VT_I4 : lVal = V_UNION(left,lVal); resT=VT_I4; break;
2869 case VT_INT : lVal = V_UNION(left,lVal); resT=VT_I4; break;
2870 case VT_UI1 : lVal = V_UNION(left,bVal); resT=VT_I4; break;
2871 case VT_UI2 : lVal = V_UNION(left,uiVal); resT=VT_I4; break;
2872 case VT_UI4 : lVal = V_UNION(left,ulVal); resT=VT_I4; break;
2873 case VT_UINT : lVal = V_UNION(left,ulVal); 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_UNION(right,cVal); resT=VT_I4; break;
2881 case VT_I2 : rVal = V_UNION(right,iVal); resT=max(VT_I2, resT); break;
2882 case VT_I4 : rVal = V_UNION(right,lVal); resT=VT_I4; break;
2883 case VT_INT : rVal = V_UNION(right,lVal); resT=VT_I4; break;
2884 case VT_UI1 : rVal = V_UNION(right,bVal); resT=VT_I4; break;
2885 case VT_UI2 : rVal = V_UNION(right,uiVal); resT=VT_I4; break;
2886 case VT_UI4 : rVal = V_UNION(right,ulVal); resT=VT_I4; break;
2887 case VT_UINT : rVal = V_UNION(right,ulVal); 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_UNION(result,iVal) = res; break;
2897 case VT_I4 : V_UNION(result,lVal) = res; break;
2899 FIXME("Unexpected result variant type %x\n", resT);
2900 V_UNION(result,lVal) = res;
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]
2918 HRESULT WINAPI VarMul(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2920 HRESULT rc = E_FAIL;
2921 VARTYPE lvt,rvt,resvt;
2925 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2926 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2928 VariantInit(&lv);VariantInit(&rv);
2929 lvt = V_VT(left)&VT_TYPEMASK;
2930 rvt = V_VT(right)&VT_TYPEMASK;
2931 found = FALSE;resvt=VT_VOID;
2932 if (((1<<lvt) | (1<<rvt)) & ((1<<VT_R4)|(1<<VT_R8))) {
2936 if (!found && (((1<<lvt) | (1<<rvt)) & ((1<<VT_I1)|(1<<VT_I2)|(1<<VT_UI1)|(1<<VT_UI2)|(1<<VT_I4)|(1<<VT_UI4)|(1<<VT_INT)|(1<<VT_UINT)))) {
2941 FIXME("can't expand vt %d vs %d to a target type.\n",lvt,rvt);
2944 rc = VariantChangeType(&lv, left, 0, resvt);
2946 FIXME("Could not convert 0x%x to %d?\n",V_VT(left),resvt);
2949 rc = VariantChangeType(&rv, right, 0, resvt);
2951 FIXME("Could not convert 0x%x to %d?\n",V_VT(right),resvt);
2956 V_VT(result) = resvt;
2957 V_R8(result) = V_R8(&lv) * V_R8(&rv);
2961 V_VT(result) = resvt;
2962 V_I4(result) = V_I4(&lv) * V_I4(&rv);
2966 TRACE("returning 0x%8lx (%s%s),%g\n", rc, debugstr_VT(result),
2967 debugstr_VF(result), V_VT(result) == VT_R8 ? V_R8(result) : (double)V_I4(result));
2971 /**********************************************************************
2972 * VarDiv [OLEAUT32.143]
2975 HRESULT WINAPI VarDiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2977 HRESULT rc = E_FAIL;
2978 VARTYPE lvt,rvt,resvt;
2982 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2983 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2985 VariantInit(&lv);VariantInit(&rv);
2986 lvt = V_VT(left)&VT_TYPEMASK;
2987 rvt = V_VT(right)&VT_TYPEMASK;
2988 found = FALSE;resvt = VT_VOID;
2989 if (((1<<lvt) | (1<<rvt)) & ((1<<VT_R4)|(1<<VT_R8))) {
2993 if (!found && (((1<<lvt) | (1<<rvt)) & ((1<<VT_I1)|(1<<VT_I2)|(1<<VT_UI1)|(1<<VT_UI2)|(1<<VT_I4)|(1<<VT_UI4)|(1<<VT_INT)|(1<<VT_UINT)))) {
2998 FIXME("can't expand vt %d vs %d to a target type.\n",lvt,rvt);
3001 rc = VariantChangeType(&lv, left, 0, resvt);
3003 FIXME("Could not convert 0x%x to %d?\n",V_VT(left),resvt);
3006 rc = VariantChangeType(&rv, right, 0, resvt);
3008 FIXME("Could not convert 0x%x to %d?\n",V_VT(right),resvt);
3013 V_VT(result) = resvt;
3014 V_R8(result) = V_R8(&lv) / V_R8(&rv);
3018 V_VT(result) = resvt;
3019 V_I4(result) = V_I4(&lv) / V_I4(&rv);
3023 TRACE("returning 0x%8lx (%s%s),%g\n", rc, debugstr_VT(result),
3024 debugstr_VF(result), V_VT(result) == VT_R8 ? V_R8(result) : (double)V_I4(result));
3028 /**********************************************************************
3029 * VarSub [OLEAUT32.159]
3032 HRESULT WINAPI VarSub(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3034 HRESULT rc = E_FAIL;
3035 VARTYPE lvt,rvt,resvt;
3039 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3040 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
3042 VariantInit(&lv);VariantInit(&rv);
3043 lvt = V_VT(left)&VT_TYPEMASK;
3044 rvt = V_VT(right)&VT_TYPEMASK;
3045 found = FALSE;resvt = VT_VOID;
3046 if (((1<<lvt) | (1<<rvt)) & ((1<<VT_DATE)|(1<<VT_R4)|(1<<VT_R8))) {
3050 if (!found && (((1<<lvt) | (1<<rvt)) & ((1<<VT_I1)|(1<<VT_I2)|(1<<VT_UI1)|(1<<VT_UI2)|(1<<VT_I4)|(1<<VT_UI4)|(1<<VT_INT)|(1<<VT_UINT)))) {
3055 FIXME("can't expand vt %d vs %d to a target type.\n",lvt,rvt);
3058 rc = VariantChangeType(&lv, left, 0, resvt);
3060 FIXME("Could not convert 0x%x to %d?\n",V_VT(left),resvt);
3063 rc = VariantChangeType(&rv, right, 0, resvt);
3065 FIXME("Could not convert 0x%x to %d?\n",V_VT(right),resvt);
3070 V_VT(result) = resvt;
3071 V_R8(result) = V_R8(&lv) - V_R8(&rv);
3075 V_VT(result) = resvt;
3076 V_I4(result) = V_I4(&lv) - V_I4(&rv);
3080 TRACE("returning 0x%8lx (%s%s),%g\n", rc, debugstr_VT(result),
3081 debugstr_VF(result), V_VT(result) == VT_R8 ? V_R8(result) : (double)V_I4(result));
3085 /**********************************************************************
3086 * VarOr [OLEAUT32.157]
3088 * Perform a logical or (OR) operation on two variants.
3091 * pVarLeft [I] First variant
3092 * pVarRight [I] Variant to OR with pVarLeft
3093 * pVarOut [O] Destination for OR result
3096 * Success: S_OK. pVarOut contains the result of the operation with its type
3097 * taken from the table listed under VarXor().
3098 * Failure: An HRESULT error code indicating the error.
3101 * See the Notes section of VarXor() for further information.
3103 HRESULT WINAPI VarOr(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
3106 VARIANT varLeft, varRight, varStr;
3109 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
3110 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
3111 debugstr_VF(pVarRight), pVarOut);
3113 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
3114 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
3115 V_VT(pVarLeft) == VT_DISPATCH || V_VT(pVarRight) == VT_DISPATCH ||
3116 V_VT(pVarLeft) == VT_RECORD || V_VT(pVarRight) == VT_RECORD)
3117 return DISP_E_BADVARTYPE;
3119 V_VT(&varLeft) = V_VT(&varRight) = V_VT(&varStr) = VT_EMPTY;
3121 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
3123 /* NULL OR Zero is NULL, NULL OR value is value */
3124 if (V_VT(pVarLeft) == VT_NULL)
3125 pVarLeft = pVarRight; /* point to the non-NULL var */
3127 V_VT(pVarOut) = VT_NULL;
3130 switch (V_VT(pVarLeft))
3132 case VT_DATE: case VT_R8:
3137 if (V_BOOL(pVarLeft))
3138 *pVarOut = *pVarLeft;
3140 case VT_I2: case VT_UI2:
3149 if (V_UI1(pVarLeft))
3150 *pVarOut = *pVarLeft;
3156 case VT_I4: case VT_UI4: case VT_INT: case VT_UINT:
3161 if (V_CY(pVarLeft).int64)
3164 case VT_I8: case VT_UI8:
3169 if (DEC_HI32(&V_DECIMAL(pVarLeft)) || DEC_LO64(&V_DECIMAL(pVarLeft)))
3176 if (!V_BSTR(pVarLeft))
3177 return DISP_E_BADVARTYPE;
3179 hRet = VarBoolFromStr(V_BSTR(pVarLeft), LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
3180 if (SUCCEEDED(hRet) && b)
3182 V_VT(pVarOut) = VT_BOOL;
3183 V_BOOL(pVarOut) = b;
3187 case VT_NULL: case VT_EMPTY:
3188 V_VT(pVarOut) = VT_NULL;
3191 return DISP_E_BADVARTYPE;
3195 if (V_VT(pVarLeft) == VT_EMPTY || V_VT(pVarRight) == VT_EMPTY)
3197 if (V_VT(pVarLeft) == VT_EMPTY)
3198 pVarLeft = pVarRight; /* point to the non-EMPTY var */
3201 /* Since one argument is empty (0), OR'ing it with the other simply
3202 * gives the others value (as 0|x => x). So just convert the other
3203 * argument to the required result type.
3205 switch (V_VT(pVarLeft))
3208 if (!V_BSTR(pVarLeft))
3209 return DISP_E_BADVARTYPE;
3211 hRet = VariantCopy(&varStr, pVarLeft);
3215 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
3218 /* Fall Through ... */
3219 case VT_EMPTY: case VT_UI1: case VT_BOOL: case VT_I2:
3220 V_VT(pVarOut) = VT_I2;
3222 case VT_DATE: case VT_CY: case VT_DECIMAL: case VT_R4: case VT_R8:
3223 case VT_I1: case VT_UI2: case VT_I4: case VT_UI4:
3224 case VT_INT: case VT_UINT: case VT_UI8:
3225 V_VT(pVarOut) = VT_I4;
3228 V_VT(pVarOut) = VT_I8;
3231 return DISP_E_BADVARTYPE;
3233 hRet = VariantCopy(&varLeft, pVarLeft);
3236 pVarLeft = &varLeft;
3237 hRet = VariantChangeType(pVarOut, pVarLeft, 0, V_VT(pVarOut));
3241 if (V_VT(pVarLeft) == VT_BOOL && V_VT(pVarRight) == VT_BOOL)
3243 V_VT(pVarOut) = VT_BOOL;
3244 V_BOOL(pVarOut) = V_BOOL(pVarLeft) | V_BOOL(pVarRight);
3248 if (V_VT(pVarLeft) == VT_UI1 && V_VT(pVarRight) == VT_UI1)
3250 V_VT(pVarOut) = VT_UI1;
3251 V_UI1(pVarOut) = V_UI1(pVarLeft) | V_UI1(pVarRight);
3255 if (V_VT(pVarLeft) == VT_BSTR)
3257 hRet = VariantCopy(&varStr, pVarLeft);
3261 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
3266 if (V_VT(pVarLeft) == VT_BOOL &&
3267 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_BSTR))
3271 else if ((V_VT(pVarLeft) == VT_BOOL || V_VT(pVarLeft) == VT_UI1 ||
3272 V_VT(pVarLeft) == VT_I2 || V_VT(pVarLeft) == VT_BSTR) &&
3273 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_UI1 ||
3274 V_VT(pVarRight) == VT_I2 || V_VT(pVarRight) == VT_BSTR))
3278 else if (V_VT(pVarLeft) == VT_I8 || V_VT(pVarRight) == VT_I8)
3280 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
3281 return DISP_E_TYPEMISMATCH;
3285 hRet = VariantCopy(&varLeft, pVarLeft);
3289 hRet = VariantCopy(&varRight, pVarRight);
3293 if (vt == VT_I4 && V_VT(&varLeft) == VT_UI4)
3294 V_VT(&varLeft) = VT_I4; /* Don't overflow */
3299 if (V_VT(&varLeft) == VT_BSTR &&
3300 FAILED(VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d)))
3301 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL, VT_BOOL);
3302 if (SUCCEEDED(hRet) && V_VT(&varLeft) != vt)
3303 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
3308 if (vt == VT_I4 && V_VT(&varRight) == VT_UI4)
3309 V_VT(&varRight) = VT_I4; /* Don't overflow */
3314 if (V_VT(&varRight) == VT_BSTR &&
3315 FAILED(VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d)))
3316 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL, VT_BOOL);
3317 if (SUCCEEDED(hRet) && V_VT(&varRight) != vt)
3318 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
3326 V_I8(pVarOut) = V_I8(&varLeft) | V_I8(&varRight);
3328 else if (vt == VT_I4)
3330 V_I4(pVarOut) = V_I4(&varLeft) | V_I4(&varRight);
3334 V_I2(pVarOut) = V_I2(&varLeft) | V_I2(&varRight);
3338 VariantClear(&varStr);
3339 VariantClear(&varLeft);
3340 VariantClear(&varRight);
3344 /**********************************************************************
3345 * VarAbs [OLEAUT32.168]
3347 * Convert a variant to its absolute value.
3350 * pVarIn [I] Source variant
3351 * pVarOut [O] Destination for converted value
3354 * Success: S_OK. pVarOut contains the absolute value of pVarIn.
3355 * Failure: An HRESULT error code indicating the error.
3358 * - This function does not process by-reference variants.
3359 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3360 * according to the following table:
3361 *| Input Type Output Type
3362 *| ---------- -----------
3365 *| (All others) Unchanged
3367 HRESULT WINAPI VarAbs(LPVARIANT pVarIn, LPVARIANT pVarOut)
3370 HRESULT hRet = S_OK;
3372 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
3373 debugstr_VF(pVarIn), pVarOut);
3375 if (V_ISARRAY(pVarIn) || V_VT(pVarIn) == VT_UNKNOWN ||
3376 V_VT(pVarIn) == VT_DISPATCH || V_VT(pVarIn) == VT_RECORD ||
3377 V_VT(pVarIn) == VT_ERROR)
3378 return DISP_E_TYPEMISMATCH;
3380 *pVarOut = *pVarIn; /* Shallow copy the value, and invert it if needed */
3382 #define ABS_CASE(typ,min) \
3383 case VT_##typ: if (V_##typ(pVarIn) == min) hRet = DISP_E_OVERFLOW; \
3384 else if (V_##typ(pVarIn) < 0) V_##typ(pVarOut) = -V_##typ(pVarIn); \
3387 switch (V_VT(pVarIn))
3389 ABS_CASE(I1,I1_MIN);
3391 V_VT(pVarOut) = VT_I2;
3392 /* BOOL->I2, Fall through ... */
3393 ABS_CASE(I2,I2_MIN);
3395 ABS_CASE(I4,I4_MIN);
3396 ABS_CASE(I8,I8_MIN);
3397 ABS_CASE(R4,R4_MIN);
3399 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
3402 V_VT(pVarOut) = VT_R8;
3404 /* Fall through ... */
3406 ABS_CASE(R8,R8_MIN);
3408 hRet = VarCyAbs(V_CY(pVarIn), & V_CY(pVarOut));
3411 DEC_SIGN(&V_DECIMAL(pVarOut)) &= ~DECIMAL_NEG;
3421 V_VT(pVarOut) = VT_I2;
3426 hRet = DISP_E_BADVARTYPE;
3432 /**********************************************************************
3433 * VarFix [OLEAUT32.169]
3435 * Truncate a variants value to a whole number.
3438 * pVarIn [I] Source variant
3439 * pVarOut [O] Destination for converted value
3442 * Success: S_OK. pVarOut contains the converted value.
3443 * Failure: An HRESULT error code indicating the error.
3446 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3447 * according to the following table:
3448 *| Input Type Output Type
3449 *| ---------- -----------
3453 *| All Others Unchanged
3454 * - The difference between this function and VarInt() is that VarInt() rounds
3455 * negative numbers away from 0, while this function rounds them towards zero.
3457 HRESULT WINAPI VarFix(LPVARIANT pVarIn, LPVARIANT pVarOut)
3459 HRESULT hRet = S_OK;
3461 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
3462 debugstr_VF(pVarIn), pVarOut);
3464 V_VT(pVarOut) = V_VT(pVarIn);
3466 switch (V_VT(pVarIn))
3469 V_UI1(pVarOut) = V_UI1(pVarIn);
3472 V_VT(pVarOut) = VT_I2;
3475 V_I2(pVarOut) = V_I2(pVarIn);
3478 V_I4(pVarOut) = V_I4(pVarIn);
3481 V_I8(pVarOut) = V_I8(pVarIn);
3484 if (V_R4(pVarIn) < 0.0f)
3485 V_R4(pVarOut) = (float)ceil(V_R4(pVarIn));
3487 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
3490 V_VT(pVarOut) = VT_R8;
3491 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
3496 if (V_R8(pVarIn) < 0.0)
3497 V_R8(pVarOut) = ceil(V_R8(pVarIn));
3499 V_R8(pVarOut) = floor(V_R8(pVarIn));
3502 hRet = VarCyFix(V_CY(pVarIn), &V_CY(pVarOut));
3505 hRet = VarDecFix(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
3508 V_VT(pVarOut) = VT_I2;
3515 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
3516 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
3517 hRet = DISP_E_BADVARTYPE;
3519 hRet = DISP_E_TYPEMISMATCH;
3522 V_VT(pVarOut) = VT_EMPTY;
3527 /**********************************************************************
3528 * VarInt [OLEAUT32.172]
3530 * Truncate a variants value to a whole number.
3533 * pVarIn [I] Source variant
3534 * pVarOut [O] Destination for converted value
3537 * Success: S_OK. pVarOut contains the converted value.
3538 * Failure: An HRESULT error code indicating the error.
3541 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3542 * according to the following table:
3543 *| Input Type Output Type
3544 *| ---------- -----------
3548 *| All Others Unchanged
3549 * - The difference between this function and VarFix() is that VarFix() rounds
3550 * negative numbers towards 0, while this function rounds them away from zero.
3552 HRESULT WINAPI VarInt(LPVARIANT pVarIn, LPVARIANT pVarOut)
3554 HRESULT hRet = S_OK;
3556 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
3557 debugstr_VF(pVarIn), pVarOut);
3559 V_VT(pVarOut) = V_VT(pVarIn);
3561 switch (V_VT(pVarIn))
3564 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
3567 V_VT(pVarOut) = VT_R8;
3568 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
3573 V_R8(pVarOut) = floor(V_R8(pVarIn));
3576 hRet = VarCyInt(V_CY(pVarIn), &V_CY(pVarOut));
3579 hRet = VarDecInt(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
3582 return VarFix(pVarIn, pVarOut);
3588 /**********************************************************************
3589 * VarXor [OLEAUT32.167]
3591 * Perform a logical exclusive-or (XOR) operation on two variants.
3594 * pVarLeft [I] First variant
3595 * pVarRight [I] Variant to XOR with pVarLeft
3596 * pVarOut [O] Destination for XOR result
3599 * Success: S_OK. pVarOut contains the result of the operation with its type
3600 * taken from the table below).
3601 * Failure: An HRESULT error code indicating the error.
3604 * - Neither pVarLeft or pVarRight are modified by this function.
3605 * - This function does not process by-reference variants.
3606 * - Input types of VT_BSTR may be numeric strings or boolean text.
3607 * - The type of result stored in pVarOut depends on the types of pVarLeft
3608 * and pVarRight, and will be one of VT_UI1, VT_I2, VT_I4, VT_I8, VT_BOOL,
3609 * or VT_NULL if the function succeeds.
3610 * - Type promotion is inconsistent and as a result certain combinations of
3611 * values will return DISP_E_OVERFLOW even when they could be represented.
3612 * This matches the behaviour of native oleaut32.
3614 HRESULT WINAPI VarXor(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
3617 VARIANT varLeft, varRight;
3621 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
3622 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
3623 debugstr_VF(pVarRight), pVarOut);
3625 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
3626 V_VT(pVarLeft) > VT_UINT || V_VT(pVarRight) > VT_UINT ||
3627 V_VT(pVarLeft) == VT_VARIANT || V_VT(pVarRight) == VT_VARIANT ||
3628 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
3629 V_VT(pVarLeft) == (VARTYPE)15 || V_VT(pVarRight) == (VARTYPE)15 ||
3630 V_VT(pVarLeft) == VT_ERROR || V_VT(pVarRight) == VT_ERROR)
3631 return DISP_E_BADVARTYPE;
3633 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
3635 /* NULL XOR anything valid is NULL */
3636 V_VT(pVarOut) = VT_NULL;
3640 /* Copy our inputs so we don't disturb anything */
3641 V_VT(&varLeft) = V_VT(&varRight) = VT_EMPTY;
3643 hRet = VariantCopy(&varLeft, pVarLeft);
3647 hRet = VariantCopy(&varRight, pVarRight);
3651 /* Try any strings first as numbers, then as VT_BOOL */
3652 if (V_VT(&varLeft) == VT_BSTR)
3654 hRet = VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d);
3655 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL,
3656 FAILED(hRet) ? VT_BOOL : VT_I4);
3661 if (V_VT(&varRight) == VT_BSTR)
3663 hRet = VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d);
3664 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL,
3665 FAILED(hRet) ? VT_BOOL : VT_I4);
3670 /* Determine the result type */
3671 if (V_VT(&varLeft) == VT_I8 || V_VT(&varRight) == VT_I8)
3673 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
3674 return DISP_E_TYPEMISMATCH;
3679 switch ((V_VT(&varLeft) << 16) | V_VT(&varRight))
3681 case (VT_BOOL << 16) | VT_BOOL:
3684 case (VT_UI1 << 16) | VT_UI1:
3687 case (VT_EMPTY << 16) | VT_EMPTY:
3688 case (VT_EMPTY << 16) | VT_UI1:
3689 case (VT_EMPTY << 16) | VT_I2:
3690 case (VT_EMPTY << 16) | VT_BOOL:
3691 case (VT_UI1 << 16) | VT_EMPTY:
3692 case (VT_UI1 << 16) | VT_I2:
3693 case (VT_UI1 << 16) | VT_BOOL:
3694 case (VT_I2 << 16) | VT_EMPTY:
3695 case (VT_I2 << 16) | VT_UI1:
3696 case (VT_I2 << 16) | VT_I2:
3697 case (VT_I2 << 16) | VT_BOOL:
3698 case (VT_BOOL << 16) | VT_EMPTY:
3699 case (VT_BOOL << 16) | VT_UI1:
3700 case (VT_BOOL << 16) | VT_I2:
3709 /* VT_UI4 does not overflow */
3712 if (V_VT(&varLeft) == VT_UI4)
3713 V_VT(&varLeft) = VT_I4;
3714 if (V_VT(&varRight) == VT_UI4)
3715 V_VT(&varRight) = VT_I4;
3718 /* Convert our input copies to the result type */
3719 if (V_VT(&varLeft) != vt)
3720 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
3724 if (V_VT(&varRight) != vt)
3725 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
3731 /* Calculate the result */
3735 V_I8(pVarOut) = V_I8(&varLeft) ^ V_I8(&varRight);
3738 V_I4(pVarOut) = V_I4(&varLeft) ^ V_I4(&varRight);
3742 V_I2(pVarOut) = V_I2(&varLeft) ^ V_I2(&varRight);
3745 V_UI1(pVarOut) = V_UI1(&varLeft) ^ V_UI1(&varRight);
3750 VariantClear(&varLeft);
3751 VariantClear(&varRight);
3755 /**********************************************************************
3756 * VarEqv [OLEAUT32.172]
3758 * Determine if two variants contain the same value.
3761 * pVarLeft [I] First variant to compare
3762 * pVarRight [I] Variant to compare to pVarLeft
3763 * pVarOut [O] Destination for comparison result
3766 * Success: S_OK. pVarOut contains the result of the comparison (VARIANT_TRUE
3767 * if equivalent or non-zero otherwise.
3768 * Failure: An HRESULT error code indicating the error.
3771 * - This function simply calls VarXor() on pVarLeft and pVarRight and inverts
3774 HRESULT WINAPI VarEqv(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
3778 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
3779 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
3780 debugstr_VF(pVarRight), pVarOut);
3782 hRet = VarXor(pVarLeft, pVarRight, pVarOut);
3783 if (SUCCEEDED(hRet))
3785 if (V_VT(pVarOut) == VT_I8)
3786 V_I8(pVarOut) = ~V_I8(pVarOut);
3788 V_UI4(pVarOut) = ~V_UI4(pVarOut);
3793 /**********************************************************************
3794 * VarNeg [OLEAUT32.173]
3796 * Negate the value of a variant.
3799 * pVarIn [I] Source variant
3800 * pVarOut [O] Destination for converted value
3803 * Success: S_OK. pVarOut contains the converted value.
3804 * Failure: An HRESULT error code indicating the error.
3807 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3808 * according to the following table:
3809 *| Input Type Output Type
3810 *| ---------- -----------
3815 *| All Others Unchanged (unless promoted)
3816 * - Where the negated value of a variant does not fit in its base type, the type
3817 * is promoted according to the following table:
3818 *| Input Type Promoted To
3819 *| ---------- -----------
3823 * - The native version of this function returns DISP_E_BADVARTYPE for valid
3824 * variant types that cannot be negated, and returns DISP_E_TYPEMISMATCH
3825 * for types which are not valid. Since this is in contravention of the
3826 * meaning of those error codes and unlikely to be relied on by applications,
3827 * this implementation returns errors consistent with the other high level
3828 * variant math functions.
3830 HRESULT WINAPI VarNeg(LPVARIANT pVarIn, LPVARIANT pVarOut)
3832 HRESULT hRet = S_OK;
3834 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
3835 debugstr_VF(pVarIn), pVarOut);
3837 V_VT(pVarOut) = V_VT(pVarIn);
3839 switch (V_VT(pVarIn))
3842 V_VT(pVarOut) = VT_I2;
3843 V_I2(pVarOut) = -V_UI1(pVarIn);
3846 V_VT(pVarOut) = VT_I2;
3849 if (V_I2(pVarIn) == I2_MIN)
3851 V_VT(pVarOut) = VT_I4;
3852 V_I4(pVarOut) = -(int)V_I2(pVarIn);
3855 V_I2(pVarOut) = -V_I2(pVarIn);
3858 if (V_I4(pVarIn) == I4_MIN)
3860 V_VT(pVarOut) = VT_R8;
3861 V_R8(pVarOut) = -(double)V_I4(pVarIn);
3864 V_I4(pVarOut) = -V_I4(pVarIn);
3867 if (V_I8(pVarIn) == I8_MIN)
3869 V_VT(pVarOut) = VT_R8;
3870 hRet = VarR8FromI8(V_I8(pVarIn), &V_R8(pVarOut));
3871 V_R8(pVarOut) *= -1.0;
3874 V_I8(pVarOut) = -V_I8(pVarIn);
3877 V_R4(pVarOut) = -V_R4(pVarIn);
3881 V_R8(pVarOut) = -V_R8(pVarIn);
3884 hRet = VarCyNeg(V_CY(pVarIn), &V_CY(pVarOut));
3887 hRet = VarDecNeg(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
3890 V_VT(pVarOut) = VT_R8;
3891 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
3892 V_R8(pVarOut) = -V_R8(pVarOut);
3895 V_VT(pVarOut) = VT_I2;
3902 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
3903 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
3904 hRet = DISP_E_BADVARTYPE;
3906 hRet = DISP_E_TYPEMISMATCH;
3909 V_VT(pVarOut) = VT_EMPTY;
3914 /**********************************************************************
3915 * VarNot [OLEAUT32.174]
3917 * Perform a not operation on a variant.
3920 * pVarIn [I] Source variant
3921 * pVarOut [O] Destination for converted value
3924 * Success: S_OK. pVarOut contains the converted value.
3925 * Failure: An HRESULT error code indicating the error.
3928 * - Strictly speaking, this function performs a bitwise ones compliment
3929 * on the variants value (after possibly converting to VT_I4, see below).
3930 * This only behaves like a boolean not operation if the value in
3931 * pVarIn is either VARIANT_TRUE or VARIANT_FALSE and the type is signed.
3932 * - To perform a genuine not operation, convert the variant to a VT_BOOL
3933 * before calling this function.
3934 * - This function does not process by-reference variants.
3935 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3936 * according to the following table:
3937 *| Input Type Output Type
3938 *| ---------- -----------
3945 *| (All others) Unchanged
3947 HRESULT WINAPI VarNot(LPVARIANT pVarIn, LPVARIANT pVarOut)
3950 HRESULT hRet = S_OK;
3952 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
3953 debugstr_VF(pVarIn), pVarOut);
3955 V_VT(pVarOut) = V_VT(pVarIn);
3957 switch (V_VT(pVarIn))
3960 V_I4(pVarOut) = ~V_I1(pVarIn);
3961 V_VT(pVarOut) = VT_I4;
3963 case VT_UI1: V_UI1(pVarOut) = ~V_UI1(pVarIn); break;
3965 case VT_I2: V_I2(pVarOut) = ~V_I2(pVarIn); break;
3967 V_I4(pVarOut) = ~V_UI2(pVarIn);
3968 V_VT(pVarOut) = VT_I4;
3971 hRet = VarI4FromDec(&V_DECIMAL(pVarIn), &V_I4(&varIn));
3975 /* Fall through ... */
3977 V_VT(pVarOut) = VT_I4;
3978 /* Fall through ... */
3979 case VT_I4: V_I4(pVarOut) = ~V_I4(pVarIn); break;
3982 V_I4(pVarOut) = ~V_UI4(pVarIn);
3983 V_VT(pVarOut) = VT_I4;
3985 case VT_I8: V_I8(pVarOut) = ~V_I8(pVarIn); break;
3987 V_I4(pVarOut) = ~V_UI8(pVarIn);
3988 V_VT(pVarOut) = VT_I4;
3991 hRet = VarI4FromR4(V_R4(pVarIn), &V_I4(pVarOut));
3992 V_I4(pVarOut) = ~V_I4(pVarOut);
3993 V_VT(pVarOut) = VT_I4;
3996 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
4000 /* Fall through ... */
4003 hRet = VarI4FromR8(V_R8(pVarIn), &V_I4(pVarOut));
4004 V_I4(pVarOut) = ~V_I4(pVarOut);
4005 V_VT(pVarOut) = VT_I4;
4008 hRet = VarI4FromCy(V_CY(pVarIn), &V_I4(pVarOut));
4009 V_I4(pVarOut) = ~V_I4(pVarOut);
4010 V_VT(pVarOut) = VT_I4;
4014 V_VT(pVarOut) = VT_I2;
4020 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4021 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4022 hRet = DISP_E_BADVARTYPE;
4024 hRet = DISP_E_TYPEMISMATCH;
4027 V_VT(pVarOut) = VT_EMPTY;
4032 /**********************************************************************
4033 * VarRound [OLEAUT32.175]
4035 * Perform a round operation on a variant.
4038 * pVarIn [I] Source variant
4039 * deci [I] Number of decimals to round to
4040 * pVarOut [O] Destination for converted value
4043 * Success: S_OK. pVarOut contains the converted value.
4044 * Failure: An HRESULT error code indicating the error.
4047 * - Floating point values are rounded to the desired number of decimals.
4048 * - Some integer types are just copied to the return variable.
4049 * - Some other integer types are not handled and fail.
4051 HRESULT WINAPI VarRound(LPVARIANT pVarIn, int deci, LPVARIANT pVarOut)
4054 HRESULT hRet = S_OK;
4057 TRACE("(%p->(%s%s),%d)\n", pVarIn, debugstr_VT(pVarIn), debugstr_VF(pVarIn), deci);
4059 switch (V_VT(pVarIn))
4061 /* cases that fail on windows */
4066 hRet = DISP_E_BADVARTYPE;
4069 /* cases just copying in to out */
4071 V_VT(pVarOut) = V_VT(pVarIn);
4072 V_UI1(pVarOut) = V_UI1(pVarIn);
4075 V_VT(pVarOut) = V_VT(pVarIn);
4076 V_I2(pVarOut) = V_I2(pVarIn);
4079 V_VT(pVarOut) = V_VT(pVarIn);
4080 V_I4(pVarOut) = V_I4(pVarIn);
4083 V_VT(pVarOut) = V_VT(pVarIn);
4084 /* value unchanged */
4087 /* cases that change type */
4089 V_VT(pVarOut) = VT_I2;
4093 V_VT(pVarOut) = VT_I2;
4094 V_I2(pVarOut) = V_BOOL(pVarIn);
4097 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
4102 /* Fall through ... */
4104 /* cases we need to do math */
4106 if (V_R8(pVarIn)>0) {
4107 V_R8(pVarOut)=floor(V_R8(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
4109 V_R8(pVarOut)=ceil(V_R8(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
4111 V_VT(pVarOut) = V_VT(pVarIn);
4114 if (V_R4(pVarIn)>0) {
4115 V_R4(pVarOut)=floor(V_R4(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
4117 V_R4(pVarOut)=ceil(V_R4(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
4119 V_VT(pVarOut) = V_VT(pVarIn);
4122 if (V_DATE(pVarIn)>0) {
4123 V_DATE(pVarOut)=floor(V_DATE(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
4125 V_DATE(pVarOut)=ceil(V_DATE(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
4127 V_VT(pVarOut) = V_VT(pVarIn);
4133 factor=pow(10, 4-deci);
4135 if (V_CY(pVarIn).int64>0) {
4136 V_CY(pVarOut).int64=floor(V_CY(pVarIn).int64/factor)*factor;
4138 V_CY(pVarOut).int64=ceil(V_CY(pVarIn).int64/factor)*factor;
4140 V_VT(pVarOut) = V_VT(pVarIn);
4143 /* cases we don't know yet */
4145 FIXME("unimplemented part, V_VT(pVarIn) == 0x%X, deci == %d\n",
4146 V_VT(pVarIn) & VT_TYPEMASK, deci);
4147 hRet = DISP_E_BADVARTYPE;
4151 V_VT(pVarOut) = VT_EMPTY;
4153 TRACE("returning 0x%08lx (%s%s),%f\n", hRet, debugstr_VT(pVarOut),
4154 debugstr_VF(pVarOut), (V_VT(pVarOut) == VT_R4) ? V_R4(pVarOut) :
4155 (V_VT(pVarOut) == VT_R8) ? V_R8(pVarOut) : 0);
4161 /**********************************************************************
4162 * VarMod [OLEAUT32.154]
4164 * Perform the modulus operation of the right hand variant on the left
4167 * left [I] Left hand variant
4168 * right [I] Right hand variant
4169 * result [O] Destination for converted value
4172 * Success: S_OK. result contains the remainder.
4173 * Failure: An HRESULT error code indicating the error.
4176 * If an error occurs the type of result will be modified but the value will not be.
4177 * Doesn't support arrays or any special flags yet.
4179 HRESULT WINAPI VarMod(LPVARIANT left, LPVARIANT right, LPVARIANT result)
4183 HRESULT rc = E_FAIL;
4190 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
4191 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
4193 /* check for invalid inputs */
4195 switch (V_VT(left) & VT_TYPEMASK) {
4216 V_VT(result) = VT_EMPTY;
4217 return DISP_E_TYPEMISMATCH;
4219 V_VT(result) = VT_EMPTY;
4220 return E_INVALIDARG;
4222 return DISP_E_TYPEMISMATCH;
4224 V_VT(result) = VT_EMPTY;
4225 return DISP_E_TYPEMISMATCH;
4229 V_VT(result) = VT_EMPTY;
4230 return DISP_E_BADVARTYPE;
4235 switch (V_VT(right) & VT_TYPEMASK) {
4241 if((V_VT(left) == VT_INT) && (V_VT(right) == VT_I8))
4243 V_VT(result) = VT_EMPTY;
4244 return DISP_E_TYPEMISMATCH;
4247 if((V_VT(right) == VT_INT) && (V_VT(left) == VT_I8))
4249 V_VT(result) = VT_EMPTY;
4250 return DISP_E_TYPEMISMATCH;
4260 if(V_VT(left) == VT_EMPTY)
4262 V_VT(result) = VT_I4;
4268 if(V_VT(left) == VT_NULL)
4270 V_VT(result) = VT_NULL;
4276 V_VT(result) = VT_EMPTY;
4277 return DISP_E_BADVARTYPE;
4279 if(V_VT(left) == VT_VOID)
4281 V_VT(result) = VT_EMPTY;
4282 return DISP_E_BADVARTYPE;
4283 } else if((V_VT(left) == VT_NULL) || (V_VT(left) == VT_EMPTY) || (V_VT(left) == VT_ERROR) ||
4286 V_VT(result) = VT_NULL;
4290 V_VT(result) = VT_NULL;
4291 return DISP_E_BADVARTYPE;
4295 V_VT(result) = VT_EMPTY;
4296 return DISP_E_TYPEMISMATCH;
4298 if(V_VT(left) == VT_ERROR)
4300 V_VT(result) = VT_EMPTY;
4301 return DISP_E_TYPEMISMATCH;
4304 V_VT(result) = VT_EMPTY;
4305 return E_INVALIDARG;
4308 return DISP_E_TYPEMISMATCH;
4310 if((V_VT(left) == 15) || ((V_VT(left) >= 24) && (V_VT(left) <= 35)) || !lOk)
4312 V_VT(result) = VT_EMPTY;
4313 return DISP_E_BADVARTYPE;
4316 V_VT(result) = VT_EMPTY;
4317 return DISP_E_TYPEMISMATCH;
4320 V_VT(result) = VT_EMPTY;
4321 return DISP_E_BADVARTYPE;
4324 /* determine the result type */
4325 if((V_VT(left) == VT_I8) || (V_VT(right) == VT_I8)) resT = VT_I8;
4326 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
4327 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_UI1)) resT = VT_UI1;
4328 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_I2)) resT = VT_I2;
4329 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
4330 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_UI1)) resT = VT_I2;
4331 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_I2)) resT = VT_I2;
4332 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
4333 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_UI1)) resT = VT_I2;
4334 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_I2)) resT = VT_I2;
4335 else resT = VT_I4; /* most outputs are I4 */
4337 /* convert to I8 for the modulo */
4338 rc = VariantChangeType(&lv, left, 0, VT_I8);
4341 FIXME("Could not convert left type %d to %d? rc == 0x%lX\n", V_VT(left), VT_I8, rc);
4345 rc = VariantChangeType(&rv, right, 0, VT_I8);
4348 FIXME("Could not convert right type %d to %d? rc == 0x%lX\n", V_VT(right), VT_I8, rc);
4352 /* if right is zero set VT_EMPTY and return divide by zero */
4355 V_VT(result) = VT_EMPTY;
4356 return DISP_E_DIVBYZERO;
4359 /* perform the modulo operation */
4360 V_VT(result) = VT_I8;
4361 V_I8(result) = V_I8(&lv) % V_I8(&rv);
4363 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));
4365 /* convert left and right to the destination type */
4366 rc = VariantChangeType(result, result, 0, resT);
4369 FIXME("Could not convert 0x%x to %d?\n", V_VT(result), resT);
4376 /**********************************************************************
4377 * VarPow [OLEAUT32.158]
4380 HRESULT WINAPI VarPow(LPVARIANT left, LPVARIANT right, LPVARIANT result)
4385 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left), debugstr_VF(left),
4386 right, debugstr_VT(right), debugstr_VF(right), result);
4388 hr = VariantChangeType(&dl,left,0,VT_R8);
4389 if (!SUCCEEDED(hr)) {
4390 ERR("Could not change passed left argument to VT_R8, handle it differently.\n");
4393 hr = VariantChangeType(&dr,right,0,VT_R8);
4394 if (!SUCCEEDED(hr)) {
4395 ERR("Could not change passed right argument to VT_R8, handle it differently.\n");
4398 V_VT(result) = VT_R8;
4399 V_R8(result) = pow(V_R8(&dl),V_R8(&dr));