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 <tools/errcode.hxx>
21 #include <basic/sbx.hxx>
22 #include "sbxconv.hxx"
24 sal_uInt16
ImpGetUShort( const SbxValues
* p
)
32 SbxBase::SetError( SbxERR_CONVERSION
);
39 nRes
= p
->nByte
; break;
44 SbxBase::SetError( SbxERR_OVERFLOW
); nRes
= 0;
54 if( p
->nLong
> SbxMAXUINT
)
56 SbxBase::SetError( SbxERR_OVERFLOW
); nRes
= SbxMAXUINT
;
58 else if( p
->nLong
< 0 )
60 SbxBase::SetError( SbxERR_OVERFLOW
); nRes
= 0;
63 nRes
= (sal_uInt16
) p
->nLong
;
66 if( p
->nULong
> SbxMAXUINT
)
68 SbxBase::SetError( SbxERR_OVERFLOW
); nRes
= SbxMAXUINT
;
71 nRes
= (sal_uInt16
) p
->nULong
;
74 if( p
->nInt64
/ CURRENCY_FACTOR
> SbxMAXUINT
)
76 SbxBase::SetError( SbxERR_OVERFLOW
); nRes
= SbxMAXUINT
;
78 else if( p
->nInt64
< 0 )
80 SbxBase::SetError( SbxERR_OVERFLOW
); nRes
= 0;
83 nRes
= (sal_uInt16
) (p
->nInt64
/ CURRENCY_FACTOR
);
86 if( p
->nInt64
> SbxMAXUINT
)
88 SbxBase::SetError( SbxERR_OVERFLOW
); nRes
= SbxMAXUINT
;
90 else if( p
->nInt64
< 0 )
92 SbxBase::SetError( SbxERR_OVERFLOW
); nRes
= 0;
95 nRes
= (sal_uInt16
) p
->nInt64
;
98 if( p
->uInt64
> SbxMAXUINT
)
100 SbxBase::SetError( SbxERR_OVERFLOW
); nRes
= SbxMAXUINT
;
103 nRes
= (sal_uInt16
) p
->uInt64
;
106 if( p
->nSingle
> SbxMAXUINT
)
108 SbxBase::SetError( SbxERR_OVERFLOW
); nRes
= SbxMAXUINT
;
110 else if( p
->nSingle
< 0 )
112 SbxBase::SetError( SbxERR_OVERFLOW
); nRes
= 0;
115 nRes
= (sal_uInt16
) ( p
->nSingle
+ 0.5 );
120 case SbxBYREF
| SbxDECIMAL
:
123 if( p
->eType
== SbxDECIMAL
)
127 p
->pDecimal
->getDouble( dVal
);
132 if( dVal
> SbxMAXUINT
)
134 SbxBase::SetError( SbxERR_OVERFLOW
); nRes
= SbxMAXUINT
;
138 SbxBase::SetError( SbxERR_OVERFLOW
); nRes
= 0;
141 nRes
= (sal_uInt16
) ( dVal
+ 0.5 );
144 case SbxBYREF
| SbxSTRING
:
153 if( ImpScan( *p
->pOUString
, d
, t
, NULL
) != SbxERR_OK
)
155 else if( d
> SbxMAXUINT
)
157 SbxBase::SetError( SbxERR_OVERFLOW
); nRes
= SbxMAXUINT
;
161 SbxBase::SetError( SbxERR_OVERFLOW
); nRes
= 0;
164 nRes
= (sal_uInt16
) ( d
+ 0.5 );
169 SbxValue
* pVal
= PTR_CAST(SbxValue
,p
->pObj
);
171 nRes
= pVal
->GetUShort();
174 SbxBase::SetError( SbxERR_NO_OBJECT
); nRes
= 0;
179 case SbxBYREF
| SbxBYTE
:
180 nRes
= *p
->pByte
; break;
181 case SbxBYREF
| SbxERROR
:
182 case SbxBYREF
| SbxUSHORT
:
183 nRes
= *p
->pUShort
; break;
185 // from here on will be tested
186 case SbxBYREF
| SbxCHAR
:
187 aTmp
.nChar
= *p
->pChar
; goto ref
;
188 case SbxBYREF
| SbxINTEGER
:
189 case SbxBYREF
| SbxBOOL
:
190 aTmp
.nInteger
= *p
->pInteger
; goto ref
;
191 case SbxBYREF
| SbxLONG
:
192 aTmp
.nLong
= *p
->pLong
; goto ref
;
193 case SbxBYREF
| SbxULONG
:
194 aTmp
.nULong
= *p
->pULong
; goto ref
;
195 case SbxBYREF
| SbxSINGLE
:
196 aTmp
.nSingle
= *p
->pSingle
; goto ref
;
197 case SbxBYREF
| SbxDATE
:
198 case SbxBYREF
| SbxDOUBLE
:
199 aTmp
.nDouble
= *p
->pDouble
; goto ref
;
200 case SbxBYREF
| SbxCURRENCY
:
201 case SbxBYREF
| SbxSALINT64
:
202 aTmp
.nInt64
= *p
->pnInt64
; goto ref
;
203 case SbxBYREF
| SbxSALUINT64
:
204 aTmp
.uInt64
= *p
->puInt64
; goto ref
;
206 aTmp
.eType
= SbxDataType( p
->eType
& 0x0FFF );
207 p
= &aTmp
; goto start
;
210 SbxBase::SetError( SbxERR_CONVERSION
); nRes
= 0;
215 void ImpPutUShort( SbxValues
* p
, sal_uInt16 n
)
224 p
->nUShort
= n
; break;
228 p
->nULong
= n
; break;
230 p
->nSingle
= n
; break;
233 p
->nDouble
= n
; break;
235 p
->nInt64
= n
* CURRENCY_FACTOR
; break;
237 p
->nInt64
= n
; break;
239 p
->uInt64
= n
; break;
241 case SbxBYREF
| SbxDECIMAL
:
242 ImpCreateDecimal( p
)->setUInt( n
);
245 // from here on tests
247 aTmp
.pChar
= &p
->nChar
; goto direct
;
249 aTmp
.pByte
= &p
->nByte
; goto direct
;
252 aTmp
.pInteger
= &p
->nInteger
;
254 aTmp
.eType
= SbxDataType( p
->eType
| SbxBYREF
);
255 p
= &aTmp
; goto start
;
257 case SbxBYREF
| SbxSTRING
:
261 p
->pOUString
= new OUString
;
262 ImpCvtNum( (double) n
, 0, *p
->pOUString
);
266 SbxValue
* pVal
= PTR_CAST(SbxValue
,p
->pObj
);
268 pVal
->PutUShort( n
);
270 SbxBase::SetError( SbxERR_NO_OBJECT
);
274 case SbxBYREF
| SbxCHAR
:
275 *p
->pChar
= (sal_Unicode
) n
; break;
276 case SbxBYREF
| SbxBYTE
:
279 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXBYTE
;
281 *p
->pByte
= (sal_uInt8
) n
; break;
282 case SbxBYREF
| SbxINTEGER
:
283 case SbxBYREF
| SbxBOOL
:
286 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXINT
;
288 *p
->pInteger
= (sal_Int16
) n
; break;
289 case SbxBYREF
| SbxERROR
:
290 case SbxBYREF
| SbxUSHORT
:
291 *p
->pUShort
= n
; break;
292 case SbxBYREF
| SbxLONG
:
293 *p
->pLong
= n
; break;
294 case SbxBYREF
| SbxULONG
:
295 *p
->pULong
= n
; break;
296 case SbxBYREF
| SbxSINGLE
:
297 *p
->pSingle
= n
; break;
298 case SbxBYREF
| SbxDATE
:
299 case SbxBYREF
| SbxDOUBLE
:
300 *p
->pDouble
= n
; break;
301 case SbxBYREF
| SbxCURRENCY
:
302 *p
->pnInt64
= n
* CURRENCY_FACTOR
; break;
303 case SbxBYREF
| SbxSALINT64
:
304 *p
->pnInt64
= n
; break;
305 case SbxBYREF
| SbxSALUINT64
:
306 *p
->puInt64
= n
; break;
309 SbxBase::SetError( SbxERR_CONVERSION
);
313 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */