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 sal_uInt16
ImpGetUShort( const SbxValues
* p
)
34 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
42 nRes
= p
->nByte
; break;
47 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
57 if( p
->nLong
> SbxMAXUINT
)
59 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXUINT
;
61 else if( p
->nLong
< 0 )
63 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
66 nRes
= static_cast<sal_uInt16
>(p
->nLong
);
69 if( p
->nULong
> SbxMAXUINT
)
71 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXUINT
;
74 nRes
= static_cast<sal_uInt16
>(p
->nULong
);
77 nRes
= CurTo
<sal_uInt16
>(p
->nInt64
);
80 if( p
->nInt64
> SbxMAXUINT
)
82 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXUINT
;
84 else if( p
->nInt64
< 0 )
86 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= 0;
89 nRes
= static_cast<sal_uInt16
>(p
->nInt64
);
92 if( p
->uInt64
> SbxMAXUINT
)
94 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXUINT
;
97 nRes
= static_cast<sal_uInt16
>(p
->uInt64
);
100 nRes
= ImpDoubleToUShort(p
->nSingle
);
105 case SbxBYREF
| SbxDECIMAL
:
108 if( p
->eType
== SbxDECIMAL
)
112 p
->pDecimal
->getDouble( dVal
);
117 nRes
= ImpDoubleToUShort(dVal
);
120 case SbxBYREF
| SbxSTRING
:
129 if( ImpScan( *p
->pOUString
, d
, t
, nullptr ) != ERRCODE_NONE
)
132 nRes
= ImpDoubleToUShort(d
);
137 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
139 nRes
= pVal
->GetUShort();
142 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
); nRes
= 0;
147 case SbxBYREF
| SbxBYTE
:
148 nRes
= *p
->pByte
; break;
149 case SbxBYREF
| SbxERROR
:
150 case SbxBYREF
| SbxUSHORT
:
151 nRes
= *p
->pUShort
; break;
153 // from here on will be tested
154 case SbxBYREF
| SbxCHAR
:
155 aTmp
.nChar
= *p
->pChar
; goto ref
;
156 case SbxBYREF
| SbxINTEGER
:
157 case SbxBYREF
| SbxBOOL
:
158 aTmp
.nInteger
= *p
->pInteger
; goto ref
;
159 case SbxBYREF
| SbxLONG
:
160 aTmp
.nLong
= *p
->pLong
; goto ref
;
161 case SbxBYREF
| SbxULONG
:
162 aTmp
.nULong
= *p
->pULong
; 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
;
174 aTmp
.eType
= SbxDataType( p
->eType
& 0x0FFF );
175 p
= &aTmp
; goto start
;
178 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
); nRes
= 0;
183 void ImpPutUShort( SbxValues
* p
, sal_uInt16 n
)
192 p
->nUShort
= n
; break;
196 p
->nULong
= n
; break;
198 p
->nSingle
= n
; break;
201 p
->nDouble
= n
; break;
203 p
->nInt64
= CurFrom(n
); break;
205 p
->nInt64
= n
; break;
207 p
->uInt64
= n
; break;
209 case SbxBYREF
| SbxDECIMAL
:
210 ImpCreateDecimal( p
)->setUInt( n
);
213 // from here on tests
215 aTmp
.pChar
= &p
->nChar
; goto direct
;
217 aTmp
.pByte
= &p
->nByte
; goto direct
;
220 aTmp
.pInteger
= &p
->nInteger
;
222 aTmp
.eType
= SbxDataType( p
->eType
| SbxBYREF
);
223 p
= &aTmp
; goto start
;
225 case SbxBYREF
| SbxSTRING
:
229 p
->pOUString
= new OUString
;
230 ImpCvtNum( static_cast<double>(n
), 0, *p
->pOUString
);
234 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
236 pVal
->PutUShort( n
);
238 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
);
242 case SbxBYREF
| SbxCHAR
:
243 *p
->pChar
= static_cast<sal_Unicode
>(n
); break;
244 case SbxBYREF
| SbxBYTE
:
247 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXBYTE
;
249 *p
->pByte
= static_cast<sal_uInt8
>(n
); break;
250 case SbxBYREF
| SbxINTEGER
:
251 case SbxBYREF
| SbxBOOL
:
254 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); n
= SbxMAXINT
;
256 *p
->pInteger
= static_cast<sal_Int16
>(n
); break;
257 case SbxBYREF
| SbxERROR
:
258 case SbxBYREF
| SbxUSHORT
:
259 *p
->pUShort
= n
; break;
260 case SbxBYREF
| SbxLONG
:
261 *p
->pLong
= n
; break;
262 case SbxBYREF
| SbxULONG
:
263 *p
->pULong
= n
; break;
264 case SbxBYREF
| SbxSINGLE
:
265 *p
->pSingle
= n
; break;
266 case SbxBYREF
| SbxDATE
:
267 case SbxBYREF
| SbxDOUBLE
:
268 *p
->pDouble
= n
; break;
269 case SbxBYREF
| SbxCURRENCY
:
270 *p
->pnInt64
= CurFrom(n
); break;
271 case SbxBYREF
| SbxSALINT64
:
272 *p
->pnInt64
= n
; break;
273 case SbxBYREF
| SbxSALUINT64
:
274 *p
->puInt64
= n
; break;
277 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
281 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */