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 <vcl/errcode.hxx>
23 #include <basic/sberrors.hxx>
24 #include "sbxconv.hxx"
25 #include <rtlproto.hxx>
27 #include <rtl/math.hxx>
29 sal_Int32
ImpGetLong( const SbxValues
* p
)
37 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
42 nRes
= p
->nChar
; break;
44 nRes
= p
->nByte
; break;
47 nRes
= p
->nInteger
; break;
50 nRes
= p
->nUShort
; break;
52 nRes
= p
->nLong
; break;
54 if( p
->nULong
> SbxMAXLNG
)
56 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXLNG
;
59 nRes
= static_cast<sal_Int32
>(p
->nULong
);
62 nRes
= ImpDoubleToLong(p
->nSingle
);
72 sal_Int64 tstVal
= p
->nInt64
/ CURRENCY_FACTOR
;
73 nRes
= static_cast<sal_Int32
>(tstVal
);
74 if( tstVal
< SbxMINLNG
|| SbxMAXLNG
< tstVal
) SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
);
75 if( SbxMAXLNG
< tstVal
) nRes
= SbxMAXLNG
;
76 if( tstVal
< SbxMINLNG
) nRes
= SbxMINLNG
;
82 case SbxBYREF
| SbxDECIMAL
:
85 if( p
->eType
== SbxDECIMAL
)
89 p
->pDecimal
->getDouble( dVal
);
94 nRes
= ImpDoubleToLong(dVal
);
97 case SbxBYREF
| SbxSTRING
:
106 if( ImpScan( *p
->pOUString
, d
, t
, nullptr, !LibreOffice6FloatingPointMode() ) != ERRCODE_NONE
)
109 nRes
= ImpDoubleToLong(d
);
114 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
116 nRes
= pVal
->GetLong();
119 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
); nRes
= 0;
124 case SbxBYREF
| SbxCHAR
:
125 nRes
= *p
->pChar
; break;
126 case SbxBYREF
| SbxBYTE
:
127 nRes
= *p
->pByte
; break;
128 case SbxBYREF
| SbxINTEGER
:
129 case SbxBYREF
| SbxBOOL
:
130 nRes
= *p
->pInteger
; break;
131 case SbxBYREF
| SbxLONG
:
132 nRes
= *p
->pLong
; break;
134 // from here had to be tested
135 case SbxBYREF
| SbxULONG
:
136 aTmp
.nULong
= *p
->pULong
; goto ref
;
137 case SbxBYREF
| SbxERROR
:
138 case SbxBYREF
| SbxUSHORT
:
139 aTmp
.nUShort
= *p
->pUShort
; goto ref
;
140 case SbxBYREF
| SbxSINGLE
:
141 aTmp
.nSingle
= *p
->pSingle
; goto ref
;
142 case SbxBYREF
| SbxDATE
:
143 case SbxBYREF
| SbxDOUBLE
:
144 aTmp
.nDouble
= *p
->pDouble
; goto ref
;
145 case SbxBYREF
| SbxCURRENCY
:
146 case SbxBYREF
| SbxSALINT64
:
147 aTmp
.nInt64
= *p
->pnInt64
; goto ref
;
148 case SbxBYREF
| SbxSALUINT64
:
149 aTmp
.uInt64
= *p
->puInt64
; goto ref
;
152 aTmp
.eType
= SbxDataType( p
->eType
& 0x0FFF );
153 p
= &aTmp
; goto start
;
156 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
); nRes
= 0;
161 void ImpPutLong( SbxValues
* p
, sal_Int32 n
)
168 // From here had to be tested
170 aTmp
.pChar
= &p
->nChar
; goto direct
;
172 aTmp
.pByte
= &p
->nByte
; goto direct
;
175 aTmp
.pInteger
= &p
->nInteger
; goto direct
;
177 aTmp
.pULong
= &p
->nULong
; goto direct
;
179 aTmp
.puInt64
= &p
->uInt64
; goto direct
;
182 aTmp
.pUShort
= &p
->nUShort
;
184 aTmp
.eType
= SbxDataType( p
->eType
| SbxBYREF
);
185 p
= &aTmp
; goto start
;
187 // from here no longer
191 p
->nSingle
= static_cast<float>(n
); break;
194 p
->nDouble
= n
; break;
196 p
->nInt64
= n
* CURRENCY_FACTOR
; break;
198 p
->nInt64
= n
; break;
200 case SbxBYREF
| SbxDECIMAL
:
201 ImpCreateDecimal( p
)->setLong( n
);
204 case SbxBYREF
| SbxSTRING
:
208 p
->pOUString
= new OUString
;
209 ImpCvtNum( static_cast<double>(n
), 0, *p
->pOUString
);
213 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
217 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
);
220 case SbxBYREF
| SbxCHAR
:
223 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXCHAR
;
225 else if( n
< SbxMINCHAR
)
227 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMINCHAR
;
229 *p
->pChar
= static_cast<sal_Unicode
>(n
); break;
230 case SbxBYREF
| SbxBYTE
:
233 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXBYTE
;
237 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= 0;
239 *p
->pByte
= static_cast<sal_uInt8
>(n
); break;
240 case SbxBYREF
| SbxINTEGER
:
241 case SbxBYREF
| SbxBOOL
:
244 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXINT
;
246 else if( n
< SbxMININT
)
248 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMININT
;
250 *p
->pInteger
= static_cast<sal_Int16
>(n
); break;
251 case SbxBYREF
| SbxERROR
:
252 case SbxBYREF
| SbxUSHORT
:
255 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXUINT
;
259 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= 0;
261 *p
->pUShort
= static_cast<sal_uInt16
>(n
); break;
262 case SbxBYREF
| SbxLONG
:
263 *p
->pLong
= n
; break;
264 case SbxBYREF
| SbxULONG
:
267 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= 0;
269 *p
->pULong
= static_cast<sal_uInt32
>(n
); break;
270 case SbxBYREF
| SbxSALINT64
:
271 *p
->pnInt64
= n
; break;
272 case SbxBYREF
| SbxSALUINT64
:
275 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); *p
->puInt64
= 0;
280 case SbxBYREF
| SbxSINGLE
:
281 *p
->pSingle
= static_cast<float>(n
); break;
282 case SbxBYREF
| SbxDATE
:
283 case SbxBYREF
| SbxDOUBLE
:
284 *p
->pDouble
= static_cast<double>(n
); break;
285 case SbxBYREF
| SbxCURRENCY
:
286 *p
->pnInt64
= static_cast<sal_Int64
>(n
) * sal_Int64(CURRENCY_FACTOR
); break;
288 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
292 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */