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 <vcl/errcode.hxx>
21 //#include <basic/sbx.hxx>
22 #include <basic/sberrors.hxx>
23 #include "sbxconv.hxx"
25 #include <rtl/math.hxx>
27 sal_uInt8
ImpGetByte( const SbxValues
* p
)
35 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
40 if( p
->nChar
> SbxMAXBYTE
)
42 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
45 nRes
= static_cast<sal_uInt8
>(p
->nChar
);
48 nRes
= p
->nByte
; break;
51 if( p
->nInteger
> SbxMAXBYTE
)
53 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXBYTE
;
55 else if( p
->nInteger
< 0 )
57 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
60 nRes
= static_cast<sal_uInt8
>(p
->nInteger
);
64 if( p
->nUShort
> sal_uInt16(SbxMAXBYTE
) )
66 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXBYTE
;
69 nRes
= static_cast<sal_uInt8
>(p
->nUShort
);
72 if( p
->nLong
> SbxMAXBYTE
)
74 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXBYTE
;
76 else if( p
->nLong
< 0 )
78 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
81 nRes
= static_cast<sal_uInt8
>(p
->nLong
);
84 if( p
->nULong
> SbxMAXBYTE
)
86 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXBYTE
;
89 nRes
= static_cast<sal_uInt8
>(p
->nULong
);
94 sal_Int64 val
= p
->nInt64
;
95 if ( p
->eType
== SbxCURRENCY
)
96 val
= val
/ CURRENCY_FACTOR
;
97 if( val
> SbxMAXBYTE
)
99 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXBYTE
;
101 else if( p
->nInt64
< 0 )
103 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
106 nRes
= static_cast<sal_uInt8
>(val
);
110 if( p
->uInt64
> SbxMAXBYTE
)
112 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXBYTE
;
115 nRes
= static_cast<sal_uInt8
>(p
->uInt64
);
118 if( p
->nSingle
> SbxMAXBYTE
)
120 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXBYTE
;
122 else if( p
->nSingle
< 0 )
124 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
127 nRes
= static_cast<sal_uInt8
>(rtl::math::round( p
->nSingle
));
132 case SbxBYREF
| SbxDECIMAL
:
135 if( p
->eType
== SbxDECIMAL
)
139 p
->pDecimal
->getDouble( dVal
);
144 if( dVal
> SbxMAXBYTE
)
146 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXBYTE
;
150 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
153 nRes
= static_cast<sal_uInt8
>(rtl::math::round( dVal
));
156 case SbxBYREF
| SbxSTRING
:
165 if( ImpScan( *p
->pOUString
, d
, t
, nullptr, false ) != ERRCODE_NONE
)
167 else if( d
> SbxMAXBYTE
)
169 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXBYTE
;
173 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
176 nRes
= static_cast<sal_uInt8
>( d
+ 0.5 );
181 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
183 nRes
= pVal
->GetByte();
186 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
); nRes
= 0;
191 case SbxBYREF
| SbxBYTE
:
192 nRes
= p
->nByte
; break;
194 // from here on will be tested
195 case SbxBYREF
| SbxCHAR
:
196 aTmp
.nChar
= *p
->pChar
; goto ref
;
197 case SbxBYREF
| SbxINTEGER
:
198 case SbxBYREF
| SbxBOOL
:
199 aTmp
.nInteger
= *p
->pInteger
; goto ref
;
200 case SbxBYREF
| SbxLONG
:
201 aTmp
.nLong
= *p
->pLong
; goto ref
;
202 case SbxBYREF
| SbxULONG
:
203 aTmp
.nULong
= *p
->pULong
; goto ref
;
204 case SbxBYREF
| SbxERROR
:
205 case SbxBYREF
| SbxUSHORT
:
206 aTmp
.nUShort
= *p
->pUShort
; goto ref
;
207 case SbxBYREF
| SbxSINGLE
:
208 aTmp
.nSingle
= *p
->pSingle
; goto ref
;
209 case SbxBYREF
| SbxDATE
:
210 case SbxBYREF
| SbxDOUBLE
:
211 aTmp
.nDouble
= *p
->pDouble
; goto ref
;
212 case SbxBYREF
| SbxCURRENCY
:
213 case SbxBYREF
| SbxSALINT64
:
214 aTmp
.nInt64
= *p
->pnInt64
; goto ref
;
215 case SbxBYREF
| SbxSALUINT64
:
216 aTmp
.uInt64
= *p
->puInt64
; goto ref
;
218 aTmp
.eType
= SbxDataType( p
->eType
& 0x0FFF );
219 p
= &aTmp
; goto start
;
222 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
); nRes
= 0;
227 void ImpPutByte( SbxValues
* p
, sal_uInt8 n
)
235 p
->nInteger
= n
; break;
238 p
->nUShort
= n
; break;
242 p
->nULong
= n
; break;
244 p
->nSingle
= n
; break;
247 p
->nDouble
= n
; break;
249 p
->nInt64
= n
* CURRENCY_FACTOR
; break;
251 p
->nInt64
= n
; break;
253 p
->uInt64
= n
; break;
255 case SbxBYREF
| SbxDECIMAL
:
256 ImpCreateDecimal( p
)->setByte( n
);
260 p
->nChar
= static_cast<sal_Unicode
>(n
); break;
262 case SbxBYREF
| SbxSTRING
:
266 p
->pOUString
= new OUString
;
267 ImpCvtNum( static_cast<double>(n
), 0, *p
->pOUString
);
271 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
275 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
);
278 case SbxBYREF
| SbxCHAR
:
279 *p
->pChar
= static_cast<sal_Unicode
>(n
); break;
280 case SbxBYREF
| SbxBYTE
:
281 *p
->pByte
= n
; break;
282 case SbxBYREF
| SbxINTEGER
:
283 case SbxBYREF
| SbxBOOL
:
284 *p
->pInteger
= n
; break;
285 case SbxBYREF
| SbxERROR
:
286 case SbxBYREF
| SbxUSHORT
:
287 *p
->pUShort
= n
; break;
288 case SbxBYREF
| SbxLONG
:
289 *p
->pLong
= n
; break;
290 case SbxBYREF
| SbxULONG
:
291 *p
->pULong
= n
; break;
292 case SbxBYREF
| SbxSINGLE
:
293 *p
->pSingle
= n
; break;
294 case SbxBYREF
| SbxDATE
:
295 case SbxBYREF
| SbxDOUBLE
:
296 *p
->pDouble
= n
; break;
297 case SbxBYREF
| SbxCURRENCY
:
298 p
->nInt64
= n
* CURRENCY_FACTOR
; break;
299 case SbxBYREF
| SbxSALINT64
:
300 *p
->pnInt64
= n
; break;
301 case SbxBYREF
| SbxSALUINT64
:
302 *p
->puInt64
= n
; break;
305 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
309 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */