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 <comphelper/errcode.hxx>
23 #include <basic/sberrors.hxx>
24 #include "sbxconv.hxx"
26 #include <rtl/math.hxx>
28 sal_Int32
ImpGetLong( const SbxValues
* p
)
36 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
41 nRes
= p
->nChar
; break;
43 nRes
= p
->nByte
; break;
46 nRes
= p
->nInteger
; break;
49 nRes
= p
->nUShort
; break;
51 nRes
= p
->nLong
; break;
53 if( p
->nULong
> SbxMAXLNG
)
55 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXLNG
;
58 nRes
= static_cast<sal_Int32
>(p
->nULong
);
61 nRes
= ImpDoubleToLong(p
->nSingle
);
70 nRes
= CurTo
<sal_Int32
>(p
->nInt64
);
75 case SbxBYREF
| SbxDECIMAL
:
78 if( p
->eType
== SbxDECIMAL
)
82 p
->pDecimal
->getDouble( dVal
);
87 nRes
= ImpDoubleToLong(dVal
);
90 case SbxBYREF
| SbxSTRING
:
99 if( ImpScan( *p
->pOUString
, d
, t
, nullptr ) != ERRCODE_NONE
)
102 nRes
= ImpDoubleToLong(d
);
107 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
109 nRes
= pVal
->GetLong();
112 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
); nRes
= 0;
117 case SbxBYREF
| SbxCHAR
:
118 nRes
= *p
->pChar
; break;
119 case SbxBYREF
| SbxBYTE
:
120 nRes
= *p
->pByte
; break;
121 case SbxBYREF
| SbxINTEGER
:
122 case SbxBYREF
| SbxBOOL
:
123 nRes
= *p
->pInteger
; break;
124 case SbxBYREF
| SbxLONG
:
125 nRes
= *p
->pLong
; break;
127 // from here had to be tested
128 case SbxBYREF
| SbxULONG
:
129 aTmp
.nULong
= *p
->pULong
; goto ref
;
130 case SbxBYREF
| SbxERROR
:
131 case SbxBYREF
| SbxUSHORT
:
132 aTmp
.nUShort
= *p
->pUShort
; goto ref
;
133 case SbxBYREF
| SbxSINGLE
:
134 aTmp
.nSingle
= *p
->pSingle
; goto ref
;
135 case SbxBYREF
| SbxDATE
:
136 case SbxBYREF
| SbxDOUBLE
:
137 aTmp
.nDouble
= *p
->pDouble
; goto ref
;
138 case SbxBYREF
| SbxCURRENCY
:
139 case SbxBYREF
| SbxSALINT64
:
140 aTmp
.nInt64
= *p
->pnInt64
; goto ref
;
141 case SbxBYREF
| SbxSALUINT64
:
142 aTmp
.uInt64
= *p
->puInt64
; goto ref
;
145 aTmp
.eType
= SbxDataType( p
->eType
& 0x0FFF );
146 p
= &aTmp
; goto start
;
149 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
); nRes
= 0;
154 void ImpPutLong( SbxValues
* p
, sal_Int32 n
)
161 // From here had to be tested
163 aTmp
.pChar
= &p
->nChar
; goto direct
;
165 aTmp
.pByte
= &p
->nByte
; goto direct
;
168 aTmp
.pInteger
= &p
->nInteger
; goto direct
;
170 aTmp
.pULong
= &p
->nULong
; goto direct
;
172 aTmp
.puInt64
= &p
->uInt64
; goto direct
;
175 aTmp
.pUShort
= &p
->nUShort
;
177 aTmp
.eType
= SbxDataType( p
->eType
| SbxBYREF
);
178 p
= &aTmp
; goto start
;
180 // from here no longer
184 p
->nSingle
= static_cast<float>(n
); break;
187 p
->nDouble
= n
; break;
189 p
->nInt64
= CurFrom(n
); break;
191 p
->nInt64
= n
; break;
193 case SbxBYREF
| SbxDECIMAL
:
194 ImpCreateDecimal( p
)->setLong( n
);
197 case SbxBYREF
| SbxSTRING
:
201 p
->pOUString
= new OUString
;
202 ImpCvtNum( static_cast<double>(n
), 0, *p
->pOUString
);
206 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
210 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
);
213 case SbxBYREF
| SbxCHAR
:
216 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXCHAR
;
218 else if( n
< SbxMINCHAR
)
220 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMINCHAR
;
222 *p
->pChar
= static_cast<sal_Unicode
>(n
); break;
223 case SbxBYREF
| SbxBYTE
:
226 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXBYTE
;
230 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= 0;
232 *p
->pByte
= static_cast<sal_uInt8
>(n
); break;
233 case SbxBYREF
| SbxINTEGER
:
234 case SbxBYREF
| SbxBOOL
:
237 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXINT
;
239 else if( n
< SbxMININT
)
241 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMININT
;
243 *p
->pInteger
= static_cast<sal_Int16
>(n
); break;
244 case SbxBYREF
| SbxERROR
:
245 case SbxBYREF
| SbxUSHORT
:
248 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXUINT
;
252 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= 0;
254 *p
->pUShort
= static_cast<sal_uInt16
>(n
); break;
255 case SbxBYREF
| SbxLONG
:
256 *p
->pLong
= n
; break;
257 case SbxBYREF
| SbxULONG
:
260 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= 0;
262 *p
->pULong
= static_cast<sal_uInt32
>(n
); break;
263 case SbxBYREF
| SbxSALINT64
:
264 *p
->pnInt64
= n
; break;
265 case SbxBYREF
| SbxSALUINT64
:
268 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); *p
->puInt64
= 0;
273 case SbxBYREF
| SbxSINGLE
:
274 *p
->pSingle
= static_cast<float>(n
); break;
275 case SbxBYREF
| SbxDATE
:
276 case SbxBYREF
| SbxDOUBLE
:
277 *p
->pDouble
= static_cast<double>(n
); break;
278 case SbxBYREF
| SbxCURRENCY
:
279 *p
->pnInt64
= CurFrom(n
); break;
281 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
285 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */