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
36 #define NONAMELESSUNION
37 #define NONAMELESSSTRUCT
41 #include "wine/debug.h"
42 #include "wine/unicode.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(ole);
48 const char* wine_vtypes[VT_CLSID] =
50 "VT_EMPTY","VT_NULL","VT_I2","VT_I4","VT_R4","VT_R8","VT_CY","VT_DATE",
51 "VT_BSTR","VT_DISPATCH","VT_ERROR","VT_BOOL","VT_VARIANT","VT_UNKNOWN",
52 "VT_DECIMAL","15","VT_I1","VT_UI1","VT_UI2","VT_UI4","VT_I8","VT_UI8",
53 "VT_INT","VT_UINT","VT_VOID","VT_HRESULT","VT_PTR","VT_SAFEARRAY",
54 "VT_CARRAY","VT_USERDEFINED","VT_LPSTR","VT_LPWSTR""32","33","34","35",
55 "VT_RECORD","VT_INT_PTR","VT_UINT_PTR","39","40","41","42","43","44","45",
56 "46","47","48","49","50","51","52","53","54","55","56","57","58","59","60",
57 "61","62","63","VT_FILETIME","VT_BLOB","VT_STREAM","VT_STORAGE",
58 "VT_STREAMED_OBJECT","VT_STORED_OBJECT","VT_BLOB_OBJECT","VT_CF","VT_CLSID"
61 const char* wine_vflags[16] =
66 "|VT_VECTOR|VT_ARRAY",
68 "|VT_VECTOR|VT_ARRAY",
70 "|VT_VECTOR|VT_ARRAY|VT_BYREF",
72 "|VT_VECTOR|VT_HARDTYPE",
73 "|VT_ARRAY|VT_HARDTYPE",
74 "|VT_VECTOR|VT_ARRAY|VT_HARDTYPE",
75 "|VT_BYREF|VT_HARDTYPE",
76 "|VT_VECTOR|VT_ARRAY|VT_HARDTYPE",
77 "|VT_ARRAY|VT_BYREF|VT_HARDTYPE",
78 "|VT_VECTOR|VT_ARRAY|VT_BYREF|VT_HARDTYPE",
81 /* Convert a variant from one type to another */
82 static inline HRESULT VARIANT_Coerce(VARIANTARG* pd, LCID lcid, USHORT wFlags,
83 VARIANTARG* ps, VARTYPE vt)
85 HRESULT res = DISP_E_TYPEMISMATCH;
86 VARTYPE vtFrom = V_TYPE(ps);
89 TRACE("(%p->(%s%s),0x%08lx,0x%04x,%p->(%s%s),%s%s)\n", pd, debugstr_VT(pd),
90 debugstr_VF(pd), lcid, wFlags, ps, debugstr_VT(ps), debugstr_VF(ps),
91 debugstr_vt(vt), debugstr_vf(vt));
93 if (vt == VT_BSTR || vtFrom == VT_BSTR)
95 /* All flags passed to low level function are only used for
96 * changing to or from strings. Map these here.
98 if (wFlags & VARIANT_LOCALBOOL)
99 dwFlags |= VAR_LOCALBOOL;
100 if (wFlags & VARIANT_CALENDAR_HIJRI)
101 dwFlags |= VAR_CALENDAR_HIJRI;
102 if (wFlags & VARIANT_CALENDAR_THAI)
103 dwFlags |= VAR_CALENDAR_THAI;
104 if (wFlags & VARIANT_CALENDAR_GREGORIAN)
105 dwFlags |= VAR_CALENDAR_GREGORIAN;
106 if (wFlags & VARIANT_NOUSEROVERRIDE)
107 dwFlags |= LOCALE_NOUSEROVERRIDE;
108 if (wFlags & VARIANT_USE_NLS)
109 dwFlags |= LOCALE_USE_NLS;
112 /* Map int/uint to i4/ui4 */
115 else if (vt == VT_UINT)
118 if (vtFrom == VT_INT)
120 else if (vtFrom == VT_UINT)
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));
201 case VT_UI4: return VarI4FromUI4(V_UI4(ps), &V_I4(pd));
202 case VT_I8: return VarI4FromI8(V_I8(ps), &V_I4(pd));
203 case VT_UI8: return VarI4FromUI8(V_UI8(ps), &V_I4(pd));
204 case VT_R4: return VarI4FromR4(V_R4(ps), &V_I4(pd));
205 case VT_R8: return VarI4FromR8(V_R8(ps), &V_I4(pd));
206 case VT_DATE: return VarI4FromDate(V_DATE(ps), &V_I4(pd));
207 case VT_BOOL: return VarI4FromBool(V_BOOL(ps), &V_I4(pd));
208 case VT_CY: return VarI4FromCy(V_CY(ps), &V_I4(pd));
209 case VT_DECIMAL: return VarI4FromDec(&V_DECIMAL(ps), &V_I4(pd));
210 case VT_DISPATCH: return VarI4FromDisp(V_DISPATCH(ps), lcid, &V_I4(pd));
211 case VT_BSTR: return VarI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_I4(pd));
218 case VT_EMPTY: V_UI1(pd) = 0; return S_OK;
219 case VT_I1: return VarUI1FromI1(V_I1(ps), &V_UI1(pd));
220 case VT_I2: return VarUI1FromI2(V_I2(ps), &V_UI1(pd));
221 case VT_I4: return VarUI1FromI4(V_I4(ps), &V_UI1(pd));
222 case VT_UI2: return VarUI1FromUI2(V_UI2(ps), &V_UI1(pd));
223 case VT_UI4: return VarUI1FromUI4(V_UI4(ps), &V_UI1(pd));
224 case VT_I8: return VarUI1FromI8(V_I8(ps), &V_UI1(pd));
225 case VT_UI8: return VarUI1FromUI8(V_UI8(ps), &V_UI1(pd));
226 case VT_R4: return VarUI1FromR4(V_R4(ps), &V_UI1(pd));
227 case VT_R8: return VarUI1FromR8(V_R8(ps), &V_UI1(pd));
228 case VT_DATE: return VarUI1FromDate(V_DATE(ps), &V_UI1(pd));
229 case VT_BOOL: return VarUI1FromBool(V_BOOL(ps), &V_UI1(pd));
230 case VT_CY: return VarUI1FromCy(V_CY(ps), &V_UI1(pd));
231 case VT_DECIMAL: return VarUI1FromDec(&V_DECIMAL(ps), &V_UI1(pd));
232 case VT_DISPATCH: return VarUI1FromDisp(V_DISPATCH(ps), lcid, &V_UI1(pd));
233 case VT_BSTR: return VarUI1FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI1(pd));
240 case VT_EMPTY: V_UI2(pd) = 0; return S_OK;
241 case VT_I1: return VarUI2FromI1(V_I1(ps), &V_UI2(pd));
242 case VT_I2: return VarUI2FromI2(V_I2(ps), &V_UI2(pd));
243 case VT_I4: return VarUI2FromI4(V_I4(ps), &V_UI2(pd));
244 case VT_UI1: return VarUI2FromUI1(V_UI1(ps), &V_UI2(pd));
245 case VT_UI4: return VarUI2FromUI4(V_UI4(ps), &V_UI2(pd));
246 case VT_I8: return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
247 case VT_UI8: return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
248 case VT_R4: return VarUI2FromR4(V_R4(ps), &V_UI2(pd));
249 case VT_R8: return VarUI2FromR8(V_R8(ps), &V_UI2(pd));
250 case VT_DATE: return VarUI2FromDate(V_DATE(ps), &V_UI2(pd));
251 case VT_BOOL: return VarUI2FromBool(V_BOOL(ps), &V_UI2(pd));
252 case VT_CY: return VarUI2FromCy(V_CY(ps), &V_UI2(pd));
253 case VT_DECIMAL: return VarUI2FromDec(&V_DECIMAL(ps), &V_UI2(pd));
254 case VT_DISPATCH: return VarUI2FromDisp(V_DISPATCH(ps), lcid, &V_UI2(pd));
255 case VT_BSTR: return VarUI2FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI2(pd));
262 case VT_EMPTY: V_UI4(pd) = 0; return S_OK;
263 case VT_I1: return VarUI4FromI1(V_I1(ps), &V_UI4(pd));
264 case VT_I2: return VarUI4FromI2(V_I2(ps), &V_UI4(pd));
265 case VT_I4: return VarUI4FromI4(V_I4(ps), &V_UI4(pd));
266 case VT_UI1: return VarUI4FromUI1(V_UI1(ps), &V_UI4(pd));
267 case VT_UI2: return VarUI4FromUI2(V_UI2(ps), &V_UI4(pd));
268 case VT_I8: return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
269 case VT_UI8: return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
270 case VT_R4: return VarUI4FromR4(V_R4(ps), &V_UI4(pd));
271 case VT_R8: return VarUI4FromR8(V_R8(ps), &V_UI4(pd));
272 case VT_DATE: return VarUI4FromDate(V_DATE(ps), &V_UI4(pd));
273 case VT_BOOL: return VarUI4FromBool(V_BOOL(ps), &V_UI4(pd));
274 case VT_CY: return VarUI4FromCy(V_CY(ps), &V_UI4(pd));
275 case VT_DECIMAL: return VarUI4FromDec(&V_DECIMAL(ps), &V_UI4(pd));
276 case VT_DISPATCH: return VarUI4FromDisp(V_DISPATCH(ps), lcid, &V_UI4(pd));
277 case VT_BSTR: return VarUI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI4(pd));
284 case VT_EMPTY: V_UI8(pd) = 0; return S_OK;
285 case VT_I4: if (V_I4(ps) < 0) return DISP_E_OVERFLOW; V_UI8(pd) = V_I4(ps); return S_OK;
286 case VT_I1: return VarUI8FromI1(V_I1(ps), &V_UI8(pd));
287 case VT_I2: return VarUI8FromI2(V_I2(ps), &V_UI8(pd));
288 case VT_UI1: return VarUI8FromUI1(V_UI1(ps), &V_UI8(pd));
289 case VT_UI2: return VarUI8FromUI2(V_UI2(ps), &V_UI8(pd));
290 case VT_UI4: return VarUI8FromUI4(V_UI4(ps), &V_UI8(pd));
291 case VT_I8: return VarUI8FromI8(V_I8(ps), &V_UI8(pd));
292 case VT_R4: return VarUI8FromR4(V_R4(ps), &V_UI8(pd));
293 case VT_R8: return VarUI8FromR8(V_R8(ps), &V_UI8(pd));
294 case VT_DATE: return VarUI8FromDate(V_DATE(ps), &V_UI8(pd));
295 case VT_BOOL: return VarUI8FromBool(V_BOOL(ps), &V_UI8(pd));
296 case VT_CY: return VarUI8FromCy(V_CY(ps), &V_UI8(pd));
297 case VT_DECIMAL: return VarUI8FromDec(&V_DECIMAL(ps), &V_UI8(pd));
298 case VT_DISPATCH: return VarUI8FromDisp(V_DISPATCH(ps), lcid, &V_UI8(pd));
299 case VT_BSTR: return VarUI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI8(pd));
306 case VT_EMPTY: V_I8(pd) = 0; return S_OK;
307 case VT_I4: V_I8(pd) = V_I4(ps); return S_OK;
308 case VT_I1: return VarI8FromI1(V_I1(ps), &V_I8(pd));
309 case VT_I2: return VarI8FromI2(V_I2(ps), &V_I8(pd));
310 case VT_UI1: return VarI8FromUI1(V_UI1(ps), &V_I8(pd));
311 case VT_UI2: return VarI8FromUI2(V_UI2(ps), &V_I8(pd));
312 case VT_UI4: return VarI8FromUI4(V_UI4(ps), &V_I8(pd));
313 case VT_UI8: return VarI8FromUI8(V_I8(ps), &V_I8(pd));
314 case VT_R4: return VarI8FromR4(V_R4(ps), &V_I8(pd));
315 case VT_R8: return VarI8FromR8(V_R8(ps), &V_I8(pd));
316 case VT_DATE: return VarI8FromDate(V_DATE(ps), &V_I8(pd));
317 case VT_BOOL: return VarI8FromBool(V_BOOL(ps), &V_I8(pd));
318 case VT_CY: return VarI8FromCy(V_CY(ps), &V_I8(pd));
319 case VT_DECIMAL: return VarI8FromDec(&V_DECIMAL(ps), &V_I8(pd));
320 case VT_DISPATCH: return VarI8FromDisp(V_DISPATCH(ps), lcid, &V_I8(pd));
321 case VT_BSTR: return VarI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_I8(pd));
328 case VT_EMPTY: V_R4(pd) = 0.0f; return S_OK;
329 case VT_I1: return VarR4FromI1(V_I1(ps), &V_R4(pd));
330 case VT_I2: return VarR4FromI2(V_I2(ps), &V_R4(pd));
331 case VT_I4: return VarR4FromI4(V_I4(ps), &V_R4(pd));
332 case VT_UI1: return VarR4FromUI1(V_UI1(ps), &V_R4(pd));
333 case VT_UI2: return VarR4FromUI2(V_UI2(ps), &V_R4(pd));
334 case VT_UI4: return VarR4FromUI4(V_UI4(ps), &V_R4(pd));
335 case VT_I8: return VarR4FromI8(V_I8(ps), &V_R4(pd));
336 case VT_UI8: return VarR4FromUI8(V_UI8(ps), &V_R4(pd));
337 case VT_R8: return VarR4FromR8(V_R8(ps), &V_R4(pd));
338 case VT_DATE: return VarR4FromDate(V_DATE(ps), &V_R4(pd));
339 case VT_BOOL: return VarR4FromBool(V_BOOL(ps), &V_R4(pd));
340 case VT_CY: return VarR4FromCy(V_CY(ps), &V_R4(pd));
341 case VT_DECIMAL: return VarR4FromDec(&V_DECIMAL(ps), &V_R4(pd));
342 case VT_DISPATCH: return VarR4FromDisp(V_DISPATCH(ps), lcid, &V_R4(pd));
343 case VT_BSTR: return VarR4FromStr(V_BSTR(ps), lcid, dwFlags, &V_R4(pd));
350 case VT_EMPTY: V_R8(pd) = 0.0; return S_OK;
351 case VT_I1: return VarR8FromI1(V_I1(ps), &V_R8(pd));
352 case VT_I2: return VarR8FromI2(V_I2(ps), &V_R8(pd));
353 case VT_I4: return VarR8FromI4(V_I4(ps), &V_R8(pd));
354 case VT_UI1: return VarR8FromUI1(V_UI1(ps), &V_R8(pd));
355 case VT_UI2: return VarR8FromUI2(V_UI2(ps), &V_R8(pd));
356 case VT_UI4: return VarR8FromUI4(V_UI4(ps), &V_R8(pd));
357 case VT_I8: return VarR8FromI8(V_I8(ps), &V_R8(pd));
358 case VT_UI8: return VarR8FromUI8(V_UI8(ps), &V_R8(pd));
359 case VT_R4: return VarR8FromR4(V_R4(ps), &V_R8(pd));
360 case VT_DATE: return VarR8FromDate(V_DATE(ps), &V_R8(pd));
361 case VT_BOOL: return VarR8FromBool(V_BOOL(ps), &V_R8(pd));
362 case VT_CY: return VarR8FromCy(V_CY(ps), &V_R8(pd));
363 case VT_DECIMAL: return VarR8FromDec(&V_DECIMAL(ps), &V_R8(pd));
364 case VT_DISPATCH: return VarR8FromDisp(V_DISPATCH(ps), lcid, &V_R8(pd));
365 case VT_BSTR: return VarR8FromStr(V_BSTR(ps), lcid, dwFlags, &V_R8(pd));
372 case VT_EMPTY: V_DATE(pd) = 0.0; return S_OK;
373 case VT_I1: return VarDateFromI1(V_I1(ps), &V_DATE(pd));
374 case VT_I2: return VarDateFromI2(V_I2(ps), &V_DATE(pd));
375 case VT_I4: return VarDateFromI4(V_I4(ps), &V_DATE(pd));
376 case VT_UI1: return VarDateFromUI1(V_UI1(ps), &V_DATE(pd));
377 case VT_UI2: return VarDateFromUI2(V_UI2(ps), &V_DATE(pd));
378 case VT_UI4: return VarDateFromUI4(V_UI4(ps), &V_DATE(pd));
379 case VT_I8: return VarDateFromI8(V_I8(ps), &V_DATE(pd));
380 case VT_UI8: return VarDateFromUI8(V_UI8(ps), &V_DATE(pd));
381 case VT_R4: return VarDateFromR4(V_R4(ps), &V_DATE(pd));
382 case VT_R8: return VarDateFromR8(V_R8(ps), &V_DATE(pd));
383 case VT_BOOL: return VarDateFromBool(V_BOOL(ps), &V_DATE(pd));
384 case VT_CY: return VarDateFromCy(V_CY(ps), &V_DATE(pd));
385 case VT_DECIMAL: return VarDateFromDec(&V_DECIMAL(ps), &V_DATE(pd));
386 case VT_DISPATCH: return VarDateFromDisp(V_DISPATCH(ps), lcid, &V_DATE(pd));
387 case VT_BSTR: return VarDateFromStr(V_BSTR(ps), lcid, dwFlags, &V_DATE(pd));
394 case VT_EMPTY: V_BOOL(pd) = 0; return S_OK;
395 case VT_I1: return VarBoolFromI1(V_I1(ps), &V_BOOL(pd));
396 case VT_I2: return VarBoolFromI2(V_I2(ps), &V_BOOL(pd));
397 case VT_I4: return VarBoolFromI4(V_I4(ps), &V_BOOL(pd));
398 case VT_UI1: return VarBoolFromUI1(V_UI1(ps), &V_BOOL(pd));
399 case VT_UI2: return VarBoolFromUI2(V_UI2(ps), &V_BOOL(pd));
400 case VT_UI4: return VarBoolFromUI4(V_UI4(ps), &V_BOOL(pd));
401 case VT_I8: return VarBoolFromI8(V_I8(ps), &V_BOOL(pd));
402 case VT_UI8: return VarBoolFromUI8(V_UI8(ps), &V_BOOL(pd));
403 case VT_R4: return VarBoolFromR4(V_R4(ps), &V_BOOL(pd));
404 case VT_R8: return VarBoolFromR8(V_R8(ps), &V_BOOL(pd));
405 case VT_DATE: return VarBoolFromDate(V_DATE(ps), &V_BOOL(pd));
406 case VT_CY: return VarBoolFromCy(V_CY(ps), &V_BOOL(pd));
407 case VT_DECIMAL: return VarBoolFromDec(&V_DECIMAL(ps), &V_BOOL(pd));
408 case VT_DISPATCH: return VarBoolFromDisp(V_DISPATCH(ps), lcid, &V_BOOL(pd));
409 case VT_BSTR: return VarBoolFromStr(V_BSTR(ps), lcid, dwFlags, &V_BOOL(pd));
417 V_BSTR(pd) = SysAllocStringLen(NULL, 0);
418 return V_BSTR(pd) ? S_OK : E_OUTOFMEMORY;
420 if (wFlags & (VARIANT_ALPHABOOL|VARIANT_LOCALBOOL))
421 return VarBstrFromBool(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
422 return VarBstrFromI2(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
423 case VT_I1: return VarBstrFromI1(V_I1(ps), lcid, dwFlags, &V_BSTR(pd));
424 case VT_I2: return VarBstrFromI2(V_I2(ps), lcid, dwFlags, &V_BSTR(pd));
425 case VT_I4: return VarBstrFromI4(V_I4(ps), lcid, dwFlags, &V_BSTR(pd));
426 case VT_UI1: return VarBstrFromUI1(V_UI1(ps), lcid, dwFlags, &V_BSTR(pd));
427 case VT_UI2: return VarBstrFromUI2(V_UI2(ps), lcid, dwFlags, &V_BSTR(pd));
428 case VT_UI4: return VarBstrFromUI4(V_UI4(ps), lcid, dwFlags, &V_BSTR(pd));
429 case VT_I8: return VarBstrFromI8(V_I8(ps), lcid, dwFlags, &V_BSTR(pd));
430 case VT_UI8: return VarBstrFromUI8(V_UI8(ps), lcid, dwFlags, &V_BSTR(pd));
431 case VT_R4: return VarBstrFromR4(V_R4(ps), lcid, dwFlags, &V_BSTR(pd));
432 case VT_R8: return VarBstrFromR8(V_R8(ps), lcid, dwFlags, &V_BSTR(pd));
433 case VT_DATE: return VarBstrFromDate(V_DATE(ps), lcid, dwFlags, &V_BSTR(pd));
434 case VT_CY: return VarBstrFromCy(V_CY(ps), lcid, dwFlags, &V_BSTR(pd));
435 case VT_DECIMAL: return VarBstrFromDec(&V_DECIMAL(ps), lcid, dwFlags, &V_BSTR(pd));
436 /* case VT_DISPATCH: return VarBstrFromDisp(V_DISPATCH(ps), lcid, dwFlags, &V_BSTR(pd)); */
443 case VT_EMPTY: V_CY(pd).int64 = 0; return S_OK;
444 case VT_I1: return VarCyFromI1(V_I1(ps), &V_CY(pd));
445 case VT_I2: return VarCyFromI2(V_I2(ps), &V_CY(pd));
446 case VT_I4: return VarCyFromI4(V_I4(ps), &V_CY(pd));
447 case VT_UI1: return VarCyFromUI1(V_UI1(ps), &V_CY(pd));
448 case VT_UI2: return VarCyFromUI2(V_UI2(ps), &V_CY(pd));
449 case VT_UI4: return VarCyFromUI4(V_UI4(ps), &V_CY(pd));
450 case VT_I8: return VarCyFromI8(V_I8(ps), &V_CY(pd));
451 case VT_UI8: return VarCyFromUI8(V_UI8(ps), &V_CY(pd));
452 case VT_R4: return VarCyFromR4(V_R4(ps), &V_CY(pd));
453 case VT_R8: return VarCyFromR8(V_R8(ps), &V_CY(pd));
454 case VT_DATE: return VarCyFromDate(V_DATE(ps), &V_CY(pd));
455 case VT_BOOL: return VarCyFromBool(V_BOOL(ps), &V_CY(pd));
456 case VT_DECIMAL: return VarCyFromDec(&V_DECIMAL(ps), &V_CY(pd));
457 case VT_DISPATCH: return VarCyFromDisp(V_DISPATCH(ps), lcid, &V_CY(pd));
458 case VT_BSTR: return VarCyFromStr(V_BSTR(ps), lcid, dwFlags, &V_CY(pd));
467 DEC_SIGNSCALE(&V_DECIMAL(pd)) = SIGNSCALE(DECIMAL_POS,0);
468 DEC_HI32(&V_DECIMAL(pd)) = 0;
469 DEC_MID32(&V_DECIMAL(pd)) = 0;
470 /* VarDecFromBool() coerces to -1/0, ChangeTypeEx() coerces to 1/0.
471 * VT_NULL and VT_EMPTY always give a 0 value.
473 DEC_LO32(&V_DECIMAL(pd)) = vtFrom == VT_BOOL && V_BOOL(ps) ? 1 : 0;
475 case VT_I1: return VarDecFromI1(V_I1(ps), &V_DECIMAL(pd));
476 case VT_I2: return VarDecFromI2(V_I2(ps), &V_DECIMAL(pd));
477 case VT_I4: return VarDecFromI4(V_I4(ps), &V_DECIMAL(pd));
478 case VT_UI1: return VarDecFromUI1(V_UI1(ps), &V_DECIMAL(pd));
479 case VT_UI2: return VarDecFromUI2(V_UI2(ps), &V_DECIMAL(pd));
480 case VT_UI4: return VarDecFromUI4(V_UI4(ps), &V_DECIMAL(pd));
481 case VT_I8: return VarDecFromI8(V_I8(ps), &V_DECIMAL(pd));
482 case VT_UI8: return VarDecFromUI8(V_UI8(ps), &V_DECIMAL(pd));
483 case VT_R4: return VarDecFromR4(V_R4(ps), &V_DECIMAL(pd));
484 case VT_R8: return VarDecFromR8(V_R8(ps), &V_DECIMAL(pd));
485 case VT_DATE: return VarDecFromDate(V_DATE(ps), &V_DECIMAL(pd));
486 case VT_CY: return VarDecFromCy(V_CY(pd), &V_DECIMAL(ps));
487 case VT_DISPATCH: return VarDecFromDisp(V_DISPATCH(ps), lcid, &V_DECIMAL(ps));
488 case VT_BSTR: return VarDecFromStr(V_BSTR(ps), lcid, dwFlags, &V_DECIMAL(pd));
496 if (V_DISPATCH(ps) == NULL)
497 V_UNKNOWN(pd) = NULL;
499 res = IDispatch_QueryInterface(V_DISPATCH(ps), &IID_IUnknown, (LPVOID*)&V_UNKNOWN(pd));
508 if (V_UNKNOWN(ps) == NULL)
509 V_DISPATCH(pd) = NULL;
511 res = IUnknown_QueryInterface(V_UNKNOWN(ps), &IID_IDispatch, (LPVOID*)&V_DISPATCH(pd));
522 /* Coerce to/from an array */
523 static inline HRESULT VARIANT_CoerceArray(VARIANTARG* pd, VARIANTARG* ps, VARTYPE vt)
525 if (vt == VT_BSTR && V_VT(ps) == (VT_ARRAY|VT_UI1))
526 return BstrFromVector(V_ARRAY(ps), &V_BSTR(pd));
528 if (V_VT(ps) == VT_BSTR && vt == (VT_ARRAY|VT_UI1))
529 return VectorFromBstr(V_BSTR(ps), &V_ARRAY(ps));
532 return SafeArrayCopy(V_ARRAY(ps), &V_ARRAY(pd));
534 return DISP_E_TYPEMISMATCH;
537 /******************************************************************************
538 * Check if a variants type is valid.
540 static inline HRESULT VARIANT_ValidateType(VARTYPE vt)
542 VARTYPE vtExtra = vt & VT_EXTRA_TYPE;
546 if (!(vtExtra & (VT_VECTOR|VT_RESERVED)))
548 if (vt < VT_VOID || vt == VT_RECORD || vt == VT_CLSID)
550 if ((vtExtra & (VT_BYREF|VT_ARRAY)) && vt <= VT_NULL)
551 return DISP_E_BADVARTYPE;
552 if (vt != (VARTYPE)15)
556 return DISP_E_BADVARTYPE;
559 /******************************************************************************
560 * VariantInit [OLEAUT32.8]
562 * Initialise a variant.
565 * pVarg [O] Variant to initialise
571 * This function simply sets the type of the variant to VT_EMPTY. It does not
572 * free any existing value, use VariantClear() for that.
574 void WINAPI VariantInit(VARIANTARG* pVarg)
576 TRACE("(%p)\n", pVarg);
578 V_VT(pVarg) = VT_EMPTY; /* Native doesn't set any other fields */
581 /******************************************************************************
582 * VariantClear [OLEAUT32.9]
587 * pVarg [I/O] Variant to clear
590 * Success: S_OK. Any previous value in pVarg is freed and its type is set to VT_EMPTY.
591 * Failure: DISP_E_BADVARTYPE, if the variant is a not a valid variant type.
593 HRESULT WINAPI VariantClear(VARIANTARG* pVarg)
597 TRACE("(%p->(%s%s))\n", pVarg, debugstr_VT(pVarg), debugstr_VF(pVarg));
599 hres = VARIANT_ValidateType(V_VT(pVarg));
603 if (!V_ISBYREF(pVarg))
605 if (V_ISARRAY(pVarg) || V_VT(pVarg) == VT_SAFEARRAY)
608 hres = SafeArrayDestroy(V_ARRAY(pVarg));
610 else if (V_VT(pVarg) == VT_BSTR)
613 SysFreeString(V_BSTR(pVarg));
615 else if (V_VT(pVarg) == VT_RECORD)
617 struct __tagBRECORD* pBr = &V_UNION(pVarg,brecVal);
620 IRecordInfo_RecordClear(pBr->pRecInfo, pBr->pvRecord);
621 IRecordInfo_Release(pBr->pRecInfo);
624 else if (V_VT(pVarg) == VT_DISPATCH ||
625 V_VT(pVarg) == VT_UNKNOWN)
627 if (V_UNKNOWN(pVarg))
628 IUnknown_Release(V_UNKNOWN(pVarg));
630 else if (V_VT(pVarg) == VT_VARIANT)
632 if (V_VARIANTREF(pVarg))
633 VariantClear(V_VARIANTREF(pVarg));
636 V_VT(pVarg) = VT_EMPTY;
641 /******************************************************************************
642 * Copy an IRecordInfo object contained in a variant.
644 static HRESULT VARIANT_CopyIRecordInfo(struct __tagBRECORD* pBr)
652 hres = IRecordInfo_GetSize(pBr->pRecInfo, &ulSize);
655 PVOID pvRecord = HeapAlloc(GetProcessHeap(), 0, ulSize);
657 hres = E_OUTOFMEMORY;
660 memcpy(pvRecord, pBr->pvRecord, ulSize);
661 pBr->pvRecord = pvRecord;
663 hres = IRecordInfo_RecordCopy(pBr->pRecInfo, pvRecord, pvRecord);
665 IRecordInfo_AddRef(pBr->pRecInfo);
669 else if (pBr->pvRecord)
674 /******************************************************************************
675 * VariantCopy [OLEAUT32.10]
680 * pvargDest [O] Destination for copy
681 * pvargSrc [I] Source variant to copy
684 * Success: S_OK. pvargDest contains a copy of pvargSrc.
685 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid type.
686 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
687 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
688 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
691 * - If pvargSrc == pvargDest, this function does nothing, and succeeds if
692 * pvargSrc is valid. Otherwise, pvargDest is always cleared using
693 * VariantClear() before pvargSrc is copied to it. If clearing pvargDest
694 * fails, so does this function.
695 * - VT_CLSID is a valid type type for pvargSrc, but not for pvargDest.
696 * - For by-value non-intrinsic types, a deep copy is made, i.e. The whole value
697 * is copied rather than just any pointers to it.
698 * - For by-value object types the object pointer is copied and the objects
699 * reference count increased using IUnknown_AddRef().
700 * - For all by-reference types, only the referencing pointer is copied.
702 HRESULT WINAPI VariantCopy(VARIANTARG* pvargDest, VARIANTARG* pvargSrc)
706 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
707 debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
708 debugstr_VF(pvargSrc));
710 if (V_TYPE(pvargSrc) == VT_CLSID || /* VT_CLSID is a special case */
711 FAILED(VARIANT_ValidateType(V_VT(pvargSrc))))
712 return DISP_E_BADVARTYPE;
714 if (pvargSrc != pvargDest &&
715 SUCCEEDED(hres = VariantClear(pvargDest)))
717 *pvargDest = *pvargSrc; /* Shallow copy the value */
719 if (!V_ISBYREF(pvargSrc))
721 if (V_ISARRAY(pvargSrc))
723 if (V_ARRAY(pvargSrc))
724 hres = SafeArrayCopy(V_ARRAY(pvargSrc), &V_ARRAY(pvargDest));
726 else if (V_VT(pvargSrc) == VT_BSTR)
728 if (V_BSTR(pvargSrc))
730 V_BSTR(pvargDest) = SysAllocStringLen(V_BSTR(pvargSrc), SysStringLen(V_BSTR(pvargSrc)));
731 if (!V_BSTR(pvargDest))
732 hres = E_OUTOFMEMORY;
735 else if (V_VT(pvargSrc) == VT_RECORD)
737 hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
739 else if (V_VT(pvargSrc) == VT_DISPATCH ||
740 V_VT(pvargSrc) == VT_UNKNOWN)
742 if (V_UNKNOWN(pvargSrc))
743 IUnknown_AddRef(V_UNKNOWN(pvargSrc));
750 /* Return the byte size of a variants data */
751 static inline size_t VARIANT_DataSize(const VARIANT* pv)
756 case VT_UI1: return sizeof(BYTE); break;
758 case VT_UI2: return sizeof(SHORT); break;
762 case VT_UI4: return sizeof(LONG); break;
764 case VT_UI8: return sizeof(LONGLONG); break;
765 case VT_R4: return sizeof(float); break;
766 case VT_R8: return sizeof(double); break;
767 case VT_DATE: return sizeof(DATE); break;
768 case VT_BOOL: return sizeof(VARIANT_BOOL); break;
771 case VT_BSTR: return sizeof(void*); break;
772 case VT_CY: return sizeof(CY); break;
773 case VT_ERROR: return sizeof(SCODE); break;
775 TRACE("Shouldn't be called for vt %s%s!\n", debugstr_VT(pv), debugstr_VF(pv));
779 /******************************************************************************
780 * VariantCopyInd [OLEAUT32.11]
782 * Copy a variant, dereferencing it it is by-reference.
785 * pvargDest [O] Destination for copy
786 * pvargSrc [I] Source variant to copy
789 * Success: S_OK. pvargDest contains a copy of pvargSrc.
790 * Failure: An HRESULT error code indicating the error.
793 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid by-value type.
794 * E_INVALIDARG, if pvargSrc is an invalid by-reference type.
795 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
796 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
797 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
800 * - If pvargSrc is by-value, this function behaves exactly as VariantCopy().
801 * - If pvargSrc is by-reference, the value copied to pvargDest is the pointed-to
803 * - if pvargSrc == pvargDest, this function dereferences in place. Otherwise,
804 * pvargDest is always cleared using VariantClear() before pvargSrc is copied
805 * to it. If clearing pvargDest fails, so does this function.
807 HRESULT WINAPI VariantCopyInd(VARIANT* pvargDest, VARIANTARG* pvargSrc)
809 VARIANTARG vTmp, *pSrc = pvargSrc;
813 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
814 debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
815 debugstr_VF(pvargSrc));
817 if (!V_ISBYREF(pvargSrc))
818 return VariantCopy(pvargDest, pvargSrc);
820 /* Argument checking is more lax than VariantCopy()... */
821 vt = V_TYPE(pvargSrc);
822 if (V_ISARRAY(pvargSrc) ||
823 (vt > VT_NULL && vt != (VARTYPE)15 && vt < VT_VOID &&
824 !(V_VT(pvargSrc) & (VT_VECTOR|VT_RESERVED))))
829 return E_INVALIDARG; /* ...And the return value for invalid types differs too */
831 if (pvargSrc == pvargDest)
833 /* In place copy. Use a shallow copy of pvargSrc & init pvargDest.
834 * This avoids an expensive VariantCopy() call - e.g. SafeArrayCopy().
838 V_VT(pvargDest) = VT_EMPTY;
842 /* Copy into another variant. Free the variant in pvargDest */
843 if (FAILED(hres = VariantClear(pvargDest)))
849 /* Native doesn't check that *V_ARRAYREF(pSrc) is valid */
850 hres = SafeArrayCopy(*V_ARRAYREF(pSrc), &V_ARRAY(pvargDest));
852 else if (V_VT(pSrc) == (VT_BSTR|VT_BYREF))
854 /* Native doesn't check that *V_BSTRREF(pSrc) is valid */
855 V_BSTR(pvargDest) = SysAllocStringLen(*V_BSTRREF(pSrc), SysStringLen(*V_BSTRREF(pSrc)));
857 else if (V_VT(pSrc) == (VT_RECORD|VT_BYREF))
859 V_UNION(pvargDest,brecVal) = V_UNION(pvargSrc,brecVal);
860 hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
862 else if (V_VT(pSrc) == (VT_DISPATCH|VT_BYREF) ||
863 V_VT(pSrc) == (VT_UNKNOWN|VT_BYREF))
865 /* Native doesn't check that *V_UNKNOWNREF(pSrc) is valid */
866 V_UNKNOWN(pvargDest) = *V_UNKNOWNREF(pSrc);
867 if (*V_UNKNOWNREF(pSrc))
868 IUnknown_AddRef(*V_UNKNOWNREF(pSrc));
870 else if (V_VT(pSrc) == (VT_VARIANT|VT_BYREF))
872 /* Native doesn't check that *V_VARIANTREF(pSrc) is valid */
873 if (V_VT(V_VARIANTREF(pSrc)) == (VT_VARIANT|VT_BYREF))
874 hres = E_INVALIDARG; /* Don't dereference more than one level */
876 hres = VariantCopyInd(pvargDest, V_VARIANTREF(pSrc));
878 /* Use the dereferenced variants type value, not VT_VARIANT */
879 goto VariantCopyInd_Return;
881 else if (V_VT(pSrc) == (VT_DECIMAL|VT_BYREF))
883 memcpy(&DEC_SCALE(&V_DECIMAL(pvargDest)), &DEC_SCALE(V_DECIMALREF(pSrc)),
884 sizeof(DECIMAL) - sizeof(USHORT));
888 /* Copy the pointed to data into this variant */
889 memcpy(&V_BYREF(pvargDest), V_BYREF(pSrc), VARIANT_DataSize(pSrc));
892 V_VT(pvargDest) = V_VT(pSrc) & ~VT_BYREF;
894 VariantCopyInd_Return:
896 if (pSrc != pvargSrc)
899 TRACE("returning 0x%08lx, %p->(%s%s)\n", hres, pvargDest,
900 debugstr_VT(pvargDest), debugstr_VF(pvargDest));
904 /******************************************************************************
905 * VariantChangeType [OLEAUT32.12]
907 * Change the type of a variant.
910 * pvargDest [O] Destination for the converted variant
911 * pvargSrc [O] Source variant to change the type of
912 * wFlags [I] VARIANT_ flags from "oleauto.h"
913 * vt [I] Variant type to change pvargSrc into
916 * Success: S_OK. pvargDest contains the converted value.
917 * Failure: An HRESULT error code describing the failure.
920 * The LCID used for the conversion is LOCALE_USER_DEFAULT.
921 * See VariantChangeTypeEx.
923 HRESULT WINAPI VariantChangeType(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
924 USHORT wFlags, VARTYPE vt)
926 return VariantChangeTypeEx( pvargDest, pvargSrc, LOCALE_USER_DEFAULT, wFlags, vt );
929 /******************************************************************************
930 * VariantChangeTypeEx [OLEAUT32.147]
932 * Change the type of a variant.
935 * pvargDest [O] Destination for the converted variant
936 * pvargSrc [O] Source variant to change the type of
937 * lcid [I] LCID for the conversion
938 * wFlags [I] VARIANT_ flags from "oleauto.h"
939 * vt [I] Variant type to change pvargSrc into
942 * Success: S_OK. pvargDest contains the converted value.
943 * Failure: An HRESULT error code describing the failure.
946 * pvargDest and pvargSrc can point to the same variant to perform an in-place
947 * conversion. If the conversion is successful, pvargSrc will be freed.
949 HRESULT WINAPI VariantChangeTypeEx(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
950 LCID lcid, USHORT wFlags, VARTYPE vt)
954 TRACE("(%p->(%s%s),%p->(%s%s),0x%08lx,0x%04x,%s%s)\n", pvargDest,
955 debugstr_VT(pvargDest), debugstr_VF(pvargDest), pvargSrc,
956 debugstr_VT(pvargSrc), debugstr_VF(pvargSrc), lcid, wFlags,
957 debugstr_vt(vt), debugstr_vf(vt));
960 res = DISP_E_BADVARTYPE;
963 res = VARIANT_ValidateType(V_VT(pvargSrc));
967 res = VARIANT_ValidateType(vt);
973 V_VT(&vTmp) = VT_EMPTY;
974 res = VariantCopyInd(&vTmp, pvargSrc);
978 res = VariantClear(pvargDest);
982 if (V_ISARRAY(&vTmp) || (vt & VT_ARRAY))
983 res = VARIANT_CoerceArray(pvargDest, &vTmp, vt);
985 res = VARIANT_Coerce(pvargDest, lcid, wFlags, &vTmp, vt);
988 V_VT(pvargDest) = vt;
996 TRACE("returning 0x%08lx, %p->(%s%s)\n", res, pvargDest,
997 debugstr_VT(pvargDest), debugstr_VF(pvargDest));
1001 /* Date Conversions */
1003 #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
1005 /* Convert a VT_DATE value to a Julian Date */
1006 static inline int VARIANT_JulianFromDate(int dateIn)
1008 int julianDays = dateIn;
1010 julianDays -= DATE_MIN; /* Convert to + days from 1 Jan 100 AD */
1011 julianDays += 1757585; /* Convert to + days from 23 Nov 4713 BC (Julian) */
1015 /* Convert a Julian Date to a VT_DATE value */
1016 static inline int VARIANT_DateFromJulian(int dateIn)
1018 int julianDays = dateIn;
1020 julianDays -= 1757585; /* Convert to + days from 1 Jan 100 AD */
1021 julianDays += DATE_MIN; /* Convert to +/- days from 1 Jan 1899 AD */
1025 /* Convert a Julian date to Day/Month/Year - from PostgreSQL */
1026 static inline void VARIANT_DMYFromJulian(int jd, USHORT *year, USHORT *month, USHORT *day)
1032 l -= (n * 146097 + 3) / 4;
1033 i = (4000 * (l + 1)) / 1461001;
1034 l += 31 - (i * 1461) / 4;
1035 j = (l * 80) / 2447;
1036 *day = l - (j * 2447) / 80;
1038 *month = (j + 2) - (12 * l);
1039 *year = 100 * (n - 49) + i + l;
1042 /* Convert Day/Month/Year to a Julian date - from PostgreSQL */
1043 static inline double VARIANT_JulianFromDMY(USHORT year, USHORT month, USHORT day)
1045 int m12 = (month - 14) / 12;
1047 return ((1461 * (year + 4800 + m12)) / 4 + (367 * (month - 2 - 12 * m12)) / 12 -
1048 (3 * ((year + 4900 + m12) / 100)) / 4 + day - 32075);
1051 /* Macros for accessing DOS format date/time fields */
1052 #define DOS_YEAR(x) (1980 + (x >> 9))
1053 #define DOS_MONTH(x) ((x >> 5) & 0xf)
1054 #define DOS_DAY(x) (x & 0x1f)
1055 #define DOS_HOUR(x) (x >> 11)
1056 #define DOS_MINUTE(x) ((x >> 5) & 0x3f)
1057 #define DOS_SECOND(x) ((x & 0x1f) << 1)
1058 /* Create a DOS format date/time */
1059 #define DOS_DATE(d,m,y) (d | (m << 5) | ((y-1980) << 9))
1060 #define DOS_TIME(h,m,s) ((s >> 1) | (m << 5) | (h << 11))
1062 /* Roll a date forwards or backwards to correct it */
1063 static HRESULT VARIANT_RollUdate(UDATE *lpUd)
1065 static const BYTE days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1067 TRACE("Raw date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
1068 lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
1070 /* Years < 100 are treated as 1900 + year */
1071 if (lpUd->st.wYear < 100)
1072 lpUd->st.wYear += 1900;
1074 if (!lpUd->st.wMonth)
1076 /* Roll back to December of the previous year */
1077 lpUd->st.wMonth = 12;
1080 else while (lpUd->st.wMonth > 12)
1082 /* Roll forward the correct number of months */
1084 lpUd->st.wMonth -= 12;
1087 if (lpUd->st.wYear > 9999 || lpUd->st.wHour > 23 ||
1088 lpUd->st.wMinute > 59 || lpUd->st.wSecond > 59)
1089 return E_INVALIDARG; /* Invalid values */
1093 /* Roll back the date one day */
1094 if (lpUd->st.wMonth == 1)
1096 /* Roll back to December 31 of the previous year */
1098 lpUd->st.wMonth = 12;
1103 lpUd->st.wMonth--; /* Previous month */
1104 if (lpUd->st.wMonth == 2 && IsLeapYear(lpUd->st.wYear))
1105 lpUd->st.wDay = 29; /* Februaury has 29 days on leap years */
1107 lpUd->st.wDay = days[lpUd->st.wMonth]; /* Last day of the month */
1110 else if (lpUd->st.wDay > 28)
1112 int rollForward = 0;
1114 /* Possibly need to roll the date forward */
1115 if (lpUd->st.wMonth == 2 && IsLeapYear(lpUd->st.wYear))
1116 rollForward = lpUd->st.wDay - 29; /* Februaury has 29 days on leap years */
1118 rollForward = lpUd->st.wDay - days[lpUd->st.wMonth];
1120 if (rollForward > 0)
1122 lpUd->st.wDay = rollForward;
1124 if (lpUd->st.wMonth > 12)
1126 lpUd->st.wMonth = 1; /* Roll forward into January of the next year */
1131 TRACE("Rolled date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
1132 lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
1136 /**********************************************************************
1137 * DosDateTimeToVariantTime [OLEAUT32.14]
1139 * Convert a Dos format date and time into variant VT_DATE format.
1142 * wDosDate [I] Dos format date
1143 * wDosTime [I] Dos format time
1144 * pDateOut [O] Destination for VT_DATE format
1147 * Success: TRUE. pDateOut contains the converted time.
1148 * Failure: FALSE, if wDosDate or wDosTime are invalid (see notes).
1151 * - Dos format dates can only hold dates from 1-Jan-1980 to 31-Dec-2099.
1152 * - Dos format times are accurate to only 2 second precision.
1153 * - The format of a Dos Date is:
1154 *| Bits Values Meaning
1155 *| ---- ------ -------
1156 *| 0-4 1-31 Day of the week. 0 rolls back one day. A value greater than
1157 *| the days in the month rolls forward the extra days.
1158 *| 5-8 1-12 Month of the year. 0 rolls back to December of the previous
1159 *| year. 13-15 are invalid.
1160 *| 9-15 0-119 Year based from 1980 (Max 2099). 120-127 are invalid.
1161 * - The format of a Dos Time is:
1162 *| Bits Values Meaning
1163 *| ---- ------ -------
1164 *| 0-4 0-29 Seconds/2. 30 and 31 are invalid.
1165 *| 5-10 0-59 Minutes. 60-63 are invalid.
1166 *| 11-15 0-23 Hours (24 hour clock). 24-32 are invalid.
1168 INT WINAPI DosDateTimeToVariantTime(USHORT wDosDate, USHORT wDosTime,
1173 TRACE("(0x%x(%d/%d/%d),0x%x(%d:%d:%d),%p)\n",
1174 wDosDate, DOS_YEAR(wDosDate), DOS_MONTH(wDosDate), DOS_DAY(wDosDate),
1175 wDosTime, DOS_HOUR(wDosTime), DOS_MINUTE(wDosTime), DOS_SECOND(wDosTime),
1178 ud.st.wYear = DOS_YEAR(wDosDate);
1179 ud.st.wMonth = DOS_MONTH(wDosDate);
1180 if (ud.st.wYear > 2099 || ud.st.wMonth > 12)
1182 ud.st.wDay = DOS_DAY(wDosDate);
1183 ud.st.wHour = DOS_HOUR(wDosTime);
1184 ud.st.wMinute = DOS_MINUTE(wDosTime);
1185 ud.st.wSecond = DOS_SECOND(wDosTime);
1186 ud.st.wDayOfWeek = ud.st.wMilliseconds = 0;
1188 return !VarDateFromUdate(&ud, 0, pDateOut);
1191 /**********************************************************************
1192 * VariantTimeToDosDateTime [OLEAUT32.13]
1194 * Convert a variant format date into a Dos format date and time.
1196 * dateIn [I] VT_DATE time format
1197 * pwDosDate [O] Destination for Dos format date
1198 * pwDosTime [O] Destination for Dos format time
1201 * Success: TRUE. pwDosDate and pwDosTime contains the converted values.
1202 * Failure: FALSE, if dateIn cannot be represented in Dos format.
1205 * See DosDateTimeToVariantTime() for Dos format details and bugs.
1207 INT WINAPI VariantTimeToDosDateTime(double dateIn, USHORT *pwDosDate, USHORT *pwDosTime)
1211 TRACE("(%g,%p,%p)\n", dateIn, pwDosDate, pwDosTime);
1213 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1216 if (ud.st.wYear < 1980 || ud.st.wYear > 2099)
1219 *pwDosDate = DOS_DATE(ud.st.wDay, ud.st.wMonth, ud.st.wYear);
1220 *pwDosTime = DOS_TIME(ud.st.wHour, ud.st.wMinute, ud.st.wSecond);
1222 TRACE("Returning 0x%x(%d/%d/%d), 0x%x(%d:%d:%d)\n",
1223 *pwDosDate, DOS_YEAR(*pwDosDate), DOS_MONTH(*pwDosDate), DOS_DAY(*pwDosDate),
1224 *pwDosTime, DOS_HOUR(*pwDosTime), DOS_MINUTE(*pwDosTime), DOS_SECOND(*pwDosTime));
1228 /***********************************************************************
1229 * SystemTimeToVariantTime [OLEAUT32.184]
1231 * Convert a System format date and time into variant VT_DATE format.
1234 * lpSt [I] System format date and time
1235 * pDateOut [O] Destination for VT_DATE format date
1238 * Success: TRUE. *pDateOut contains the converted value.
1239 * Failure: FALSE, if lpSt cannot be represented in VT_DATE format.
1241 INT WINAPI SystemTimeToVariantTime(LPSYSTEMTIME lpSt, double *pDateOut)
1245 TRACE("(%p->%d/%d/%d %d:%d:%d,%p)\n", lpSt, lpSt->wDay, lpSt->wMonth,
1246 lpSt->wYear, lpSt->wHour, lpSt->wMinute, lpSt->wSecond, pDateOut);
1248 if (lpSt->wMonth > 12)
1251 memcpy(&ud.st, lpSt, sizeof(ud.st));
1252 return !VarDateFromUdate(&ud, 0, pDateOut);
1255 /***********************************************************************
1256 * VariantTimeToSystemTime [OLEAUT32.185]
1258 * Convert a variant VT_DATE into a System format date and time.
1261 * datein [I] Variant VT_DATE format date
1262 * lpSt [O] Destination for System format date and time
1265 * Success: TRUE. *lpSt contains the converted value.
1266 * Failure: FALSE, if dateIn is too large or small.
1268 INT WINAPI VariantTimeToSystemTime(double dateIn, LPSYSTEMTIME lpSt)
1272 TRACE("(%g,%p)\n", dateIn, lpSt);
1274 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1277 memcpy(lpSt, &ud.st, sizeof(ud.st));
1281 /***********************************************************************
1282 * VarDateFromUdate [OLEAUT32.330]
1284 * Convert an unpacked format date and time to a variant VT_DATE.
1287 * pUdateIn [I] Unpacked format date and time to convert
1288 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1289 * pDateOut [O] Destination for variant VT_DATE.
1292 * Success: S_OK. *pDateOut contains the converted value.
1293 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1295 HRESULT WINAPI VarDateFromUdate(UDATE *pUdateIn, ULONG dwFlags, DATE *pDateOut)
1300 TRACE("(%p->%d/%d/%d %d:%d:%d:%d %d %d,0x%08lx,%p)\n", pUdateIn,
1301 pUdateIn->st.wMonth, pUdateIn->st.wDay, pUdateIn->st.wYear,
1302 pUdateIn->st.wHour, pUdateIn->st.wMinute, pUdateIn->st.wSecond,
1303 pUdateIn->st.wMilliseconds, pUdateIn->st.wDayOfWeek,
1304 pUdateIn->wDayOfYear, dwFlags, pDateOut);
1306 memcpy(&ud, pUdateIn, sizeof(ud));
1308 if (dwFlags & VAR_VALIDDATE)
1309 WARN("Ignoring VAR_VALIDDATE\n");
1311 if (FAILED(VARIANT_RollUdate(&ud)))
1312 return E_INVALIDARG;
1315 dateVal = VARIANT_DateFromJulian(VARIANT_JulianFromDMY(ud.st.wYear, ud.st.wMonth, ud.st.wDay));
1318 dateVal += ud.st.wHour / 24.0;
1319 dateVal += ud.st.wMinute / 1440.0;
1320 dateVal += ud.st.wSecond / 86400.0;
1321 dateVal += ud.st.wMilliseconds / 86400000.0;
1323 TRACE("Returning %g\n", dateVal);
1324 *pDateOut = dateVal;
1328 /***********************************************************************
1329 * VarUdateFromDate [OLEAUT32.331]
1331 * Convert a variant VT_DATE into an unpacked format date and time.
1334 * datein [I] Variant VT_DATE format date
1335 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1336 * lpUdate [O] Destination for unpacked format date and time
1339 * Success: S_OK. *lpUdate contains the converted value.
1340 * Failure: E_INVALIDARG, if dateIn is too large or small.
1342 HRESULT WINAPI VarUdateFromDate(DATE dateIn, ULONG dwFlags, UDATE *lpUdate)
1344 /* Cumulative totals of days per month */
1345 static const USHORT cumulativeDays[] =
1347 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
1349 double datePart, timePart;
1352 TRACE("(%g,0x%08lx,%p)\n", dateIn, dwFlags, lpUdate);
1354 if (dateIn <= (DATE_MIN - 1.0) || dateIn >= (DATE_MAX + 1.0))
1355 return E_INVALIDARG;
1357 datePart = dateIn < 0.0 ? ceil(dateIn) : floor(dateIn);
1358 /* Compensate for int truncation (always downwards) */
1359 timePart = dateIn - datePart + 0.00000000001;
1360 if (timePart >= 1.0)
1361 timePart -= 0.00000000001;
1364 julianDays = VARIANT_JulianFromDate(dateIn);
1365 VARIANT_DMYFromJulian(julianDays, &lpUdate->st.wYear, &lpUdate->st.wMonth,
1368 datePart = (datePart + 1.5) / 7.0;
1369 lpUdate->st.wDayOfWeek = (datePart - floor(datePart)) * 7;
1370 if (lpUdate->st.wDayOfWeek == 0)
1371 lpUdate->st.wDayOfWeek = 5;
1372 else if (lpUdate->st.wDayOfWeek == 1)
1373 lpUdate->st.wDayOfWeek = 6;
1375 lpUdate->st.wDayOfWeek -= 2;
1377 if (lpUdate->st.wMonth > 2 && IsLeapYear(lpUdate->st.wYear))
1378 lpUdate->wDayOfYear = 1; /* After February, in a leap year */
1380 lpUdate->wDayOfYear = 0;
1382 lpUdate->wDayOfYear += cumulativeDays[lpUdate->st.wMonth];
1383 lpUdate->wDayOfYear += lpUdate->st.wDay;
1387 lpUdate->st.wHour = timePart;
1388 timePart -= lpUdate->st.wHour;
1390 lpUdate->st.wMinute = timePart;
1391 timePart -= lpUdate->st.wMinute;
1393 lpUdate->st.wSecond = timePart;
1394 timePart -= lpUdate->st.wSecond;
1395 lpUdate->st.wMilliseconds = 0;
1398 /* Round the milliseconds, adjusting the time/date forward if needed */
1399 if (lpUdate->st.wSecond < 59)
1400 lpUdate->st.wSecond++;
1403 lpUdate->st.wSecond = 0;
1404 if (lpUdate->st.wMinute < 59)
1405 lpUdate->st.wMinute++;
1408 lpUdate->st.wMinute = 0;
1409 if (lpUdate->st.wHour < 23)
1410 lpUdate->st.wHour++;
1413 lpUdate->st.wHour = 0;
1414 /* Roll over a whole day */
1415 if (++lpUdate->st.wDay > 28)
1416 VARIANT_RollUdate(lpUdate);
1424 #define GET_NUMBER_TEXT(fld,name) \
1426 if (!GetLocaleInfoW(lcid, lctype|fld, buff, sizeof(WCHAR) * 2)) \
1427 WARN("buffer too small for " #fld "\n"); \
1429 if (buff[0]) lpChars->name = buff[0]; \
1430 TRACE("lcid 0x%lx, " #name "=%d '%c'\n", lcid, lpChars->name, lpChars->name)
1432 /* Get the valid number characters for an lcid */
1433 void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS *lpChars, LCID lcid, DWORD dwFlags)
1435 static const VARIANT_NUMBER_CHARS defaultChars = { '-','+','.',',','$',0,'.',',' };
1436 LCTYPE lctype = dwFlags & LOCALE_NOUSEROVERRIDE;
1439 memcpy(lpChars, &defaultChars, sizeof(defaultChars));
1440 GET_NUMBER_TEXT(LOCALE_SNEGATIVESIGN, cNegativeSymbol);
1441 GET_NUMBER_TEXT(LOCALE_SPOSITIVESIGN, cPositiveSymbol);
1442 GET_NUMBER_TEXT(LOCALE_SDECIMAL, cDecimalPoint);
1443 GET_NUMBER_TEXT(LOCALE_STHOUSAND, cDigitSeperator);
1444 GET_NUMBER_TEXT(LOCALE_SMONDECIMALSEP, cCurrencyDecimalPoint);
1445 GET_NUMBER_TEXT(LOCALE_SMONTHOUSANDSEP, cCurrencyDigitSeperator);
1447 /* Local currency symbols are often 2 characters */
1448 lpChars->cCurrencyLocal2 = '\0';
1449 switch(GetLocaleInfoW(lcid, lctype|LOCALE_SCURRENCY, buff, sizeof(WCHAR) * 4))
1451 case 3: lpChars->cCurrencyLocal2 = buff[1]; /* Fall through */
1452 case 2: lpChars->cCurrencyLocal = buff[0];
1454 default: WARN("buffer too small for LOCALE_SCURRENCY\n");
1456 TRACE("lcid 0x%lx, cCurrencyLocal =%d,%d '%c','%c'\n", lcid, lpChars->cCurrencyLocal,
1457 lpChars->cCurrencyLocal2, lpChars->cCurrencyLocal, lpChars->cCurrencyLocal2);
1460 /* Number Parsing States */
1461 #define B_PROCESSING_EXPONENT 0x1
1462 #define B_NEGATIVE_EXPONENT 0x2
1463 #define B_EXPONENT_START 0x4
1464 #define B_INEXACT_ZEROS 0x8
1465 #define B_LEADING_ZERO 0x10
1467 /**********************************************************************
1468 * VarParseNumFromStr [OLEAUT32.46]
1470 * Parse a string containing a number into a NUMPARSE structure.
1473 * lpszStr [I] String to parse number from
1474 * lcid [I] Locale Id for the conversion
1475 * dwFlags [I] 0, or LOCALE_NOUSEROVERRIDE to use system default number chars
1476 * pNumprs [I/O] Destination for parsed number
1477 * rgbDig [O] Destination for digits read in
1480 * Success: S_OK. pNumprs and rgbDig contain the parsed representation of
1482 * Failure: E_INVALIDARG, if any parameter is invalid.
1483 * DISP_E_TYPEMISMATCH, if the string is not a number or is formatted
1485 * DISP_E_OVERFLOW, if rgbDig is too small to hold the number.
1488 * pNumprs must have the following fields set:
1489 * cDig: Set to the size of rgbDig.
1490 * dwInFlags: Set to the allowable syntax of the number using NUMPRS_ flags
1494 * - I am unsure if this function should parse non-arabic (e.g. Thai)
1495 * numerals, so this has not been implemented.
1497 HRESULT WINAPI VarParseNumFromStr(OLECHAR *lpszStr, LCID lcid, ULONG dwFlags,
1498 NUMPARSE *pNumprs, BYTE *rgbDig)
1500 VARIANT_NUMBER_CHARS chars;
1502 DWORD dwState = B_EXPONENT_START|B_INEXACT_ZEROS;
1503 int iMaxDigits = sizeof(rgbTmp) / sizeof(BYTE);
1506 TRACE("(%s,%ld,0x%08lx,%p,%p)\n", debugstr_w(lpszStr), lcid, dwFlags, pNumprs, rgbDig);
1508 if (pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1509 FIXME("dwInFlags & NUMPRS_HEX_OCT not yet implemented!\n");
1511 if (!pNumprs || !rgbDig)
1512 return E_INVALIDARG;
1514 if (pNumprs->cDig < iMaxDigits)
1515 iMaxDigits = pNumprs->cDig;
1518 pNumprs->dwOutFlags = 0;
1519 pNumprs->cchUsed = 0;
1520 pNumprs->nBaseShift = 0;
1521 pNumprs->nPwr10 = 0;
1524 return DISP_E_TYPEMISMATCH;
1526 VARIANT_GetLocalisedNumberChars(&chars, lcid, dwFlags);
1528 /* First consume all the leading symbols and space from the string */
1531 if (pNumprs->dwInFlags & NUMPRS_LEADING_WHITE && isspaceW(*lpszStr))
1533 pNumprs->dwOutFlags |= NUMPRS_LEADING_WHITE;
1538 } while (isspaceW(*lpszStr));
1540 else if (pNumprs->dwInFlags & NUMPRS_LEADING_PLUS &&
1541 *lpszStr == chars.cPositiveSymbol &&
1542 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS))
1544 pNumprs->dwOutFlags |= NUMPRS_LEADING_PLUS;
1548 else if (pNumprs->dwInFlags & NUMPRS_LEADING_MINUS &&
1549 *lpszStr == chars.cNegativeSymbol &&
1550 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS))
1552 pNumprs->dwOutFlags |= (NUMPRS_LEADING_MINUS|NUMPRS_NEG);
1556 else if (pNumprs->dwInFlags & NUMPRS_CURRENCY &&
1557 !(pNumprs->dwOutFlags & NUMPRS_CURRENCY) &&
1558 *lpszStr == chars.cCurrencyLocal &&
1559 (!chars.cCurrencyLocal2 || lpszStr[1] == chars.cCurrencyLocal2))
1561 pNumprs->dwOutFlags |= NUMPRS_CURRENCY;
1564 /* Only accept currency characters */
1565 chars.cDecimalPoint = chars.cCurrencyDecimalPoint;
1566 chars.cDigitSeperator = chars.cCurrencyDigitSeperator;
1568 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == '(' &&
1569 !(pNumprs->dwOutFlags & NUMPRS_PARENS))
1571 pNumprs->dwOutFlags |= NUMPRS_PARENS;
1579 if (!(pNumprs->dwOutFlags & NUMPRS_CURRENCY))
1581 /* Only accept non-currency characters */
1582 chars.cCurrencyDecimalPoint = chars.cDecimalPoint;
1583 chars.cCurrencyDigitSeperator = chars.cDigitSeperator;
1586 /* Strip Leading zeros */
1587 while (*lpszStr == '0')
1589 dwState |= B_LEADING_ZERO;
1596 if (isdigitW(*lpszStr))
1598 if (dwState & B_PROCESSING_EXPONENT)
1600 int exponentSize = 0;
1601 if (dwState & B_EXPONENT_START)
1603 while (*lpszStr == '0')
1605 /* Skip leading zero's in the exponent */
1609 if (!isdigitW(*lpszStr))
1610 break; /* No exponent digits - invalid */
1613 while (isdigitW(*lpszStr))
1616 exponentSize += *lpszStr - '0';
1620 if (dwState & B_NEGATIVE_EXPONENT)
1621 exponentSize = -exponentSize;
1622 /* Add the exponent into the powers of 10 */
1623 pNumprs->nPwr10 += exponentSize;
1624 dwState &= ~(B_PROCESSING_EXPONENT|B_EXPONENT_START);
1625 lpszStr--; /* back up to allow processing of next char */
1629 if (pNumprs->cDig >= iMaxDigits)
1631 pNumprs->dwOutFlags |= NUMPRS_INEXACT;
1633 if (*lpszStr != '0')
1634 dwState &= ~B_INEXACT_ZEROS; /* Inexact number with non-trailing zeros */
1636 /* This digit can't be represented, but count it in nPwr10 */
1637 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1644 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1645 pNumprs->nPwr10--; /* Count decimal points in nPwr10 */
1646 rgbTmp[pNumprs->cDig] = *lpszStr - '0';
1652 else if (*lpszStr == chars.cDigitSeperator && pNumprs->dwInFlags & NUMPRS_THOUSANDS)
1654 pNumprs->dwOutFlags |= NUMPRS_THOUSANDS;
1657 else if (*lpszStr == chars.cDecimalPoint &&
1658 pNumprs->dwInFlags & NUMPRS_DECIMAL &&
1659 !(pNumprs->dwOutFlags & (NUMPRS_DECIMAL|NUMPRS_EXPONENT)))
1661 pNumprs->dwOutFlags |= NUMPRS_DECIMAL;
1664 /* Remove trailing zeros from the whole number part */
1665 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1671 /* If we have no digits so far, skip leading zeros */
1674 while (lpszStr[1] == '0')
1676 dwState |= B_LEADING_ZERO;
1682 else if ((*lpszStr == 'e' || *lpszStr == 'E') &&
1683 pNumprs->dwInFlags & NUMPRS_EXPONENT &&
1684 !(pNumprs->dwOutFlags & NUMPRS_EXPONENT))
1686 dwState |= B_PROCESSING_EXPONENT;
1687 pNumprs->dwOutFlags |= NUMPRS_EXPONENT;
1690 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cPositiveSymbol)
1692 cchUsed++; /* Ignore positive exponent */
1694 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cNegativeSymbol)
1696 dwState |= B_NEGATIVE_EXPONENT;
1700 break; /* Stop at an unrecognised character */
1705 if (!pNumprs->cDig && dwState & B_LEADING_ZERO)
1707 /* Ensure a 0 on its own gets stored */
1712 if (pNumprs->dwOutFlags & NUMPRS_EXPONENT && dwState & B_PROCESSING_EXPONENT)
1714 pNumprs->cchUsed = cchUsed;
1715 return DISP_E_TYPEMISMATCH; /* Failed to completely parse the exponent */
1718 if (pNumprs->dwOutFlags & NUMPRS_INEXACT)
1720 if (dwState & B_INEXACT_ZEROS)
1721 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* All zeros doesn't set NUMPRS_INEXACT */
1725 /* Remove trailing zeros from the last (whole number or decimal) part */
1726 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1728 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1736 if (pNumprs->cDig <= iMaxDigits)
1737 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* Ignore stripped zeros for NUMPRS_INEXACT */
1739 pNumprs->cDig = iMaxDigits; /* Only return iMaxDigits worth of digits */
1741 /* Copy the digits we processed into rgbDig */
1742 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1744 /* Consume any trailing symbols and space */
1747 if ((pNumprs->dwInFlags & NUMPRS_TRAILING_WHITE) && isspaceW(*lpszStr))
1749 pNumprs->dwOutFlags |= NUMPRS_TRAILING_WHITE;
1754 } while (isspaceW(*lpszStr));
1756 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_PLUS &&
1757 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS) &&
1758 *lpszStr == chars.cPositiveSymbol)
1760 pNumprs->dwOutFlags |= NUMPRS_TRAILING_PLUS;
1764 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_MINUS &&
1765 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS) &&
1766 *lpszStr == chars.cNegativeSymbol)
1768 pNumprs->dwOutFlags |= (NUMPRS_TRAILING_MINUS|NUMPRS_NEG);
1772 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == ')' &&
1773 pNumprs->dwOutFlags & NUMPRS_PARENS)
1777 pNumprs->dwOutFlags |= NUMPRS_NEG;
1783 if (pNumprs->dwOutFlags & NUMPRS_PARENS && !(pNumprs->dwOutFlags & NUMPRS_NEG))
1785 pNumprs->cchUsed = cchUsed;
1786 return DISP_E_TYPEMISMATCH; /* Opening parenthesis not matched */
1789 if (pNumprs->dwInFlags & NUMPRS_USE_ALL && *lpszStr != '\0')
1790 return DISP_E_TYPEMISMATCH; /* Not all chars were consumed */
1793 return DISP_E_TYPEMISMATCH; /* No Number found */
1795 pNumprs->cchUsed = cchUsed;
1799 /* VTBIT flags indicating an integer value */
1800 #define INTEGER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8)
1801 /* VTBIT flags indicating a real number value */
1802 #define REAL_VTBITS (VTBIT_R4|VTBIT_R8|VTBIT_CY)
1804 /**********************************************************************
1805 * VarNumFromParseNum [OLEAUT32.47]
1807 * Convert a NUMPARSE structure into a numeric Variant type.
1810 * pNumprs [I] Source for parsed number. cDig must be set to the size of rgbDig
1811 * rgbDig [I] Source for the numbers digits
1812 * dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
1813 * pVarDst [O] Destination for the converted Variant value.
1816 * Success: S_OK. pVarDst contains the converted value.
1817 * Failure: E_INVALIDARG, if any parameter is invalid.
1818 * DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
1821 * - The smallest favoured type present in dwVtBits that can represent the
1822 * number in pNumprs without losing precision is used.
1823 * - Signed types are preferrred over unsigned types of the same size.
1824 * - Preferred types in order are: integer, float, double, currency then decimal.
1825 * - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
1826 * for details of the rounding method.
1827 * - pVarDst is not cleared before the result is stored in it.
1829 HRESULT WINAPI VarNumFromParseNum(NUMPARSE *pNumprs, BYTE *rgbDig,
1830 ULONG dwVtBits, VARIANT *pVarDst)
1832 /* Scale factors and limits for double arithmetic */
1833 static const double dblMultipliers[11] = {
1834 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
1835 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
1837 static const double dblMinimums[11] = {
1838 R8_MIN, R8_MIN*10.0, R8_MIN*100.0, R8_MIN*1000.0, R8_MIN*10000.0,
1839 R8_MIN*100000.0, R8_MIN*1000000.0, R8_MIN*10000000.0,
1840 R8_MIN*100000000.0, R8_MIN*1000000000.0, R8_MIN*10000000000.0
1842 static const double dblMaximums[11] = {
1843 R8_MAX, R8_MAX/10.0, R8_MAX/100.0, R8_MAX/1000.0, R8_MAX/10000.0,
1844 R8_MAX/100000.0, R8_MAX/1000000.0, R8_MAX/10000000.0,
1845 R8_MAX/100000000.0, R8_MAX/1000000000.0, R8_MAX/10000000000.0
1848 int wholeNumberDigits, fractionalDigits, divisor10 = 0, multiplier10 = 0;
1850 TRACE("(%p,%p,0x%lx,%p)\n", pNumprs, rgbDig, dwVtBits, pVarDst);
1852 if (pNumprs->nBaseShift)
1854 /* nBaseShift indicates a hex or octal number */
1855 FIXME("nBaseShift=%d not yet implemented, returning overflow\n", pNumprs->nBaseShift);
1856 return DISP_E_OVERFLOW;
1859 /* Count the number of relevant fractional and whole digits stored,
1860 * And compute the divisor/multiplier to scale the number by.
1862 if (pNumprs->nPwr10 < 0)
1864 if (-pNumprs->nPwr10 >= pNumprs->cDig)
1866 /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
1867 wholeNumberDigits = 0;
1868 fractionalDigits = pNumprs->cDig;
1869 divisor10 = -pNumprs->nPwr10;
1873 /* An exactly represented real number e.g. 1.024 */
1874 wholeNumberDigits = pNumprs->cDig + pNumprs->nPwr10;
1875 fractionalDigits = pNumprs->cDig - wholeNumberDigits;
1876 divisor10 = pNumprs->cDig - wholeNumberDigits;
1879 else if (pNumprs->nPwr10 == 0)
1881 /* An exactly represented whole number e.g. 1024 */
1882 wholeNumberDigits = pNumprs->cDig;
1883 fractionalDigits = 0;
1885 else /* pNumprs->nPwr10 > 0 */
1887 /* A whole number followed by nPwr10 0's e.g. 102400 */
1888 wholeNumberDigits = pNumprs->cDig;
1889 fractionalDigits = 0;
1890 multiplier10 = pNumprs->nPwr10;
1893 TRACE("cDig %d; nPwr10 %d, whole %d, frac %d ", pNumprs->cDig,
1894 pNumprs->nPwr10, wholeNumberDigits, fractionalDigits);
1895 TRACE("mult %d; div %d\n", multiplier10, divisor10);
1897 if (dwVtBits & (INTEGER_VTBITS|VTBIT_DECIMAL) &&
1898 (!fractionalDigits || !(dwVtBits & (REAL_VTBITS|VTBIT_CY|VTBIT_DECIMAL))))
1900 /* We have one or more integer output choices, and either:
1901 * 1) An integer input value, or
1902 * 2) A real number input value but no floating output choices.
1903 * Alternately, we have a DECIMAL output available and an integer input.
1905 * So, place the integer value into pVarDst, using the smallest type
1906 * possible and preferring signed over unsigned types.
1908 BOOL bOverflow = FALSE, bNegative;
1912 /* Convert the integer part of the number into a UI8 */
1913 for (i = 0; i < wholeNumberDigits; i++)
1915 if (ul64 > (UI8_MAX / 10 - rgbDig[i]))
1917 TRACE("Overflow multiplying digits\n");
1921 ul64 = ul64 * 10 + rgbDig[i];
1924 /* Account for the scale of the number */
1925 if (!bOverflow && multiplier10)
1927 for (i = 0; i < multiplier10; i++)
1929 if (ul64 > (UI8_MAX / 10))
1931 TRACE("Overflow scaling number\n");
1939 /* If we have any fractional digits, round the value.
1940 * Note we don't have to do this if divisor10 is < 1,
1941 * because this means the fractional part must be < 0.5
1943 if (!bOverflow && fractionalDigits && divisor10 > 0)
1945 const BYTE* fracDig = rgbDig + wholeNumberDigits;
1946 BOOL bAdjust = FALSE;
1948 TRACE("first decimal value is %d\n", *fracDig);
1951 bAdjust = TRUE; /* > 0.5 */
1952 else if (*fracDig == 5)
1954 for (i = 1; i < fractionalDigits; i++)
1958 bAdjust = TRUE; /* > 0.5 */
1962 /* If exactly 0.5, round only odd values */
1963 if (i == fractionalDigits && (ul64 & 1))
1969 if (ul64 == UI8_MAX)
1971 TRACE("Overflow after rounding\n");
1978 /* Zero is not a negative number */
1979 bNegative = pNumprs->dwOutFlags & NUMPRS_NEG && ul64 ? TRUE : FALSE;
1981 TRACE("Integer value is %lld, bNeg %d\n", ul64, bNegative);
1983 /* For negative integers, try the signed types in size order */
1984 if (!bOverflow && bNegative)
1986 if (dwVtBits & (VTBIT_I1|VTBIT_I2|VTBIT_I4|VTBIT_I8))
1988 if (dwVtBits & VTBIT_I1 && ul64 <= -I1_MIN)
1990 V_VT(pVarDst) = VT_I1;
1991 V_I1(pVarDst) = -ul64;
1994 else if (dwVtBits & VTBIT_I2 && ul64 <= -I2_MIN)
1996 V_VT(pVarDst) = VT_I2;
1997 V_I2(pVarDst) = -ul64;
2000 else if (dwVtBits & VTBIT_I4 && ul64 <= -((LONGLONG)I4_MIN))
2002 V_VT(pVarDst) = VT_I4;
2003 V_I4(pVarDst) = -ul64;
2006 else if (dwVtBits & VTBIT_I8 && ul64 <= (ULONGLONG)I8_MAX + 1)
2008 V_VT(pVarDst) = VT_I8;
2009 V_I8(pVarDst) = -ul64;
2012 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2014 /* Decimal is only output choice left - fast path */
2015 V_VT(pVarDst) = VT_DECIMAL;
2016 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_NEG,0);
2017 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2018 DEC_LO64(&V_DECIMAL(pVarDst)) = -ul64;
2023 else if (!bOverflow)
2025 /* For positive integers, try signed then unsigned types in size order */
2026 if (dwVtBits & VTBIT_I1 && ul64 <= I1_MAX)
2028 V_VT(pVarDst) = VT_I1;
2029 V_I1(pVarDst) = ul64;
2032 else if (dwVtBits & VTBIT_UI1 && ul64 <= UI1_MAX)
2034 V_VT(pVarDst) = VT_UI1;
2035 V_UI1(pVarDst) = ul64;
2038 else if (dwVtBits & VTBIT_I2 && ul64 <= I2_MAX)
2040 V_VT(pVarDst) = VT_I2;
2041 V_I2(pVarDst) = ul64;
2044 else if (dwVtBits & VTBIT_UI2 && ul64 <= UI2_MAX)
2046 V_VT(pVarDst) = VT_UI2;
2047 V_UI2(pVarDst) = ul64;
2050 else if (dwVtBits & VTBIT_I4 && ul64 <= I4_MAX)
2052 V_VT(pVarDst) = VT_I4;
2053 V_I4(pVarDst) = ul64;
2056 else if (dwVtBits & VTBIT_UI4 && ul64 <= UI4_MAX)
2058 V_VT(pVarDst) = VT_UI4;
2059 V_UI4(pVarDst) = ul64;
2062 else if (dwVtBits & VTBIT_I8 && ul64 <= I8_MAX)
2064 V_VT(pVarDst) = VT_I8;
2065 V_I8(pVarDst) = ul64;
2068 else if (dwVtBits & VTBIT_UI8)
2070 V_VT(pVarDst) = VT_UI8;
2071 V_UI8(pVarDst) = ul64;
2074 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2076 /* Decimal is only output choice left - fast path */
2077 V_VT(pVarDst) = VT_DECIMAL;
2078 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2079 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2080 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2086 if (dwVtBits & REAL_VTBITS)
2088 /* Try to put the number into a float or real */
2089 BOOL bOverflow = FALSE, bNegative = pNumprs->dwOutFlags & NUMPRS_NEG;
2093 /* Convert the number into a double */
2094 for (i = 0; i < pNumprs->cDig; i++)
2095 whole = whole * 10.0 + rgbDig[i];
2097 TRACE("Whole double value is %16.16g\n", whole);
2099 /* Account for the scale */
2100 while (multiplier10 > 10)
2102 if (whole > dblMaximums[10])
2104 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2108 whole = whole * dblMultipliers[10];
2113 if (whole > dblMaximums[multiplier10])
2115 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2119 whole = whole * dblMultipliers[multiplier10];
2122 TRACE("Scaled double value is %16.16g\n", whole);
2124 while (divisor10 > 10)
2126 if (whole < dblMinimums[10])
2128 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2132 whole = whole / dblMultipliers[10];
2137 if (whole < dblMinimums[divisor10])
2139 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2143 whole = whole / dblMultipliers[divisor10];
2146 TRACE("Final double value is %16.16g\n", whole);
2148 if (dwVtBits & VTBIT_R4 &&
2149 ((whole <= R4_MAX && whole >= R4_MIN) || whole == 0.0))
2151 TRACE("Set R4 to final value\n");
2152 V_VT(pVarDst) = VT_R4; /* Fits into a float */
2153 V_R4(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2157 if (dwVtBits & VTBIT_R8)
2159 TRACE("Set R8 to final value\n");
2160 V_VT(pVarDst) = VT_R8; /* Fits into a double */
2161 V_R8(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2165 if (dwVtBits & VTBIT_CY)
2167 if (SUCCEEDED(VarCyFromR8(bNegative ? -whole : whole, &V_CY(pVarDst))))
2169 V_VT(pVarDst) = VT_CY; /* Fits into a currency */
2170 TRACE("Set CY to final value\n");
2173 TRACE("Value Overflows CY\n");
2177 if (dwVtBits & VTBIT_DECIMAL)
2182 DECIMAL* pDec = &V_DECIMAL(pVarDst);
2184 DECIMAL_SETZERO(pDec);
2187 if (pNumprs->dwOutFlags & NUMPRS_NEG)
2188 DEC_SIGN(pDec) = DECIMAL_NEG;
2190 DEC_SIGN(pDec) = DECIMAL_POS;
2192 /* Factor the significant digits */
2193 for (i = 0; i < pNumprs->cDig; i++)
2195 tmp = (ULONG64)DEC_LO32(pDec) * 10 + rgbDig[i];
2196 carry = (ULONG)(tmp >> 32);
2197 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2198 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2199 carry = (ULONG)(tmp >> 32);
2200 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2201 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2202 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2204 if (tmp >> 32 & UI4_MAX)
2206 VarNumFromParseNum_DecOverflow:
2207 TRACE("Overflow\n");
2208 DEC_LO32(pDec) = DEC_MID32(pDec) = DEC_HI32(pDec) = UI4_MAX;
2209 return DISP_E_OVERFLOW;
2213 /* Account for the scale of the number */
2214 while (multiplier10 > 0)
2216 tmp = (ULONG64)DEC_LO32(pDec) * 10;
2217 carry = (ULONG)(tmp >> 32);
2218 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2219 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2220 carry = (ULONG)(tmp >> 32);
2221 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2222 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2223 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2225 if (tmp >> 32 & UI4_MAX)
2226 goto VarNumFromParseNum_DecOverflow;
2229 DEC_SCALE(pDec) = divisor10;
2231 V_VT(pVarDst) = VT_DECIMAL;
2234 return DISP_E_OVERFLOW; /* No more output choices */
2237 /**********************************************************************
2238 * VarCat [OLEAUT32.318]
2240 HRESULT WINAPI VarCat(LPVARIANT left, LPVARIANT right, LPVARIANT out)
2242 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2243 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), out);
2245 /* Should we VariantClear out? */
2246 /* Can we handle array, vector, by ref etc. */
2247 if ((V_VT(left)&VT_TYPEMASK) == VT_NULL &&
2248 (V_VT(right)&VT_TYPEMASK) == VT_NULL)
2250 V_VT(out) = VT_NULL;
2254 if (V_VT(left) == VT_BSTR && V_VT(right) == VT_BSTR)
2256 V_VT(out) = VT_BSTR;
2257 VarBstrCat (V_BSTR(left), V_BSTR(right), &V_BSTR(out));
2260 if (V_VT(left) == VT_BSTR) {
2264 V_VT(out) = VT_BSTR;
2265 hres = VariantChangeTypeEx(&bstrvar,right,0,0,VT_BSTR);
2267 FIXME("Failed to convert right side from vt %d to VT_BSTR?\n",V_VT(right));
2270 VarBstrCat (V_BSTR(left), V_BSTR(&bstrvar), &V_BSTR(out));
2273 if (V_VT(right) == VT_BSTR) {
2277 V_VT(out) = VT_BSTR;
2278 hres = VariantChangeTypeEx(&bstrvar,left,0,0,VT_BSTR);
2280 FIXME("Failed to convert right side from vt %d to VT_BSTR?\n",V_VT(right));
2283 VarBstrCat (V_BSTR(&bstrvar), V_BSTR(right), &V_BSTR(out));
2286 FIXME ("types %d / %d not supported\n",V_VT(left)&VT_TYPEMASK, V_VT(right)&VT_TYPEMASK);
2290 /**********************************************************************
2291 * VarCmp [OLEAUT32.176]
2294 * NORM_IGNORECASE, NORM_IGNORENONSPACE, NORM_IGNORESYMBOLS
2295 * NORM_IGNOREWIDTH, NORM_IGNOREKANATYPE, NORM_IGNOREKASHIDA
2298 HRESULT WINAPI VarCmp(LPVARIANT left, LPVARIANT right, LCID lcid, DWORD flags)
2308 TRACE("(%p->(%s%s),%p->(%s%s),0x%08lx,0x%08lx)\n", left, debugstr_VT(left),
2309 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), lcid, flags);
2311 VariantInit(&lv);VariantInit(&rv);
2312 V_VT(right) &= ~0x8000; /* hack since we sometime get this flag. */
2313 V_VT(left) &= ~0x8000; /* hack since we sometime get this flag. */
2315 /* If either are null, then return VARCMP_NULL */
2316 if ((V_VT(left)&VT_TYPEMASK) == VT_NULL ||
2317 (V_VT(right)&VT_TYPEMASK) == VT_NULL)
2320 /* Strings - use VarBstrCmp */
2321 if ((V_VT(left)&VT_TYPEMASK) == VT_BSTR &&
2322 (V_VT(right)&VT_TYPEMASK) == VT_BSTR) {
2323 return VarBstrCmp(V_BSTR(left), V_BSTR(right), lcid, flags);
2326 xmask = (1<<(V_VT(left)&VT_TYPEMASK))|(1<<(V_VT(right)&VT_TYPEMASK));
2327 if (xmask & (1<<VT_R8)) {
2328 rc = VariantChangeType(&lv,left,0,VT_R8);
2329 if (FAILED(rc)) return rc;
2330 rc = VariantChangeType(&rv,right,0,VT_R8);
2331 if (FAILED(rc)) return rc;
2333 if (V_R8(&lv) == V_R8(&rv)) return VARCMP_EQ;
2334 if (V_R8(&lv) < V_R8(&rv)) return VARCMP_LT;
2335 if (V_R8(&lv) > V_R8(&rv)) return VARCMP_GT;
2336 return E_FAIL; /* can't get here */
2338 if (xmask & (1<<VT_R4)) {
2339 rc = VariantChangeType(&lv,left,0,VT_R4);
2340 if (FAILED(rc)) return rc;
2341 rc = VariantChangeType(&rv,right,0,VT_R4);
2342 if (FAILED(rc)) return rc;
2344 if (V_R4(&lv) == V_R4(&rv)) return VARCMP_EQ;
2345 if (V_R4(&lv) < V_R4(&rv)) return VARCMP_LT;
2346 if (V_R4(&lv) > V_R4(&rv)) return VARCMP_GT;
2347 return E_FAIL; /* can't get here */
2350 /* Integers - Ideally like to use VarDecCmp, but no Dec support yet
2351 Use LONGLONG to maximize ranges */
2353 switch (V_VT(left)&VT_TYPEMASK) {
2354 case VT_I1 : lVal = V_UNION(left,cVal); break;
2355 case VT_I2 : lVal = V_UNION(left,iVal); break;
2356 case VT_I4 : lVal = V_UNION(left,lVal); break;
2357 case VT_INT : lVal = V_UNION(left,lVal); break;
2358 case VT_UI1 : lVal = V_UNION(left,bVal); break;
2359 case VT_UI2 : lVal = V_UNION(left,uiVal); break;
2360 case VT_UI4 : lVal = V_UNION(left,ulVal); break;
2361 case VT_UINT : lVal = V_UNION(left,ulVal); break;
2362 case VT_BOOL : lVal = V_UNION(left,boolVal); break;
2363 default: lOk = FALSE;
2367 switch (V_VT(right)&VT_TYPEMASK) {
2368 case VT_I1 : rVal = V_UNION(right,cVal); break;
2369 case VT_I2 : rVal = V_UNION(right,iVal); break;
2370 case VT_I4 : rVal = V_UNION(right,lVal); break;
2371 case VT_INT : rVal = V_UNION(right,lVal); break;
2372 case VT_UI1 : rVal = V_UNION(right,bVal); break;
2373 case VT_UI2 : rVal = V_UNION(right,uiVal); break;
2374 case VT_UI4 : rVal = V_UNION(right,ulVal); break;
2375 case VT_UINT : rVal = V_UNION(right,ulVal); break;
2376 case VT_BOOL : rVal = V_UNION(right,boolVal); break;
2377 default: rOk = FALSE;
2383 } else if (lVal > rVal) {
2390 /* Strings - use VarBstrCmp */
2391 if ((V_VT(left)&VT_TYPEMASK) == VT_DATE &&
2392 (V_VT(right)&VT_TYPEMASK) == VT_DATE) {
2394 if (floor(V_UNION(left,date)) == floor(V_UNION(right,date))) {
2395 /* Due to floating point rounding errors, calculate varDate in whole numbers) */
2396 double wholePart = 0.0;
2400 /* Get the fraction * 24*60*60 to make it into whole seconds */
2401 wholePart = (double) floor( V_UNION(left,date) );
2402 if (wholePart == 0) wholePart = 1;
2403 leftR = floor(fmod( V_UNION(left,date), wholePart ) * (24*60*60));
2405 wholePart = (double) floor( V_UNION(right,date) );
2406 if (wholePart == 0) wholePart = 1;
2407 rightR = floor(fmod( V_UNION(right,date), wholePart ) * (24*60*60));
2409 if (leftR < rightR) {
2411 } else if (leftR > rightR) {
2417 } else if (V_UNION(left,date) < V_UNION(right,date)) {
2419 } else if (V_UNION(left,date) > V_UNION(right,date)) {
2423 FIXME("VarCmp partial implementation, doesn't support vt 0x%x / 0x%x\n",V_VT(left), V_VT(right));
2427 /**********************************************************************
2428 * VarAnd [OLEAUT32.142]
2431 HRESULT WINAPI VarAnd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2433 HRESULT rc = E_FAIL;
2435 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2436 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2438 if ((V_VT(left)&VT_TYPEMASK) == VT_BOOL &&
2439 (V_VT(right)&VT_TYPEMASK) == VT_BOOL) {
2441 V_VT(result) = VT_BOOL;
2442 if (V_BOOL(left) && V_BOOL(right)) {
2443 V_BOOL(result) = VARIANT_TRUE;
2445 V_BOOL(result) = VARIANT_FALSE;
2456 int resT = 0; /* Testing has shown I2 & I2 == I2, all else
2457 becomes I4, even unsigned ints (incl. UI2) */
2460 switch (V_VT(left)&VT_TYPEMASK) {
2461 case VT_I1 : lVal = V_UNION(left,cVal); resT=VT_I4; break;
2462 case VT_I2 : lVal = V_UNION(left,iVal); resT=VT_I2; break;
2463 case VT_I4 : lVal = V_UNION(left,lVal); resT=VT_I4; break;
2464 case VT_INT : lVal = V_UNION(left,lVal); resT=VT_I4; break;
2465 case VT_UI1 : lVal = V_UNION(left,bVal); resT=VT_I4; break;
2466 case VT_UI2 : lVal = V_UNION(left,uiVal); resT=VT_I4; break;
2467 case VT_UI4 : lVal = V_UNION(left,ulVal); resT=VT_I4; break;
2468 case VT_UINT : lVal = V_UNION(left,ulVal); resT=VT_I4; break;
2469 default: lOk = FALSE;
2473 switch (V_VT(right)&VT_TYPEMASK) {
2474 case VT_I1 : rVal = V_UNION(right,cVal); resT=VT_I4; break;
2475 case VT_I2 : rVal = V_UNION(right,iVal); resT=max(VT_I2, resT); break;
2476 case VT_I4 : rVal = V_UNION(right,lVal); resT=VT_I4; break;
2477 case VT_INT : rVal = V_UNION(right,lVal); resT=VT_I4; break;
2478 case VT_UI1 : rVal = V_UNION(right,bVal); resT=VT_I4; break;
2479 case VT_UI2 : rVal = V_UNION(right,uiVal); resT=VT_I4; break;
2480 case VT_UI4 : rVal = V_UNION(right,ulVal); resT=VT_I4; break;
2481 case VT_UINT : rVal = V_UNION(right,ulVal); resT=VT_I4; break;
2482 default: rOk = FALSE;
2486 res = (lVal & rVal);
2487 V_VT(result) = resT;
2489 case VT_I2 : V_UNION(result,iVal) = res; break;
2490 case VT_I4 : V_UNION(result,lVal) = res; break;
2492 FIXME("Unexpected result variant type %x\n", resT);
2493 V_UNION(result,lVal) = res;
2498 FIXME("VarAnd stub\n");
2502 TRACE("returning 0x%8lx (%s%s),%ld\n", rc, debugstr_VT(result),
2503 debugstr_VF(result), V_VT(result) == VT_I4 ? V_I4(result) : V_I2(result));
2507 /**********************************************************************
2508 * VarAdd [OLEAUT32.141]
2509 * FIXME: From MSDN: If ... Then
2510 * Both expressions are of the string type Concatenated.
2511 * One expression is a string type and the other a character Addition.
2512 * One expression is numeric and the other is a string Addition.
2513 * Both expressions are numeric Addition.
2514 * Either expression is NULL NULL is returned.
2515 * Both expressions are empty Integer subtype is returned.
2518 HRESULT WINAPI VarAdd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2520 HRESULT rc = E_FAIL;
2522 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2523 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2525 if ((V_VT(left)&VT_TYPEMASK) == VT_EMPTY)
2526 return VariantCopy(result,right);
2528 if ((V_VT(right)&VT_TYPEMASK) == VT_EMPTY)
2529 return VariantCopy(result,left);
2531 /* check if we add doubles */
2532 if (((V_VT(left)&VT_TYPEMASK) == VT_R8) || ((V_VT(right)&VT_TYPEMASK) == VT_R8)) {
2540 switch (V_VT(left)&VT_TYPEMASK) {
2541 case VT_I1 : lVal = V_UNION(left,cVal); break;
2542 case VT_I2 : lVal = V_UNION(left,iVal); break;
2543 case VT_I4 : lVal = V_UNION(left,lVal); break;
2544 case VT_INT : lVal = V_UNION(left,lVal); break;
2545 case VT_UI1 : lVal = V_UNION(left,bVal); break;
2546 case VT_UI2 : lVal = V_UNION(left,uiVal); break;
2547 case VT_UI4 : lVal = V_UNION(left,ulVal); break;
2548 case VT_UINT : lVal = V_UNION(left,ulVal); break;
2549 case VT_R4 : lVal = V_UNION(left,fltVal); break;
2550 case VT_R8 : lVal = V_UNION(left,dblVal); break;
2551 case VT_NULL : lVal = 0.0; break;
2552 default: lOk = FALSE;
2556 switch (V_VT(right)&VT_TYPEMASK) {
2557 case VT_I1 : rVal = V_UNION(right,cVal); break;
2558 case VT_I2 : rVal = V_UNION(right,iVal); break;
2559 case VT_I4 : rVal = V_UNION(right,lVal); break;
2560 case VT_INT : rVal = V_UNION(right,lVal); break;
2561 case VT_UI1 : rVal = V_UNION(right,bVal); break;
2562 case VT_UI2 : rVal = V_UNION(right,uiVal); break;
2563 case VT_UI4 : rVal = V_UNION(right,ulVal); break;
2564 case VT_UINT : rVal = V_UNION(right,ulVal); break;
2565 case VT_R4 : rVal = V_UNION(right,fltVal);break;
2566 case VT_R8 : rVal = V_UNION(right,dblVal);break;
2567 case VT_NULL : rVal = 0.0; break;
2568 default: rOk = FALSE;
2572 res = (lVal + rVal);
2573 V_VT(result) = VT_R8;
2574 V_UNION(result,dblVal) = res;
2577 FIXME("Unhandled type pair %d / %d in double addition.\n",
2578 (V_VT(left)&VT_TYPEMASK),
2579 (V_VT(right)&VT_TYPEMASK)
2585 /* now check if we add floats. VT_R8 can no longer happen here! */
2586 if (((V_VT(left)&VT_TYPEMASK) == VT_R4) || ((V_VT(right)&VT_TYPEMASK) == VT_R4)) {
2594 switch (V_VT(left)&VT_TYPEMASK) {
2595 case VT_I1 : lVal = V_UNION(left,cVal); break;
2596 case VT_I2 : lVal = V_UNION(left,iVal); break;
2597 case VT_I4 : lVal = V_UNION(left,lVal); break;
2598 case VT_INT : lVal = V_UNION(left,lVal); break;
2599 case VT_UI1 : lVal = V_UNION(left,bVal); break;
2600 case VT_UI2 : lVal = V_UNION(left,uiVal); break;
2601 case VT_UI4 : lVal = V_UNION(left,ulVal); break;
2602 case VT_UINT : lVal = V_UNION(left,ulVal); break;
2603 case VT_R4 : lVal = V_UNION(left,fltVal); break;
2604 case VT_NULL : lVal = 0.0; break;
2605 default: lOk = FALSE;
2609 switch (V_VT(right)&VT_TYPEMASK) {
2610 case VT_I1 : rVal = V_UNION(right,cVal); break;
2611 case VT_I2 : rVal = V_UNION(right,iVal); break;
2612 case VT_I4 : rVal = V_UNION(right,lVal); break;
2613 case VT_INT : rVal = V_UNION(right,lVal); break;
2614 case VT_UI1 : rVal = V_UNION(right,bVal); break;
2615 case VT_UI2 : rVal = V_UNION(right,uiVal); break;
2616 case VT_UI4 : rVal = V_UNION(right,ulVal); break;
2617 case VT_UINT : rVal = V_UNION(right,ulVal); break;
2618 case VT_R4 : rVal = V_UNION(right,fltVal);break;
2619 case VT_NULL : rVal = 0.0; break;
2620 default: rOk = FALSE;
2624 res = (lVal + rVal);
2625 V_VT(result) = VT_R4;
2626 V_UNION(result,fltVal) = res;
2629 FIXME("Unhandled type pair %d / %d in float addition.\n",
2630 (V_VT(left)&VT_TYPEMASK),
2631 (V_VT(right)&VT_TYPEMASK)
2637 /* Handle strings as concat */
2638 if ((V_VT(left)&VT_TYPEMASK) == VT_BSTR &&
2639 (V_VT(right)&VT_TYPEMASK) == VT_BSTR) {
2640 V_VT(result) = VT_BSTR;
2641 return VarBstrCat(V_BSTR(left), V_BSTR(right), &V_BSTR(result));
2650 int resT = 0; /* Testing has shown I2 + I2 == I2, all else
2654 switch (V_VT(left)&VT_TYPEMASK) {
2655 case VT_I1 : lVal = V_UNION(left,cVal); resT=VT_I4; break;
2656 case VT_I2 : lVal = V_UNION(left,iVal); resT=VT_I2; break;
2657 case VT_I4 : lVal = V_UNION(left,lVal); resT=VT_I4; break;
2658 case VT_INT : lVal = V_UNION(left,lVal); resT=VT_I4; break;
2659 case VT_UI1 : lVal = V_UNION(left,bVal); resT=VT_I4; break;
2660 case VT_UI2 : lVal = V_UNION(left,uiVal); resT=VT_I4; break;
2661 case VT_UI4 : lVal = V_UNION(left,ulVal); resT=VT_I4; break;
2662 case VT_UINT : lVal = V_UNION(left,ulVal); resT=VT_I4; break;
2663 case VT_NULL : lVal = 0; resT = VT_I4; break;
2664 default: lOk = FALSE;
2668 switch (V_VT(right)&VT_TYPEMASK) {
2669 case VT_I1 : rVal = V_UNION(right,cVal); resT=VT_I4; break;
2670 case VT_I2 : rVal = V_UNION(right,iVal); resT=max(VT_I2, resT); break;
2671 case VT_I4 : rVal = V_UNION(right,lVal); resT=VT_I4; break;
2672 case VT_INT : rVal = V_UNION(right,lVal); resT=VT_I4; break;
2673 case VT_UI1 : rVal = V_UNION(right,bVal); resT=VT_I4; break;
2674 case VT_UI2 : rVal = V_UNION(right,uiVal); resT=VT_I4; break;
2675 case VT_UI4 : rVal = V_UNION(right,ulVal); resT=VT_I4; break;
2676 case VT_UINT : rVal = V_UNION(right,ulVal); resT=VT_I4; break;
2677 case VT_NULL : rVal = 0; resT=VT_I4; break;
2678 default: rOk = FALSE;
2682 res = (lVal + rVal);
2683 V_VT(result) = resT;
2685 case VT_I2 : V_UNION(result,iVal) = res; break;
2686 case VT_I4 : V_UNION(result,lVal) = res; break;
2688 FIXME("Unexpected result variant type %x\n", resT);
2689 V_UNION(result,lVal) = res;
2694 FIXME("unimplemented part (0x%x + 0x%x)\n",V_VT(left), V_VT(right));
2698 TRACE("returning 0x%8lx (%s%s),%ld\n", rc, debugstr_VT(result),
2699 debugstr_VF(result), V_VT(result) == VT_I4 ? V_I4(result) : V_I2(result));
2703 /**********************************************************************
2704 * VarMul [OLEAUT32.156]
2707 HRESULT WINAPI VarMul(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2709 HRESULT rc = E_FAIL;
2710 VARTYPE lvt,rvt,resvt;
2714 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2715 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2717 VariantInit(&lv);VariantInit(&rv);
2718 lvt = V_VT(left)&VT_TYPEMASK;
2719 rvt = V_VT(right)&VT_TYPEMASK;
2720 found = FALSE;resvt=VT_VOID;
2721 if (((1<<lvt) | (1<<rvt)) & ((1<<VT_R4)|(1<<VT_R8))) {
2725 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)))) {
2730 FIXME("can't expand vt %d vs %d to a target type.\n",lvt,rvt);
2733 rc = VariantChangeType(&lv, left, 0, resvt);
2735 FIXME("Could not convert 0x%x to %d?\n",V_VT(left),resvt);
2738 rc = VariantChangeType(&rv, right, 0, resvt);
2740 FIXME("Could not convert 0x%x to %d?\n",V_VT(right),resvt);
2745 V_VT(result) = resvt;
2746 V_R8(result) = V_R8(&lv) * V_R8(&rv);
2750 V_VT(result) = resvt;
2751 V_I4(result) = V_I4(&lv) * V_I4(&rv);
2755 TRACE("returning 0x%8lx (%s%s),%g\n", rc, debugstr_VT(result),
2756 debugstr_VF(result), V_VT(result) == VT_R8 ? V_R8(result) : (double)V_I4(result));
2760 /**********************************************************************
2761 * VarDiv [OLEAUT32.143]
2764 HRESULT WINAPI VarDiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2766 HRESULT rc = E_FAIL;
2767 VARTYPE lvt,rvt,resvt;
2771 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2772 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2774 VariantInit(&lv);VariantInit(&rv);
2775 lvt = V_VT(left)&VT_TYPEMASK;
2776 rvt = V_VT(right)&VT_TYPEMASK;
2777 found = FALSE;resvt = VT_VOID;
2778 if (((1<<lvt) | (1<<rvt)) & ((1<<VT_R4)|(1<<VT_R8))) {
2782 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)))) {
2787 FIXME("can't expand vt %d vs %d to a target type.\n",lvt,rvt);
2790 rc = VariantChangeType(&lv, left, 0, resvt);
2792 FIXME("Could not convert 0x%x to %d?\n",V_VT(left),resvt);
2795 rc = VariantChangeType(&rv, right, 0, resvt);
2797 FIXME("Could not convert 0x%x to %d?\n",V_VT(right),resvt);
2802 V_VT(result) = resvt;
2803 V_R8(result) = V_R8(&lv) / V_R8(&rv);
2807 V_VT(result) = resvt;
2808 V_I4(result) = V_I4(&lv) / V_I4(&rv);
2812 TRACE("returning 0x%8lx (%s%s),%g\n", rc, debugstr_VT(result),
2813 debugstr_VF(result), V_VT(result) == VT_R8 ? V_R8(result) : (double)V_I4(result));
2817 /**********************************************************************
2818 * VarSub [OLEAUT32.159]
2821 HRESULT WINAPI VarSub(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2823 HRESULT rc = E_FAIL;
2824 VARTYPE lvt,rvt,resvt;
2828 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2829 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2831 VariantInit(&lv);VariantInit(&rv);
2832 lvt = V_VT(left)&VT_TYPEMASK;
2833 rvt = V_VT(right)&VT_TYPEMASK;
2834 found = FALSE;resvt = VT_VOID;
2835 if (((1<<lvt) | (1<<rvt)) & ((1<<VT_DATE)|(1<<VT_R4)|(1<<VT_R8))) {
2839 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)))) {
2844 FIXME("can't expand vt %d vs %d to a target type.\n",lvt,rvt);
2847 rc = VariantChangeType(&lv, left, 0, resvt);
2849 FIXME("Could not convert 0x%x to %d?\n",V_VT(left),resvt);
2852 rc = VariantChangeType(&rv, right, 0, resvt);
2854 FIXME("Could not convert 0x%x to %d?\n",V_VT(right),resvt);
2859 V_VT(result) = resvt;
2860 V_R8(result) = V_R8(&lv) - V_R8(&rv);
2864 V_VT(result) = resvt;
2865 V_I4(result) = V_I4(&lv) - V_I4(&rv);
2869 TRACE("returning 0x%8lx (%s%s),%g\n", rc, debugstr_VT(result),
2870 debugstr_VF(result), V_VT(result) == VT_R8 ? V_R8(result) : (double)V_I4(result));
2874 /**********************************************************************
2875 * VarOr [OLEAUT32.157]
2878 HRESULT WINAPI VarOr(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2880 HRESULT rc = E_FAIL;
2882 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2883 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2885 if ((V_VT(left)&VT_TYPEMASK) == VT_BOOL &&
2886 (V_VT(right)&VT_TYPEMASK) == VT_BOOL) {
2888 V_VT(result) = VT_BOOL;
2889 if (V_BOOL(left) || V_BOOL(right)) {
2890 V_BOOL(result) = VARIANT_TRUE;
2892 V_BOOL(result) = VARIANT_FALSE;
2903 int resT = 0; /* Testing has shown I2 & I2 == I2, all else
2904 becomes I4, even unsigned ints (incl. UI2) */
2907 switch (V_VT(left)&VT_TYPEMASK) {
2908 case VT_I1 : lVal = V_UNION(left,cVal); resT=VT_I4; break;
2909 case VT_I2 : lVal = V_UNION(left,iVal); resT=VT_I2; break;
2910 case VT_I4 : lVal = V_UNION(left,lVal); resT=VT_I4; break;
2911 case VT_INT : lVal = V_UNION(left,lVal); resT=VT_I4; break;
2912 case VT_UI1 : lVal = V_UNION(left,bVal); resT=VT_I4; break;
2913 case VT_UI2 : lVal = V_UNION(left,uiVal); resT=VT_I4; break;
2914 case VT_UI4 : lVal = V_UNION(left,ulVal); resT=VT_I4; break;
2915 case VT_UINT : lVal = V_UNION(left,ulVal); resT=VT_I4; break;
2916 default: lOk = FALSE;
2920 switch (V_VT(right)&VT_TYPEMASK) {
2921 case VT_I1 : rVal = V_UNION(right,cVal); resT=VT_I4; break;
2922 case VT_I2 : rVal = V_UNION(right,iVal); resT=max(VT_I2, resT); break;
2923 case VT_I4 : rVal = V_UNION(right,lVal); resT=VT_I4; break;
2924 case VT_INT : rVal = V_UNION(right,lVal); resT=VT_I4; break;
2925 case VT_UI1 : rVal = V_UNION(right,bVal); resT=VT_I4; break;
2926 case VT_UI2 : rVal = V_UNION(right,uiVal); resT=VT_I4; break;
2927 case VT_UI4 : rVal = V_UNION(right,ulVal); resT=VT_I4; break;
2928 case VT_UINT : rVal = V_UNION(right,ulVal); resT=VT_I4; break;
2929 default: rOk = FALSE;
2933 res = (lVal | rVal);
2934 V_VT(result) = resT;
2936 case VT_I2 : V_UNION(result,iVal) = res; break;
2937 case VT_I4 : V_UNION(result,lVal) = res; break;
2939 FIXME("Unexpected result variant type %x\n", resT);
2940 V_UNION(result,lVal) = res;
2945 FIXME("unimplemented part\n");
2949 TRACE("returning 0x%8lx (%s%s),%ld\n", rc, debugstr_VT(result),
2950 debugstr_VF(result), V_VT(result) == VT_I4 ? V_I4(result) : V_I2(result));
2954 /**********************************************************************
2955 * VarAbs [OLEAUT32.168]
2957 * Convert a variant to its absolute value.
2960 * pVarIn [I] Source variant
2961 * pVarOut [O] Destination for converted value
2964 * Success: S_OK. pVarOut contains the absolute value of pVarIn.
2965 * Failure: An HRESULT error code indicating the error.
2968 * - This function does not process by-reference variants.
2969 * - The type of the value stored in pVarOut depends on the type of pVarIn,
2970 * according to the following table:
2971 *| Input Type Output Type
2972 *| ---------- -----------
2975 *| (All others) Unchanged
2977 HRESULT WINAPI VarAbs(LPVARIANT pVarIn, LPVARIANT pVarOut)
2980 HRESULT hRet = S_OK;
2982 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
2983 debugstr_VF(pVarIn), pVarOut);
2985 if (V_ISARRAY(pVarIn) || V_VT(pVarIn) == VT_UNKNOWN ||
2986 V_VT(pVarIn) == VT_DISPATCH || V_VT(pVarIn) == VT_RECORD ||
2987 V_VT(pVarIn) == VT_ERROR)
2988 return DISP_E_TYPEMISMATCH;
2990 *pVarOut = *pVarIn; /* Shallow copy the value, and invert it if needed */
2992 #define ABS_CASE(typ,min) \
2993 case VT_##typ: if (V_##typ(pVarIn) == min) hRet = DISP_E_OVERFLOW; \
2994 else if (V_##typ(pVarIn) < 0) V_##typ(pVarOut) = -V_##typ(pVarIn); \
2997 switch (V_VT(pVarIn))
2999 ABS_CASE(I1,I1_MIN);
3001 V_VT(pVarOut) = VT_I2;
3002 /* BOOL->I2, Fall through ... */
3003 ABS_CASE(I2,I2_MIN);
3005 ABS_CASE(I4,I4_MIN);
3006 ABS_CASE(I8,I8_MIN);
3007 ABS_CASE(R4,R4_MIN);
3009 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
3012 V_VT(pVarOut) = VT_R8;
3014 /* Fall through ... */
3016 ABS_CASE(R8,R8_MIN);
3018 hRet = VarCyAbs(V_CY(pVarIn), & V_CY(pVarOut));
3021 DEC_SIGN(&V_DECIMAL(pVarOut)) &= ~DECIMAL_NEG;
3033 hRet = DISP_E_BADVARTYPE;
3039 /**********************************************************************
3040 * VarNot [OLEAUT32.174]
3042 * Perform a not operation on a variant.
3045 * pVarIn [I] Source variant
3046 * pVarOut [O] Destination for converted value
3049 * Success: S_OK. pVarOut contains the converted value.
3050 * Failure: An HRESULT error code indicating the error.
3053 * - Strictly speaking, this function performs a bitwise ones compliment
3054 * on the variants value (after possibly converting to VT_I4, see below).
3055 * This only behaves like a boolean not operation if the value in
3056 * pVarIn is either VARIANT_TRUE or VARIANT_FALSE and the type is signed.
3057 * - To perform a genuine not operation, convert the variant to a VT_BOOL
3058 * before calling this function.
3059 * - This function does not process by-reference variants.
3060 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3061 * according to the following table:
3062 *| Input Type Output Type
3063 *| ---------- -----------
3069 *| (All others) Unchanged
3071 HRESULT WINAPI VarNot(LPVARIANT pVarIn, LPVARIANT pVarOut)
3074 HRESULT hRet = S_OK;
3076 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
3077 debugstr_VF(pVarIn), pVarOut);
3079 V_VT(pVarOut) = V_VT(pVarIn);
3081 switch (V_VT(pVarIn))
3083 case VT_I1: V_I1(pVarOut) = ~V_I1(pVarIn); break;
3084 case VT_UI1: V_UI1(pVarOut) = ~V_UI1(pVarIn); break;
3086 case VT_I2: V_I2(pVarOut) = ~V_I2(pVarIn); break;
3087 case VT_UI2: V_UI2(pVarOut) = ~V_UI2(pVarIn); break;
3089 hRet = VarI4FromDec(&V_DECIMAL(pVarIn), &V_I4(&varIn));
3093 V_VT(pVarOut) = VT_I4;
3094 /* Fall through ... */
3096 case VT_I4: V_I4(pVarOut) = ~V_I4(pVarIn); break;
3098 case VT_UI4: V_UI4(pVarOut) = ~V_UI4(pVarIn); break;
3099 case VT_I8: V_I8(pVarOut) = ~V_I8(pVarIn); break;
3100 case VT_UI8: V_UI8(pVarOut) = ~V_UI8(pVarIn); break;
3102 hRet = VarI4FromR4(V_R4(pVarIn), &V_I4(pVarOut));
3103 V_I4(pVarOut) = ~V_I4(pVarOut);
3104 V_VT(pVarOut) = VT_I4;
3107 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
3111 /* Fall through ... */
3114 hRet = VarI4FromR8(V_R8(pVarIn), &V_I4(pVarOut));
3115 V_I4(pVarOut) = ~V_I4(pVarOut);
3116 V_VT(pVarOut) = VT_I4;
3126 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
3127 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
3128 hRet = DISP_E_BADVARTYPE;
3130 hRet = DISP_E_TYPEMISMATCH;
3133 V_VT(pVarOut) = VT_EMPTY;
3138 /**********************************************************************
3139 * VarMod [OLEAUT32.154]
3142 HRESULT WINAPI VarMod(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
3144 FIXME("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
3145 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
3146 debugstr_VF(pVarRight), pVarOut);
3150 /**********************************************************************
3151 * VarPow [OLEAUT32.158]
3154 HRESULT WINAPI VarPow(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3159 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left), debugstr_VF(left),
3160 right, debugstr_VT(right), debugstr_VF(right), result);
3162 hr = VariantChangeType(&dl,left,0,VT_R8);
3163 if (!SUCCEEDED(hr)) {
3164 ERR("Could not change passed left argument to VT_R8, handle it differently.\n");
3167 hr = VariantChangeType(&dr,right,0,VT_R8);
3168 if (!SUCCEEDED(hr)) {
3169 ERR("Could not change passed right argument to VT_R8, handle it differently.\n");
3172 V_VT(result) = VT_R8;
3173 V_R8(result) = pow(V_R8(&dl),V_R8(&dr));
3177 /**********************************************************************
3178 * VarInt [OLEAUT32.172]
3181 HRESULT WINAPI VarInt(LPVARIANT var, LPVARIANT result)
3183 TRACE("(%p->(%s%s),%p)\n", var, debugstr_VT(var), debugstr_VF(var), result);
3187 V_VT(result) = VT_I4;
3188 V_I4(result) = floor(V_R4(var));
3191 V_VT(result) = VT_I4;
3192 V_I4(result) = floor(V_R8(var));
3195 FIXME("Unhandled variant type 0x%x\n", V_VT(var));
3196 return DISP_E_TYPEMISMATCH;