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 "sbxconv.hxx"
24 #include <rtl/math.hxx>
26 sal_Int32
ImpGetLong( const SbxValues
* p
)
34 SbxBase::SetError( ERRCODE_SBX_CONVERSION
);
39 nRes
= p
->nChar
; break;
41 nRes
= p
->nByte
; break;
44 nRes
= p
->nInteger
; break;
47 nRes
= p
->nUShort
; break;
49 nRes
= p
->nLong
; break;
51 if( p
->nULong
> SbxMAXLNG
)
53 SbxBase::SetError( ERRCODE_SBX_OVERFLOW
); nRes
= SbxMAXLNG
;
56 nRes
= (sal_Int32
) p
->nULong
;
59 if( p
->nSingle
> SbxMAXLNG
)
61 SbxBase::SetError( ERRCODE_SBX_OVERFLOW
); nRes
= SbxMAXLNG
;
63 else if( p
->nSingle
< SbxMINLNG
)
65 SbxBase::SetError( ERRCODE_SBX_OVERFLOW
); nRes
= SbxMINLNG
;
68 nRes
= (sal_Int32
) rtl::math::round( p
->nSingle
);
78 sal_Int64 tstVal
= p
->nInt64
/ CURRENCY_FACTOR
;
79 nRes
= (sal_Int32
) (tstVal
);
80 if( tstVal
< SbxMINLNG
|| SbxMAXLNG
< tstVal
) SbxBase::SetError( ERRCODE_SBX_OVERFLOW
);
81 if( SbxMAXLNG
< tstVal
) nRes
= SbxMAXLNG
;
82 if( tstVal
< SbxMINLNG
) nRes
= SbxMINLNG
;
88 case SbxBYREF
| SbxDECIMAL
:
91 if( p
->eType
== SbxDECIMAL
)
95 p
->pDecimal
->getDouble( dVal
);
100 if( dVal
> SbxMAXLNG
)
102 SbxBase::SetError( ERRCODE_SBX_OVERFLOW
); nRes
= SbxMAXLNG
;
104 else if( dVal
< SbxMINLNG
)
106 SbxBase::SetError( ERRCODE_SBX_OVERFLOW
); nRes
= SbxMINLNG
;
109 nRes
= (sal_Int32
) rtl::math::round( dVal
);
112 case SbxBYREF
| SbxSTRING
:
121 if( ImpScan( *p
->pOUString
, d
, t
, nullptr ) != ERRCODE_SBX_OK
)
123 else if( d
> SbxMAXLNG
)
125 SbxBase::SetError( ERRCODE_SBX_OVERFLOW
); nRes
= SbxMAXLNG
;
127 else if( d
< SbxMINLNG
)
129 SbxBase::SetError( ERRCODE_SBX_OVERFLOW
); nRes
= SbxMINLNG
;
132 nRes
= (sal_Int32
) rtl::math::round( d
);
137 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
139 nRes
= pVal
->GetLong();
142 SbxBase::SetError( ERRCODE_SBX_NO_OBJECT
); nRes
= 0;
147 case SbxBYREF
| SbxCHAR
:
148 nRes
= *p
->pChar
; break;
149 case SbxBYREF
| SbxBYTE
:
150 nRes
= *p
->pByte
; break;
151 case SbxBYREF
| SbxINTEGER
:
152 case SbxBYREF
| SbxBOOL
:
153 nRes
= *p
->pInteger
; break;
154 case SbxBYREF
| SbxLONG
:
155 nRes
= *p
->pLong
; break;
157 // from here had to be tested
158 case SbxBYREF
| SbxULONG
:
159 aTmp
.nULong
= *p
->pULong
; goto ref
;
160 case SbxBYREF
| SbxERROR
:
161 case SbxBYREF
| SbxUSHORT
:
162 aTmp
.nUShort
= *p
->pUShort
; goto ref
;
163 case SbxBYREF
| SbxSINGLE
:
164 aTmp
.nSingle
= *p
->pSingle
; goto ref
;
165 case SbxBYREF
| SbxDATE
:
166 case SbxBYREF
| SbxDOUBLE
:
167 aTmp
.nDouble
= *p
->pDouble
; goto ref
;
168 case SbxBYREF
| SbxCURRENCY
:
169 case SbxBYREF
| SbxSALINT64
:
170 aTmp
.nInt64
= *p
->pnInt64
; goto ref
;
171 case SbxBYREF
| SbxSALUINT64
:
172 aTmp
.uInt64
= *p
->puInt64
; goto ref
;
175 aTmp
.eType
= SbxDataType( p
->eType
& 0x0FFF );
176 p
= &aTmp
; goto start
;
179 SbxBase::SetError( ERRCODE_SBX_CONVERSION
); nRes
= 0;
184 void ImpPutLong( SbxValues
* p
, sal_Int32 n
)
191 // From here had to be tested
193 aTmp
.pChar
= &p
->nChar
; goto direct
;
195 aTmp
.pByte
= &p
->nByte
; goto direct
;
198 aTmp
.pInteger
= &p
->nInteger
; goto direct
;
200 aTmp
.pULong
= &p
->nULong
; goto direct
;
202 aTmp
.puInt64
= &p
->uInt64
; goto direct
;
205 aTmp
.pUShort
= &p
->nUShort
;
207 aTmp
.eType
= SbxDataType( p
->eType
| SbxBYREF
);
208 p
= &aTmp
; goto start
;
210 // from here no longer
214 p
->nSingle
= (float) n
; break;
217 p
->nDouble
= n
; break;
219 p
->nInt64
= n
* CURRENCY_FACTOR
; break;
221 p
->nInt64
= n
; break;
223 case SbxBYREF
| SbxDECIMAL
:
224 ImpCreateDecimal( p
)->setLong( n
);
227 case SbxBYREF
| SbxSTRING
:
231 p
->pOUString
= new OUString
;
232 ImpCvtNum( (double) n
, 0, *p
->pOUString
);
236 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
240 SbxBase::SetError( ERRCODE_SBX_NO_OBJECT
);
243 case SbxBYREF
| SbxCHAR
:
246 SbxBase::SetError( ERRCODE_SBX_OVERFLOW
); n
= SbxMAXCHAR
;
248 else if( n
< SbxMINCHAR
)
250 SbxBase::SetError( ERRCODE_SBX_OVERFLOW
); n
= SbxMINCHAR
;
252 *p
->pChar
= (sal_Unicode
) n
; break;
253 case SbxBYREF
| SbxBYTE
:
256 SbxBase::SetError( ERRCODE_SBX_OVERFLOW
); n
= SbxMAXBYTE
;
260 SbxBase::SetError( ERRCODE_SBX_OVERFLOW
); n
= 0;
262 *p
->pByte
= (sal_uInt8
) n
; break;
263 case SbxBYREF
| SbxINTEGER
:
264 case SbxBYREF
| SbxBOOL
:
267 SbxBase::SetError( ERRCODE_SBX_OVERFLOW
); n
= SbxMAXINT
;
269 else if( n
< SbxMININT
)
271 SbxBase::SetError( ERRCODE_SBX_OVERFLOW
); n
= SbxMININT
;
273 *p
->pInteger
= (sal_Int16
) n
; break;
274 case SbxBYREF
| SbxERROR
:
275 case SbxBYREF
| SbxUSHORT
:
278 SbxBase::SetError( ERRCODE_SBX_OVERFLOW
); n
= SbxMAXUINT
;
282 SbxBase::SetError( ERRCODE_SBX_OVERFLOW
); n
= 0;
284 *p
->pUShort
= (sal_uInt16
) n
; break;
285 case SbxBYREF
| SbxLONG
:
286 *p
->pLong
= n
; break;
287 case SbxBYREF
| SbxULONG
:
290 SbxBase::SetError( ERRCODE_SBX_OVERFLOW
); n
= 0;
292 *p
->pULong
= (sal_uInt32
) n
; break;
293 case SbxBYREF
| SbxSALINT64
:
294 *p
->pnInt64
= n
; break;
295 case SbxBYREF
| SbxSALUINT64
:
298 SbxBase::SetError( ERRCODE_SBX_OVERFLOW
); *p
->puInt64
= 0;
303 case SbxBYREF
| SbxSINGLE
:
304 *p
->pSingle
= (float) n
; break;
305 case SbxBYREF
| SbxDATE
:
306 case SbxBYREF
| SbxDOUBLE
:
307 *p
->pDouble
= (double) n
; break;
308 case SbxBYREF
| SbxCURRENCY
:
309 *p
->pnInt64
= (sal_Int64
)n
* (sal_Int64
)CURRENCY_FACTOR
; break;
311 SbxBase::SetError( ERRCODE_SBX_CONVERSION
);
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */