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/safeint.hxx>
23 #include <vcl/errcode.hxx>
24 //#include <basic/sbx.hxx>
25 #include <basic/sberrors.hxx>
26 #include "sbxconv.hxx"
28 #include <rtl/math.hxx>
30 sal_uInt8
ImpGetByte( const SbxValues
* p
)
38 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
43 if( p
->nChar
> SbxMAXBYTE
)
45 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
48 nRes
= static_cast<sal_uInt8
>(p
->nChar
);
51 nRes
= p
->nByte
; break;
54 if( p
->nInteger
> SbxMAXBYTE
)
56 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXBYTE
;
58 else if( p
->nInteger
< 0 )
60 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
63 nRes
= static_cast<sal_uInt8
>(p
->nInteger
);
67 if( p
->nUShort
> o3tl::make_unsigned(SbxMAXBYTE
) )
69 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXBYTE
;
72 nRes
= static_cast<sal_uInt8
>(p
->nUShort
);
75 if( p
->nLong
> SbxMAXBYTE
)
77 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXBYTE
;
79 else if( p
->nLong
< 0 )
81 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
84 nRes
= static_cast<sal_uInt8
>(p
->nLong
);
87 if( p
->nULong
> SbxMAXBYTE
)
89 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXBYTE
;
92 nRes
= static_cast<sal_uInt8
>(p
->nULong
);
97 sal_Int64 val
= p
->nInt64
;
98 if ( p
->eType
== SbxCURRENCY
)
99 val
= val
/ CURRENCY_FACTOR
;
100 if( val
> SbxMAXBYTE
)
102 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXBYTE
;
104 else if( p
->nInt64
< 0 )
106 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
109 nRes
= static_cast<sal_uInt8
>(val
);
113 if( p
->uInt64
> SbxMAXBYTE
)
115 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXBYTE
;
118 nRes
= static_cast<sal_uInt8
>(p
->uInt64
);
121 if( p
->nSingle
> SbxMAXBYTE
)
123 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXBYTE
;
125 else if( p
->nSingle
< 0 )
127 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
130 nRes
= static_cast<sal_uInt8
>(rtl::math::round( p
->nSingle
));
135 case SbxBYREF
| SbxDECIMAL
:
138 if( p
->eType
== SbxDECIMAL
)
142 p
->pDecimal
->getDouble( dVal
);
147 if( dVal
> SbxMAXBYTE
)
149 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXBYTE
;
153 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
156 nRes
= static_cast<sal_uInt8
>(rtl::math::round( dVal
));
159 case SbxBYREF
| SbxSTRING
:
168 if( ImpScan( *p
->pOUString
, d
, t
, nullptr, true ) != ERRCODE_NONE
)
170 else if( d
> SbxMAXBYTE
)
172 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXBYTE
;
176 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
179 nRes
= static_cast<sal_uInt8
>( d
+ 0.5 );
184 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
186 nRes
= pVal
->GetByte();
189 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
); nRes
= 0;
194 case SbxBYREF
| SbxBYTE
:
195 nRes
= p
->nByte
; break;
197 // from here on will be tested
198 case SbxBYREF
| SbxCHAR
:
199 aTmp
.nChar
= *p
->pChar
; goto ref
;
200 case SbxBYREF
| SbxINTEGER
:
201 case SbxBYREF
| SbxBOOL
:
202 aTmp
.nInteger
= *p
->pInteger
; goto ref
;
203 case SbxBYREF
| SbxLONG
:
204 aTmp
.nLong
= *p
->pLong
; goto ref
;
205 case SbxBYREF
| SbxULONG
:
206 aTmp
.nULong
= *p
->pULong
; goto ref
;
207 case SbxBYREF
| SbxERROR
:
208 case SbxBYREF
| SbxUSHORT
:
209 aTmp
.nUShort
= *p
->pUShort
; goto ref
;
210 case SbxBYREF
| SbxSINGLE
:
211 aTmp
.nSingle
= *p
->pSingle
; goto ref
;
212 case SbxBYREF
| SbxDATE
:
213 case SbxBYREF
| SbxDOUBLE
:
214 aTmp
.nDouble
= *p
->pDouble
; goto ref
;
215 case SbxBYREF
| SbxCURRENCY
:
216 case SbxBYREF
| SbxSALINT64
:
217 aTmp
.nInt64
= *p
->pnInt64
; goto ref
;
218 case SbxBYREF
| SbxSALUINT64
:
219 aTmp
.uInt64
= *p
->puInt64
; goto ref
;
221 aTmp
.eType
= SbxDataType( p
->eType
& 0x0FFF );
222 p
= &aTmp
; goto start
;
225 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
); nRes
= 0;
230 void ImpPutByte( SbxValues
* p
, sal_uInt8 n
)
238 p
->nInteger
= n
; break;
241 p
->nUShort
= n
; break;
245 p
->nULong
= n
; break;
247 p
->nSingle
= n
; break;
250 p
->nDouble
= n
; break;
252 p
->nInt64
= n
* CURRENCY_FACTOR
; break;
254 p
->nInt64
= n
; break;
256 p
->uInt64
= n
; break;
258 case SbxBYREF
| SbxDECIMAL
:
259 ImpCreateDecimal( p
)->setByte( n
);
263 p
->nChar
= static_cast<sal_Unicode
>(n
); break;
265 case SbxBYREF
| SbxSTRING
:
269 p
->pOUString
= new OUString
;
270 ImpCvtNum( static_cast<double>(n
), 0, *p
->pOUString
);
274 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
278 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
);
281 case SbxBYREF
| SbxCHAR
:
282 *p
->pChar
= static_cast<sal_Unicode
>(n
); break;
283 case SbxBYREF
| SbxBYTE
:
284 *p
->pByte
= n
; break;
285 case SbxBYREF
| SbxINTEGER
:
286 case SbxBYREF
| SbxBOOL
:
287 *p
->pInteger
= n
; break;
288 case SbxBYREF
| SbxERROR
:
289 case SbxBYREF
| SbxUSHORT
:
290 *p
->pUShort
= n
; break;
291 case SbxBYREF
| SbxLONG
:
292 *p
->pLong
= n
; break;
293 case SbxBYREF
| SbxULONG
:
294 *p
->pULong
= n
; break;
295 case SbxBYREF
| SbxSINGLE
:
296 *p
->pSingle
= n
; break;
297 case SbxBYREF
| SbxDATE
:
298 case SbxBYREF
| SbxDOUBLE
:
299 *p
->pDouble
= n
; break;
300 case SbxBYREF
| SbxCURRENCY
:
301 p
->nInt64
= n
* CURRENCY_FACTOR
; break;
302 case SbxBYREF
| SbxSALINT64
:
303 *p
->pnInt64
= n
; break;
304 case SbxBYREF
| SbxSALUINT64
:
305 *p
->puInt64
= n
; break;
308 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */