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 <sal/config.h>
22 #include <o3tl/float_int_conversion.hxx>
23 #include <vcl/errcode.hxx>
24 #include <basic/sberrors.hxx>
25 #include "sbxconv.hxx"
27 float ImpGetSingle( const SbxValues
* p
)
35 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
40 nRes
= p
->nChar
; break;
42 nRes
= p
->nByte
; break;
45 nRes
= p
->nInteger
; break;
48 nRes
= p
->nUShort
; break;
50 nRes
= static_cast<float>(p
->nLong
); break;
52 nRes
= static_cast<float>(p
->nULong
); break;
54 nRes
= p
->nSingle
; break;
56 case SbxBYREF
| SbxDECIMAL
:
58 p
->pDecimal
->getSingle( nRes
);
69 if( p
->eType
== SbxCURRENCY
)
70 dVal
= ImpCurrencyToDouble( p
->nInt64
);
71 else if( p
->eType
== SbxSALINT64
)
72 dVal
= static_cast<float>(p
->nInt64
);
73 else if( p
->eType
== SbxSALUINT64
)
74 dVal
= static_cast<float>(p
->uInt64
);
78 if( dVal
> SbxMAXSNG
)
80 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
);
81 nRes
= static_cast< float >(SbxMAXSNG
);
83 else if( dVal
< SbxMINSNG
)
85 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
);
86 nRes
= static_cast< float >(SbxMINSNG
);
88 // tests for underflow - storing value too small for precision of single
89 else if( dVal
> 0 && dVal
< SbxMAXSNG2
)
91 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
);
92 nRes
= static_cast< float >(SbxMAXSNG2
);
94 else if( dVal
< 0 && dVal
> SbxMINSNG2
)
96 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
);
97 nRes
= static_cast< float >(SbxMINSNG2
);
100 nRes
= static_cast<float>(dVal
);
103 case SbxBYREF
| SbxSTRING
:
112 if( ImpScan( *p
->pOUString
, d
, t
, nullptr, false ) != ERRCODE_NONE
)
114 else if( d
> SbxMAXSNG
)
116 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
);
117 nRes
= static_cast< float >(SbxMAXSNG
);
119 else if( d
< SbxMINSNG
)
121 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
);
122 nRes
= static_cast< float >(SbxMINSNG
);
125 nRes
= static_cast<float>(d
);
130 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
132 nRes
= pVal
->GetSingle();
135 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
); nRes
= 0;
140 case SbxBYREF
| SbxCHAR
:
141 nRes
= *p
->pChar
; break;
142 case SbxBYREF
| SbxBYTE
:
143 nRes
= *p
->pByte
; break;
144 case SbxBYREF
| SbxINTEGER
:
145 case SbxBYREF
| SbxBOOL
:
146 nRes
= *p
->pInteger
; break;
147 case SbxBYREF
| SbxLONG
:
148 nRes
= static_cast<float>(*p
->pLong
); break;
149 case SbxBYREF
| SbxULONG
:
150 nRes
= static_cast<float>(*p
->pULong
); break;
151 case SbxBYREF
| SbxERROR
:
152 case SbxBYREF
| SbxUSHORT
:
153 nRes
= *p
->pUShort
; break;
154 case SbxBYREF
| SbxSINGLE
:
155 nRes
= *p
->pSingle
; break;
156 // from here had to be tested
157 case SbxBYREF
| SbxDATE
:
158 case SbxBYREF
| SbxDOUBLE
:
159 aTmp
.nDouble
= *p
->pDouble
; goto ref
;
160 case SbxBYREF
| SbxSALINT64
:
161 case SbxBYREF
| SbxCURRENCY
:
162 aTmp
.nInt64
= *p
->pnInt64
; goto ref
;
163 case SbxBYREF
| SbxSALUINT64
:
164 aTmp
.uInt64
= *p
->puInt64
; goto ref
;
166 aTmp
.eType
= SbxDataType( p
->eType
& 0x0FFF );
167 p
= &aTmp
; goto start
;
170 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
); nRes
= 0;
175 void ImpPutSingle( SbxValues
* p
, float n
)
182 aTmp
.pChar
= &p
->nChar
; goto direct
;
184 aTmp
.pByte
= &p
->nByte
; goto direct
;
187 aTmp
.pInteger
= &p
->nInteger
; goto direct
;
189 aTmp
.pLong
= &p
->nLong
; goto direct
;
191 aTmp
.pULong
= &p
->nULong
; goto direct
;
194 aTmp
.pUShort
= &p
->nUShort
; goto direct
;
197 aTmp
.pnInt64
= &p
->nInt64
; goto direct
;
199 aTmp
.puInt64
= &p
->uInt64
; goto direct
;
201 case SbxBYREF
| SbxDECIMAL
:
203 SbxDecimal
* pDec
= ImpCreateDecimal( p
);
204 if( !pDec
->setSingle( n
) )
205 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
);
209 aTmp
.eType
= SbxDataType( p
->eType
| SbxBYREF
);
210 p
= &aTmp
; goto start
;
212 // from here no tests
214 p
->nSingle
= n
; break;
217 p
->nDouble
= n
; break;
219 case SbxBYREF
| SbxSTRING
:
224 p
->pOUString
= new OUString
;
225 ImpCvtNum( static_cast<double>(n
), 6, *p
->pOUString
);
230 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
232 pVal
->PutSingle( n
);
234 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
);
237 case SbxBYREF
| SbxCHAR
:
238 if( !o3tl::convertsToAtMost(o3tl::roundAway(n
), SbxMAXCHAR
) )
240 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXCHAR
;
242 else if( !o3tl::convertsToAtLeast(o3tl::roundAway(n
), SbxMINCHAR
) )
244 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMINCHAR
;
246 *p
->pChar
= static_cast<sal_Unicode
>(n
); break;
247 case SbxBYREF
| SbxBYTE
:
248 if( !o3tl::convertsToAtMost(o3tl::roundAway(n
), SbxMAXBYTE
) )
250 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXBYTE
;
254 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= 0;
256 *p
->pByte
= static_cast<sal_uInt8
>(n
); break;
257 case SbxBYREF
| SbxINTEGER
:
258 case SbxBYREF
| SbxBOOL
:
259 if( !o3tl::convertsToAtMost(o3tl::roundAway(n
), SbxMAXINT
) )
261 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXINT
;
263 else if( !o3tl::convertsToAtLeast(o3tl::roundAway(n
), SbxMININT
) )
265 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMININT
;
267 *p
->pInteger
= static_cast<sal_Int16
>(n
); break;
268 case SbxBYREF
| SbxERROR
:
269 case SbxBYREF
| SbxUSHORT
:
270 if( !o3tl::convertsToAtMost(o3tl::roundAway(n
), SbxMAXUINT
) )
272 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXUINT
;
276 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= 0;
278 *p
->pUShort
= static_cast<sal_uInt16
>(n
); break;
279 case SbxBYREF
| SbxLONG
:
282 if( !o3tl::convertsToAtMost(o3tl::roundAway(n
), SbxMAXLNG
) )
284 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); i
= SbxMAXLNG
;
286 else if( !o3tl::convertsToAtLeast(o3tl::roundAway(n
), SbxMINLNG
) )
288 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); i
= SbxMINLNG
;
292 i
= sal::static_int_cast
< sal_Int32
>(n
);
294 *p
->pLong
= i
; break;
296 case SbxBYREF
| SbxULONG
:
299 if( !o3tl::convertsToAtMost(o3tl::roundAway(n
), SbxMAXULNG
) )
301 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); i
= SbxMAXULNG
;
305 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); i
= 0;
309 i
= sal::static_int_cast
< sal_uInt32
>(n
);
311 *p
->pULong
= i
; break;
313 case SbxBYREF
| SbxSINGLE
:
314 *p
->pSingle
= n
; break;
315 case SbxBYREF
| SbxDATE
:
316 case SbxBYREF
| SbxDOUBLE
:
317 *p
->pDouble
= static_cast<double>(n
); break;
318 case SbxBYREF
| SbxSALINT64
:
319 *p
->pnInt64
= static_cast<sal_Int64
>(n
); break;
320 case SbxBYREF
| SbxSALUINT64
:
321 *p
->puInt64
= static_cast<sal_uInt64
>(n
); break;
322 case SbxBYREF
| SbxCURRENCY
:
326 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); d
= SbxMAXCURR
;
328 else if( n
< SbxMINCURR
)
330 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); d
= SbxMINCURR
;
336 *p
->pnInt64
= ImpDoubleToCurrency( d
); break;
339 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
343 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */