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
31 #define NONAMELESSUNION
32 #define NONAMELESSSTRUCT
36 #include "wine/debug.h"
37 #include "wine/unicode.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(variant);
43 const char* wine_vtypes[VT_CLSID] =
45 "VT_EMPTY","VT_NULL","VT_I2","VT_I4","VT_R4","VT_R8","VT_CY","VT_DATE",
46 "VT_BSTR","VT_DISPATCH","VT_ERROR","VT_BOOL","VT_VARIANT","VT_UNKNOWN",
47 "VT_DECIMAL","15","VT_I1","VT_UI1","VT_UI2","VT_UI4","VT_I8","VT_UI8",
48 "VT_INT","VT_UINT","VT_VOID","VT_HRESULT","VT_PTR","VT_SAFEARRAY",
49 "VT_CARRAY","VT_USERDEFINED","VT_LPSTR","VT_LPWSTR""32","33","34","35",
50 "VT_RECORD","VT_INT_PTR","VT_UINT_PTR","39","40","41","42","43","44","45",
51 "46","47","48","49","50","51","52","53","54","55","56","57","58","59","60",
52 "61","62","63","VT_FILETIME","VT_BLOB","VT_STREAM","VT_STORAGE",
53 "VT_STREAMED_OBJECT","VT_STORED_OBJECT","VT_BLOB_OBJECT","VT_CF","VT_CLSID"
56 const char* wine_vflags[16] =
61 "|VT_VECTOR|VT_ARRAY",
63 "|VT_VECTOR|VT_ARRAY",
65 "|VT_VECTOR|VT_ARRAY|VT_BYREF",
67 "|VT_VECTOR|VT_HARDTYPE",
68 "|VT_ARRAY|VT_HARDTYPE",
69 "|VT_VECTOR|VT_ARRAY|VT_HARDTYPE",
70 "|VT_BYREF|VT_HARDTYPE",
71 "|VT_VECTOR|VT_ARRAY|VT_HARDTYPE",
72 "|VT_ARRAY|VT_BYREF|VT_HARDTYPE",
73 "|VT_VECTOR|VT_ARRAY|VT_BYREF|VT_HARDTYPE",
76 /* Convert a variant from one type to another */
77 static inline HRESULT VARIANT_Coerce(VARIANTARG* pd, LCID lcid, USHORT wFlags,
78 VARIANTARG* ps, VARTYPE vt)
80 HRESULT res = DISP_E_TYPEMISMATCH;
81 VARTYPE vtFrom = V_TYPE(ps);
82 BOOL bIgnoreOverflow = FALSE;
85 TRACE("(%p->(%s%s),0x%08lx,0x%04x,%p->(%s%s),%s%s)\n", pd, debugstr_VT(pd),
86 debugstr_VF(pd), lcid, wFlags, ps, debugstr_VT(ps), debugstr_VF(ps),
87 debugstr_vt(vt), debugstr_vf(vt));
89 if (vt == VT_BSTR || vtFrom == VT_BSTR)
91 /* All flags passed to low level function are only used for
92 * changing to or from strings. Map these here.
94 if (wFlags & VARIANT_LOCALBOOL)
95 dwFlags |= VAR_LOCALBOOL;
96 if (wFlags & VARIANT_CALENDAR_HIJRI)
97 dwFlags |= VAR_CALENDAR_HIJRI;
98 if (wFlags & VARIANT_CALENDAR_THAI)
99 dwFlags |= VAR_CALENDAR_THAI;
100 if (wFlags & VARIANT_CALENDAR_GREGORIAN)
101 dwFlags |= VAR_CALENDAR_GREGORIAN;
102 if (wFlags & VARIANT_NOUSEROVERRIDE)
103 dwFlags |= LOCALE_NOUSEROVERRIDE;
104 if (wFlags & VARIANT_USE_NLS)
105 dwFlags |= LOCALE_USE_NLS;
108 /* Map int/uint to i4/ui4 */
111 else if (vt == VT_UINT)
114 if (vtFrom == VT_INT)
116 else if (vtFrom == VT_UINT)
120 bIgnoreOverflow = TRUE;
124 return VariantCopy(pd, ps);
126 if (wFlags & VARIANT_NOVALUEPROP && vtFrom == VT_DISPATCH && vt != VT_UNKNOWN)
128 /* VARIANT_NOVALUEPROP prevents IDispatch objects from being coerced by
129 * accessing the default object property.
131 return DISP_E_TYPEMISMATCH;
137 if (vtFrom == VT_NULL)
138 return DISP_E_TYPEMISMATCH;
139 /* ... Fall through */
141 if (vtFrom <= VT_UINT && vtFrom != (VARTYPE)15 && vtFrom != VT_ERROR)
143 res = VariantClear( pd );
144 if (vt == VT_NULL && SUCCEEDED(res))
152 case VT_EMPTY: V_I1(pd) = 0; return S_OK;
153 case VT_I2: return VarI1FromI2(V_I2(ps), &V_I1(pd));
154 case VT_I4: return VarI1FromI4(V_I4(ps), &V_I1(pd));
155 case VT_UI1: return VarI1FromUI1(V_UI1(ps), &V_I1(pd));
156 case VT_UI2: return VarI1FromUI2(V_UI2(ps), &V_I1(pd));
157 case VT_UI4: return VarI1FromUI4(V_UI4(ps), &V_I1(pd));
158 case VT_I8: return VarI1FromI8(V_I8(ps), &V_I1(pd));
159 case VT_UI8: return VarI1FromUI8(V_UI8(ps), &V_I1(pd));
160 case VT_R4: return VarI1FromR4(V_R4(ps), &V_I1(pd));
161 case VT_R8: return VarI1FromR8(V_R8(ps), &V_I1(pd));
162 case VT_DATE: return VarI1FromDate(V_DATE(ps), &V_I1(pd));
163 case VT_BOOL: return VarI1FromBool(V_BOOL(ps), &V_I1(pd));
164 case VT_CY: return VarI1FromCy(V_CY(ps), &V_I1(pd));
165 case VT_DECIMAL: return VarI1FromDec(&V_DECIMAL(ps), &V_I1(pd) );
166 case VT_DISPATCH: return VarI1FromDisp(V_DISPATCH(ps), lcid, &V_I1(pd) );
167 case VT_BSTR: return VarI1FromStr(V_BSTR(ps), lcid, dwFlags, &V_I1(pd) );
174 case VT_EMPTY: V_I2(pd) = 0; return S_OK;
175 case VT_I1: return VarI2FromI1(V_I1(ps), &V_I2(pd));
176 case VT_I4: return VarI2FromI4(V_I4(ps), &V_I2(pd));
177 case VT_UI1: return VarI2FromUI1(V_UI1(ps), &V_I2(pd));
178 case VT_UI2: return VarI2FromUI2(V_UI2(ps), &V_I2(pd));
179 case VT_UI4: return VarI2FromUI4(V_UI4(ps), &V_I2(pd));
180 case VT_I8: return VarI2FromI8(V_I8(ps), &V_I2(pd));
181 case VT_UI8: return VarI2FromUI8(V_UI8(ps), &V_I2(pd));
182 case VT_R4: return VarI2FromR4(V_R4(ps), &V_I2(pd));
183 case VT_R8: return VarI2FromR8(V_R8(ps), &V_I2(pd));
184 case VT_DATE: return VarI2FromDate(V_DATE(ps), &V_I2(pd));
185 case VT_BOOL: return VarI2FromBool(V_BOOL(ps), &V_I2(pd));
186 case VT_CY: return VarI2FromCy(V_CY(ps), &V_I2(pd));
187 case VT_DECIMAL: return VarI2FromDec(&V_DECIMAL(ps), &V_I2(pd));
188 case VT_DISPATCH: return VarI2FromDisp(V_DISPATCH(ps), lcid, &V_I2(pd));
189 case VT_BSTR: return VarI2FromStr(V_BSTR(ps), lcid, dwFlags, &V_I2(pd));
196 case VT_EMPTY: V_I4(pd) = 0; return S_OK;
197 case VT_I1: return VarI4FromI1(V_I1(ps), &V_I4(pd));
198 case VT_I2: return VarI4FromI2(V_I2(ps), &V_I4(pd));
199 case VT_UI1: return VarI4FromUI1(V_UI1(ps), &V_I4(pd));
200 case VT_UI2: return VarI4FromUI2(V_UI2(ps), &V_I4(pd));
208 return VarI4FromUI4(V_UI4(ps), &V_I4(pd));
209 case VT_I8: return VarI4FromI8(V_I8(ps), &V_I4(pd));
210 case VT_UI8: return VarI4FromUI8(V_UI8(ps), &V_I4(pd));
211 case VT_R4: return VarI4FromR4(V_R4(ps), &V_I4(pd));
212 case VT_R8: return VarI4FromR8(V_R8(ps), &V_I4(pd));
213 case VT_DATE: return VarI4FromDate(V_DATE(ps), &V_I4(pd));
214 case VT_BOOL: return VarI4FromBool(V_BOOL(ps), &V_I4(pd));
215 case VT_CY: return VarI4FromCy(V_CY(ps), &V_I4(pd));
216 case VT_DECIMAL: return VarI4FromDec(&V_DECIMAL(ps), &V_I4(pd));
217 case VT_DISPATCH: return VarI4FromDisp(V_DISPATCH(ps), lcid, &V_I4(pd));
218 case VT_BSTR: return VarI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_I4(pd));
225 case VT_EMPTY: V_UI1(pd) = 0; return S_OK;
226 case VT_I1: return VarUI1FromI1(V_I1(ps), &V_UI1(pd));
227 case VT_I2: return VarUI1FromI2(V_I2(ps), &V_UI1(pd));
228 case VT_I4: return VarUI1FromI4(V_I4(ps), &V_UI1(pd));
229 case VT_UI2: return VarUI1FromUI2(V_UI2(ps), &V_UI1(pd));
230 case VT_UI4: return VarUI1FromUI4(V_UI4(ps), &V_UI1(pd));
231 case VT_I8: return VarUI1FromI8(V_I8(ps), &V_UI1(pd));
232 case VT_UI8: return VarUI1FromUI8(V_UI8(ps), &V_UI1(pd));
233 case VT_R4: return VarUI1FromR4(V_R4(ps), &V_UI1(pd));
234 case VT_R8: return VarUI1FromR8(V_R8(ps), &V_UI1(pd));
235 case VT_DATE: return VarUI1FromDate(V_DATE(ps), &V_UI1(pd));
236 case VT_BOOL: return VarUI1FromBool(V_BOOL(ps), &V_UI1(pd));
237 case VT_CY: return VarUI1FromCy(V_CY(ps), &V_UI1(pd));
238 case VT_DECIMAL: return VarUI1FromDec(&V_DECIMAL(ps), &V_UI1(pd));
239 case VT_DISPATCH: return VarUI1FromDisp(V_DISPATCH(ps), lcid, &V_UI1(pd));
240 case VT_BSTR: return VarUI1FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI1(pd));
247 case VT_EMPTY: V_UI2(pd) = 0; return S_OK;
248 case VT_I1: return VarUI2FromI1(V_I1(ps), &V_UI2(pd));
249 case VT_I2: return VarUI2FromI2(V_I2(ps), &V_UI2(pd));
250 case VT_I4: return VarUI2FromI4(V_I4(ps), &V_UI2(pd));
251 case VT_UI1: return VarUI2FromUI1(V_UI1(ps), &V_UI2(pd));
252 case VT_UI4: return VarUI2FromUI4(V_UI4(ps), &V_UI2(pd));
253 case VT_I8: return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
254 case VT_UI8: return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
255 case VT_R4: return VarUI2FromR4(V_R4(ps), &V_UI2(pd));
256 case VT_R8: return VarUI2FromR8(V_R8(ps), &V_UI2(pd));
257 case VT_DATE: return VarUI2FromDate(V_DATE(ps), &V_UI2(pd));
258 case VT_BOOL: return VarUI2FromBool(V_BOOL(ps), &V_UI2(pd));
259 case VT_CY: return VarUI2FromCy(V_CY(ps), &V_UI2(pd));
260 case VT_DECIMAL: return VarUI2FromDec(&V_DECIMAL(ps), &V_UI2(pd));
261 case VT_DISPATCH: return VarUI2FromDisp(V_DISPATCH(ps), lcid, &V_UI2(pd));
262 case VT_BSTR: return VarUI2FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI2(pd));
269 case VT_EMPTY: V_UI4(pd) = 0; return S_OK;
270 case VT_I1: return VarUI4FromI1(V_I1(ps), &V_UI4(pd));
271 case VT_I2: return VarUI4FromI2(V_I2(ps), &V_UI4(pd));
272 case VT_I4: return VarUI4FromI4(V_I4(ps), &V_UI4(pd));
273 case VT_UI1: return VarUI4FromUI1(V_UI1(ps), &V_UI4(pd));
274 case VT_UI2: return VarUI4FromUI2(V_UI2(ps), &V_UI4(pd));
275 case VT_I8: return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
276 case VT_UI8: return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
277 case VT_R4: return VarUI4FromR4(V_R4(ps), &V_UI4(pd));
278 case VT_R8: return VarUI4FromR8(V_R8(ps), &V_UI4(pd));
279 case VT_DATE: return VarUI4FromDate(V_DATE(ps), &V_UI4(pd));
280 case VT_BOOL: return VarUI4FromBool(V_BOOL(ps), &V_UI4(pd));
281 case VT_CY: return VarUI4FromCy(V_CY(ps), &V_UI4(pd));
282 case VT_DECIMAL: return VarUI4FromDec(&V_DECIMAL(ps), &V_UI4(pd));
283 case VT_DISPATCH: return VarUI4FromDisp(V_DISPATCH(ps), lcid, &V_UI4(pd));
284 case VT_BSTR: return VarUI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI4(pd));
291 case VT_EMPTY: V_UI8(pd) = 0; return S_OK;
292 case VT_I4: if (V_I4(ps) < 0) return DISP_E_OVERFLOW; V_UI8(pd) = V_I4(ps); return S_OK;
293 case VT_I1: return VarUI8FromI1(V_I1(ps), &V_UI8(pd));
294 case VT_I2: return VarUI8FromI2(V_I2(ps), &V_UI8(pd));
295 case VT_UI1: return VarUI8FromUI1(V_UI1(ps), &V_UI8(pd));
296 case VT_UI2: return VarUI8FromUI2(V_UI2(ps), &V_UI8(pd));
297 case VT_UI4: return VarUI8FromUI4(V_UI4(ps), &V_UI8(pd));
298 case VT_I8: return VarUI8FromI8(V_I8(ps), &V_UI8(pd));
299 case VT_R4: return VarUI8FromR4(V_R4(ps), &V_UI8(pd));
300 case VT_R8: return VarUI8FromR8(V_R8(ps), &V_UI8(pd));
301 case VT_DATE: return VarUI8FromDate(V_DATE(ps), &V_UI8(pd));
302 case VT_BOOL: return VarUI8FromBool(V_BOOL(ps), &V_UI8(pd));
303 case VT_CY: return VarUI8FromCy(V_CY(ps), &V_UI8(pd));
304 case VT_DECIMAL: return VarUI8FromDec(&V_DECIMAL(ps), &V_UI8(pd));
305 case VT_DISPATCH: return VarUI8FromDisp(V_DISPATCH(ps), lcid, &V_UI8(pd));
306 case VT_BSTR: return VarUI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI8(pd));
313 case VT_EMPTY: V_I8(pd) = 0; return S_OK;
314 case VT_I4: V_I8(pd) = V_I4(ps); return S_OK;
315 case VT_I1: return VarI8FromI1(V_I1(ps), &V_I8(pd));
316 case VT_I2: return VarI8FromI2(V_I2(ps), &V_I8(pd));
317 case VT_UI1: return VarI8FromUI1(V_UI1(ps), &V_I8(pd));
318 case VT_UI2: return VarI8FromUI2(V_UI2(ps), &V_I8(pd));
319 case VT_UI4: return VarI8FromUI4(V_UI4(ps), &V_I8(pd));
320 case VT_UI8: return VarI8FromUI8(V_I8(ps), &V_I8(pd));
321 case VT_R4: return VarI8FromR4(V_R4(ps), &V_I8(pd));
322 case VT_R8: return VarI8FromR8(V_R8(ps), &V_I8(pd));
323 case VT_DATE: return VarI8FromDate(V_DATE(ps), &V_I8(pd));
324 case VT_BOOL: return VarI8FromBool(V_BOOL(ps), &V_I8(pd));
325 case VT_CY: return VarI8FromCy(V_CY(ps), &V_I8(pd));
326 case VT_DECIMAL: return VarI8FromDec(&V_DECIMAL(ps), &V_I8(pd));
327 case VT_DISPATCH: return VarI8FromDisp(V_DISPATCH(ps), lcid, &V_I8(pd));
328 case VT_BSTR: return VarI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_I8(pd));
335 case VT_EMPTY: V_R4(pd) = 0.0f; return S_OK;
336 case VT_I1: return VarR4FromI1(V_I1(ps), &V_R4(pd));
337 case VT_I2: return VarR4FromI2(V_I2(ps), &V_R4(pd));
338 case VT_I4: return VarR4FromI4(V_I4(ps), &V_R4(pd));
339 case VT_UI1: return VarR4FromUI1(V_UI1(ps), &V_R4(pd));
340 case VT_UI2: return VarR4FromUI2(V_UI2(ps), &V_R4(pd));
341 case VT_UI4: return VarR4FromUI4(V_UI4(ps), &V_R4(pd));
342 case VT_I8: return VarR4FromI8(V_I8(ps), &V_R4(pd));
343 case VT_UI8: return VarR4FromUI8(V_UI8(ps), &V_R4(pd));
344 case VT_R8: return VarR4FromR8(V_R8(ps), &V_R4(pd));
345 case VT_DATE: return VarR4FromDate(V_DATE(ps), &V_R4(pd));
346 case VT_BOOL: return VarR4FromBool(V_BOOL(ps), &V_R4(pd));
347 case VT_CY: return VarR4FromCy(V_CY(ps), &V_R4(pd));
348 case VT_DECIMAL: return VarR4FromDec(&V_DECIMAL(ps), &V_R4(pd));
349 case VT_DISPATCH: return VarR4FromDisp(V_DISPATCH(ps), lcid, &V_R4(pd));
350 case VT_BSTR: return VarR4FromStr(V_BSTR(ps), lcid, dwFlags, &V_R4(pd));
357 case VT_EMPTY: V_R8(pd) = 0.0; return S_OK;
358 case VT_I1: return VarR8FromI1(V_I1(ps), &V_R8(pd));
359 case VT_I2: return VarR8FromI2(V_I2(ps), &V_R8(pd));
360 case VT_I4: return VarR8FromI4(V_I4(ps), &V_R8(pd));
361 case VT_UI1: return VarR8FromUI1(V_UI1(ps), &V_R8(pd));
362 case VT_UI2: return VarR8FromUI2(V_UI2(ps), &V_R8(pd));
363 case VT_UI4: return VarR8FromUI4(V_UI4(ps), &V_R8(pd));
364 case VT_I8: return VarR8FromI8(V_I8(ps), &V_R8(pd));
365 case VT_UI8: return VarR8FromUI8(V_UI8(ps), &V_R8(pd));
366 case VT_R4: return VarR8FromR4(V_R4(ps), &V_R8(pd));
367 case VT_DATE: return VarR8FromDate(V_DATE(ps), &V_R8(pd));
368 case VT_BOOL: return VarR8FromBool(V_BOOL(ps), &V_R8(pd));
369 case VT_CY: return VarR8FromCy(V_CY(ps), &V_R8(pd));
370 case VT_DECIMAL: return VarR8FromDec(&V_DECIMAL(ps), &V_R8(pd));
371 case VT_DISPATCH: return VarR8FromDisp(V_DISPATCH(ps), lcid, &V_R8(pd));
372 case VT_BSTR: return VarR8FromStr(V_BSTR(ps), lcid, dwFlags, &V_R8(pd));
379 case VT_EMPTY: V_DATE(pd) = 0.0; return S_OK;
380 case VT_I1: return VarDateFromI1(V_I1(ps), &V_DATE(pd));
381 case VT_I2: return VarDateFromI2(V_I2(ps), &V_DATE(pd));
382 case VT_I4: return VarDateFromI4(V_I4(ps), &V_DATE(pd));
383 case VT_UI1: return VarDateFromUI1(V_UI1(ps), &V_DATE(pd));
384 case VT_UI2: return VarDateFromUI2(V_UI2(ps), &V_DATE(pd));
385 case VT_UI4: return VarDateFromUI4(V_UI4(ps), &V_DATE(pd));
386 case VT_I8: return VarDateFromI8(V_I8(ps), &V_DATE(pd));
387 case VT_UI8: return VarDateFromUI8(V_UI8(ps), &V_DATE(pd));
388 case VT_R4: return VarDateFromR4(V_R4(ps), &V_DATE(pd));
389 case VT_R8: return VarDateFromR8(V_R8(ps), &V_DATE(pd));
390 case VT_BOOL: return VarDateFromBool(V_BOOL(ps), &V_DATE(pd));
391 case VT_CY: return VarDateFromCy(V_CY(ps), &V_DATE(pd));
392 case VT_DECIMAL: return VarDateFromDec(&V_DECIMAL(ps), &V_DATE(pd));
393 case VT_DISPATCH: return VarDateFromDisp(V_DISPATCH(ps), lcid, &V_DATE(pd));
394 case VT_BSTR: return VarDateFromStr(V_BSTR(ps), lcid, dwFlags, &V_DATE(pd));
401 case VT_EMPTY: V_BOOL(pd) = 0; return S_OK;
402 case VT_I1: return VarBoolFromI1(V_I1(ps), &V_BOOL(pd));
403 case VT_I2: return VarBoolFromI2(V_I2(ps), &V_BOOL(pd));
404 case VT_I4: return VarBoolFromI4(V_I4(ps), &V_BOOL(pd));
405 case VT_UI1: return VarBoolFromUI1(V_UI1(ps), &V_BOOL(pd));
406 case VT_UI2: return VarBoolFromUI2(V_UI2(ps), &V_BOOL(pd));
407 case VT_UI4: return VarBoolFromUI4(V_UI4(ps), &V_BOOL(pd));
408 case VT_I8: return VarBoolFromI8(V_I8(ps), &V_BOOL(pd));
409 case VT_UI8: return VarBoolFromUI8(V_UI8(ps), &V_BOOL(pd));
410 case VT_R4: return VarBoolFromR4(V_R4(ps), &V_BOOL(pd));
411 case VT_R8: return VarBoolFromR8(V_R8(ps), &V_BOOL(pd));
412 case VT_DATE: return VarBoolFromDate(V_DATE(ps), &V_BOOL(pd));
413 case VT_CY: return VarBoolFromCy(V_CY(ps), &V_BOOL(pd));
414 case VT_DECIMAL: return VarBoolFromDec(&V_DECIMAL(ps), &V_BOOL(pd));
415 case VT_DISPATCH: return VarBoolFromDisp(V_DISPATCH(ps), lcid, &V_BOOL(pd));
416 case VT_BSTR: return VarBoolFromStr(V_BSTR(ps), lcid, dwFlags, &V_BOOL(pd));
424 V_BSTR(pd) = SysAllocStringLen(NULL, 0);
425 return V_BSTR(pd) ? S_OK : E_OUTOFMEMORY;
427 if (wFlags & (VARIANT_ALPHABOOL|VARIANT_LOCALBOOL))
428 return VarBstrFromBool(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
429 return VarBstrFromI2(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
430 case VT_I1: return VarBstrFromI1(V_I1(ps), lcid, dwFlags, &V_BSTR(pd));
431 case VT_I2: return VarBstrFromI2(V_I2(ps), lcid, dwFlags, &V_BSTR(pd));
432 case VT_I4: return VarBstrFromI4(V_I4(ps), lcid, dwFlags, &V_BSTR(pd));
433 case VT_UI1: return VarBstrFromUI1(V_UI1(ps), lcid, dwFlags, &V_BSTR(pd));
434 case VT_UI2: return VarBstrFromUI2(V_UI2(ps), lcid, dwFlags, &V_BSTR(pd));
435 case VT_UI4: return VarBstrFromUI4(V_UI4(ps), lcid, dwFlags, &V_BSTR(pd));
436 case VT_I8: return VarBstrFromI8(V_I8(ps), lcid, dwFlags, &V_BSTR(pd));
437 case VT_UI8: return VarBstrFromUI8(V_UI8(ps), lcid, dwFlags, &V_BSTR(pd));
438 case VT_R4: return VarBstrFromR4(V_R4(ps), lcid, dwFlags, &V_BSTR(pd));
439 case VT_R8: return VarBstrFromR8(V_R8(ps), lcid, dwFlags, &V_BSTR(pd));
440 case VT_DATE: return VarBstrFromDate(V_DATE(ps), lcid, dwFlags, &V_BSTR(pd));
441 case VT_CY: return VarBstrFromCy(V_CY(ps), lcid, dwFlags, &V_BSTR(pd));
442 case VT_DECIMAL: return VarBstrFromDec(&V_DECIMAL(ps), lcid, dwFlags, &V_BSTR(pd));
443 /* case VT_DISPATCH: return VarBstrFromDisp(V_DISPATCH(ps), lcid, dwFlags, &V_BSTR(pd)); */
450 case VT_EMPTY: V_CY(pd).int64 = 0; return S_OK;
451 case VT_I1: return VarCyFromI1(V_I1(ps), &V_CY(pd));
452 case VT_I2: return VarCyFromI2(V_I2(ps), &V_CY(pd));
453 case VT_I4: return VarCyFromI4(V_I4(ps), &V_CY(pd));
454 case VT_UI1: return VarCyFromUI1(V_UI1(ps), &V_CY(pd));
455 case VT_UI2: return VarCyFromUI2(V_UI2(ps), &V_CY(pd));
456 case VT_UI4: return VarCyFromUI4(V_UI4(ps), &V_CY(pd));
457 case VT_I8: return VarCyFromI8(V_I8(ps), &V_CY(pd));
458 case VT_UI8: return VarCyFromUI8(V_UI8(ps), &V_CY(pd));
459 case VT_R4: return VarCyFromR4(V_R4(ps), &V_CY(pd));
460 case VT_R8: return VarCyFromR8(V_R8(ps), &V_CY(pd));
461 case VT_DATE: return VarCyFromDate(V_DATE(ps), &V_CY(pd));
462 case VT_BOOL: return VarCyFromBool(V_BOOL(ps), &V_CY(pd));
463 case VT_DECIMAL: return VarCyFromDec(&V_DECIMAL(ps), &V_CY(pd));
464 case VT_DISPATCH: return VarCyFromDisp(V_DISPATCH(ps), lcid, &V_CY(pd));
465 case VT_BSTR: return VarCyFromStr(V_BSTR(ps), lcid, dwFlags, &V_CY(pd));
474 DEC_SIGNSCALE(&V_DECIMAL(pd)) = SIGNSCALE(DECIMAL_POS,0);
475 DEC_HI32(&V_DECIMAL(pd)) = 0;
476 DEC_MID32(&V_DECIMAL(pd)) = 0;
477 /* VarDecFromBool() coerces to -1/0, ChangeTypeEx() coerces to 1/0.
478 * VT_NULL and VT_EMPTY always give a 0 value.
480 DEC_LO32(&V_DECIMAL(pd)) = vtFrom == VT_BOOL && V_BOOL(ps) ? 1 : 0;
482 case VT_I1: return VarDecFromI1(V_I1(ps), &V_DECIMAL(pd));
483 case VT_I2: return VarDecFromI2(V_I2(ps), &V_DECIMAL(pd));
484 case VT_I4: return VarDecFromI4(V_I4(ps), &V_DECIMAL(pd));
485 case VT_UI1: return VarDecFromUI1(V_UI1(ps), &V_DECIMAL(pd));
486 case VT_UI2: return VarDecFromUI2(V_UI2(ps), &V_DECIMAL(pd));
487 case VT_UI4: return VarDecFromUI4(V_UI4(ps), &V_DECIMAL(pd));
488 case VT_I8: return VarDecFromI8(V_I8(ps), &V_DECIMAL(pd));
489 case VT_UI8: return VarDecFromUI8(V_UI8(ps), &V_DECIMAL(pd));
490 case VT_R4: return VarDecFromR4(V_R4(ps), &V_DECIMAL(pd));
491 case VT_R8: return VarDecFromR8(V_R8(ps), &V_DECIMAL(pd));
492 case VT_DATE: return VarDecFromDate(V_DATE(ps), &V_DECIMAL(pd));
493 case VT_CY: return VarDecFromCy(V_CY(pd), &V_DECIMAL(ps));
494 case VT_DISPATCH: return VarDecFromDisp(V_DISPATCH(ps), lcid, &V_DECIMAL(ps));
495 case VT_BSTR: return VarDecFromStr(V_BSTR(ps), lcid, dwFlags, &V_DECIMAL(pd));
503 if (V_DISPATCH(ps) == NULL)
504 V_UNKNOWN(pd) = NULL;
506 res = IDispatch_QueryInterface(V_DISPATCH(ps), &IID_IUnknown, (LPVOID*)&V_UNKNOWN(pd));
515 if (V_UNKNOWN(ps) == NULL)
516 V_DISPATCH(pd) = NULL;
518 res = IUnknown_QueryInterface(V_UNKNOWN(ps), &IID_IDispatch, (LPVOID*)&V_DISPATCH(pd));
529 /* Coerce to/from an array */
530 static inline HRESULT VARIANT_CoerceArray(VARIANTARG* pd, VARIANTARG* ps, VARTYPE vt)
532 if (vt == VT_BSTR && V_VT(ps) == (VT_ARRAY|VT_UI1))
533 return BstrFromVector(V_ARRAY(ps), &V_BSTR(pd));
535 if (V_VT(ps) == VT_BSTR && vt == (VT_ARRAY|VT_UI1))
536 return VectorFromBstr(V_BSTR(ps), &V_ARRAY(ps));
539 return SafeArrayCopy(V_ARRAY(ps), &V_ARRAY(pd));
541 return DISP_E_TYPEMISMATCH;
544 /******************************************************************************
545 * Check if a variants type is valid.
547 static inline HRESULT VARIANT_ValidateType(VARTYPE vt)
549 VARTYPE vtExtra = vt & VT_EXTRA_TYPE;
553 if (!(vtExtra & (VT_VECTOR|VT_RESERVED)))
555 if (vt < VT_VOID || vt == VT_RECORD || vt == VT_CLSID)
557 if ((vtExtra & (VT_BYREF|VT_ARRAY)) && vt <= VT_NULL)
558 return DISP_E_BADVARTYPE;
559 if (vt != (VARTYPE)15)
563 return DISP_E_BADVARTYPE;
566 /******************************************************************************
567 * VariantInit [OLEAUT32.8]
569 * Initialise a variant.
572 * pVarg [O] Variant to initialise
578 * This function simply sets the type of the variant to VT_EMPTY. It does not
579 * free any existing value, use VariantClear() for that.
581 void WINAPI VariantInit(VARIANTARG* pVarg)
583 TRACE("(%p)\n", pVarg);
585 V_VT(pVarg) = VT_EMPTY; /* Native doesn't set any other fields */
588 /******************************************************************************
589 * VariantClear [OLEAUT32.9]
594 * pVarg [I/O] Variant to clear
597 * Success: S_OK. Any previous value in pVarg is freed and its type is set to VT_EMPTY.
598 * Failure: DISP_E_BADVARTYPE, if the variant is a not a valid variant type.
600 HRESULT WINAPI VariantClear(VARIANTARG* pVarg)
604 TRACE("(%p->(%s%s))\n", pVarg, debugstr_VT(pVarg), debugstr_VF(pVarg));
606 hres = VARIANT_ValidateType(V_VT(pVarg));
610 if (!V_ISBYREF(pVarg))
612 if (V_ISARRAY(pVarg) || V_VT(pVarg) == VT_SAFEARRAY)
615 hres = SafeArrayDestroy(V_ARRAY(pVarg));
617 else if (V_VT(pVarg) == VT_BSTR)
620 SysFreeString(V_BSTR(pVarg));
622 else if (V_VT(pVarg) == VT_RECORD)
624 struct __tagBRECORD* pBr = &V_UNION(pVarg,brecVal);
627 IRecordInfo_RecordClear(pBr->pRecInfo, pBr->pvRecord);
628 IRecordInfo_Release(pBr->pRecInfo);
631 else if (V_VT(pVarg) == VT_DISPATCH ||
632 V_VT(pVarg) == VT_UNKNOWN)
634 if (V_UNKNOWN(pVarg))
635 IUnknown_Release(V_UNKNOWN(pVarg));
637 else if (V_VT(pVarg) == VT_VARIANT)
639 if (V_VARIANTREF(pVarg))
640 VariantClear(V_VARIANTREF(pVarg));
643 V_VT(pVarg) = VT_EMPTY;
648 /******************************************************************************
649 * Copy an IRecordInfo object contained in a variant.
651 static HRESULT VARIANT_CopyIRecordInfo(struct __tagBRECORD* pBr)
659 hres = IRecordInfo_GetSize(pBr->pRecInfo, &ulSize);
662 PVOID pvRecord = HeapAlloc(GetProcessHeap(), 0, ulSize);
664 hres = E_OUTOFMEMORY;
667 memcpy(pvRecord, pBr->pvRecord, ulSize);
668 pBr->pvRecord = pvRecord;
670 hres = IRecordInfo_RecordCopy(pBr->pRecInfo, pvRecord, pvRecord);
672 IRecordInfo_AddRef(pBr->pRecInfo);
676 else if (pBr->pvRecord)
681 /******************************************************************************
682 * VariantCopy [OLEAUT32.10]
687 * pvargDest [O] Destination for copy
688 * pvargSrc [I] Source variant to copy
691 * Success: S_OK. pvargDest contains a copy of pvargSrc.
692 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid type.
693 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
694 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
695 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
698 * - If pvargSrc == pvargDest, this function does nothing, and succeeds if
699 * pvargSrc is valid. Otherwise, pvargDest is always cleared using
700 * VariantClear() before pvargSrc is copied to it. If clearing pvargDest
701 * fails, so does this function.
702 * - VT_CLSID is a valid type type for pvargSrc, but not for pvargDest.
703 * - For by-value non-intrinsic types, a deep copy is made, i.e. The whole value
704 * is copied rather than just any pointers to it.
705 * - For by-value object types the object pointer is copied and the objects
706 * reference count increased using IUnknown_AddRef().
707 * - For all by-reference types, only the referencing pointer is copied.
709 HRESULT WINAPI VariantCopy(VARIANTARG* pvargDest, VARIANTARG* pvargSrc)
713 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
714 debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
715 debugstr_VF(pvargSrc));
717 if (V_TYPE(pvargSrc) == VT_CLSID || /* VT_CLSID is a special case */
718 FAILED(VARIANT_ValidateType(V_VT(pvargSrc))))
719 return DISP_E_BADVARTYPE;
721 if (pvargSrc != pvargDest &&
722 SUCCEEDED(hres = VariantClear(pvargDest)))
724 *pvargDest = *pvargSrc; /* Shallow copy the value */
726 if (!V_ISBYREF(pvargSrc))
728 if (V_ISARRAY(pvargSrc))
730 if (V_ARRAY(pvargSrc))
731 hres = SafeArrayCopy(V_ARRAY(pvargSrc), &V_ARRAY(pvargDest));
733 else if (V_VT(pvargSrc) == VT_BSTR)
735 if (V_BSTR(pvargSrc))
737 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)V_BSTR(pvargSrc), SysStringByteLen(V_BSTR(pvargSrc)));
738 if (!V_BSTR(pvargDest))
740 TRACE("!V_BSTR(pvargDest), SysAllocStringByteLen() failed to allocate %d bytes\n", SysStringByteLen(V_BSTR(pvargSrc)));
741 hres = E_OUTOFMEMORY;
745 else if (V_VT(pvargSrc) == VT_RECORD)
747 hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
749 else if (V_VT(pvargSrc) == VT_DISPATCH ||
750 V_VT(pvargSrc) == VT_UNKNOWN)
752 if (V_UNKNOWN(pvargSrc))
753 IUnknown_AddRef(V_UNKNOWN(pvargSrc));
760 /* Return the byte size of a variants data */
761 static inline size_t VARIANT_DataSize(const VARIANT* pv)
766 case VT_UI1: return sizeof(BYTE); break;
768 case VT_UI2: return sizeof(SHORT); break;
772 case VT_UI4: return sizeof(LONG); break;
774 case VT_UI8: return sizeof(LONGLONG); break;
775 case VT_R4: return sizeof(float); break;
776 case VT_R8: return sizeof(double); break;
777 case VT_DATE: return sizeof(DATE); break;
778 case VT_BOOL: return sizeof(VARIANT_BOOL); break;
781 case VT_BSTR: return sizeof(void*); break;
782 case VT_CY: return sizeof(CY); break;
783 case VT_ERROR: return sizeof(SCODE); break;
785 TRACE("Shouldn't be called for vt %s%s!\n", debugstr_VT(pv), debugstr_VF(pv));
789 /******************************************************************************
790 * VariantCopyInd [OLEAUT32.11]
792 * Copy a variant, dereferencing it it is by-reference.
795 * pvargDest [O] Destination for copy
796 * pvargSrc [I] Source variant to copy
799 * Success: S_OK. pvargDest contains a copy of pvargSrc.
800 * Failure: An HRESULT error code indicating the error.
803 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid by-value type.
804 * E_INVALIDARG, if pvargSrc is an invalid by-reference type.
805 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
806 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
807 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
810 * - If pvargSrc is by-value, this function behaves exactly as VariantCopy().
811 * - If pvargSrc is by-reference, the value copied to pvargDest is the pointed-to
813 * - if pvargSrc == pvargDest, this function dereferences in place. Otherwise,
814 * pvargDest is always cleared using VariantClear() before pvargSrc is copied
815 * to it. If clearing pvargDest fails, so does this function.
817 HRESULT WINAPI VariantCopyInd(VARIANT* pvargDest, VARIANTARG* pvargSrc)
819 VARIANTARG vTmp, *pSrc = pvargSrc;
823 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
824 debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
825 debugstr_VF(pvargSrc));
827 if (!V_ISBYREF(pvargSrc))
828 return VariantCopy(pvargDest, pvargSrc);
830 /* Argument checking is more lax than VariantCopy()... */
831 vt = V_TYPE(pvargSrc);
832 if (V_ISARRAY(pvargSrc) ||
833 (vt > VT_NULL && vt != (VARTYPE)15 && vt < VT_VOID &&
834 !(V_VT(pvargSrc) & (VT_VECTOR|VT_RESERVED))))
839 return E_INVALIDARG; /* ...And the return value for invalid types differs too */
841 if (pvargSrc == pvargDest)
843 /* In place copy. Use a shallow copy of pvargSrc & init pvargDest.
844 * This avoids an expensive VariantCopy() call - e.g. SafeArrayCopy().
848 V_VT(pvargDest) = VT_EMPTY;
852 /* Copy into another variant. Free the variant in pvargDest */
853 if (FAILED(hres = VariantClear(pvargDest)))
855 TRACE("VariantClear() of destination failed\n");
862 /* Native doesn't check that *V_ARRAYREF(pSrc) is valid */
863 hres = SafeArrayCopy(*V_ARRAYREF(pSrc), &V_ARRAY(pvargDest));
865 else if (V_VT(pSrc) == (VT_BSTR|VT_BYREF))
867 /* Native doesn't check that *V_BSTRREF(pSrc) is valid */
868 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)*V_BSTRREF(pSrc), SysStringByteLen(*V_BSTRREF(pSrc)));
870 else if (V_VT(pSrc) == (VT_RECORD|VT_BYREF))
872 V_UNION(pvargDest,brecVal) = V_UNION(pvargSrc,brecVal);
873 hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
875 else if (V_VT(pSrc) == (VT_DISPATCH|VT_BYREF) ||
876 V_VT(pSrc) == (VT_UNKNOWN|VT_BYREF))
878 /* Native doesn't check that *V_UNKNOWNREF(pSrc) is valid */
879 V_UNKNOWN(pvargDest) = *V_UNKNOWNREF(pSrc);
880 if (*V_UNKNOWNREF(pSrc))
881 IUnknown_AddRef(*V_UNKNOWNREF(pSrc));
883 else if (V_VT(pSrc) == (VT_VARIANT|VT_BYREF))
885 /* Native doesn't check that *V_VARIANTREF(pSrc) is valid */
886 if (V_VT(V_VARIANTREF(pSrc)) == (VT_VARIANT|VT_BYREF))
887 hres = E_INVALIDARG; /* Don't dereference more than one level */
889 hres = VariantCopyInd(pvargDest, V_VARIANTREF(pSrc));
891 /* Use the dereferenced variants type value, not VT_VARIANT */
892 goto VariantCopyInd_Return;
894 else if (V_VT(pSrc) == (VT_DECIMAL|VT_BYREF))
896 memcpy(&DEC_SCALE(&V_DECIMAL(pvargDest)), &DEC_SCALE(V_DECIMALREF(pSrc)),
897 sizeof(DECIMAL) - sizeof(USHORT));
901 /* Copy the pointed to data into this variant */
902 memcpy(&V_BYREF(pvargDest), V_BYREF(pSrc), VARIANT_DataSize(pSrc));
905 V_VT(pvargDest) = V_VT(pSrc) & ~VT_BYREF;
907 VariantCopyInd_Return:
909 if (pSrc != pvargSrc)
912 TRACE("returning 0x%08lx, %p->(%s%s)\n", hres, pvargDest,
913 debugstr_VT(pvargDest), debugstr_VF(pvargDest));
917 /******************************************************************************
918 * VariantChangeType [OLEAUT32.12]
920 * Change the type of a variant.
923 * pvargDest [O] Destination for the converted variant
924 * pvargSrc [O] Source variant to change the type of
925 * wFlags [I] VARIANT_ flags from "oleauto.h"
926 * vt [I] Variant type to change pvargSrc into
929 * Success: S_OK. pvargDest contains the converted value.
930 * Failure: An HRESULT error code describing the failure.
933 * The LCID used for the conversion is LOCALE_USER_DEFAULT.
934 * See VariantChangeTypeEx.
936 HRESULT WINAPI VariantChangeType(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
937 USHORT wFlags, VARTYPE vt)
939 return VariantChangeTypeEx( pvargDest, pvargSrc, LOCALE_USER_DEFAULT, wFlags, vt );
942 /******************************************************************************
943 * VariantChangeTypeEx [OLEAUT32.147]
945 * Change the type of a variant.
948 * pvargDest [O] Destination for the converted variant
949 * pvargSrc [O] Source variant to change the type of
950 * lcid [I] LCID for the conversion
951 * wFlags [I] VARIANT_ flags from "oleauto.h"
952 * vt [I] Variant type to change pvargSrc into
955 * Success: S_OK. pvargDest contains the converted value.
956 * Failure: An HRESULT error code describing the failure.
959 * pvargDest and pvargSrc can point to the same variant to perform an in-place
960 * conversion. If the conversion is successful, pvargSrc will be freed.
962 HRESULT WINAPI VariantChangeTypeEx(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
963 LCID lcid, USHORT wFlags, VARTYPE vt)
967 TRACE("(%p->(%s%s),%p->(%s%s),0x%08lx,0x%04x,%s%s)\n", pvargDest,
968 debugstr_VT(pvargDest), debugstr_VF(pvargDest), pvargSrc,
969 debugstr_VT(pvargSrc), debugstr_VF(pvargSrc), lcid, wFlags,
970 debugstr_vt(vt), debugstr_vf(vt));
973 res = DISP_E_BADVARTYPE;
976 res = VARIANT_ValidateType(V_VT(pvargSrc));
980 res = VARIANT_ValidateType(vt);
986 V_VT(&vTmp) = VT_EMPTY;
987 res = VariantCopyInd(&vTmp, pvargSrc);
991 res = VariantClear(pvargDest);
995 if (V_ISARRAY(&vTmp) || (vt & VT_ARRAY))
996 res = VARIANT_CoerceArray(pvargDest, &vTmp, vt);
998 res = VARIANT_Coerce(pvargDest, lcid, wFlags, &vTmp, vt);
1001 V_VT(pvargDest) = vt;
1003 VariantClear(&vTmp);
1009 TRACE("returning 0x%08lx, %p->(%s%s)\n", res, pvargDest,
1010 debugstr_VT(pvargDest), debugstr_VF(pvargDest));
1014 /* Date Conversions */
1016 #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
1018 /* Convert a VT_DATE value to a Julian Date */
1019 static inline int VARIANT_JulianFromDate(int dateIn)
1021 int julianDays = dateIn;
1023 julianDays -= DATE_MIN; /* Convert to + days from 1 Jan 100 AD */
1024 julianDays += 1757585; /* Convert to + days from 23 Nov 4713 BC (Julian) */
1028 /* Convert a Julian Date to a VT_DATE value */
1029 static inline int VARIANT_DateFromJulian(int dateIn)
1031 int julianDays = dateIn;
1033 julianDays -= 1757585; /* Convert to + days from 1 Jan 100 AD */
1034 julianDays += DATE_MIN; /* Convert to +/- days from 1 Jan 1899 AD */
1038 /* Convert a Julian date to Day/Month/Year - from PostgreSQL */
1039 static inline void VARIANT_DMYFromJulian(int jd, USHORT *year, USHORT *month, USHORT *day)
1045 l -= (n * 146097 + 3) / 4;
1046 i = (4000 * (l + 1)) / 1461001;
1047 l += 31 - (i * 1461) / 4;
1048 j = (l * 80) / 2447;
1049 *day = l - (j * 2447) / 80;
1051 *month = (j + 2) - (12 * l);
1052 *year = 100 * (n - 49) + i + l;
1055 /* Convert Day/Month/Year to a Julian date - from PostgreSQL */
1056 static inline double VARIANT_JulianFromDMY(USHORT year, USHORT month, USHORT day)
1058 int m12 = (month - 14) / 12;
1060 return ((1461 * (year + 4800 + m12)) / 4 + (367 * (month - 2 - 12 * m12)) / 12 -
1061 (3 * ((year + 4900 + m12) / 100)) / 4 + day - 32075);
1064 /* Macros for accessing DOS format date/time fields */
1065 #define DOS_YEAR(x) (1980 + (x >> 9))
1066 #define DOS_MONTH(x) ((x >> 5) & 0xf)
1067 #define DOS_DAY(x) (x & 0x1f)
1068 #define DOS_HOUR(x) (x >> 11)
1069 #define DOS_MINUTE(x) ((x >> 5) & 0x3f)
1070 #define DOS_SECOND(x) ((x & 0x1f) << 1)
1071 /* Create a DOS format date/time */
1072 #define DOS_DATE(d,m,y) (d | (m << 5) | ((y-1980) << 9))
1073 #define DOS_TIME(h,m,s) ((s >> 1) | (m << 5) | (h << 11))
1075 /* Roll a date forwards or backwards to correct it */
1076 static HRESULT VARIANT_RollUdate(UDATE *lpUd)
1078 static const BYTE days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1080 TRACE("Raw date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
1081 lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
1083 /* Years < 100 are treated as 1900 + year */
1084 if (lpUd->st.wYear < 100)
1085 lpUd->st.wYear += 1900;
1087 if (!lpUd->st.wMonth)
1089 /* Roll back to December of the previous year */
1090 lpUd->st.wMonth = 12;
1093 else while (lpUd->st.wMonth > 12)
1095 /* Roll forward the correct number of months */
1097 lpUd->st.wMonth -= 12;
1100 if (lpUd->st.wYear > 9999 || lpUd->st.wHour > 23 ||
1101 lpUd->st.wMinute > 59 || lpUd->st.wSecond > 59)
1102 return E_INVALIDARG; /* Invalid values */
1106 /* Roll back the date one day */
1107 if (lpUd->st.wMonth == 1)
1109 /* Roll back to December 31 of the previous year */
1111 lpUd->st.wMonth = 12;
1116 lpUd->st.wMonth--; /* Previous month */
1117 if (lpUd->st.wMonth == 2 && IsLeapYear(lpUd->st.wYear))
1118 lpUd->st.wDay = 29; /* Februaury has 29 days on leap years */
1120 lpUd->st.wDay = days[lpUd->st.wMonth]; /* Last day of the month */
1123 else if (lpUd->st.wDay > 28)
1125 int rollForward = 0;
1127 /* Possibly need to roll the date forward */
1128 if (lpUd->st.wMonth == 2 && IsLeapYear(lpUd->st.wYear))
1129 rollForward = lpUd->st.wDay - 29; /* Februaury has 29 days on leap years */
1131 rollForward = lpUd->st.wDay - days[lpUd->st.wMonth];
1133 if (rollForward > 0)
1135 lpUd->st.wDay = rollForward;
1137 if (lpUd->st.wMonth > 12)
1139 lpUd->st.wMonth = 1; /* Roll forward into January of the next year */
1144 TRACE("Rolled date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
1145 lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
1149 /**********************************************************************
1150 * DosDateTimeToVariantTime [OLEAUT32.14]
1152 * Convert a Dos format date and time into variant VT_DATE format.
1155 * wDosDate [I] Dos format date
1156 * wDosTime [I] Dos format time
1157 * pDateOut [O] Destination for VT_DATE format
1160 * Success: TRUE. pDateOut contains the converted time.
1161 * Failure: FALSE, if wDosDate or wDosTime are invalid (see notes).
1164 * - Dos format dates can only hold dates from 1-Jan-1980 to 31-Dec-2099.
1165 * - Dos format times are accurate to only 2 second precision.
1166 * - The format of a Dos Date is:
1167 *| Bits Values Meaning
1168 *| ---- ------ -------
1169 *| 0-4 1-31 Day of the week. 0 rolls back one day. A value greater than
1170 *| the days in the month rolls forward the extra days.
1171 *| 5-8 1-12 Month of the year. 0 rolls back to December of the previous
1172 *| year. 13-15 are invalid.
1173 *| 9-15 0-119 Year based from 1980 (Max 2099). 120-127 are invalid.
1174 * - The format of a Dos Time is:
1175 *| Bits Values Meaning
1176 *| ---- ------ -------
1177 *| 0-4 0-29 Seconds/2. 30 and 31 are invalid.
1178 *| 5-10 0-59 Minutes. 60-63 are invalid.
1179 *| 11-15 0-23 Hours (24 hour clock). 24-32 are invalid.
1181 INT WINAPI DosDateTimeToVariantTime(USHORT wDosDate, USHORT wDosTime,
1186 TRACE("(0x%x(%d/%d/%d),0x%x(%d:%d:%d),%p)\n",
1187 wDosDate, DOS_YEAR(wDosDate), DOS_MONTH(wDosDate), DOS_DAY(wDosDate),
1188 wDosTime, DOS_HOUR(wDosTime), DOS_MINUTE(wDosTime), DOS_SECOND(wDosTime),
1191 ud.st.wYear = DOS_YEAR(wDosDate);
1192 ud.st.wMonth = DOS_MONTH(wDosDate);
1193 if (ud.st.wYear > 2099 || ud.st.wMonth > 12)
1195 ud.st.wDay = DOS_DAY(wDosDate);
1196 ud.st.wHour = DOS_HOUR(wDosTime);
1197 ud.st.wMinute = DOS_MINUTE(wDosTime);
1198 ud.st.wSecond = DOS_SECOND(wDosTime);
1199 ud.st.wDayOfWeek = ud.st.wMilliseconds = 0;
1201 return !VarDateFromUdate(&ud, 0, pDateOut);
1204 /**********************************************************************
1205 * VariantTimeToDosDateTime [OLEAUT32.13]
1207 * Convert a variant format date into a Dos format date and time.
1209 * dateIn [I] VT_DATE time format
1210 * pwDosDate [O] Destination for Dos format date
1211 * pwDosTime [O] Destination for Dos format time
1214 * Success: TRUE. pwDosDate and pwDosTime contains the converted values.
1215 * Failure: FALSE, if dateIn cannot be represented in Dos format.
1218 * See DosDateTimeToVariantTime() for Dos format details and bugs.
1220 INT WINAPI VariantTimeToDosDateTime(double dateIn, USHORT *pwDosDate, USHORT *pwDosTime)
1224 TRACE("(%g,%p,%p)\n", dateIn, pwDosDate, pwDosTime);
1226 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1229 if (ud.st.wYear < 1980 || ud.st.wYear > 2099)
1232 *pwDosDate = DOS_DATE(ud.st.wDay, ud.st.wMonth, ud.st.wYear);
1233 *pwDosTime = DOS_TIME(ud.st.wHour, ud.st.wMinute, ud.st.wSecond);
1235 TRACE("Returning 0x%x(%d/%d/%d), 0x%x(%d:%d:%d)\n",
1236 *pwDosDate, DOS_YEAR(*pwDosDate), DOS_MONTH(*pwDosDate), DOS_DAY(*pwDosDate),
1237 *pwDosTime, DOS_HOUR(*pwDosTime), DOS_MINUTE(*pwDosTime), DOS_SECOND(*pwDosTime));
1241 /***********************************************************************
1242 * SystemTimeToVariantTime [OLEAUT32.184]
1244 * Convert a System format date and time into variant VT_DATE format.
1247 * lpSt [I] System format date and time
1248 * pDateOut [O] Destination for VT_DATE format date
1251 * Success: TRUE. *pDateOut contains the converted value.
1252 * Failure: FALSE, if lpSt cannot be represented in VT_DATE format.
1254 INT WINAPI SystemTimeToVariantTime(LPSYSTEMTIME lpSt, double *pDateOut)
1258 TRACE("(%p->%d/%d/%d %d:%d:%d,%p)\n", lpSt, lpSt->wDay, lpSt->wMonth,
1259 lpSt->wYear, lpSt->wHour, lpSt->wMinute, lpSt->wSecond, pDateOut);
1261 if (lpSt->wMonth > 12)
1264 memcpy(&ud.st, lpSt, sizeof(ud.st));
1265 return !VarDateFromUdate(&ud, 0, pDateOut);
1268 /***********************************************************************
1269 * VariantTimeToSystemTime [OLEAUT32.185]
1271 * Convert a variant VT_DATE into a System format date and time.
1274 * datein [I] Variant VT_DATE format date
1275 * lpSt [O] Destination for System format date and time
1278 * Success: TRUE. *lpSt contains the converted value.
1279 * Failure: FALSE, if dateIn is too large or small.
1281 INT WINAPI VariantTimeToSystemTime(double dateIn, LPSYSTEMTIME lpSt)
1285 TRACE("(%g,%p)\n", dateIn, lpSt);
1287 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1290 memcpy(lpSt, &ud.st, sizeof(ud.st));
1294 /***********************************************************************
1295 * VarDateFromUdateEx [OLEAUT32.319]
1297 * Convert an unpacked format date and time to a variant VT_DATE.
1300 * pUdateIn [I] Unpacked format date and time to convert
1301 * lcid [I] Locale identifier for the conversion
1302 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1303 * pDateOut [O] Destination for variant VT_DATE.
1306 * Success: S_OK. *pDateOut contains the converted value.
1307 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1309 HRESULT WINAPI VarDateFromUdateEx(UDATE *pUdateIn, LCID lcid, ULONG dwFlags, DATE *pDateOut)
1314 TRACE("(%p->%d/%d/%d %d:%d:%d:%d %d %d,0x%08lx,0x%08lx,%p)\n", pUdateIn,
1315 pUdateIn->st.wMonth, pUdateIn->st.wDay, pUdateIn->st.wYear,
1316 pUdateIn->st.wHour, pUdateIn->st.wMinute, pUdateIn->st.wSecond,
1317 pUdateIn->st.wMilliseconds, pUdateIn->st.wDayOfWeek,
1318 pUdateIn->wDayOfYear, lcid, dwFlags, pDateOut);
1320 if (lcid != MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT))
1321 FIXME("lcid possibly not handled, treating as en-us\n");
1323 memcpy(&ud, pUdateIn, sizeof(ud));
1325 if (dwFlags & VAR_VALIDDATE)
1326 WARN("Ignoring VAR_VALIDDATE\n");
1328 if (FAILED(VARIANT_RollUdate(&ud)))
1329 return E_INVALIDARG;
1332 dateVal = VARIANT_DateFromJulian(VARIANT_JulianFromDMY(ud.st.wYear, ud.st.wMonth, ud.st.wDay));
1335 dateVal += ud.st.wHour / 24.0;
1336 dateVal += ud.st.wMinute / 1440.0;
1337 dateVal += ud.st.wSecond / 86400.0;
1338 dateVal += ud.st.wMilliseconds / 86400000.0;
1340 TRACE("Returning %g\n", dateVal);
1341 *pDateOut = dateVal;
1345 /***********************************************************************
1346 * VarDateFromUdate [OLEAUT32.330]
1348 * Convert an unpacked format date and time to a variant VT_DATE.
1351 * pUdateIn [I] Unpacked format date and time to convert
1352 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1353 * pDateOut [O] Destination for variant VT_DATE.
1356 * Success: S_OK. *pDateOut contains the converted value.
1357 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1360 * This function uses the United States English locale for the conversion. Use
1361 * VarDateFromUdateEx() for alternate locales.
1363 HRESULT WINAPI VarDateFromUdate(UDATE *pUdateIn, ULONG dwFlags, DATE *pDateOut)
1365 LCID lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
1367 return VarDateFromUdateEx(pUdateIn, lcid, dwFlags, pDateOut);
1370 /***********************************************************************
1371 * VarUdateFromDate [OLEAUT32.331]
1373 * Convert a variant VT_DATE into an unpacked format date and time.
1376 * datein [I] Variant VT_DATE format date
1377 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1378 * lpUdate [O] Destination for unpacked format date and time
1381 * Success: S_OK. *lpUdate contains the converted value.
1382 * Failure: E_INVALIDARG, if dateIn is too large or small.
1384 HRESULT WINAPI VarUdateFromDate(DATE dateIn, ULONG dwFlags, UDATE *lpUdate)
1386 /* Cumulative totals of days per month */
1387 static const USHORT cumulativeDays[] =
1389 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
1391 double datePart, timePart;
1394 TRACE("(%g,0x%08lx,%p)\n", dateIn, dwFlags, lpUdate);
1396 if (dateIn <= (DATE_MIN - 1.0) || dateIn >= (DATE_MAX + 1.0))
1397 return E_INVALIDARG;
1399 datePart = dateIn < 0.0 ? ceil(dateIn) : floor(dateIn);
1400 /* Compensate for int truncation (always downwards) */
1401 timePart = dateIn - datePart + 0.00000000001;
1402 if (timePart >= 1.0)
1403 timePart -= 0.00000000001;
1406 julianDays = VARIANT_JulianFromDate(dateIn);
1407 VARIANT_DMYFromJulian(julianDays, &lpUdate->st.wYear, &lpUdate->st.wMonth,
1410 datePart = (datePart + 1.5) / 7.0;
1411 lpUdate->st.wDayOfWeek = (datePart - floor(datePart)) * 7;
1412 if (lpUdate->st.wDayOfWeek == 0)
1413 lpUdate->st.wDayOfWeek = 5;
1414 else if (lpUdate->st.wDayOfWeek == 1)
1415 lpUdate->st.wDayOfWeek = 6;
1417 lpUdate->st.wDayOfWeek -= 2;
1419 if (lpUdate->st.wMonth > 2 && IsLeapYear(lpUdate->st.wYear))
1420 lpUdate->wDayOfYear = 1; /* After February, in a leap year */
1422 lpUdate->wDayOfYear = 0;
1424 lpUdate->wDayOfYear += cumulativeDays[lpUdate->st.wMonth];
1425 lpUdate->wDayOfYear += lpUdate->st.wDay;
1429 lpUdate->st.wHour = timePart;
1430 timePart -= lpUdate->st.wHour;
1432 lpUdate->st.wMinute = timePart;
1433 timePart -= lpUdate->st.wMinute;
1435 lpUdate->st.wSecond = timePart;
1436 timePart -= lpUdate->st.wSecond;
1437 lpUdate->st.wMilliseconds = 0;
1440 /* Round the milliseconds, adjusting the time/date forward if needed */
1441 if (lpUdate->st.wSecond < 59)
1442 lpUdate->st.wSecond++;
1445 lpUdate->st.wSecond = 0;
1446 if (lpUdate->st.wMinute < 59)
1447 lpUdate->st.wMinute++;
1450 lpUdate->st.wMinute = 0;
1451 if (lpUdate->st.wHour < 23)
1452 lpUdate->st.wHour++;
1455 lpUdate->st.wHour = 0;
1456 /* Roll over a whole day */
1457 if (++lpUdate->st.wDay > 28)
1458 VARIANT_RollUdate(lpUdate);
1466 #define GET_NUMBER_TEXT(fld,name) \
1468 if (!GetLocaleInfoW(lcid, lctype|fld, buff, 2)) \
1469 WARN("buffer too small for " #fld "\n"); \
1471 if (buff[0]) lpChars->name = buff[0]; \
1472 TRACE("lcid 0x%lx, " #name "=%d '%c'\n", lcid, lpChars->name, lpChars->name)
1474 /* Get the valid number characters for an lcid */
1475 void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS *lpChars, LCID lcid, DWORD dwFlags)
1477 static const VARIANT_NUMBER_CHARS defaultChars = { '-','+','.',',','$',0,'.',',' };
1478 LCTYPE lctype = dwFlags & LOCALE_NOUSEROVERRIDE;
1481 memcpy(lpChars, &defaultChars, sizeof(defaultChars));
1482 GET_NUMBER_TEXT(LOCALE_SNEGATIVESIGN, cNegativeSymbol);
1483 GET_NUMBER_TEXT(LOCALE_SPOSITIVESIGN, cPositiveSymbol);
1484 GET_NUMBER_TEXT(LOCALE_SDECIMAL, cDecimalPoint);
1485 GET_NUMBER_TEXT(LOCALE_STHOUSAND, cDigitSeperator);
1486 GET_NUMBER_TEXT(LOCALE_SMONDECIMALSEP, cCurrencyDecimalPoint);
1487 GET_NUMBER_TEXT(LOCALE_SMONTHOUSANDSEP, cCurrencyDigitSeperator);
1489 /* Local currency symbols are often 2 characters */
1490 lpChars->cCurrencyLocal2 = '\0';
1491 switch(GetLocaleInfoW(lcid, lctype|LOCALE_SCURRENCY, buff, sizeof(buff)/sizeof(WCHAR)))
1493 case 3: lpChars->cCurrencyLocal2 = buff[1]; /* Fall through */
1494 case 2: lpChars->cCurrencyLocal = buff[0];
1496 default: WARN("buffer too small for LOCALE_SCURRENCY\n");
1498 TRACE("lcid 0x%lx, cCurrencyLocal =%d,%d '%c','%c'\n", lcid, lpChars->cCurrencyLocal,
1499 lpChars->cCurrencyLocal2, lpChars->cCurrencyLocal, lpChars->cCurrencyLocal2);
1502 /* Number Parsing States */
1503 #define B_PROCESSING_EXPONENT 0x1
1504 #define B_NEGATIVE_EXPONENT 0x2
1505 #define B_EXPONENT_START 0x4
1506 #define B_INEXACT_ZEROS 0x8
1507 #define B_LEADING_ZERO 0x10
1508 #define B_PROCESSING_HEX 0x20
1509 #define B_PROCESSING_OCT 0x40
1511 /**********************************************************************
1512 * VarParseNumFromStr [OLEAUT32.46]
1514 * Parse a string containing a number into a NUMPARSE structure.
1517 * lpszStr [I] String to parse number from
1518 * lcid [I] Locale Id for the conversion
1519 * dwFlags [I] 0, or LOCALE_NOUSEROVERRIDE to use system default number chars
1520 * pNumprs [I/O] Destination for parsed number
1521 * rgbDig [O] Destination for digits read in
1524 * Success: S_OK. pNumprs and rgbDig contain the parsed representation of
1526 * Failure: E_INVALIDARG, if any parameter is invalid.
1527 * DISP_E_TYPEMISMATCH, if the string is not a number or is formatted
1529 * DISP_E_OVERFLOW, if rgbDig is too small to hold the number.
1532 * pNumprs must have the following fields set:
1533 * cDig: Set to the size of rgbDig.
1534 * dwInFlags: Set to the allowable syntax of the number using NUMPRS_ flags
1538 * - I am unsure if this function should parse non-arabic (e.g. Thai)
1539 * numerals, so this has not been implemented.
1541 HRESULT WINAPI VarParseNumFromStr(OLECHAR *lpszStr, LCID lcid, ULONG dwFlags,
1542 NUMPARSE *pNumprs, BYTE *rgbDig)
1544 VARIANT_NUMBER_CHARS chars;
1546 DWORD dwState = B_EXPONENT_START|B_INEXACT_ZEROS;
1547 int iMaxDigits = sizeof(rgbTmp) / sizeof(BYTE);
1550 TRACE("(%s,%ld,0x%08lx,%p,%p)\n", debugstr_w(lpszStr), lcid, dwFlags, pNumprs, rgbDig);
1552 if (!pNumprs || !rgbDig)
1553 return E_INVALIDARG;
1555 if (pNumprs->cDig < iMaxDigits)
1556 iMaxDigits = pNumprs->cDig;
1559 pNumprs->dwOutFlags = 0;
1560 pNumprs->cchUsed = 0;
1561 pNumprs->nBaseShift = 0;
1562 pNumprs->nPwr10 = 0;
1565 return DISP_E_TYPEMISMATCH;
1567 VARIANT_GetLocalisedNumberChars(&chars, lcid, dwFlags);
1569 /* First consume all the leading symbols and space from the string */
1572 if (pNumprs->dwInFlags & NUMPRS_LEADING_WHITE && isspaceW(*lpszStr))
1574 pNumprs->dwOutFlags |= NUMPRS_LEADING_WHITE;
1579 } while (isspaceW(*lpszStr));
1581 else if (pNumprs->dwInFlags & NUMPRS_LEADING_PLUS &&
1582 *lpszStr == chars.cPositiveSymbol &&
1583 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS))
1585 pNumprs->dwOutFlags |= NUMPRS_LEADING_PLUS;
1589 else if (pNumprs->dwInFlags & NUMPRS_LEADING_MINUS &&
1590 *lpszStr == chars.cNegativeSymbol &&
1591 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS))
1593 pNumprs->dwOutFlags |= (NUMPRS_LEADING_MINUS|NUMPRS_NEG);
1597 else if (pNumprs->dwInFlags & NUMPRS_CURRENCY &&
1598 !(pNumprs->dwOutFlags & NUMPRS_CURRENCY) &&
1599 *lpszStr == chars.cCurrencyLocal &&
1600 (!chars.cCurrencyLocal2 || lpszStr[1] == chars.cCurrencyLocal2))
1602 pNumprs->dwOutFlags |= NUMPRS_CURRENCY;
1605 /* Only accept currency characters */
1606 chars.cDecimalPoint = chars.cCurrencyDecimalPoint;
1607 chars.cDigitSeperator = chars.cCurrencyDigitSeperator;
1609 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == '(' &&
1610 !(pNumprs->dwOutFlags & NUMPRS_PARENS))
1612 pNumprs->dwOutFlags |= NUMPRS_PARENS;
1620 if (!(pNumprs->dwOutFlags & NUMPRS_CURRENCY))
1622 /* Only accept non-currency characters */
1623 chars.cCurrencyDecimalPoint = chars.cDecimalPoint;
1624 chars.cCurrencyDigitSeperator = chars.cDigitSeperator;
1627 if ((*lpszStr == '&' && (*(lpszStr+1) == 'H' || *(lpszStr+1) == 'h')) &&
1628 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1630 dwState |= B_PROCESSING_HEX;
1631 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1635 else if ((*lpszStr == '&' && (*(lpszStr+1) == 'O' || *(lpszStr+1) == 'o')) &&
1636 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1638 dwState |= B_PROCESSING_OCT;
1639 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1644 /* Strip Leading zeros */
1645 while (*lpszStr == '0')
1647 dwState |= B_LEADING_ZERO;
1654 if (isdigitW(*lpszStr))
1656 if (dwState & B_PROCESSING_EXPONENT)
1658 int exponentSize = 0;
1659 if (dwState & B_EXPONENT_START)
1661 while (*lpszStr == '0')
1663 /* Skip leading zero's in the exponent */
1667 if (!isdigitW(*lpszStr))
1668 break; /* No exponent digits - invalid */
1671 while (isdigitW(*lpszStr))
1674 exponentSize += *lpszStr - '0';
1678 if (dwState & B_NEGATIVE_EXPONENT)
1679 exponentSize = -exponentSize;
1680 /* Add the exponent into the powers of 10 */
1681 pNumprs->nPwr10 += exponentSize;
1682 dwState &= ~(B_PROCESSING_EXPONENT|B_EXPONENT_START);
1683 lpszStr--; /* back up to allow processing of next char */
1687 if ((pNumprs->cDig >= iMaxDigits) && !(dwState & B_PROCESSING_HEX)
1688 && !(dwState & B_PROCESSING_OCT))
1690 pNumprs->dwOutFlags |= NUMPRS_INEXACT;
1692 if (*lpszStr != '0')
1693 dwState &= ~B_INEXACT_ZEROS; /* Inexact number with non-trailing zeros */
1695 /* This digit can't be represented, but count it in nPwr10 */
1696 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1703 if ((dwState & B_PROCESSING_OCT) && ((*lpszStr == '8') || (*lpszStr == '9'))) {
1704 return DISP_E_TYPEMISMATCH;
1707 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1708 pNumprs->nPwr10--; /* Count decimal points in nPwr10 */
1710 rgbTmp[pNumprs->cDig] = *lpszStr - '0';
1716 else if (*lpszStr == chars.cDigitSeperator && pNumprs->dwInFlags & NUMPRS_THOUSANDS)
1718 pNumprs->dwOutFlags |= NUMPRS_THOUSANDS;
1721 else if (*lpszStr == chars.cDecimalPoint &&
1722 pNumprs->dwInFlags & NUMPRS_DECIMAL &&
1723 !(pNumprs->dwOutFlags & (NUMPRS_DECIMAL|NUMPRS_EXPONENT)))
1725 pNumprs->dwOutFlags |= NUMPRS_DECIMAL;
1728 /* Remove trailing zeros from the whole number part */
1729 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1735 /* If we have no digits so far, skip leading zeros */
1738 while (lpszStr[1] == '0')
1740 dwState |= B_LEADING_ZERO;
1746 else if ((*lpszStr == 'e' || *lpszStr == 'E') &&
1747 pNumprs->dwInFlags & NUMPRS_EXPONENT &&
1748 !(pNumprs->dwOutFlags & NUMPRS_EXPONENT))
1750 dwState |= B_PROCESSING_EXPONENT;
1751 pNumprs->dwOutFlags |= NUMPRS_EXPONENT;
1754 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cPositiveSymbol)
1756 cchUsed++; /* Ignore positive exponent */
1758 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cNegativeSymbol)
1760 dwState |= B_NEGATIVE_EXPONENT;
1763 else if (((*lpszStr >= 'a' && *lpszStr <= 'f') ||
1764 (*lpszStr >= 'A' && *lpszStr <= 'F')) &&
1765 dwState & B_PROCESSING_HEX)
1767 if (pNumprs->cDig >= iMaxDigits)
1769 return DISP_E_OVERFLOW;
1773 if (*lpszStr >= 'a')
1774 rgbTmp[pNumprs->cDig] = *lpszStr - 'a' + 10;
1776 rgbTmp[pNumprs->cDig] = *lpszStr - 'A' + 10;
1782 break; /* Stop at an unrecognised character */
1787 if (!pNumprs->cDig && dwState & B_LEADING_ZERO)
1789 /* Ensure a 0 on its own gets stored */
1794 if (pNumprs->dwOutFlags & NUMPRS_EXPONENT && dwState & B_PROCESSING_EXPONENT)
1796 pNumprs->cchUsed = cchUsed;
1797 return DISP_E_TYPEMISMATCH; /* Failed to completely parse the exponent */
1800 if (pNumprs->dwOutFlags & NUMPRS_INEXACT)
1802 if (dwState & B_INEXACT_ZEROS)
1803 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* All zeros doesn't set NUMPRS_INEXACT */
1804 } else if(pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1806 /* copy all of the digits into the output digit buffer */
1807 /* this is exactly what windows does although it also returns */
1808 /* cDig of X and writes X+Y where Y>=0 number of digits to rgbDig */
1809 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1811 if (dwState & B_PROCESSING_HEX) {
1812 /* hex numbers have always the same format */
1814 pNumprs->nBaseShift=4;
1816 if (dwState & B_PROCESSING_OCT) {
1817 /* oct numbers have always the same format */
1819 pNumprs->nBaseShift=3;
1821 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1823 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1834 /* Remove trailing zeros from the last (whole number or decimal) part */
1835 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1837 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1846 if (pNumprs->cDig <= iMaxDigits)
1847 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* Ignore stripped zeros for NUMPRS_INEXACT */
1849 pNumprs->cDig = iMaxDigits; /* Only return iMaxDigits worth of digits */
1851 /* Copy the digits we processed into rgbDig */
1852 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1854 /* Consume any trailing symbols and space */
1857 if ((pNumprs->dwInFlags & NUMPRS_TRAILING_WHITE) && isspaceW(*lpszStr))
1859 pNumprs->dwOutFlags |= NUMPRS_TRAILING_WHITE;
1864 } while (isspaceW(*lpszStr));
1866 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_PLUS &&
1867 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS) &&
1868 *lpszStr == chars.cPositiveSymbol)
1870 pNumprs->dwOutFlags |= NUMPRS_TRAILING_PLUS;
1874 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_MINUS &&
1875 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS) &&
1876 *lpszStr == chars.cNegativeSymbol)
1878 pNumprs->dwOutFlags |= (NUMPRS_TRAILING_MINUS|NUMPRS_NEG);
1882 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == ')' &&
1883 pNumprs->dwOutFlags & NUMPRS_PARENS)
1887 pNumprs->dwOutFlags |= NUMPRS_NEG;
1893 if (pNumprs->dwOutFlags & NUMPRS_PARENS && !(pNumprs->dwOutFlags & NUMPRS_NEG))
1895 pNumprs->cchUsed = cchUsed;
1896 return DISP_E_TYPEMISMATCH; /* Opening parenthesis not matched */
1899 if (pNumprs->dwInFlags & NUMPRS_USE_ALL && *lpszStr != '\0')
1900 return DISP_E_TYPEMISMATCH; /* Not all chars were consumed */
1903 return DISP_E_TYPEMISMATCH; /* No Number found */
1905 pNumprs->cchUsed = cchUsed;
1909 /* VTBIT flags indicating an integer value */
1910 #define INTEGER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8)
1911 /* VTBIT flags indicating a real number value */
1912 #define REAL_VTBITS (VTBIT_R4|VTBIT_R8|VTBIT_CY)
1914 /**********************************************************************
1915 * VarNumFromParseNum [OLEAUT32.47]
1917 * Convert a NUMPARSE structure into a numeric Variant type.
1920 * pNumprs [I] Source for parsed number. cDig must be set to the size of rgbDig
1921 * rgbDig [I] Source for the numbers digits
1922 * dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
1923 * pVarDst [O] Destination for the converted Variant value.
1926 * Success: S_OK. pVarDst contains the converted value.
1927 * Failure: E_INVALIDARG, if any parameter is invalid.
1928 * DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
1931 * - The smallest favoured type present in dwVtBits that can represent the
1932 * number in pNumprs without losing precision is used.
1933 * - Signed types are preferrred over unsigned types of the same size.
1934 * - Preferred types in order are: integer, float, double, currency then decimal.
1935 * - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
1936 * for details of the rounding method.
1937 * - pVarDst is not cleared before the result is stored in it.
1939 HRESULT WINAPI VarNumFromParseNum(NUMPARSE *pNumprs, BYTE *rgbDig,
1940 ULONG dwVtBits, VARIANT *pVarDst)
1942 /* Scale factors and limits for double arithmetic */
1943 static const double dblMultipliers[11] = {
1944 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
1945 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
1947 static const double dblMinimums[11] = {
1948 R8_MIN, R8_MIN*10.0, R8_MIN*100.0, R8_MIN*1000.0, R8_MIN*10000.0,
1949 R8_MIN*100000.0, R8_MIN*1000000.0, R8_MIN*10000000.0,
1950 R8_MIN*100000000.0, R8_MIN*1000000000.0, R8_MIN*10000000000.0
1952 static const double dblMaximums[11] = {
1953 R8_MAX, R8_MAX/10.0, R8_MAX/100.0, R8_MAX/1000.0, R8_MAX/10000.0,
1954 R8_MAX/100000.0, R8_MAX/1000000.0, R8_MAX/10000000.0,
1955 R8_MAX/100000000.0, R8_MAX/1000000000.0, R8_MAX/10000000000.0
1958 int wholeNumberDigits, fractionalDigits, divisor10 = 0, multiplier10 = 0;
1960 TRACE("(%p,%p,0x%lx,%p)\n", pNumprs, rgbDig, dwVtBits, pVarDst);
1962 if (pNumprs->nBaseShift)
1964 /* nBaseShift indicates a hex or octal number */
1969 /* Convert the hex or octal number string into a UI64 */
1970 for (i = 0; i < pNumprs->cDig; i++)
1972 if (ul64 > ((UI8_MAX>>pNumprs->nBaseShift) - rgbDig[i]))
1974 TRACE("Overflow multiplying digits\n");
1975 return DISP_E_OVERFLOW;
1977 ul64 = (ul64<<pNumprs->nBaseShift) + rgbDig[i];
1980 /* also make a negative representation */
1983 /* Try signed and unsigned types in size order */
1984 if (dwVtBits & VTBIT_I1 && ((ul64 <= I1_MAX)||(l64 >= I1_MIN)))
1986 V_VT(pVarDst) = VT_I1;
1988 V_I1(pVarDst) = ul64;
1990 V_I1(pVarDst) = l64;
1993 else if (dwVtBits & VTBIT_UI1 && ul64 <= UI1_MAX)
1995 V_VT(pVarDst) = VT_UI1;
1996 V_UI1(pVarDst) = ul64;
1999 else if (dwVtBits & VTBIT_I2 && ((ul64 <= I2_MAX)||(l64 >= I2_MIN)))
2001 V_VT(pVarDst) = VT_I2;
2003 V_I2(pVarDst) = ul64;
2005 V_I2(pVarDst) = l64;
2008 else if (dwVtBits & VTBIT_UI2 && ul64 <= UI2_MAX)
2010 V_VT(pVarDst) = VT_UI2;
2011 V_UI2(pVarDst) = ul64;
2014 else if (dwVtBits & VTBIT_I4 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2016 V_VT(pVarDst) = VT_I4;
2018 V_I4(pVarDst) = ul64;
2020 V_I4(pVarDst) = l64;
2023 else if (dwVtBits & VTBIT_UI4 && ul64 <= UI4_MAX)
2025 V_VT(pVarDst) = VT_UI4;
2026 V_UI4(pVarDst) = ul64;
2029 else if (dwVtBits & VTBIT_I8 && ((ul64 <= I4_MAX)||(l64>=I4_MIN)))
2031 V_VT(pVarDst) = VT_I8;
2032 V_I8(pVarDst) = ul64;
2035 else if (dwVtBits & VTBIT_UI8)
2037 V_VT(pVarDst) = VT_UI8;
2038 V_UI8(pVarDst) = ul64;
2041 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2043 V_VT(pVarDst) = VT_DECIMAL;
2044 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2045 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2046 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2049 else if (dwVtBits & VTBIT_R4 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2051 V_VT(pVarDst) = VT_R4;
2053 V_R4(pVarDst) = ul64;
2055 V_R4(pVarDst) = l64;
2058 else if (dwVtBits & VTBIT_R8 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2060 V_VT(pVarDst) = VT_R8;
2062 V_R8(pVarDst) = ul64;
2064 V_R8(pVarDst) = l64;
2068 TRACE("Overflow: possible return types: 0x%lx, value: %s\n", dwVtBits, wine_dbgstr_longlong(ul64));
2069 return DISP_E_OVERFLOW;
2072 /* Count the number of relevant fractional and whole digits stored,
2073 * And compute the divisor/multiplier to scale the number by.
2075 if (pNumprs->nPwr10 < 0)
2077 if (-pNumprs->nPwr10 >= pNumprs->cDig)
2079 /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
2080 wholeNumberDigits = 0;
2081 fractionalDigits = pNumprs->cDig;
2082 divisor10 = -pNumprs->nPwr10;
2086 /* An exactly represented real number e.g. 1.024 */
2087 wholeNumberDigits = pNumprs->cDig + pNumprs->nPwr10;
2088 fractionalDigits = pNumprs->cDig - wholeNumberDigits;
2089 divisor10 = pNumprs->cDig - wholeNumberDigits;
2092 else if (pNumprs->nPwr10 == 0)
2094 /* An exactly represented whole number e.g. 1024 */
2095 wholeNumberDigits = pNumprs->cDig;
2096 fractionalDigits = 0;
2098 else /* pNumprs->nPwr10 > 0 */
2100 /* A whole number followed by nPwr10 0's e.g. 102400 */
2101 wholeNumberDigits = pNumprs->cDig;
2102 fractionalDigits = 0;
2103 multiplier10 = pNumprs->nPwr10;
2106 TRACE("cDig %d; nPwr10 %d, whole %d, frac %d ", pNumprs->cDig,
2107 pNumprs->nPwr10, wholeNumberDigits, fractionalDigits);
2108 TRACE("mult %d; div %d\n", multiplier10, divisor10);
2110 if (dwVtBits & (INTEGER_VTBITS|VTBIT_DECIMAL) &&
2111 (!fractionalDigits || !(dwVtBits & (REAL_VTBITS|VTBIT_CY|VTBIT_DECIMAL))))
2113 /* We have one or more integer output choices, and either:
2114 * 1) An integer input value, or
2115 * 2) A real number input value but no floating output choices.
2116 * Alternately, we have a DECIMAL output available and an integer input.
2118 * So, place the integer value into pVarDst, using the smallest type
2119 * possible and preferring signed over unsigned types.
2121 BOOL bOverflow = FALSE, bNegative;
2125 /* Convert the integer part of the number into a UI8 */
2126 for (i = 0; i < wholeNumberDigits; i++)
2128 if (ul64 > (UI8_MAX / 10 - rgbDig[i]))
2130 TRACE("Overflow multiplying digits\n");
2134 ul64 = ul64 * 10 + rgbDig[i];
2137 /* Account for the scale of the number */
2138 if (!bOverflow && multiplier10)
2140 for (i = 0; i < multiplier10; i++)
2142 if (ul64 > (UI8_MAX / 10))
2144 TRACE("Overflow scaling number\n");
2152 /* If we have any fractional digits, round the value.
2153 * Note we don't have to do this if divisor10 is < 1,
2154 * because this means the fractional part must be < 0.5
2156 if (!bOverflow && fractionalDigits && divisor10 > 0)
2158 const BYTE* fracDig = rgbDig + wholeNumberDigits;
2159 BOOL bAdjust = FALSE;
2161 TRACE("first decimal value is %d\n", *fracDig);
2164 bAdjust = TRUE; /* > 0.5 */
2165 else if (*fracDig == 5)
2167 for (i = 1; i < fractionalDigits; i++)
2171 bAdjust = TRUE; /* > 0.5 */
2175 /* If exactly 0.5, round only odd values */
2176 if (i == fractionalDigits && (ul64 & 1))
2182 if (ul64 == UI8_MAX)
2184 TRACE("Overflow after rounding\n");
2191 /* Zero is not a negative number */
2192 bNegative = pNumprs->dwOutFlags & NUMPRS_NEG && ul64 ? TRUE : FALSE;
2194 TRACE("Integer value is %lld, bNeg %d\n", ul64, bNegative);
2196 /* For negative integers, try the signed types in size order */
2197 if (!bOverflow && bNegative)
2199 if (dwVtBits & (VTBIT_I1|VTBIT_I2|VTBIT_I4|VTBIT_I8))
2201 if (dwVtBits & VTBIT_I1 && ul64 <= -I1_MIN)
2203 V_VT(pVarDst) = VT_I1;
2204 V_I1(pVarDst) = -ul64;
2207 else if (dwVtBits & VTBIT_I2 && ul64 <= -I2_MIN)
2209 V_VT(pVarDst) = VT_I2;
2210 V_I2(pVarDst) = -ul64;
2213 else if (dwVtBits & VTBIT_I4 && ul64 <= -((LONGLONG)I4_MIN))
2215 V_VT(pVarDst) = VT_I4;
2216 V_I4(pVarDst) = -ul64;
2219 else if (dwVtBits & VTBIT_I8 && ul64 <= (ULONGLONG)I8_MAX + 1)
2221 V_VT(pVarDst) = VT_I8;
2222 V_I8(pVarDst) = -ul64;
2225 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2227 /* Decimal is only output choice left - fast path */
2228 V_VT(pVarDst) = VT_DECIMAL;
2229 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_NEG,0);
2230 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2231 DEC_LO64(&V_DECIMAL(pVarDst)) = -ul64;
2236 else if (!bOverflow)
2238 /* For positive integers, try signed then unsigned types in size order */
2239 if (dwVtBits & VTBIT_I1 && ul64 <= I1_MAX)
2241 V_VT(pVarDst) = VT_I1;
2242 V_I1(pVarDst) = ul64;
2245 else if (dwVtBits & VTBIT_UI1 && ul64 <= UI1_MAX)
2247 V_VT(pVarDst) = VT_UI1;
2248 V_UI1(pVarDst) = ul64;
2251 else if (dwVtBits & VTBIT_I2 && ul64 <= I2_MAX)
2253 V_VT(pVarDst) = VT_I2;
2254 V_I2(pVarDst) = ul64;
2257 else if (dwVtBits & VTBIT_UI2 && ul64 <= UI2_MAX)
2259 V_VT(pVarDst) = VT_UI2;
2260 V_UI2(pVarDst) = ul64;
2263 else if (dwVtBits & VTBIT_I4 && ul64 <= I4_MAX)
2265 V_VT(pVarDst) = VT_I4;
2266 V_I4(pVarDst) = ul64;
2269 else if (dwVtBits & VTBIT_UI4 && ul64 <= UI4_MAX)
2271 V_VT(pVarDst) = VT_UI4;
2272 V_UI4(pVarDst) = ul64;
2275 else if (dwVtBits & VTBIT_I8 && ul64 <= I8_MAX)
2277 V_VT(pVarDst) = VT_I8;
2278 V_I8(pVarDst) = ul64;
2281 else if (dwVtBits & VTBIT_UI8)
2283 V_VT(pVarDst) = VT_UI8;
2284 V_UI8(pVarDst) = ul64;
2287 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2289 /* Decimal is only output choice left - fast path */
2290 V_VT(pVarDst) = VT_DECIMAL;
2291 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2292 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2293 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2299 if (dwVtBits & REAL_VTBITS)
2301 /* Try to put the number into a float or real */
2302 BOOL bOverflow = FALSE, bNegative = pNumprs->dwOutFlags & NUMPRS_NEG;
2306 /* Convert the number into a double */
2307 for (i = 0; i < pNumprs->cDig; i++)
2308 whole = whole * 10.0 + rgbDig[i];
2310 TRACE("Whole double value is %16.16g\n", whole);
2312 /* Account for the scale */
2313 while (multiplier10 > 10)
2315 if (whole > dblMaximums[10])
2317 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2321 whole = whole * dblMultipliers[10];
2326 if (whole > dblMaximums[multiplier10])
2328 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2332 whole = whole * dblMultipliers[multiplier10];
2335 TRACE("Scaled double value is %16.16g\n", whole);
2337 while (divisor10 > 10)
2339 if (whole < dblMinimums[10])
2341 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2345 whole = whole / dblMultipliers[10];
2350 if (whole < dblMinimums[divisor10])
2352 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2356 whole = whole / dblMultipliers[divisor10];
2359 TRACE("Final double value is %16.16g\n", whole);
2361 if (dwVtBits & VTBIT_R4 &&
2362 ((whole <= R4_MAX && whole >= R4_MIN) || whole == 0.0))
2364 TRACE("Set R4 to final value\n");
2365 V_VT(pVarDst) = VT_R4; /* Fits into a float */
2366 V_R4(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2370 if (dwVtBits & VTBIT_R8)
2372 TRACE("Set R8 to final value\n");
2373 V_VT(pVarDst) = VT_R8; /* Fits into a double */
2374 V_R8(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2378 if (dwVtBits & VTBIT_CY)
2380 if (SUCCEEDED(VarCyFromR8(bNegative ? -whole : whole, &V_CY(pVarDst))))
2382 V_VT(pVarDst) = VT_CY; /* Fits into a currency */
2383 TRACE("Set CY to final value\n");
2386 TRACE("Value Overflows CY\n");
2390 if (dwVtBits & VTBIT_DECIMAL)
2395 DECIMAL* pDec = &V_DECIMAL(pVarDst);
2397 DECIMAL_SETZERO(pDec);
2400 if (pNumprs->dwOutFlags & NUMPRS_NEG)
2401 DEC_SIGN(pDec) = DECIMAL_NEG;
2403 DEC_SIGN(pDec) = DECIMAL_POS;
2405 /* Factor the significant digits */
2406 for (i = 0; i < pNumprs->cDig; i++)
2408 tmp = (ULONG64)DEC_LO32(pDec) * 10 + rgbDig[i];
2409 carry = (ULONG)(tmp >> 32);
2410 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2411 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2412 carry = (ULONG)(tmp >> 32);
2413 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2414 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2415 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2417 if (tmp >> 32 & UI4_MAX)
2419 VarNumFromParseNum_DecOverflow:
2420 TRACE("Overflow\n");
2421 DEC_LO32(pDec) = DEC_MID32(pDec) = DEC_HI32(pDec) = UI4_MAX;
2422 return DISP_E_OVERFLOW;
2426 /* Account for the scale of the number */
2427 while (multiplier10 > 0)
2429 tmp = (ULONG64)DEC_LO32(pDec) * 10;
2430 carry = (ULONG)(tmp >> 32);
2431 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2432 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2433 carry = (ULONG)(tmp >> 32);
2434 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2435 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2436 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2438 if (tmp >> 32 & UI4_MAX)
2439 goto VarNumFromParseNum_DecOverflow;
2442 DEC_SCALE(pDec) = divisor10;
2444 V_VT(pVarDst) = VT_DECIMAL;
2447 return DISP_E_OVERFLOW; /* No more output choices */
2450 /**********************************************************************
2451 * VarCat [OLEAUT32.318]
2453 HRESULT WINAPI VarCat(LPVARIANT left, LPVARIANT right, LPVARIANT out)
2455 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2456 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), out);
2458 /* Should we VariantClear out? */
2459 /* Can we handle array, vector, by ref etc. */
2460 if ((V_VT(left)&VT_TYPEMASK) == VT_NULL &&
2461 (V_VT(right)&VT_TYPEMASK) == VT_NULL)
2463 V_VT(out) = VT_NULL;
2467 if (V_VT(left) == VT_BSTR && V_VT(right) == VT_BSTR)
2469 V_VT(out) = VT_BSTR;
2470 VarBstrCat (V_BSTR(left), V_BSTR(right), &V_BSTR(out));
2473 if (V_VT(left) == VT_BSTR) {
2477 V_VT(out) = VT_BSTR;
2478 hres = VariantChangeTypeEx(&bstrvar,right,0,0,VT_BSTR);
2480 FIXME("Failed to convert right side from vt %d to VT_BSTR?\n",V_VT(right));
2483 VarBstrCat (V_BSTR(left), V_BSTR(&bstrvar), &V_BSTR(out));
2486 if (V_VT(right) == VT_BSTR) {
2490 V_VT(out) = VT_BSTR;
2491 hres = VariantChangeTypeEx(&bstrvar,left,0,0,VT_BSTR);
2493 FIXME("Failed to convert right side from vt %d to VT_BSTR?\n",V_VT(right));
2496 VarBstrCat (V_BSTR(&bstrvar), V_BSTR(right), &V_BSTR(out));
2499 FIXME ("types %d / %d not supported\n",V_VT(left)&VT_TYPEMASK, V_VT(right)&VT_TYPEMASK);
2503 /**********************************************************************
2504 * VarCmp [OLEAUT32.176]
2507 * NORM_IGNORECASE, NORM_IGNORENONSPACE, NORM_IGNORESYMBOLS
2508 * NORM_IGNOREWIDTH, NORM_IGNOREKANATYPE, NORM_IGNOREKASHIDA
2511 HRESULT WINAPI VarCmp(LPVARIANT left, LPVARIANT right, LCID lcid, DWORD flags)
2521 TRACE("(%p->(%s%s),%p->(%s%s),0x%08lx,0x%08lx)\n", left, debugstr_VT(left),
2522 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), lcid, flags);
2524 VariantInit(&lv);VariantInit(&rv);
2525 V_VT(right) &= ~0x8000; /* hack since we sometime get this flag. */
2526 V_VT(left) &= ~0x8000; /* hack since we sometime get this flag. */
2528 /* If either are null, then return VARCMP_NULL */
2529 if ((V_VT(left)&VT_TYPEMASK) == VT_NULL ||
2530 (V_VT(right)&VT_TYPEMASK) == VT_NULL)
2533 /* Strings - use VarBstrCmp */
2534 if ((V_VT(left)&VT_TYPEMASK) == VT_BSTR &&
2535 (V_VT(right)&VT_TYPEMASK) == VT_BSTR) {
2536 return VarBstrCmp(V_BSTR(left), V_BSTR(right), lcid, flags);
2539 xmask = (1<<(V_VT(left)&VT_TYPEMASK))|(1<<(V_VT(right)&VT_TYPEMASK));
2540 if (xmask & (1<<VT_R8)) {
2541 rc = VariantChangeType(&lv,left,0,VT_R8);
2542 if (FAILED(rc)) return rc;
2543 rc = VariantChangeType(&rv,right,0,VT_R8);
2544 if (FAILED(rc)) return rc;
2546 if (V_R8(&lv) == V_R8(&rv)) return VARCMP_EQ;
2547 if (V_R8(&lv) < V_R8(&rv)) return VARCMP_LT;
2548 if (V_R8(&lv) > V_R8(&rv)) return VARCMP_GT;
2549 return E_FAIL; /* can't get here */
2551 if (xmask & (1<<VT_R4)) {
2552 rc = VariantChangeType(&lv,left,0,VT_R4);
2553 if (FAILED(rc)) return rc;
2554 rc = VariantChangeType(&rv,right,0,VT_R4);
2555 if (FAILED(rc)) return rc;
2557 if (V_R4(&lv) == V_R4(&rv)) return VARCMP_EQ;
2558 if (V_R4(&lv) < V_R4(&rv)) return VARCMP_LT;
2559 if (V_R4(&lv) > V_R4(&rv)) return VARCMP_GT;
2560 return E_FAIL; /* can't get here */
2563 /* Integers - Ideally like to use VarDecCmp, but no Dec support yet
2564 Use LONGLONG to maximize ranges */
2566 switch (V_VT(left)&VT_TYPEMASK) {
2567 case VT_I1 : lVal = V_UNION(left,cVal); break;
2568 case VT_I2 : lVal = V_UNION(left,iVal); break;
2569 case VT_I4 : lVal = V_UNION(left,lVal); break;
2570 case VT_INT : lVal = V_UNION(left,lVal); break;
2571 case VT_UI1 : lVal = V_UNION(left,bVal); break;
2572 case VT_UI2 : lVal = V_UNION(left,uiVal); break;
2573 case VT_UI4 : lVal = V_UNION(left,ulVal); break;
2574 case VT_UINT : lVal = V_UNION(left,ulVal); break;
2575 case VT_BOOL : lVal = V_UNION(left,boolVal); break;
2576 default: lOk = FALSE;
2580 switch (V_VT(right)&VT_TYPEMASK) {
2581 case VT_I1 : rVal = V_UNION(right,cVal); break;
2582 case VT_I2 : rVal = V_UNION(right,iVal); break;
2583 case VT_I4 : rVal = V_UNION(right,lVal); break;
2584 case VT_INT : rVal = V_UNION(right,lVal); break;
2585 case VT_UI1 : rVal = V_UNION(right,bVal); break;
2586 case VT_UI2 : rVal = V_UNION(right,uiVal); break;
2587 case VT_UI4 : rVal = V_UNION(right,ulVal); break;
2588 case VT_UINT : rVal = V_UNION(right,ulVal); break;
2589 case VT_BOOL : rVal = V_UNION(right,boolVal); break;
2590 default: rOk = FALSE;
2596 } else if (lVal > rVal) {
2603 /* Strings - use VarBstrCmp */
2604 if ((V_VT(left)&VT_TYPEMASK) == VT_DATE &&
2605 (V_VT(right)&VT_TYPEMASK) == VT_DATE) {
2607 if (floor(V_UNION(left,date)) == floor(V_UNION(right,date))) {
2608 /* Due to floating point rounding errors, calculate varDate in whole numbers) */
2609 double wholePart = 0.0;
2613 /* Get the fraction * 24*60*60 to make it into whole seconds */
2614 wholePart = (double) floor( V_UNION(left,date) );
2615 if (wholePart == 0) wholePart = 1;
2616 leftR = floor(fmod( V_UNION(left,date), wholePart ) * (24*60*60));
2618 wholePart = (double) floor( V_UNION(right,date) );
2619 if (wholePart == 0) wholePart = 1;
2620 rightR = floor(fmod( V_UNION(right,date), wholePart ) * (24*60*60));
2622 if (leftR < rightR) {
2624 } else if (leftR > rightR) {
2630 } else if (V_UNION(left,date) < V_UNION(right,date)) {
2632 } else if (V_UNION(left,date) > V_UNION(right,date)) {
2636 FIXME("VarCmp partial implementation, doesn't support vt 0x%x / 0x%x\n",V_VT(left), V_VT(right));
2640 /**********************************************************************
2641 * VarAnd [OLEAUT32.142]
2644 HRESULT WINAPI VarAnd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2646 HRESULT rc = E_FAIL;
2648 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2649 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2651 if ((V_VT(left)&VT_TYPEMASK) == VT_BOOL &&
2652 (V_VT(right)&VT_TYPEMASK) == VT_BOOL) {
2654 V_VT(result) = VT_BOOL;
2655 if (V_BOOL(left) && V_BOOL(right)) {
2656 V_BOOL(result) = VARIANT_TRUE;
2658 V_BOOL(result) = VARIANT_FALSE;
2669 int resT = 0; /* Testing has shown I2 & I2 == I2, all else
2670 becomes I4, even unsigned ints (incl. UI2) */
2673 switch (V_VT(left)&VT_TYPEMASK) {
2674 case VT_I1 : lVal = V_UNION(left,cVal); resT=VT_I4; break;
2675 case VT_I2 : lVal = V_UNION(left,iVal); resT=VT_I2; break;
2676 case VT_I4 : lVal = V_UNION(left,lVal); resT=VT_I4; break;
2677 case VT_INT : lVal = V_UNION(left,lVal); resT=VT_I4; break;
2678 case VT_UI1 : lVal = V_UNION(left,bVal); resT=VT_I4; break;
2679 case VT_UI2 : lVal = V_UNION(left,uiVal); resT=VT_I4; break;
2680 case VT_UI4 : lVal = V_UNION(left,ulVal); resT=VT_I4; break;
2681 case VT_UINT : lVal = V_UNION(left,ulVal); resT=VT_I4; break;
2682 case VT_BOOL : rVal = V_UNION(left,boolVal); resT=VT_I4; break;
2683 default: lOk = FALSE;
2687 switch (V_VT(right)&VT_TYPEMASK) {
2688 case VT_I1 : rVal = V_UNION(right,cVal); resT=VT_I4; break;
2689 case VT_I2 : rVal = V_UNION(right,iVal); resT=max(VT_I2, resT); break;
2690 case VT_I4 : rVal = V_UNION(right,lVal); resT=VT_I4; break;
2691 case VT_INT : rVal = V_UNION(right,lVal); resT=VT_I4; break;
2692 case VT_UI1 : rVal = V_UNION(right,bVal); resT=VT_I4; break;
2693 case VT_UI2 : rVal = V_UNION(right,uiVal); resT=VT_I4; break;
2694 case VT_UI4 : rVal = V_UNION(right,ulVal); resT=VT_I4; break;
2695 case VT_UINT : rVal = V_UNION(right,ulVal); resT=VT_I4; break;
2696 case VT_BOOL : rVal = V_UNION(right,boolVal); resT=VT_I4; break;
2697 default: rOk = FALSE;
2701 res = (lVal & rVal);
2702 V_VT(result) = resT;
2704 case VT_I2 : V_UNION(result,iVal) = res; break;
2705 case VT_I4 : V_UNION(result,lVal) = res; break;
2707 FIXME("Unexpected result variant type %x\n", resT);
2708 V_UNION(result,lVal) = res;
2713 FIXME("VarAnd stub\n");
2717 TRACE("returning 0x%8lx (%s%s),%ld\n", rc, debugstr_VT(result),
2718 debugstr_VF(result), V_VT(result) == VT_I4 ? V_I4(result) : V_I2(result));
2722 /**********************************************************************
2723 * VarAdd [OLEAUT32.141]
2724 * FIXME: From MSDN: If ... Then
2725 * Both expressions are of the string type Concatenated.
2726 * One expression is a string type and the other a character Addition.
2727 * One expression is numeric and the other is a string Addition.
2728 * Both expressions are numeric Addition.
2729 * Either expression is NULL NULL is returned.
2730 * Both expressions are empty Integer subtype is returned.
2733 HRESULT WINAPI VarAdd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2735 HRESULT rc = E_FAIL;
2737 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2738 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2740 if ((V_VT(left)&VT_TYPEMASK) == VT_EMPTY)
2741 return VariantCopy(result,right);
2743 if ((V_VT(right)&VT_TYPEMASK) == VT_EMPTY)
2744 return VariantCopy(result,left);
2746 /* check if we add doubles */
2747 if (((V_VT(left)&VT_TYPEMASK) == VT_R8) || ((V_VT(right)&VT_TYPEMASK) == VT_R8)) {
2755 switch (V_VT(left)&VT_TYPEMASK) {
2756 case VT_I1 : lVal = V_UNION(left,cVal); break;
2757 case VT_I2 : lVal = V_UNION(left,iVal); break;
2758 case VT_I4 : lVal = V_UNION(left,lVal); break;
2759 case VT_INT : lVal = V_UNION(left,lVal); break;
2760 case VT_UI1 : lVal = V_UNION(left,bVal); break;
2761 case VT_UI2 : lVal = V_UNION(left,uiVal); break;
2762 case VT_UI4 : lVal = V_UNION(left,ulVal); break;
2763 case VT_UINT : lVal = V_UNION(left,ulVal); break;
2764 case VT_R4 : lVal = V_UNION(left,fltVal); break;
2765 case VT_R8 : lVal = V_UNION(left,dblVal); break;
2766 case VT_NULL : lVal = 0.0; break;
2767 default: lOk = FALSE;
2771 switch (V_VT(right)&VT_TYPEMASK) {
2772 case VT_I1 : rVal = V_UNION(right,cVal); break;
2773 case VT_I2 : rVal = V_UNION(right,iVal); break;
2774 case VT_I4 : rVal = V_UNION(right,lVal); break;
2775 case VT_INT : rVal = V_UNION(right,lVal); break;
2776 case VT_UI1 : rVal = V_UNION(right,bVal); break;
2777 case VT_UI2 : rVal = V_UNION(right,uiVal); break;
2778 case VT_UI4 : rVal = V_UNION(right,ulVal); break;
2779 case VT_UINT : rVal = V_UNION(right,ulVal); break;
2780 case VT_R4 : rVal = V_UNION(right,fltVal);break;
2781 case VT_R8 : rVal = V_UNION(right,dblVal);break;
2782 case VT_NULL : rVal = 0.0; break;
2783 default: rOk = FALSE;
2787 res = (lVal + rVal);
2788 V_VT(result) = VT_R8;
2789 V_UNION(result,dblVal) = res;
2792 FIXME("Unhandled type pair %d / %d in double addition.\n",
2793 (V_VT(left)&VT_TYPEMASK),
2794 (V_VT(right)&VT_TYPEMASK)
2800 /* now check if we add floats. VT_R8 can no longer happen here! */
2801 if (((V_VT(left)&VT_TYPEMASK) == VT_R4) || ((V_VT(right)&VT_TYPEMASK) == VT_R4)) {
2809 switch (V_VT(left)&VT_TYPEMASK) {
2810 case VT_I1 : lVal = V_UNION(left,cVal); break;
2811 case VT_I2 : lVal = V_UNION(left,iVal); break;
2812 case VT_I4 : lVal = V_UNION(left,lVal); break;
2813 case VT_INT : lVal = V_UNION(left,lVal); break;
2814 case VT_UI1 : lVal = V_UNION(left,bVal); break;
2815 case VT_UI2 : lVal = V_UNION(left,uiVal); break;
2816 case VT_UI4 : lVal = V_UNION(left,ulVal); break;
2817 case VT_UINT : lVal = V_UNION(left,ulVal); break;
2818 case VT_R4 : lVal = V_UNION(left,fltVal); break;
2819 case VT_NULL : lVal = 0.0; break;
2820 default: lOk = FALSE;
2824 switch (V_VT(right)&VT_TYPEMASK) {
2825 case VT_I1 : rVal = V_UNION(right,cVal); break;
2826 case VT_I2 : rVal = V_UNION(right,iVal); break;
2827 case VT_I4 : rVal = V_UNION(right,lVal); break;
2828 case VT_INT : rVal = V_UNION(right,lVal); break;
2829 case VT_UI1 : rVal = V_UNION(right,bVal); break;
2830 case VT_UI2 : rVal = V_UNION(right,uiVal); break;
2831 case VT_UI4 : rVal = V_UNION(right,ulVal); break;
2832 case VT_UINT : rVal = V_UNION(right,ulVal); break;
2833 case VT_R4 : rVal = V_UNION(right,fltVal);break;
2834 case VT_NULL : rVal = 0.0; break;
2835 default: rOk = FALSE;
2839 res = (lVal + rVal);
2840 V_VT(result) = VT_R4;
2841 V_UNION(result,fltVal) = res;
2844 FIXME("Unhandled type pair %d / %d in float addition.\n",
2845 (V_VT(left)&VT_TYPEMASK),
2846 (V_VT(right)&VT_TYPEMASK)
2852 /* Handle strings as concat */
2853 if ((V_VT(left)&VT_TYPEMASK) == VT_BSTR &&
2854 (V_VT(right)&VT_TYPEMASK) == VT_BSTR) {
2855 V_VT(result) = VT_BSTR;
2856 return VarBstrCat(V_BSTR(left), V_BSTR(right), &V_BSTR(result));
2865 int resT = 0; /* Testing has shown I2 + I2 == I2, all else
2869 switch (V_VT(left)&VT_TYPEMASK) {
2870 case VT_I1 : lVal = V_UNION(left,cVal); resT=VT_I4; break;
2871 case VT_I2 : lVal = V_UNION(left,iVal); resT=VT_I2; break;
2872 case VT_I4 : lVal = V_UNION(left,lVal); resT=VT_I4; break;
2873 case VT_INT : lVal = V_UNION(left,lVal); resT=VT_I4; break;
2874 case VT_UI1 : lVal = V_UNION(left,bVal); resT=VT_I4; break;
2875 case VT_UI2 : lVal = V_UNION(left,uiVal); resT=VT_I4; break;
2876 case VT_UI4 : lVal = V_UNION(left,ulVal); resT=VT_I4; break;
2877 case VT_UINT : lVal = V_UNION(left,ulVal); resT=VT_I4; break;
2878 case VT_NULL : lVal = 0; resT = VT_I4; break;
2879 default: lOk = FALSE;
2883 switch (V_VT(right)&VT_TYPEMASK) {
2884 case VT_I1 : rVal = V_UNION(right,cVal); resT=VT_I4; break;
2885 case VT_I2 : rVal = V_UNION(right,iVal); resT=max(VT_I2, resT); break;
2886 case VT_I4 : rVal = V_UNION(right,lVal); resT=VT_I4; break;
2887 case VT_INT : rVal = V_UNION(right,lVal); resT=VT_I4; break;
2888 case VT_UI1 : rVal = V_UNION(right,bVal); resT=VT_I4; break;
2889 case VT_UI2 : rVal = V_UNION(right,uiVal); resT=VT_I4; break;
2890 case VT_UI4 : rVal = V_UNION(right,ulVal); resT=VT_I4; break;
2891 case VT_UINT : rVal = V_UNION(right,ulVal); resT=VT_I4; break;
2892 case VT_NULL : rVal = 0; resT=VT_I4; break;
2893 default: rOk = FALSE;
2897 res = (lVal + rVal);
2898 V_VT(result) = resT;
2900 case VT_I2 : V_UNION(result,iVal) = res; break;
2901 case VT_I4 : V_UNION(result,lVal) = res; break;
2903 FIXME("Unexpected result variant type %x\n", resT);
2904 V_UNION(result,lVal) = res;
2909 FIXME("unimplemented part (0x%x + 0x%x)\n",V_VT(left), V_VT(right));
2913 TRACE("returning 0x%8lx (%s%s),%ld\n", rc, debugstr_VT(result),
2914 debugstr_VF(result), V_VT(result) == VT_I4 ? V_I4(result) : V_I2(result));
2918 /**********************************************************************
2919 * VarMul [OLEAUT32.156]
2922 HRESULT WINAPI VarMul(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2924 HRESULT rc = E_FAIL;
2925 VARTYPE lvt,rvt,resvt;
2929 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2930 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2932 VariantInit(&lv);VariantInit(&rv);
2933 lvt = V_VT(left)&VT_TYPEMASK;
2934 rvt = V_VT(right)&VT_TYPEMASK;
2935 found = FALSE;resvt=VT_VOID;
2936 if (((1<<lvt) | (1<<rvt)) & ((1<<VT_R4)|(1<<VT_R8))) {
2940 if (!found && (((1<<lvt) | (1<<rvt)) & ((1<<VT_I1)|(1<<VT_I2)|(1<<VT_UI1)|(1<<VT_UI2)|(1<<VT_I4)|(1<<VT_UI4)|(1<<VT_INT)|(1<<VT_UINT)))) {
2945 FIXME("can't expand vt %d vs %d to a target type.\n",lvt,rvt);
2948 rc = VariantChangeType(&lv, left, 0, resvt);
2950 FIXME("Could not convert 0x%x to %d?\n",V_VT(left),resvt);
2953 rc = VariantChangeType(&rv, right, 0, resvt);
2955 FIXME("Could not convert 0x%x to %d?\n",V_VT(right),resvt);
2960 V_VT(result) = resvt;
2961 V_R8(result) = V_R8(&lv) * V_R8(&rv);
2965 V_VT(result) = resvt;
2966 V_I4(result) = V_I4(&lv) * V_I4(&rv);
2970 TRACE("returning 0x%8lx (%s%s),%g\n", rc, debugstr_VT(result),
2971 debugstr_VF(result), V_VT(result) == VT_R8 ? V_R8(result) : (double)V_I4(result));
2975 /**********************************************************************
2976 * VarDiv [OLEAUT32.143]
2979 HRESULT WINAPI VarDiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2981 HRESULT rc = E_FAIL;
2982 VARTYPE lvt,rvt,resvt;
2986 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2987 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2989 VariantInit(&lv);VariantInit(&rv);
2990 lvt = V_VT(left)&VT_TYPEMASK;
2991 rvt = V_VT(right)&VT_TYPEMASK;
2992 found = FALSE;resvt = VT_VOID;
2993 if (((1<<lvt) | (1<<rvt)) & ((1<<VT_R4)|(1<<VT_R8))) {
2997 if (!found && (((1<<lvt) | (1<<rvt)) & ((1<<VT_I1)|(1<<VT_I2)|(1<<VT_UI1)|(1<<VT_UI2)|(1<<VT_I4)|(1<<VT_UI4)|(1<<VT_INT)|(1<<VT_UINT)))) {
3002 FIXME("can't expand vt %d vs %d to a target type.\n",lvt,rvt);
3005 rc = VariantChangeType(&lv, left, 0, resvt);
3007 FIXME("Could not convert 0x%x to %d?\n",V_VT(left),resvt);
3010 rc = VariantChangeType(&rv, right, 0, resvt);
3012 FIXME("Could not convert 0x%x to %d?\n",V_VT(right),resvt);
3017 V_VT(result) = resvt;
3018 V_R8(result) = V_R8(&lv) / V_R8(&rv);
3022 V_VT(result) = resvt;
3023 V_I4(result) = V_I4(&lv) / V_I4(&rv);
3027 TRACE("returning 0x%8lx (%s%s),%g\n", rc, debugstr_VT(result),
3028 debugstr_VF(result), V_VT(result) == VT_R8 ? V_R8(result) : (double)V_I4(result));
3032 /**********************************************************************
3033 * VarSub [OLEAUT32.159]
3036 HRESULT WINAPI VarSub(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3038 HRESULT rc = E_FAIL;
3039 VARTYPE lvt,rvt,resvt;
3043 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3044 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
3046 VariantInit(&lv);VariantInit(&rv);
3047 lvt = V_VT(left)&VT_TYPEMASK;
3048 rvt = V_VT(right)&VT_TYPEMASK;
3049 found = FALSE;resvt = VT_VOID;
3050 if (((1<<lvt) | (1<<rvt)) & ((1<<VT_DATE)|(1<<VT_R4)|(1<<VT_R8))) {
3054 if (!found && (((1<<lvt) | (1<<rvt)) & ((1<<VT_I1)|(1<<VT_I2)|(1<<VT_UI1)|(1<<VT_UI2)|(1<<VT_I4)|(1<<VT_UI4)|(1<<VT_INT)|(1<<VT_UINT)))) {
3059 FIXME("can't expand vt %d vs %d to a target type.\n",lvt,rvt);
3062 rc = VariantChangeType(&lv, left, 0, resvt);
3064 FIXME("Could not convert 0x%x to %d?\n",V_VT(left),resvt);
3067 rc = VariantChangeType(&rv, right, 0, resvt);
3069 FIXME("Could not convert 0x%x to %d?\n",V_VT(right),resvt);
3074 V_VT(result) = resvt;
3075 V_R8(result) = V_R8(&lv) - V_R8(&rv);
3079 V_VT(result) = resvt;
3080 V_I4(result) = V_I4(&lv) - V_I4(&rv);
3084 TRACE("returning 0x%8lx (%s%s),%g\n", rc, debugstr_VT(result),
3085 debugstr_VF(result), V_VT(result) == VT_R8 ? V_R8(result) : (double)V_I4(result));
3089 /**********************************************************************
3090 * VarOr [OLEAUT32.157]
3092 * Perform a logical or (OR) operation on two variants.
3095 * pVarLeft [I] First variant
3096 * pVarRight [I] Variant to OR with pVarLeft
3097 * pVarOut [O] Destination for OR result
3100 * Success: S_OK. pVarOut contains the result of the operation with its type
3101 * taken from the table listed under VarXor().
3102 * Failure: An HRESULT error code indicating the error.
3105 * See the Notes section of VarXor() for further information.
3107 HRESULT WINAPI VarOr(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
3110 VARIANT varLeft, varRight, varStr;
3113 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
3114 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
3115 debugstr_VF(pVarRight), pVarOut);
3117 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
3118 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
3119 V_VT(pVarLeft) == VT_DISPATCH || V_VT(pVarRight) == VT_DISPATCH ||
3120 V_VT(pVarLeft) == VT_RECORD || V_VT(pVarRight) == VT_RECORD)
3121 return DISP_E_BADVARTYPE;
3123 V_VT(&varLeft) = V_VT(&varRight) = V_VT(&varStr) = VT_EMPTY;
3125 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
3127 /* NULL OR Zero is NULL, NULL OR value is value */
3128 if (V_VT(pVarLeft) == VT_NULL)
3129 pVarLeft = pVarRight; /* point to the non-NULL var */
3131 V_VT(pVarOut) = VT_NULL;
3134 switch (V_VT(pVarLeft))
3136 case VT_DATE: case VT_R8:
3141 if (V_BOOL(pVarLeft))
3142 *pVarOut = *pVarLeft;
3144 case VT_I2: case VT_UI2:
3153 if (V_UI1(pVarLeft))
3154 *pVarOut = *pVarLeft;
3160 case VT_I4: case VT_UI4: case VT_INT: case VT_UINT:
3165 if (V_CY(pVarLeft).int64)
3168 case VT_I8: case VT_UI8:
3173 if (DEC_HI32(&V_DECIMAL(pVarLeft)) || DEC_LO64(&V_DECIMAL(pVarLeft)))
3180 if (!V_BSTR(pVarLeft))
3181 return DISP_E_BADVARTYPE;
3183 hRet = VarBoolFromStr(V_BSTR(pVarLeft), LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
3184 if (SUCCEEDED(hRet) && b)
3186 V_VT(pVarOut) = VT_BOOL;
3187 V_BOOL(pVarOut) = b;
3191 case VT_NULL: case VT_EMPTY:
3192 V_VT(pVarOut) = VT_NULL;
3195 return DISP_E_BADVARTYPE;
3199 if (V_VT(pVarLeft) == VT_EMPTY || V_VT(pVarRight) == VT_EMPTY)
3201 if (V_VT(pVarLeft) == VT_EMPTY)
3202 pVarLeft = pVarRight; /* point to the non-EMPTY var */
3205 /* Since one argument is empty (0), OR'ing it with the other simply
3206 * gives the others value (as 0|x => x). So just convert the other
3207 * argument to the required result type.
3209 switch (V_VT(pVarLeft))
3212 if (!V_BSTR(pVarLeft))
3213 return DISP_E_BADVARTYPE;
3215 hRet = VariantCopy(&varStr, pVarLeft);
3219 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
3222 /* Fall Through ... */
3223 case VT_EMPTY: case VT_UI1: case VT_BOOL: case VT_I2:
3224 V_VT(pVarOut) = VT_I2;
3226 case VT_DATE: case VT_CY: case VT_DECIMAL: case VT_R4: case VT_R8:
3227 case VT_I1: case VT_UI2: case VT_I4: case VT_UI4:
3228 case VT_INT: case VT_UINT: case VT_UI8:
3229 V_VT(pVarOut) = VT_I4;
3232 V_VT(pVarOut) = VT_I8;
3235 return DISP_E_BADVARTYPE;
3237 hRet = VariantCopy(&varLeft, pVarLeft);
3240 pVarLeft = &varLeft;
3241 hRet = VariantChangeType(pVarOut, pVarLeft, 0, V_VT(pVarOut));
3245 if (V_VT(pVarLeft) == VT_BOOL && V_VT(pVarRight) == VT_BOOL)
3247 V_VT(pVarOut) = VT_BOOL;
3248 V_BOOL(pVarOut) = V_BOOL(pVarLeft) | V_BOOL(pVarRight);
3252 if (V_VT(pVarLeft) == VT_UI1 && V_VT(pVarRight) == VT_UI1)
3254 V_VT(pVarOut) = VT_UI1;
3255 V_UI1(pVarOut) = V_UI1(pVarLeft) | V_UI1(pVarRight);
3259 if (V_VT(pVarLeft) == VT_BSTR)
3261 hRet = VariantCopy(&varStr, pVarLeft);
3265 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
3270 if (V_VT(pVarLeft) == VT_BOOL &&
3271 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_BSTR))
3275 else if ((V_VT(pVarLeft) == VT_BOOL || V_VT(pVarLeft) == VT_UI1 ||
3276 V_VT(pVarLeft) == VT_I2 || V_VT(pVarLeft) == VT_BSTR) &&
3277 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_UI1 ||
3278 V_VT(pVarRight) == VT_I2 || V_VT(pVarRight) == VT_BSTR))
3282 else if (V_VT(pVarLeft) == VT_I8 || V_VT(pVarRight) == VT_I8)
3284 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
3285 return DISP_E_TYPEMISMATCH;
3289 hRet = VariantCopy(&varLeft, pVarLeft);
3293 hRet = VariantCopy(&varRight, pVarRight);
3297 if (vt == VT_I4 && V_VT(&varLeft) == VT_UI4)
3298 V_VT(&varLeft) = VT_I4; /* Don't overflow */
3303 if (V_VT(&varLeft) == VT_BSTR &&
3304 FAILED(VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d)))
3305 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL, VT_BOOL);
3306 if (SUCCEEDED(hRet) && V_VT(&varLeft) != vt)
3307 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
3312 if (vt == VT_I4 && V_VT(&varRight) == VT_UI4)
3313 V_VT(&varRight) = VT_I4; /* Don't overflow */
3318 if (V_VT(&varRight) == VT_BSTR &&
3319 FAILED(VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d)))
3320 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL, VT_BOOL);
3321 if (SUCCEEDED(hRet) && V_VT(&varRight) != vt)
3322 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
3330 V_I8(pVarOut) = V_I8(&varLeft) | V_I8(&varRight);
3332 else if (vt == VT_I4)
3334 V_I4(pVarOut) = V_I4(&varLeft) | V_I4(&varRight);
3338 V_I2(pVarOut) = V_I2(&varLeft) | V_I2(&varRight);
3342 VariantClear(&varStr);
3343 VariantClear(&varLeft);
3344 VariantClear(&varRight);
3348 /**********************************************************************
3349 * VarAbs [OLEAUT32.168]
3351 * Convert a variant to its absolute value.
3354 * pVarIn [I] Source variant
3355 * pVarOut [O] Destination for converted value
3358 * Success: S_OK. pVarOut contains the absolute value of pVarIn.
3359 * Failure: An HRESULT error code indicating the error.
3362 * - This function does not process by-reference variants.
3363 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3364 * according to the following table:
3365 *| Input Type Output Type
3366 *| ---------- -----------
3369 *| (All others) Unchanged
3371 HRESULT WINAPI VarAbs(LPVARIANT pVarIn, LPVARIANT pVarOut)
3374 HRESULT hRet = S_OK;
3376 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
3377 debugstr_VF(pVarIn), pVarOut);
3379 if (V_ISARRAY(pVarIn) || V_VT(pVarIn) == VT_UNKNOWN ||
3380 V_VT(pVarIn) == VT_DISPATCH || V_VT(pVarIn) == VT_RECORD ||
3381 V_VT(pVarIn) == VT_ERROR)
3382 return DISP_E_TYPEMISMATCH;
3384 *pVarOut = *pVarIn; /* Shallow copy the value, and invert it if needed */
3386 #define ABS_CASE(typ,min) \
3387 case VT_##typ: if (V_##typ(pVarIn) == min) hRet = DISP_E_OVERFLOW; \
3388 else if (V_##typ(pVarIn) < 0) V_##typ(pVarOut) = -V_##typ(pVarIn); \
3391 switch (V_VT(pVarIn))
3393 ABS_CASE(I1,I1_MIN);
3395 V_VT(pVarOut) = VT_I2;
3396 /* BOOL->I2, Fall through ... */
3397 ABS_CASE(I2,I2_MIN);
3399 ABS_CASE(I4,I4_MIN);
3400 ABS_CASE(I8,I8_MIN);
3401 ABS_CASE(R4,R4_MIN);
3403 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
3406 V_VT(pVarOut) = VT_R8;
3408 /* Fall through ... */
3410 ABS_CASE(R8,R8_MIN);
3412 hRet = VarCyAbs(V_CY(pVarIn), & V_CY(pVarOut));
3415 DEC_SIGN(&V_DECIMAL(pVarOut)) &= ~DECIMAL_NEG;
3425 V_VT(pVarOut) = VT_I2;
3430 hRet = DISP_E_BADVARTYPE;
3436 /**********************************************************************
3437 * VarFix [OLEAUT32.169]
3439 * Truncate a variants value to a whole number.
3442 * pVarIn [I] Source variant
3443 * pVarOut [O] Destination for converted value
3446 * Success: S_OK. pVarOut contains the converted value.
3447 * Failure: An HRESULT error code indicating the error.
3450 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3451 * according to the following table:
3452 *| Input Type Output Type
3453 *| ---------- -----------
3457 *| All Others Unchanged
3458 * - The difference between this function and VarInt() is that VarInt() rounds
3459 * negative numbers away from 0, while this function rounds them towards zero.
3461 HRESULT WINAPI VarFix(LPVARIANT pVarIn, LPVARIANT pVarOut)
3463 HRESULT hRet = S_OK;
3465 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
3466 debugstr_VF(pVarIn), pVarOut);
3468 V_VT(pVarOut) = V_VT(pVarIn);
3470 switch (V_VT(pVarIn))
3473 V_UI1(pVarOut) = V_UI1(pVarIn);
3476 V_VT(pVarOut) = VT_I2;
3479 V_I2(pVarOut) = V_I2(pVarIn);
3482 V_I4(pVarOut) = V_I4(pVarIn);
3485 V_I8(pVarOut) = V_I8(pVarIn);
3488 if (V_R4(pVarIn) < 0.0f)
3489 V_R4(pVarOut) = (float)ceil(V_R4(pVarIn));
3491 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
3494 V_VT(pVarOut) = VT_R8;
3495 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
3500 if (V_R8(pVarIn) < 0.0)
3501 V_R8(pVarOut) = ceil(V_R8(pVarIn));
3503 V_R8(pVarOut) = floor(V_R8(pVarIn));
3506 hRet = VarCyFix(V_CY(pVarIn), &V_CY(pVarOut));
3509 hRet = VarDecFix(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
3512 V_VT(pVarOut) = VT_I2;
3519 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
3520 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
3521 hRet = DISP_E_BADVARTYPE;
3523 hRet = DISP_E_TYPEMISMATCH;
3526 V_VT(pVarOut) = VT_EMPTY;
3531 /**********************************************************************
3532 * VarInt [OLEAUT32.172]
3534 * Truncate a variants value to a whole number.
3537 * pVarIn [I] Source variant
3538 * pVarOut [O] Destination for converted value
3541 * Success: S_OK. pVarOut contains the converted value.
3542 * Failure: An HRESULT error code indicating the error.
3545 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3546 * according to the following table:
3547 *| Input Type Output Type
3548 *| ---------- -----------
3552 *| All Others Unchanged
3553 * - The difference between this function and VarFix() is that VarFix() rounds
3554 * negative numbers towards 0, while this function rounds them away from zero.
3556 HRESULT WINAPI VarInt(LPVARIANT pVarIn, LPVARIANT pVarOut)
3558 HRESULT hRet = S_OK;
3560 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
3561 debugstr_VF(pVarIn), pVarOut);
3563 V_VT(pVarOut) = V_VT(pVarIn);
3565 switch (V_VT(pVarIn))
3568 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
3571 V_VT(pVarOut) = VT_R8;
3572 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
3577 V_R8(pVarOut) = floor(V_R8(pVarIn));
3580 hRet = VarCyInt(V_CY(pVarIn), &V_CY(pVarOut));
3583 hRet = VarDecInt(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
3586 return VarFix(pVarIn, pVarOut);
3592 /**********************************************************************
3593 * VarXor [OLEAUT32.167]
3595 * Perform a logical exclusive-or (XOR) operation on two variants.
3598 * pVarLeft [I] First variant
3599 * pVarRight [I] Variant to XOR with pVarLeft
3600 * pVarOut [O] Destination for XOR result
3603 * Success: S_OK. pVarOut contains the result of the operation with its type
3604 * taken from the table below).
3605 * Failure: An HRESULT error code indicating the error.
3608 * - Neither pVarLeft or pVarRight are modified by this function.
3609 * - This function does not process by-reference variants.
3610 * - Input types of VT_BSTR may be numeric strings or boolean text.
3611 * - The type of result stored in pVarOut depends on the types of pVarLeft
3612 * and pVarRight, and will be one of VT_UI1, VT_I2, VT_I4, VT_I8, VT_BOOL,
3613 * or VT_NULL if the function succeeds.
3614 * - Type promotion is inconsistent and as a result certain combinations of
3615 * values will return DISP_E_OVERFLOW even when they could be represented.
3616 * This matches the behaviour of native oleaut32.
3618 HRESULT WINAPI VarXor(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
3621 VARIANT varLeft, varRight;
3625 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
3626 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
3627 debugstr_VF(pVarRight), pVarOut);
3629 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
3630 V_VT(pVarLeft) > VT_UINT || V_VT(pVarRight) > VT_UINT ||
3631 V_VT(pVarLeft) == VT_VARIANT || V_VT(pVarRight) == VT_VARIANT ||
3632 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
3633 V_VT(pVarLeft) == (VARTYPE)15 || V_VT(pVarRight) == (VARTYPE)15 ||
3634 V_VT(pVarLeft) == VT_ERROR || V_VT(pVarRight) == VT_ERROR)
3635 return DISP_E_BADVARTYPE;
3637 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
3639 /* NULL XOR anything valid is NULL */
3640 V_VT(pVarOut) = VT_NULL;
3644 /* Copy our inputs so we don't disturb anything */
3645 V_VT(&varLeft) = V_VT(&varRight) = VT_EMPTY;
3647 hRet = VariantCopy(&varLeft, pVarLeft);
3651 hRet = VariantCopy(&varRight, pVarRight);
3655 /* Try any strings first as numbers, then as VT_BOOL */
3656 if (V_VT(&varLeft) == VT_BSTR)
3658 hRet = VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d);
3659 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL,
3660 FAILED(hRet) ? VT_BOOL : VT_I4);
3665 if (V_VT(&varRight) == VT_BSTR)
3667 hRet = VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d);
3668 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL,
3669 FAILED(hRet) ? VT_BOOL : VT_I4);
3674 /* Determine the result type */
3675 if (V_VT(&varLeft) == VT_I8 || V_VT(&varRight) == VT_I8)
3677 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
3678 return DISP_E_TYPEMISMATCH;
3683 switch ((V_VT(&varLeft) << 16) | V_VT(&varRight))
3685 case (VT_BOOL << 16) | VT_BOOL:
3688 case (VT_UI1 << 16) | VT_UI1:
3691 case (VT_EMPTY << 16) | VT_EMPTY:
3692 case (VT_EMPTY << 16) | VT_UI1:
3693 case (VT_EMPTY << 16) | VT_I2:
3694 case (VT_EMPTY << 16) | VT_BOOL:
3695 case (VT_UI1 << 16) | VT_EMPTY:
3696 case (VT_UI1 << 16) | VT_I2:
3697 case (VT_UI1 << 16) | VT_BOOL:
3698 case (VT_I2 << 16) | VT_EMPTY:
3699 case (VT_I2 << 16) | VT_UI1:
3700 case (VT_I2 << 16) | VT_I2:
3701 case (VT_I2 << 16) | VT_BOOL:
3702 case (VT_BOOL << 16) | VT_EMPTY:
3703 case (VT_BOOL << 16) | VT_UI1:
3704 case (VT_BOOL << 16) | VT_I2:
3713 /* VT_UI4 does not overflow */
3716 if (V_VT(&varLeft) == VT_UI4)
3717 V_VT(&varLeft) = VT_I4;
3718 if (V_VT(&varRight) == VT_UI4)
3719 V_VT(&varRight) = VT_I4;
3722 /* Convert our input copies to the result type */
3723 if (V_VT(&varLeft) != vt)
3724 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
3728 if (V_VT(&varRight) != vt)
3729 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
3735 /* Calculate the result */
3739 V_I8(pVarOut) = V_I8(&varLeft) ^ V_I8(&varRight);
3742 V_I4(pVarOut) = V_I4(&varLeft) ^ V_I4(&varRight);
3746 V_I2(pVarOut) = V_I2(&varLeft) ^ V_I2(&varRight);
3749 V_UI1(pVarOut) = V_UI1(&varLeft) ^ V_UI1(&varRight);
3754 VariantClear(&varLeft);
3755 VariantClear(&varRight);
3759 /**********************************************************************
3760 * VarEqv [OLEAUT32.172]
3762 * Determine if two variants contain the same value.
3765 * pVarLeft [I] First variant to compare
3766 * pVarRight [I] Variant to compare to pVarLeft
3767 * pVarOut [O] Destination for comparison result
3770 * Success: S_OK. pVarOut contains the result of the comparison (VARIANT_TRUE
3771 * if equivalent or non-zero otherwise.
3772 * Failure: An HRESULT error code indicating the error.
3775 * - This function simply calls VarXor() on pVarLeft and pVarRight and inverts
3778 HRESULT WINAPI VarEqv(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
3782 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
3783 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
3784 debugstr_VF(pVarRight), pVarOut);
3786 hRet = VarXor(pVarLeft, pVarRight, pVarOut);
3787 if (SUCCEEDED(hRet))
3789 if (V_VT(pVarOut) == VT_I8)
3790 V_I8(pVarOut) = ~V_I8(pVarOut);
3792 V_UI4(pVarOut) = ~V_UI4(pVarOut);
3797 /**********************************************************************
3798 * VarNeg [OLEAUT32.173]
3800 * Negate the value of a variant.
3803 * pVarIn [I] Source variant
3804 * pVarOut [O] Destination for converted value
3807 * Success: S_OK. pVarOut contains the converted value.
3808 * Failure: An HRESULT error code indicating the error.
3811 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3812 * according to the following table:
3813 *| Input Type Output Type
3814 *| ---------- -----------
3819 *| All Others Unchanged (unless promoted)
3820 * - Where the negated value of a variant does not fit in its base type, the type
3821 * is promoted according to the following table:
3822 *| Input Type Promoted To
3823 *| ---------- -----------
3827 * - The native version of this function returns DISP_E_BADVARTYPE for valid
3828 * variant types that cannot be negated, and returns DISP_E_TYPEMISMATCH
3829 * for types which are not valid. Since this is in contravention of the
3830 * meaning of those error codes and unlikely to be relied on by applications,
3831 * this implementation returns errors consistent with the other high level
3832 * variant math functions.
3834 HRESULT WINAPI VarNeg(LPVARIANT pVarIn, LPVARIANT pVarOut)
3836 HRESULT hRet = S_OK;
3838 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
3839 debugstr_VF(pVarIn), pVarOut);
3841 V_VT(pVarOut) = V_VT(pVarIn);
3843 switch (V_VT(pVarIn))
3846 V_VT(pVarOut) = VT_I2;
3847 V_I2(pVarOut) = -V_UI1(pVarIn);
3850 V_VT(pVarOut) = VT_I2;
3853 if (V_I2(pVarIn) == I2_MIN)
3855 V_VT(pVarOut) = VT_I4;
3856 V_I4(pVarOut) = -(int)V_I2(pVarIn);
3859 V_I2(pVarOut) = -V_I2(pVarIn);
3862 if (V_I4(pVarIn) == I4_MIN)
3864 V_VT(pVarOut) = VT_R8;
3865 V_R8(pVarOut) = -(double)V_I4(pVarIn);
3868 V_I4(pVarOut) = -V_I4(pVarIn);
3871 if (V_I8(pVarIn) == I8_MIN)
3873 V_VT(pVarOut) = VT_R8;
3874 hRet = VarR8FromI8(V_I8(pVarIn), &V_R8(pVarOut));
3875 V_R8(pVarOut) *= -1.0;
3878 V_I8(pVarOut) = -V_I8(pVarIn);
3881 V_R4(pVarOut) = -V_R4(pVarIn);
3885 V_R8(pVarOut) = -V_R8(pVarIn);
3888 hRet = VarCyNeg(V_CY(pVarIn), &V_CY(pVarOut));
3891 hRet = VarDecNeg(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
3894 V_VT(pVarOut) = VT_R8;
3895 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
3896 V_R8(pVarOut) = -V_R8(pVarOut);
3899 V_VT(pVarOut) = VT_I2;
3906 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
3907 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
3908 hRet = DISP_E_BADVARTYPE;
3910 hRet = DISP_E_TYPEMISMATCH;
3913 V_VT(pVarOut) = VT_EMPTY;
3918 /**********************************************************************
3919 * VarNot [OLEAUT32.174]
3921 * Perform a not operation on a variant.
3924 * pVarIn [I] Source variant
3925 * pVarOut [O] Destination for converted value
3928 * Success: S_OK. pVarOut contains the converted value.
3929 * Failure: An HRESULT error code indicating the error.
3932 * - Strictly speaking, this function performs a bitwise ones compliment
3933 * on the variants value (after possibly converting to VT_I4, see below).
3934 * This only behaves like a boolean not operation if the value in
3935 * pVarIn is either VARIANT_TRUE or VARIANT_FALSE and the type is signed.
3936 * - To perform a genuine not operation, convert the variant to a VT_BOOL
3937 * before calling this function.
3938 * - This function does not process by-reference variants.
3939 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3940 * according to the following table:
3941 *| Input Type Output Type
3942 *| ---------- -----------
3949 *| (All others) Unchanged
3951 HRESULT WINAPI VarNot(LPVARIANT pVarIn, LPVARIANT pVarOut)
3954 HRESULT hRet = S_OK;
3956 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
3957 debugstr_VF(pVarIn), pVarOut);
3959 V_VT(pVarOut) = V_VT(pVarIn);
3961 switch (V_VT(pVarIn))
3964 V_I4(pVarOut) = ~V_I1(pVarIn);
3965 V_VT(pVarOut) = VT_I4;
3967 case VT_UI1: V_UI1(pVarOut) = ~V_UI1(pVarIn); break;
3969 case VT_I2: V_I2(pVarOut) = ~V_I2(pVarIn); break;
3971 V_I4(pVarOut) = ~V_UI2(pVarIn);
3972 V_VT(pVarOut) = VT_I4;
3975 hRet = VarI4FromDec(&V_DECIMAL(pVarIn), &V_I4(&varIn));
3979 /* Fall through ... */
3981 V_VT(pVarOut) = VT_I4;
3982 /* Fall through ... */
3983 case VT_I4: V_I4(pVarOut) = ~V_I4(pVarIn); break;
3986 V_I4(pVarOut) = ~V_UI4(pVarIn);
3987 V_VT(pVarOut) = VT_I4;
3989 case VT_I8: V_I8(pVarOut) = ~V_I8(pVarIn); break;
3991 V_I4(pVarOut) = ~V_UI8(pVarIn);
3992 V_VT(pVarOut) = VT_I4;
3995 hRet = VarI4FromR4(V_R4(pVarIn), &V_I4(pVarOut));
3996 V_I4(pVarOut) = ~V_I4(pVarOut);
3997 V_VT(pVarOut) = VT_I4;
4000 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
4004 /* Fall through ... */
4007 hRet = VarI4FromR8(V_R8(pVarIn), &V_I4(pVarOut));
4008 V_I4(pVarOut) = ~V_I4(pVarOut);
4009 V_VT(pVarOut) = VT_I4;
4012 hRet = VarI4FromCy(V_CY(pVarIn), &V_I4(pVarOut));
4013 V_I4(pVarOut) = ~V_I4(pVarOut);
4014 V_VT(pVarOut) = VT_I4;
4018 V_VT(pVarOut) = VT_I2;
4024 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4025 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4026 hRet = DISP_E_BADVARTYPE;
4028 hRet = DISP_E_TYPEMISMATCH;
4031 V_VT(pVarOut) = VT_EMPTY;
4036 /**********************************************************************
4037 * VarRound [OLEAUT32.175]
4039 * Perform a round operation on a variant.
4042 * pVarIn [I] Source variant
4043 * deci [I] Number of decimals to round to
4044 * pVarOut [O] Destination for converted value
4047 * Success: S_OK. pVarOut contains the converted value.
4048 * Failure: An HRESULT error code indicating the error.
4051 * - Floating point values are rounded to the desired number of decimals.
4052 * - Some integer types are just copied to the return variable.
4053 * - Some other integer types are not handled and fail.
4055 HRESULT WINAPI VarRound(LPVARIANT pVarIn, int deci, LPVARIANT pVarOut)
4058 HRESULT hRet = S_OK;
4061 TRACE("(%p->(%s%s),%d)\n", pVarIn, debugstr_VT(pVarIn), debugstr_VF(pVarIn), deci);
4063 switch (V_VT(pVarIn))
4065 /* cases that fail on windows */
4070 hRet = DISP_E_BADVARTYPE;
4073 /* cases just copying in to out */
4075 V_VT(pVarOut) = V_VT(pVarIn);
4076 V_UI1(pVarOut) = V_UI1(pVarIn);
4079 V_VT(pVarOut) = V_VT(pVarIn);
4080 V_I2(pVarOut) = V_I2(pVarIn);
4083 V_VT(pVarOut) = V_VT(pVarIn);
4084 V_I4(pVarOut) = V_I4(pVarIn);
4087 V_VT(pVarOut) = V_VT(pVarIn);
4088 /* value unchanged */
4091 /* cases that change type */
4093 V_VT(pVarOut) = VT_I2;
4097 V_VT(pVarOut) = VT_I2;
4098 V_I2(pVarOut) = V_BOOL(pVarIn);
4101 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
4106 /* Fall through ... */
4108 /* cases we need to do math */
4110 if (V_R8(pVarIn)>0) {
4111 V_R8(pVarOut)=floor(V_R8(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
4113 V_R8(pVarOut)=ceil(V_R8(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
4115 V_VT(pVarOut) = V_VT(pVarIn);
4118 if (V_R4(pVarIn)>0) {
4119 V_R4(pVarOut)=floor(V_R4(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
4121 V_R4(pVarOut)=ceil(V_R4(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
4123 V_VT(pVarOut) = V_VT(pVarIn);
4126 if (V_DATE(pVarIn)>0) {
4127 V_DATE(pVarOut)=floor(V_DATE(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
4129 V_DATE(pVarOut)=ceil(V_DATE(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
4131 V_VT(pVarOut) = V_VT(pVarIn);
4137 factor=pow(10, 4-deci);
4139 if (V_CY(pVarIn).int64>0) {
4140 V_CY(pVarOut).int64=floor(V_CY(pVarIn).int64/factor)*factor;
4142 V_CY(pVarOut).int64=ceil(V_CY(pVarIn).int64/factor)*factor;
4144 V_VT(pVarOut) = V_VT(pVarIn);
4147 /* cases we don't know yet */
4149 FIXME("unimplemented part, V_VT(pVarIn) == 0x%X, deci == %d\n",
4150 V_VT(pVarIn) & VT_TYPEMASK, deci);
4151 hRet = DISP_E_BADVARTYPE;
4155 V_VT(pVarOut) = VT_EMPTY;
4157 TRACE("returning 0x%08lx (%s%s),%f\n", hRet, debugstr_VT(pVarOut),
4158 debugstr_VF(pVarOut), (V_VT(pVarOut) == VT_R4) ? V_R4(pVarOut) :
4159 (V_VT(pVarOut) == VT_R8) ? V_R8(pVarOut) : 0);
4165 /**********************************************************************
4166 * VarMod [OLEAUT32.154]
4168 * Perform the modulus operation of the right hand variant on the left
4171 * left [I] Left hand variant
4172 * right [I] Right hand variant
4173 * result [O] Destination for converted value
4176 * Success: S_OK. result contains the remainder.
4177 * Failure: An HRESULT error code indicating the error.
4180 * If an error occurs the type of result will be modified but the value will not be.
4181 * Doesn't support arrays or any special flags yet.
4183 HRESULT WINAPI VarMod(LPVARIANT left, LPVARIANT right, LPVARIANT result)
4187 HRESULT rc = E_FAIL;
4194 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
4195 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
4197 /* check for invalid inputs */
4199 switch (V_VT(left) & VT_TYPEMASK) {
4220 V_VT(result) = VT_EMPTY;
4221 return DISP_E_TYPEMISMATCH;
4223 V_VT(result) = VT_EMPTY;
4224 return E_INVALIDARG;
4226 return DISP_E_TYPEMISMATCH;
4228 V_VT(result) = VT_EMPTY;
4229 return DISP_E_TYPEMISMATCH;
4233 V_VT(result) = VT_EMPTY;
4234 return DISP_E_BADVARTYPE;
4239 switch (V_VT(right) & VT_TYPEMASK) {
4245 if((V_VT(left) == VT_INT) && (V_VT(right) == VT_I8))
4247 V_VT(result) = VT_EMPTY;
4248 return DISP_E_TYPEMISMATCH;
4251 if((V_VT(right) == VT_INT) && (V_VT(left) == VT_I8))
4253 V_VT(result) = VT_EMPTY;
4254 return DISP_E_TYPEMISMATCH;
4264 if(V_VT(left) == VT_EMPTY)
4266 V_VT(result) = VT_I4;
4272 if(V_VT(left) == VT_NULL)
4274 V_VT(result) = VT_NULL;
4280 V_VT(result) = VT_EMPTY;
4281 return DISP_E_BADVARTYPE;
4283 if(V_VT(left) == VT_VOID)
4285 V_VT(result) = VT_EMPTY;
4286 return DISP_E_BADVARTYPE;
4287 } else if((V_VT(left) == VT_NULL) || (V_VT(left) == VT_EMPTY) || (V_VT(left) == VT_ERROR) ||
4290 V_VT(result) = VT_NULL;
4294 V_VT(result) = VT_NULL;
4295 return DISP_E_BADVARTYPE;
4299 V_VT(result) = VT_EMPTY;
4300 return DISP_E_TYPEMISMATCH;
4302 if(V_VT(left) == VT_ERROR)
4304 V_VT(result) = VT_EMPTY;
4305 return DISP_E_TYPEMISMATCH;
4308 V_VT(result) = VT_EMPTY;
4309 return E_INVALIDARG;
4312 return DISP_E_TYPEMISMATCH;
4314 if((V_VT(left) == 15) || ((V_VT(left) >= 24) && (V_VT(left) <= 35)) || !lOk)
4316 V_VT(result) = VT_EMPTY;
4317 return DISP_E_BADVARTYPE;
4320 V_VT(result) = VT_EMPTY;
4321 return DISP_E_TYPEMISMATCH;
4324 V_VT(result) = VT_EMPTY;
4325 return DISP_E_BADVARTYPE;
4328 /* determine the result type */
4329 if((V_VT(left) == VT_I8) || (V_VT(right) == VT_I8)) resT = VT_I8;
4330 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
4331 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_UI1)) resT = VT_UI1;
4332 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_I2)) resT = VT_I2;
4333 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
4334 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_UI1)) resT = VT_I2;
4335 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_I2)) resT = VT_I2;
4336 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
4337 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_UI1)) resT = VT_I2;
4338 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_I2)) resT = VT_I2;
4339 else resT = VT_I4; /* most outputs are I4 */
4341 /* convert to I8 for the modulo */
4342 rc = VariantChangeType(&lv, left, 0, VT_I8);
4345 FIXME("Could not convert left type %d to %d? rc == 0x%lX\n", V_VT(left), VT_I8, rc);
4349 rc = VariantChangeType(&rv, right, 0, VT_I8);
4352 FIXME("Could not convert right type %d to %d? rc == 0x%lX\n", V_VT(right), VT_I8, rc);
4356 /* if right is zero set VT_EMPTY and return divide by zero */
4359 V_VT(result) = VT_EMPTY;
4360 return DISP_E_DIVBYZERO;
4363 /* perform the modulo operation */
4364 V_VT(result) = VT_I8;
4365 V_I8(result) = V_I8(&lv) % V_I8(&rv);
4367 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));
4369 /* convert left and right to the destination type */
4370 rc = VariantChangeType(result, result, 0, resT);
4373 FIXME("Could not convert 0x%x to %d?\n", V_VT(result), resT);
4380 /**********************************************************************
4381 * VarPow [OLEAUT32.158]
4384 HRESULT WINAPI VarPow(LPVARIANT left, LPVARIANT right, LPVARIANT result)
4389 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left), debugstr_VF(left),
4390 right, debugstr_VT(right), debugstr_VF(right), result);
4392 hr = VariantChangeType(&dl,left,0,VT_R8);
4393 if (!SUCCEEDED(hr)) {
4394 ERR("Could not change passed left argument to VT_R8, handle it differently.\n");
4397 hr = VariantChangeType(&dr,right,0,VT_R8);
4398 if (!SUCCEEDED(hr)) {
4399 ERR("Could not change passed right argument to VT_R8, handle it differently.\n");
4402 V_VT(result) = VT_R8;
4403 V_R8(result) = pow(V_R8(&dl),V_R8(&dr));