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 sal_uInt32
ImpGetULong( const SbxValues
* p
)
35 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
43 nRes
= p
->nByte
; break;
48 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
60 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
66 nRes
= p
->nULong
; break;
68 if( !o3tl::convertsToAtMost(o3tl::roundAway(p
->nSingle
), SbxMAXULNG
) )
70 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXULNG
;
72 else if( p
->nSingle
< 0 )
74 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
77 nRes
= static_cast<sal_uInt32
>( p
->nSingle
+ 0.5 );
85 case SbxBYREF
| SbxDECIMAL
:
88 if( p
->eType
== SbxCURRENCY
)
89 dVal
= ImpCurrencyToDouble( p
->nInt64
);
90 else if( p
->eType
== SbxSALINT64
)
91 dVal
= static_cast< double >(p
->nInt64
);
92 else if( p
->eType
== SbxSALUINT64
)
93 dVal
= ImpSalUInt64ToDouble( p
->uInt64
);
94 else if( p
->eType
== SbxDECIMAL
)
98 p
->pDecimal
->getDouble( dVal
);
103 if( !o3tl::convertsToAtMost(o3tl::roundAway(dVal
), SbxMAXULNG
) )
105 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXULNG
;
109 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
112 nRes
= static_cast<sal_uInt32
>( dVal
+ 0.5 );
115 case SbxBYREF
| SbxSTRING
:
124 if( ImpScan( *p
->pOUString
, d
, t
, nullptr, false ) != ERRCODE_NONE
)
126 else if( !o3tl::convertsToAtMost(o3tl::roundAway(d
), SbxMAXULNG
) )
128 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXULNG
;
132 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
135 nRes
= static_cast<sal_uInt32
>( d
+ 0.5 );
140 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
142 nRes
= pVal
->GetULong();
145 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
); nRes
= 0;
150 case SbxBYREF
| SbxBYTE
:
151 nRes
= *p
->pByte
; break;
152 case SbxBYREF
| SbxERROR
:
153 case SbxBYREF
| SbxUSHORT
:
154 nRes
= *p
->pUShort
; break;
155 case SbxBYREF
| SbxULONG
:
156 nRes
= *p
->pULong
; break;
158 // from here on tests
159 case SbxBYREF
| SbxCHAR
:
160 aTmp
.nChar
= *p
->pChar
; goto ref
;
161 case SbxBYREF
| SbxINTEGER
:
162 case SbxBYREF
| SbxBOOL
:
163 aTmp
.nInteger
= *p
->pInteger
; goto ref
;
164 case SbxBYREF
| SbxLONG
:
165 aTmp
.nLong
= *p
->pLong
; goto ref
;
166 case SbxBYREF
| SbxSINGLE
:
167 aTmp
.nSingle
= *p
->pSingle
; goto ref
;
168 case SbxBYREF
| SbxDATE
:
169 case SbxBYREF
| SbxDOUBLE
:
170 aTmp
.nDouble
= *p
->pDouble
; goto ref
;
171 case SbxBYREF
| SbxCURRENCY
:
172 case SbxBYREF
| SbxSALINT64
:
173 aTmp
.nInt64
= *p
->pnInt64
; goto ref
;
174 case SbxBYREF
| SbxSALUINT64
:
175 aTmp
.uInt64
= *p
->puInt64
; goto ref
;
177 aTmp
.eType
= SbxDataType( p
->eType
& 0x0FFF );
178 p
= &aTmp
; goto start
;
181 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
); nRes
= 0;
186 void ImpPutULong( SbxValues
* p
, sal_uInt32 n
)
193 p
->nULong
= n
; break;
195 p
->nSingle
= static_cast<float>(n
); break;
198 p
->nDouble
= n
; break;
201 aTmp
.pnInt64
= &p
->nInt64
; goto direct
;
203 p
->uInt64
= n
; break;
205 case SbxBYREF
| SbxDECIMAL
:
206 ImpCreateDecimal( p
)->setULong( n
);
209 // from here on tests
211 aTmp
.pChar
= &p
->nChar
; goto direct
;
213 aTmp
.pByte
= &p
->nByte
; goto direct
;
216 aTmp
.pInteger
= &p
->nInteger
; goto direct
;
218 aTmp
.pLong
= &p
->nLong
; goto direct
;
221 aTmp
.pUShort
= &p
->nUShort
; goto direct
;
223 aTmp
.eType
= SbxDataType( p
->eType
| SbxBYREF
);
224 p
= &aTmp
; goto start
;
226 case SbxBYREF
| SbxSTRING
:
230 p
->pOUString
= new OUString
;
231 ImpCvtNum( static_cast<double>(n
), 0, *p
->pOUString
);
235 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
239 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
);
242 case SbxBYREF
| SbxCHAR
:
245 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXCHAR
;
247 *p
->pChar
= static_cast<sal_Unicode
>(n
); break;
248 case SbxBYREF
| SbxBYTE
:
251 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXBYTE
;
253 *p
->pByte
= static_cast<sal_uInt8
>(n
); break;
254 case SbxBYREF
| SbxINTEGER
:
255 case SbxBYREF
| SbxBOOL
:
258 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXINT
;
260 *p
->pInteger
= static_cast<sal_Int16
>(n
); break;
261 case SbxBYREF
| SbxERROR
:
262 case SbxBYREF
| SbxUSHORT
:
265 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXUINT
;
267 *p
->pUShort
= static_cast<sal_uInt16
>(n
); break;
268 case SbxBYREF
| SbxLONG
:
271 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXLNG
;
273 *p
->pLong
= static_cast<sal_Int32
>(n
); break;
274 case SbxBYREF
| SbxULONG
:
275 *p
->pULong
= n
; break;
276 case SbxBYREF
| SbxSINGLE
:
277 *p
->pSingle
= static_cast<float>(n
); break;
278 case SbxBYREF
| SbxDATE
:
279 case SbxBYREF
| SbxDOUBLE
:
280 *p
->pDouble
= n
; break;
281 case SbxBYREF
| SbxCURRENCY
:
282 *p
->pnInt64
= n
* CURRENCY_FACTOR
; break;
283 case SbxBYREF
| SbxSALINT64
:
284 *p
->pnInt64
= n
; break;
285 case SbxBYREF
| SbxSALUINT64
:
286 *p
->puInt64
= n
; break;
289 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */