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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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
] =
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
));
628 else if (V_VT(pVarg
) == VT_VARIANT
)
630 if (V_VARIANTREF(pVarg
))
631 VariantClear(V_VARIANTREF(pVarg
));
634 V_VT(pVarg
) = VT_EMPTY
;
639 /******************************************************************************
640 * Copy an IRecordInfo object contained in a variant.
642 static HRESULT
VARIANT_CopyIRecordInfo(struct __tagBRECORD
* pBr
)
650 hres
= IRecordInfo_GetSize(pBr
->pRecInfo
, &ulSize
);
653 PVOID pvRecord
= HeapAlloc(GetProcessHeap(), 0, ulSize
);
655 hres
= E_OUTOFMEMORY
;
658 memcpy(pvRecord
, pBr
->pvRecord
, ulSize
);
659 pBr
->pvRecord
= pvRecord
;
661 hres
= IRecordInfo_RecordCopy(pBr
->pRecInfo
, pvRecord
, pvRecord
);
663 IRecordInfo_AddRef(pBr
->pRecInfo
);
667 else if (pBr
->pvRecord
)
672 /******************************************************************************
673 * VariantCopy [OLEAUT32.10]
678 * pvargDest [O] Destination for copy
679 * pvargSrc [I] Source variant to copy
682 * Success: S_OK. pvargDest contains a copy of pvargSrc.
683 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid type.
684 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
685 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
686 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
689 * - If pvargSrc == pvargDest, this function does nothing, and succeeds if
690 * pvargSrc is valid. Otherwise, pvargDest is always cleared using
691 * VariantClear() before pvargSrc is copied to it. If clearing pvargDest
692 * fails, so does this function.
693 * - VT_CLSID is a valid type type for pvargSrc, but not for pvargDest.
694 * - For by-value non-intrinsic types, a deep copy is made, i.e. The whole value
695 * is copied rather than just any pointers to it.
696 * - For by-value object types the object pointer is copied and the objects
697 * reference count increased using IUnknown_AddRef().
698 * - For all by-reference types, only the referencing pointer is copied.
700 HRESULT WINAPI
VariantCopy(VARIANTARG
* pvargDest
, VARIANTARG
* pvargSrc
)
704 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest
, debugstr_VT(pvargDest
),
705 debugstr_VF(pvargDest
), pvargSrc
, debugstr_VT(pvargSrc
),
706 debugstr_VF(pvargSrc
));
708 if (V_TYPE(pvargSrc
) == VT_CLSID
|| /* VT_CLSID is a special case */
709 FAILED(VARIANT_ValidateType(V_VT(pvargSrc
))))
710 return DISP_E_BADVARTYPE
;
712 if (pvargSrc
!= pvargDest
&&
713 SUCCEEDED(hres
= VariantClear(pvargDest
)))
715 *pvargDest
= *pvargSrc
; /* Shallow copy the value */
717 if (!V_ISBYREF(pvargSrc
))
719 if (V_ISARRAY(pvargSrc
))
721 if (V_ARRAY(pvargSrc
))
722 hres
= SafeArrayCopy(V_ARRAY(pvargSrc
), &V_ARRAY(pvargDest
));
724 else if (V_VT(pvargSrc
) == VT_BSTR
)
726 if (V_BSTR(pvargSrc
))
728 V_BSTR(pvargDest
) = SysAllocStringByteLen((char*)V_BSTR(pvargSrc
), SysStringByteLen(V_BSTR(pvargSrc
)));
729 if (!V_BSTR(pvargDest
))
731 TRACE("!V_BSTR(pvargDest), SysAllocStringByteLen() failed to allocate %d bytes\n", SysStringByteLen(V_BSTR(pvargSrc
)));
732 hres
= E_OUTOFMEMORY
;
736 else if (V_VT(pvargSrc
) == VT_RECORD
)
738 hres
= VARIANT_CopyIRecordInfo(&V_UNION(pvargDest
,brecVal
));
740 else if (V_VT(pvargSrc
) == VT_DISPATCH
||
741 V_VT(pvargSrc
) == VT_UNKNOWN
)
743 if (V_UNKNOWN(pvargSrc
))
744 IUnknown_AddRef(V_UNKNOWN(pvargSrc
));
751 /* Return the byte size of a variants data */
752 static inline size_t VARIANT_DataSize(const VARIANT
* pv
)
757 case VT_UI1
: return sizeof(BYTE
);
759 case VT_UI2
: return sizeof(SHORT
);
763 case VT_UI4
: return sizeof(LONG
);
765 case VT_UI8
: return sizeof(LONGLONG
);
766 case VT_R4
: return sizeof(float);
767 case VT_R8
: return sizeof(double);
768 case VT_DATE
: return sizeof(DATE
);
769 case VT_BOOL
: return sizeof(VARIANT_BOOL
);
772 case VT_BSTR
: return sizeof(void*);
773 case VT_CY
: return sizeof(CY
);
774 case VT_ERROR
: return sizeof(SCODE
);
776 TRACE("Shouldn't be called for vt %s%s!\n", debugstr_VT(pv
), debugstr_VF(pv
));
780 /******************************************************************************
781 * VariantCopyInd [OLEAUT32.11]
783 * Copy a variant, dereferencing it it is by-reference.
786 * pvargDest [O] Destination for copy
787 * pvargSrc [I] Source variant to copy
790 * Success: S_OK. pvargDest contains a copy of pvargSrc.
791 * Failure: An HRESULT error code indicating the error.
794 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid by-value type.
795 * E_INVALIDARG, if pvargSrc is an invalid by-reference type.
796 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
797 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
798 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
801 * - If pvargSrc is by-value, this function behaves exactly as VariantCopy().
802 * - If pvargSrc is by-reference, the value copied to pvargDest is the pointed-to
804 * - if pvargSrc == pvargDest, this function dereferences in place. Otherwise,
805 * pvargDest is always cleared using VariantClear() before pvargSrc is copied
806 * to it. If clearing pvargDest fails, so does this function.
808 HRESULT WINAPI
VariantCopyInd(VARIANT
* pvargDest
, VARIANTARG
* pvargSrc
)
810 VARIANTARG vTmp
, *pSrc
= pvargSrc
;
814 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest
, debugstr_VT(pvargDest
),
815 debugstr_VF(pvargDest
), pvargSrc
, debugstr_VT(pvargSrc
),
816 debugstr_VF(pvargSrc
));
818 if (!V_ISBYREF(pvargSrc
))
819 return VariantCopy(pvargDest
, pvargSrc
);
821 /* Argument checking is more lax than VariantCopy()... */
822 vt
= V_TYPE(pvargSrc
);
823 if (V_ISARRAY(pvargSrc
) ||
824 (vt
> VT_NULL
&& vt
!= (VARTYPE
)15 && vt
< VT_VOID
&&
825 !(V_VT(pvargSrc
) & (VT_VECTOR
|VT_RESERVED
))))
830 return E_INVALIDARG
; /* ...And the return value for invalid types differs too */
832 if (pvargSrc
== pvargDest
)
834 /* In place copy. Use a shallow copy of pvargSrc & init pvargDest.
835 * This avoids an expensive VariantCopy() call - e.g. SafeArrayCopy().
839 V_VT(pvargDest
) = VT_EMPTY
;
843 /* Copy into another variant. Free the variant in pvargDest */
844 if (FAILED(hres
= VariantClear(pvargDest
)))
846 TRACE("VariantClear() of destination failed\n");
853 /* Native doesn't check that *V_ARRAYREF(pSrc) is valid */
854 hres
= SafeArrayCopy(*V_ARRAYREF(pSrc
), &V_ARRAY(pvargDest
));
856 else if (V_VT(pSrc
) == (VT_BSTR
|VT_BYREF
))
858 /* Native doesn't check that *V_BSTRREF(pSrc) is valid */
859 V_BSTR(pvargDest
) = SysAllocStringByteLen((char*)*V_BSTRREF(pSrc
), SysStringByteLen(*V_BSTRREF(pSrc
)));
861 else if (V_VT(pSrc
) == (VT_RECORD
|VT_BYREF
))
863 V_UNION(pvargDest
,brecVal
) = V_UNION(pvargSrc
,brecVal
);
864 hres
= VARIANT_CopyIRecordInfo(&V_UNION(pvargDest
,brecVal
));
866 else if (V_VT(pSrc
) == (VT_DISPATCH
|VT_BYREF
) ||
867 V_VT(pSrc
) == (VT_UNKNOWN
|VT_BYREF
))
869 /* Native doesn't check that *V_UNKNOWNREF(pSrc) is valid */
870 V_UNKNOWN(pvargDest
) = *V_UNKNOWNREF(pSrc
);
871 if (*V_UNKNOWNREF(pSrc
))
872 IUnknown_AddRef(*V_UNKNOWNREF(pSrc
));
874 else if (V_VT(pSrc
) == (VT_VARIANT
|VT_BYREF
))
876 /* Native doesn't check that *V_VARIANTREF(pSrc) is valid */
877 if (V_VT(V_VARIANTREF(pSrc
)) == (VT_VARIANT
|VT_BYREF
))
878 hres
= E_INVALIDARG
; /* Don't dereference more than one level */
880 hres
= VariantCopyInd(pvargDest
, V_VARIANTREF(pSrc
));
882 /* Use the dereferenced variants type value, not VT_VARIANT */
883 goto VariantCopyInd_Return
;
885 else if (V_VT(pSrc
) == (VT_DECIMAL
|VT_BYREF
))
887 memcpy(&DEC_SCALE(&V_DECIMAL(pvargDest
)), &DEC_SCALE(V_DECIMALREF(pSrc
)),
888 sizeof(DECIMAL
) - sizeof(USHORT
));
892 /* Copy the pointed to data into this variant */
893 memcpy(&V_BYREF(pvargDest
), V_BYREF(pSrc
), VARIANT_DataSize(pSrc
));
896 V_VT(pvargDest
) = V_VT(pSrc
) & ~VT_BYREF
;
898 VariantCopyInd_Return
:
900 if (pSrc
!= pvargSrc
)
903 TRACE("returning 0x%08lx, %p->(%s%s)\n", hres
, pvargDest
,
904 debugstr_VT(pvargDest
), debugstr_VF(pvargDest
));
908 /******************************************************************************
909 * VariantChangeType [OLEAUT32.12]
911 * Change the type of a variant.
914 * pvargDest [O] Destination for the converted variant
915 * pvargSrc [O] Source variant to change the type of
916 * wFlags [I] VARIANT_ flags from "oleauto.h"
917 * vt [I] Variant type to change pvargSrc into
920 * Success: S_OK. pvargDest contains the converted value.
921 * Failure: An HRESULT error code describing the failure.
924 * The LCID used for the conversion is LOCALE_USER_DEFAULT.
925 * See VariantChangeTypeEx.
927 HRESULT WINAPI
VariantChangeType(VARIANTARG
* pvargDest
, VARIANTARG
* pvargSrc
,
928 USHORT wFlags
, VARTYPE vt
)
930 return VariantChangeTypeEx( pvargDest
, pvargSrc
, LOCALE_USER_DEFAULT
, wFlags
, vt
);
933 /******************************************************************************
934 * VariantChangeTypeEx [OLEAUT32.147]
936 * Change the type of a variant.
939 * pvargDest [O] Destination for the converted variant
940 * pvargSrc [O] Source variant to change the type of
941 * lcid [I] LCID for the conversion
942 * wFlags [I] VARIANT_ flags from "oleauto.h"
943 * vt [I] Variant type to change pvargSrc into
946 * Success: S_OK. pvargDest contains the converted value.
947 * Failure: An HRESULT error code describing the failure.
950 * pvargDest and pvargSrc can point to the same variant to perform an in-place
951 * conversion. If the conversion is successful, pvargSrc will be freed.
953 HRESULT WINAPI
VariantChangeTypeEx(VARIANTARG
* pvargDest
, VARIANTARG
* pvargSrc
,
954 LCID lcid
, USHORT wFlags
, VARTYPE vt
)
958 TRACE("(%p->(%s%s),%p->(%s%s),0x%08lx,0x%04x,%s%s)\n", pvargDest
,
959 debugstr_VT(pvargDest
), debugstr_VF(pvargDest
), pvargSrc
,
960 debugstr_VT(pvargSrc
), debugstr_VF(pvargSrc
), lcid
, wFlags
,
961 debugstr_vt(vt
), debugstr_vf(vt
));
964 res
= DISP_E_BADVARTYPE
;
967 res
= VARIANT_ValidateType(V_VT(pvargSrc
));
971 res
= VARIANT_ValidateType(vt
);
975 VARIANTARG vTmp
, vSrcDeref
;
977 if(V_ISBYREF(pvargSrc
) && !V_BYREF(pvargSrc
))
978 res
= DISP_E_TYPEMISMATCH
;
981 V_VT(&vTmp
) = VT_EMPTY
;
982 V_VT(&vSrcDeref
) = VT_EMPTY
;
984 VariantClear(&vSrcDeref
);
989 res
= VariantCopyInd(&vSrcDeref
, pvargSrc
);
992 if (V_ISARRAY(&vSrcDeref
) || (vt
& VT_ARRAY
))
993 res
= VARIANT_CoerceArray(&vTmp
, &vSrcDeref
, vt
);
995 res
= VARIANT_Coerce(&vTmp
, lcid
, wFlags
, &vSrcDeref
, vt
);
997 if (SUCCEEDED(res
)) {
999 VariantCopy(pvargDest
, &vTmp
);
1001 VariantClear(&vTmp
);
1002 VariantClear(&vSrcDeref
);
1009 TRACE("returning 0x%08lx, %p->(%s%s)\n", res
, pvargDest
,
1010 debugstr_VT(pvargDest
), debugstr_VF(pvargDest
));
1014 /* Date Conversions */
1016 #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
1018 /* Convert a VT_DATE value to a Julian Date */
1019 static inline int VARIANT_JulianFromDate(int dateIn
)
1021 int julianDays
= dateIn
;
1023 julianDays
-= DATE_MIN
; /* Convert to + days from 1 Jan 100 AD */
1024 julianDays
+= 1757585; /* Convert to + days from 23 Nov 4713 BC (Julian) */
1028 /* Convert a Julian Date to a VT_DATE value */
1029 static inline int VARIANT_DateFromJulian(int dateIn
)
1031 int julianDays
= dateIn
;
1033 julianDays
-= 1757585; /* Convert to + days from 1 Jan 100 AD */
1034 julianDays
+= DATE_MIN
; /* Convert to +/- days from 1 Jan 1899 AD */
1038 /* Convert a Julian date to Day/Month/Year - from PostgreSQL */
1039 static inline void VARIANT_DMYFromJulian(int jd
, USHORT
*year
, USHORT
*month
, USHORT
*day
)
1045 l
-= (n
* 146097 + 3) / 4;
1046 i
= (4000 * (l
+ 1)) / 1461001;
1047 l
+= 31 - (i
* 1461) / 4;
1048 j
= (l
* 80) / 2447;
1049 *day
= l
- (j
* 2447) / 80;
1051 *month
= (j
+ 2) - (12 * l
);
1052 *year
= 100 * (n
- 49) + i
+ l
;
1055 /* Convert Day/Month/Year to a Julian date - from PostgreSQL */
1056 static inline double VARIANT_JulianFromDMY(USHORT year
, USHORT month
, USHORT day
)
1058 int m12
= (month
- 14) / 12;
1060 return ((1461 * (year
+ 4800 + m12
)) / 4 + (367 * (month
- 2 - 12 * m12
)) / 12 -
1061 (3 * ((year
+ 4900 + m12
) / 100)) / 4 + day
- 32075);
1064 /* Macros for accessing DOS format date/time fields */
1065 #define DOS_YEAR(x) (1980 + (x >> 9))
1066 #define DOS_MONTH(x) ((x >> 5) & 0xf)
1067 #define DOS_DAY(x) (x & 0x1f)
1068 #define DOS_HOUR(x) (x >> 11)
1069 #define DOS_MINUTE(x) ((x >> 5) & 0x3f)
1070 #define DOS_SECOND(x) ((x & 0x1f) << 1)
1071 /* Create a DOS format date/time */
1072 #define DOS_DATE(d,m,y) (d | (m << 5) | ((y-1980) << 9))
1073 #define DOS_TIME(h,m,s) ((s >> 1) | (m << 5) | (h << 11))
1075 /* Roll a date forwards or backwards to correct it */
1076 static HRESULT
VARIANT_RollUdate(UDATE
*lpUd
)
1078 static const BYTE days
[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1080 TRACE("Raw date: %d/%d/%d %d:%d:%d\n", lpUd
->st
.wDay
, lpUd
->st
.wMonth
,
1081 lpUd
->st
.wYear
, lpUd
->st
.wHour
, lpUd
->st
.wMinute
, lpUd
->st
.wSecond
);
1083 /* Years < 100 are treated as 1900 + year */
1084 if (lpUd
->st
.wYear
< 100)
1085 lpUd
->st
.wYear
+= 1900;
1087 if (!lpUd
->st
.wMonth
)
1089 /* Roll back to December of the previous year */
1090 lpUd
->st
.wMonth
= 12;
1093 else while (lpUd
->st
.wMonth
> 12)
1095 /* Roll forward the correct number of months */
1097 lpUd
->st
.wMonth
-= 12;
1100 if (lpUd
->st
.wYear
> 9999 || lpUd
->st
.wHour
> 23 ||
1101 lpUd
->st
.wMinute
> 59 || lpUd
->st
.wSecond
> 59)
1102 return E_INVALIDARG
; /* Invalid values */
1106 /* Roll back the date one day */
1107 if (lpUd
->st
.wMonth
== 1)
1109 /* Roll back to December 31 of the previous year */
1111 lpUd
->st
.wMonth
= 12;
1116 lpUd
->st
.wMonth
--; /* Previous month */
1117 if (lpUd
->st
.wMonth
== 2 && IsLeapYear(lpUd
->st
.wYear
))
1118 lpUd
->st
.wDay
= 29; /* Februaury has 29 days on leap years */
1120 lpUd
->st
.wDay
= days
[lpUd
->st
.wMonth
]; /* Last day of the month */
1123 else if (lpUd
->st
.wDay
> 28)
1125 int rollForward
= 0;
1127 /* Possibly need to roll the date forward */
1128 if (lpUd
->st
.wMonth
== 2 && IsLeapYear(lpUd
->st
.wYear
))
1129 rollForward
= lpUd
->st
.wDay
- 29; /* Februaury has 29 days on leap years */
1131 rollForward
= lpUd
->st
.wDay
- days
[lpUd
->st
.wMonth
];
1133 if (rollForward
> 0)
1135 lpUd
->st
.wDay
= rollForward
;
1137 if (lpUd
->st
.wMonth
> 12)
1139 lpUd
->st
.wMonth
= 1; /* Roll forward into January of the next year */
1144 TRACE("Rolled date: %d/%d/%d %d:%d:%d\n", lpUd
->st
.wDay
, lpUd
->st
.wMonth
,
1145 lpUd
->st
.wYear
, lpUd
->st
.wHour
, lpUd
->st
.wMinute
, lpUd
->st
.wSecond
);
1149 /**********************************************************************
1150 * DosDateTimeToVariantTime [OLEAUT32.14]
1152 * Convert a Dos format date and time into variant VT_DATE format.
1155 * wDosDate [I] Dos format date
1156 * wDosTime [I] Dos format time
1157 * pDateOut [O] Destination for VT_DATE format
1160 * Success: TRUE. pDateOut contains the converted time.
1161 * Failure: FALSE, if wDosDate or wDosTime are invalid (see notes).
1164 * - Dos format dates can only hold dates from 1-Jan-1980 to 31-Dec-2099.
1165 * - Dos format times are accurate to only 2 second precision.
1166 * - The format of a Dos Date is:
1167 *| Bits Values Meaning
1168 *| ---- ------ -------
1169 *| 0-4 1-31 Day of the week. 0 rolls back one day. A value greater than
1170 *| the days in the month rolls forward the extra days.
1171 *| 5-8 1-12 Month of the year. 0 rolls back to December of the previous
1172 *| year. 13-15 are invalid.
1173 *| 9-15 0-119 Year based from 1980 (Max 2099). 120-127 are invalid.
1174 * - The format of a Dos Time is:
1175 *| Bits Values Meaning
1176 *| ---- ------ -------
1177 *| 0-4 0-29 Seconds/2. 30 and 31 are invalid.
1178 *| 5-10 0-59 Minutes. 60-63 are invalid.
1179 *| 11-15 0-23 Hours (24 hour clock). 24-32 are invalid.
1181 INT WINAPI
DosDateTimeToVariantTime(USHORT wDosDate
, USHORT wDosTime
,
1186 TRACE("(0x%x(%d/%d/%d),0x%x(%d:%d:%d),%p)\n",
1187 wDosDate
, DOS_YEAR(wDosDate
), DOS_MONTH(wDosDate
), DOS_DAY(wDosDate
),
1188 wDosTime
, DOS_HOUR(wDosTime
), DOS_MINUTE(wDosTime
), DOS_SECOND(wDosTime
),
1191 ud
.st
.wYear
= DOS_YEAR(wDosDate
);
1192 ud
.st
.wMonth
= DOS_MONTH(wDosDate
);
1193 if (ud
.st
.wYear
> 2099 || ud
.st
.wMonth
> 12)
1195 ud
.st
.wDay
= DOS_DAY(wDosDate
);
1196 ud
.st
.wHour
= DOS_HOUR(wDosTime
);
1197 ud
.st
.wMinute
= DOS_MINUTE(wDosTime
);
1198 ud
.st
.wSecond
= DOS_SECOND(wDosTime
);
1199 ud
.st
.wDayOfWeek
= ud
.st
.wMilliseconds
= 0;
1201 return !VarDateFromUdate(&ud
, 0, pDateOut
);
1204 /**********************************************************************
1205 * VariantTimeToDosDateTime [OLEAUT32.13]
1207 * Convert a variant format date into a Dos format date and time.
1209 * dateIn [I] VT_DATE time format
1210 * pwDosDate [O] Destination for Dos format date
1211 * pwDosTime [O] Destination for Dos format time
1214 * Success: TRUE. pwDosDate and pwDosTime contains the converted values.
1215 * Failure: FALSE, if dateIn cannot be represented in Dos format.
1218 * See DosDateTimeToVariantTime() for Dos format details and bugs.
1220 INT WINAPI
VariantTimeToDosDateTime(double dateIn
, USHORT
*pwDosDate
, USHORT
*pwDosTime
)
1224 TRACE("(%g,%p,%p)\n", dateIn
, pwDosDate
, pwDosTime
);
1226 if (FAILED(VarUdateFromDate(dateIn
, 0, &ud
)))
1229 if (ud
.st
.wYear
< 1980 || ud
.st
.wYear
> 2099)
1232 *pwDosDate
= DOS_DATE(ud
.st
.wDay
, ud
.st
.wMonth
, ud
.st
.wYear
);
1233 *pwDosTime
= DOS_TIME(ud
.st
.wHour
, ud
.st
.wMinute
, ud
.st
.wSecond
);
1235 TRACE("Returning 0x%x(%d/%d/%d), 0x%x(%d:%d:%d)\n",
1236 *pwDosDate
, DOS_YEAR(*pwDosDate
), DOS_MONTH(*pwDosDate
), DOS_DAY(*pwDosDate
),
1237 *pwDosTime
, DOS_HOUR(*pwDosTime
), DOS_MINUTE(*pwDosTime
), DOS_SECOND(*pwDosTime
));
1241 /***********************************************************************
1242 * SystemTimeToVariantTime [OLEAUT32.184]
1244 * Convert a System format date and time into variant VT_DATE format.
1247 * lpSt [I] System format date and time
1248 * pDateOut [O] Destination for VT_DATE format date
1251 * Success: TRUE. *pDateOut contains the converted value.
1252 * Failure: FALSE, if lpSt cannot be represented in VT_DATE format.
1254 INT WINAPI
SystemTimeToVariantTime(LPSYSTEMTIME lpSt
, double *pDateOut
)
1258 TRACE("(%p->%d/%d/%d %d:%d:%d,%p)\n", lpSt
, lpSt
->wDay
, lpSt
->wMonth
,
1259 lpSt
->wYear
, lpSt
->wHour
, lpSt
->wMinute
, lpSt
->wSecond
, pDateOut
);
1261 if (lpSt
->wMonth
> 12)
1264 memcpy(&ud
.st
, lpSt
, sizeof(ud
.st
));
1265 return !VarDateFromUdate(&ud
, 0, pDateOut
);
1268 /***********************************************************************
1269 * VariantTimeToSystemTime [OLEAUT32.185]
1271 * Convert a variant VT_DATE into a System format date and time.
1274 * datein [I] Variant VT_DATE format date
1275 * lpSt [O] Destination for System format date and time
1278 * Success: TRUE. *lpSt contains the converted value.
1279 * Failure: FALSE, if dateIn is too large or small.
1281 INT WINAPI
VariantTimeToSystemTime(double dateIn
, LPSYSTEMTIME lpSt
)
1285 TRACE("(%g,%p)\n", dateIn
, lpSt
);
1287 if (FAILED(VarUdateFromDate(dateIn
, 0, &ud
)))
1290 memcpy(lpSt
, &ud
.st
, sizeof(ud
.st
));
1294 /***********************************************************************
1295 * VarDateFromUdateEx [OLEAUT32.319]
1297 * Convert an unpacked format date and time to a variant VT_DATE.
1300 * pUdateIn [I] Unpacked format date and time to convert
1301 * lcid [I] Locale identifier for the conversion
1302 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1303 * pDateOut [O] Destination for variant VT_DATE.
1306 * Success: S_OK. *pDateOut contains the converted value.
1307 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1309 HRESULT WINAPI
VarDateFromUdateEx(UDATE
*pUdateIn
, LCID lcid
, ULONG dwFlags
, DATE
*pDateOut
)
1314 TRACE("(%p->%d/%d/%d %d:%d:%d:%d %d %d,0x%08lx,0x%08lx,%p)\n", pUdateIn
,
1315 pUdateIn
->st
.wMonth
, pUdateIn
->st
.wDay
, pUdateIn
->st
.wYear
,
1316 pUdateIn
->st
.wHour
, pUdateIn
->st
.wMinute
, pUdateIn
->st
.wSecond
,
1317 pUdateIn
->st
.wMilliseconds
, pUdateIn
->st
.wDayOfWeek
,
1318 pUdateIn
->wDayOfYear
, lcid
, dwFlags
, pDateOut
);
1320 if (lcid
!= MAKELCID(MAKELANGID(LANG_ENGLISH
, SUBLANG_ENGLISH_US
), SORT_DEFAULT
))
1321 FIXME("lcid possibly not handled, treating as en-us\n");
1323 memcpy(&ud
, pUdateIn
, sizeof(ud
));
1325 if (dwFlags
& VAR_VALIDDATE
)
1326 WARN("Ignoring VAR_VALIDDATE\n");
1328 if (FAILED(VARIANT_RollUdate(&ud
)))
1329 return E_INVALIDARG
;
1332 dateVal
= VARIANT_DateFromJulian(VARIANT_JulianFromDMY(ud
.st
.wYear
, ud
.st
.wMonth
, ud
.st
.wDay
));
1335 dateVal
+= ud
.st
.wHour
/ 24.0;
1336 dateVal
+= ud
.st
.wMinute
/ 1440.0;
1337 dateVal
+= ud
.st
.wSecond
/ 86400.0;
1338 dateVal
+= ud
.st
.wMilliseconds
/ 86400000.0;
1340 TRACE("Returning %g\n", dateVal
);
1341 *pDateOut
= dateVal
;
1345 /***********************************************************************
1346 * VarDateFromUdate [OLEAUT32.330]
1348 * Convert an unpacked format date and time to a variant VT_DATE.
1351 * pUdateIn [I] Unpacked format date and time to convert
1352 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1353 * pDateOut [O] Destination for variant VT_DATE.
1356 * Success: S_OK. *pDateOut contains the converted value.
1357 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1360 * This function uses the United States English locale for the conversion. Use
1361 * VarDateFromUdateEx() for alternate locales.
1363 HRESULT WINAPI
VarDateFromUdate(UDATE
*pUdateIn
, ULONG dwFlags
, DATE
*pDateOut
)
1365 LCID lcid
= MAKELCID(MAKELANGID(LANG_ENGLISH
, SUBLANG_ENGLISH_US
), SORT_DEFAULT
);
1367 return VarDateFromUdateEx(pUdateIn
, lcid
, dwFlags
, pDateOut
);
1370 /***********************************************************************
1371 * VarUdateFromDate [OLEAUT32.331]
1373 * Convert a variant VT_DATE into an unpacked format date and time.
1376 * datein [I] Variant VT_DATE format date
1377 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1378 * lpUdate [O] Destination for unpacked format date and time
1381 * Success: S_OK. *lpUdate contains the converted value.
1382 * Failure: E_INVALIDARG, if dateIn is too large or small.
1384 HRESULT WINAPI
VarUdateFromDate(DATE dateIn
, ULONG dwFlags
, UDATE
*lpUdate
)
1386 /* Cumulative totals of days per month */
1387 static const USHORT cumulativeDays
[] =
1389 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
1391 double datePart
, timePart
;
1394 TRACE("(%g,0x%08lx,%p)\n", dateIn
, dwFlags
, lpUdate
);
1396 if (dateIn
<= (DATE_MIN
- 1.0) || dateIn
>= (DATE_MAX
+ 1.0))
1397 return E_INVALIDARG
;
1399 datePart
= dateIn
< 0.0 ? ceil(dateIn
) : floor(dateIn
);
1400 /* Compensate for int truncation (always downwards) */
1401 timePart
= dateIn
- datePart
+ 0.00000000001;
1402 if (timePart
>= 1.0)
1403 timePart
-= 0.00000000001;
1406 julianDays
= VARIANT_JulianFromDate(dateIn
);
1407 VARIANT_DMYFromJulian(julianDays
, &lpUdate
->st
.wYear
, &lpUdate
->st
.wMonth
,
1410 datePart
= (datePart
+ 1.5) / 7.0;
1411 lpUdate
->st
.wDayOfWeek
= (datePart
- floor(datePart
)) * 7;
1412 if (lpUdate
->st
.wDayOfWeek
== 0)
1413 lpUdate
->st
.wDayOfWeek
= 5;
1414 else if (lpUdate
->st
.wDayOfWeek
== 1)
1415 lpUdate
->st
.wDayOfWeek
= 6;
1417 lpUdate
->st
.wDayOfWeek
-= 2;
1419 if (lpUdate
->st
.wMonth
> 2 && IsLeapYear(lpUdate
->st
.wYear
))
1420 lpUdate
->wDayOfYear
= 1; /* After February, in a leap year */
1422 lpUdate
->wDayOfYear
= 0;
1424 lpUdate
->wDayOfYear
+= cumulativeDays
[lpUdate
->st
.wMonth
];
1425 lpUdate
->wDayOfYear
+= lpUdate
->st
.wDay
;
1429 lpUdate
->st
.wHour
= timePart
;
1430 timePart
-= lpUdate
->st
.wHour
;
1432 lpUdate
->st
.wMinute
= timePart
;
1433 timePart
-= lpUdate
->st
.wMinute
;
1435 lpUdate
->st
.wSecond
= timePart
;
1436 timePart
-= lpUdate
->st
.wSecond
;
1437 lpUdate
->st
.wMilliseconds
= 0;
1440 /* Round the milliseconds, adjusting the time/date forward if needed */
1441 if (lpUdate
->st
.wSecond
< 59)
1442 lpUdate
->st
.wSecond
++;
1445 lpUdate
->st
.wSecond
= 0;
1446 if (lpUdate
->st
.wMinute
< 59)
1447 lpUdate
->st
.wMinute
++;
1450 lpUdate
->st
.wMinute
= 0;
1451 if (lpUdate
->st
.wHour
< 23)
1452 lpUdate
->st
.wHour
++;
1455 lpUdate
->st
.wHour
= 0;
1456 /* Roll over a whole day */
1457 if (++lpUdate
->st
.wDay
> 28)
1458 VARIANT_RollUdate(lpUdate
);
1466 #define GET_NUMBER_TEXT(fld,name) \
1468 if (!GetLocaleInfoW(lcid, lctype|fld, buff, 2)) \
1469 WARN("buffer too small for " #fld "\n"); \
1471 if (buff[0]) lpChars->name = buff[0]; \
1472 TRACE("lcid 0x%lx, " #name "=%d '%c'\n", lcid, lpChars->name, lpChars->name)
1474 /* Get the valid number characters for an lcid */
1475 void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS
*lpChars
, LCID lcid
, DWORD dwFlags
)
1477 static const VARIANT_NUMBER_CHARS defaultChars
= { '-','+','.',',','$',0,'.',',' };
1478 LCTYPE lctype
= dwFlags
& LOCALE_NOUSEROVERRIDE
;
1481 memcpy(lpChars
, &defaultChars
, sizeof(defaultChars
));
1482 GET_NUMBER_TEXT(LOCALE_SNEGATIVESIGN
, cNegativeSymbol
);
1483 GET_NUMBER_TEXT(LOCALE_SPOSITIVESIGN
, cPositiveSymbol
);
1484 GET_NUMBER_TEXT(LOCALE_SDECIMAL
, cDecimalPoint
);
1485 GET_NUMBER_TEXT(LOCALE_STHOUSAND
, cDigitSeperator
);
1486 GET_NUMBER_TEXT(LOCALE_SMONDECIMALSEP
, cCurrencyDecimalPoint
);
1487 GET_NUMBER_TEXT(LOCALE_SMONTHOUSANDSEP
, cCurrencyDigitSeperator
);
1489 /* Local currency symbols are often 2 characters */
1490 lpChars
->cCurrencyLocal2
= '\0';
1491 switch(GetLocaleInfoW(lcid
, lctype
|LOCALE_SCURRENCY
, buff
, sizeof(buff
)/sizeof(WCHAR
)))
1493 case 3: lpChars
->cCurrencyLocal2
= buff
[1]; /* Fall through */
1494 case 2: lpChars
->cCurrencyLocal
= buff
[0];
1496 default: WARN("buffer too small for LOCALE_SCURRENCY\n");
1498 TRACE("lcid 0x%lx, cCurrencyLocal =%d,%d '%c','%c'\n", lcid
, lpChars
->cCurrencyLocal
,
1499 lpChars
->cCurrencyLocal2
, lpChars
->cCurrencyLocal
, lpChars
->cCurrencyLocal2
);
1502 /* Number Parsing States */
1503 #define B_PROCESSING_EXPONENT 0x1
1504 #define B_NEGATIVE_EXPONENT 0x2
1505 #define B_EXPONENT_START 0x4
1506 #define B_INEXACT_ZEROS 0x8
1507 #define B_LEADING_ZERO 0x10
1508 #define B_PROCESSING_HEX 0x20
1509 #define B_PROCESSING_OCT 0x40
1511 /**********************************************************************
1512 * VarParseNumFromStr [OLEAUT32.46]
1514 * Parse a string containing a number into a NUMPARSE structure.
1517 * lpszStr [I] String to parse number from
1518 * lcid [I] Locale Id for the conversion
1519 * dwFlags [I] 0, or LOCALE_NOUSEROVERRIDE to use system default number chars
1520 * pNumprs [I/O] Destination for parsed number
1521 * rgbDig [O] Destination for digits read in
1524 * Success: S_OK. pNumprs and rgbDig contain the parsed representation of
1526 * Failure: E_INVALIDARG, if any parameter is invalid.
1527 * DISP_E_TYPEMISMATCH, if the string is not a number or is formatted
1529 * DISP_E_OVERFLOW, if rgbDig is too small to hold the number.
1532 * pNumprs must have the following fields set:
1533 * cDig: Set to the size of rgbDig.
1534 * dwInFlags: Set to the allowable syntax of the number using NUMPRS_ flags
1538 * - I am unsure if this function should parse non-arabic (e.g. Thai)
1539 * numerals, so this has not been implemented.
1541 HRESULT WINAPI
VarParseNumFromStr(OLECHAR
*lpszStr
, LCID lcid
, ULONG dwFlags
,
1542 NUMPARSE
*pNumprs
, BYTE
*rgbDig
)
1544 VARIANT_NUMBER_CHARS chars
;
1546 DWORD dwState
= B_EXPONENT_START
|B_INEXACT_ZEROS
;
1547 int iMaxDigits
= sizeof(rgbTmp
) / sizeof(BYTE
);
1550 TRACE("(%s,%ld,0x%08lx,%p,%p)\n", debugstr_w(lpszStr
), lcid
, dwFlags
, pNumprs
, rgbDig
);
1552 if (!pNumprs
|| !rgbDig
)
1553 return E_INVALIDARG
;
1555 if (pNumprs
->cDig
< iMaxDigits
)
1556 iMaxDigits
= pNumprs
->cDig
;
1559 pNumprs
->dwOutFlags
= 0;
1560 pNumprs
->cchUsed
= 0;
1561 pNumprs
->nBaseShift
= 0;
1562 pNumprs
->nPwr10
= 0;
1565 return DISP_E_TYPEMISMATCH
;
1567 VARIANT_GetLocalisedNumberChars(&chars
, lcid
, dwFlags
);
1569 /* First consume all the leading symbols and space from the string */
1572 if (pNumprs
->dwInFlags
& NUMPRS_LEADING_WHITE
&& isspaceW(*lpszStr
))
1574 pNumprs
->dwOutFlags
|= NUMPRS_LEADING_WHITE
;
1579 } while (isspaceW(*lpszStr
));
1581 else if (pNumprs
->dwInFlags
& NUMPRS_LEADING_PLUS
&&
1582 *lpszStr
== chars
.cPositiveSymbol
&&
1583 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_PLUS
))
1585 pNumprs
->dwOutFlags
|= NUMPRS_LEADING_PLUS
;
1589 else if (pNumprs
->dwInFlags
& NUMPRS_LEADING_MINUS
&&
1590 *lpszStr
== chars
.cNegativeSymbol
&&
1591 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_MINUS
))
1593 pNumprs
->dwOutFlags
|= (NUMPRS_LEADING_MINUS
|NUMPRS_NEG
);
1597 else if (pNumprs
->dwInFlags
& NUMPRS_CURRENCY
&&
1598 !(pNumprs
->dwOutFlags
& NUMPRS_CURRENCY
) &&
1599 *lpszStr
== chars
.cCurrencyLocal
&&
1600 (!chars
.cCurrencyLocal2
|| lpszStr
[1] == chars
.cCurrencyLocal2
))
1602 pNumprs
->dwOutFlags
|= NUMPRS_CURRENCY
;
1605 /* Only accept currency characters */
1606 chars
.cDecimalPoint
= chars
.cCurrencyDecimalPoint
;
1607 chars
.cDigitSeperator
= chars
.cCurrencyDigitSeperator
;
1609 else if (pNumprs
->dwInFlags
& NUMPRS_PARENS
&& *lpszStr
== '(' &&
1610 !(pNumprs
->dwOutFlags
& NUMPRS_PARENS
))
1612 pNumprs
->dwOutFlags
|= NUMPRS_PARENS
;
1620 if (!(pNumprs
->dwOutFlags
& NUMPRS_CURRENCY
))
1622 /* Only accept non-currency characters */
1623 chars
.cCurrencyDecimalPoint
= chars
.cDecimalPoint
;
1624 chars
.cCurrencyDigitSeperator
= chars
.cDigitSeperator
;
1627 if ((*lpszStr
== '&' && (*(lpszStr
+1) == 'H' || *(lpszStr
+1) == 'h')) &&
1628 pNumprs
->dwInFlags
& NUMPRS_HEX_OCT
)
1630 dwState
|= B_PROCESSING_HEX
;
1631 pNumprs
->dwOutFlags
|= NUMPRS_HEX_OCT
;
1635 else if ((*lpszStr
== '&' && (*(lpszStr
+1) == 'O' || *(lpszStr
+1) == 'o')) &&
1636 pNumprs
->dwInFlags
& NUMPRS_HEX_OCT
)
1638 dwState
|= B_PROCESSING_OCT
;
1639 pNumprs
->dwOutFlags
|= NUMPRS_HEX_OCT
;
1644 /* Strip Leading zeros */
1645 while (*lpszStr
== '0')
1647 dwState
|= B_LEADING_ZERO
;
1654 if (isdigitW(*lpszStr
))
1656 if (dwState
& B_PROCESSING_EXPONENT
)
1658 int exponentSize
= 0;
1659 if (dwState
& B_EXPONENT_START
)
1661 if (!isdigitW(*lpszStr
))
1662 break; /* No exponent digits - invalid */
1663 while (*lpszStr
== '0')
1665 /* Skip leading zero's in the exponent */
1671 while (isdigitW(*lpszStr
))
1674 exponentSize
+= *lpszStr
- '0';
1678 if (dwState
& B_NEGATIVE_EXPONENT
)
1679 exponentSize
= -exponentSize
;
1680 /* Add the exponent into the powers of 10 */
1681 pNumprs
->nPwr10
+= exponentSize
;
1682 dwState
&= ~(B_PROCESSING_EXPONENT
|B_EXPONENT_START
);
1683 lpszStr
--; /* back up to allow processing of next char */
1687 if ((pNumprs
->cDig
>= iMaxDigits
) && !(dwState
& B_PROCESSING_HEX
)
1688 && !(dwState
& B_PROCESSING_OCT
))
1690 pNumprs
->dwOutFlags
|= NUMPRS_INEXACT
;
1692 if (*lpszStr
!= '0')
1693 dwState
&= ~B_INEXACT_ZEROS
; /* Inexact number with non-trailing zeros */
1695 /* This digit can't be represented, but count it in nPwr10 */
1696 if (pNumprs
->dwOutFlags
& NUMPRS_DECIMAL
)
1703 if ((dwState
& B_PROCESSING_OCT
) && ((*lpszStr
== '8') || (*lpszStr
== '9'))) {
1704 return DISP_E_TYPEMISMATCH
;
1707 if (pNumprs
->dwOutFlags
& NUMPRS_DECIMAL
)
1708 pNumprs
->nPwr10
--; /* Count decimal points in nPwr10 */
1710 rgbTmp
[pNumprs
->cDig
] = *lpszStr
- '0';
1716 else if (*lpszStr
== chars
.cDigitSeperator
&& pNumprs
->dwInFlags
& NUMPRS_THOUSANDS
)
1718 pNumprs
->dwOutFlags
|= NUMPRS_THOUSANDS
;
1721 else if (*lpszStr
== chars
.cDecimalPoint
&&
1722 pNumprs
->dwInFlags
& NUMPRS_DECIMAL
&&
1723 !(pNumprs
->dwOutFlags
& (NUMPRS_DECIMAL
|NUMPRS_EXPONENT
)))
1725 pNumprs
->dwOutFlags
|= NUMPRS_DECIMAL
;
1728 /* If we have no digits so far, skip leading zeros */
1731 while (lpszStr
[1] == '0')
1733 dwState
|= B_LEADING_ZERO
;
1740 else if ((*lpszStr
== 'e' || *lpszStr
== 'E') &&
1741 pNumprs
->dwInFlags
& NUMPRS_EXPONENT
&&
1742 !(pNumprs
->dwOutFlags
& NUMPRS_EXPONENT
))
1744 dwState
|= B_PROCESSING_EXPONENT
;
1745 pNumprs
->dwOutFlags
|= NUMPRS_EXPONENT
;
1748 else if (dwState
& B_PROCESSING_EXPONENT
&& *lpszStr
== chars
.cPositiveSymbol
)
1750 cchUsed
++; /* Ignore positive exponent */
1752 else if (dwState
& B_PROCESSING_EXPONENT
&& *lpszStr
== chars
.cNegativeSymbol
)
1754 dwState
|= B_NEGATIVE_EXPONENT
;
1757 else if (((*lpszStr
>= 'a' && *lpszStr
<= 'f') ||
1758 (*lpszStr
>= 'A' && *lpszStr
<= 'F')) &&
1759 dwState
& B_PROCESSING_HEX
)
1761 if (pNumprs
->cDig
>= iMaxDigits
)
1763 return DISP_E_OVERFLOW
;
1767 if (*lpszStr
>= 'a')
1768 rgbTmp
[pNumprs
->cDig
] = *lpszStr
- 'a' + 10;
1770 rgbTmp
[pNumprs
->cDig
] = *lpszStr
- 'A' + 10;
1776 break; /* Stop at an unrecognised character */
1781 if (!pNumprs
->cDig
&& dwState
& B_LEADING_ZERO
)
1783 /* Ensure a 0 on its own gets stored */
1788 if (pNumprs
->dwOutFlags
& NUMPRS_EXPONENT
&& dwState
& B_PROCESSING_EXPONENT
)
1790 pNumprs
->cchUsed
= cchUsed
;
1791 return DISP_E_TYPEMISMATCH
; /* Failed to completely parse the exponent */
1794 if (pNumprs
->dwOutFlags
& NUMPRS_INEXACT
)
1796 if (dwState
& B_INEXACT_ZEROS
)
1797 pNumprs
->dwOutFlags
&= ~NUMPRS_INEXACT
; /* All zeros doesn't set NUMPRS_INEXACT */
1798 } else if(pNumprs
->dwInFlags
& NUMPRS_HEX_OCT
)
1800 /* copy all of the digits into the output digit buffer */
1801 /* this is exactly what windows does although it also returns */
1802 /* cDig of X and writes X+Y where Y>=0 number of digits to rgbDig */
1803 memcpy(rgbDig
, rgbTmp
, pNumprs
->cDig
* sizeof(BYTE
));
1805 if (dwState
& B_PROCESSING_HEX
) {
1806 /* hex numbers have always the same format */
1808 pNumprs
->nBaseShift
=4;
1810 if (dwState
& B_PROCESSING_OCT
) {
1811 /* oct numbers have always the same format */
1813 pNumprs
->nBaseShift
=3;
1815 while (pNumprs
->cDig
> 1 && !rgbTmp
[pNumprs
->cDig
- 1])
1824 /* Remove trailing zeros from the last (whole number or decimal) part */
1825 while (pNumprs
->cDig
> 1 && !rgbTmp
[pNumprs
->cDig
- 1])
1832 if (pNumprs
->cDig
<= iMaxDigits
)
1833 pNumprs
->dwOutFlags
&= ~NUMPRS_INEXACT
; /* Ignore stripped zeros for NUMPRS_INEXACT */
1835 pNumprs
->cDig
= iMaxDigits
; /* Only return iMaxDigits worth of digits */
1837 /* Copy the digits we processed into rgbDig */
1838 memcpy(rgbDig
, rgbTmp
, pNumprs
->cDig
* sizeof(BYTE
));
1840 /* Consume any trailing symbols and space */
1843 if ((pNumprs
->dwInFlags
& NUMPRS_TRAILING_WHITE
) && isspaceW(*lpszStr
))
1845 pNumprs
->dwOutFlags
|= NUMPRS_TRAILING_WHITE
;
1850 } while (isspaceW(*lpszStr
));
1852 else if (pNumprs
->dwInFlags
& NUMPRS_TRAILING_PLUS
&&
1853 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_PLUS
) &&
1854 *lpszStr
== chars
.cPositiveSymbol
)
1856 pNumprs
->dwOutFlags
|= NUMPRS_TRAILING_PLUS
;
1860 else if (pNumprs
->dwInFlags
& NUMPRS_TRAILING_MINUS
&&
1861 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_MINUS
) &&
1862 *lpszStr
== chars
.cNegativeSymbol
)
1864 pNumprs
->dwOutFlags
|= (NUMPRS_TRAILING_MINUS
|NUMPRS_NEG
);
1868 else if (pNumprs
->dwInFlags
& NUMPRS_PARENS
&& *lpszStr
== ')' &&
1869 pNumprs
->dwOutFlags
& NUMPRS_PARENS
)
1873 pNumprs
->dwOutFlags
|= NUMPRS_NEG
;
1879 if (pNumprs
->dwOutFlags
& NUMPRS_PARENS
&& !(pNumprs
->dwOutFlags
& NUMPRS_NEG
))
1881 pNumprs
->cchUsed
= cchUsed
;
1882 return DISP_E_TYPEMISMATCH
; /* Opening parenthesis not matched */
1885 if (pNumprs
->dwInFlags
& NUMPRS_USE_ALL
&& *lpszStr
!= '\0')
1886 return DISP_E_TYPEMISMATCH
; /* Not all chars were consumed */
1889 return DISP_E_TYPEMISMATCH
; /* No Number found */
1891 pNumprs
->cchUsed
= cchUsed
;
1895 /* VTBIT flags indicating an integer value */
1896 #define INTEGER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8)
1897 /* VTBIT flags indicating a real number value */
1898 #define REAL_VTBITS (VTBIT_R4|VTBIT_R8|VTBIT_CY)
1900 /* Helper macros to check whether bit pattern fits in VARIANT (x is a ULONG64 ) */
1901 #define FITS_AS_I1(x) ((x) >> 8 == 0)
1902 #define FITS_AS_I2(x) ((x) >> 16 == 0)
1903 #define FITS_AS_I4(x) ((x) >> 32 == 0)
1905 /**********************************************************************
1906 * VarNumFromParseNum [OLEAUT32.47]
1908 * Convert a NUMPARSE structure into a numeric Variant type.
1911 * pNumprs [I] Source for parsed number. cDig must be set to the size of rgbDig
1912 * rgbDig [I] Source for the numbers digits
1913 * dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
1914 * pVarDst [O] Destination for the converted Variant value.
1917 * Success: S_OK. pVarDst contains the converted value.
1918 * Failure: E_INVALIDARG, if any parameter is invalid.
1919 * DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
1922 * - The smallest favoured type present in dwVtBits that can represent the
1923 * number in pNumprs without losing precision is used.
1924 * - Signed types are preferrred over unsigned types of the same size.
1925 * - Preferred types in order are: integer, float, double, currency then decimal.
1926 * - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
1927 * for details of the rounding method.
1928 * - pVarDst is not cleared before the result is stored in it.
1929 * - WinXP and Win2003 support VTBIT_I8, VTBIT_UI8 but that's buggy (by
1930 * design?): If some other VTBIT's for integers are specified together
1931 * with VTBIT_I8 and the number will fit only in a VT_I8 Windows will "cast"
1932 * the number to the smallest requested integer truncating this way the
1933 * number. Wine dosn't implement this "feature" (yet?).
1935 HRESULT WINAPI
VarNumFromParseNum(NUMPARSE
*pNumprs
, BYTE
*rgbDig
,
1936 ULONG dwVtBits
, VARIANT
*pVarDst
)
1938 /* Scale factors and limits for double arithmetic */
1939 static const double dblMultipliers
[11] = {
1940 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
1941 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
1943 static const double dblMinimums
[11] = {
1944 R8_MIN
, R8_MIN
*10.0, R8_MIN
*100.0, R8_MIN
*1000.0, R8_MIN
*10000.0,
1945 R8_MIN
*100000.0, R8_MIN
*1000000.0, R8_MIN
*10000000.0,
1946 R8_MIN
*100000000.0, R8_MIN
*1000000000.0, R8_MIN
*10000000000.0
1948 static const double dblMaximums
[11] = {
1949 R8_MAX
, R8_MAX
/10.0, R8_MAX
/100.0, R8_MAX
/1000.0, R8_MAX
/10000.0,
1950 R8_MAX
/100000.0, R8_MAX
/1000000.0, R8_MAX
/10000000.0,
1951 R8_MAX
/100000000.0, R8_MAX
/1000000000.0, R8_MAX
/10000000000.0
1954 int wholeNumberDigits
, fractionalDigits
, divisor10
= 0, multiplier10
= 0;
1956 TRACE("(%p,%p,0x%lx,%p)\n", pNumprs
, rgbDig
, dwVtBits
, pVarDst
);
1958 if (pNumprs
->nBaseShift
)
1960 /* nBaseShift indicates a hex or octal number */
1965 /* Convert the hex or octal number string into a UI64 */
1966 for (i
= 0; i
< pNumprs
->cDig
; i
++)
1968 if (ul64
> ((UI8_MAX
>>pNumprs
->nBaseShift
) - rgbDig
[i
]))
1970 TRACE("Overflow multiplying digits\n");
1971 return DISP_E_OVERFLOW
;
1973 ul64
= (ul64
<<pNumprs
->nBaseShift
) + rgbDig
[i
];
1976 /* also make a negative representation */
1979 /* Try signed and unsigned types in size order */
1980 if (dwVtBits
& VTBIT_I1
&& FITS_AS_I1(ul64
))
1982 V_VT(pVarDst
) = VT_I1
;
1983 V_I1(pVarDst
) = ul64
;
1986 else if (dwVtBits
& VTBIT_UI1
&& FITS_AS_I1(ul64
))
1988 V_VT(pVarDst
) = VT_UI1
;
1989 V_UI1(pVarDst
) = ul64
;
1992 else if (dwVtBits
& VTBIT_I2
&& FITS_AS_I2(ul64
))
1994 V_VT(pVarDst
) = VT_I2
;
1995 V_I2(pVarDst
) = ul64
;
1998 else if (dwVtBits
& VTBIT_UI2
&& FITS_AS_I2(ul64
))
2000 V_VT(pVarDst
) = VT_UI2
;
2001 V_UI2(pVarDst
) = ul64
;
2004 else if (dwVtBits
& VTBIT_I4
&& FITS_AS_I4(ul64
))
2006 V_VT(pVarDst
) = VT_I4
;
2007 V_I4(pVarDst
) = ul64
;
2010 else if (dwVtBits
& VTBIT_UI4
&& FITS_AS_I4(ul64
))
2012 V_VT(pVarDst
) = VT_UI4
;
2013 V_UI4(pVarDst
) = ul64
;
2016 else if (dwVtBits
& VTBIT_I8
&& ((ul64
<= I8_MAX
)||(l64
>=I8_MIN
)))
2018 V_VT(pVarDst
) = VT_I8
;
2019 V_I8(pVarDst
) = ul64
;
2022 else if (dwVtBits
& VTBIT_UI8
)
2024 V_VT(pVarDst
) = VT_UI8
;
2025 V_UI8(pVarDst
) = ul64
;
2028 else if ((dwVtBits
& REAL_VTBITS
) == VTBIT_DECIMAL
)
2030 V_VT(pVarDst
) = VT_DECIMAL
;
2031 DEC_SIGNSCALE(&V_DECIMAL(pVarDst
)) = SIGNSCALE(DECIMAL_POS
,0);
2032 DEC_HI32(&V_DECIMAL(pVarDst
)) = 0;
2033 DEC_LO64(&V_DECIMAL(pVarDst
)) = ul64
;
2036 else if (dwVtBits
& VTBIT_R4
&& ((ul64
<= I4_MAX
)||(l64
>= I4_MIN
)))
2038 V_VT(pVarDst
) = VT_R4
;
2040 V_R4(pVarDst
) = ul64
;
2042 V_R4(pVarDst
) = l64
;
2045 else if (dwVtBits
& VTBIT_R8
&& ((ul64
<= I4_MAX
)||(l64
>= I4_MIN
)))
2047 V_VT(pVarDst
) = VT_R8
;
2049 V_R8(pVarDst
) = ul64
;
2051 V_R8(pVarDst
) = l64
;
2055 TRACE("Overflow: possible return types: 0x%lx, value: %s\n", dwVtBits
, wine_dbgstr_longlong(ul64
));
2056 return DISP_E_OVERFLOW
;
2059 /* Count the number of relevant fractional and whole digits stored,
2060 * And compute the divisor/multiplier to scale the number by.
2062 if (pNumprs
->nPwr10
< 0)
2064 if (-pNumprs
->nPwr10
>= pNumprs
->cDig
)
2066 /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
2067 wholeNumberDigits
= 0;
2068 fractionalDigits
= pNumprs
->cDig
;
2069 divisor10
= -pNumprs
->nPwr10
;
2073 /* An exactly represented real number e.g. 1.024 */
2074 wholeNumberDigits
= pNumprs
->cDig
+ pNumprs
->nPwr10
;
2075 fractionalDigits
= pNumprs
->cDig
- wholeNumberDigits
;
2076 divisor10
= pNumprs
->cDig
- wholeNumberDigits
;
2079 else if (pNumprs
->nPwr10
== 0)
2081 /* An exactly represented whole number e.g. 1024 */
2082 wholeNumberDigits
= pNumprs
->cDig
;
2083 fractionalDigits
= 0;
2085 else /* pNumprs->nPwr10 > 0 */
2087 /* A whole number followed by nPwr10 0's e.g. 102400 */
2088 wholeNumberDigits
= pNumprs
->cDig
;
2089 fractionalDigits
= 0;
2090 multiplier10
= pNumprs
->nPwr10
;
2093 TRACE("cDig %d; nPwr10 %d, whole %d, frac %d ", pNumprs
->cDig
,
2094 pNumprs
->nPwr10
, wholeNumberDigits
, fractionalDigits
);
2095 TRACE("mult %d; div %d\n", multiplier10
, divisor10
);
2097 if (dwVtBits
& (INTEGER_VTBITS
|VTBIT_DECIMAL
) &&
2098 (!fractionalDigits
|| !(dwVtBits
& (REAL_VTBITS
|VTBIT_CY
|VTBIT_DECIMAL
))))
2100 /* We have one or more integer output choices, and either:
2101 * 1) An integer input value, or
2102 * 2) A real number input value but no floating output choices.
2103 * Alternately, we have a DECIMAL output available and an integer input.
2105 * So, place the integer value into pVarDst, using the smallest type
2106 * possible and preferring signed over unsigned types.
2108 BOOL bOverflow
= FALSE
, bNegative
;
2112 /* Convert the integer part of the number into a UI8 */
2113 for (i
= 0; i
< wholeNumberDigits
; i
++)
2115 if (ul64
> (UI8_MAX
/ 10 - rgbDig
[i
]))
2117 TRACE("Overflow multiplying digits\n");
2121 ul64
= ul64
* 10 + rgbDig
[i
];
2124 /* Account for the scale of the number */
2125 if (!bOverflow
&& multiplier10
)
2127 for (i
= 0; i
< multiplier10
; i
++)
2129 if (ul64
> (UI8_MAX
/ 10))
2131 TRACE("Overflow scaling number\n");
2139 /* If we have any fractional digits, round the value.
2140 * Note we don't have to do this if divisor10 is < 1,
2141 * because this means the fractional part must be < 0.5
2143 if (!bOverflow
&& fractionalDigits
&& divisor10
> 0)
2145 const BYTE
* fracDig
= rgbDig
+ wholeNumberDigits
;
2146 BOOL bAdjust
= FALSE
;
2148 TRACE("first decimal value is %d\n", *fracDig
);
2151 bAdjust
= TRUE
; /* > 0.5 */
2152 else if (*fracDig
== 5)
2154 for (i
= 1; i
< fractionalDigits
; i
++)
2158 bAdjust
= TRUE
; /* > 0.5 */
2162 /* If exactly 0.5, round only odd values */
2163 if (i
== fractionalDigits
&& (ul64
& 1))
2169 if (ul64
== UI8_MAX
)
2171 TRACE("Overflow after rounding\n");
2178 /* Zero is not a negative number */
2179 bNegative
= pNumprs
->dwOutFlags
& NUMPRS_NEG
&& ul64
? TRUE
: FALSE
;
2181 TRACE("Integer value is %lld, bNeg %d\n", ul64
, bNegative
);
2183 /* For negative integers, try the signed types in size order */
2184 if (!bOverflow
&& bNegative
)
2186 if (dwVtBits
& (VTBIT_I1
|VTBIT_I2
|VTBIT_I4
|VTBIT_I8
))
2188 if (dwVtBits
& VTBIT_I1
&& ul64
<= -I1_MIN
)
2190 V_VT(pVarDst
) = VT_I1
;
2191 V_I1(pVarDst
) = -ul64
;
2194 else if (dwVtBits
& VTBIT_I2
&& ul64
<= -I2_MIN
)
2196 V_VT(pVarDst
) = VT_I2
;
2197 V_I2(pVarDst
) = -ul64
;
2200 else if (dwVtBits
& VTBIT_I4
&& ul64
<= -((LONGLONG
)I4_MIN
))
2202 V_VT(pVarDst
) = VT_I4
;
2203 V_I4(pVarDst
) = -ul64
;
2206 else if (dwVtBits
& VTBIT_I8
&& ul64
<= (ULONGLONG
)I8_MAX
+ 1)
2208 V_VT(pVarDst
) = VT_I8
;
2209 V_I8(pVarDst
) = -ul64
;
2212 else if ((dwVtBits
& REAL_VTBITS
) == VTBIT_DECIMAL
)
2214 /* Decimal is only output choice left - fast path */
2215 V_VT(pVarDst
) = VT_DECIMAL
;
2216 DEC_SIGNSCALE(&V_DECIMAL(pVarDst
)) = SIGNSCALE(DECIMAL_NEG
,0);
2217 DEC_HI32(&V_DECIMAL(pVarDst
)) = 0;
2218 DEC_LO64(&V_DECIMAL(pVarDst
)) = -ul64
;
2223 else if (!bOverflow
)
2225 /* For positive integers, try signed then unsigned types in size order */
2226 if (dwVtBits
& VTBIT_I1
&& ul64
<= I1_MAX
)
2228 V_VT(pVarDst
) = VT_I1
;
2229 V_I1(pVarDst
) = ul64
;
2232 else if (dwVtBits
& VTBIT_UI1
&& ul64
<= UI1_MAX
)
2234 V_VT(pVarDst
) = VT_UI1
;
2235 V_UI1(pVarDst
) = ul64
;
2238 else if (dwVtBits
& VTBIT_I2
&& ul64
<= I2_MAX
)
2240 V_VT(pVarDst
) = VT_I2
;
2241 V_I2(pVarDst
) = ul64
;
2244 else if (dwVtBits
& VTBIT_UI2
&& ul64
<= UI2_MAX
)
2246 V_VT(pVarDst
) = VT_UI2
;
2247 V_UI2(pVarDst
) = ul64
;
2250 else if (dwVtBits
& VTBIT_I4
&& ul64
<= I4_MAX
)
2252 V_VT(pVarDst
) = VT_I4
;
2253 V_I4(pVarDst
) = ul64
;
2256 else if (dwVtBits
& VTBIT_UI4
&& ul64
<= UI4_MAX
)
2258 V_VT(pVarDst
) = VT_UI4
;
2259 V_UI4(pVarDst
) = ul64
;
2262 else if (dwVtBits
& VTBIT_I8
&& ul64
<= I8_MAX
)
2264 V_VT(pVarDst
) = VT_I8
;
2265 V_I8(pVarDst
) = ul64
;
2268 else if (dwVtBits
& VTBIT_UI8
)
2270 V_VT(pVarDst
) = VT_UI8
;
2271 V_UI8(pVarDst
) = ul64
;
2274 else if ((dwVtBits
& REAL_VTBITS
) == VTBIT_DECIMAL
)
2276 /* Decimal is only output choice left - fast path */
2277 V_VT(pVarDst
) = VT_DECIMAL
;
2278 DEC_SIGNSCALE(&V_DECIMAL(pVarDst
)) = SIGNSCALE(DECIMAL_POS
,0);
2279 DEC_HI32(&V_DECIMAL(pVarDst
)) = 0;
2280 DEC_LO64(&V_DECIMAL(pVarDst
)) = ul64
;
2286 if (dwVtBits
& REAL_VTBITS
)
2288 /* Try to put the number into a float or real */
2289 BOOL bOverflow
= FALSE
, bNegative
= pNumprs
->dwOutFlags
& NUMPRS_NEG
;
2293 /* Convert the number into a double */
2294 for (i
= 0; i
< pNumprs
->cDig
; i
++)
2295 whole
= whole
* 10.0 + rgbDig
[i
];
2297 TRACE("Whole double value is %16.16g\n", whole
);
2299 /* Account for the scale */
2300 while (multiplier10
> 10)
2302 if (whole
> dblMaximums
[10])
2304 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
);
2308 whole
= whole
* dblMultipliers
[10];
2313 if (whole
> dblMaximums
[multiplier10
])
2315 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
);
2319 whole
= whole
* dblMultipliers
[multiplier10
];
2322 TRACE("Scaled double value is %16.16g\n", whole
);
2324 while (divisor10
> 10)
2326 if (whole
< dblMinimums
[10] && whole
!= 0)
2328 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
); /* Underflow */
2332 whole
= whole
/ dblMultipliers
[10];
2337 if (whole
< dblMinimums
[divisor10
] && whole
!= 0)
2339 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
); /* Underflow */
2343 whole
= whole
/ dblMultipliers
[divisor10
];
2346 TRACE("Final double value is %16.16g\n", whole
);
2348 if (dwVtBits
& VTBIT_R4
&&
2349 ((whole
<= R4_MAX
&& whole
>= R4_MIN
) || whole
== 0.0))
2351 TRACE("Set R4 to final value\n");
2352 V_VT(pVarDst
) = VT_R4
; /* Fits into a float */
2353 V_R4(pVarDst
) = pNumprs
->dwOutFlags
& NUMPRS_NEG
? -whole
: whole
;
2357 if (dwVtBits
& VTBIT_R8
)
2359 TRACE("Set R8 to final value\n");
2360 V_VT(pVarDst
) = VT_R8
; /* Fits into a double */
2361 V_R8(pVarDst
) = pNumprs
->dwOutFlags
& NUMPRS_NEG
? -whole
: whole
;
2365 if (dwVtBits
& VTBIT_CY
)
2367 if (SUCCEEDED(VarCyFromR8(bNegative
? -whole
: whole
, &V_CY(pVarDst
))))
2369 V_VT(pVarDst
) = VT_CY
; /* Fits into a currency */
2370 TRACE("Set CY to final value\n");
2373 TRACE("Value Overflows CY\n");
2377 if (dwVtBits
& VTBIT_DECIMAL
)
2382 DECIMAL
* pDec
= &V_DECIMAL(pVarDst
);
2384 DECIMAL_SETZERO(*pDec
);
2387 if (pNumprs
->dwOutFlags
& NUMPRS_NEG
)
2388 DEC_SIGN(pDec
) = DECIMAL_NEG
;
2390 DEC_SIGN(pDec
) = DECIMAL_POS
;
2392 /* Factor the significant digits */
2393 for (i
= 0; i
< pNumprs
->cDig
; i
++)
2395 tmp
= (ULONG64
)DEC_LO32(pDec
) * 10 + rgbDig
[i
];
2396 carry
= (ULONG
)(tmp
>> 32);
2397 DEC_LO32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2398 tmp
= (ULONG64
)DEC_MID32(pDec
) * 10 + carry
;
2399 carry
= (ULONG
)(tmp
>> 32);
2400 DEC_MID32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2401 tmp
= (ULONG64
)DEC_HI32(pDec
) * 10 + carry
;
2402 DEC_HI32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2404 if (tmp
>> 32 & UI4_MAX
)
2406 VarNumFromParseNum_DecOverflow
:
2407 TRACE("Overflow\n");
2408 DEC_LO32(pDec
) = DEC_MID32(pDec
) = DEC_HI32(pDec
) = UI4_MAX
;
2409 return DISP_E_OVERFLOW
;
2413 /* Account for the scale of the number */
2414 while (multiplier10
> 0)
2416 tmp
= (ULONG64
)DEC_LO32(pDec
) * 10;
2417 carry
= (ULONG
)(tmp
>> 32);
2418 DEC_LO32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2419 tmp
= (ULONG64
)DEC_MID32(pDec
) * 10 + carry
;
2420 carry
= (ULONG
)(tmp
>> 32);
2421 DEC_MID32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2422 tmp
= (ULONG64
)DEC_HI32(pDec
) * 10 + carry
;
2423 DEC_HI32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2425 if (tmp
>> 32 & UI4_MAX
)
2426 goto VarNumFromParseNum_DecOverflow
;
2429 DEC_SCALE(pDec
) = divisor10
;
2431 V_VT(pVarDst
) = VT_DECIMAL
;
2434 return DISP_E_OVERFLOW
; /* No more output choices */
2437 /**********************************************************************
2438 * VarCat [OLEAUT32.318]
2440 * Concatenates one variant onto another.
2443 * left [I] First variant
2444 * right [I] Second variant
2445 * result [O] Result variant
2449 * Failure: An HRESULT error code indicating the error.
2451 HRESULT WINAPI
VarCat(LPVARIANT left
, LPVARIANT right
, LPVARIANT out
)
2453 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
),
2454 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
), out
);
2456 /* Should we VariantClear out? */
2457 /* Can we handle array, vector, by ref etc. */
2458 if ((V_VT(left
)&VT_TYPEMASK
) == VT_NULL
&&
2459 (V_VT(right
)&VT_TYPEMASK
) == VT_NULL
)
2461 V_VT(out
) = VT_NULL
;
2465 if (V_VT(left
) == VT_BSTR
&& V_VT(right
) == VT_BSTR
)
2467 V_VT(out
) = VT_BSTR
;
2468 VarBstrCat (V_BSTR(left
), V_BSTR(right
), &V_BSTR(out
));
2471 if (V_VT(left
) == VT_BSTR
) {
2475 V_VT(out
) = VT_BSTR
;
2476 VariantInit(&bstrvar
);
2477 hres
= VariantChangeTypeEx(&bstrvar
,right
,0,0,VT_BSTR
);
2479 FIXME("Failed to convert right side from vt %d to VT_BSTR?\n",V_VT(right
));
2482 VarBstrCat (V_BSTR(left
), V_BSTR(&bstrvar
), &V_BSTR(out
));
2485 if (V_VT(right
) == VT_BSTR
) {
2489 V_VT(out
) = VT_BSTR
;
2490 VariantInit(&bstrvar
);
2491 hres
= VariantChangeTypeEx(&bstrvar
,left
,0,0,VT_BSTR
);
2493 FIXME("Failed to convert right side from vt %d to VT_BSTR?\n",V_VT(right
));
2496 VarBstrCat (V_BSTR(&bstrvar
), V_BSTR(right
), &V_BSTR(out
));
2499 FIXME ("types %d / %d not supported\n",V_VT(left
)&VT_TYPEMASK
, V_VT(right
)&VT_TYPEMASK
);
2503 /**********************************************************************
2504 * VarCmp [OLEAUT32.176]
2507 * NORM_IGNORECASE, NORM_IGNORENONSPACE, NORM_IGNORESYMBOLS
2508 * NORM_IGNOREWIDTH, NORM_IGNOREKANATYPE, NORM_IGNOREKASHIDA
2511 HRESULT WINAPI
VarCmp(LPVARIANT left
, LPVARIANT right
, LCID lcid
, DWORD flags
)
2521 TRACE("(%p->(%s%s),%p->(%s%s),0x%08lx,0x%08lx)\n", left
, debugstr_VT(left
),
2522 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
), lcid
, flags
);
2524 VariantInit(&lv
);VariantInit(&rv
);
2525 V_VT(right
) &= ~0x8000; /* hack since we sometime get this flag. */
2526 V_VT(left
) &= ~0x8000; /* hack since we sometime get this flag. */
2528 /* If either are null, then return VARCMP_NULL */
2529 if ((V_VT(left
)&VT_TYPEMASK
) == VT_NULL
||
2530 (V_VT(right
)&VT_TYPEMASK
) == VT_NULL
)
2533 /* Strings - use VarBstrCmp */
2534 if ((V_VT(left
)&VT_TYPEMASK
) == VT_BSTR
&&
2535 (V_VT(right
)&VT_TYPEMASK
) == VT_BSTR
) {
2536 return VarBstrCmp(V_BSTR(left
), V_BSTR(right
), lcid
, flags
);
2539 xmask
= (1<<(V_VT(left
)&VT_TYPEMASK
))|(1<<(V_VT(right
)&VT_TYPEMASK
));
2540 if (xmask
& VTBIT_R8
) {
2541 rc
= VariantChangeType(&lv
,left
,0,VT_R8
);
2542 if (FAILED(rc
)) return rc
;
2543 rc
= VariantChangeType(&rv
,right
,0,VT_R8
);
2544 if (FAILED(rc
)) return rc
;
2546 if (V_R8(&lv
) == V_R8(&rv
)) return VARCMP_EQ
;
2547 if (V_R8(&lv
) < V_R8(&rv
)) return VARCMP_LT
;
2548 if (V_R8(&lv
) > V_R8(&rv
)) return VARCMP_GT
;
2549 return E_FAIL
; /* can't get here */
2551 if (xmask
& VTBIT_R4
) {
2552 rc
= VariantChangeType(&lv
,left
,0,VT_R4
);
2553 if (FAILED(rc
)) return rc
;
2554 rc
= VariantChangeType(&rv
,right
,0,VT_R4
);
2555 if (FAILED(rc
)) return rc
;
2557 if (V_R4(&lv
) == V_R4(&rv
)) return VARCMP_EQ
;
2558 if (V_R4(&lv
) < V_R4(&rv
)) return VARCMP_LT
;
2559 if (V_R4(&lv
) > V_R4(&rv
)) return VARCMP_GT
;
2560 return E_FAIL
; /* can't get here */
2563 /* Integers - Ideally like to use VarDecCmp, but no Dec support yet
2564 Use LONGLONG to maximize ranges */
2566 switch (V_VT(left
)&VT_TYPEMASK
) {
2567 case VT_I1
: lVal
= V_I1(left
); break;
2568 case VT_I2
: lVal
= V_I2(left
); break;
2570 case VT_INT
: lVal
= V_I4(left
); break;
2571 case VT_UI1
: lVal
= V_UI1(left
); break;
2572 case VT_UI2
: lVal
= V_UI2(left
); break;
2574 case VT_UINT
: lVal
= V_UI4(left
); break;
2575 case VT_BOOL
: lVal
= V_BOOL(left
); break;
2576 case VT_EMPTY
: lVal
= 0; break;
2577 default: lOk
= FALSE
;
2581 switch (V_VT(right
)&VT_TYPEMASK
) {
2582 case VT_I1
: rVal
= V_I1(right
); break;
2583 case VT_I2
: rVal
= V_I2(right
); break;
2585 case VT_INT
: rVal
= V_I4(right
); break;
2586 case VT_UI1
: rVal
= V_UI1(right
); break;
2587 case VT_UI2
: rVal
= V_UI2(right
); break;
2589 case VT_UINT
: rVal
= V_UI4(right
); break;
2590 case VT_BOOL
: rVal
= V_BOOL(right
); break;
2591 case VT_EMPTY
: rVal
= 0; break;
2592 default: rOk
= FALSE
;
2598 } else if (lVal
> rVal
) {
2606 if ((V_VT(left
)&VT_TYPEMASK
) == VT_DATE
&&
2607 (V_VT(right
)&VT_TYPEMASK
) == VT_DATE
) {
2609 if (floor(V_DATE(left
)) == floor(V_DATE(right
))) {
2610 /* Due to floating point rounding errors, calculate varDate in whole numbers) */
2611 double wholePart
= 0.0;
2615 /* Get the fraction * 24*60*60 to make it into whole seconds */
2616 wholePart
= (double) floor( V_DATE(left
) );
2617 if (wholePart
== 0) wholePart
= 1;
2618 leftR
= floor(fmod( V_DATE(left
), wholePart
) * (24*60*60));
2620 wholePart
= (double) floor( V_DATE(right
) );
2621 if (wholePart
== 0) wholePart
= 1;
2622 rightR
= floor(fmod( V_DATE(right
), wholePart
) * (24*60*60));
2624 if (leftR
< rightR
) {
2626 } else if (leftR
> rightR
) {
2632 } else if (V_DATE(left
) < V_DATE(right
)) {
2634 } else if (V_DATE(left
) > V_DATE(right
)) {
2638 FIXME("VarCmp partial implementation, doesn't support vt 0x%x / 0x%x\n",V_VT(left
), V_VT(right
));
2642 /**********************************************************************
2643 * VarAnd [OLEAUT32.142]
2645 * Computes the logical AND of two variants.
2648 * left [I] First variant
2649 * right [I] Second variant
2650 * result [O] Result variant
2654 * Failure: An HRESULT error code indicating the error.
2656 HRESULT WINAPI
VarAnd(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
2658 HRESULT rc
= E_FAIL
;
2660 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
),
2661 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
), result
);
2663 if ((V_VT(left
)&VT_TYPEMASK
) == VT_BOOL
&&
2664 (V_VT(right
)&VT_TYPEMASK
) == VT_BOOL
) {
2666 V_VT(result
) = VT_BOOL
;
2667 if (V_BOOL(left
) && V_BOOL(right
)) {
2668 V_BOOL(result
) = VARIANT_TRUE
;
2670 V_BOOL(result
) = VARIANT_FALSE
;
2681 int resT
= 0; /* Testing has shown I2 & I2 == I2, all else
2682 becomes I4, even unsigned ints (incl. UI2) */
2685 switch (V_VT(left
)&VT_TYPEMASK
) {
2686 case VT_I1
: lVal
= V_I1(left
); resT
=VT_I4
; break;
2687 case VT_I2
: lVal
= V_I2(left
); resT
=VT_I2
; break;
2689 case VT_INT
: lVal
= V_I4(left
); resT
=VT_I4
; break;
2690 case VT_UI1
: lVal
= V_UI1(left
); resT
=VT_I4
; break;
2691 case VT_UI2
: lVal
= V_UI2(left
); resT
=VT_I4
; break;
2693 case VT_UINT
: lVal
= V_UI4(left
); resT
=VT_I4
; break;
2694 case VT_BOOL
: rVal
= V_BOOL(left
); resT
=VT_I4
; break;
2695 default: lOk
= FALSE
;
2699 switch (V_VT(right
)&VT_TYPEMASK
) {
2700 case VT_I1
: rVal
= V_I1(right
); resT
=VT_I4
; break;
2701 case VT_I2
: rVal
= V_I2(right
); resT
=max(VT_I2
, resT
); break;
2703 case VT_INT
: rVal
= V_I4(right
); resT
=VT_I4
; break;
2704 case VT_UI1
: rVal
= V_UI1(right
); resT
=VT_I4
; break;
2705 case VT_UI2
: rVal
= V_UI2(right
); resT
=VT_I4
; break;
2707 case VT_UINT
: rVal
= V_UI4(right
); resT
=VT_I4
; break;
2708 case VT_BOOL
: rVal
= V_BOOL(right
); resT
=VT_I4
; break;
2709 default: rOk
= FALSE
;
2713 res
= (lVal
& rVal
);
2714 V_VT(result
) = resT
;
2716 case VT_I2
: V_I2(result
) = res
; break;
2717 case VT_I4
: V_I4(result
) = res
; break;
2719 FIXME("Unexpected result variant type %x\n", resT
);
2725 FIXME("VarAnd stub\n");
2729 TRACE("returning 0x%8lx (%s%s),%ld\n", rc
, debugstr_VT(result
),
2730 debugstr_VF(result
), V_VT(result
) == VT_I4
? V_I4(result
) : V_I2(result
));
2734 /**********************************************************************
2735 * VarAdd [OLEAUT32.141]
2740 * left [I] First variant
2741 * right [I] Second variant
2742 * result [O] Result variant
2746 * Failure: An HRESULT error code indicating the error.
2749 * Native VarAdd up to and including WinXP dosn't like as input variants
2750 * I1, UI2, UI4, UI8, INT and UINT.
2752 * Native VarAdd dosn't check for NULL in/out pointers and crashes. We do the
2756 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
2759 HRESULT WINAPI
VarAdd(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
2762 VARTYPE lvt
, rvt
, resvt
, tvt
;
2766 /* Variant priority for coercion. Sorted from lowest to highest.
2767 VT_ERROR shows an invalid input variant type. */
2768 enum coerceprio
{ vt_EMPTY
, vt_UI1
, vt_I2
, vt_I4
, vt_I8
, vt_BSTR
,vt_R4
,
2769 vt_R8
, vt_CY
, vt_DATE
, vt_DECIMAL
, vt_DISPATCH
, vt_NULL
,
2771 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
2772 VARTYPE prio2vt
[] = { VT_EMPTY
, VT_UI1
, VT_I2
, VT_I4
, VT_I8
, VT_BSTR
, VT_R4
,
2773 VT_R8
, VT_CY
, VT_DATE
, VT_DECIMAL
, VT_DISPATCH
,
2774 VT_NULL
, VT_ERROR
};
2776 /* Mapping for coercion from input variant to priority of result variant. */
2777 static VARTYPE coerce
[] = {
2778 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
2779 vt_EMPTY
, vt_NULL
, vt_I2
, vt_I4
, vt_R4
,
2780 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
2781 vt_R8
, vt_CY
, vt_DATE
, vt_BSTR
, vt_DISPATCH
,
2782 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
2783 vt_ERROR
, vt_I2
, vt_ERROR
, vt_ERROR
, vt_DECIMAL
,
2784 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
2785 vt_ERROR
, vt_ERROR
, vt_UI1
, vt_ERROR
, vt_ERROR
, vt_I8
2788 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
),
2789 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
),
2795 lvt
= V_VT(left
)&VT_TYPEMASK
;
2796 rvt
= V_VT(right
)&VT_TYPEMASK
;
2798 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
2799 Same for any input variant type > VT_I8 */
2800 if (V_VT(left
) & ~VT_TYPEMASK
|| V_VT(right
) & ~VT_TYPEMASK
||
2801 lvt
> VT_I8
|| rvt
> VT_I8
) {
2802 hres
= DISP_E_BADVARTYPE
;
2806 /* Determine the variant type to coerce to. */
2807 if (coerce
[lvt
] > coerce
[rvt
]) {
2808 resvt
= prio2vt
[coerce
[lvt
]];
2809 tvt
= prio2vt
[coerce
[rvt
]];
2811 resvt
= prio2vt
[coerce
[rvt
]];
2812 tvt
= prio2vt
[coerce
[lvt
]];
2815 /* Special cases where the result variant type is defined by both
2816 input variants and not only that with the highest priority */
2817 if (resvt
== VT_BSTR
) {
2818 if (tvt
== VT_EMPTY
|| tvt
== VT_BSTR
)
2823 if (resvt
== VT_R4
&& (tvt
== VT_BSTR
|| tvt
== VT_I8
|| tvt
== VT_I4
))
2826 /* For overflow detection use the biggest compatible type for the
2830 hres
= DISP_E_BADVARTYPE
;
2834 V_VT(result
) = VT_NULL
;
2837 FIXME("cannot handle variant type VT_DISPATCH\n");
2838 hres
= DISP_E_TYPEMISMATCH
;
2857 /* Now coerce the variants */
2858 hres
= VariantChangeType(&lv
, left
, 0, tvt
);
2861 hres
= VariantChangeType(&rv
, right
, 0, tvt
);
2868 V_VT(result
) = resvt
;
2871 hres
= VarDecAdd(&V_DECIMAL(&lv
), &V_DECIMAL(&rv
),
2872 &V_DECIMAL(result
));
2875 hres
= VarCyAdd(V_CY(&lv
), V_CY(&rv
), &V_CY(result
));
2878 /* We do not add those, we concatenate them. */
2879 hres
= VarBstrCat(V_BSTR(&lv
), V_BSTR(&rv
), &V_BSTR(result
));
2882 /* Overflow detection */
2883 r8res
= (double)V_I8(&lv
) + (double)V_I8(&rv
);
2884 if (r8res
> (double)I8_MAX
|| r8res
< (double)I8_MIN
) {
2885 V_VT(result
) = VT_R8
;
2886 V_R8(result
) = r8res
;
2889 V_I8(&tv
) = V_I8(&lv
) + V_I8(&rv
);
2892 /* FIXME: overflow detection */
2893 V_R8(&tv
) = V_R8(&lv
) + V_R8(&rv
);
2896 ERR("We shouldn't get here! tvt = %d!\n", tvt
);
2900 if ((hres
= VariantChangeType(result
, &tv
, 0, resvt
)) != S_OK
) {
2901 /* Overflow! Change to the vartype with the next higher priority.
2902 With one exception: I4 ==> R8 even if it would fit in I8 */
2906 resvt
= prio2vt
[coerce
[resvt
] + 1];
2907 hres
= VariantChangeType(result
, &tv
, 0, resvt
);
2910 hres
= VariantCopy(result
, &tv
);
2914 V_VT(result
) = VT_EMPTY
;
2915 V_I4(result
) = 0; /* No V_EMPTY */
2920 TRACE("returning 0x%8lx (variant type %s)\n", hres
, debugstr_VT(result
));
2924 /**********************************************************************
2925 * VarMul [OLEAUT32.156]
2927 * Multiply two variants.
2930 * left [I] First variant
2931 * right [I] Second variant
2932 * result [O] Result variant
2936 * Failure: An HRESULT error code indicating the error.
2939 * Native VarMul up to and including WinXP dosn't like as input variants
2940 * I1, UI2, UI4, UI8, INT and UINT. But it can multiply apples with oranges.
2942 * Native VarMul dosn't check for NULL in/out pointers and crashes. We do the
2946 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
2949 HRESULT WINAPI
VarMul(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
2952 VARTYPE lvt
, rvt
, resvt
, tvt
;
2956 /* Variant priority for coercion. Sorted from lowest to highest.
2957 VT_ERROR shows an invalid input variant type. */
2958 enum coerceprio
{ vt_UI1
= 0, vt_I2
, vt_I4
, vt_I8
, vt_CY
, vt_R4
, vt_R8
,
2959 vt_DECIMAL
, vt_NULL
, vt_ERROR
};
2960 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
2961 VARTYPE prio2vt
[] = { VT_UI1
, VT_I2
, VT_I4
, VT_I8
, VT_CY
, VT_R4
, VT_R8
,
2962 VT_DECIMAL
, VT_NULL
, VT_ERROR
};
2964 /* Mapping for coercion from input variant to priority of result variant. */
2965 static VARTYPE coerce
[] = {
2966 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
2967 vt_UI1
, vt_NULL
, vt_I2
, vt_I4
, vt_R4
,
2968 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
2969 vt_R8
, vt_CY
, vt_R8
, vt_R8
, vt_ERROR
,
2970 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
2971 vt_ERROR
, vt_I2
, vt_ERROR
, vt_ERROR
, vt_DECIMAL
,
2972 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
2973 vt_ERROR
, vt_ERROR
, vt_UI1
, vt_ERROR
, vt_ERROR
, vt_I8
2976 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
),
2977 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
),
2983 lvt
= V_VT(left
)&VT_TYPEMASK
;
2984 rvt
= V_VT(right
)&VT_TYPEMASK
;
2986 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
2987 Same for any input variant type > VT_I8 */
2988 if (V_VT(left
) & ~VT_TYPEMASK
|| V_VT(right
) & ~VT_TYPEMASK
||
2989 lvt
> VT_I8
|| rvt
> VT_I8
) {
2990 hres
= DISP_E_BADVARTYPE
;
2994 /* Determine the variant type to coerce to. */
2995 if (coerce
[lvt
] > coerce
[rvt
]) {
2996 resvt
= prio2vt
[coerce
[lvt
]];
2997 tvt
= prio2vt
[coerce
[rvt
]];
2999 resvt
= prio2vt
[coerce
[rvt
]];
3000 tvt
= prio2vt
[coerce
[lvt
]];
3003 /* Special cases where the result variant type is defined by both
3004 input variants and not only that with the highest priority */
3005 if (resvt
== VT_R4
&& (tvt
== VT_CY
|| tvt
== VT_I8
|| tvt
== VT_I4
))
3007 if (lvt
== VT_EMPTY
&& rvt
== VT_EMPTY
)
3010 /* For overflow detection use the biggest compatible type for the
3014 hres
= DISP_E_BADVARTYPE
;
3018 V_VT(result
) = VT_NULL
;
3033 /* Now coerce the variants */
3034 hres
= VariantChangeType(&lv
, left
, 0, tvt
);
3037 hres
= VariantChangeType(&rv
, right
, 0, tvt
);
3044 V_VT(result
) = resvt
;
3047 hres
= VarDecMul(&V_DECIMAL(&lv
), &V_DECIMAL(&rv
),
3048 &V_DECIMAL(result
));
3051 hres
= VarCyMul(V_CY(&lv
), V_CY(&rv
), &V_CY(result
));
3054 /* Overflow detection */
3055 r8res
= (double)V_I8(&lv
) * (double)V_I8(&rv
);
3056 if (r8res
> (double)I8_MAX
|| r8res
< (double)I8_MIN
) {
3057 V_VT(result
) = VT_R8
;
3058 V_R8(result
) = r8res
;
3061 V_I8(&tv
) = V_I8(&lv
) * V_I8(&rv
);
3064 /* FIXME: overflow detection */
3065 V_R8(&tv
) = V_R8(&lv
) * V_R8(&rv
);
3068 ERR("We shouldn't get here! tvt = %d!\n", tvt
);
3072 while ((hres
= VariantChangeType(result
, &tv
, 0, resvt
)) != S_OK
) {
3073 /* Overflow! Change to the vartype with the next higher priority.
3074 With one exception: I4 ==> R8 even if it would fit in I8 */
3078 resvt
= prio2vt
[coerce
[resvt
] + 1];
3081 hres
= VariantCopy(result
, &tv
);
3085 V_VT(result
) = VT_EMPTY
;
3086 V_I4(result
) = 0; /* No V_EMPTY */
3091 TRACE("returning 0x%8lx (variant type %s)\n", hres
, debugstr_VT(result
));
3095 /**********************************************************************
3096 * VarDiv [OLEAUT32.143]
3098 * Divides one variant with another.
3101 * left [I] First variant
3102 * right [I] Second variant
3103 * result [O] Result variant
3107 * Failure: An HRESULT error code indicating the error.
3109 HRESULT WINAPI
VarDiv(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3111 HRESULT rc
= E_FAIL
;
3112 VARTYPE lvt
,rvt
,resvt
;
3116 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
),
3117 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
), result
);
3119 VariantInit(&lv
);VariantInit(&rv
);
3120 lvt
= V_VT(left
)&VT_TYPEMASK
;
3121 rvt
= V_VT(right
)&VT_TYPEMASK
;
3122 found
= FALSE
;resvt
= VT_VOID
;
3123 if (((1<<lvt
) | (1<<rvt
)) & (VTBIT_R4
|VTBIT_R8
)) {
3127 if (!found
&& (((1<<lvt
) | (1<<rvt
)) & (VTBIT_DECIMAL
))) {
3131 if (!found
&& (((1<<lvt
) | (1<<rvt
)) & (VTBIT_I1
|VTBIT_I2
|VTBIT_UI1
|VTBIT_UI2
|VTBIT_I4
|VTBIT_UI4
|(1<<VT_INT
)|(1<<VT_UINT
)))) {
3136 FIXME("can't expand vt %d vs %d to a target type.\n",lvt
,rvt
);
3139 rc
= VariantChangeType(&lv
, left
, 0, resvt
);
3141 FIXME("Could not convert 0x%x to %d?\n",V_VT(left
),resvt
);
3144 rc
= VariantChangeType(&rv
, right
, 0, resvt
);
3146 FIXME("Could not convert 0x%x to %d?\n",V_VT(right
),resvt
);
3151 if (V_R8(&rv
) == 0) return DISP_E_DIVBYZERO
;
3152 V_VT(result
) = resvt
;
3153 V_R8(result
) = V_R8(&lv
) / V_R8(&rv
);
3157 rc
= VarDecDiv(&(V_DECIMAL(&lv
)), &(V_DECIMAL(&rv
)), &(V_DECIMAL(result
)));
3158 V_VT(result
) = resvt
;
3161 if (V_I4(&rv
) == 0) return DISP_E_DIVBYZERO
;
3162 V_VT(result
) = resvt
;
3163 V_I4(result
) = V_I4(&lv
) / V_I4(&rv
);
3167 TRACE("returning 0x%8lx (%s%s),%g\n", rc
, debugstr_VT(result
),
3168 debugstr_VF(result
), V_VT(result
) == VT_R8
? V_R8(result
) : (double)V_I4(result
));
3172 /**********************************************************************
3173 * VarSub [OLEAUT32.159]
3175 * Subtract two variants.
3178 * left [I] First variant
3179 * right [I] Second variant
3180 * result [O] Result variant
3184 * Failure: An HRESULT error code indicating the error.
3186 HRESULT WINAPI
VarSub(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3188 HRESULT rc
= E_FAIL
;
3189 VARTYPE lvt
,rvt
,resvt
;
3193 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
),
3194 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
), result
);
3196 VariantInit(&lv
);VariantInit(&rv
);
3197 lvt
= V_VT(left
)&VT_TYPEMASK
;
3198 rvt
= V_VT(right
)&VT_TYPEMASK
;
3199 found
= FALSE
;resvt
= VT_VOID
;
3200 if (((1<<lvt
) | (1<<rvt
)) & ((1<<VT_DATE
)|(1<<VT_R4
)|(1<<VT_R8
))) {
3204 if (!found
&& (((1<<lvt
) | (1<<rvt
)) & (VTBIT_DECIMAL
))) {
3208 if (!found
&& (((1<<lvt
) | (1<<rvt
)) & (VTBIT_I1
|VTBIT_I2
|VTBIT_UI1
|VTBIT_UI2
|VTBIT_I4
|VTBIT_UI4
|(1<<VT_INT
)|(1<<VT_UINT
)))) {
3213 FIXME("can't expand vt %d vs %d to a target type.\n",lvt
,rvt
);
3216 rc
= VariantChangeType(&lv
, left
, 0, resvt
);
3218 FIXME("Could not convert 0x%x to %d?\n",V_VT(left
),resvt
);
3221 rc
= VariantChangeType(&rv
, right
, 0, resvt
);
3223 FIXME("Could not convert 0x%x to %d?\n",V_VT(right
),resvt
);
3228 V_VT(result
) = resvt
;
3229 V_R8(result
) = V_R8(&lv
) - V_R8(&rv
);
3233 rc
= VarDecSub(&(V_DECIMAL(&lv
)), &(V_DECIMAL(&rv
)), &(V_DECIMAL(result
)));
3234 V_VT(result
) = resvt
;
3237 V_VT(result
) = resvt
;
3238 V_I4(result
) = V_I4(&lv
) - V_I4(&rv
);
3242 TRACE("returning 0x%8lx (%s%s),%g\n", rc
, debugstr_VT(result
),
3243 debugstr_VF(result
), V_VT(result
) == VT_R8
? V_R8(result
) : (double)V_I4(result
));
3247 /**********************************************************************
3248 * VarOr [OLEAUT32.157]
3250 * Perform a logical or (OR) operation on two variants.
3253 * pVarLeft [I] First variant
3254 * pVarRight [I] Variant to OR with pVarLeft
3255 * pVarOut [O] Destination for OR result
3258 * Success: S_OK. pVarOut contains the result of the operation with its type
3259 * taken from the table listed under VarXor().
3260 * Failure: An HRESULT error code indicating the error.
3263 * See the Notes section of VarXor() for further information.
3265 HRESULT WINAPI
VarOr(LPVARIANT pVarLeft
, LPVARIANT pVarRight
, LPVARIANT pVarOut
)
3268 VARIANT varLeft
, varRight
, varStr
;
3271 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft
, debugstr_VT(pVarLeft
),
3272 debugstr_VF(pVarLeft
), pVarRight
, debugstr_VT(pVarRight
),
3273 debugstr_VF(pVarRight
), pVarOut
);
3275 if (V_EXTRA_TYPE(pVarLeft
) || V_EXTRA_TYPE(pVarRight
) ||
3276 V_VT(pVarLeft
) == VT_UNKNOWN
|| V_VT(pVarRight
) == VT_UNKNOWN
||
3277 V_VT(pVarLeft
) == VT_DISPATCH
|| V_VT(pVarRight
) == VT_DISPATCH
||
3278 V_VT(pVarLeft
) == VT_RECORD
|| V_VT(pVarRight
) == VT_RECORD
)
3279 return DISP_E_BADVARTYPE
;
3281 V_VT(&varLeft
) = V_VT(&varRight
) = V_VT(&varStr
) = VT_EMPTY
;
3283 if (V_VT(pVarLeft
) == VT_NULL
|| V_VT(pVarRight
) == VT_NULL
)
3285 /* NULL OR Zero is NULL, NULL OR value is value */
3286 if (V_VT(pVarLeft
) == VT_NULL
)
3287 pVarLeft
= pVarRight
; /* point to the non-NULL var */
3289 V_VT(pVarOut
) = VT_NULL
;
3292 switch (V_VT(pVarLeft
))
3294 case VT_DATE
: case VT_R8
:
3299 if (V_BOOL(pVarLeft
))
3300 *pVarOut
= *pVarLeft
;
3302 case VT_I2
: case VT_UI2
:
3311 if (V_UI1(pVarLeft
))
3312 *pVarOut
= *pVarLeft
;
3318 case VT_I4
: case VT_UI4
: case VT_INT
: case VT_UINT
:
3323 if (V_CY(pVarLeft
).int64
)
3326 case VT_I8
: case VT_UI8
:
3331 if (DEC_HI32(&V_DECIMAL(pVarLeft
)) || DEC_LO64(&V_DECIMAL(pVarLeft
)))
3338 if (!V_BSTR(pVarLeft
))
3339 return DISP_E_BADVARTYPE
;
3341 hRet
= VarBoolFromStr(V_BSTR(pVarLeft
), LOCALE_USER_DEFAULT
, VAR_LOCALBOOL
, &b
);
3342 if (SUCCEEDED(hRet
) && b
)
3344 V_VT(pVarOut
) = VT_BOOL
;
3345 V_BOOL(pVarOut
) = b
;
3349 case VT_NULL
: case VT_EMPTY
:
3350 V_VT(pVarOut
) = VT_NULL
;
3353 return DISP_E_BADVARTYPE
;
3357 if (V_VT(pVarLeft
) == VT_EMPTY
|| V_VT(pVarRight
) == VT_EMPTY
)
3359 if (V_VT(pVarLeft
) == VT_EMPTY
)
3360 pVarLeft
= pVarRight
; /* point to the non-EMPTY var */
3363 /* Since one argument is empty (0), OR'ing it with the other simply
3364 * gives the others value (as 0|x => x). So just convert the other
3365 * argument to the required result type.
3367 switch (V_VT(pVarLeft
))
3370 if (!V_BSTR(pVarLeft
))
3371 return DISP_E_BADVARTYPE
;
3373 hRet
= VariantCopy(&varStr
, pVarLeft
);
3377 hRet
= VariantChangeType(pVarLeft
, pVarLeft
, 0, VT_BOOL
);
3380 /* Fall Through ... */
3381 case VT_EMPTY
: case VT_UI1
: case VT_BOOL
: case VT_I2
:
3382 V_VT(pVarOut
) = VT_I2
;
3384 case VT_DATE
: case VT_CY
: case VT_DECIMAL
: case VT_R4
: case VT_R8
:
3385 case VT_I1
: case VT_UI2
: case VT_I4
: case VT_UI4
:
3386 case VT_INT
: case VT_UINT
: case VT_UI8
:
3387 V_VT(pVarOut
) = VT_I4
;
3390 V_VT(pVarOut
) = VT_I8
;
3393 return DISP_E_BADVARTYPE
;
3395 hRet
= VariantCopy(&varLeft
, pVarLeft
);
3398 pVarLeft
= &varLeft
;
3399 hRet
= VariantChangeType(pVarOut
, pVarLeft
, 0, V_VT(pVarOut
));
3403 if (V_VT(pVarLeft
) == VT_BOOL
&& V_VT(pVarRight
) == VT_BOOL
)
3405 V_VT(pVarOut
) = VT_BOOL
;
3406 V_BOOL(pVarOut
) = V_BOOL(pVarLeft
) | V_BOOL(pVarRight
);
3410 if (V_VT(pVarLeft
) == VT_UI1
&& V_VT(pVarRight
) == VT_UI1
)
3412 V_VT(pVarOut
) = VT_UI1
;
3413 V_UI1(pVarOut
) = V_UI1(pVarLeft
) | V_UI1(pVarRight
);
3417 if (V_VT(pVarLeft
) == VT_BSTR
)
3419 hRet
= VariantCopy(&varStr
, pVarLeft
);
3423 hRet
= VariantChangeType(pVarLeft
, pVarLeft
, 0, VT_BOOL
);
3428 if (V_VT(pVarLeft
) == VT_BOOL
&&
3429 (V_VT(pVarRight
) == VT_BOOL
|| V_VT(pVarRight
) == VT_BSTR
))
3433 else if ((V_VT(pVarLeft
) == VT_BOOL
|| V_VT(pVarLeft
) == VT_UI1
||
3434 V_VT(pVarLeft
) == VT_I2
|| V_VT(pVarLeft
) == VT_BSTR
) &&
3435 (V_VT(pVarRight
) == VT_BOOL
|| V_VT(pVarRight
) == VT_UI1
||
3436 V_VT(pVarRight
) == VT_I2
|| V_VT(pVarRight
) == VT_BSTR
))
3440 else if (V_VT(pVarLeft
) == VT_I8
|| V_VT(pVarRight
) == VT_I8
)
3442 if (V_VT(pVarLeft
) == VT_INT
|| V_VT(pVarRight
) == VT_INT
)
3443 return DISP_E_TYPEMISMATCH
;
3447 hRet
= VariantCopy(&varLeft
, pVarLeft
);
3451 hRet
= VariantCopy(&varRight
, pVarRight
);
3455 if (vt
== VT_I4
&& V_VT(&varLeft
) == VT_UI4
)
3456 V_VT(&varLeft
) = VT_I4
; /* Don't overflow */
3461 if (V_VT(&varLeft
) == VT_BSTR
&&
3462 FAILED(VarR8FromStr(V_BSTR(&varLeft
), LOCALE_USER_DEFAULT
, 0, &d
)))
3463 hRet
= VariantChangeType(&varLeft
, &varLeft
, VARIANT_LOCALBOOL
, VT_BOOL
);
3464 if (SUCCEEDED(hRet
) && V_VT(&varLeft
) != vt
)
3465 hRet
= VariantChangeType(&varLeft
, &varLeft
, 0, vt
);
3470 if (vt
== VT_I4
&& V_VT(&varRight
) == VT_UI4
)
3471 V_VT(&varRight
) = VT_I4
; /* Don't overflow */
3476 if (V_VT(&varRight
) == VT_BSTR
&&
3477 FAILED(VarR8FromStr(V_BSTR(&varRight
), LOCALE_USER_DEFAULT
, 0, &d
)))
3478 hRet
= VariantChangeType(&varRight
, &varRight
, VARIANT_LOCALBOOL
, VT_BOOL
);
3479 if (SUCCEEDED(hRet
) && V_VT(&varRight
) != vt
)
3480 hRet
= VariantChangeType(&varRight
, &varRight
, 0, vt
);
3488 V_I8(pVarOut
) = V_I8(&varLeft
) | V_I8(&varRight
);
3490 else if (vt
== VT_I4
)
3492 V_I4(pVarOut
) = V_I4(&varLeft
) | V_I4(&varRight
);
3496 V_I2(pVarOut
) = V_I2(&varLeft
) | V_I2(&varRight
);
3500 VariantClear(&varStr
);
3501 VariantClear(&varLeft
);
3502 VariantClear(&varRight
);
3506 /**********************************************************************
3507 * VarAbs [OLEAUT32.168]
3509 * Convert a variant to its absolute value.
3512 * pVarIn [I] Source variant
3513 * pVarOut [O] Destination for converted value
3516 * Success: S_OK. pVarOut contains the absolute value of pVarIn.
3517 * Failure: An HRESULT error code indicating the error.
3520 * - This function does not process by-reference variants.
3521 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3522 * according to the following table:
3523 *| Input Type Output Type
3524 *| ---------- -----------
3527 *| (All others) Unchanged
3529 HRESULT WINAPI
VarAbs(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
3532 HRESULT hRet
= S_OK
;
3534 TRACE("(%p->(%s%s),%p)\n", pVarIn
, debugstr_VT(pVarIn
),
3535 debugstr_VF(pVarIn
), pVarOut
);
3537 if (V_ISARRAY(pVarIn
) || V_VT(pVarIn
) == VT_UNKNOWN
||
3538 V_VT(pVarIn
) == VT_DISPATCH
|| V_VT(pVarIn
) == VT_RECORD
||
3539 V_VT(pVarIn
) == VT_ERROR
)
3540 return DISP_E_TYPEMISMATCH
;
3542 *pVarOut
= *pVarIn
; /* Shallow copy the value, and invert it if needed */
3544 #define ABS_CASE(typ,min) \
3545 case VT_##typ: if (V_##typ(pVarIn) == min) hRet = DISP_E_OVERFLOW; \
3546 else if (V_##typ(pVarIn) < 0) V_##typ(pVarOut) = -V_##typ(pVarIn); \
3549 switch (V_VT(pVarIn
))
3551 ABS_CASE(I1
,I1_MIN
);
3553 V_VT(pVarOut
) = VT_I2
;
3554 /* BOOL->I2, Fall through ... */
3555 ABS_CASE(I2
,I2_MIN
);
3557 ABS_CASE(I4
,I4_MIN
);
3558 ABS_CASE(I8
,I8_MIN
);
3559 ABS_CASE(R4
,R4_MIN
);
3561 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(&varIn
));
3564 V_VT(pVarOut
) = VT_R8
;
3566 /* Fall through ... */
3568 ABS_CASE(R8
,R8_MIN
);
3570 hRet
= VarCyAbs(V_CY(pVarIn
), & V_CY(pVarOut
));
3573 DEC_SIGN(&V_DECIMAL(pVarOut
)) &= ~DECIMAL_NEG
;
3583 V_VT(pVarOut
) = VT_I2
;
3588 hRet
= DISP_E_BADVARTYPE
;
3594 /**********************************************************************
3595 * VarFix [OLEAUT32.169]
3597 * Truncate a variants value to a whole number.
3600 * pVarIn [I] Source variant
3601 * pVarOut [O] Destination for converted value
3604 * Success: S_OK. pVarOut contains the converted value.
3605 * Failure: An HRESULT error code indicating the error.
3608 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3609 * according to the following table:
3610 *| Input Type Output Type
3611 *| ---------- -----------
3615 *| All Others Unchanged
3616 * - The difference between this function and VarInt() is that VarInt() rounds
3617 * negative numbers away from 0, while this function rounds them towards zero.
3619 HRESULT WINAPI
VarFix(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
3621 HRESULT hRet
= S_OK
;
3623 TRACE("(%p->(%s%s),%p)\n", pVarIn
, debugstr_VT(pVarIn
),
3624 debugstr_VF(pVarIn
), pVarOut
);
3626 V_VT(pVarOut
) = V_VT(pVarIn
);
3628 switch (V_VT(pVarIn
))
3631 V_UI1(pVarOut
) = V_UI1(pVarIn
);
3634 V_VT(pVarOut
) = VT_I2
;
3637 V_I2(pVarOut
) = V_I2(pVarIn
);
3640 V_I4(pVarOut
) = V_I4(pVarIn
);
3643 V_I8(pVarOut
) = V_I8(pVarIn
);
3646 if (V_R4(pVarIn
) < 0.0f
)
3647 V_R4(pVarOut
) = (float)ceil(V_R4(pVarIn
));
3649 V_R4(pVarOut
) = (float)floor(V_R4(pVarIn
));
3652 V_VT(pVarOut
) = VT_R8
;
3653 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(pVarOut
));
3658 if (V_R8(pVarIn
) < 0.0)
3659 V_R8(pVarOut
) = ceil(V_R8(pVarIn
));
3661 V_R8(pVarOut
) = floor(V_R8(pVarIn
));
3664 hRet
= VarCyFix(V_CY(pVarIn
), &V_CY(pVarOut
));
3667 hRet
= VarDecFix(&V_DECIMAL(pVarIn
), &V_DECIMAL(pVarOut
));
3670 V_VT(pVarOut
) = VT_I2
;
3677 if (V_TYPE(pVarIn
) == VT_CLSID
|| /* VT_CLSID is a special case */
3678 FAILED(VARIANT_ValidateType(V_VT(pVarIn
))))
3679 hRet
= DISP_E_BADVARTYPE
;
3681 hRet
= DISP_E_TYPEMISMATCH
;
3684 V_VT(pVarOut
) = VT_EMPTY
;
3689 /**********************************************************************
3690 * VarInt [OLEAUT32.172]
3692 * Truncate a variants value to a whole number.
3695 * pVarIn [I] Source variant
3696 * pVarOut [O] Destination for converted value
3699 * Success: S_OK. pVarOut contains the converted value.
3700 * Failure: An HRESULT error code indicating the error.
3703 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3704 * according to the following table:
3705 *| Input Type Output Type
3706 *| ---------- -----------
3710 *| All Others Unchanged
3711 * - The difference between this function and VarFix() is that VarFix() rounds
3712 * negative numbers towards 0, while this function rounds them away from zero.
3714 HRESULT WINAPI
VarInt(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
3716 HRESULT hRet
= S_OK
;
3718 TRACE("(%p->(%s%s),%p)\n", pVarIn
, debugstr_VT(pVarIn
),
3719 debugstr_VF(pVarIn
), pVarOut
);
3721 V_VT(pVarOut
) = V_VT(pVarIn
);
3723 switch (V_VT(pVarIn
))
3726 V_R4(pVarOut
) = (float)floor(V_R4(pVarIn
));
3729 V_VT(pVarOut
) = VT_R8
;
3730 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(pVarOut
));
3735 V_R8(pVarOut
) = floor(V_R8(pVarIn
));
3738 hRet
= VarCyInt(V_CY(pVarIn
), &V_CY(pVarOut
));
3741 hRet
= VarDecInt(&V_DECIMAL(pVarIn
), &V_DECIMAL(pVarOut
));
3744 return VarFix(pVarIn
, pVarOut
);
3750 /**********************************************************************
3751 * VarXor [OLEAUT32.167]
3753 * Perform a logical exclusive-or (XOR) operation on two variants.
3756 * pVarLeft [I] First variant
3757 * pVarRight [I] Variant to XOR with pVarLeft
3758 * pVarOut [O] Destination for XOR result
3761 * Success: S_OK. pVarOut contains the result of the operation with its type
3762 * taken from the table below).
3763 * Failure: An HRESULT error code indicating the error.
3766 * - Neither pVarLeft or pVarRight are modified by this function.
3767 * - This function does not process by-reference variants.
3768 * - Input types of VT_BSTR may be numeric strings or boolean text.
3769 * - The type of result stored in pVarOut depends on the types of pVarLeft
3770 * and pVarRight, and will be one of VT_UI1, VT_I2, VT_I4, VT_I8, VT_BOOL,
3771 * or VT_NULL if the function succeeds.
3772 * - Type promotion is inconsistent and as a result certain combinations of
3773 * values will return DISP_E_OVERFLOW even when they could be represented.
3774 * This matches the behaviour of native oleaut32.
3776 HRESULT WINAPI
VarXor(LPVARIANT pVarLeft
, LPVARIANT pVarRight
, LPVARIANT pVarOut
)
3779 VARIANT varLeft
, varRight
;
3783 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft
, debugstr_VT(pVarLeft
),
3784 debugstr_VF(pVarLeft
), pVarRight
, debugstr_VT(pVarRight
),
3785 debugstr_VF(pVarRight
), pVarOut
);
3787 if (V_EXTRA_TYPE(pVarLeft
) || V_EXTRA_TYPE(pVarRight
) ||
3788 V_VT(pVarLeft
) > VT_UINT
|| V_VT(pVarRight
) > VT_UINT
||
3789 V_VT(pVarLeft
) == VT_VARIANT
|| V_VT(pVarRight
) == VT_VARIANT
||
3790 V_VT(pVarLeft
) == VT_UNKNOWN
|| V_VT(pVarRight
) == VT_UNKNOWN
||
3791 V_VT(pVarLeft
) == (VARTYPE
)15 || V_VT(pVarRight
) == (VARTYPE
)15 ||
3792 V_VT(pVarLeft
) == VT_ERROR
|| V_VT(pVarRight
) == VT_ERROR
)
3793 return DISP_E_BADVARTYPE
;
3795 if (V_VT(pVarLeft
) == VT_NULL
|| V_VT(pVarRight
) == VT_NULL
)
3797 /* NULL XOR anything valid is NULL */
3798 V_VT(pVarOut
) = VT_NULL
;
3802 /* Copy our inputs so we don't disturb anything */
3803 V_VT(&varLeft
) = V_VT(&varRight
) = VT_EMPTY
;
3805 hRet
= VariantCopy(&varLeft
, pVarLeft
);
3809 hRet
= VariantCopy(&varRight
, pVarRight
);
3813 /* Try any strings first as numbers, then as VT_BOOL */
3814 if (V_VT(&varLeft
) == VT_BSTR
)
3816 hRet
= VarR8FromStr(V_BSTR(&varLeft
), LOCALE_USER_DEFAULT
, 0, &d
);
3817 hRet
= VariantChangeType(&varLeft
, &varLeft
, VARIANT_LOCALBOOL
,
3818 FAILED(hRet
) ? VT_BOOL
: VT_I4
);
3823 if (V_VT(&varRight
) == VT_BSTR
)
3825 hRet
= VarR8FromStr(V_BSTR(&varRight
), LOCALE_USER_DEFAULT
, 0, &d
);
3826 hRet
= VariantChangeType(&varRight
, &varRight
, VARIANT_LOCALBOOL
,
3827 FAILED(hRet
) ? VT_BOOL
: VT_I4
);
3832 /* Determine the result type */
3833 if (V_VT(&varLeft
) == VT_I8
|| V_VT(&varRight
) == VT_I8
)
3835 if (V_VT(pVarLeft
) == VT_INT
|| V_VT(pVarRight
) == VT_INT
)
3836 return DISP_E_TYPEMISMATCH
;
3841 switch ((V_VT(&varLeft
) << 16) | V_VT(&varRight
))
3843 case (VT_BOOL
<< 16) | VT_BOOL
:
3846 case (VT_UI1
<< 16) | VT_UI1
:
3849 case (VT_EMPTY
<< 16) | VT_EMPTY
:
3850 case (VT_EMPTY
<< 16) | VT_UI1
:
3851 case (VT_EMPTY
<< 16) | VT_I2
:
3852 case (VT_EMPTY
<< 16) | VT_BOOL
:
3853 case (VT_UI1
<< 16) | VT_EMPTY
:
3854 case (VT_UI1
<< 16) | VT_I2
:
3855 case (VT_UI1
<< 16) | VT_BOOL
:
3856 case (VT_I2
<< 16) | VT_EMPTY
:
3857 case (VT_I2
<< 16) | VT_UI1
:
3858 case (VT_I2
<< 16) | VT_I2
:
3859 case (VT_I2
<< 16) | VT_BOOL
:
3860 case (VT_BOOL
<< 16) | VT_EMPTY
:
3861 case (VT_BOOL
<< 16) | VT_UI1
:
3862 case (VT_BOOL
<< 16) | VT_I2
:
3871 /* VT_UI4 does not overflow */
3874 if (V_VT(&varLeft
) == VT_UI4
)
3875 V_VT(&varLeft
) = VT_I4
;
3876 if (V_VT(&varRight
) == VT_UI4
)
3877 V_VT(&varRight
) = VT_I4
;
3880 /* Convert our input copies to the result type */
3881 if (V_VT(&varLeft
) != vt
)
3882 hRet
= VariantChangeType(&varLeft
, &varLeft
, 0, vt
);
3886 if (V_VT(&varRight
) != vt
)
3887 hRet
= VariantChangeType(&varRight
, &varRight
, 0, vt
);
3893 /* Calculate the result */
3897 V_I8(pVarOut
) = V_I8(&varLeft
) ^ V_I8(&varRight
);
3900 V_I4(pVarOut
) = V_I4(&varLeft
) ^ V_I4(&varRight
);
3904 V_I2(pVarOut
) = V_I2(&varLeft
) ^ V_I2(&varRight
);
3907 V_UI1(pVarOut
) = V_UI1(&varLeft
) ^ V_UI1(&varRight
);
3912 VariantClear(&varLeft
);
3913 VariantClear(&varRight
);
3917 /**********************************************************************
3918 * VarEqv [OLEAUT32.172]
3920 * Determine if two variants contain the same value.
3923 * pVarLeft [I] First variant to compare
3924 * pVarRight [I] Variant to compare to pVarLeft
3925 * pVarOut [O] Destination for comparison result
3928 * Success: S_OK. pVarOut contains the result of the comparison (VARIANT_TRUE
3929 * if equivalent or non-zero otherwise.
3930 * Failure: An HRESULT error code indicating the error.
3933 * - This function simply calls VarXor() on pVarLeft and pVarRight and inverts
3936 HRESULT WINAPI
VarEqv(LPVARIANT pVarLeft
, LPVARIANT pVarRight
, LPVARIANT pVarOut
)
3940 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft
, debugstr_VT(pVarLeft
),
3941 debugstr_VF(pVarLeft
), pVarRight
, debugstr_VT(pVarRight
),
3942 debugstr_VF(pVarRight
), pVarOut
);
3944 hRet
= VarXor(pVarLeft
, pVarRight
, pVarOut
);
3945 if (SUCCEEDED(hRet
))
3947 if (V_VT(pVarOut
) == VT_I8
)
3948 V_I8(pVarOut
) = ~V_I8(pVarOut
);
3950 V_UI4(pVarOut
) = ~V_UI4(pVarOut
);
3955 /**********************************************************************
3956 * VarNeg [OLEAUT32.173]
3958 * Negate the value of a variant.
3961 * pVarIn [I] Source variant
3962 * pVarOut [O] Destination for converted value
3965 * Success: S_OK. pVarOut contains the converted value.
3966 * Failure: An HRESULT error code indicating the error.
3969 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3970 * according to the following table:
3971 *| Input Type Output Type
3972 *| ---------- -----------
3977 *| All Others Unchanged (unless promoted)
3978 * - Where the negated value of a variant does not fit in its base type, the type
3979 * is promoted according to the following table:
3980 *| Input Type Promoted To
3981 *| ---------- -----------
3985 * - The native version of this function returns DISP_E_BADVARTYPE for valid
3986 * variant types that cannot be negated, and returns DISP_E_TYPEMISMATCH
3987 * for types which are not valid. Since this is in contravention of the
3988 * meaning of those error codes and unlikely to be relied on by applications,
3989 * this implementation returns errors consistent with the other high level
3990 * variant math functions.
3992 HRESULT WINAPI
VarNeg(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
3994 HRESULT hRet
= S_OK
;
3996 TRACE("(%p->(%s%s),%p)\n", pVarIn
, debugstr_VT(pVarIn
),
3997 debugstr_VF(pVarIn
), pVarOut
);
3999 V_VT(pVarOut
) = V_VT(pVarIn
);
4001 switch (V_VT(pVarIn
))
4004 V_VT(pVarOut
) = VT_I2
;
4005 V_I2(pVarOut
) = -V_UI1(pVarIn
);
4008 V_VT(pVarOut
) = VT_I2
;
4011 if (V_I2(pVarIn
) == I2_MIN
)
4013 V_VT(pVarOut
) = VT_I4
;
4014 V_I4(pVarOut
) = -(int)V_I2(pVarIn
);
4017 V_I2(pVarOut
) = -V_I2(pVarIn
);
4020 if (V_I4(pVarIn
) == I4_MIN
)
4022 V_VT(pVarOut
) = VT_R8
;
4023 V_R8(pVarOut
) = -(double)V_I4(pVarIn
);
4026 V_I4(pVarOut
) = -V_I4(pVarIn
);
4029 if (V_I8(pVarIn
) == I8_MIN
)
4031 V_VT(pVarOut
) = VT_R8
;
4032 hRet
= VarR8FromI8(V_I8(pVarIn
), &V_R8(pVarOut
));
4033 V_R8(pVarOut
) *= -1.0;
4036 V_I8(pVarOut
) = -V_I8(pVarIn
);
4039 V_R4(pVarOut
) = -V_R4(pVarIn
);
4043 V_R8(pVarOut
) = -V_R8(pVarIn
);
4046 hRet
= VarCyNeg(V_CY(pVarIn
), &V_CY(pVarOut
));
4049 hRet
= VarDecNeg(&V_DECIMAL(pVarIn
), &V_DECIMAL(pVarOut
));
4052 V_VT(pVarOut
) = VT_R8
;
4053 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(pVarOut
));
4054 V_R8(pVarOut
) = -V_R8(pVarOut
);
4057 V_VT(pVarOut
) = VT_I2
;
4064 if (V_TYPE(pVarIn
) == VT_CLSID
|| /* VT_CLSID is a special case */
4065 FAILED(VARIANT_ValidateType(V_VT(pVarIn
))))
4066 hRet
= DISP_E_BADVARTYPE
;
4068 hRet
= DISP_E_TYPEMISMATCH
;
4071 V_VT(pVarOut
) = VT_EMPTY
;
4076 /**********************************************************************
4077 * VarNot [OLEAUT32.174]
4079 * Perform a not operation on a variant.
4082 * pVarIn [I] Source variant
4083 * pVarOut [O] Destination for converted value
4086 * Success: S_OK. pVarOut contains the converted value.
4087 * Failure: An HRESULT error code indicating the error.
4090 * - Strictly speaking, this function performs a bitwise ones complement
4091 * on the variants value (after possibly converting to VT_I4, see below).
4092 * This only behaves like a boolean not operation if the value in
4093 * pVarIn is either VARIANT_TRUE or VARIANT_FALSE and the type is signed.
4094 * - To perform a genuine not operation, convert the variant to a VT_BOOL
4095 * before calling this function.
4096 * - This function does not process by-reference variants.
4097 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4098 * according to the following table:
4099 *| Input Type Output Type
4100 *| ---------- -----------
4107 *| (All others) Unchanged
4109 HRESULT WINAPI
VarNot(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
4112 HRESULT hRet
= S_OK
;
4114 TRACE("(%p->(%s%s),%p)\n", pVarIn
, debugstr_VT(pVarIn
),
4115 debugstr_VF(pVarIn
), pVarOut
);
4117 V_VT(pVarOut
) = V_VT(pVarIn
);
4119 switch (V_VT(pVarIn
))
4122 V_I4(pVarOut
) = ~V_I1(pVarIn
);
4123 V_VT(pVarOut
) = VT_I4
;
4125 case VT_UI1
: V_UI1(pVarOut
) = ~V_UI1(pVarIn
); break;
4127 case VT_I2
: V_I2(pVarOut
) = ~V_I2(pVarIn
); break;
4129 V_I4(pVarOut
) = ~V_UI2(pVarIn
);
4130 V_VT(pVarOut
) = VT_I4
;
4133 hRet
= VarI4FromDec(&V_DECIMAL(pVarIn
), &V_I4(&varIn
));
4137 /* Fall through ... */
4139 V_VT(pVarOut
) = VT_I4
;
4140 /* Fall through ... */
4141 case VT_I4
: V_I4(pVarOut
) = ~V_I4(pVarIn
); break;
4144 V_I4(pVarOut
) = ~V_UI4(pVarIn
);
4145 V_VT(pVarOut
) = VT_I4
;
4147 case VT_I8
: V_I8(pVarOut
) = ~V_I8(pVarIn
); break;
4149 V_I4(pVarOut
) = ~V_UI8(pVarIn
);
4150 V_VT(pVarOut
) = VT_I4
;
4153 hRet
= VarI4FromR4(V_R4(pVarIn
), &V_I4(pVarOut
));
4154 V_I4(pVarOut
) = ~V_I4(pVarOut
);
4155 V_VT(pVarOut
) = VT_I4
;
4158 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(&varIn
));
4162 /* Fall through ... */
4165 hRet
= VarI4FromR8(V_R8(pVarIn
), &V_I4(pVarOut
));
4166 V_I4(pVarOut
) = ~V_I4(pVarOut
);
4167 V_VT(pVarOut
) = VT_I4
;
4170 hRet
= VarI4FromCy(V_CY(pVarIn
), &V_I4(pVarOut
));
4171 V_I4(pVarOut
) = ~V_I4(pVarOut
);
4172 V_VT(pVarOut
) = VT_I4
;
4176 V_VT(pVarOut
) = VT_I2
;
4182 if (V_TYPE(pVarIn
) == VT_CLSID
|| /* VT_CLSID is a special case */
4183 FAILED(VARIANT_ValidateType(V_VT(pVarIn
))))
4184 hRet
= DISP_E_BADVARTYPE
;
4186 hRet
= DISP_E_TYPEMISMATCH
;
4189 V_VT(pVarOut
) = VT_EMPTY
;
4194 /**********************************************************************
4195 * VarRound [OLEAUT32.175]
4197 * Perform a round operation on a variant.
4200 * pVarIn [I] Source variant
4201 * deci [I] Number of decimals to round to
4202 * pVarOut [O] Destination for converted value
4205 * Success: S_OK. pVarOut contains the converted value.
4206 * Failure: An HRESULT error code indicating the error.
4209 * - Floating point values are rounded to the desired number of decimals.
4210 * - Some integer types are just copied to the return variable.
4211 * - Some other integer types are not handled and fail.
4213 HRESULT WINAPI
VarRound(LPVARIANT pVarIn
, int deci
, LPVARIANT pVarOut
)
4216 HRESULT hRet
= S_OK
;
4219 TRACE("(%p->(%s%s),%d)\n", pVarIn
, debugstr_VT(pVarIn
), debugstr_VF(pVarIn
), deci
);
4221 switch (V_VT(pVarIn
))
4223 /* cases that fail on windows */
4228 hRet
= DISP_E_BADVARTYPE
;
4231 /* cases just copying in to out */
4233 V_VT(pVarOut
) = V_VT(pVarIn
);
4234 V_UI1(pVarOut
) = V_UI1(pVarIn
);
4237 V_VT(pVarOut
) = V_VT(pVarIn
);
4238 V_I2(pVarOut
) = V_I2(pVarIn
);
4241 V_VT(pVarOut
) = V_VT(pVarIn
);
4242 V_I4(pVarOut
) = V_I4(pVarIn
);
4245 V_VT(pVarOut
) = V_VT(pVarIn
);
4246 /* value unchanged */
4249 /* cases that change type */
4251 V_VT(pVarOut
) = VT_I2
;
4255 V_VT(pVarOut
) = VT_I2
;
4256 V_I2(pVarOut
) = V_BOOL(pVarIn
);
4259 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(&varIn
));
4264 /* Fall through ... */
4266 /* cases we need to do math */
4268 if (V_R8(pVarIn
)>0) {
4269 V_R8(pVarOut
)=floor(V_R8(pVarIn
)*pow(10, deci
)+0.5)/pow(10, deci
);
4271 V_R8(pVarOut
)=ceil(V_R8(pVarIn
)*pow(10, deci
)-0.5)/pow(10, deci
);
4273 V_VT(pVarOut
) = V_VT(pVarIn
);
4276 if (V_R4(pVarIn
)>0) {
4277 V_R4(pVarOut
)=floor(V_R4(pVarIn
)*pow(10, deci
)+0.5)/pow(10, deci
);
4279 V_R4(pVarOut
)=ceil(V_R4(pVarIn
)*pow(10, deci
)-0.5)/pow(10, deci
);
4281 V_VT(pVarOut
) = V_VT(pVarIn
);
4284 if (V_DATE(pVarIn
)>0) {
4285 V_DATE(pVarOut
)=floor(V_DATE(pVarIn
)*pow(10, deci
)+0.5)/pow(10, deci
);
4287 V_DATE(pVarOut
)=ceil(V_DATE(pVarIn
)*pow(10, deci
)-0.5)/pow(10, deci
);
4289 V_VT(pVarOut
) = V_VT(pVarIn
);
4295 factor
=pow(10, 4-deci
);
4297 if (V_CY(pVarIn
).int64
>0) {
4298 V_CY(pVarOut
).int64
=floor(V_CY(pVarIn
).int64
/factor
)*factor
;
4300 V_CY(pVarOut
).int64
=ceil(V_CY(pVarIn
).int64
/factor
)*factor
;
4302 V_VT(pVarOut
) = V_VT(pVarIn
);
4305 /* cases we don't know yet */
4307 FIXME("unimplemented part, V_VT(pVarIn) == 0x%X, deci == %d\n",
4308 V_VT(pVarIn
) & VT_TYPEMASK
, deci
);
4309 hRet
= DISP_E_BADVARTYPE
;
4313 V_VT(pVarOut
) = VT_EMPTY
;
4315 TRACE("returning 0x%08lx (%s%s),%f\n", hRet
, debugstr_VT(pVarOut
),
4316 debugstr_VF(pVarOut
), (V_VT(pVarOut
) == VT_R4
) ? V_R4(pVarOut
) :
4317 (V_VT(pVarOut
) == VT_R8
) ? V_R8(pVarOut
) : 0);
4322 /**********************************************************************
4323 * VarIdiv [OLEAUT32.153]
4325 * Converts input variants to integers and divides them.
4328 * left [I] Left hand variant
4329 * right [I] Right hand variant
4330 * result [O] Destination for quotient
4333 * Success: S_OK. result contains the quotient.
4334 * Failure: An HRESULT error code indicating the error.
4337 * If either expression is null, null is returned, as per MSDN
4339 HRESULT WINAPI
VarIdiv(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
4347 if ((V_VT(left
) == VT_NULL
) || (V_VT(right
) == VT_NULL
)) {
4348 hr
= VariantChangeType(result
, result
, 0, VT_NULL
);
4350 /* This should never happen */
4351 FIXME("Failed to convert return value to VT_NULL.\n");
4357 hr
= VariantChangeType(&lv
, left
, 0, VT_I4
);
4361 hr
= VariantChangeType(&rv
, right
, 0, VT_I4
);
4366 hr
= VarDiv(&lv
, &rv
, result
);
4371 /**********************************************************************
4372 * VarMod [OLEAUT32.155]
4374 * Perform the modulus operation of the right hand variant on the left
4377 * left [I] Left hand variant
4378 * right [I] Right hand variant
4379 * result [O] Destination for converted value
4382 * Success: S_OK. result contains the remainder.
4383 * Failure: An HRESULT error code indicating the error.
4386 * If an error occurs the type of result will be modified but the value will not be.
4387 * Doesn't support arrays or any special flags yet.
4389 HRESULT WINAPI
VarMod(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
4393 HRESULT rc
= E_FAIL
;
4400 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
),
4401 debugstr_VF(left
), right
, debugstr_VT(right
), debugstr_VF(right
), result
);
4403 /* check for invalid inputs */
4405 switch (V_VT(left
) & VT_TYPEMASK
) {
4426 V_VT(result
) = VT_EMPTY
;
4427 return DISP_E_TYPEMISMATCH
;
4429 V_VT(result
) = VT_EMPTY
;
4430 return DISP_E_OVERFLOW
;
4432 return DISP_E_TYPEMISMATCH
;
4434 V_VT(result
) = VT_EMPTY
;
4435 return DISP_E_TYPEMISMATCH
;
4439 V_VT(result
) = VT_EMPTY
;
4440 return DISP_E_BADVARTYPE
;
4445 switch (V_VT(right
) & VT_TYPEMASK
) {
4451 if((V_VT(left
) == VT_INT
) && (V_VT(right
) == VT_I8
))
4453 V_VT(result
) = VT_EMPTY
;
4454 return DISP_E_TYPEMISMATCH
;
4457 if((V_VT(right
) == VT_INT
) && (V_VT(left
) == VT_I8
))
4459 V_VT(result
) = VT_EMPTY
;
4460 return DISP_E_TYPEMISMATCH
;
4470 if(V_VT(left
) == VT_EMPTY
)
4472 V_VT(result
) = VT_I4
;
4478 if(V_VT(left
) == VT_NULL
)
4480 V_VT(result
) = VT_NULL
;
4486 V_VT(result
) = VT_EMPTY
;
4487 return DISP_E_BADVARTYPE
;
4489 if(V_VT(left
) == VT_VOID
)
4491 V_VT(result
) = VT_EMPTY
;
4492 return DISP_E_BADVARTYPE
;
4493 } else if((V_VT(left
) == VT_NULL
) || (V_VT(left
) == VT_EMPTY
) || (V_VT(left
) == VT_ERROR
) ||
4496 V_VT(result
) = VT_NULL
;
4500 V_VT(result
) = VT_NULL
;
4501 return DISP_E_BADVARTYPE
;
4505 V_VT(result
) = VT_EMPTY
;
4506 return DISP_E_TYPEMISMATCH
;
4508 if(V_VT(left
) == VT_ERROR
)
4510 V_VT(result
) = VT_EMPTY
;
4511 return DISP_E_TYPEMISMATCH
;
4514 V_VT(result
) = VT_EMPTY
;
4515 return DISP_E_OVERFLOW
;
4518 return DISP_E_TYPEMISMATCH
;
4520 if((V_VT(left
) == 15) || ((V_VT(left
) >= 24) && (V_VT(left
) <= 35)) || !lOk
)
4522 V_VT(result
) = VT_EMPTY
;
4523 return DISP_E_BADVARTYPE
;
4526 V_VT(result
) = VT_EMPTY
;
4527 return DISP_E_TYPEMISMATCH
;
4530 V_VT(result
) = VT_EMPTY
;
4531 return DISP_E_BADVARTYPE
;
4534 /* determine the result type */
4535 if((V_VT(left
) == VT_I8
) || (V_VT(right
) == VT_I8
)) resT
= VT_I8
;
4536 else if((V_VT(left
) == VT_UI1
) && (V_VT(right
) == VT_BOOL
)) resT
= VT_I2
;
4537 else if((V_VT(left
) == VT_UI1
) && (V_VT(right
) == VT_UI1
)) resT
= VT_UI1
;
4538 else if((V_VT(left
) == VT_UI1
) && (V_VT(right
) == VT_I2
)) resT
= VT_I2
;
4539 else if((V_VT(left
) == VT_I2
) && (V_VT(right
) == VT_BOOL
)) resT
= VT_I2
;
4540 else if((V_VT(left
) == VT_I2
) && (V_VT(right
) == VT_UI1
)) resT
= VT_I2
;
4541 else if((V_VT(left
) == VT_I2
) && (V_VT(right
) == VT_I2
)) resT
= VT_I2
;
4542 else if((V_VT(left
) == VT_BOOL
) && (V_VT(right
) == VT_BOOL
)) resT
= VT_I2
;
4543 else if((V_VT(left
) == VT_BOOL
) && (V_VT(right
) == VT_UI1
)) resT
= VT_I2
;
4544 else if((V_VT(left
) == VT_BOOL
) && (V_VT(right
) == VT_I2
)) resT
= VT_I2
;
4545 else resT
= VT_I4
; /* most outputs are I4 */
4547 /* convert to I8 for the modulo */
4548 rc
= VariantChangeType(&lv
, left
, 0, VT_I8
);
4551 FIXME("Could not convert left type %d to %d? rc == 0x%lX\n", V_VT(left
), VT_I8
, rc
);
4555 rc
= VariantChangeType(&rv
, right
, 0, VT_I8
);
4558 FIXME("Could not convert right type %d to %d? rc == 0x%lX\n", V_VT(right
), VT_I8
, rc
);
4562 /* if right is zero set VT_EMPTY and return divide by zero */
4565 V_VT(result
) = VT_EMPTY
;
4566 return DISP_E_DIVBYZERO
;
4569 /* perform the modulo operation */
4570 V_VT(result
) = VT_I8
;
4571 V_I8(result
) = V_I8(&lv
) % V_I8(&rv
);
4573 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
));
4575 /* convert left and right to the destination type */
4576 rc
= VariantChangeType(result
, result
, 0, resT
);
4579 FIXME("Could not convert 0x%x to %d?\n", V_VT(result
), resT
);
4586 /**********************************************************************
4587 * VarPow [OLEAUT32.158]
4589 * Computes the power of one variant to another variant.
4592 * left [I] First variant
4593 * right [I] Second variant
4594 * result [O] Result variant
4598 * Failure: An HRESULT error code indicating the error.
4600 HRESULT WINAPI
VarPow(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
4605 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left
, debugstr_VT(left
), debugstr_VF(left
),
4606 right
, debugstr_VT(right
), debugstr_VF(right
), result
);
4608 hr
= VariantChangeType(&dl
,left
,0,VT_R8
);
4609 if (!SUCCEEDED(hr
)) {
4610 ERR("Could not change passed left argument to VT_R8, handle it differently.\n");
4613 hr
= VariantChangeType(&dr
,right
,0,VT_R8
);
4614 if (!SUCCEEDED(hr
)) {
4615 ERR("Could not change passed right argument to VT_R8, handle it differently.\n");
4618 V_VT(result
) = VT_R8
;
4619 V_R8(result
) = pow(V_R8(&dl
),V_R8(&dr
));