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 sal_uInt16
ImpGetUShort( const SbxValues
* p
)
35 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
43 nRes
= p
->nByte
; break;
48 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
58 if( p
->nLong
> SbxMAXUINT
)
60 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXUINT
;
62 else if( p
->nLong
< 0 )
64 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
67 nRes
= static_cast<sal_uInt16
>(p
->nLong
);
70 if( p
->nULong
> SbxMAXUINT
)
72 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXUINT
;
75 nRes
= static_cast<sal_uInt16
>(p
->nULong
);
78 if( p
->nInt64
/ CURRENCY_FACTOR
> SbxMAXUINT
)
80 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXUINT
;
82 else if( p
->nInt64
< 0 )
84 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
87 nRes
= static_cast<sal_uInt16
>(p
->nInt64
/ CURRENCY_FACTOR
);
90 if( p
->nInt64
> SbxMAXUINT
)
92 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXUINT
;
94 else if( p
->nInt64
< 0 )
96 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
99 nRes
= static_cast<sal_uInt16
>(p
->nInt64
);
102 if( p
->uInt64
> SbxMAXUINT
)
104 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXUINT
;
107 nRes
= static_cast<sal_uInt16
>(p
->uInt64
);
110 nRes
= ImpDoubleToUShort(p
->nSingle
);
115 case SbxBYREF
| SbxDECIMAL
:
118 if( p
->eType
== SbxDECIMAL
)
122 p
->pDecimal
->getDouble( dVal
);
127 nRes
= ImpDoubleToUShort(dVal
);
130 case SbxBYREF
| SbxSTRING
:
139 if( ImpScan( *p
->pOUString
, d
, t
, nullptr, !LibreOffice6FloatingPointMode() ) != ERRCODE_NONE
)
142 nRes
= ImpDoubleToUShort(d
);
147 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
149 nRes
= pVal
->GetUShort();
152 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
); nRes
= 0;
157 case SbxBYREF
| SbxBYTE
:
158 nRes
= *p
->pByte
; break;
159 case SbxBYREF
| SbxERROR
:
160 case SbxBYREF
| SbxUSHORT
:
161 nRes
= *p
->pUShort
; break;
163 // from here on will be tested
164 case SbxBYREF
| SbxCHAR
:
165 aTmp
.nChar
= *p
->pChar
; goto ref
;
166 case SbxBYREF
| SbxINTEGER
:
167 case SbxBYREF
| SbxBOOL
:
168 aTmp
.nInteger
= *p
->pInteger
; goto ref
;
169 case SbxBYREF
| SbxLONG
:
170 aTmp
.nLong
= *p
->pLong
; goto ref
;
171 case SbxBYREF
| SbxULONG
:
172 aTmp
.nULong
= *p
->pULong
; goto ref
;
173 case SbxBYREF
| SbxSINGLE
:
174 aTmp
.nSingle
= *p
->pSingle
; goto ref
;
175 case SbxBYREF
| SbxDATE
:
176 case SbxBYREF
| SbxDOUBLE
:
177 aTmp
.nDouble
= *p
->pDouble
; goto ref
;
178 case SbxBYREF
| SbxCURRENCY
:
179 case SbxBYREF
| SbxSALINT64
:
180 aTmp
.nInt64
= *p
->pnInt64
; goto ref
;
181 case SbxBYREF
| SbxSALUINT64
:
182 aTmp
.uInt64
= *p
->puInt64
; goto ref
;
184 aTmp
.eType
= SbxDataType( p
->eType
& 0x0FFF );
185 p
= &aTmp
; goto start
;
188 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
); nRes
= 0;
193 void ImpPutUShort( SbxValues
* p
, sal_uInt16 n
)
202 p
->nUShort
= n
; break;
206 p
->nULong
= n
; break;
208 p
->nSingle
= n
; break;
211 p
->nDouble
= n
; break;
213 p
->nInt64
= n
* CURRENCY_FACTOR
; break;
215 p
->nInt64
= n
; break;
217 p
->uInt64
= n
; break;
219 case SbxBYREF
| SbxDECIMAL
:
220 ImpCreateDecimal( p
)->setUInt( n
);
223 // from here on tests
225 aTmp
.pChar
= &p
->nChar
; goto direct
;
227 aTmp
.pByte
= &p
->nByte
; goto direct
;
230 aTmp
.pInteger
= &p
->nInteger
;
232 aTmp
.eType
= SbxDataType( p
->eType
| SbxBYREF
);
233 p
= &aTmp
; goto start
;
235 case SbxBYREF
| SbxSTRING
:
239 p
->pOUString
= new OUString
;
240 ImpCvtNum( static_cast<double>(n
), 0, *p
->pOUString
);
244 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
246 pVal
->PutUShort( n
);
248 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
);
252 case SbxBYREF
| SbxCHAR
:
253 *p
->pChar
= static_cast<sal_Unicode
>(n
); break;
254 case SbxBYREF
| SbxBYTE
:
257 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXBYTE
;
259 *p
->pByte
= static_cast<sal_uInt8
>(n
); break;
260 case SbxBYREF
| SbxINTEGER
:
261 case SbxBYREF
| SbxBOOL
:
264 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXINT
;
266 *p
->pInteger
= static_cast<sal_Int16
>(n
); break;
267 case SbxBYREF
| SbxERROR
:
268 case SbxBYREF
| SbxUSHORT
:
269 *p
->pUShort
= n
; break;
270 case SbxBYREF
| SbxLONG
:
271 *p
->pLong
= n
; break;
272 case SbxBYREF
| SbxULONG
:
273 *p
->pULong
= n
; break;
274 case SbxBYREF
| SbxSINGLE
:
275 *p
->pSingle
= n
; break;
276 case SbxBYREF
| SbxDATE
:
277 case SbxBYREF
| SbxDOUBLE
:
278 *p
->pDouble
= n
; break;
279 case SbxBYREF
| SbxCURRENCY
:
280 *p
->pnInt64
= n
* CURRENCY_FACTOR
; break;
281 case SbxBYREF
| SbxSALINT64
:
282 *p
->pnInt64
= n
; break;
283 case SbxBYREF
| SbxSALUINT64
:
284 *p
->puInt64
= n
; break;
287 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */