1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <config_features.h>
22 #include <o3tl/float_int_conversion.hxx>
23 #include <vcl/errcode.hxx>
24 #include "sbxconv.hxx"
25 #include <runtime.hxx>
27 double ImpGetDouble( const SbxValues
* p
)
33 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
38 nRes
= p
->nChar
; break;
40 nRes
= p
->nByte
; break;
43 nRes
= p
->nInteger
; break;
46 nRes
= p
->nUShort
; break;
48 nRes
= p
->nLong
; break;
50 nRes
= p
->nULong
; break;
52 nRes
= p
->nSingle
; break;
55 nRes
= p
->nDouble
; break;
57 nRes
= ImpCurrencyToDouble( p
->nInt64
); break;
59 nRes
= static_cast< double >(p
->nInt64
); break;
61 nRes
= ImpSalUInt64ToDouble( p
->uInt64
); break;
63 case SbxBYREF
| SbxDECIMAL
:
65 p
->pDecimal
->getDouble( nRes
);
69 case SbxBYREF
| SbxSTRING
:
75 #if HAVE_FEATURE_SCRIPTING
76 if ( SbiRuntime::isVBAEnabled() )// VBA only behaviour
77 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
84 if( ImpScan( *p
->pOUString
, d
, t
, nullptr, false ) != ERRCODE_NONE
)
87 #if HAVE_FEATURE_SCRIPTING
88 if ( SbiRuntime::isVBAEnabled() )// VBA only behaviour
89 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
98 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
100 nRes
= pVal
->GetDouble();
103 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
); nRes
= 0;
108 case SbxBYREF
| SbxCHAR
:
109 nRes
= *p
->pChar
; break;
110 case SbxBYREF
| SbxBYTE
:
111 nRes
= *p
->pByte
; break;
112 case SbxBYREF
| SbxINTEGER
:
113 case SbxBYREF
| SbxBOOL
:
114 nRes
= *p
->pInteger
; break;
115 case SbxBYREF
| SbxLONG
:
116 nRes
= *p
->pLong
; break;
117 case SbxBYREF
| SbxULONG
:
118 nRes
= *p
->pULong
; break;
119 case SbxBYREF
| SbxERROR
:
120 case SbxBYREF
| SbxUSHORT
:
121 nRes
= *p
->pUShort
; break;
122 case SbxBYREF
| SbxSINGLE
:
123 nRes
= *p
->pSingle
; break;
124 case SbxBYREF
| SbxDATE
:
125 case SbxBYREF
| SbxDOUBLE
:
126 nRes
= *p
->pDouble
; break;
127 case SbxBYREF
| SbxCURRENCY
:
128 nRes
= ImpCurrencyToDouble( *p
->pnInt64
); break;
129 case SbxBYREF
| SbxSALINT64
:
130 nRes
= static_cast< double >(*p
->pnInt64
); break;
131 case SbxBYREF
| SbxSALUINT64
:
132 nRes
= ImpSalUInt64ToDouble( *p
->puInt64
); break;
135 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
); nRes
= 0;
140 void ImpPutDouble( SbxValues
* p
, double n
, bool bCoreString
)
146 // Here are tests necessary
148 aTmp
.pChar
= &p
->nChar
; goto direct
;
150 aTmp
.pByte
= &p
->nByte
; goto direct
;
153 aTmp
.pInteger
= &p
->nInteger
; goto direct
;
155 aTmp
.pLong
= &p
->nLong
; goto direct
;
157 aTmp
.pULong
= &p
->nULong
; goto direct
;
160 aTmp
.pUShort
= &p
->nUShort
; goto direct
;
162 aTmp
.pSingle
= &p
->nSingle
; goto direct
;
164 case SbxBYREF
| SbxDECIMAL
:
166 SbxDecimal
* pDec
= ImpCreateDecimal( p
);
167 if( !pDec
->setDouble( n
) )
168 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
);
172 aTmp
.eType
= SbxDataType( p
->eType
| SbxBYREF
);
173 p
= &aTmp
; goto start
;
178 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXCURR
;
180 else if( n
< SbxMINCURR
)
182 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMINCURR
;
184 p
->nInt64
= ImpDoubleToCurrency( n
);
187 // from here on no longer
189 p
->nInt64
= ImpDoubleToSalInt64( n
); break;
191 p
->uInt64
= ImpDoubleToSalUInt64( n
); break;
194 p
->nDouble
= n
; break;
196 case SbxBYREF
| SbxSTRING
:
200 p
->pOUString
= new OUString
;
201 ImpCvtNum( n
, 14, *p
->pOUString
, bCoreString
);
205 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
207 pVal
->PutDouble( n
);
209 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
);
212 case SbxBYREF
| SbxCHAR
:
213 if( !o3tl::convertsToAtMost(o3tl::roundAway(n
), SbxMAXCHAR
) )
215 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXCHAR
;
217 else if( !o3tl::convertsToAtLeast(o3tl::roundAway(n
), SbxMINCHAR
) )
219 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMINCHAR
;
221 *p
->pChar
= static_cast<sal_Unicode
>(n
); break;
222 case SbxBYREF
| SbxBYTE
:
223 if( !o3tl::convertsToAtMost(o3tl::roundAway(n
), SbxMAXBYTE
) )
225 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXBYTE
;
229 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= 0;
231 *p
->pByte
= static_cast<sal_uInt8
>(n
); break;
232 case SbxBYREF
| SbxINTEGER
:
233 case SbxBYREF
| SbxBOOL
:
234 if( !o3tl::convertsToAtMost(o3tl::roundAway(n
), SbxMAXINT
) )
236 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXINT
;
238 else if( !o3tl::convertsToAtLeast(o3tl::roundAway(n
), SbxMININT
) )
240 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMININT
;
242 *p
->pInteger
= static_cast<sal_Int16
>(n
); break;
243 case SbxBYREF
| SbxERROR
:
244 case SbxBYREF
| SbxUSHORT
:
245 if( !o3tl::convertsToAtMost(o3tl::roundAway(n
), SbxMAXUINT
) )
247 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXUINT
;
251 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= 0;
253 *p
->pUShort
= static_cast<sal_uInt16
>(n
); break;
254 case SbxBYREF
| SbxLONG
:
255 if( !o3tl::convertsToAtMost(o3tl::roundAway(n
), SbxMAXLNG
) )
257 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXLNG
;
259 else if( !o3tl::convertsToAtLeast(o3tl::roundAway(n
), SbxMINLNG
) )
261 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMINLNG
;
263 *p
->pLong
= static_cast<sal_Int32
>(n
); break;
264 case SbxBYREF
| SbxULONG
:
265 if( !o3tl::convertsToAtMost(o3tl::roundAway(n
), SbxMAXULNG
) )
267 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXULNG
;
271 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= 0;
273 *p
->pULong
= static_cast<sal_uInt32
>(n
); break;
274 case SbxBYREF
| SbxSINGLE
:
277 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXSNG
;
279 else if( n
< SbxMINSNG
)
281 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMINSNG
;
283 else if( n
> 0 && n
< SbxMAXSNG2
)
285 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXSNG2
;
287 else if( n
< 0 && n
> SbxMINSNG2
)
289 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMINSNG2
;
291 *p
->pSingle
= static_cast<float>(n
); break;
292 case SbxBYREF
| SbxSALINT64
:
293 *p
->pnInt64
= ImpDoubleToSalInt64( n
); break;
294 case SbxBYREF
| SbxSALUINT64
:
295 *p
->puInt64
= ImpDoubleToSalUInt64( n
); break;
296 case SbxBYREF
| SbxDATE
:
297 case SbxBYREF
| SbxDOUBLE
:
298 *p
->pDouble
= n
; break;
299 case SbxBYREF
| SbxCURRENCY
:
302 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXCURR
;
304 else if( n
< SbxMINCURR
)
306 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMINCURR
;
308 *p
->pnInt64
= ImpDoubleToCurrency( n
); break;
311 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */