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);
769 case VT_UI2: return sizeof(SHORT);
773 case VT_UI4: return sizeof(LONG);
775 case VT_UI8: return sizeof(LONGLONG);
776 case VT_R4: return sizeof(float);
777 case VT_R8: return sizeof(double);
778 case VT_DATE: return sizeof(DATE);
779 case VT_BOOL: return sizeof(VARIANT_BOOL);
782 case VT_BSTR: return sizeof(void*);
783 case VT_CY: return sizeof(CY);
784 case VT_ERROR: return sizeof(SCODE);
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_ISBYREF(pvargSrc) && !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 /* Helper macros to check whether bit pattern fits in VARIANT (x is a ULONG64 ) */
1911 #define FITS_AS_I1(x) ((x) >> 8 == 0)
1912 #define FITS_AS_I2(x) ((x) >> 16 == 0)
1913 #define FITS_AS_I4(x) ((x) >> 32 == 0)
1915 /**********************************************************************
1916 * VarNumFromParseNum [OLEAUT32.47]
1918 * Convert a NUMPARSE structure into a numeric Variant type.
1921 * pNumprs [I] Source for parsed number. cDig must be set to the size of rgbDig
1922 * rgbDig [I] Source for the numbers digits
1923 * dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
1924 * pVarDst [O] Destination for the converted Variant value.
1927 * Success: S_OK. pVarDst contains the converted value.
1928 * Failure: E_INVALIDARG, if any parameter is invalid.
1929 * DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
1932 * - The smallest favoured type present in dwVtBits that can represent the
1933 * number in pNumprs without losing precision is used.
1934 * - Signed types are preferrred over unsigned types of the same size.
1935 * - Preferred types in order are: integer, float, double, currency then decimal.
1936 * - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
1937 * for details of the rounding method.
1938 * - pVarDst is not cleared before the result is stored in it.
1940 HRESULT WINAPI VarNumFromParseNum(NUMPARSE *pNumprs, BYTE *rgbDig,
1941 ULONG dwVtBits, VARIANT *pVarDst)
1943 /* Scale factors and limits for double arithmetic */
1944 static const double dblMultipliers[11] = {
1945 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
1946 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
1948 static const double dblMinimums[11] = {
1949 R8_MIN, R8_MIN*10.0, R8_MIN*100.0, R8_MIN*1000.0, R8_MIN*10000.0,
1950 R8_MIN*100000.0, R8_MIN*1000000.0, R8_MIN*10000000.0,
1951 R8_MIN*100000000.0, R8_MIN*1000000000.0, R8_MIN*10000000000.0
1953 static const double dblMaximums[11] = {
1954 R8_MAX, R8_MAX/10.0, R8_MAX/100.0, R8_MAX/1000.0, R8_MAX/10000.0,
1955 R8_MAX/100000.0, R8_MAX/1000000.0, R8_MAX/10000000.0,
1956 R8_MAX/100000000.0, R8_MAX/1000000000.0, R8_MAX/10000000000.0
1959 int wholeNumberDigits, fractionalDigits, divisor10 = 0, multiplier10 = 0;
1961 TRACE("(%p,%p,0x%lx,%p)\n", pNumprs, rgbDig, dwVtBits, pVarDst);
1963 if (pNumprs->nBaseShift)
1965 /* nBaseShift indicates a hex or octal number */
1970 /* Convert the hex or octal number string into a UI64 */
1971 for (i = 0; i < pNumprs->cDig; i++)
1973 if (ul64 > ((UI8_MAX>>pNumprs->nBaseShift) - rgbDig[i]))
1975 TRACE("Overflow multiplying digits\n");
1976 return DISP_E_OVERFLOW;
1978 ul64 = (ul64<<pNumprs->nBaseShift) + rgbDig[i];
1981 /* also make a negative representation */
1984 /* Try signed and unsigned types in size order */
1985 if (dwVtBits & VTBIT_I1 && FITS_AS_I1(ul64))
1987 V_VT(pVarDst) = VT_I1;
1988 V_I1(pVarDst) = ul64;
1991 else if (dwVtBits & VTBIT_UI1 && FITS_AS_I1(ul64))
1993 V_VT(pVarDst) = VT_UI1;
1994 V_UI1(pVarDst) = ul64;
1997 else if (dwVtBits & VTBIT_I2 && FITS_AS_I2(ul64))
1999 V_VT(pVarDst) = VT_I2;
2000 V_I2(pVarDst) = ul64;
2003 else if (dwVtBits & VTBIT_UI2 && FITS_AS_I2(ul64))
2005 V_VT(pVarDst) = VT_UI2;
2006 V_UI2(pVarDst) = ul64;
2009 else if (dwVtBits & VTBIT_I4 && FITS_AS_I4(ul64))
2011 V_VT(pVarDst) = VT_I4;
2012 V_I4(pVarDst) = ul64;
2015 else if (dwVtBits & VTBIT_UI4 && FITS_AS_I4(ul64))
2017 V_VT(pVarDst) = VT_UI4;
2018 V_UI4(pVarDst) = ul64;
2021 else if (dwVtBits & VTBIT_I8 && ((ul64 <= I8_MAX)||(l64>=I8_MIN)))
2023 V_VT(pVarDst) = VT_I8;
2024 V_I8(pVarDst) = ul64;
2027 else if (dwVtBits & VTBIT_UI8)
2029 V_VT(pVarDst) = VT_UI8;
2030 V_UI8(pVarDst) = ul64;
2033 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2035 V_VT(pVarDst) = VT_DECIMAL;
2036 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2037 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2038 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2041 else if (dwVtBits & VTBIT_R4 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2043 V_VT(pVarDst) = VT_R4;
2045 V_R4(pVarDst) = ul64;
2047 V_R4(pVarDst) = l64;
2050 else if (dwVtBits & VTBIT_R8 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2052 V_VT(pVarDst) = VT_R8;
2054 V_R8(pVarDst) = ul64;
2056 V_R8(pVarDst) = l64;
2060 TRACE("Overflow: possible return types: 0x%lx, value: %s\n", dwVtBits, wine_dbgstr_longlong(ul64));
2061 return DISP_E_OVERFLOW;
2064 /* Count the number of relevant fractional and whole digits stored,
2065 * And compute the divisor/multiplier to scale the number by.
2067 if (pNumprs->nPwr10 < 0)
2069 if (-pNumprs->nPwr10 >= pNumprs->cDig)
2071 /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
2072 wholeNumberDigits = 0;
2073 fractionalDigits = pNumprs->cDig;
2074 divisor10 = -pNumprs->nPwr10;
2078 /* An exactly represented real number e.g. 1.024 */
2079 wholeNumberDigits = pNumprs->cDig + pNumprs->nPwr10;
2080 fractionalDigits = pNumprs->cDig - wholeNumberDigits;
2081 divisor10 = pNumprs->cDig - wholeNumberDigits;
2084 else if (pNumprs->nPwr10 == 0)
2086 /* An exactly represented whole number e.g. 1024 */
2087 wholeNumberDigits = pNumprs->cDig;
2088 fractionalDigits = 0;
2090 else /* pNumprs->nPwr10 > 0 */
2092 /* A whole number followed by nPwr10 0's e.g. 102400 */
2093 wholeNumberDigits = pNumprs->cDig;
2094 fractionalDigits = 0;
2095 multiplier10 = pNumprs->nPwr10;
2098 TRACE("cDig %d; nPwr10 %d, whole %d, frac %d ", pNumprs->cDig,
2099 pNumprs->nPwr10, wholeNumberDigits, fractionalDigits);
2100 TRACE("mult %d; div %d\n", multiplier10, divisor10);
2102 if (dwVtBits & (INTEGER_VTBITS|VTBIT_DECIMAL) &&
2103 (!fractionalDigits || !(dwVtBits & (REAL_VTBITS|VTBIT_CY|VTBIT_DECIMAL))))
2105 /* We have one or more integer output choices, and either:
2106 * 1) An integer input value, or
2107 * 2) A real number input value but no floating output choices.
2108 * Alternately, we have a DECIMAL output available and an integer input.
2110 * So, place the integer value into pVarDst, using the smallest type
2111 * possible and preferring signed over unsigned types.
2113 BOOL bOverflow = FALSE, bNegative;
2117 /* Convert the integer part of the number into a UI8 */
2118 for (i = 0; i < wholeNumberDigits; i++)
2120 if (ul64 > (UI8_MAX / 10 - rgbDig[i]))
2122 TRACE("Overflow multiplying digits\n");
2126 ul64 = ul64 * 10 + rgbDig[i];
2129 /* Account for the scale of the number */
2130 if (!bOverflow && multiplier10)
2132 for (i = 0; i < multiplier10; i++)
2134 if (ul64 > (UI8_MAX / 10))
2136 TRACE("Overflow scaling number\n");
2144 /* If we have any fractional digits, round the value.
2145 * Note we don't have to do this if divisor10 is < 1,
2146 * because this means the fractional part must be < 0.5
2148 if (!bOverflow && fractionalDigits && divisor10 > 0)
2150 const BYTE* fracDig = rgbDig + wholeNumberDigits;
2151 BOOL bAdjust = FALSE;
2153 TRACE("first decimal value is %d\n", *fracDig);
2156 bAdjust = TRUE; /* > 0.5 */
2157 else if (*fracDig == 5)
2159 for (i = 1; i < fractionalDigits; i++)
2163 bAdjust = TRUE; /* > 0.5 */
2167 /* If exactly 0.5, round only odd values */
2168 if (i == fractionalDigits && (ul64 & 1))
2174 if (ul64 == UI8_MAX)
2176 TRACE("Overflow after rounding\n");
2183 /* Zero is not a negative number */
2184 bNegative = pNumprs->dwOutFlags & NUMPRS_NEG && ul64 ? TRUE : FALSE;
2186 TRACE("Integer value is %lld, bNeg %d\n", ul64, bNegative);
2188 /* For negative integers, try the signed types in size order */
2189 if (!bOverflow && bNegative)
2191 if (dwVtBits & (VTBIT_I1|VTBIT_I2|VTBIT_I4|VTBIT_I8))
2193 if (dwVtBits & VTBIT_I1 && ul64 <= -I1_MIN)
2195 V_VT(pVarDst) = VT_I1;
2196 V_I1(pVarDst) = -ul64;
2199 else if (dwVtBits & VTBIT_I2 && ul64 <= -I2_MIN)
2201 V_VT(pVarDst) = VT_I2;
2202 V_I2(pVarDst) = -ul64;
2205 else if (dwVtBits & VTBIT_I4 && ul64 <= -((LONGLONG)I4_MIN))
2207 V_VT(pVarDst) = VT_I4;
2208 V_I4(pVarDst) = -ul64;
2211 else if (dwVtBits & VTBIT_I8 && ul64 <= (ULONGLONG)I8_MAX + 1)
2213 V_VT(pVarDst) = VT_I8;
2214 V_I8(pVarDst) = -ul64;
2217 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2219 /* Decimal is only output choice left - fast path */
2220 V_VT(pVarDst) = VT_DECIMAL;
2221 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_NEG,0);
2222 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2223 DEC_LO64(&V_DECIMAL(pVarDst)) = -ul64;
2228 else if (!bOverflow)
2230 /* For positive integers, try signed then unsigned types in size order */
2231 if (dwVtBits & VTBIT_I1 && ul64 <= I1_MAX)
2233 V_VT(pVarDst) = VT_I1;
2234 V_I1(pVarDst) = ul64;
2237 else if (dwVtBits & VTBIT_UI1 && ul64 <= UI1_MAX)
2239 V_VT(pVarDst) = VT_UI1;
2240 V_UI1(pVarDst) = ul64;
2243 else if (dwVtBits & VTBIT_I2 && ul64 <= I2_MAX)
2245 V_VT(pVarDst) = VT_I2;
2246 V_I2(pVarDst) = ul64;
2249 else if (dwVtBits & VTBIT_UI2 && ul64 <= UI2_MAX)
2251 V_VT(pVarDst) = VT_UI2;
2252 V_UI2(pVarDst) = ul64;
2255 else if (dwVtBits & VTBIT_I4 && ul64 <= I4_MAX)
2257 V_VT(pVarDst) = VT_I4;
2258 V_I4(pVarDst) = ul64;
2261 else if (dwVtBits & VTBIT_UI4 && ul64 <= UI4_MAX)
2263 V_VT(pVarDst) = VT_UI4;
2264 V_UI4(pVarDst) = ul64;
2267 else if (dwVtBits & VTBIT_I8 && ul64 <= I8_MAX)
2269 V_VT(pVarDst) = VT_I8;
2270 V_I8(pVarDst) = ul64;
2273 else if (dwVtBits & VTBIT_UI8)
2275 V_VT(pVarDst) = VT_UI8;
2276 V_UI8(pVarDst) = ul64;
2279 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2281 /* Decimal is only output choice left - fast path */
2282 V_VT(pVarDst) = VT_DECIMAL;
2283 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2284 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2285 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2291 if (dwVtBits & REAL_VTBITS)
2293 /* Try to put the number into a float or real */
2294 BOOL bOverflow = FALSE, bNegative = pNumprs->dwOutFlags & NUMPRS_NEG;
2298 /* Convert the number into a double */
2299 for (i = 0; i < pNumprs->cDig; i++)
2300 whole = whole * 10.0 + rgbDig[i];
2302 TRACE("Whole double value is %16.16g\n", whole);
2304 /* Account for the scale */
2305 while (multiplier10 > 10)
2307 if (whole > dblMaximums[10])
2309 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2313 whole = whole * dblMultipliers[10];
2318 if (whole > dblMaximums[multiplier10])
2320 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2324 whole = whole * dblMultipliers[multiplier10];
2327 TRACE("Scaled double value is %16.16g\n", whole);
2329 while (divisor10 > 10)
2331 if (whole < dblMinimums[10] && whole != 0)
2333 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2337 whole = whole / dblMultipliers[10];
2342 if (whole < dblMinimums[divisor10] && whole != 0)
2344 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2348 whole = whole / dblMultipliers[divisor10];
2351 TRACE("Final double value is %16.16g\n", whole);
2353 if (dwVtBits & VTBIT_R4 &&
2354 ((whole <= R4_MAX && whole >= R4_MIN) || whole == 0.0))
2356 TRACE("Set R4 to final value\n");
2357 V_VT(pVarDst) = VT_R4; /* Fits into a float */
2358 V_R4(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2362 if (dwVtBits & VTBIT_R8)
2364 TRACE("Set R8 to final value\n");
2365 V_VT(pVarDst) = VT_R8; /* Fits into a double */
2366 V_R8(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2370 if (dwVtBits & VTBIT_CY)
2372 if (SUCCEEDED(VarCyFromR8(bNegative ? -whole : whole, &V_CY(pVarDst))))
2374 V_VT(pVarDst) = VT_CY; /* Fits into a currency */
2375 TRACE("Set CY to final value\n");
2378 TRACE("Value Overflows CY\n");
2382 if (dwVtBits & VTBIT_DECIMAL)
2387 DECIMAL* pDec = &V_DECIMAL(pVarDst);
2389 DECIMAL_SETZERO(*pDec);
2392 if (pNumprs->dwOutFlags & NUMPRS_NEG)
2393 DEC_SIGN(pDec) = DECIMAL_NEG;
2395 DEC_SIGN(pDec) = DECIMAL_POS;
2397 /* Factor the significant digits */
2398 for (i = 0; i < pNumprs->cDig; i++)
2400 tmp = (ULONG64)DEC_LO32(pDec) * 10 + rgbDig[i];
2401 carry = (ULONG)(tmp >> 32);
2402 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2403 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2404 carry = (ULONG)(tmp >> 32);
2405 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2406 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2407 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2409 if (tmp >> 32 & UI4_MAX)
2411 VarNumFromParseNum_DecOverflow:
2412 TRACE("Overflow\n");
2413 DEC_LO32(pDec) = DEC_MID32(pDec) = DEC_HI32(pDec) = UI4_MAX;
2414 return DISP_E_OVERFLOW;
2418 /* Account for the scale of the number */
2419 while (multiplier10 > 0)
2421 tmp = (ULONG64)DEC_LO32(pDec) * 10;
2422 carry = (ULONG)(tmp >> 32);
2423 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2424 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2425 carry = (ULONG)(tmp >> 32);
2426 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2427 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2428 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2430 if (tmp >> 32 & UI4_MAX)
2431 goto VarNumFromParseNum_DecOverflow;
2434 DEC_SCALE(pDec) = divisor10;
2436 V_VT(pVarDst) = VT_DECIMAL;
2439 return DISP_E_OVERFLOW; /* No more output choices */
2442 /**********************************************************************
2443 * VarCat [OLEAUT32.318]
2445 HRESULT WINAPI VarCat(LPVARIANT left, LPVARIANT right, LPVARIANT out)
2447 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2448 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), out);
2450 /* Should we VariantClear out? */
2451 /* Can we handle array, vector, by ref etc. */
2452 if ((V_VT(left)&VT_TYPEMASK) == VT_NULL &&
2453 (V_VT(right)&VT_TYPEMASK) == VT_NULL)
2455 V_VT(out) = VT_NULL;
2459 if (V_VT(left) == VT_BSTR && V_VT(right) == VT_BSTR)
2461 V_VT(out) = VT_BSTR;
2462 VarBstrCat (V_BSTR(left), V_BSTR(right), &V_BSTR(out));
2465 if (V_VT(left) == VT_BSTR) {
2469 V_VT(out) = VT_BSTR;
2470 VariantInit(&bstrvar);
2471 hres = VariantChangeTypeEx(&bstrvar,right,0,0,VT_BSTR);
2473 FIXME("Failed to convert right side from vt %d to VT_BSTR?\n",V_VT(right));
2476 VarBstrCat (V_BSTR(left), V_BSTR(&bstrvar), &V_BSTR(out));
2479 if (V_VT(right) == VT_BSTR) {
2483 V_VT(out) = VT_BSTR;
2484 VariantInit(&bstrvar);
2485 hres = VariantChangeTypeEx(&bstrvar,left,0,0,VT_BSTR);
2487 FIXME("Failed to convert right side from vt %d to VT_BSTR?\n",V_VT(right));
2490 VarBstrCat (V_BSTR(&bstrvar), V_BSTR(right), &V_BSTR(out));
2493 FIXME ("types %d / %d not supported\n",V_VT(left)&VT_TYPEMASK, V_VT(right)&VT_TYPEMASK);
2497 /**********************************************************************
2498 * VarCmp [OLEAUT32.176]
2501 * NORM_IGNORECASE, NORM_IGNORENONSPACE, NORM_IGNORESYMBOLS
2502 * NORM_IGNOREWIDTH, NORM_IGNOREKANATYPE, NORM_IGNOREKASHIDA
2505 HRESULT WINAPI VarCmp(LPVARIANT left, LPVARIANT right, LCID lcid, DWORD flags)
2515 TRACE("(%p->(%s%s),%p->(%s%s),0x%08lx,0x%08lx)\n", left, debugstr_VT(left),
2516 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), lcid, flags);
2518 VariantInit(&lv);VariantInit(&rv);
2519 V_VT(right) &= ~0x8000; /* hack since we sometime get this flag. */
2520 V_VT(left) &= ~0x8000; /* hack since we sometime get this flag. */
2522 /* If either are null, then return VARCMP_NULL */
2523 if ((V_VT(left)&VT_TYPEMASK) == VT_NULL ||
2524 (V_VT(right)&VT_TYPEMASK) == VT_NULL)
2527 /* Strings - use VarBstrCmp */
2528 if ((V_VT(left)&VT_TYPEMASK) == VT_BSTR &&
2529 (V_VT(right)&VT_TYPEMASK) == VT_BSTR) {
2530 return VarBstrCmp(V_BSTR(left), V_BSTR(right), lcid, flags);
2533 xmask = (1<<(V_VT(left)&VT_TYPEMASK))|(1<<(V_VT(right)&VT_TYPEMASK));
2534 if (xmask & VTBIT_R8) {
2535 rc = VariantChangeType(&lv,left,0,VT_R8);
2536 if (FAILED(rc)) return rc;
2537 rc = VariantChangeType(&rv,right,0,VT_R8);
2538 if (FAILED(rc)) return rc;
2540 if (V_R8(&lv) == V_R8(&rv)) return VARCMP_EQ;
2541 if (V_R8(&lv) < V_R8(&rv)) return VARCMP_LT;
2542 if (V_R8(&lv) > V_R8(&rv)) return VARCMP_GT;
2543 return E_FAIL; /* can't get here */
2545 if (xmask & VTBIT_R4) {
2546 rc = VariantChangeType(&lv,left,0,VT_R4);
2547 if (FAILED(rc)) return rc;
2548 rc = VariantChangeType(&rv,right,0,VT_R4);
2549 if (FAILED(rc)) return rc;
2551 if (V_R4(&lv) == V_R4(&rv)) return VARCMP_EQ;
2552 if (V_R4(&lv) < V_R4(&rv)) return VARCMP_LT;
2553 if (V_R4(&lv) > V_R4(&rv)) return VARCMP_GT;
2554 return E_FAIL; /* can't get here */
2557 /* Integers - Ideally like to use VarDecCmp, but no Dec support yet
2558 Use LONGLONG to maximize ranges */
2560 switch (V_VT(left)&VT_TYPEMASK) {
2561 case VT_I1 : lVal = V_I1(left); break;
2562 case VT_I2 : lVal = V_I2(left); break;
2564 case VT_INT : lVal = V_I4(left); break;
2565 case VT_UI1 : lVal = V_UI1(left); break;
2566 case VT_UI2 : lVal = V_UI2(left); break;
2568 case VT_UINT : lVal = V_UI4(left); break;
2569 case VT_BOOL : lVal = V_BOOL(left); break;
2570 default: lOk = FALSE;
2574 switch (V_VT(right)&VT_TYPEMASK) {
2575 case VT_I1 : rVal = V_I1(right); break;
2576 case VT_I2 : rVal = V_I2(right); break;
2578 case VT_INT : rVal = V_I4(right); break;
2579 case VT_UI1 : rVal = V_UI1(right); break;
2580 case VT_UI2 : rVal = V_UI2(right); break;
2582 case VT_UINT : rVal = V_UI4(right); break;
2583 case VT_BOOL : rVal = V_BOOL(right); break;
2584 default: rOk = FALSE;
2590 } else if (lVal > rVal) {
2597 /* Strings - use VarBstrCmp */
2598 if ((V_VT(left)&VT_TYPEMASK) == VT_DATE &&
2599 (V_VT(right)&VT_TYPEMASK) == VT_DATE) {
2601 if (floor(V_DATE(left)) == floor(V_DATE(right))) {
2602 /* Due to floating point rounding errors, calculate varDate in whole numbers) */
2603 double wholePart = 0.0;
2607 /* Get the fraction * 24*60*60 to make it into whole seconds */
2608 wholePart = (double) floor( V_DATE(left) );
2609 if (wholePart == 0) wholePart = 1;
2610 leftR = floor(fmod( V_DATE(left), wholePart ) * (24*60*60));
2612 wholePart = (double) floor( V_DATE(right) );
2613 if (wholePart == 0) wholePart = 1;
2614 rightR = floor(fmod( V_DATE(right), wholePart ) * (24*60*60));
2616 if (leftR < rightR) {
2618 } else if (leftR > rightR) {
2624 } else if (V_DATE(left) < V_DATE(right)) {
2626 } else if (V_DATE(left) > V_DATE(right)) {
2630 FIXME("VarCmp partial implementation, doesn't support vt 0x%x / 0x%x\n",V_VT(left), V_VT(right));
2634 /**********************************************************************
2635 * VarAnd [OLEAUT32.142]
2638 HRESULT WINAPI VarAnd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2640 HRESULT rc = E_FAIL;
2642 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2643 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2645 if ((V_VT(left)&VT_TYPEMASK) == VT_BOOL &&
2646 (V_VT(right)&VT_TYPEMASK) == VT_BOOL) {
2648 V_VT(result) = VT_BOOL;
2649 if (V_BOOL(left) && V_BOOL(right)) {
2650 V_BOOL(result) = VARIANT_TRUE;
2652 V_BOOL(result) = VARIANT_FALSE;
2663 int resT = 0; /* Testing has shown I2 & I2 == I2, all else
2664 becomes I4, even unsigned ints (incl. UI2) */
2667 switch (V_VT(left)&VT_TYPEMASK) {
2668 case VT_I1 : lVal = V_I1(left); resT=VT_I4; break;
2669 case VT_I2 : lVal = V_I2(left); resT=VT_I2; break;
2671 case VT_INT : lVal = V_I4(left); resT=VT_I4; break;
2672 case VT_UI1 : lVal = V_UI1(left); resT=VT_I4; break;
2673 case VT_UI2 : lVal = V_UI2(left); resT=VT_I4; break;
2675 case VT_UINT : lVal = V_UI4(left); resT=VT_I4; break;
2676 case VT_BOOL : rVal = V_BOOL(left); resT=VT_I4; break;
2677 default: lOk = FALSE;
2681 switch (V_VT(right)&VT_TYPEMASK) {
2682 case VT_I1 : rVal = V_I1(right); resT=VT_I4; break;
2683 case VT_I2 : rVal = V_I2(right); resT=max(VT_I2, resT); break;
2685 case VT_INT : rVal = V_I4(right); resT=VT_I4; break;
2686 case VT_UI1 : rVal = V_UI1(right); resT=VT_I4; break;
2687 case VT_UI2 : rVal = V_UI2(right); resT=VT_I4; break;
2689 case VT_UINT : rVal = V_UI4(right); resT=VT_I4; break;
2690 case VT_BOOL : rVal = V_BOOL(right); resT=VT_I4; break;
2691 default: rOk = FALSE;
2695 res = (lVal & rVal);
2696 V_VT(result) = resT;
2698 case VT_I2 : V_I2(result) = res; break;
2699 case VT_I4 : V_I4(result) = res; break;
2701 FIXME("Unexpected result variant type %x\n", resT);
2707 FIXME("VarAnd stub\n");
2711 TRACE("returning 0x%8lx (%s%s),%ld\n", rc, debugstr_VT(result),
2712 debugstr_VF(result), V_VT(result) == VT_I4 ? V_I4(result) : V_I2(result));
2716 /**********************************************************************
2717 * VarAdd [OLEAUT32.141]
2718 * FIXME: From MSDN: If ... Then
2719 * Both expressions are of the string type Concatenated.
2720 * One expression is a string type and the other a character Addition.
2721 * One expression is numeric and the other is a string Addition.
2722 * Both expressions are numeric Addition.
2723 * Either expression is NULL NULL is returned.
2724 * Both expressions are empty Integer subtype is returned.
2727 HRESULT WINAPI VarAdd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2729 HRESULT rc = E_FAIL;
2731 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2732 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2734 if ((V_VT(left)&VT_TYPEMASK) == VT_EMPTY)
2735 return VariantCopy(result,right);
2737 if ((V_VT(right)&VT_TYPEMASK) == VT_EMPTY)
2738 return VariantCopy(result,left);
2740 /* check if we add doubles */
2741 if (((V_VT(left)&VT_TYPEMASK) == VT_R8) || ((V_VT(right)&VT_TYPEMASK) == VT_R8)) {
2749 switch (V_VT(left)&VT_TYPEMASK) {
2750 case VT_I1 : lVal = V_I1(left); break;
2751 case VT_I2 : lVal = V_I2(left); break;
2753 case VT_INT : lVal = V_I4(left); break;
2754 case VT_UI1 : lVal = V_UI1(left); break;
2755 case VT_UI2 : lVal = V_UI2(left); break;
2757 case VT_UINT : lVal = V_UI4(left); break;
2758 case VT_R4 : lVal = V_R4(left); break;
2759 case VT_R8 : lVal = V_R8(left); break;
2760 case VT_NULL : lVal = 0.0; break;
2761 default: lOk = FALSE;
2765 switch (V_VT(right)&VT_TYPEMASK) {
2766 case VT_I1 : rVal = V_I1(right); break;
2767 case VT_I2 : rVal = V_I2(right); break;
2769 case VT_INT : rVal = V_I4(right); break;
2770 case VT_UI1 : rVal = V_UI1(right); break;
2771 case VT_UI2 : rVal = V_UI2(right); break;
2773 case VT_UINT : rVal = V_UI4(right); break;
2774 case VT_R4 : rVal = V_R4(right);break;
2775 case VT_R8 : rVal = V_R8(right);break;
2776 case VT_NULL : rVal = 0.0; break;
2777 default: rOk = FALSE;
2781 res = (lVal + rVal);
2782 V_VT(result) = VT_R8;
2786 FIXME("Unhandled type pair %d / %d in double addition.\n",
2787 (V_VT(left)&VT_TYPEMASK),
2788 (V_VT(right)&VT_TYPEMASK)
2794 /* now check if we add floats. VT_R8 can no longer happen here! */
2795 if (((V_VT(left)&VT_TYPEMASK) == VT_R4) || ((V_VT(right)&VT_TYPEMASK) == VT_R4)) {
2803 switch (V_VT(left)&VT_TYPEMASK) {
2804 case VT_I1 : lVal = V_I1(left); break;
2805 case VT_I2 : lVal = V_I2(left); break;
2807 case VT_INT : lVal = V_I4(left); break;
2808 case VT_UI1 : lVal = V_UI1(left); break;
2809 case VT_UI2 : lVal = V_UI2(left); break;
2811 case VT_UINT : lVal = V_UI4(left); break;
2812 case VT_R4 : lVal = V_R4(left); break;
2813 case VT_NULL : lVal = 0.0; break;
2814 default: lOk = FALSE;
2818 switch (V_VT(right)&VT_TYPEMASK) {
2819 case VT_I1 : rVal = V_I1(right); break;
2820 case VT_I2 : rVal = V_I2(right); break;
2822 case VT_INT : rVal = V_I4(right); break;
2823 case VT_UI1 : rVal = V_UI1(right); break;
2824 case VT_UI2 : rVal = V_UI2(right); break;
2826 case VT_UINT : rVal = V_UI4(right); break;
2827 case VT_R4 : rVal = V_R4(right);break;
2828 case VT_NULL : rVal = 0.0; break;
2829 default: rOk = FALSE;
2833 res = (lVal + rVal);
2834 V_VT(result) = VT_R4;
2838 FIXME("Unhandled type pair %d / %d in float addition.\n",
2839 (V_VT(left)&VT_TYPEMASK),
2840 (V_VT(right)&VT_TYPEMASK)
2846 /* Handle strings as concat */
2847 if ((V_VT(left)&VT_TYPEMASK) == VT_BSTR &&
2848 (V_VT(right)&VT_TYPEMASK) == VT_BSTR) {
2849 V_VT(result) = VT_BSTR;
2850 return VarBstrCat(V_BSTR(left), V_BSTR(right), &V_BSTR(result));
2859 int resT = 0; /* Testing has shown I2 + I2 == I2, all else
2863 switch (V_VT(left)&VT_TYPEMASK) {
2864 case VT_I1 : lVal = V_I1(left); resT=VT_I4; break;
2865 case VT_I2 : lVal = V_I2(left); resT=VT_I2; break;
2867 case VT_INT : lVal = V_I4(left); resT=VT_I4; break;
2868 case VT_UI1 : lVal = V_UI1(left); resT=VT_I4; break;
2869 case VT_UI2 : lVal = V_UI2(left); resT=VT_I4; break;
2871 case VT_UINT : lVal = V_UI4(left); resT=VT_I4; break;
2872 case VT_NULL : lVal = 0; resT = VT_I4; break;
2873 default: lOk = FALSE;
2877 switch (V_VT(right)&VT_TYPEMASK) {
2878 case VT_I1 : rVal = V_I1(right); resT=VT_I4; break;
2879 case VT_I2 : rVal = V_I2(right); resT=max(VT_I2, resT); break;
2881 case VT_INT : rVal = V_I4(right); resT=VT_I4; break;
2882 case VT_UI1 : rVal = V_UI1(right); resT=VT_I4; break;
2883 case VT_UI2 : rVal = V_UI2(right); resT=VT_I4; break;
2885 case VT_UINT : rVal = V_UI4(right); resT=VT_I4; break;
2886 case VT_NULL : rVal = 0; resT=VT_I4; break;
2887 default: rOk = FALSE;
2891 res = (lVal + rVal);
2892 V_VT(result) = resT;
2894 case VT_I2 : V_I2(result) = res; break;
2895 case VT_I4 : V_I4(result) = res; break;
2897 FIXME("Unexpected result variant type %x\n", resT);
2903 FIXME("unimplemented part (0x%x + 0x%x)\n",V_VT(left), V_VT(right));
2907 TRACE("returning 0x%8lx (%s%s),%ld\n", rc, debugstr_VT(result),
2908 debugstr_VF(result), V_VT(result) == VT_I4 ? V_I4(result) : V_I2(result));
2912 /**********************************************************************
2913 * VarMul [OLEAUT32.156]
2916 HRESULT WINAPI VarMul(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2918 HRESULT rc = E_FAIL;
2919 VARTYPE lvt,rvt,resvt;
2923 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2924 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2926 VariantInit(&lv);VariantInit(&rv);
2927 lvt = V_VT(left)&VT_TYPEMASK;
2928 rvt = V_VT(right)&VT_TYPEMASK;
2929 found = FALSE;resvt=VT_VOID;
2930 if (((1<<lvt) | (1<<rvt)) & (VTBIT_R4|VTBIT_R8)) {
2934 if (!found && (((1<<lvt) | (1<<rvt)) & (VTBIT_I1|VTBIT_I2|VTBIT_UI1|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|(1<<VT_INT)|(1<<VT_UINT)))) {
2939 FIXME("can't expand vt %d vs %d to a target type.\n",lvt,rvt);
2942 rc = VariantChangeType(&lv, left, 0, resvt);
2944 FIXME("Could not convert 0x%x to %d?\n",V_VT(left),resvt);
2947 rc = VariantChangeType(&rv, right, 0, resvt);
2949 FIXME("Could not convert 0x%x to %d?\n",V_VT(right),resvt);
2954 V_VT(result) = resvt;
2955 V_R8(result) = V_R8(&lv) * V_R8(&rv);
2959 V_VT(result) = resvt;
2960 V_I4(result) = V_I4(&lv) * V_I4(&rv);
2964 TRACE("returning 0x%8lx (%s%s),%g\n", rc, debugstr_VT(result),
2965 debugstr_VF(result), V_VT(result) == VT_R8 ? V_R8(result) : (double)V_I4(result));
2969 /**********************************************************************
2970 * VarDiv [OLEAUT32.143]
2973 HRESULT WINAPI VarDiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2975 HRESULT rc = E_FAIL;
2976 VARTYPE lvt,rvt,resvt;
2980 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2981 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2983 VariantInit(&lv);VariantInit(&rv);
2984 lvt = V_VT(left)&VT_TYPEMASK;
2985 rvt = V_VT(right)&VT_TYPEMASK;
2986 found = FALSE;resvt = VT_VOID;
2987 if (((1<<lvt) | (1<<rvt)) & (VTBIT_R4|VTBIT_R8)) {
2991 if (!found && (((1<<lvt) | (1<<rvt)) & (VTBIT_I1|VTBIT_I2|VTBIT_UI1|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|(1<<VT_INT)|(1<<VT_UINT)))) {
2996 FIXME("can't expand vt %d vs %d to a target type.\n",lvt,rvt);
2999 rc = VariantChangeType(&lv, left, 0, resvt);
3001 FIXME("Could not convert 0x%x to %d?\n",V_VT(left),resvt);
3004 rc = VariantChangeType(&rv, right, 0, resvt);
3006 FIXME("Could not convert 0x%x to %d?\n",V_VT(right),resvt);
3011 if (V_R8(&rv) == 0) return DISP_E_DIVBYZERO;
3012 V_VT(result) = resvt;
3013 V_R8(result) = V_R8(&lv) / V_R8(&rv);
3017 if (V_I4(&rv) == 0) return DISP_E_DIVBYZERO;
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)) & (VTBIT_I1|VTBIT_I2|VTBIT_UI1|VTBIT_UI2|VTBIT_I4|VTBIT_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 complement
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));