4 * Copyright 1998 Jean-Claude Cote
5 * Copyright 2003 Jon Griffiths
6 * Copyright 2005 Daniel Remenak
8 * The alorithm for conversion from Julian days to day/month/year is based on
9 * that devised by Henry Fliegel, as implemented in PostgreSQL, which is
10 * Copyright 1994-7 Regents of the University of California
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
39 #include "wine/unicode.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(variant
);
46 const char* wine_vtypes
[VT_CLSID
+1] =
48 "VT_EMPTY","VT_NULL","VT_I2","VT_I4","VT_R4","VT_R8","VT_CY","VT_DATE",
49 "VT_BSTR","VT_DISPATCH","VT_ERROR","VT_BOOL","VT_VARIANT","VT_UNKNOWN",
50 "VT_DECIMAL","15","VT_I1","VT_UI1","VT_UI2","VT_UI4","VT_I8","VT_UI8",
51 "VT_INT","VT_UINT","VT_VOID","VT_HRESULT","VT_PTR","VT_SAFEARRAY",
52 "VT_CARRAY","VT_USERDEFINED","VT_LPSTR","VT_LPWSTR","32","33","34","35",
53 "VT_RECORD","VT_INT_PTR","VT_UINT_PTR","39","40","41","42","43","44","45",
54 "46","47","48","49","50","51","52","53","54","55","56","57","58","59","60",
55 "61","62","63","VT_FILETIME","VT_BLOB","VT_STREAM","VT_STORAGE",
56 "VT_STREAMED_OBJECT","VT_STORED_OBJECT","VT_BLOB_OBJECT","VT_CF","VT_CLSID"
59 const char* wine_vflags
[16] =
64 "|VT_VECTOR|VT_ARRAY",
66 "|VT_VECTOR|VT_ARRAY",
68 "|VT_VECTOR|VT_ARRAY|VT_BYREF",
70 "|VT_VECTOR|VT_HARDTYPE",
71 "|VT_ARRAY|VT_HARDTYPE",
72 "|VT_VECTOR|VT_ARRAY|VT_HARDTYPE",
73 "|VT_BYREF|VT_HARDTYPE",
74 "|VT_VECTOR|VT_ARRAY|VT_HARDTYPE",
75 "|VT_ARRAY|VT_BYREF|VT_HARDTYPE",
76 "|VT_VECTOR|VT_ARRAY|VT_BYREF|VT_HARDTYPE",
79 /* Convert a variant from one type to another */
80 static inline HRESULT
VARIANT_Coerce(VARIANTARG
* pd
, LCID lcid
, USHORT wFlags
,
81 VARIANTARG
* ps
, VARTYPE vt
)
83 HRESULT res
= DISP_E_TYPEMISMATCH
;
84 VARTYPE vtFrom
= V_TYPE(ps
);
87 TRACE("(%p->(%s%s),0x%08lx,0x%04x,%p->(%s%s),%s%s)\n", pd
, debugstr_VT(pd
),
88 debugstr_VF(pd
), lcid
, wFlags
, ps
, debugstr_VT(ps
), debugstr_VF(ps
),
89 debugstr_vt(vt
), debugstr_vf(vt
));
91 if (vt
== VT_BSTR
|| vtFrom
== VT_BSTR
)
93 /* All flags passed to low level function are only used for
94 * changing to or from strings. Map these here.
96 if (wFlags
& VARIANT_LOCALBOOL
)
97 dwFlags
|= VAR_LOCALBOOL
;
98 if (wFlags
& VARIANT_CALENDAR_HIJRI
)
99 dwFlags
|= VAR_CALENDAR_HIJRI
;
100 if (wFlags
& VARIANT_CALENDAR_THAI
)
101 dwFlags
|= VAR_CALENDAR_THAI
;
102 if (wFlags
& VARIANT_CALENDAR_GREGORIAN
)
103 dwFlags
|= VAR_CALENDAR_GREGORIAN
;
104 if (wFlags
& VARIANT_NOUSEROVERRIDE
)
105 dwFlags
|= LOCALE_NOUSEROVERRIDE
;
106 if (wFlags
& VARIANT_USE_NLS
)
107 dwFlags
|= LOCALE_USE_NLS
;
110 /* Map int/uint to i4/ui4 */
113 else if (vt
== VT_UINT
)
116 if (vtFrom
== VT_INT
)
118 else if (vtFrom
== VT_UINT
)
122 return VariantCopy(pd
, ps
);
124 if (wFlags
& VARIANT_NOVALUEPROP
&& vtFrom
== VT_DISPATCH
&& vt
!= VT_UNKNOWN
)
126 /* VARIANT_NOVALUEPROP prevents IDispatch objects from being coerced by
127 * accessing the default object property.
129 return DISP_E_TYPEMISMATCH
;
135 if (vtFrom
== VT_NULL
)
136 return DISP_E_TYPEMISMATCH
;
137 /* ... Fall through */
139 if (vtFrom
<= VT_UINT
&& vtFrom
!= (VARTYPE
)15 && vtFrom
!= VT_ERROR
)
141 res
= VariantClear( pd
);
142 if (vt
== VT_NULL
&& SUCCEEDED(res
))
150 case VT_EMPTY
: V_I1(pd
) = 0; return S_OK
;
151 case VT_I2
: return VarI1FromI2(V_I2(ps
), &V_I1(pd
));
152 case VT_I4
: return VarI1FromI4(V_I4(ps
), &V_I1(pd
));
153 case VT_UI1
: V_I1(pd
) = V_UI1(ps
); return S_OK
;
154 case VT_UI2
: return VarI1FromUI2(V_UI2(ps
), &V_I1(pd
));
155 case VT_UI4
: return VarI1FromUI4(V_UI4(ps
), &V_I1(pd
));
156 case VT_I8
: return VarI1FromI8(V_I8(ps
), &V_I1(pd
));
157 case VT_UI8
: return VarI1FromUI8(V_UI8(ps
), &V_I1(pd
));
158 case VT_R4
: return VarI1FromR4(V_R4(ps
), &V_I1(pd
));
159 case VT_R8
: return VarI1FromR8(V_R8(ps
), &V_I1(pd
));
160 case VT_DATE
: return VarI1FromDate(V_DATE(ps
), &V_I1(pd
));
161 case VT_BOOL
: return VarI1FromBool(V_BOOL(ps
), &V_I1(pd
));
162 case VT_CY
: return VarI1FromCy(V_CY(ps
), &V_I1(pd
));
163 case VT_DECIMAL
: return VarI1FromDec(&V_DECIMAL(ps
), &V_I1(pd
) );
164 case VT_DISPATCH
: return VarI1FromDisp(V_DISPATCH(ps
), lcid
, &V_I1(pd
) );
165 case VT_BSTR
: return VarI1FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I1(pd
) );
172 case VT_EMPTY
: V_I2(pd
) = 0; return S_OK
;
173 case VT_I1
: return VarI2FromI1(V_I1(ps
), &V_I2(pd
));
174 case VT_I4
: return VarI2FromI4(V_I4(ps
), &V_I2(pd
));
175 case VT_UI1
: return VarI2FromUI1(V_UI1(ps
), &V_I2(pd
));
176 case VT_UI2
: V_I2(pd
) = V_UI2(ps
); return S_OK
;
177 case VT_UI4
: return VarI2FromUI4(V_UI4(ps
), &V_I2(pd
));
178 case VT_I8
: return VarI2FromI8(V_I8(ps
), &V_I2(pd
));
179 case VT_UI8
: return VarI2FromUI8(V_UI8(ps
), &V_I2(pd
));
180 case VT_R4
: return VarI2FromR4(V_R4(ps
), &V_I2(pd
));
181 case VT_R8
: return VarI2FromR8(V_R8(ps
), &V_I2(pd
));
182 case VT_DATE
: return VarI2FromDate(V_DATE(ps
), &V_I2(pd
));
183 case VT_BOOL
: return VarI2FromBool(V_BOOL(ps
), &V_I2(pd
));
184 case VT_CY
: return VarI2FromCy(V_CY(ps
), &V_I2(pd
));
185 case VT_DECIMAL
: return VarI2FromDec(&V_DECIMAL(ps
), &V_I2(pd
));
186 case VT_DISPATCH
: return VarI2FromDisp(V_DISPATCH(ps
), lcid
, &V_I2(pd
));
187 case VT_BSTR
: return VarI2FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I2(pd
));
194 case VT_EMPTY
: V_I4(pd
) = 0; return S_OK
;
195 case VT_I1
: return VarI4FromI1(V_I1(ps
), &V_I4(pd
));
196 case VT_I2
: return VarI4FromI2(V_I2(ps
), &V_I4(pd
));
197 case VT_UI1
: return VarI4FromUI1(V_UI1(ps
), &V_I4(pd
));
198 case VT_UI2
: return VarI4FromUI2(V_UI2(ps
), &V_I4(pd
));
199 case VT_UI4
: V_I4(pd
) = V_UI4(ps
); return S_OK
;
200 case VT_I8
: return VarI4FromI8(V_I8(ps
), &V_I4(pd
));
201 case VT_UI8
: return VarI4FromUI8(V_UI8(ps
), &V_I4(pd
));
202 case VT_R4
: return VarI4FromR4(V_R4(ps
), &V_I4(pd
));
203 case VT_R8
: return VarI4FromR8(V_R8(ps
), &V_I4(pd
));
204 case VT_DATE
: return VarI4FromDate(V_DATE(ps
), &V_I4(pd
));
205 case VT_BOOL
: return VarI4FromBool(V_BOOL(ps
), &V_I4(pd
));
206 case VT_CY
: return VarI4FromCy(V_CY(ps
), &V_I4(pd
));
207 case VT_DECIMAL
: return VarI4FromDec(&V_DECIMAL(ps
), &V_I4(pd
));
208 case VT_DISPATCH
: return VarI4FromDisp(V_DISPATCH(ps
), lcid
, &V_I4(pd
));
209 case VT_BSTR
: return VarI4FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I4(pd
));
216 case VT_EMPTY
: V_UI1(pd
) = 0; return S_OK
;
217 case VT_I1
: V_UI1(pd
) = V_I1(ps
); return S_OK
;
218 case VT_I2
: return VarUI1FromI2(V_I2(ps
), &V_UI1(pd
));
219 case VT_I4
: return VarUI1FromI4(V_I4(ps
), &V_UI1(pd
));
220 case VT_UI2
: return VarUI1FromUI2(V_UI2(ps
), &V_UI1(pd
));
221 case VT_UI4
: return VarUI1FromUI4(V_UI4(ps
), &V_UI1(pd
));
222 case VT_I8
: return VarUI1FromI8(V_I8(ps
), &V_UI1(pd
));
223 case VT_UI8
: return VarUI1FromUI8(V_UI8(ps
), &V_UI1(pd
));
224 case VT_R4
: return VarUI1FromR4(V_R4(ps
), &V_UI1(pd
));
225 case VT_R8
: return VarUI1FromR8(V_R8(ps
), &V_UI1(pd
));
226 case VT_DATE
: return VarUI1FromDate(V_DATE(ps
), &V_UI1(pd
));
227 case VT_BOOL
: return VarUI1FromBool(V_BOOL(ps
), &V_UI1(pd
));
228 case VT_CY
: return VarUI1FromCy(V_CY(ps
), &V_UI1(pd
));
229 case VT_DECIMAL
: return VarUI1FromDec(&V_DECIMAL(ps
), &V_UI1(pd
));
230 case VT_DISPATCH
: return VarUI1FromDisp(V_DISPATCH(ps
), lcid
, &V_UI1(pd
));
231 case VT_BSTR
: return VarUI1FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI1(pd
));
238 case VT_EMPTY
: V_UI2(pd
) = 0; return S_OK
;
239 case VT_I1
: return VarUI2FromI1(V_I1(ps
), &V_UI2(pd
));
240 case VT_I2
: V_UI2(pd
) = V_I2(ps
); return S_OK
;
241 case VT_I4
: return VarUI2FromI4(V_I4(ps
), &V_UI2(pd
));
242 case VT_UI1
: return VarUI2FromUI1(V_UI1(ps
), &V_UI2(pd
));
243 case VT_UI4
: return VarUI2FromUI4(V_UI4(ps
), &V_UI2(pd
));
244 case VT_I8
: return VarUI4FromI8(V_I8(ps
), &V_UI4(pd
));
245 case VT_UI8
: return VarUI4FromUI8(V_UI8(ps
), &V_UI4(pd
));
246 case VT_R4
: return VarUI2FromR4(V_R4(ps
), &V_UI2(pd
));
247 case VT_R8
: return VarUI2FromR8(V_R8(ps
), &V_UI2(pd
));
248 case VT_DATE
: return VarUI2FromDate(V_DATE(ps
), &V_UI2(pd
));
249 case VT_BOOL
: return VarUI2FromBool(V_BOOL(ps
), &V_UI2(pd
));
250 case VT_CY
: return VarUI2FromCy(V_CY(ps
), &V_UI2(pd
));
251 case VT_DECIMAL
: return VarUI2FromDec(&V_DECIMAL(ps
), &V_UI2(pd
));
252 case VT_DISPATCH
: return VarUI2FromDisp(V_DISPATCH(ps
), lcid
, &V_UI2(pd
));
253 case VT_BSTR
: return VarUI2FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI2(pd
));
260 case VT_EMPTY
: V_UI4(pd
) = 0; return S_OK
;
261 case VT_I1
: return VarUI4FromI1(V_I1(ps
), &V_UI4(pd
));
262 case VT_I2
: return VarUI4FromI2(V_I2(ps
), &V_UI4(pd
));
263 case VT_I4
: V_UI4(pd
) = V_I4(ps
); return S_OK
;
264 case VT_UI1
: return VarUI4FromUI1(V_UI1(ps
), &V_UI4(pd
));
265 case VT_UI2
: return VarUI4FromUI2(V_UI2(ps
), &V_UI4(pd
));
266 case VT_I8
: return VarUI4FromI8(V_I8(ps
), &V_UI4(pd
));
267 case VT_UI8
: return VarUI4FromUI8(V_UI8(ps
), &V_UI4(pd
));
268 case VT_R4
: return VarUI4FromR4(V_R4(ps
), &V_UI4(pd
));
269 case VT_R8
: return VarUI4FromR8(V_R8(ps
), &V_UI4(pd
));
270 case VT_DATE
: return VarUI4FromDate(V_DATE(ps
), &V_UI4(pd
));
271 case VT_BOOL
: return VarUI4FromBool(V_BOOL(ps
), &V_UI4(pd
));
272 case VT_CY
: return VarUI4FromCy(V_CY(ps
), &V_UI4(pd
));
273 case VT_DECIMAL
: return VarUI4FromDec(&V_DECIMAL(ps
), &V_UI4(pd
));
274 case VT_DISPATCH
: return VarUI4FromDisp(V_DISPATCH(ps
), lcid
, &V_UI4(pd
));
275 case VT_BSTR
: return VarUI4FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI4(pd
));
282 case VT_EMPTY
: V_UI8(pd
) = 0; return S_OK
;
283 case VT_I4
: if (V_I4(ps
) < 0) return DISP_E_OVERFLOW
; V_UI8(pd
) = V_I4(ps
); return S_OK
;
284 case VT_I1
: return VarUI8FromI1(V_I1(ps
), &V_UI8(pd
));
285 case VT_I2
: return VarUI8FromI2(V_I2(ps
), &V_UI8(pd
));
286 case VT_UI1
: return VarUI8FromUI1(V_UI1(ps
), &V_UI8(pd
));
287 case VT_UI2
: return VarUI8FromUI2(V_UI2(ps
), &V_UI8(pd
));
288 case VT_UI4
: return VarUI8FromUI4(V_UI4(ps
), &V_UI8(pd
));
289 case VT_I8
: V_UI8(pd
) = V_I8(ps
); return S_OK
;
290 case VT_R4
: return VarUI8FromR4(V_R4(ps
), &V_UI8(pd
));
291 case VT_R8
: return VarUI8FromR8(V_R8(ps
), &V_UI8(pd
));
292 case VT_DATE
: return VarUI8FromDate(V_DATE(ps
), &V_UI8(pd
));
293 case VT_BOOL
: return VarUI8FromBool(V_BOOL(ps
), &V_UI8(pd
));
294 case VT_CY
: return VarUI8FromCy(V_CY(ps
), &V_UI8(pd
));
295 case VT_DECIMAL
: return VarUI8FromDec(&V_DECIMAL(ps
), &V_UI8(pd
));
296 case VT_DISPATCH
: return VarUI8FromDisp(V_DISPATCH(ps
), lcid
, &V_UI8(pd
));
297 case VT_BSTR
: return VarUI8FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI8(pd
));
304 case VT_EMPTY
: V_I8(pd
) = 0; return S_OK
;
305 case VT_I4
: V_I8(pd
) = V_I4(ps
); return S_OK
;
306 case VT_I1
: return VarI8FromI1(V_I1(ps
), &V_I8(pd
));
307 case VT_I2
: return VarI8FromI2(V_I2(ps
), &V_I8(pd
));
308 case VT_UI1
: return VarI8FromUI1(V_UI1(ps
), &V_I8(pd
));
309 case VT_UI2
: return VarI8FromUI2(V_UI2(ps
), &V_I8(pd
));
310 case VT_UI4
: return VarI8FromUI4(V_UI4(ps
), &V_I8(pd
));
311 case VT_UI8
: V_I8(pd
) = V_UI8(ps
); return S_OK
;
312 case VT_R4
: return VarI8FromR4(V_R4(ps
), &V_I8(pd
));
313 case VT_R8
: return VarI8FromR8(V_R8(ps
), &V_I8(pd
));
314 case VT_DATE
: return VarI8FromDate(V_DATE(ps
), &V_I8(pd
));
315 case VT_BOOL
: return VarI8FromBool(V_BOOL(ps
), &V_I8(pd
));
316 case VT_CY
: return VarI8FromCy(V_CY(ps
), &V_I8(pd
));
317 case VT_DECIMAL
: return VarI8FromDec(&V_DECIMAL(ps
), &V_I8(pd
));
318 case VT_DISPATCH
: return VarI8FromDisp(V_DISPATCH(ps
), lcid
, &V_I8(pd
));
319 case VT_BSTR
: return VarI8FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I8(pd
));
326 case VT_EMPTY
: V_R4(pd
) = 0.0f
; return S_OK
;
327 case VT_I1
: return VarR4FromI1(V_I1(ps
), &V_R4(pd
));
328 case VT_I2
: return VarR4FromI2(V_I2(ps
), &V_R4(pd
));
329 case VT_I4
: return VarR4FromI4(V_I4(ps
), &V_R4(pd
));
330 case VT_UI1
: return VarR4FromUI1(V_UI1(ps
), &V_R4(pd
));
331 case VT_UI2
: return VarR4FromUI2(V_UI2(ps
), &V_R4(pd
));
332 case VT_UI4
: return VarR4FromUI4(V_UI4(ps
), &V_R4(pd
));
333 case VT_I8
: return VarR4FromI8(V_I8(ps
), &V_R4(pd
));
334 case VT_UI8
: return VarR4FromUI8(V_UI8(ps
), &V_R4(pd
));
335 case VT_R8
: return VarR4FromR8(V_R8(ps
), &V_R4(pd
));
336 case VT_DATE
: return VarR4FromDate(V_DATE(ps
), &V_R4(pd
));
337 case VT_BOOL
: return VarR4FromBool(V_BOOL(ps
), &V_R4(pd
));
338 case VT_CY
: return VarR4FromCy(V_CY(ps
), &V_R4(pd
));
339 case VT_DECIMAL
: return VarR4FromDec(&V_DECIMAL(ps
), &V_R4(pd
));
340 case VT_DISPATCH
: return VarR4FromDisp(V_DISPATCH(ps
), lcid
, &V_R4(pd
));
341 case VT_BSTR
: return VarR4FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_R4(pd
));
348 case VT_EMPTY
: V_R8(pd
) = 0.0; return S_OK
;
349 case VT_I1
: return VarR8FromI1(V_I1(ps
), &V_R8(pd
));
350 case VT_I2
: return VarR8FromI2(V_I2(ps
), &V_R8(pd
));
351 case VT_I4
: return VarR8FromI4(V_I4(ps
), &V_R8(pd
));
352 case VT_UI1
: return VarR8FromUI1(V_UI1(ps
), &V_R8(pd
));
353 case VT_UI2
: return VarR8FromUI2(V_UI2(ps
), &V_R8(pd
));
354 case VT_UI4
: return VarR8FromUI4(V_UI4(ps
), &V_R8(pd
));
355 case VT_I8
: return VarR8FromI8(V_I8(ps
), &V_R8(pd
));
356 case VT_UI8
: return VarR8FromUI8(V_UI8(ps
), &V_R8(pd
));
357 case VT_R4
: return VarR8FromR4(V_R4(ps
), &V_R8(pd
));
358 case VT_DATE
: return VarR8FromDate(V_DATE(ps
), &V_R8(pd
));
359 case VT_BOOL
: return VarR8FromBool(V_BOOL(ps
), &V_R8(pd
));
360 case VT_CY
: return VarR8FromCy(V_CY(ps
), &V_R8(pd
));
361 case VT_DECIMAL
: return VarR8FromDec(&V_DECIMAL(ps
), &V_R8(pd
));
362 case VT_DISPATCH
: return VarR8FromDisp(V_DISPATCH(ps
), lcid
, &V_R8(pd
));
363 case VT_BSTR
: return VarR8FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_R8(pd
));
370 case VT_EMPTY
: V_DATE(pd
) = 0.0; return S_OK
;
371 case VT_I1
: return VarDateFromI1(V_I1(ps
), &V_DATE(pd
));
372 case VT_I2
: return VarDateFromI2(V_I2(ps
), &V_DATE(pd
));
373 case VT_I4
: return VarDateFromI4(V_I4(ps
), &V_DATE(pd
));
374 case VT_UI1
: return VarDateFromUI1(V_UI1(ps
), &V_DATE(pd
));
375 case VT_UI2
: return VarDateFromUI2(V_UI2(ps
), &V_DATE(pd
));
376 case VT_UI4
: return VarDateFromUI4(V_UI4(ps
), &V_DATE(pd
));
377 case VT_I8
: return VarDateFromI8(V_I8(ps
), &V_DATE(pd
));
378 case VT_UI8
: return VarDateFromUI8(V_UI8(ps
), &V_DATE(pd
));
379 case VT_R4
: return VarDateFromR4(V_R4(ps
), &V_DATE(pd
));
380 case VT_R8
: return VarDateFromR8(V_R8(ps
), &V_DATE(pd
));
381 case VT_BOOL
: return VarDateFromBool(V_BOOL(ps
), &V_DATE(pd
));
382 case VT_CY
: return VarDateFromCy(V_CY(ps
), &V_DATE(pd
));
383 case VT_DECIMAL
: return VarDateFromDec(&V_DECIMAL(ps
), &V_DATE(pd
));
384 case VT_DISPATCH
: return VarDateFromDisp(V_DISPATCH(ps
), lcid
, &V_DATE(pd
));
385 case VT_BSTR
: return VarDateFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_DATE(pd
));
392 case VT_EMPTY
: V_BOOL(pd
) = 0; return S_OK
;
393 case VT_I1
: return VarBoolFromI1(V_I1(ps
), &V_BOOL(pd
));
394 case VT_I2
: return VarBoolFromI2(V_I2(ps
), &V_BOOL(pd
));
395 case VT_I4
: return VarBoolFromI4(V_I4(ps
), &V_BOOL(pd
));
396 case VT_UI1
: return VarBoolFromUI1(V_UI1(ps
), &V_BOOL(pd
));
397 case VT_UI2
: return VarBoolFromUI2(V_UI2(ps
), &V_BOOL(pd
));
398 case VT_UI4
: return VarBoolFromUI4(V_UI4(ps
), &V_BOOL(pd
));
399 case VT_I8
: return VarBoolFromI8(V_I8(ps
), &V_BOOL(pd
));
400 case VT_UI8
: return VarBoolFromUI8(V_UI8(ps
), &V_BOOL(pd
));
401 case VT_R4
: return VarBoolFromR4(V_R4(ps
), &V_BOOL(pd
));
402 case VT_R8
: return VarBoolFromR8(V_R8(ps
), &V_BOOL(pd
));
403 case VT_DATE
: return VarBoolFromDate(V_DATE(ps
), &V_BOOL(pd
));
404 case VT_CY
: return VarBoolFromCy(V_CY(ps
), &V_BOOL(pd
));
405 case VT_DECIMAL
: return VarBoolFromDec(&V_DECIMAL(ps
), &V_BOOL(pd
));
406 case VT_DISPATCH
: return VarBoolFromDisp(V_DISPATCH(ps
), lcid
, &V_BOOL(pd
));
407 case VT_BSTR
: return VarBoolFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_BOOL(pd
));
415 V_BSTR(pd
) = SysAllocStringLen(NULL
, 0);
416 return V_BSTR(pd
) ? S_OK
: E_OUTOFMEMORY
;
418 if (wFlags
& (VARIANT_ALPHABOOL
|VARIANT_LOCALBOOL
))
419 return VarBstrFromBool(V_BOOL(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
420 return VarBstrFromI2(V_BOOL(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
421 case VT_I1
: return VarBstrFromI1(V_I1(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
422 case VT_I2
: return VarBstrFromI2(V_I2(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
423 case VT_I4
: return VarBstrFromI4(V_I4(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
424 case VT_UI1
: return VarBstrFromUI1(V_UI1(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
425 case VT_UI2
: return VarBstrFromUI2(V_UI2(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
426 case VT_UI4
: return VarBstrFromUI4(V_UI4(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
427 case VT_I8
: return VarBstrFromI8(V_I8(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
428 case VT_UI8
: return VarBstrFromUI8(V_UI8(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
429 case VT_R4
: return VarBstrFromR4(V_R4(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
430 case VT_R8
: return VarBstrFromR8(V_R8(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
431 case VT_DATE
: return VarBstrFromDate(V_DATE(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
432 case VT_CY
: return VarBstrFromCy(V_CY(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
433 case VT_DECIMAL
: return VarBstrFromDec(&V_DECIMAL(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
434 case VT_DISPATCH
: return VarBstrFromDisp(V_DISPATCH(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
441 case VT_EMPTY
: V_CY(pd
).int64
= 0; return S_OK
;
442 case VT_I1
: return VarCyFromI1(V_I1(ps
), &V_CY(pd
));
443 case VT_I2
: return VarCyFromI2(V_I2(ps
), &V_CY(pd
));
444 case VT_I4
: return VarCyFromI4(V_I4(ps
), &V_CY(pd
));
445 case VT_UI1
: return VarCyFromUI1(V_UI1(ps
), &V_CY(pd
));
446 case VT_UI2
: return VarCyFromUI2(V_UI2(ps
), &V_CY(pd
));
447 case VT_UI4
: return VarCyFromUI4(V_UI4(ps
), &V_CY(pd
));
448 case VT_I8
: return VarCyFromI8(V_I8(ps
), &V_CY(pd
));
449 case VT_UI8
: return VarCyFromUI8(V_UI8(ps
), &V_CY(pd
));
450 case VT_R4
: return VarCyFromR4(V_R4(ps
), &V_CY(pd
));
451 case VT_R8
: return VarCyFromR8(V_R8(ps
), &V_CY(pd
));
452 case VT_DATE
: return VarCyFromDate(V_DATE(ps
), &V_CY(pd
));
453 case VT_BOOL
: return VarCyFromBool(V_BOOL(ps
), &V_CY(pd
));
454 case VT_DECIMAL
: return VarCyFromDec(&V_DECIMAL(ps
), &V_CY(pd
));
455 case VT_DISPATCH
: return VarCyFromDisp(V_DISPATCH(ps
), lcid
, &V_CY(pd
));
456 case VT_BSTR
: return VarCyFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_CY(pd
));
465 DEC_SIGNSCALE(&V_DECIMAL(pd
)) = SIGNSCALE(DECIMAL_POS
,0);
466 DEC_HI32(&V_DECIMAL(pd
)) = 0;
467 DEC_MID32(&V_DECIMAL(pd
)) = 0;
468 /* VarDecFromBool() coerces to -1/0, ChangeTypeEx() coerces to 1/0.
469 * VT_NULL and VT_EMPTY always give a 0 value.
471 DEC_LO32(&V_DECIMAL(pd
)) = vtFrom
== VT_BOOL
&& V_BOOL(ps
) ? 1 : 0;
473 case VT_I1
: return VarDecFromI1(V_I1(ps
), &V_DECIMAL(pd
));
474 case VT_I2
: return VarDecFromI2(V_I2(ps
), &V_DECIMAL(pd
));
475 case VT_I4
: return VarDecFromI4(V_I4(ps
), &V_DECIMAL(pd
));
476 case VT_UI1
: return VarDecFromUI1(V_UI1(ps
), &V_DECIMAL(pd
));
477 case VT_UI2
: return VarDecFromUI2(V_UI2(ps
), &V_DECIMAL(pd
));
478 case VT_UI4
: return VarDecFromUI4(V_UI4(ps
), &V_DECIMAL(pd
));
479 case VT_I8
: return VarDecFromI8(V_I8(ps
), &V_DECIMAL(pd
));
480 case VT_UI8
: return VarDecFromUI8(V_UI8(ps
), &V_DECIMAL(pd
));
481 case VT_R4
: return VarDecFromR4(V_R4(ps
), &V_DECIMAL(pd
));
482 case VT_R8
: return VarDecFromR8(V_R8(ps
), &V_DECIMAL(pd
));
483 case VT_DATE
: return VarDecFromDate(V_DATE(ps
), &V_DECIMAL(pd
));
484 case VT_CY
: return VarDecFromCy(V_CY(ps
), &V_DECIMAL(pd
));
485 case VT_DISPATCH
: return VarDecFromDisp(V_DISPATCH(ps
), lcid
, &V_DECIMAL(pd
));
486 case VT_BSTR
: return VarDecFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_DECIMAL(pd
));
494 if (V_DISPATCH(ps
) == NULL
)
495 V_UNKNOWN(pd
) = NULL
;
497 res
= IDispatch_QueryInterface(V_DISPATCH(ps
), &IID_IUnknown
, (LPVOID
*)&V_UNKNOWN(pd
));
506 if (V_UNKNOWN(ps
) == NULL
)
507 V_DISPATCH(pd
) = NULL
;
509 res
= IUnknown_QueryInterface(V_UNKNOWN(ps
), &IID_IDispatch
, (LPVOID
*)&V_DISPATCH(pd
));
520 /* Coerce to/from an array */
521 static inline HRESULT
VARIANT_CoerceArray(VARIANTARG
* pd
, VARIANTARG
* ps
, VARTYPE vt
)
523 if (vt
== VT_BSTR
&& V_VT(ps
) == (VT_ARRAY
|VT_UI1
))
524 return BstrFromVector(V_ARRAY(ps
), &V_BSTR(pd
));
526 if (V_VT(ps
) == VT_BSTR
&& vt
== (VT_ARRAY
|VT_UI1
))
527 return VectorFromBstr(V_BSTR(ps
), &V_ARRAY(ps
));
530 return SafeArrayCopy(V_ARRAY(ps
), &V_ARRAY(pd
));
532 return DISP_E_TYPEMISMATCH
;
535 /******************************************************************************
536 * Check if a variants type is valid.
538 static inline HRESULT
VARIANT_ValidateType(VARTYPE vt
)
540 VARTYPE vtExtra
= vt
& VT_EXTRA_TYPE
;
544 if (!(vtExtra
& (VT_VECTOR
|VT_RESERVED
)))
546 if (vt
< VT_VOID
|| vt
== VT_RECORD
|| vt
== VT_CLSID
)
548 if ((vtExtra
& (VT_BYREF
|VT_ARRAY
)) && vt
<= VT_NULL
)
549 return DISP_E_BADVARTYPE
;
550 if (vt
!= (VARTYPE
)15)
554 return DISP_E_BADVARTYPE
;
557 /******************************************************************************
558 * VariantInit [OLEAUT32.8]
560 * Initialise a variant.
563 * pVarg [O] Variant to initialise
569 * This function simply sets the type of the variant to VT_EMPTY. It does not
570 * free any existing value, use VariantClear() for that.
572 void WINAPI
VariantInit(VARIANTARG
* pVarg
)
574 TRACE("(%p)\n", pVarg
);
576 V_VT(pVarg
) = VT_EMPTY
; /* Native doesn't set any other fields */
579 /******************************************************************************
580 * VariantClear [OLEAUT32.9]
585 * pVarg [I/O] Variant to clear
588 * Success: S_OK. Any previous value in pVarg is freed and its type is set to VT_EMPTY.
589 * Failure: DISP_E_BADVARTYPE, if the variant is a not a valid variant type.
591 HRESULT WINAPI
VariantClear(VARIANTARG
* pVarg
)
595 TRACE("(%p->(%s%s))\n", pVarg
, debugstr_VT(pVarg
), debugstr_VF(pVarg
));
597 hres
= VARIANT_ValidateType(V_VT(pVarg
));
601 if (!V_ISBYREF(pVarg
))
603 if (V_ISARRAY(pVarg
) || V_VT(pVarg
) == VT_SAFEARRAY
)
606 hres
= SafeArrayDestroy(V_ARRAY(pVarg
));
608 else if (V_VT(pVarg
) == VT_BSTR
)
611 SysFreeString(V_BSTR(pVarg
));
613 else if (V_VT(pVarg
) == VT_RECORD
)
615 struct __tagBRECORD
* pBr
= &V_UNION(pVarg
,brecVal
);
618 IRecordInfo_RecordClear(pBr
->pRecInfo
, pBr
->pvRecord
);
619 IRecordInfo_Release(pBr
->pRecInfo
);
622 else if (V_VT(pVarg
) == VT_DISPATCH
||
623 V_VT(pVarg
) == VT_UNKNOWN
)
625 if (V_UNKNOWN(pVarg
))
626 IUnknown_Release(V_UNKNOWN(pVarg
));
629 V_VT(pVarg
) = VT_EMPTY
;
634 /******************************************************************************
635 * Copy an IRecordInfo object contained in a variant.
637 static HRESULT
VARIANT_CopyIRecordInfo(struct __tagBRECORD
* pBr
)
645 hres
= IRecordInfo_GetSize(pBr
->pRecInfo
, &ulSize
);
648 PVOID pvRecord
= HeapAlloc(GetProcessHeap(), 0, ulSize
);
650 hres
= E_OUTOFMEMORY
;
653 memcpy(pvRecord
, pBr
->pvRecord
, ulSize
);
654 pBr
->pvRecord
= pvRecord
;
656 hres
= IRecordInfo_RecordCopy(pBr
->pRecInfo
, pvRecord
, pvRecord
);
658 IRecordInfo_AddRef(pBr
->pRecInfo
);
662 else if (pBr
->pvRecord
)
667 /******************************************************************************
668 * VariantCopy [OLEAUT32.10]
673 * pvargDest [O] Destination for copy
674 * pvargSrc [I] Source variant to copy
677 * Success: S_OK. pvargDest contains a copy of pvargSrc.
678 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid type.
679 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
680 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
681 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
684 * - If pvargSrc == pvargDest, this function does nothing, and succeeds if
685 * pvargSrc is valid. Otherwise, pvargDest is always cleared using
686 * VariantClear() before pvargSrc is copied to it. If clearing pvargDest
687 * fails, so does this function.
688 * - VT_CLSID is a valid type type for pvargSrc, but not for pvargDest.
689 * - For by-value non-intrinsic types, a deep copy is made, i.e. The whole value
690 * is copied rather than just any pointers to it.
691 * - For by-value object types the object pointer is copied and the objects
692 * reference count increased using IUnknown_AddRef().
693 * - For all by-reference types, only the referencing pointer is copied.
695 HRESULT WINAPI
VariantCopy(VARIANTARG
* pvargDest
, VARIANTARG
* pvargSrc
)
699 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest
, debugstr_VT(pvargDest
),
700 debugstr_VF(pvargDest
), pvargSrc
, debugstr_VT(pvargSrc
),
701 debugstr_VF(pvargSrc
));
703 if (V_TYPE(pvargSrc
) == VT_CLSID
|| /* VT_CLSID is a special case */
704 FAILED(VARIANT_ValidateType(V_VT(pvargSrc
))))
705 return DISP_E_BADVARTYPE
;
707 if (pvargSrc
!= pvargDest
&&
708 SUCCEEDED(hres
= VariantClear(pvargDest
)))
710 *pvargDest
= *pvargSrc
; /* Shallow copy the value */
712 if (!V_ISBYREF(pvargSrc
))
714 if (V_ISARRAY(pvargSrc
))
716 if (V_ARRAY(pvargSrc
))
717 hres
= SafeArrayCopy(V_ARRAY(pvargSrc
), &V_ARRAY(pvargDest
));
719 else if (V_VT(pvargSrc
) == VT_BSTR
)
721 if (V_BSTR(pvargSrc
))
723 V_BSTR(pvargDest
) = SysAllocStringByteLen((char*)V_BSTR(pvargSrc
), SysStringByteLen(V_BSTR(pvargSrc
)));
724 if (!V_BSTR(pvargDest
))
726 TRACE("!V_BSTR(pvargDest), SysAllocStringByteLen() failed to allocate %d bytes\n", SysStringByteLen(V_BSTR(pvargSrc
)));
727 hres
= E_OUTOFMEMORY
;
731 else if (V_VT(pvargSrc
) == VT_RECORD
)
733 hres
= VARIANT_CopyIRecordInfo(&V_UNION(pvargDest
,brecVal
));
735 else if (V_VT(pvargSrc
) == VT_DISPATCH
||
736 V_VT(pvargSrc
) == VT_UNKNOWN
)
738 if (V_UNKNOWN(pvargSrc
))
739 IUnknown_AddRef(V_UNKNOWN(pvargSrc
));
746 /* Return the byte size of a variants data */
747 static inline size_t VARIANT_DataSize(const VARIANT
* pv
)
752 case VT_UI1
: return sizeof(BYTE
);
754 case VT_UI2
: return sizeof(SHORT
);
758 case VT_UI4
: return sizeof(LONG
);
760 case VT_UI8
: return sizeof(LONGLONG
);
761 case VT_R4
: return sizeof(float);
762 case VT_R8
: return sizeof(double);
763 case VT_DATE
: return sizeof(DATE
);
764 case VT_BOOL
: return sizeof(VARIANT_BOOL
);
767 case VT_BSTR
: return sizeof(void*);
768 case VT_CY
: return sizeof(CY
);
769 case VT_ERROR
: return sizeof(SCODE
);
771 TRACE("Shouldn't be called for vt %s%s!\n", debugstr_VT(pv
), debugstr_VF(pv
));
775 /******************************************************************************
776 * VariantCopyInd [OLEAUT32.11]
778 * Copy a variant, dereferencing it it is by-reference.
781 * pvargDest [O] Destination for copy
782 * pvargSrc [I] Source variant to copy
785 * Success: S_OK. pvargDest contains a copy of pvargSrc.
786 * Failure: An HRESULT error code indicating the error.
789 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid by-value type.
790 * E_INVALIDARG, if pvargSrc is an invalid by-reference type.
791 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
792 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
793 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
796 * - If pvargSrc is by-value, this function behaves exactly as VariantCopy().
797 * - If pvargSrc is by-reference, the value copied to pvargDest is the pointed-to
799 * - if pvargSrc == pvargDest, this function dereferences in place. Otherwise,
800 * pvargDest is always cleared using VariantClear() before pvargSrc is copied
801 * to it. If clearing pvargDest fails, so does this function.
803 HRESULT WINAPI
VariantCopyInd(VARIANT
* pvargDest
, VARIANTARG
* pvargSrc
)
805 VARIANTARG vTmp
, *pSrc
= pvargSrc
;
809 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest
, debugstr_VT(pvargDest
),
810 debugstr_VF(pvargDest
), pvargSrc
, debugstr_VT(pvargSrc
),
811 debugstr_VF(pvargSrc
));
813 if (!V_ISBYREF(pvargSrc
))
814 return VariantCopy(pvargDest
, pvargSrc
);
816 /* Argument checking is more lax than VariantCopy()... */
817 vt
= V_TYPE(pvargSrc
);
818 if (V_ISARRAY(pvargSrc
) ||
819 (vt
> VT_NULL
&& vt
!= (VARTYPE
)15 && vt
< VT_VOID
&&
820 !(V_VT(pvargSrc
) & (VT_VECTOR
|VT_RESERVED
))))
825 return E_INVALIDARG
; /* ...And the return value for invalid types differs too */
827 if (pvargSrc
== pvargDest
)
829 /* In place copy. Use a shallow copy of pvargSrc & init pvargDest.
830 * This avoids an expensive VariantCopy() call - e.g. SafeArrayCopy().
834 V_VT(pvargDest
) = VT_EMPTY
;
838 /* Copy into another variant. Free the variant in pvargDest */
839 if (FAILED(hres
= VariantClear(pvargDest
)))
841 TRACE("VariantClear() of destination failed\n");
848 /* Native doesn't check that *V_ARRAYREF(pSrc) is valid */
849 hres
= SafeArrayCopy(*V_ARRAYREF(pSrc
), &V_ARRAY(pvargDest
));
851 else if (V_VT(pSrc
) == (VT_BSTR
|VT_BYREF
))
853 /* Native doesn't check that *V_BSTRREF(pSrc) is valid */
854 V_BSTR(pvargDest
) = SysAllocStringByteLen((char*)*V_BSTRREF(pSrc
), SysStringByteLen(*V_BSTRREF(pSrc
)));
856 else if (V_VT(pSrc
) == (VT_RECORD
|VT_BYREF
))
858 V_UNION(pvargDest
,brecVal
) = V_UNION(pvargSrc
,brecVal
);
859 hres
= VARIANT_CopyIRecordInfo(&V_UNION(pvargDest
,brecVal
));
861 else if (V_VT(pSrc
) == (VT_DISPATCH
|VT_BYREF
) ||
862 V_VT(pSrc
) == (VT_UNKNOWN
|VT_BYREF
))
864 /* Native doesn't check that *V_UNKNOWNREF(pSrc) is valid */
865 V_UNKNOWN(pvargDest
) = *V_UNKNOWNREF(pSrc
);
866 if (*V_UNKNOWNREF(pSrc
))
867 IUnknown_AddRef(*V_UNKNOWNREF(pSrc
));
869 else if (V_VT(pSrc
) == (VT_VARIANT
|VT_BYREF
))
871 /* Native doesn't check that *V_VARIANTREF(pSrc) is valid */
872 if (V_VT(V_VARIANTREF(pSrc
)) == (VT_VARIANT
|VT_BYREF
))
873 hres
= E_INVALIDARG
; /* Don't dereference more than one level */
875 hres
= VariantCopyInd(pvargDest
, V_VARIANTREF(pSrc
));
877 /* Use the dereferenced variants type value, not VT_VARIANT */
878 goto VariantCopyInd_Return
;
880 else if (V_VT(pSrc
) == (VT_DECIMAL
|VT_BYREF
))
882 memcpy(&DEC_SCALE(&V_DECIMAL(pvargDest
)), &DEC_SCALE(V_DECIMALREF(pSrc
)),
883 sizeof(DECIMAL
) - sizeof(USHORT
));
887 /* Copy the pointed to data into this variant */
888 memcpy(&V_BYREF(pvargDest
), V_BYREF(pSrc
), VARIANT_DataSize(pSrc
));
891 V_VT(pvargDest
) = V_VT(pSrc
) & ~VT_BYREF
;
893 VariantCopyInd_Return
:
895 if (pSrc
!= pvargSrc
)
898 TRACE("returning 0x%08lx, %p->(%s%s)\n", hres
, pvargDest
,
899 debugstr_VT(pvargDest
), debugstr_VF(pvargDest
));
903 /******************************************************************************
904 * VariantChangeType [OLEAUT32.12]
906 * Change the type of a variant.
909 * pvargDest [O] Destination for the converted variant
910 * pvargSrc [O] Source variant to change the type of
911 * wFlags [I] VARIANT_ flags from "oleauto.h"
912 * vt [I] Variant type to change pvargSrc into
915 * Success: S_OK. pvargDest contains the converted value.
916 * Failure: An HRESULT error code describing the failure.
919 * The LCID used for the conversion is LOCALE_USER_DEFAULT.
920 * See VariantChangeTypeEx.
922 HRESULT WINAPI
VariantChangeType(VARIANTARG
* pvargDest
, VARIANTARG
* pvargSrc
,
923 USHORT wFlags
, VARTYPE vt
)
925 return VariantChangeTypeEx( pvargDest
, pvargSrc
, LOCALE_USER_DEFAULT
, wFlags
, vt
);
928 /******************************************************************************
929 * VariantChangeTypeEx [OLEAUT32.147]
931 * Change the type of a variant.
934 * pvargDest [O] Destination for the converted variant
935 * pvargSrc [O] Source variant to change the type of
936 * lcid [I] LCID for the conversion
937 * wFlags [I] VARIANT_ flags from "oleauto.h"
938 * vt [I] Variant type to change pvargSrc into
941 * Success: S_OK. pvargDest contains the converted value.
942 * Failure: An HRESULT error code describing the failure.
945 * pvargDest and pvargSrc can point to the same variant to perform an in-place
946 * conversion. If the conversion is successful, pvargSrc will be freed.
948 HRESULT WINAPI
VariantChangeTypeEx(VARIANTARG
* pvargDest
, VARIANTARG
* pvargSrc
,
949 LCID lcid
, USHORT wFlags
, VARTYPE vt
)
953 TRACE("(%p->(%s%s),%p->(%s%s),0x%08lx,0x%04x,%s%s)\n", pvargDest
,
954 debugstr_VT(pvargDest
), debugstr_VF(pvargDest
), pvargSrc
,
955 debugstr_VT(pvargSrc
), debugstr_VF(pvargSrc
), lcid
, wFlags
,
956 debugstr_vt(vt
), debugstr_vf(vt
));
959 res
= DISP_E_BADVARTYPE
;
962 res
= VARIANT_ValidateType(V_VT(pvargSrc
));
966 res
= VARIANT_ValidateType(vt
);
970 VARIANTARG vTmp
, vSrcDeref
;
972 if(V_ISBYREF(pvargSrc
) && !V_BYREF(pvargSrc
))
973 res
= DISP_E_TYPEMISMATCH
;
976 V_VT(&vTmp
) = VT_EMPTY
;
977 V_VT(&vSrcDeref
) = VT_EMPTY
;
979 VariantClear(&vSrcDeref
);
984 res
= VariantCopyInd(&vSrcDeref
, pvargSrc
);
987 if (V_ISARRAY(&vSrcDeref
) || (vt
& VT_ARRAY
))
988 res
= VARIANT_CoerceArray(&vTmp
, &vSrcDeref
, vt
);
990 res
= VARIANT_Coerce(&vTmp
, lcid
, wFlags
, &vSrcDeref
, vt
);
992 if (SUCCEEDED(res
)) {
994 VariantCopy(pvargDest
, &vTmp
);
997 VariantClear(&vSrcDeref
);
1004 TRACE("returning 0x%08lx, %p->(%s%s)\n", res
, pvargDest
,
1005 debugstr_VT(pvargDest
), debugstr_VF(pvargDest
));
1009 /* Date Conversions */
1011 #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
1013 /* Convert a VT_DATE value to a Julian Date */
1014 static inline int VARIANT_JulianFromDate(int dateIn
)
1016 int julianDays
= dateIn
;
1018 julianDays
-= DATE_MIN
; /* Convert to + days from 1 Jan 100 AD */
1019 julianDays
+= 1757585; /* Convert to + days from 23 Nov 4713 BC (Julian) */
1023 /* Convert a Julian Date to a VT_DATE value */
1024 static inline int VARIANT_DateFromJulian(int dateIn
)
1026 int julianDays
= dateIn
;
1028 julianDays
-= 1757585; /* Convert to + days from 1 Jan 100 AD */
1029 julianDays
+= DATE_MIN
; /* Convert to +/- days from 1 Jan 1899 AD */
1033 /* Convert a Julian date to Day/Month/Year - from PostgreSQL */
1034 static inline void VARIANT_DMYFromJulian(int jd
, USHORT
*year
, USHORT
*month
, USHORT
*day
)
1040 l
-= (n
* 146097 + 3) / 4;
1041 i
= (4000 * (l
+ 1)) / 1461001;
1042 l
+= 31 - (i
* 1461) / 4;
1043 j
= (l
* 80) / 2447;
1044 *day
= l
- (j
* 2447) / 80;
1046 *month
= (j
+ 2) - (12 * l
);
1047 *year
= 100 * (n
- 49) + i
+ l
;
1050 /* Convert Day/Month/Year to a Julian date - from PostgreSQL */
1051 static inline double VARIANT_JulianFromDMY(USHORT year
, USHORT month
, USHORT day
)
1053 int m12
= (month
- 14) / 12;
1055 return ((1461 * (year
+ 4800 + m12
)) / 4 + (367 * (month
- 2 - 12 * m12
)) / 12 -
1056 (3 * ((year
+ 4900 + m12
) / 100)) / 4 + day
- 32075);
1059 /* Macros for accessing DOS format date/time fields */
1060 #define DOS_YEAR(x) (1980 + (x >> 9))
1061 #define DOS_MONTH(x) ((x >> 5) & 0xf)
1062 #define DOS_DAY(x) (x & 0x1f)
1063 #define DOS_HOUR(x) (x >> 11)
1064 #define DOS_MINUTE(x) ((x >> 5) & 0x3f)
1065 #define DOS_SECOND(x) ((x & 0x1f) << 1)
1066 /* Create a DOS format date/time */
1067 #define DOS_DATE(d,m,y) (d | (m << 5) | ((y-1980) << 9))
1068 #define DOS_TIME(h,m,s) ((s >> 1) | (m << 5) | (h << 11))
1070 /* Roll a date forwards or backwards to correct it */
1071 static HRESULT
VARIANT_RollUdate(UDATE
*lpUd
)
1073 static const BYTE days
[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1075 TRACE("Raw date: %d/%d/%d %d:%d:%d\n", lpUd
->st
.wDay
, lpUd
->st
.wMonth
,
1076 lpUd
->st
.wYear
, lpUd
->st
.wHour
, lpUd
->st
.wMinute
, lpUd
->st
.wSecond
);
1078 /* Years < 100 are treated as 1900 + year */
1079 if (lpUd
->st
.wYear
< 100)
1080 lpUd
->st
.wYear
+= 1900;
1082 if (!lpUd
->st
.wMonth
)
1084 /* Roll back to December of the previous year */
1085 lpUd
->st
.wMonth
= 12;
1088 else while (lpUd
->st
.wMonth
> 12)
1090 /* Roll forward the correct number of months */
1092 lpUd
->st
.wMonth
-= 12;
1095 if (lpUd
->st
.wYear
> 9999 || lpUd
->st
.wHour
> 23 ||
1096 lpUd
->st
.wMinute
> 59 || lpUd
->st
.wSecond
> 59)
1097 return E_INVALIDARG
; /* Invalid values */
1101 /* Roll back the date one day */
1102 if (lpUd
->st
.wMonth
== 1)
1104 /* Roll back to December 31 of the previous year */
1106 lpUd
->st
.wMonth
= 12;
1111 lpUd
->st
.wMonth
--; /* Previous month */
1112 if (lpUd
->st
.wMonth
== 2 && IsLeapYear(lpUd
->st
.wYear
))
1113 lpUd
->st
.wDay
= 29; /* Februaury has 29 days on leap years */
1115 lpUd
->st
.wDay
= days
[lpUd
->st
.wMonth
]; /* Last day of the month */
1118 else if (lpUd
->st
.wDay
> 28)
1120 int rollForward
= 0;
1122 /* Possibly need to roll the date forward */
1123 if (lpUd
->st
.wMonth
== 2 && IsLeapYear(lpUd
->st
.wYear
))
1124 rollForward
= lpUd
->st
.wDay
- 29; /* Februaury has 29 days on leap years */
1126 rollForward
= lpUd
->st
.wDay
- days
[lpUd
->st
.wMonth
];
1128 if (rollForward
> 0)
1130 lpUd
->st
.wDay
= rollForward
;
1132 if (lpUd
->st
.wMonth
> 12)
1134 lpUd
->st
.wMonth
= 1; /* Roll forward into January of the next year */
1139 TRACE("Rolled date: %d/%d/%d %d:%d:%d\n", lpUd
->st
.wDay
, lpUd
->st
.wMonth
,
1140 lpUd
->st
.wYear
, lpUd
->st
.wHour
, lpUd
->st
.wMinute
, lpUd
->st
.wSecond
);
1144 /**********************************************************************
1145 * DosDateTimeToVariantTime [OLEAUT32.14]
1147 * Convert a Dos format date and time into variant VT_DATE format.
1150 * wDosDate [I] Dos format date
1151 * wDosTime [I] Dos format time
1152 * pDateOut [O] Destination for VT_DATE format
1155 * Success: TRUE. pDateOut contains the converted time.
1156 * Failure: FALSE, if wDosDate or wDosTime are invalid (see notes).
1159 * - Dos format dates can only hold dates from 1-Jan-1980 to 31-Dec-2099.
1160 * - Dos format times are accurate to only 2 second precision.
1161 * - The format of a Dos Date is:
1162 *| Bits Values Meaning
1163 *| ---- ------ -------
1164 *| 0-4 1-31 Day of the week. 0 rolls back one day. A value greater than
1165 *| the days in the month rolls forward the extra days.
1166 *| 5-8 1-12 Month of the year. 0 rolls back to December of the previous
1167 *| year. 13-15 are invalid.
1168 *| 9-15 0-119 Year based from 1980 (Max 2099). 120-127 are invalid.
1169 * - The format of a Dos Time is:
1170 *| Bits Values Meaning
1171 *| ---- ------ -------
1172 *| 0-4 0-29 Seconds/2. 30 and 31 are invalid.
1173 *| 5-10 0-59 Minutes. 60-63 are invalid.
1174 *| 11-15 0-23 Hours (24 hour clock). 24-32 are invalid.
1176 INT WINAPI
DosDateTimeToVariantTime(USHORT wDosDate
, USHORT wDosTime
,
1181 TRACE("(0x%x(%d/%d/%d),0x%x(%d:%d:%d),%p)\n",
1182 wDosDate
, DOS_YEAR(wDosDate
), DOS_MONTH(wDosDate
), DOS_DAY(wDosDate
),
1183 wDosTime
, DOS_HOUR(wDosTime
), DOS_MINUTE(wDosTime
), DOS_SECOND(wDosTime
),
1186 ud
.st
.wYear
= DOS_YEAR(wDosDate
);
1187 ud
.st
.wMonth
= DOS_MONTH(wDosDate
);
1188 if (ud
.st
.wYear
> 2099 || ud
.st
.wMonth
> 12)
1190 ud
.st
.wDay
= DOS_DAY(wDosDate
);
1191 ud
.st
.wHour
= DOS_HOUR(wDosTime
);
1192 ud
.st
.wMinute
= DOS_MINUTE(wDosTime
);
1193 ud
.st
.wSecond
= DOS_SECOND(wDosTime
);
1194 ud
.st
.wDayOfWeek
= ud
.st
.wMilliseconds
= 0;
1196 return !VarDateFromUdate(&ud
, 0, pDateOut
);
1199 /**********************************************************************
1200 * VariantTimeToDosDateTime [OLEAUT32.13]
1202 * Convert a variant format date into a Dos format date and time.
1204 * dateIn [I] VT_DATE time format
1205 * pwDosDate [O] Destination for Dos format date
1206 * pwDosTime [O] Destination for Dos format time
1209 * Success: TRUE. pwDosDate and pwDosTime contains the converted values.
1210 * Failure: FALSE, if dateIn cannot be represented in Dos format.
1213 * See DosDateTimeToVariantTime() for Dos format details and bugs.
1215 INT WINAPI
VariantTimeToDosDateTime(double dateIn
, USHORT
*pwDosDate
, USHORT
*pwDosTime
)
1219 TRACE("(%g,%p,%p)\n", dateIn
, pwDosDate
, pwDosTime
);
1221 if (FAILED(VarUdateFromDate(dateIn
, 0, &ud
)))
1224 if (ud
.st
.wYear
< 1980 || ud
.st
.wYear
> 2099)
1227 *pwDosDate
= DOS_DATE(ud
.st
.wDay
, ud
.st
.wMonth
, ud
.st
.wYear
);
1228 *pwDosTime
= DOS_TIME(ud
.st
.wHour
, ud
.st
.wMinute
, ud
.st
.wSecond
);
1230 TRACE("Returning 0x%x(%d/%d/%d), 0x%x(%d:%d:%d)\n",
1231 *pwDosDate
, DOS_YEAR(*pwDosDate
), DOS_MONTH(*pwDosDate
), DOS_DAY(*pwDosDate
),
1232 *pwDosTime
, DOS_HOUR(*pwDosTime
), DOS_MINUTE(*pwDosTime
), DOS_SECOND(*pwDosTime
));
1236 /***********************************************************************
1237 * SystemTimeToVariantTime [OLEAUT32.184]
1239 * Convert a System format date and time into variant VT_DATE format.
1242 * lpSt [I] System format date and time
1243 * pDateOut [O] Destination for VT_DATE format date
1246 * Success: TRUE. *pDateOut contains the converted value.
1247 * Failure: FALSE, if lpSt cannot be represented in VT_DATE format.
1249 INT WINAPI
SystemTimeToVariantTime(LPSYSTEMTIME lpSt
, double *pDateOut
)
1253 TRACE("(%p->%d/%d/%d %d:%d:%d,%p)\n", lpSt
, lpSt
->wDay
, lpSt
->wMonth
,
1254 lpSt
->wYear
, lpSt
->wHour
, lpSt
->wMinute
, lpSt
->wSecond
, pDateOut
);
1256 if (lpSt
->wMonth
> 12)
1259 memcpy(&ud
.st
, lpSt
, sizeof(ud
.st
));
1260 return !VarDateFromUdate(&ud
, 0, pDateOut
);
1263 /***********************************************************************
1264 * VariantTimeToSystemTime [OLEAUT32.185]
1266 * Convert a variant VT_DATE into a System format date and time.
1269 * datein [I] Variant VT_DATE format date
1270 * lpSt [O] Destination for System format date and time
1273 * Success: TRUE. *lpSt contains the converted value.
1274 * Failure: FALSE, if dateIn is too large or small.
1276 INT WINAPI
VariantTimeToSystemTime(double dateIn
, LPSYSTEMTIME lpSt
)
1280 TRACE("(%g,%p)\n", dateIn
, lpSt
);
1282 if (FAILED(VarUdateFromDate(dateIn
, 0, &ud
)))
1285 memcpy(lpSt
, &ud
.st
, sizeof(ud
.st
));
1289 /***********************************************************************
1290 * VarDateFromUdateEx [OLEAUT32.319]
1292 * Convert an unpacked format date and time to a variant VT_DATE.
1295 * pUdateIn [I] Unpacked format date and time to convert
1296 * lcid [I] Locale identifier for the conversion
1297 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1298 * pDateOut [O] Destination for variant VT_DATE.
1301 * Success: S_OK. *pDateOut contains the converted value.
1302 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1304 HRESULT WINAPI
VarDateFromUdateEx(UDATE
*pUdateIn
, LCID lcid
, ULONG dwFlags
, DATE
*pDateOut
)
1309 TRACE("(%p->%d/%d/%d %d:%d:%d:%d %d %d,0x%08lx,0x%08lx,%p)\n", pUdateIn
,
1310 pUdateIn
->st
.wMonth
, pUdateIn
->st
.wDay
, pUdateIn
->st
.wYear
,
1311 pUdateIn
->st
.wHour
, pUdateIn
->st
.wMinute
, pUdateIn
->st
.wSecond
,
1312 pUdateIn
->st
.wMilliseconds
, pUdateIn
->st
.wDayOfWeek
,
1313 pUdateIn
->wDayOfYear
, lcid
, dwFlags
, pDateOut
);
1315 if (lcid
!= MAKELCID(MAKELANGID(LANG_ENGLISH
, SUBLANG_ENGLISH_US
), SORT_DEFAULT
))
1316 FIXME("lcid possibly not handled, treating as en-us\n");
1318 memcpy(&ud
, pUdateIn
, sizeof(ud
));
1320 if (dwFlags
& VAR_VALIDDATE
)
1321 WARN("Ignoring VAR_VALIDDATE\n");
1323 if (FAILED(VARIANT_RollUdate(&ud
)))
1324 return E_INVALIDARG
;
1327 dateVal
= VARIANT_DateFromJulian(VARIANT_JulianFromDMY(ud
.st
.wYear
, ud
.st
.wMonth
, ud
.st
.wDay
));
1330 dateVal
+= ud
.st
.wHour
/ 24.0;
1331 dateVal
+= ud
.st
.wMinute
/ 1440.0;
1332 dateVal
+= ud
.st
.wSecond
/ 86400.0;
1333 dateVal
+= ud
.st
.wMilliseconds
/ 86400000.0;
1335 TRACE("Returning %g\n", dateVal
);
1336 *pDateOut
= dateVal
;
1340 /***********************************************************************
1341 * VarDateFromUdate [OLEAUT32.330]
1343 * Convert an unpacked format date and time to a variant VT_DATE.
1346 * pUdateIn [I] Unpacked format date and time to convert
1347 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1348 * pDateOut [O] Destination for variant VT_DATE.
1351 * Success: S_OK. *pDateOut contains the converted value.
1352 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1355 * This function uses the United States English locale for the conversion. Use
1356 * VarDateFromUdateEx() for alternate locales.
1358 HRESULT WINAPI
VarDateFromUdate(UDATE
*pUdateIn
, ULONG dwFlags
, DATE
*pDateOut
)
1360 LCID lcid
= MAKELCID(MAKELANGID(LANG_ENGLISH
, SUBLANG_ENGLISH_US
), SORT_DEFAULT
);
1362 return VarDateFromUdateEx(pUdateIn
, lcid
, dwFlags
, pDateOut
);
1365 /***********************************************************************
1366 * VarUdateFromDate [OLEAUT32.331]
1368 * Convert a variant VT_DATE into an unpacked format date and time.
1371 * datein [I] Variant VT_DATE format date
1372 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1373 * lpUdate [O] Destination for unpacked format date and time
1376 * Success: S_OK. *lpUdate contains the converted value.
1377 * Failure: E_INVALIDARG, if dateIn is too large or small.
1379 HRESULT WINAPI
VarUdateFromDate(DATE dateIn
, ULONG dwFlags
, UDATE
*lpUdate
)
1381 /* Cumulative totals of days per month */
1382 static const USHORT cumulativeDays
[] =
1384 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
1386 double datePart
, timePart
;
1389 TRACE("(%g,0x%08lx,%p)\n", dateIn
, dwFlags
, lpUdate
);
1391 if (dateIn
<= (DATE_MIN
- 1.0) || dateIn
>= (DATE_MAX
+ 1.0))
1392 return E_INVALIDARG
;
1394 datePart
= dateIn
< 0.0 ? ceil(dateIn
) : floor(dateIn
);
1395 /* Compensate for int truncation (always downwards) */
1396 timePart
= dateIn
- datePart
+ 0.00000000001;
1397 if (timePart
>= 1.0)
1398 timePart
-= 0.00000000001;
1401 julianDays
= VARIANT_JulianFromDate(dateIn
);
1402 VARIANT_DMYFromJulian(julianDays
, &lpUdate
->st
.wYear
, &lpUdate
->st
.wMonth
,
1405 datePart
= (datePart
+ 1.5) / 7.0;
1406 lpUdate
->st
.wDayOfWeek
= (datePart
- floor(datePart
)) * 7;
1407 if (lpUdate
->st
.wDayOfWeek
== 0)
1408 lpUdate
->st
.wDayOfWeek
= 5;
1409 else if (lpUdate
->st
.wDayOfWeek
== 1)
1410 lpUdate
->st
.wDayOfWeek
= 6;
1412 lpUdate
->st
.wDayOfWeek
-= 2;
1414 if (lpUdate
->st
.wMonth
> 2 && IsLeapYear(lpUdate
->st
.wYear
))
1415 lpUdate
->wDayOfYear
= 1; /* After February, in a leap year */
1417 lpUdate
->wDayOfYear
= 0;
1419 lpUdate
->wDayOfYear
+= cumulativeDays
[lpUdate
->st
.wMonth
];
1420 lpUdate
->wDayOfYear
+= lpUdate
->st
.wDay
;
1424 lpUdate
->st
.wHour
= timePart
;
1425 timePart
-= lpUdate
->st
.wHour
;
1427 lpUdate
->st
.wMinute
= timePart
;
1428 timePart
-= lpUdate
->st
.wMinute
;
1430 lpUdate
->st
.wSecond
= timePart
;
1431 timePart
-= lpUdate
->st
.wSecond
;
1432 lpUdate
->st
.wMilliseconds
= 0;
1435 /* Round the milliseconds, adjusting the time/date forward if needed */
1436 if (lpUdate
->st
.wSecond
< 59)
1437 lpUdate
->st
.wSecond
++;
1440 lpUdate
->st
.wSecond
= 0;
1441 if (lpUdate
->st
.wMinute
< 59)
1442 lpUdate
->st
.wMinute
++;
1445 lpUdate
->st
.wMinute
= 0;
1446 if (lpUdate
->st
.wHour
< 23)
1447 lpUdate
->st
.wHour
++;
1450 lpUdate
->st
.wHour
= 0;
1451 /* Roll over a whole day */
1452 if (++lpUdate
->st
.wDay
> 28)
1453 VARIANT_RollUdate(lpUdate
);
1461 #define GET_NUMBER_TEXT(fld,name) \
1463 if (!GetLocaleInfoW(lcid, lctype|fld, buff, 2)) \
1464 WARN("buffer too small for " #fld "\n"); \
1466 if (buff[0]) lpChars->name = buff[0]; \
1467 TRACE("lcid 0x%lx, " #name "=%d '%c'\n", lcid, lpChars->name, lpChars->name)
1469 /* Get the valid number characters for an lcid */
1470 void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS
*lpChars
, LCID lcid
, DWORD dwFlags
)
1472 static const VARIANT_NUMBER_CHARS defaultChars
= { '-','+','.',',','$',0,'.',',' };
1473 LCTYPE lctype
= dwFlags
& LOCALE_NOUSEROVERRIDE
;
1476 memcpy(lpChars
, &defaultChars
, sizeof(defaultChars
));
1477 GET_NUMBER_TEXT(LOCALE_SNEGATIVESIGN
, cNegativeSymbol
);
1478 GET_NUMBER_TEXT(LOCALE_SPOSITIVESIGN
, cPositiveSymbol
);
1479 GET_NUMBER_TEXT(LOCALE_SDECIMAL
, cDecimalPoint
);
1480 GET_NUMBER_TEXT(LOCALE_STHOUSAND
, cDigitSeperator
);
1481 GET_NUMBER_TEXT(LOCALE_SMONDECIMALSEP
, cCurrencyDecimalPoint
);
1482 GET_NUMBER_TEXT(LOCALE_SMONTHOUSANDSEP
, cCurrencyDigitSeperator
);
1484 /* Local currency symbols are often 2 characters */
1485 lpChars
->cCurrencyLocal2
= '\0';
1486 switch(GetLocaleInfoW(lcid
, lctype
|LOCALE_SCURRENCY
, buff
, sizeof(buff
)/sizeof(WCHAR
)))
1488 case 3: lpChars
->cCurrencyLocal2
= buff
[1]; /* Fall through */
1489 case 2: lpChars
->cCurrencyLocal
= buff
[0];
1491 default: WARN("buffer too small for LOCALE_SCURRENCY\n");
1493 TRACE("lcid 0x%lx, cCurrencyLocal =%d,%d '%c','%c'\n", lcid
, lpChars
->cCurrencyLocal
,
1494 lpChars
->cCurrencyLocal2
, lpChars
->cCurrencyLocal
, lpChars
->cCurrencyLocal2
);
1497 /* Number Parsing States */
1498 #define B_PROCESSING_EXPONENT 0x1
1499 #define B_NEGATIVE_EXPONENT 0x2
1500 #define B_EXPONENT_START 0x4
1501 #define B_INEXACT_ZEROS 0x8
1502 #define B_LEADING_ZERO 0x10
1503 #define B_PROCESSING_HEX 0x20
1504 #define B_PROCESSING_OCT 0x40
1506 /**********************************************************************
1507 * VarParseNumFromStr [OLEAUT32.46]
1509 * Parse a string containing a number into a NUMPARSE structure.
1512 * lpszStr [I] String to parse number from
1513 * lcid [I] Locale Id for the conversion
1514 * dwFlags [I] 0, or LOCALE_NOUSEROVERRIDE to use system default number chars
1515 * pNumprs [I/O] Destination for parsed number
1516 * rgbDig [O] Destination for digits read in
1519 * Success: S_OK. pNumprs and rgbDig contain the parsed representation of
1521 * Failure: E_INVALIDARG, if any parameter is invalid.
1522 * DISP_E_TYPEMISMATCH, if the string is not a number or is formatted
1524 * DISP_E_OVERFLOW, if rgbDig is too small to hold the number.
1527 * pNumprs must have the following fields set:
1528 * cDig: Set to the size of rgbDig.
1529 * dwInFlags: Set to the allowable syntax of the number using NUMPRS_ flags
1533 * - I am unsure if this function should parse non-arabic (e.g. Thai)
1534 * numerals, so this has not been implemented.
1536 HRESULT WINAPI
VarParseNumFromStr(OLECHAR
*lpszStr
, LCID lcid
, ULONG dwFlags
,
1537 NUMPARSE
*pNumprs
, BYTE
*rgbDig
)
1539 VARIANT_NUMBER_CHARS chars
;
1541 DWORD dwState
= B_EXPONENT_START
|B_INEXACT_ZEROS
;
1542 int iMaxDigits
= sizeof(rgbTmp
) / sizeof(BYTE
);
1545 TRACE("(%s,%ld,0x%08lx,%p,%p)\n", debugstr_w(lpszStr
), lcid
, dwFlags
, pNumprs
, rgbDig
);
1547 if (!pNumprs
|| !rgbDig
)
1548 return E_INVALIDARG
;
1550 if (pNumprs
->cDig
< iMaxDigits
)
1551 iMaxDigits
= pNumprs
->cDig
;
1554 pNumprs
->dwOutFlags
= 0;
1555 pNumprs
->cchUsed
= 0;
1556 pNumprs
->nBaseShift
= 0;
1557 pNumprs
->nPwr10
= 0;
1560 return DISP_E_TYPEMISMATCH
;
1562 VARIANT_GetLocalisedNumberChars(&chars
, lcid
, dwFlags
);
1564 /* First consume all the leading symbols and space from the string */
1567 if (pNumprs
->dwInFlags
& NUMPRS_LEADING_WHITE
&& isspaceW(*lpszStr
))
1569 pNumprs
->dwOutFlags
|= NUMPRS_LEADING_WHITE
;
1574 } while (isspaceW(*lpszStr
));
1576 else if (pNumprs
->dwInFlags
& NUMPRS_LEADING_PLUS
&&
1577 *lpszStr
== chars
.cPositiveSymbol
&&
1578 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_PLUS
))
1580 pNumprs
->dwOutFlags
|= NUMPRS_LEADING_PLUS
;
1584 else if (pNumprs
->dwInFlags
& NUMPRS_LEADING_MINUS
&&
1585 *lpszStr
== chars
.cNegativeSymbol
&&
1586 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_MINUS
))
1588 pNumprs
->dwOutFlags
|= (NUMPRS_LEADING_MINUS
|NUMPRS_NEG
);
1592 else if (pNumprs
->dwInFlags
& NUMPRS_CURRENCY
&&
1593 !(pNumprs
->dwOutFlags
& NUMPRS_CURRENCY
) &&
1594 *lpszStr
== chars
.cCurrencyLocal
&&
1595 (!chars
.cCurrencyLocal2
|| lpszStr
[1] == chars
.cCurrencyLocal2
))
1597 pNumprs
->dwOutFlags
|= NUMPRS_CURRENCY
;
1600 /* Only accept currency characters */
1601 chars
.cDecimalPoint
= chars
.cCurrencyDecimalPoint
;
1602 chars
.cDigitSeperator
= chars
.cCurrencyDigitSeperator
;
1604 else if (pNumprs
->dwInFlags
& NUMPRS_PARENS
&& *lpszStr
== '(' &&
1605 !(pNumprs
->dwOutFlags
& NUMPRS_PARENS
))
1607 pNumprs
->dwOutFlags
|= NUMPRS_PARENS
;
1615 if (!(pNumprs
->dwOutFlags
& NUMPRS_CURRENCY
))
1617 /* Only accept non-currency characters */
1618 chars
.cCurrencyDecimalPoint
= chars
.cDecimalPoint
;
1619 chars
.cCurrencyDigitSeperator
= chars
.cDigitSeperator
;
1622 if ((*lpszStr
== '&' && (*(lpszStr
+1) == 'H' || *(lpszStr
+1) == 'h')) &&
1623 pNumprs
->dwInFlags
& NUMPRS_HEX_OCT
)
1625 dwState
|= B_PROCESSING_HEX
;
1626 pNumprs
->dwOutFlags
|= NUMPRS_HEX_OCT
;
1630 else if ((*lpszStr
== '&' && (*(lpszStr
+1) == 'O' || *(lpszStr
+1) == 'o')) &&
1631 pNumprs
->dwInFlags
& NUMPRS_HEX_OCT
)
1633 dwState
|= B_PROCESSING_OCT
;
1634 pNumprs
->dwOutFlags
|= NUMPRS_HEX_OCT
;
1639 /* Strip Leading zeros */
1640 while (*lpszStr
== '0')
1642 dwState
|= B_LEADING_ZERO
;
1649 if (isdigitW(*lpszStr
))
1651 if (dwState
& B_PROCESSING_EXPONENT
)
1653 int exponentSize
= 0;
1654 if (dwState
& B_EXPONENT_START
)
1656 if (!isdigitW(*lpszStr
))
1657 break; /* No exponent digits - invalid */
1658 while (*lpszStr
== '0')
1660 /* Skip leading zero's in the exponent */
1666 while (isdigitW(*lpszStr
))
1669 exponentSize
+= *lpszStr
- '0';
1673 if (dwState
& B_NEGATIVE_EXPONENT
)
1674 exponentSize
= -exponentSize
;
1675 /* Add the exponent into the powers of 10 */
1676 pNumprs
->nPwr10
+= exponentSize
;
1677 dwState
&= ~(B_PROCESSING_EXPONENT
|B_EXPONENT_START
);
1678 lpszStr
--; /* back up to allow processing of next char */
1682 if ((pNumprs
->cDig
>= iMaxDigits
) && !(dwState
& B_PROCESSING_HEX
)
1683 && !(dwState
& B_PROCESSING_OCT
))
1685 pNumprs
->dwOutFlags
|= NUMPRS_INEXACT
;
1687 if (*lpszStr
!= '0')
1688 dwState
&= ~B_INEXACT_ZEROS
; /* Inexact number with non-trailing zeros */
1690 /* This digit can't be represented, but count it in nPwr10 */
1691 if (pNumprs
->dwOutFlags
& NUMPRS_DECIMAL
)
1698 if ((dwState
& B_PROCESSING_OCT
) && ((*lpszStr
== '8') || (*lpszStr
== '9'))) {
1699 return DISP_E_TYPEMISMATCH
;
1702 if (pNumprs
->dwOutFlags
& NUMPRS_DECIMAL
)
1703 pNumprs
->nPwr10
--; /* Count decimal points in nPwr10 */
1705 rgbTmp
[pNumprs
->cDig
] = *lpszStr
- '0';
1711 else if (*lpszStr
== chars
.cDigitSeperator
&& pNumprs
->dwInFlags
& NUMPRS_THOUSANDS
)
1713 pNumprs
->dwOutFlags
|= NUMPRS_THOUSANDS
;
1716 else if (*lpszStr
== chars
.cDecimalPoint
&&
1717 pNumprs
->dwInFlags
& NUMPRS_DECIMAL
&&
1718 !(pNumprs
->dwOutFlags
& (NUMPRS_DECIMAL
|NUMPRS_EXPONENT
)))
1720 pNumprs
->dwOutFlags
|= NUMPRS_DECIMAL
;
1723 /* If we have no digits so far, skip leading zeros */
1726 while (lpszStr
[1] == '0')
1728 dwState
|= B_LEADING_ZERO
;
1735 else if (((*lpszStr
>= 'a' && *lpszStr
<= 'f') ||
1736 (*lpszStr
>= 'A' && *lpszStr
<= 'F')) &&
1737 dwState
& B_PROCESSING_HEX
)
1739 if (pNumprs
->cDig
>= iMaxDigits
)
1741 return DISP_E_OVERFLOW
;
1745 if (*lpszStr
>= 'a')
1746 rgbTmp
[pNumprs
->cDig
] = *lpszStr
- 'a' + 10;
1748 rgbTmp
[pNumprs
->cDig
] = *lpszStr
- 'A' + 10;
1753 else if ((*lpszStr
== 'e' || *lpszStr
== 'E') &&
1754 pNumprs
->dwInFlags
& NUMPRS_EXPONENT
&&
1755 !(pNumprs
->dwOutFlags
& NUMPRS_EXPONENT
))
1757 dwState
|= B_PROCESSING_EXPONENT
;
1758 pNumprs
->dwOutFlags
|= NUMPRS_EXPONENT
;
1761 else if (dwState
& B_PROCESSING_EXPONENT
&& *lpszStr
== chars
.cPositiveSymbol
)
1763 cchUsed
++; /* Ignore positive exponent */
1765 else if (dwState
& B_PROCESSING_EXPONENT
&& *lpszStr
== chars
.cNegativeSymbol
)
1767 dwState
|= B_NEGATIVE_EXPONENT
;
1771 break; /* Stop at an unrecognised character */
1776 if (!pNumprs
->cDig
&& dwState
& B_LEADING_ZERO
)
1778 /* Ensure a 0 on its own gets stored */
1783 if (pNumprs
->dwOutFlags
& NUMPRS_EXPONENT
&& dwState
& B_PROCESSING_EXPONENT
)
1785 pNumprs
->cchUsed
= cchUsed
;
1786 WARN("didn't completely parse exponent\n");
1787 return DISP_E_TYPEMISMATCH
; /* Failed to completely parse the exponent */
1790 if (pNumprs
->dwOutFlags
& NUMPRS_INEXACT
)
1792 if (dwState
& B_INEXACT_ZEROS
)
1793 pNumprs
->dwOutFlags
&= ~NUMPRS_INEXACT
; /* All zeros doesn't set NUMPRS_INEXACT */
1794 } else if(pNumprs
->dwInFlags
& NUMPRS_HEX_OCT
)
1796 /* copy all of the digits into the output digit buffer */
1797 /* this is exactly what windows does although it also returns */
1798 /* cDig of X and writes X+Y where Y>=0 number of digits to rgbDig */
1799 memcpy(rgbDig
, rgbTmp
, pNumprs
->cDig
* sizeof(BYTE
));
1801 if (dwState
& B_PROCESSING_HEX
) {
1802 /* hex numbers have always the same format */
1804 pNumprs
->nBaseShift
=4;
1806 if (dwState
& B_PROCESSING_OCT
) {
1807 /* oct numbers have always the same format */
1809 pNumprs
->nBaseShift
=3;
1811 while (pNumprs
->cDig
> 1 && !rgbTmp
[pNumprs
->cDig
- 1])
1820 /* Remove trailing zeros from the last (whole number or decimal) part */
1821 while (pNumprs
->cDig
> 1 && !rgbTmp
[pNumprs
->cDig
- 1])
1828 if (pNumprs
->cDig
<= iMaxDigits
)
1829 pNumprs
->dwOutFlags
&= ~NUMPRS_INEXACT
; /* Ignore stripped zeros for NUMPRS_INEXACT */
1831 pNumprs
->cDig
= iMaxDigits
; /* Only return iMaxDigits worth of digits */
1833 /* Copy the digits we processed into rgbDig */
1834 memcpy(rgbDig
, rgbTmp
, pNumprs
->cDig
* sizeof(BYTE
));
1836 /* Consume any trailing symbols and space */
1839 if ((pNumprs
->dwInFlags
& NUMPRS_TRAILING_WHITE
) && isspaceW(*lpszStr
))
1841 pNumprs
->dwOutFlags
|= NUMPRS_TRAILING_WHITE
;
1846 } while (isspaceW(*lpszStr
));
1848 else if (pNumprs
->dwInFlags
& NUMPRS_TRAILING_PLUS
&&
1849 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_PLUS
) &&
1850 *lpszStr
== chars
.cPositiveSymbol
)
1852 pNumprs
->dwOutFlags
|= NUMPRS_TRAILING_PLUS
;
1856 else if (pNumprs
->dwInFlags
& NUMPRS_TRAILING_MINUS
&&
1857 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_MINUS
) &&
1858 *lpszStr
== chars
.cNegativeSymbol
)
1860 pNumprs
->dwOutFlags
|= (NUMPRS_TRAILING_MINUS
|NUMPRS_NEG
);
1864 else if (pNumprs
->dwInFlags
& NUMPRS_PARENS
&& *lpszStr
== ')' &&
1865 pNumprs
->dwOutFlags
& NUMPRS_PARENS
)
1869 pNumprs
->dwOutFlags
|= NUMPRS_NEG
;
1875 if (pNumprs
->dwOutFlags
& NUMPRS_PARENS
&& !(pNumprs
->dwOutFlags
& NUMPRS_NEG
))
1877 pNumprs
->cchUsed
= cchUsed
;
1878 return DISP_E_TYPEMISMATCH
; /* Opening parenthesis not matched */
1881 if (pNumprs
->dwInFlags
& NUMPRS_USE_ALL
&& *lpszStr
!= '\0')
1882 return DISP_E_TYPEMISMATCH
; /* Not all chars were consumed */
1885 return DISP_E_TYPEMISMATCH
; /* No Number found */
1887 pNumprs
->cchUsed
= cchUsed
;
1891 /* VTBIT flags indicating an integer value */
1892 #define INTEGER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8)
1893 /* VTBIT flags indicating a real number value */
1894 #define REAL_VTBITS (VTBIT_R4|VTBIT_R8|VTBIT_CY)
1896 /* Helper macros to check whether bit pattern fits in VARIANT (x is a ULONG64 ) */
1897 #define FITS_AS_I1(x) ((x) >> 8 == 0)
1898 #define FITS_AS_I2(x) ((x) >> 16 == 0)
1899 #define FITS_AS_I4(x) ((x) >> 32 == 0)
1901 /**********************************************************************
1902 * VarNumFromParseNum [OLEAUT32.47]
1904 * Convert a NUMPARSE structure into a numeric Variant type.
1907 * pNumprs [I] Source for parsed number. cDig must be set to the size of rgbDig
1908 * rgbDig [I] Source for the numbers digits
1909 * dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
1910 * pVarDst [O] Destination for the converted Variant value.
1913 * Success: S_OK. pVarDst contains the converted value.
1914 * Failure: E_INVALIDARG, if any parameter is invalid.
1915 * DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
1918 * - The smallest favoured type present in dwVtBits that can represent the
1919 * number in pNumprs without losing precision is used.
1920 * - Signed types are preferrred over unsigned types of the same size.
1921 * - Preferred types in order are: integer, float, double, currency then decimal.
1922 * - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
1923 * for details of the rounding method.
1924 * - pVarDst is not cleared before the result is stored in it.
1925 * - WinXP and Win2003 support VTBIT_I8, VTBIT_UI8 but that's buggy (by
1926 * design?): If some other VTBIT's for integers are specified together
1927 * with VTBIT_I8 and the number will fit only in a VT_I8 Windows will "cast"
1928 * the number to the smallest requested integer truncating this way the
1929 * number. Wine dosn't implement this "feature" (yet?).
1931 HRESULT WINAPI
VarNumFromParseNum(NUMPARSE
*pNumprs
, BYTE
*rgbDig
,
1932 ULONG dwVtBits
, VARIANT
*pVarDst
)
1934 /* Scale factors and limits for double arithmetic */
1935 static const double dblMultipliers
[11] = {
1936 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
1937 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
1939 static const double dblMinimums
[11] = {
1940 R8_MIN
, R8_MIN
*10.0, R8_MIN
*100.0, R8_MIN
*1000.0, R8_MIN
*10000.0,
1941 R8_MIN
*100000.0, R8_MIN
*1000000.0, R8_MIN
*10000000.0,
1942 R8_MIN
*100000000.0, R8_MIN
*1000000000.0, R8_MIN
*10000000000.0
1944 static const double dblMaximums
[11] = {
1945 R8_MAX
, R8_MAX
/10.0, R8_MAX
/100.0, R8_MAX
/1000.0, R8_MAX
/10000.0,
1946 R8_MAX
/100000.0, R8_MAX
/1000000.0, R8_MAX
/10000000.0,
1947 R8_MAX
/100000000.0, R8_MAX
/1000000000.0, R8_MAX
/10000000000.0
1950 int wholeNumberDigits
, fractionalDigits
, divisor10
= 0, multiplier10
= 0;
1952 TRACE("(%p,%p,0x%lx,%p)\n", pNumprs
, rgbDig
, dwVtBits
, pVarDst
);
1954 if (pNumprs
->nBaseShift
)
1956 /* nBaseShift indicates a hex or octal number */
1961 /* Convert the hex or octal number string into a UI64 */
1962 for (i
= 0; i
< pNumprs
->cDig
; i
++)
1964 if (ul64
> ((UI8_MAX
>>pNumprs
->nBaseShift
) - rgbDig
[i
]))
1966 TRACE("Overflow multiplying digits\n");
1967 return DISP_E_OVERFLOW
;
1969 ul64
= (ul64
<<pNumprs
->nBaseShift
) + rgbDig
[i
];
1972 /* also make a negative representation */
1975 /* Try signed and unsigned types in size order */
1976 if (dwVtBits
& VTBIT_I1
&& FITS_AS_I1(ul64
))
1978 V_VT(pVarDst
) = VT_I1
;
1979 V_I1(pVarDst
) = ul64
;
1982 else if (dwVtBits
& VTBIT_UI1
&& FITS_AS_I1(ul64
))
1984 V_VT(pVarDst
) = VT_UI1
;
1985 V_UI1(pVarDst
) = ul64
;
1988 else if (dwVtBits
& VTBIT_I2
&& FITS_AS_I2(ul64
))
1990 V_VT(pVarDst
) = VT_I2
;
1991 V_I2(pVarDst
) = ul64
;
1994 else if (dwVtBits
& VTBIT_UI2
&& FITS_AS_I2(ul64
))
1996 V_VT(pVarDst
) = VT_UI2
;
1997 V_UI2(pVarDst
) = ul64
;
2000 else if (dwVtBits
& VTBIT_I4
&& FITS_AS_I4(ul64
))
2002 V_VT(pVarDst
) = VT_I4
;
2003 V_I4(pVarDst
) = ul64
;
2006 else if (dwVtBits
& VTBIT_UI4
&& FITS_AS_I4(ul64
))
2008 V_VT(pVarDst
) = VT_UI4
;
2009 V_UI4(pVarDst
) = ul64
;
2012 else if (dwVtBits
& VTBIT_I8
&& ((ul64
<= I8_MAX
)||(l64
>=I8_MIN
)))
2014 V_VT(pVarDst
) = VT_I8
;
2015 V_I8(pVarDst
) = ul64
;
2018 else if (dwVtBits
& VTBIT_UI8
)
2020 V_VT(pVarDst
) = VT_UI8
;
2021 V_UI8(pVarDst
) = ul64
;
2024 else if ((dwVtBits
& REAL_VTBITS
) == VTBIT_DECIMAL
)
2026 V_VT(pVarDst
) = VT_DECIMAL
;
2027 DEC_SIGNSCALE(&V_DECIMAL(pVarDst
)) = SIGNSCALE(DECIMAL_POS
,0);
2028 DEC_HI32(&V_DECIMAL(pVarDst
)) = 0;
2029 DEC_LO64(&V_DECIMAL(pVarDst
)) = ul64
;
2032 else if (dwVtBits
& VTBIT_R4
&& ((ul64
<= I4_MAX
)||(l64
>= I4_MIN
)))
2034 V_VT(pVarDst
) = VT_R4
;
2036 V_R4(pVarDst
) = ul64
;
2038 V_R4(pVarDst
) = l64
;
2041 else if (dwVtBits
& VTBIT_R8
&& ((ul64
<= I4_MAX
)||(l64
>= I4_MIN
)))
2043 V_VT(pVarDst
) = VT_R8
;
2045 V_R8(pVarDst
) = ul64
;
2047 V_R8(pVarDst
) = l64
;
2051 TRACE("Overflow: possible return types: 0x%lx, value: %s\n", dwVtBits
, wine_dbgstr_longlong(ul64
));
2052 return DISP_E_OVERFLOW
;
2055 /* Count the number of relevant fractional and whole digits stored,
2056 * And compute the divisor/multiplier to scale the number by.
2058 if (pNumprs
->nPwr10
< 0)
2060 if (-pNumprs
->nPwr10
>= pNumprs
->cDig
)
2062 /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
2063 wholeNumberDigits
= 0;
2064 fractionalDigits
= pNumprs
->cDig
;
2065 divisor10
= -pNumprs
->nPwr10
;
2069 /* An exactly represented real number e.g. 1.024 */
2070 wholeNumberDigits
= pNumprs
->cDig
+ pNumprs
->nPwr10
;
2071 fractionalDigits
= pNumprs
->cDig
- wholeNumberDigits
;
2072 divisor10
= pNumprs
->cDig
- wholeNumberDigits
;
2075 else if (pNumprs
->nPwr10
== 0)
2077 /* An exactly represented whole number e.g. 1024 */
2078 wholeNumberDigits
= pNumprs
->cDig
;
2079 fractionalDigits
= 0;
2081 else /* pNumprs->nPwr10 > 0 */
2083 /* A whole number followed by nPwr10 0's e.g. 102400 */
2084 wholeNumberDigits
= pNumprs
->cDig
;
2085 fractionalDigits
= 0;
2086 multiplier10
= pNumprs
->nPwr10
;
2089 TRACE("cDig %d; nPwr10 %d, whole %d, frac %d ", pNumprs
->cDig
,
2090 pNumprs
->nPwr10
, wholeNumberDigits
, fractionalDigits
);
2091 TRACE("mult %d; div %d\n", multiplier10
, divisor10
);
2093 if (dwVtBits
& (INTEGER_VTBITS
|VTBIT_DECIMAL
) &&
2094 (!fractionalDigits
|| !(dwVtBits
& (REAL_VTBITS
|VTBIT_CY
|VTBIT_DECIMAL
))))
2096 /* We have one or more integer output choices, and either:
2097 * 1) An integer input value, or
2098 * 2) A real number input value but no floating output choices.
2099 * Alternately, we have a DECIMAL output available and an integer input.
2101 * So, place the integer value into pVarDst, using the smallest type
2102 * possible and preferring signed over unsigned types.
2104 BOOL bOverflow
= FALSE
, bNegative
;
2108 /* Convert the integer part of the number into a UI8 */
2109 for (i
= 0; i
< wholeNumberDigits
; i
++)
2111 if (ul64
> (UI8_MAX
/ 10 - rgbDig
[i
]))
2113 TRACE("Overflow multiplying digits\n");
2117 ul64
= ul64
* 10 + rgbDig
[i
];
2120 /* Account for the scale of the number */
2121 if (!bOverflow
&& multiplier10
)
2123 for (i
= 0; i
< multiplier10
; i
++)
2125 if (ul64
> (UI8_MAX
/ 10))
2127 TRACE("Overflow scaling number\n");
2135 /* If we have any fractional digits, round the value.
2136 * Note we don't have to do this if divisor10 is < 1,
2137 * because this means the fractional part must be < 0.5
2139 if (!bOverflow
&& fractionalDigits
&& divisor10
> 0)
2141 const BYTE
* fracDig
= rgbDig
+ wholeNumberDigits
;
2142 BOOL bAdjust
= FALSE
;
2144 TRACE("first decimal value is %d\n", *fracDig
);
2147 bAdjust
= TRUE
; /* > 0.5 */
2148 else if (*fracDig
== 5)
2150 for (i
= 1; i
< fractionalDigits
; i
++)
2154 bAdjust
= TRUE
; /* > 0.5 */
2158 /* If exactly 0.5, round only odd values */
2159 if (i
== fractionalDigits
&& (ul64
& 1))
2165 if (ul64
== UI8_MAX
)
2167 TRACE("Overflow after rounding\n");
2174 /* Zero is not a negative number */
2175 bNegative
= pNumprs
->dwOutFlags
& NUMPRS_NEG
&& ul64
? TRUE
: FALSE
;
2177 TRACE("Integer value is %lld, bNeg %d\n", ul64
, bNegative
);
2179 /* For negative integers, try the signed types in size order */
2180 if (!bOverflow
&& bNegative
)
2182 if (dwVtBits
& (VTBIT_I1
|VTBIT_I2
|VTBIT_I4
|VTBIT_I8
))
2184 if (dwVtBits
& VTBIT_I1
&& ul64
<= -I1_MIN
)
2186 V_VT(pVarDst
) = VT_I1
;
2187 V_I1(pVarDst
) = -ul64
;
2190 else if (dwVtBits
& VTBIT_I2
&& ul64
<= -I2_MIN
)
2192 V_VT(pVarDst
) = VT_I2
;
2193 V_I2(pVarDst
) = -ul64
;
2196 else if (dwVtBits
& VTBIT_I4
&& ul64
<= -((LONGLONG
)I4_MIN
))
2198 V_VT(pVarDst
) = VT_I4
;
2199 V_I4(pVarDst
) = -ul64
;
2202 else if (dwVtBits
& VTBIT_I8
&& ul64
<= (ULONGLONG
)I8_MAX
+ 1)
2204 V_VT(pVarDst
) = VT_I8
;
2205 V_I8(pVarDst
) = -ul64
;
2208 else if ((dwVtBits
& REAL_VTBITS
) == VTBIT_DECIMAL
)
2210 /* Decimal is only output choice left - fast path */
2211 V_VT(pVarDst
) = VT_DECIMAL
;
2212 DEC_SIGNSCALE(&V_DECIMAL(pVarDst
)) = SIGNSCALE(DECIMAL_NEG
,0);
2213 DEC_HI32(&V_DECIMAL(pVarDst
)) = 0;
2214 DEC_LO64(&V_DECIMAL(pVarDst
)) = -ul64
;
2219 else if (!bOverflow
)
2221 /* For positive integers, try signed then unsigned types in size order */
2222 if (dwVtBits
& VTBIT_I1
&& ul64
<= I1_MAX
)
2224 V_VT(pVarDst
) = VT_I1
;
2225 V_I1(pVarDst
) = ul64
;
2228 else if (dwVtBits
& VTBIT_UI1
&& ul64
<= UI1_MAX
)
2230 V_VT(pVarDst
) = VT_UI1
;
2231 V_UI1(pVarDst
) = ul64
;
2234 else if (dwVtBits
& VTBIT_I2
&& ul64
<= I2_MAX
)
2236 V_VT(pVarDst
) = VT_I2
;
2237 V_I2(pVarDst
) = ul64
;
2240 else if (dwVtBits
& VTBIT_UI2
&& ul64
<= UI2_MAX
)
2242 V_VT(pVarDst
) = VT_UI2
;
2243 V_UI2(pVarDst
) = ul64
;
2246 else if (dwVtBits
& VTBIT_I4
&& ul64
<= I4_MAX
)
2248 V_VT(pVarDst
) = VT_I4
;
2249 V_I4(pVarDst
) = ul64
;
2252 else if (dwVtBits
& VTBIT_UI4
&& ul64
<= UI4_MAX
)
2254 V_VT(pVarDst
) = VT_UI4
;
2255 V_UI4(pVarDst
) = ul64
;
2258 else if (dwVtBits
& VTBIT_I8
&& ul64
<= I8_MAX
)
2260 V_VT(pVarDst
) = VT_I8
;
2261 V_I8(pVarDst
) = ul64
;
2264 else if (dwVtBits
& VTBIT_UI8
)
2266 V_VT(pVarDst
) = VT_UI8
;
2267 V_UI8(pVarDst
) = ul64
;
2270 else if ((dwVtBits
& REAL_VTBITS
) == VTBIT_DECIMAL
)
2272 /* Decimal is only output choice left - fast path */
2273 V_VT(pVarDst
) = VT_DECIMAL
;
2274 DEC_SIGNSCALE(&V_DECIMAL(pVarDst
)) = SIGNSCALE(DECIMAL_POS
,0);
2275 DEC_HI32(&V_DECIMAL(pVarDst
)) = 0;
2276 DEC_LO64(&V_DECIMAL(pVarDst
)) = ul64
;
2282 if (dwVtBits
& REAL_VTBITS
)
2284 /* Try to put the number into a float or real */
2285 BOOL bOverflow
= FALSE
, bNegative
= pNumprs
->dwOutFlags
& NUMPRS_NEG
;
2289 /* Convert the number into a double */
2290 for (i
= 0; i
< pNumprs
->cDig
; i
++)
2291 whole
= whole
* 10.0 + rgbDig
[i
];
2293 TRACE("Whole double value is %16.16g\n", whole
);
2295 /* Account for the scale */
2296 while (multiplier10
> 10)
2298 if (whole
> dblMaximums
[10])
2300 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
);
2304 whole
= whole
* dblMultipliers
[10];
2309 if (whole
> dblMaximums
[multiplier10
])
2311 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
);
2315 whole
= whole
* dblMultipliers
[multiplier10
];
2318 TRACE("Scaled double value is %16.16g\n", whole
);
2320 while (divisor10
> 10)
2322 if (whole
< dblMinimums
[10] && whole
!= 0)
2324 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
); /* Underflow */
2328 whole
= whole
/ dblMultipliers
[10];
2333 if (whole
< dblMinimums
[divisor10
] && whole
!= 0)
2335 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
); /* Underflow */
2339 whole
= whole
/ dblMultipliers
[divisor10
];
2342 TRACE("Final double value is %16.16g\n", whole
);
2344 if (dwVtBits
& VTBIT_R4
&&
2345 ((whole
<= R4_MAX
&& whole
>= R4_MIN
) || whole
== 0.0))
2347 TRACE("Set R4 to final value\n");
2348 V_VT(pVarDst
) = VT_R4
; /* Fits into a float */
2349 V_R4(pVarDst
) = pNumprs
->dwOutFlags
& NUMPRS_NEG
? -whole
: whole
;
2353 if (dwVtBits
& VTBIT_R8
)
2355 TRACE("Set R8 to final value\n");
2356 V_VT(pVarDst
) = VT_R8
; /* Fits into a double */
2357 V_R8(pVarDst
) = pNumprs
->dwOutFlags
& NUMPRS_NEG
? -whole
: whole
;
2361 if (dwVtBits
& VTBIT_CY
)
2363 if (SUCCEEDED(VarCyFromR8(bNegative
? -whole
: whole
, &V_CY(pVarDst
))))
2365 V_VT(pVarDst
) = VT_CY
; /* Fits into a currency */
2366 TRACE("Set CY to final value\n");
2369 TRACE("Value Overflows CY\n");
2373 if (dwVtBits
& VTBIT_DECIMAL
)
2378 DECIMAL
* pDec
= &V_DECIMAL(pVarDst
);
2380 DECIMAL_SETZERO(*pDec
);
2383 if (pNumprs
->dwOutFlags
& NUMPRS_NEG
)
2384 DEC_SIGN(pDec
) = DECIMAL_NEG
;
2386 DEC_SIGN(pDec
) = DECIMAL_POS
;
2388 /* Factor the significant digits */
2389 for (i
= 0; i
< pNumprs
->cDig
; i
++)
2391 tmp
= (ULONG64
)DEC_LO32(pDec
) * 10 + rgbDig
[i
];
2392 carry
= (ULONG
)(tmp
>> 32);
2393 DEC_LO32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2394 tmp
= (ULONG64
)DEC_MID32(pDec
) * 10 + carry
;
2395 carry
= (ULONG
)(tmp
>> 32);
2396 DEC_MID32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2397 tmp
= (ULONG64
)DEC_HI32(pDec
) * 10 + carry
;
2398 DEC_HI32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2400 if (tmp
>> 32 & UI4_MAX
)
2402 VarNumFromParseNum_DecOverflow
:
2403 TRACE("Overflow\n");
2404 DEC_LO32(pDec
) = DEC_MID32(pDec
) = DEC_HI32(pDec
) = UI4_MAX
;
2405 return DISP_E_OVERFLOW
;
2409 /* Account for the scale of the number */
2410 while (multiplier10
> 0)
2412 tmp
= (ULONG64
)DEC_LO32(pDec
) * 10;
2413 carry
= (ULONG
)(tmp
>> 32);
2414 DEC_LO32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2415 tmp
= (ULONG64
)DEC_MID32(pDec
) * 10 + carry
;
2416 carry
= (ULONG
)(tmp
>> 32);
2417 DEC_MID32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2418 tmp
= (ULONG64
)DEC_HI32(pDec
) * 10 + carry
;
2419 DEC_HI32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2421 if (tmp
>> 32 & UI4_MAX
)
2422 goto VarNumFromParseNum_DecOverflow
;
2425 DEC_SCALE(pDec
) = divisor10
;
2427 V_VT(pVarDst
) = VT_DECIMAL
;
2430 return DISP_E_OVERFLOW
; /* No more output choices */
2433 /**********************************************************************
2434 * VarCat [OLEAUT32.318]
2436 * Concatenates one variant onto another.
2439 * left [I] First variant
2440 * right [I] Second variant
2441 * result [O] Result variant
2445 * Failure: An HRESULT error code indicating the error.
2447 HRESULT WINAPI
VarCat(LPVARIANT left
, LPVARIANT right
, LPVARIANT out
)
2449 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
),
2450 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
), out
);
2452 /* Should we VariantClear out? */
2453 /* Can we handle array, vector, by ref etc. */
2454 if ((V_VT(left
)&VT_TYPEMASK
) == VT_NULL
&&
2455 (V_VT(right
)&VT_TYPEMASK
) == VT_NULL
)
2457 V_VT(out
) = VT_NULL
;
2461 if (V_VT(left
) == VT_BSTR
&& V_VT(right
) == VT_BSTR
)
2463 V_VT(out
) = VT_BSTR
;
2464 VarBstrCat (V_BSTR(left
), V_BSTR(right
), &V_BSTR(out
));
2467 if (V_VT(left
) == VT_BSTR
) {
2471 V_VT(out
) = VT_BSTR
;
2472 VariantInit(&bstrvar
);
2473 hres
= VariantChangeTypeEx(&bstrvar
,right
,0,0,VT_BSTR
);
2475 FIXME("Failed to convert right side from vt %d to VT_BSTR?\n",V_VT(right
));
2478 VarBstrCat (V_BSTR(left
), V_BSTR(&bstrvar
), &V_BSTR(out
));
2481 if (V_VT(right
) == VT_BSTR
) {
2485 V_VT(out
) = VT_BSTR
;
2486 VariantInit(&bstrvar
);
2487 hres
= VariantChangeTypeEx(&bstrvar
,left
,0,0,VT_BSTR
);
2489 FIXME("Failed to convert right side from vt %d to VT_BSTR?\n",V_VT(right
));
2492 VarBstrCat (V_BSTR(&bstrvar
), V_BSTR(right
), &V_BSTR(out
));
2495 FIXME ("types %d / %d not supported\n",V_VT(left
)&VT_TYPEMASK
, V_VT(right
)&VT_TYPEMASK
);
2499 /* Wrapper around VariantChangeTypeEx() which permits changing a
2500 variant with VT_RESERVED flag set. Needed by VarCmp. */
2501 static HRESULT
_VarChangeTypeExWrap (VARIANTARG
* pvargDest
,
2502 VARIANTARG
* pvargSrc
, LCID lcid
, USHORT wFlags
, VARTYPE vt
)
2507 flags
= V_VT(pvargSrc
) & ~VT_TYPEMASK
;
2508 V_VT(pvargSrc
) &= ~VT_RESERVED
;
2509 res
= VariantChangeTypeEx(pvargDest
,pvargSrc
,lcid
,wFlags
,vt
);
2510 V_VT(pvargSrc
) |= flags
;
2515 /**********************************************************************
2516 * VarCmp [OLEAUT32.176]
2518 * Compare two variants.
2521 * left [I] First variant
2522 * right [I] Second variant
2523 * lcid [I] LCID (locale identifier) for the comparison
2524 * flags [I] Flags to be used in the comparision:
2525 * NORM_IGNORECASE, NORM_IGNORENONSPACE, NORM_IGNORESYMBOLS,
2526 * NORM_IGNOREWIDTH, NORM_IGNOREKANATYPE, NORM_IGNOREKASHIDA
2529 * VARCMP_LT: left variant is less than right variant.
2530 * VARCMP_EQ: input variants are equal.
2531 * VARCMP_LT: left variant is greater than right variant.
2532 * VARCMP_NULL: either one of the input variants is NULL.
2533 * Failure: An HRESULT error code indicating the error.
2536 * Native VarCmp up to and including WinXP dosn't like as input variants
2537 * I1, UI2, VT_UI4, UI8 and UINT. INT is accepted only as left variant.
2539 * If both input variants are ERROR then VARCMP_EQ will be returned, else
2540 * an ERROR variant will trigger an error.
2542 * Both input variants can have VT_RESERVED flag set which is ignored
2543 * unless one and only one of the variants is a BSTR and the other one
2544 * is not an EMPTY variant. All four VT_RESERVED combinations have a
2545 * different meaning:
2546 * - BSTR and other: BSTR is always greater than the other variant.
2547 * - BSTR|VT_RESERVED and other: a string comparision is performed.
2548 * - BSTR and other|VT_RESERVED: If the BSTR is a number a numeric
2549 * comparision will take place else the BSTR is always greater.
2550 * - BSTR|VT_RESERVED and other|VT_RESERVED: It seems that the other
2551 * variant is ignored and the return value depends only on the sign
2552 * of the BSTR if it is a number else the BSTR is always greater. A
2553 * positive BSTR is greater, a negative one is smaller than the other
2557 * VarBstrCmp for the lcid and flags usage.
2559 HRESULT WINAPI
VarCmp(LPVARIANT left
, LPVARIANT right
, LCID lcid
, DWORD flags
)
2561 VARTYPE lvt
, rvt
, vt
;
2566 TRACE("(%p->(%s%s),%p->(%s%s),0x%08lx,0x%08lx)\n", left
, debugstr_VT(left
),
2567 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
), lcid
, flags
);
2569 lvt
= V_VT(left
) & VT_TYPEMASK
;
2570 rvt
= V_VT(right
) & VT_TYPEMASK
;
2571 xmask
= (1 << lvt
) | (1 << rvt
);
2573 /* If we have any flag set except VT_RESERVED bail out.
2574 Same for the left input variant type > VT_INT and for the
2575 right input variant type > VT_I8. Yes, VT_INT is only supported
2576 as left variant. Go figure */
2577 if (((V_VT(left
) | V_VT(right
)) & ~VT_TYPEMASK
& ~VT_RESERVED
) ||
2578 lvt
> VT_INT
|| rvt
> VT_I8
) {
2579 return DISP_E_BADVARTYPE
;
2582 /* Don't ask me why but native VarCmp cannot handle: VT_I1, VT_UI2, VT_UI4,
2583 VT_UINT and VT_UI8. Tested with DCOM98, Win2k, WinXP */
2584 if (rvt
== VT_INT
|| xmask
& (VTBIT_I1
| VTBIT_UI2
| VTBIT_UI4
| VTBIT_UI8
|
2585 VTBIT_DISPATCH
| VTBIT_VARIANT
| VTBIT_UNKNOWN
| VTBIT_15
))
2586 return DISP_E_TYPEMISMATCH
;
2588 /* If both variants are VT_ERROR return VARCMP_EQ */
2589 if (xmask
== VTBIT_ERROR
)
2591 else if (xmask
& VTBIT_ERROR
)
2592 return DISP_E_TYPEMISMATCH
;
2594 if (xmask
& VTBIT_NULL
)
2600 /* Two BSTRs, ignore VT_RESERVED */
2601 if (xmask
== VTBIT_BSTR
)
2602 return VarBstrCmp(V_BSTR(left
), V_BSTR(right
), lcid
, flags
);
2604 /* A BSTR and an other variant; we have to take care of VT_RESERVED */
2605 if (xmask
& VTBIT_BSTR
) {
2606 VARIANT
*bstrv
, *nonbv
;
2610 /* Swap the variants so the BSTR is always on the left */
2611 if (lvt
== VT_BSTR
) {
2622 /* BSTR and EMPTY: ignore VT_RESERVED */
2623 if (nonbvt
== VT_EMPTY
)
2624 rc
= (!V_BSTR(bstrv
) || !*V_BSTR(bstrv
)) ? VARCMP_EQ
: VARCMP_GT
;
2626 VARTYPE breserv
= V_VT(bstrv
) & ~VT_TYPEMASK
;
2627 VARTYPE nreserv
= V_VT(nonbv
) & ~VT_TYPEMASK
;
2629 if (!breserv
&& !nreserv
)
2630 /* No VT_RESERVED set ==> BSTR always greater */
2632 else if (breserv
&& !nreserv
) {
2633 /* BSTR has VT_RESERVED set. Do a string comparision */
2634 rc
= VariantChangeTypeEx(&rv
,nonbv
,lcid
,0,VT_BSTR
);
2637 rc
= VarBstrCmp(V_BSTR(bstrv
), V_BSTR(&rv
), lcid
, flags
);
2638 } else if (V_BSTR(bstrv
) && *V_BSTR(bstrv
)) {
2639 /* Non NULL nor empty BSTR */
2640 /* If the BSTR is not a number the BSTR is greater */
2641 rc
= _VarChangeTypeExWrap(&lv
,bstrv
,lcid
,0,VT_R8
);
2644 else if (breserv
&& nreserv
)
2645 /* FIXME: This is strange: with both VT_RESERVED set it
2646 looks like the result depends only on the sign of
2648 rc
= (V_R8(&lv
) >= 0) ? VARCMP_GT
: VARCMP_LT
;
2650 /* Numeric comparision, will be handled below.
2651 VARCMP_NULL used only to break out. */
2656 /* Empty or NULL BSTR */
2659 /* Fixup the return code if we swapped left and right */
2661 if (rc
== VARCMP_GT
)
2663 else if (rc
== VARCMP_LT
)
2666 if (rc
!= VARCMP_NULL
)
2670 if (xmask
& VTBIT_DECIMAL
)
2672 else if (xmask
& VTBIT_BSTR
)
2674 else if (xmask
& VTBIT_R4
)
2676 else if (xmask
& (VTBIT_R8
| VTBIT_DATE
))
2678 else if (xmask
& VTBIT_CY
)
2684 /* Coerce the variants */
2685 rc
= _VarChangeTypeExWrap(&lv
,left
,lcid
,0,vt
);
2686 if (rc
== DISP_E_OVERFLOW
&& vt
!= VT_R8
) {
2687 /* Overflow, change to R8 */
2689 rc
= _VarChangeTypeExWrap(&lv
,left
,lcid
,0,vt
);
2693 rc
= _VarChangeTypeExWrap(&rv
,right
,lcid
,0,vt
);
2694 if (rc
== DISP_E_OVERFLOW
&& vt
!= VT_R8
) {
2695 /* Overflow, change to R8 */
2697 rc
= _VarChangeTypeExWrap(&lv
,left
,lcid
,0,vt
);
2700 rc
= _VarChangeTypeExWrap(&rv
,right
,lcid
,0,vt
);
2705 #define _VARCMP(a,b) \
2706 (((a) == (b)) ? VARCMP_EQ : (((a) < (b)) ? VARCMP_LT : VARCMP_GT))
2710 return VarCyCmp(V_CY(&lv
), V_CY(&rv
));
2712 return VarDecCmp(&V_DECIMAL(&lv
), &V_DECIMAL(&rv
));
2714 return _VARCMP(V_I8(&lv
), V_I8(&rv
));
2716 return _VARCMP(V_R4(&lv
), V_R4(&rv
));
2718 return _VARCMP(V_R8(&lv
), V_R8(&rv
));
2720 /* We should never get here */
2726 /**********************************************************************
2727 * VarAnd [OLEAUT32.142]
2729 * Computes the logical AND of two variants.
2732 * left [I] First variant
2733 * right [I] Second variant
2734 * result [O] Result variant
2738 * Failure: An HRESULT error code indicating the error.
2740 HRESULT WINAPI
VarAnd(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
2742 HRESULT rc
= E_FAIL
;
2744 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
),
2745 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
), result
);
2747 if ((V_VT(left
)&VT_TYPEMASK
) == VT_BOOL
&&
2748 (V_VT(right
)&VT_TYPEMASK
) == VT_BOOL
) {
2750 V_VT(result
) = VT_BOOL
;
2751 if (V_BOOL(left
) && V_BOOL(right
)) {
2752 V_BOOL(result
) = VARIANT_TRUE
;
2754 V_BOOL(result
) = VARIANT_FALSE
;
2765 int resT
= 0; /* Testing has shown I2 & I2 == I2, all else
2766 becomes I4, even unsigned ints (incl. UI2) */
2769 switch (V_VT(left
)&VT_TYPEMASK
) {
2770 case VT_I1
: lVal
= V_I1(left
); resT
=VT_I4
; break;
2771 case VT_I2
: lVal
= V_I2(left
); resT
=VT_I2
; break;
2773 case VT_INT
: lVal
= V_I4(left
); resT
=VT_I4
; break;
2774 case VT_UI1
: lVal
= V_UI1(left
); resT
=VT_I4
; break;
2775 case VT_UI2
: lVal
= V_UI2(left
); resT
=VT_I4
; break;
2777 case VT_UINT
: lVal
= V_UI4(left
); resT
=VT_I4
; break;
2778 case VT_BOOL
: rVal
= V_BOOL(left
); resT
=VT_I4
; break;
2779 default: lOk
= FALSE
;
2783 switch (V_VT(right
)&VT_TYPEMASK
) {
2784 case VT_I1
: rVal
= V_I1(right
); resT
=VT_I4
; break;
2785 case VT_I2
: rVal
= V_I2(right
); resT
=max(VT_I2
, resT
); break;
2787 case VT_INT
: rVal
= V_I4(right
); resT
=VT_I4
; break;
2788 case VT_UI1
: rVal
= V_UI1(right
); resT
=VT_I4
; break;
2789 case VT_UI2
: rVal
= V_UI2(right
); resT
=VT_I4
; break;
2791 case VT_UINT
: rVal
= V_UI4(right
); resT
=VT_I4
; break;
2792 case VT_BOOL
: rVal
= V_BOOL(right
); resT
=VT_I4
; break;
2793 default: rOk
= FALSE
;
2797 res
= (lVal
& rVal
);
2798 V_VT(result
) = resT
;
2800 case VT_I2
: V_I2(result
) = res
; break;
2801 case VT_I4
: V_I4(result
) = res
; break;
2803 FIXME("Unexpected result variant type %x\n", resT
);
2809 FIXME("VarAnd stub\n");
2813 TRACE("returning 0x%8lx (%s%s),%ld\n", rc
, debugstr_VT(result
),
2814 debugstr_VF(result
), V_VT(result
) == VT_I4
? V_I4(result
) : V_I2(result
));
2818 /**********************************************************************
2819 * VarAdd [OLEAUT32.141]
2824 * left [I] First variant
2825 * right [I] Second variant
2826 * result [O] Result variant
2830 * Failure: An HRESULT error code indicating the error.
2833 * Native VarAdd up to and including WinXP dosn't like as input variants
2834 * I1, UI2, UI4, UI8, INT and UINT.
2836 * Native VarAdd dosn't check for NULL in/out pointers and crashes. We do the
2840 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
2843 HRESULT WINAPI
VarAdd(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
2846 VARTYPE lvt
, rvt
, resvt
, tvt
;
2850 /* Variant priority for coercion. Sorted from lowest to highest.
2851 VT_ERROR shows an invalid input variant type. */
2852 enum coerceprio
{ vt_EMPTY
, vt_UI1
, vt_I2
, vt_I4
, vt_I8
, vt_BSTR
,vt_R4
,
2853 vt_R8
, vt_CY
, vt_DATE
, vt_DECIMAL
, vt_DISPATCH
, vt_NULL
,
2855 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
2856 VARTYPE prio2vt
[] = { VT_EMPTY
, VT_UI1
, VT_I2
, VT_I4
, VT_I8
, VT_BSTR
, VT_R4
,
2857 VT_R8
, VT_CY
, VT_DATE
, VT_DECIMAL
, VT_DISPATCH
,
2858 VT_NULL
, VT_ERROR
};
2860 /* Mapping for coercion from input variant to priority of result variant. */
2861 static VARTYPE coerce
[] = {
2862 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
2863 vt_EMPTY
, vt_NULL
, vt_I2
, vt_I4
, vt_R4
,
2864 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
2865 vt_R8
, vt_CY
, vt_DATE
, vt_BSTR
, vt_DISPATCH
,
2866 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
2867 vt_ERROR
, vt_I2
, vt_ERROR
, vt_ERROR
, vt_DECIMAL
,
2868 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
2869 vt_ERROR
, vt_ERROR
, vt_UI1
, vt_ERROR
, vt_ERROR
, vt_I8
2872 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
),
2873 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
),
2879 lvt
= V_VT(left
)&VT_TYPEMASK
;
2880 rvt
= V_VT(right
)&VT_TYPEMASK
;
2882 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
2883 Same for any input variant type > VT_I8 */
2884 if (V_VT(left
) & ~VT_TYPEMASK
|| V_VT(right
) & ~VT_TYPEMASK
||
2885 lvt
> VT_I8
|| rvt
> VT_I8
) {
2886 hres
= DISP_E_BADVARTYPE
;
2890 /* Determine the variant type to coerce to. */
2891 if (coerce
[lvt
] > coerce
[rvt
]) {
2892 resvt
= prio2vt
[coerce
[lvt
]];
2893 tvt
= prio2vt
[coerce
[rvt
]];
2895 resvt
= prio2vt
[coerce
[rvt
]];
2896 tvt
= prio2vt
[coerce
[lvt
]];
2899 /* Special cases where the result variant type is defined by both
2900 input variants and not only that with the highest priority */
2901 if (resvt
== VT_BSTR
) {
2902 if (tvt
== VT_EMPTY
|| tvt
== VT_BSTR
)
2907 if (resvt
== VT_R4
&& (tvt
== VT_BSTR
|| tvt
== VT_I8
|| tvt
== VT_I4
))
2910 /* For overflow detection use the biggest compatible type for the
2914 hres
= DISP_E_BADVARTYPE
;
2918 V_VT(result
) = VT_NULL
;
2921 FIXME("cannot handle variant type VT_DISPATCH\n");
2922 hres
= DISP_E_TYPEMISMATCH
;
2941 /* Now coerce the variants */
2942 hres
= VariantChangeType(&lv
, left
, 0, tvt
);
2945 hres
= VariantChangeType(&rv
, right
, 0, tvt
);
2951 V_VT(result
) = resvt
;
2954 hres
= VarDecAdd(&V_DECIMAL(&lv
), &V_DECIMAL(&rv
),
2955 &V_DECIMAL(result
));
2958 hres
= VarCyAdd(V_CY(&lv
), V_CY(&rv
), &V_CY(result
));
2961 /* We do not add those, we concatenate them. */
2962 hres
= VarBstrCat(V_BSTR(&lv
), V_BSTR(&rv
), &V_BSTR(result
));
2965 /* Overflow detection */
2966 r8res
= (double)V_I8(&lv
) + (double)V_I8(&rv
);
2967 if (r8res
> (double)I8_MAX
|| r8res
< (double)I8_MIN
) {
2968 V_VT(result
) = VT_R8
;
2969 V_R8(result
) = r8res
;
2973 V_I8(&tv
) = V_I8(&lv
) + V_I8(&rv
);
2978 /* FIXME: overflow detection */
2979 V_R8(&tv
) = V_R8(&lv
) + V_R8(&rv
);
2982 ERR("We shouldn't get here! tvt = %d!\n", tvt
);
2986 if ((hres
= VariantChangeType(result
, &tv
, 0, resvt
)) != S_OK
) {
2987 /* Overflow! Change to the vartype with the next higher priority.
2988 With one exception: I4 ==> R8 even if it would fit in I8 */
2992 resvt
= prio2vt
[coerce
[resvt
] + 1];
2993 hres
= VariantChangeType(result
, &tv
, 0, resvt
);
2996 hres
= VariantCopy(result
, &tv
);
3000 V_VT(result
) = VT_EMPTY
;
3001 V_I4(result
) = 0; /* No V_EMPTY */
3006 TRACE("returning 0x%8lx (variant type %s)\n", hres
, debugstr_VT(result
));
3010 /**********************************************************************
3011 * VarMul [OLEAUT32.156]
3013 * Multiply two variants.
3016 * left [I] First variant
3017 * right [I] Second variant
3018 * result [O] Result variant
3022 * Failure: An HRESULT error code indicating the error.
3025 * Native VarMul up to and including WinXP dosn't like as input variants
3026 * I1, UI2, UI4, UI8, INT and UINT. But it can multiply apples with oranges.
3028 * Native VarMul dosn't check for NULL in/out pointers and crashes. We do the
3032 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3035 HRESULT WINAPI
VarMul(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3038 VARTYPE lvt
, rvt
, resvt
, tvt
;
3042 /* Variant priority for coercion. Sorted from lowest to highest.
3043 VT_ERROR shows an invalid input variant type. */
3044 enum coerceprio
{ vt_UI1
= 0, vt_I2
, vt_I4
, vt_I8
, vt_CY
, vt_R4
, vt_R8
,
3045 vt_DECIMAL
, vt_NULL
, vt_ERROR
};
3046 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3047 VARTYPE prio2vt
[] = { VT_UI1
, VT_I2
, VT_I4
, VT_I8
, VT_CY
, VT_R4
, VT_R8
,
3048 VT_DECIMAL
, VT_NULL
, VT_ERROR
};
3050 /* Mapping for coercion from input variant to priority of result variant. */
3051 static VARTYPE coerce
[] = {
3052 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3053 vt_UI1
, vt_NULL
, vt_I2
, vt_I4
, vt_R4
,
3054 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3055 vt_R8
, vt_CY
, vt_R8
, vt_R8
, vt_ERROR
,
3056 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3057 vt_ERROR
, vt_I2
, vt_ERROR
, vt_ERROR
, vt_DECIMAL
,
3058 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3059 vt_ERROR
, vt_ERROR
, vt_UI1
, vt_ERROR
, vt_ERROR
, vt_I8
3062 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
),
3063 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
),
3069 lvt
= V_VT(left
)&VT_TYPEMASK
;
3070 rvt
= V_VT(right
)&VT_TYPEMASK
;
3072 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3073 Same for any input variant type > VT_I8 */
3074 if (V_VT(left
) & ~VT_TYPEMASK
|| V_VT(right
) & ~VT_TYPEMASK
||
3075 lvt
> VT_I8
|| rvt
> VT_I8
) {
3076 hres
= DISP_E_BADVARTYPE
;
3080 /* Determine the variant type to coerce to. */
3081 if (coerce
[lvt
] > coerce
[rvt
]) {
3082 resvt
= prio2vt
[coerce
[lvt
]];
3083 tvt
= prio2vt
[coerce
[rvt
]];
3085 resvt
= prio2vt
[coerce
[rvt
]];
3086 tvt
= prio2vt
[coerce
[lvt
]];
3089 /* Special cases where the result variant type is defined by both
3090 input variants and not only that with the highest priority */
3091 if (resvt
== VT_R4
&& (tvt
== VT_CY
|| tvt
== VT_I8
|| tvt
== VT_I4
))
3093 if (lvt
== VT_EMPTY
&& rvt
== VT_EMPTY
)
3096 /* For overflow detection use the biggest compatible type for the
3100 hres
= DISP_E_BADVARTYPE
;
3104 V_VT(result
) = VT_NULL
;
3119 /* Now coerce the variants */
3120 hres
= VariantChangeType(&lv
, left
, 0, tvt
);
3123 hres
= VariantChangeType(&rv
, right
, 0, tvt
);
3130 V_VT(result
) = resvt
;
3133 hres
= VarDecMul(&V_DECIMAL(&lv
), &V_DECIMAL(&rv
),
3134 &V_DECIMAL(result
));
3137 hres
= VarCyMul(V_CY(&lv
), V_CY(&rv
), &V_CY(result
));
3140 /* Overflow detection */
3141 r8res
= (double)V_I8(&lv
) * (double)V_I8(&rv
);
3142 if (r8res
> (double)I8_MAX
|| r8res
< (double)I8_MIN
) {
3143 V_VT(result
) = VT_R8
;
3144 V_R8(result
) = r8res
;
3147 V_I8(&tv
) = V_I8(&lv
) * V_I8(&rv
);
3150 /* FIXME: overflow detection */
3151 V_R8(&tv
) = V_R8(&lv
) * V_R8(&rv
);
3154 ERR("We shouldn't get here! tvt = %d!\n", tvt
);
3158 while ((hres
= VariantChangeType(result
, &tv
, 0, resvt
)) != S_OK
) {
3159 /* Overflow! Change to the vartype with the next higher priority.
3160 With one exception: I4 ==> R8 even if it would fit in I8 */
3164 resvt
= prio2vt
[coerce
[resvt
] + 1];
3167 hres
= VariantCopy(result
, &tv
);
3171 V_VT(result
) = VT_EMPTY
;
3172 V_I4(result
) = 0; /* No V_EMPTY */
3177 TRACE("returning 0x%8lx (variant type %s)\n", hres
, debugstr_VT(result
));
3181 /**********************************************************************
3182 * VarDiv [OLEAUT32.143]
3184 * Divides one variant with another.
3187 * left [I] First variant
3188 * right [I] Second variant
3189 * result [O] Result variant
3193 * Failure: An HRESULT error code indicating the error.
3195 HRESULT WINAPI
VarDiv(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3197 HRESULT rc
= E_FAIL
;
3198 VARTYPE lvt
,rvt
,resvt
;
3202 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
),
3203 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
), result
);
3205 VariantInit(&lv
);VariantInit(&rv
);
3206 lvt
= V_VT(left
)&VT_TYPEMASK
;
3207 rvt
= V_VT(right
)&VT_TYPEMASK
;
3208 found
= FALSE
;resvt
= VT_VOID
;
3209 if (((1<<lvt
) | (1<<rvt
)) & (VTBIT_R4
|VTBIT_R8
|VTBIT_CY
)) {
3213 if (!found
&& (((1<<lvt
) | (1<<rvt
)) & (VTBIT_DECIMAL
))) {
3217 if (!found
&& (((1<<lvt
) | (1<<rvt
)) & (VTBIT_I1
|VTBIT_I2
|VTBIT_UI1
|VTBIT_UI2
|VTBIT_I4
|VTBIT_UI4
|VTBIT_INT
|VTBIT_UINT
))) {
3222 FIXME("can't expand vt %d vs %d to a target type.\n",lvt
,rvt
);
3225 rc
= VariantChangeType(&lv
, left
, 0, resvt
);
3227 FIXME("Could not convert 0x%x to %d?\n",V_VT(left
),resvt
);
3230 rc
= VariantChangeType(&rv
, right
, 0, resvt
);
3232 FIXME("Could not convert 0x%x to %d?\n",V_VT(right
),resvt
);
3237 if (V_R8(&rv
) == 0) return DISP_E_DIVBYZERO
;
3238 V_VT(result
) = resvt
;
3239 V_R8(result
) = V_R8(&lv
) / V_R8(&rv
);
3243 rc
= VarDecDiv(&(V_DECIMAL(&lv
)), &(V_DECIMAL(&rv
)), &(V_DECIMAL(result
)));
3244 V_VT(result
) = resvt
;
3247 if (V_I4(&rv
) == 0) return DISP_E_DIVBYZERO
;
3248 V_VT(result
) = resvt
;
3249 V_I4(result
) = V_I4(&lv
) / V_I4(&rv
);
3253 TRACE("returning 0x%8lx (%s%s),%g\n", rc
, debugstr_VT(result
),
3254 debugstr_VF(result
), V_VT(result
) == VT_R8
? V_R8(result
) : (double)V_I4(result
));
3258 /**********************************************************************
3259 * VarSub [OLEAUT32.159]
3261 * Subtract two variants.
3264 * left [I] First variant
3265 * right [I] Second variant
3266 * result [O] Result variant
3270 * Failure: An HRESULT error code indicating the error.
3272 HRESULT WINAPI
VarSub(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3274 HRESULT rc
= E_FAIL
;
3275 VARTYPE lvt
,rvt
,resvt
;
3279 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
),
3280 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
), result
);
3282 VariantInit(&lv
);VariantInit(&rv
);
3283 lvt
= V_VT(left
)&VT_TYPEMASK
;
3284 rvt
= V_VT(right
)&VT_TYPEMASK
;
3285 found
= FALSE
;resvt
= VT_VOID
;
3286 if (((1<<lvt
) | (1<<rvt
)) & (VTBIT_DATE
|VTBIT_R4
|VTBIT_R8
)) {
3290 if (!found
&& (((1<<lvt
) | (1<<rvt
)) & (VTBIT_DECIMAL
))) {
3294 if (!found
&& (((1<<lvt
) | (1<<rvt
)) & (VTBIT_I1
|VTBIT_I2
|VTBIT_UI1
|VTBIT_UI2
|VTBIT_I4
|VTBIT_UI4
|VTBIT_INT
|VTBIT_UINT
))) {
3299 FIXME("can't expand vt %d vs %d to a target type.\n",lvt
,rvt
);
3302 rc
= VariantChangeType(&lv
, left
, 0, resvt
);
3304 FIXME("Could not convert 0x%x to %d?\n",V_VT(left
),resvt
);
3307 rc
= VariantChangeType(&rv
, right
, 0, resvt
);
3309 FIXME("Could not convert 0x%x to %d?\n",V_VT(right
),resvt
);
3314 V_VT(result
) = resvt
;
3315 V_R8(result
) = V_R8(&lv
) - V_R8(&rv
);
3319 rc
= VarDecSub(&(V_DECIMAL(&lv
)), &(V_DECIMAL(&rv
)), &(V_DECIMAL(result
)));
3320 V_VT(result
) = resvt
;
3323 V_VT(result
) = resvt
;
3324 V_I4(result
) = V_I4(&lv
) - V_I4(&rv
);
3328 TRACE("returning 0x%8lx (%s%s),%g\n", rc
, debugstr_VT(result
),
3329 debugstr_VF(result
), V_VT(result
) == VT_R8
? V_R8(result
) : (double)V_I4(result
));
3333 /**********************************************************************
3334 * VarOr [OLEAUT32.157]
3336 * Perform a logical or (OR) operation on two variants.
3339 * pVarLeft [I] First variant
3340 * pVarRight [I] Variant to OR with pVarLeft
3341 * pVarOut [O] Destination for OR result
3344 * Success: S_OK. pVarOut contains the result of the operation with its type
3345 * taken from the table listed under VarXor().
3346 * Failure: An HRESULT error code indicating the error.
3349 * See the Notes section of VarXor() for further information.
3351 HRESULT WINAPI
VarOr(LPVARIANT pVarLeft
, LPVARIANT pVarRight
, LPVARIANT pVarOut
)
3354 VARIANT varLeft
, varRight
, varStr
;
3357 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft
, debugstr_VT(pVarLeft
),
3358 debugstr_VF(pVarLeft
), pVarRight
, debugstr_VT(pVarRight
),
3359 debugstr_VF(pVarRight
), pVarOut
);
3361 if (V_EXTRA_TYPE(pVarLeft
) || V_EXTRA_TYPE(pVarRight
) ||
3362 V_VT(pVarLeft
) == VT_UNKNOWN
|| V_VT(pVarRight
) == VT_UNKNOWN
||
3363 V_VT(pVarLeft
) == VT_DISPATCH
|| V_VT(pVarRight
) == VT_DISPATCH
||
3364 V_VT(pVarLeft
) == VT_RECORD
|| V_VT(pVarRight
) == VT_RECORD
)
3365 return DISP_E_BADVARTYPE
;
3367 V_VT(&varLeft
) = V_VT(&varRight
) = V_VT(&varStr
) = VT_EMPTY
;
3369 if (V_VT(pVarLeft
) == VT_NULL
|| V_VT(pVarRight
) == VT_NULL
)
3371 /* NULL OR Zero is NULL, NULL OR value is value */
3372 if (V_VT(pVarLeft
) == VT_NULL
)
3373 pVarLeft
= pVarRight
; /* point to the non-NULL var */
3375 V_VT(pVarOut
) = VT_NULL
;
3378 switch (V_VT(pVarLeft
))
3380 case VT_DATE
: case VT_R8
:
3385 if (V_BOOL(pVarLeft
))
3386 *pVarOut
= *pVarLeft
;
3388 case VT_I2
: case VT_UI2
:
3397 if (V_UI1(pVarLeft
))
3398 *pVarOut
= *pVarLeft
;
3404 case VT_I4
: case VT_UI4
: case VT_INT
: case VT_UINT
:
3409 if (V_CY(pVarLeft
).int64
)
3412 case VT_I8
: case VT_UI8
:
3417 if (DEC_HI32(&V_DECIMAL(pVarLeft
)) || DEC_LO64(&V_DECIMAL(pVarLeft
)))
3424 if (!V_BSTR(pVarLeft
))
3425 return DISP_E_BADVARTYPE
;
3427 hRet
= VarBoolFromStr(V_BSTR(pVarLeft
), LOCALE_USER_DEFAULT
, VAR_LOCALBOOL
, &b
);
3428 if (SUCCEEDED(hRet
) && b
)
3430 V_VT(pVarOut
) = VT_BOOL
;
3431 V_BOOL(pVarOut
) = b
;
3435 case VT_NULL
: case VT_EMPTY
:
3436 V_VT(pVarOut
) = VT_NULL
;
3439 return DISP_E_BADVARTYPE
;
3443 if (V_VT(pVarLeft
) == VT_EMPTY
|| V_VT(pVarRight
) == VT_EMPTY
)
3445 if (V_VT(pVarLeft
) == VT_EMPTY
)
3446 pVarLeft
= pVarRight
; /* point to the non-EMPTY var */
3449 /* Since one argument is empty (0), OR'ing it with the other simply
3450 * gives the others value (as 0|x => x). So just convert the other
3451 * argument to the required result type.
3453 switch (V_VT(pVarLeft
))
3456 if (!V_BSTR(pVarLeft
))
3457 return DISP_E_BADVARTYPE
;
3459 hRet
= VariantCopy(&varStr
, pVarLeft
);
3463 hRet
= VariantChangeType(pVarLeft
, pVarLeft
, 0, VT_BOOL
);
3466 /* Fall Through ... */
3467 case VT_EMPTY
: case VT_UI1
: case VT_BOOL
: case VT_I2
:
3468 V_VT(pVarOut
) = VT_I2
;
3470 case VT_DATE
: case VT_CY
: case VT_DECIMAL
: case VT_R4
: case VT_R8
:
3471 case VT_I1
: case VT_UI2
: case VT_I4
: case VT_UI4
:
3472 case VT_INT
: case VT_UINT
: case VT_UI8
:
3473 V_VT(pVarOut
) = VT_I4
;
3476 V_VT(pVarOut
) = VT_I8
;
3479 return DISP_E_BADVARTYPE
;
3481 hRet
= VariantCopy(&varLeft
, pVarLeft
);
3484 pVarLeft
= &varLeft
;
3485 hRet
= VariantChangeType(pVarOut
, pVarLeft
, 0, V_VT(pVarOut
));
3489 if (V_VT(pVarLeft
) == VT_BOOL
&& V_VT(pVarRight
) == VT_BOOL
)
3491 V_VT(pVarOut
) = VT_BOOL
;
3492 V_BOOL(pVarOut
) = V_BOOL(pVarLeft
) | V_BOOL(pVarRight
);
3496 if (V_VT(pVarLeft
) == VT_UI1
&& V_VT(pVarRight
) == VT_UI1
)
3498 V_VT(pVarOut
) = VT_UI1
;
3499 V_UI1(pVarOut
) = V_UI1(pVarLeft
) | V_UI1(pVarRight
);
3503 if (V_VT(pVarLeft
) == VT_BSTR
)
3505 hRet
= VariantCopy(&varStr
, pVarLeft
);
3509 hRet
= VariantChangeType(pVarLeft
, pVarLeft
, 0, VT_BOOL
);
3514 if (V_VT(pVarLeft
) == VT_BOOL
&&
3515 (V_VT(pVarRight
) == VT_BOOL
|| V_VT(pVarRight
) == VT_BSTR
))
3519 else if ((V_VT(pVarLeft
) == VT_BOOL
|| V_VT(pVarLeft
) == VT_UI1
||
3520 V_VT(pVarLeft
) == VT_I2
|| V_VT(pVarLeft
) == VT_BSTR
) &&
3521 (V_VT(pVarRight
) == VT_BOOL
|| V_VT(pVarRight
) == VT_UI1
||
3522 V_VT(pVarRight
) == VT_I2
|| V_VT(pVarRight
) == VT_BSTR
))
3526 else if (V_VT(pVarLeft
) == VT_I8
|| V_VT(pVarRight
) == VT_I8
)
3528 if (V_VT(pVarLeft
) == VT_INT
|| V_VT(pVarRight
) == VT_INT
)
3529 return DISP_E_TYPEMISMATCH
;
3533 hRet
= VariantCopy(&varLeft
, pVarLeft
);
3537 hRet
= VariantCopy(&varRight
, pVarRight
);
3541 if (vt
== VT_I4
&& V_VT(&varLeft
) == VT_UI4
)
3542 V_VT(&varLeft
) = VT_I4
; /* Don't overflow */
3547 if (V_VT(&varLeft
) == VT_BSTR
&&
3548 FAILED(VarR8FromStr(V_BSTR(&varLeft
), LOCALE_USER_DEFAULT
, 0, &d
)))
3549 hRet
= VariantChangeType(&varLeft
, &varLeft
, VARIANT_LOCALBOOL
, VT_BOOL
);
3550 if (SUCCEEDED(hRet
) && V_VT(&varLeft
) != vt
)
3551 hRet
= VariantChangeType(&varLeft
, &varLeft
, 0, vt
);
3556 if (vt
== VT_I4
&& V_VT(&varRight
) == VT_UI4
)
3557 V_VT(&varRight
) = VT_I4
; /* Don't overflow */
3562 if (V_VT(&varRight
) == VT_BSTR
&&
3563 FAILED(VarR8FromStr(V_BSTR(&varRight
), LOCALE_USER_DEFAULT
, 0, &d
)))
3564 hRet
= VariantChangeType(&varRight
, &varRight
, VARIANT_LOCALBOOL
, VT_BOOL
);
3565 if (SUCCEEDED(hRet
) && V_VT(&varRight
) != vt
)
3566 hRet
= VariantChangeType(&varRight
, &varRight
, 0, vt
);
3574 V_I8(pVarOut
) = V_I8(&varLeft
) | V_I8(&varRight
);
3576 else if (vt
== VT_I4
)
3578 V_I4(pVarOut
) = V_I4(&varLeft
) | V_I4(&varRight
);
3582 V_I2(pVarOut
) = V_I2(&varLeft
) | V_I2(&varRight
);
3586 VariantClear(&varStr
);
3587 VariantClear(&varLeft
);
3588 VariantClear(&varRight
);
3592 /**********************************************************************
3593 * VarAbs [OLEAUT32.168]
3595 * Convert a variant to its absolute value.
3598 * pVarIn [I] Source variant
3599 * pVarOut [O] Destination for converted value
3602 * Success: S_OK. pVarOut contains the absolute value of pVarIn.
3603 * Failure: An HRESULT error code indicating the error.
3606 * - This function does not process by-reference variants.
3607 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3608 * according to the following table:
3609 *| Input Type Output Type
3610 *| ---------- -----------
3613 *| (All others) Unchanged
3615 HRESULT WINAPI
VarAbs(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
3618 HRESULT hRet
= S_OK
;
3620 TRACE("(%p->(%s%s),%p)\n", pVarIn
, debugstr_VT(pVarIn
),
3621 debugstr_VF(pVarIn
), pVarOut
);
3623 if (V_ISARRAY(pVarIn
) || V_VT(pVarIn
) == VT_UNKNOWN
||
3624 V_VT(pVarIn
) == VT_DISPATCH
|| V_VT(pVarIn
) == VT_RECORD
||
3625 V_VT(pVarIn
) == VT_ERROR
)
3626 return DISP_E_TYPEMISMATCH
;
3628 *pVarOut
= *pVarIn
; /* Shallow copy the value, and invert it if needed */
3630 #define ABS_CASE(typ,min) \
3631 case VT_##typ: if (V_##typ(pVarIn) == min) hRet = DISP_E_OVERFLOW; \
3632 else if (V_##typ(pVarIn) < 0) V_##typ(pVarOut) = -V_##typ(pVarIn); \
3635 switch (V_VT(pVarIn
))
3637 ABS_CASE(I1
,I1_MIN
);
3639 V_VT(pVarOut
) = VT_I2
;
3640 /* BOOL->I2, Fall through ... */
3641 ABS_CASE(I2
,I2_MIN
);
3643 ABS_CASE(I4
,I4_MIN
);
3644 ABS_CASE(I8
,I8_MIN
);
3645 ABS_CASE(R4
,R4_MIN
);
3647 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(&varIn
));
3650 V_VT(pVarOut
) = VT_R8
;
3652 /* Fall through ... */
3654 ABS_CASE(R8
,R8_MIN
);
3656 hRet
= VarCyAbs(V_CY(pVarIn
), & V_CY(pVarOut
));
3659 DEC_SIGN(&V_DECIMAL(pVarOut
)) &= ~DECIMAL_NEG
;
3669 V_VT(pVarOut
) = VT_I2
;
3674 hRet
= DISP_E_BADVARTYPE
;
3680 /**********************************************************************
3681 * VarFix [OLEAUT32.169]
3683 * Truncate a variants value to a whole number.
3686 * pVarIn [I] Source variant
3687 * pVarOut [O] Destination for converted value
3690 * Success: S_OK. pVarOut contains the converted value.
3691 * Failure: An HRESULT error code indicating the error.
3694 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3695 * according to the following table:
3696 *| Input Type Output Type
3697 *| ---------- -----------
3701 *| All Others Unchanged
3702 * - The difference between this function and VarInt() is that VarInt() rounds
3703 * negative numbers away from 0, while this function rounds them towards zero.
3705 HRESULT WINAPI
VarFix(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
3707 HRESULT hRet
= S_OK
;
3709 TRACE("(%p->(%s%s),%p)\n", pVarIn
, debugstr_VT(pVarIn
),
3710 debugstr_VF(pVarIn
), pVarOut
);
3712 V_VT(pVarOut
) = V_VT(pVarIn
);
3714 switch (V_VT(pVarIn
))
3717 V_UI1(pVarOut
) = V_UI1(pVarIn
);
3720 V_VT(pVarOut
) = VT_I2
;
3723 V_I2(pVarOut
) = V_I2(pVarIn
);
3726 V_I4(pVarOut
) = V_I4(pVarIn
);
3729 V_I8(pVarOut
) = V_I8(pVarIn
);
3732 if (V_R4(pVarIn
) < 0.0f
)
3733 V_R4(pVarOut
) = (float)ceil(V_R4(pVarIn
));
3735 V_R4(pVarOut
) = (float)floor(V_R4(pVarIn
));
3738 V_VT(pVarOut
) = VT_R8
;
3739 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(pVarOut
));
3744 if (V_R8(pVarIn
) < 0.0)
3745 V_R8(pVarOut
) = ceil(V_R8(pVarIn
));
3747 V_R8(pVarOut
) = floor(V_R8(pVarIn
));
3750 hRet
= VarCyFix(V_CY(pVarIn
), &V_CY(pVarOut
));
3753 hRet
= VarDecFix(&V_DECIMAL(pVarIn
), &V_DECIMAL(pVarOut
));
3756 V_VT(pVarOut
) = VT_I2
;
3763 if (V_TYPE(pVarIn
) == VT_CLSID
|| /* VT_CLSID is a special case */
3764 FAILED(VARIANT_ValidateType(V_VT(pVarIn
))))
3765 hRet
= DISP_E_BADVARTYPE
;
3767 hRet
= DISP_E_TYPEMISMATCH
;
3770 V_VT(pVarOut
) = VT_EMPTY
;
3775 /**********************************************************************
3776 * VarInt [OLEAUT32.172]
3778 * Truncate a variants value to a whole number.
3781 * pVarIn [I] Source variant
3782 * pVarOut [O] Destination for converted value
3785 * Success: S_OK. pVarOut contains the converted value.
3786 * Failure: An HRESULT error code indicating the error.
3789 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3790 * according to the following table:
3791 *| Input Type Output Type
3792 *| ---------- -----------
3796 *| All Others Unchanged
3797 * - The difference between this function and VarFix() is that VarFix() rounds
3798 * negative numbers towards 0, while this function rounds them away from zero.
3800 HRESULT WINAPI
VarInt(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
3802 HRESULT hRet
= S_OK
;
3804 TRACE("(%p->(%s%s),%p)\n", pVarIn
, debugstr_VT(pVarIn
),
3805 debugstr_VF(pVarIn
), pVarOut
);
3807 V_VT(pVarOut
) = V_VT(pVarIn
);
3809 switch (V_VT(pVarIn
))
3812 V_R4(pVarOut
) = (float)floor(V_R4(pVarIn
));
3815 V_VT(pVarOut
) = VT_R8
;
3816 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(pVarOut
));
3821 V_R8(pVarOut
) = floor(V_R8(pVarIn
));
3824 hRet
= VarCyInt(V_CY(pVarIn
), &V_CY(pVarOut
));
3827 hRet
= VarDecInt(&V_DECIMAL(pVarIn
), &V_DECIMAL(pVarOut
));
3830 return VarFix(pVarIn
, pVarOut
);
3836 /**********************************************************************
3837 * VarXor [OLEAUT32.167]
3839 * Perform a logical exclusive-or (XOR) operation on two variants.
3842 * pVarLeft [I] First variant
3843 * pVarRight [I] Variant to XOR with pVarLeft
3844 * pVarOut [O] Destination for XOR result
3847 * Success: S_OK. pVarOut contains the result of the operation with its type
3848 * taken from the table below).
3849 * Failure: An HRESULT error code indicating the error.
3852 * - Neither pVarLeft or pVarRight are modified by this function.
3853 * - This function does not process by-reference variants.
3854 * - Input types of VT_BSTR may be numeric strings or boolean text.
3855 * - The type of result stored in pVarOut depends on the types of pVarLeft
3856 * and pVarRight, and will be one of VT_UI1, VT_I2, VT_I4, VT_I8, VT_BOOL,
3857 * or VT_NULL if the function succeeds.
3858 * - Type promotion is inconsistent and as a result certain combinations of
3859 * values will return DISP_E_OVERFLOW even when they could be represented.
3860 * This matches the behaviour of native oleaut32.
3862 HRESULT WINAPI
VarXor(LPVARIANT pVarLeft
, LPVARIANT pVarRight
, LPVARIANT pVarOut
)
3865 VARIANT varLeft
, varRight
;
3869 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft
, debugstr_VT(pVarLeft
),
3870 debugstr_VF(pVarLeft
), pVarRight
, debugstr_VT(pVarRight
),
3871 debugstr_VF(pVarRight
), pVarOut
);
3873 if (V_EXTRA_TYPE(pVarLeft
) || V_EXTRA_TYPE(pVarRight
) ||
3874 V_VT(pVarLeft
) > VT_UINT
|| V_VT(pVarRight
) > VT_UINT
||
3875 V_VT(pVarLeft
) == VT_VARIANT
|| V_VT(pVarRight
) == VT_VARIANT
||
3876 V_VT(pVarLeft
) == VT_UNKNOWN
|| V_VT(pVarRight
) == VT_UNKNOWN
||
3877 V_VT(pVarLeft
) == (VARTYPE
)15 || V_VT(pVarRight
) == (VARTYPE
)15 ||
3878 V_VT(pVarLeft
) == VT_ERROR
|| V_VT(pVarRight
) == VT_ERROR
)
3879 return DISP_E_BADVARTYPE
;
3881 if (V_VT(pVarLeft
) == VT_NULL
|| V_VT(pVarRight
) == VT_NULL
)
3883 /* NULL XOR anything valid is NULL */
3884 V_VT(pVarOut
) = VT_NULL
;
3888 /* Copy our inputs so we don't disturb anything */
3889 V_VT(&varLeft
) = V_VT(&varRight
) = VT_EMPTY
;
3891 hRet
= VariantCopy(&varLeft
, pVarLeft
);
3895 hRet
= VariantCopy(&varRight
, pVarRight
);
3899 /* Try any strings first as numbers, then as VT_BOOL */
3900 if (V_VT(&varLeft
) == VT_BSTR
)
3902 hRet
= VarR8FromStr(V_BSTR(&varLeft
), LOCALE_USER_DEFAULT
, 0, &d
);
3903 hRet
= VariantChangeType(&varLeft
, &varLeft
, VARIANT_LOCALBOOL
,
3904 FAILED(hRet
) ? VT_BOOL
: VT_I4
);
3909 if (V_VT(&varRight
) == VT_BSTR
)
3911 hRet
= VarR8FromStr(V_BSTR(&varRight
), LOCALE_USER_DEFAULT
, 0, &d
);
3912 hRet
= VariantChangeType(&varRight
, &varRight
, VARIANT_LOCALBOOL
,
3913 FAILED(hRet
) ? VT_BOOL
: VT_I4
);
3918 /* Determine the result type */
3919 if (V_VT(&varLeft
) == VT_I8
|| V_VT(&varRight
) == VT_I8
)
3921 if (V_VT(pVarLeft
) == VT_INT
|| V_VT(pVarRight
) == VT_INT
)
3922 return DISP_E_TYPEMISMATCH
;
3927 switch ((V_VT(&varLeft
) << 16) | V_VT(&varRight
))
3929 case (VT_BOOL
<< 16) | VT_BOOL
:
3932 case (VT_UI1
<< 16) | VT_UI1
:
3935 case (VT_EMPTY
<< 16) | VT_EMPTY
:
3936 case (VT_EMPTY
<< 16) | VT_UI1
:
3937 case (VT_EMPTY
<< 16) | VT_I2
:
3938 case (VT_EMPTY
<< 16) | VT_BOOL
:
3939 case (VT_UI1
<< 16) | VT_EMPTY
:
3940 case (VT_UI1
<< 16) | VT_I2
:
3941 case (VT_UI1
<< 16) | VT_BOOL
:
3942 case (VT_I2
<< 16) | VT_EMPTY
:
3943 case (VT_I2
<< 16) | VT_UI1
:
3944 case (VT_I2
<< 16) | VT_I2
:
3945 case (VT_I2
<< 16) | VT_BOOL
:
3946 case (VT_BOOL
<< 16) | VT_EMPTY
:
3947 case (VT_BOOL
<< 16) | VT_UI1
:
3948 case (VT_BOOL
<< 16) | VT_I2
:
3957 /* VT_UI4 does not overflow */
3960 if (V_VT(&varLeft
) == VT_UI4
)
3961 V_VT(&varLeft
) = VT_I4
;
3962 if (V_VT(&varRight
) == VT_UI4
)
3963 V_VT(&varRight
) = VT_I4
;
3966 /* Convert our input copies to the result type */
3967 if (V_VT(&varLeft
) != vt
)
3968 hRet
= VariantChangeType(&varLeft
, &varLeft
, 0, vt
);
3972 if (V_VT(&varRight
) != vt
)
3973 hRet
= VariantChangeType(&varRight
, &varRight
, 0, vt
);
3979 /* Calculate the result */
3983 V_I8(pVarOut
) = V_I8(&varLeft
) ^ V_I8(&varRight
);
3986 V_I4(pVarOut
) = V_I4(&varLeft
) ^ V_I4(&varRight
);
3990 V_I2(pVarOut
) = V_I2(&varLeft
) ^ V_I2(&varRight
);
3993 V_UI1(pVarOut
) = V_UI1(&varLeft
) ^ V_UI1(&varRight
);
3998 VariantClear(&varLeft
);
3999 VariantClear(&varRight
);
4003 /**********************************************************************
4004 * VarEqv [OLEAUT32.172]
4006 * Determine if two variants contain the same value.
4009 * pVarLeft [I] First variant to compare
4010 * pVarRight [I] Variant to compare to pVarLeft
4011 * pVarOut [O] Destination for comparison result
4014 * Success: S_OK. pVarOut contains the result of the comparison (VARIANT_TRUE
4015 * if equivalent or non-zero otherwise.
4016 * Failure: An HRESULT error code indicating the error.
4019 * - This function simply calls VarXor() on pVarLeft and pVarRight and inverts
4022 HRESULT WINAPI
VarEqv(LPVARIANT pVarLeft
, LPVARIANT pVarRight
, LPVARIANT pVarOut
)
4026 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft
, debugstr_VT(pVarLeft
),
4027 debugstr_VF(pVarLeft
), pVarRight
, debugstr_VT(pVarRight
),
4028 debugstr_VF(pVarRight
), pVarOut
);
4030 hRet
= VarXor(pVarLeft
, pVarRight
, pVarOut
);
4031 if (SUCCEEDED(hRet
))
4033 if (V_VT(pVarOut
) == VT_I8
)
4034 V_I8(pVarOut
) = ~V_I8(pVarOut
);
4036 V_UI4(pVarOut
) = ~V_UI4(pVarOut
);
4041 /**********************************************************************
4042 * VarNeg [OLEAUT32.173]
4044 * Negate the value of a variant.
4047 * pVarIn [I] Source variant
4048 * pVarOut [O] Destination for converted value
4051 * Success: S_OK. pVarOut contains the converted value.
4052 * Failure: An HRESULT error code indicating the error.
4055 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4056 * according to the following table:
4057 *| Input Type Output Type
4058 *| ---------- -----------
4063 *| All Others Unchanged (unless promoted)
4064 * - Where the negated value of a variant does not fit in its base type, the type
4065 * is promoted according to the following table:
4066 *| Input Type Promoted To
4067 *| ---------- -----------
4071 * - The native version of this function returns DISP_E_BADVARTYPE for valid
4072 * variant types that cannot be negated, and returns DISP_E_TYPEMISMATCH
4073 * for types which are not valid. Since this is in contravention of the
4074 * meaning of those error codes and unlikely to be relied on by applications,
4075 * this implementation returns errors consistent with the other high level
4076 * variant math functions.
4078 HRESULT WINAPI
VarNeg(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
4080 HRESULT hRet
= S_OK
;
4082 TRACE("(%p->(%s%s),%p)\n", pVarIn
, debugstr_VT(pVarIn
),
4083 debugstr_VF(pVarIn
), pVarOut
);
4085 V_VT(pVarOut
) = V_VT(pVarIn
);
4087 switch (V_VT(pVarIn
))
4090 V_VT(pVarOut
) = VT_I2
;
4091 V_I2(pVarOut
) = -V_UI1(pVarIn
);
4094 V_VT(pVarOut
) = VT_I2
;
4097 if (V_I2(pVarIn
) == I2_MIN
)
4099 V_VT(pVarOut
) = VT_I4
;
4100 V_I4(pVarOut
) = -(int)V_I2(pVarIn
);
4103 V_I2(pVarOut
) = -V_I2(pVarIn
);
4106 if (V_I4(pVarIn
) == I4_MIN
)
4108 V_VT(pVarOut
) = VT_R8
;
4109 V_R8(pVarOut
) = -(double)V_I4(pVarIn
);
4112 V_I4(pVarOut
) = -V_I4(pVarIn
);
4115 if (V_I8(pVarIn
) == I8_MIN
)
4117 V_VT(pVarOut
) = VT_R8
;
4118 hRet
= VarR8FromI8(V_I8(pVarIn
), &V_R8(pVarOut
));
4119 V_R8(pVarOut
) *= -1.0;
4122 V_I8(pVarOut
) = -V_I8(pVarIn
);
4125 V_R4(pVarOut
) = -V_R4(pVarIn
);
4129 V_R8(pVarOut
) = -V_R8(pVarIn
);
4132 hRet
= VarCyNeg(V_CY(pVarIn
), &V_CY(pVarOut
));
4135 hRet
= VarDecNeg(&V_DECIMAL(pVarIn
), &V_DECIMAL(pVarOut
));
4138 V_VT(pVarOut
) = VT_R8
;
4139 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(pVarOut
));
4140 V_R8(pVarOut
) = -V_R8(pVarOut
);
4143 V_VT(pVarOut
) = VT_I2
;
4150 if (V_TYPE(pVarIn
) == VT_CLSID
|| /* VT_CLSID is a special case */
4151 FAILED(VARIANT_ValidateType(V_VT(pVarIn
))))
4152 hRet
= DISP_E_BADVARTYPE
;
4154 hRet
= DISP_E_TYPEMISMATCH
;
4157 V_VT(pVarOut
) = VT_EMPTY
;
4162 /**********************************************************************
4163 * VarNot [OLEAUT32.174]
4165 * Perform a not operation on a variant.
4168 * pVarIn [I] Source variant
4169 * pVarOut [O] Destination for converted value
4172 * Success: S_OK. pVarOut contains the converted value.
4173 * Failure: An HRESULT error code indicating the error.
4176 * - Strictly speaking, this function performs a bitwise ones complement
4177 * on the variants value (after possibly converting to VT_I4, see below).
4178 * This only behaves like a boolean not operation if the value in
4179 * pVarIn is either VARIANT_TRUE or VARIANT_FALSE and the type is signed.
4180 * - To perform a genuine not operation, convert the variant to a VT_BOOL
4181 * before calling this function.
4182 * - This function does not process by-reference variants.
4183 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4184 * according to the following table:
4185 *| Input Type Output Type
4186 *| ---------- -----------
4193 *| (All others) Unchanged
4195 HRESULT WINAPI
VarNot(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
4198 HRESULT hRet
= S_OK
;
4200 TRACE("(%p->(%s%s),%p)\n", pVarIn
, debugstr_VT(pVarIn
),
4201 debugstr_VF(pVarIn
), pVarOut
);
4203 V_VT(pVarOut
) = V_VT(pVarIn
);
4205 switch (V_VT(pVarIn
))
4208 V_I4(pVarOut
) = ~V_I1(pVarIn
);
4209 V_VT(pVarOut
) = VT_I4
;
4211 case VT_UI1
: V_UI1(pVarOut
) = ~V_UI1(pVarIn
); break;
4213 case VT_I2
: V_I2(pVarOut
) = ~V_I2(pVarIn
); break;
4215 V_I4(pVarOut
) = ~V_UI2(pVarIn
);
4216 V_VT(pVarOut
) = VT_I4
;
4219 hRet
= VarI4FromDec(&V_DECIMAL(pVarIn
), &V_I4(&varIn
));
4223 /* Fall through ... */
4225 V_VT(pVarOut
) = VT_I4
;
4226 /* Fall through ... */
4227 case VT_I4
: V_I4(pVarOut
) = ~V_I4(pVarIn
); break;
4230 V_I4(pVarOut
) = ~V_UI4(pVarIn
);
4231 V_VT(pVarOut
) = VT_I4
;
4233 case VT_I8
: V_I8(pVarOut
) = ~V_I8(pVarIn
); break;
4235 V_I4(pVarOut
) = ~V_UI8(pVarIn
);
4236 V_VT(pVarOut
) = VT_I4
;
4239 hRet
= VarI4FromR4(V_R4(pVarIn
), &V_I4(pVarOut
));
4240 V_I4(pVarOut
) = ~V_I4(pVarOut
);
4241 V_VT(pVarOut
) = VT_I4
;
4244 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(&varIn
));
4248 /* Fall through ... */
4251 hRet
= VarI4FromR8(V_R8(pVarIn
), &V_I4(pVarOut
));
4252 V_I4(pVarOut
) = ~V_I4(pVarOut
);
4253 V_VT(pVarOut
) = VT_I4
;
4256 hRet
= VarI4FromCy(V_CY(pVarIn
), &V_I4(pVarOut
));
4257 V_I4(pVarOut
) = ~V_I4(pVarOut
);
4258 V_VT(pVarOut
) = VT_I4
;
4262 V_VT(pVarOut
) = VT_I2
;
4268 if (V_TYPE(pVarIn
) == VT_CLSID
|| /* VT_CLSID is a special case */
4269 FAILED(VARIANT_ValidateType(V_VT(pVarIn
))))
4270 hRet
= DISP_E_BADVARTYPE
;
4272 hRet
= DISP_E_TYPEMISMATCH
;
4275 V_VT(pVarOut
) = VT_EMPTY
;
4280 /**********************************************************************
4281 * VarRound [OLEAUT32.175]
4283 * Perform a round operation on a variant.
4286 * pVarIn [I] Source variant
4287 * deci [I] Number of decimals to round to
4288 * pVarOut [O] Destination for converted value
4291 * Success: S_OK. pVarOut contains the converted value.
4292 * Failure: An HRESULT error code indicating the error.
4295 * - Floating point values are rounded to the desired number of decimals.
4296 * - Some integer types are just copied to the return variable.
4297 * - Some other integer types are not handled and fail.
4299 HRESULT WINAPI
VarRound(LPVARIANT pVarIn
, int deci
, LPVARIANT pVarOut
)
4302 HRESULT hRet
= S_OK
;
4305 TRACE("(%p->(%s%s),%d)\n", pVarIn
, debugstr_VT(pVarIn
), debugstr_VF(pVarIn
), deci
);
4307 switch (V_VT(pVarIn
))
4309 /* cases that fail on windows */
4314 hRet
= DISP_E_BADVARTYPE
;
4317 /* cases just copying in to out */
4319 V_VT(pVarOut
) = V_VT(pVarIn
);
4320 V_UI1(pVarOut
) = V_UI1(pVarIn
);
4323 V_VT(pVarOut
) = V_VT(pVarIn
);
4324 V_I2(pVarOut
) = V_I2(pVarIn
);
4327 V_VT(pVarOut
) = V_VT(pVarIn
);
4328 V_I4(pVarOut
) = V_I4(pVarIn
);
4331 V_VT(pVarOut
) = V_VT(pVarIn
);
4332 /* value unchanged */
4335 /* cases that change type */
4337 V_VT(pVarOut
) = VT_I2
;
4341 V_VT(pVarOut
) = VT_I2
;
4342 V_I2(pVarOut
) = V_BOOL(pVarIn
);
4345 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(&varIn
));
4350 /* Fall through ... */
4352 /* cases we need to do math */
4354 if (V_R8(pVarIn
)>0) {
4355 V_R8(pVarOut
)=floor(V_R8(pVarIn
)*pow(10, deci
)+0.5)/pow(10, deci
);
4357 V_R8(pVarOut
)=ceil(V_R8(pVarIn
)*pow(10, deci
)-0.5)/pow(10, deci
);
4359 V_VT(pVarOut
) = V_VT(pVarIn
);
4362 if (V_R4(pVarIn
)>0) {
4363 V_R4(pVarOut
)=floor(V_R4(pVarIn
)*pow(10, deci
)+0.5)/pow(10, deci
);
4365 V_R4(pVarOut
)=ceil(V_R4(pVarIn
)*pow(10, deci
)-0.5)/pow(10, deci
);
4367 V_VT(pVarOut
) = V_VT(pVarIn
);
4370 if (V_DATE(pVarIn
)>0) {
4371 V_DATE(pVarOut
)=floor(V_DATE(pVarIn
)*pow(10, deci
)+0.5)/pow(10, deci
);
4373 V_DATE(pVarOut
)=ceil(V_DATE(pVarIn
)*pow(10, deci
)-0.5)/pow(10, deci
);
4375 V_VT(pVarOut
) = V_VT(pVarIn
);
4381 factor
=pow(10, 4-deci
);
4383 if (V_CY(pVarIn
).int64
>0) {
4384 V_CY(pVarOut
).int64
=floor(V_CY(pVarIn
).int64
/factor
)*factor
;
4386 V_CY(pVarOut
).int64
=ceil(V_CY(pVarIn
).int64
/factor
)*factor
;
4388 V_VT(pVarOut
) = V_VT(pVarIn
);
4391 /* cases we don't know yet */
4393 FIXME("unimplemented part, V_VT(pVarIn) == 0x%X, deci == %d\n",
4394 V_VT(pVarIn
) & VT_TYPEMASK
, deci
);
4395 hRet
= DISP_E_BADVARTYPE
;
4399 V_VT(pVarOut
) = VT_EMPTY
;
4401 TRACE("returning 0x%08lx (%s%s),%f\n", hRet
, debugstr_VT(pVarOut
),
4402 debugstr_VF(pVarOut
), (V_VT(pVarOut
) == VT_R4
) ? V_R4(pVarOut
) :
4403 (V_VT(pVarOut
) == VT_R8
) ? V_R8(pVarOut
) : 0);
4408 /**********************************************************************
4409 * VarIdiv [OLEAUT32.153]
4411 * Converts input variants to integers and divides them.
4414 * left [I] Left hand variant
4415 * right [I] Right hand variant
4416 * result [O] Destination for quotient
4419 * Success: S_OK. result contains the quotient.
4420 * Failure: An HRESULT error code indicating the error.
4423 * If either expression is null, null is returned, as per MSDN
4425 HRESULT WINAPI
VarIdiv(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
4433 if ((V_VT(left
) == VT_NULL
) || (V_VT(right
) == VT_NULL
)) {
4434 hr
= VariantChangeType(result
, result
, 0, VT_NULL
);
4436 /* This should never happen */
4437 FIXME("Failed to convert return value to VT_NULL.\n");
4443 hr
= VariantChangeType(&lv
, left
, 0, VT_I4
);
4447 hr
= VariantChangeType(&rv
, right
, 0, VT_I4
);
4452 hr
= VarDiv(&lv
, &rv
, result
);
4457 /**********************************************************************
4458 * VarMod [OLEAUT32.155]
4460 * Perform the modulus operation of the right hand variant on the left
4463 * left [I] Left hand variant
4464 * right [I] Right hand variant
4465 * result [O] Destination for converted value
4468 * Success: S_OK. result contains the remainder.
4469 * Failure: An HRESULT error code indicating the error.
4472 * If an error occurs the type of result will be modified but the value will not be.
4473 * Doesn't support arrays or any special flags yet.
4475 HRESULT WINAPI
VarMod(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
4479 HRESULT rc
= E_FAIL
;
4486 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
),
4487 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
), result
);
4489 /* check for invalid inputs */
4491 switch (V_VT(left
) & VT_TYPEMASK
) {
4512 V_VT(result
) = VT_EMPTY
;
4513 return DISP_E_TYPEMISMATCH
;
4515 V_VT(result
) = VT_EMPTY
;
4516 return DISP_E_OVERFLOW
;
4518 return DISP_E_TYPEMISMATCH
;
4520 V_VT(result
) = VT_EMPTY
;
4521 return DISP_E_TYPEMISMATCH
;
4525 V_VT(result
) = VT_EMPTY
;
4526 return DISP_E_BADVARTYPE
;
4531 switch (V_VT(right
) & VT_TYPEMASK
) {
4537 if((V_VT(left
) == VT_INT
) && (V_VT(right
) == VT_I8
))
4539 V_VT(result
) = VT_EMPTY
;
4540 return DISP_E_TYPEMISMATCH
;
4543 if((V_VT(right
) == VT_INT
) && (V_VT(left
) == VT_I8
))
4545 V_VT(result
) = VT_EMPTY
;
4546 return DISP_E_TYPEMISMATCH
;
4556 if(V_VT(left
) == VT_EMPTY
)
4558 V_VT(result
) = VT_I4
;
4564 if(V_VT(left
) == VT_NULL
)
4566 V_VT(result
) = VT_NULL
;
4572 V_VT(result
) = VT_EMPTY
;
4573 return DISP_E_BADVARTYPE
;
4575 if(V_VT(left
) == VT_VOID
)
4577 V_VT(result
) = VT_EMPTY
;
4578 return DISP_E_BADVARTYPE
;
4579 } else if((V_VT(left
) == VT_NULL
) || (V_VT(left
) == VT_EMPTY
) || (V_VT(left
) == VT_ERROR
) ||
4582 V_VT(result
) = VT_NULL
;
4586 V_VT(result
) = VT_NULL
;
4587 return DISP_E_BADVARTYPE
;
4591 V_VT(result
) = VT_EMPTY
;
4592 return DISP_E_TYPEMISMATCH
;
4594 if(V_VT(left
) == VT_ERROR
)
4596 V_VT(result
) = VT_EMPTY
;
4597 return DISP_E_TYPEMISMATCH
;
4600 V_VT(result
) = VT_EMPTY
;
4601 return DISP_E_OVERFLOW
;
4604 return DISP_E_TYPEMISMATCH
;
4606 if((V_VT(left
) == 15) || ((V_VT(left
) >= 24) && (V_VT(left
) <= 35)) || !lOk
)
4608 V_VT(result
) = VT_EMPTY
;
4609 return DISP_E_BADVARTYPE
;
4612 V_VT(result
) = VT_EMPTY
;
4613 return DISP_E_TYPEMISMATCH
;
4616 V_VT(result
) = VT_EMPTY
;
4617 return DISP_E_BADVARTYPE
;
4620 /* determine the result type */
4621 if((V_VT(left
) == VT_I8
) || (V_VT(right
) == VT_I8
)) resT
= VT_I8
;
4622 else if((V_VT(left
) == VT_UI1
) && (V_VT(right
) == VT_BOOL
)) resT
= VT_I2
;
4623 else if((V_VT(left
) == VT_UI1
) && (V_VT(right
) == VT_UI1
)) resT
= VT_UI1
;
4624 else if((V_VT(left
) == VT_UI1
) && (V_VT(right
) == VT_I2
)) resT
= VT_I2
;
4625 else if((V_VT(left
) == VT_I2
) && (V_VT(right
) == VT_BOOL
)) resT
= VT_I2
;
4626 else if((V_VT(left
) == VT_I2
) && (V_VT(right
) == VT_UI1
)) resT
= VT_I2
;
4627 else if((V_VT(left
) == VT_I2
) && (V_VT(right
) == VT_I2
)) resT
= VT_I2
;
4628 else if((V_VT(left
) == VT_BOOL
) && (V_VT(right
) == VT_BOOL
)) resT
= VT_I2
;
4629 else if((V_VT(left
) == VT_BOOL
) && (V_VT(right
) == VT_UI1
)) resT
= VT_I2
;
4630 else if((V_VT(left
) == VT_BOOL
) && (V_VT(right
) == VT_I2
)) resT
= VT_I2
;
4631 else resT
= VT_I4
; /* most outputs are I4 */
4633 /* convert to I8 for the modulo */
4634 rc
= VariantChangeType(&lv
, left
, 0, VT_I8
);
4637 FIXME("Could not convert left type %d to %d? rc == 0x%lX\n", V_VT(left
), VT_I8
, rc
);
4641 rc
= VariantChangeType(&rv
, right
, 0, VT_I8
);
4644 FIXME("Could not convert right type %d to %d? rc == 0x%lX\n", V_VT(right
), VT_I8
, rc
);
4648 /* if right is zero set VT_EMPTY and return divide by zero */
4651 V_VT(result
) = VT_EMPTY
;
4652 return DISP_E_DIVBYZERO
;
4655 /* perform the modulo operation */
4656 V_VT(result
) = VT_I8
;
4657 V_I8(result
) = V_I8(&lv
) % V_I8(&rv
);
4659 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
));
4661 /* convert left and right to the destination type */
4662 rc
= VariantChangeType(result
, result
, 0, resT
);
4665 FIXME("Could not convert 0x%x to %d?\n", V_VT(result
), resT
);
4672 /**********************************************************************
4673 * VarPow [OLEAUT32.158]
4675 * Computes the power of one variant to another variant.
4678 * left [I] First variant
4679 * right [I] Second variant
4680 * result [O] Result variant
4684 * Failure: An HRESULT error code indicating the error.
4686 HRESULT WINAPI
VarPow(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
4691 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
), debugstr_VF(left
),
4692 right
, debugstr_VT(right
), debugstr_VF(right
), result
);
4694 hr
= VariantChangeType(&dl
,left
,0,VT_R8
);
4695 if (!SUCCEEDED(hr
)) {
4696 ERR("Could not change passed left argument to VT_R8, handle it differently.\n");
4699 hr
= VariantChangeType(&dr
,right
,0,VT_R8
);
4700 if (!SUCCEEDED(hr
)) {
4701 ERR("Could not change passed right argument to VT_R8, handle it differently.\n");
4704 V_VT(result
) = VT_R8
;
4705 V_R8(result
) = pow(V_R8(&dl
),V_R8(&dr
));