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 float ImpGetSingle( const SbxValues
* p
)
32 SbxBase::SetError( SbxERR_CONVERSION
);
36 nRes
= p
->nChar
; break;
38 nRes
= p
->nByte
; break;
41 nRes
= p
->nInteger
; break;
44 nRes
= p
->nUShort
; break;
46 nRes
= (float) p
->nLong
; break;
48 nRes
= (float) p
->nULong
; break;
50 nRes
= p
->nSingle
; break;
52 case SbxBYREF
| SbxDECIMAL
:
54 p
->pDecimal
->getSingle( nRes
);
65 if( p
->eType
== SbxCURRENCY
)
66 dVal
= ImpCurrencyToDouble( p
->nInt64
);
67 else if( p
->eType
== SbxSALINT64
)
68 dVal
= (float) p
->nInt64
;
69 else if( p
->eType
== SbxSALUINT64
)
70 dVal
= (float) p
->uInt64
;
74 if( dVal
> SbxMAXSNG
)
76 SbxBase::SetError( SbxERR_OVERFLOW
);
77 nRes
= static_cast< float >(SbxMAXSNG
);
79 else if( dVal
< SbxMINSNG
)
81 SbxBase::SetError( SbxERR_OVERFLOW
);
82 nRes
= static_cast< float >(SbxMINSNG
);
84 // tests for underflow - storing value too small for precision of single
85 else if( dVal
> 0 && dVal
< SbxMAXSNG2
)
87 SbxBase::SetError( SbxERR_OVERFLOW
);
88 nRes
= static_cast< float >(SbxMAXSNG2
);
90 else if( dVal
< 0 && dVal
> SbxMINSNG2
)
92 SbxBase::SetError( SbxERR_OVERFLOW
);
93 nRes
= static_cast< float >(SbxMINSNG2
);
99 case SbxBYREF
| SbxSTRING
:
108 if( ImpScan( *p
->pOUString
, d
, t
, NULL
) != SbxERR_OK
)
110 else if( d
> SbxMAXSNG
)
112 SbxBase::SetError( SbxERR_OVERFLOW
);
113 nRes
= static_cast< float >(SbxMAXSNG
);
115 else if( d
< SbxMINSNG
)
117 SbxBase::SetError( SbxERR_OVERFLOW
);
118 nRes
= static_cast< float >(SbxMINSNG
);
126 SbxValue
* pVal
= PTR_CAST(SbxValue
,p
->pObj
);
128 nRes
= pVal
->GetSingle();
131 SbxBase::SetError( SbxERR_NO_OBJECT
); nRes
= 0;
136 case SbxBYREF
| SbxCHAR
:
137 nRes
= *p
->pChar
; break;
138 case SbxBYREF
| SbxBYTE
:
139 nRes
= *p
->pByte
; break;
140 case SbxBYREF
| SbxINTEGER
:
141 case SbxBYREF
| SbxBOOL
:
142 nRes
= *p
->pInteger
; break;
143 case SbxBYREF
| SbxLONG
:
144 nRes
= (float) *p
->pLong
; break;
145 case SbxBYREF
| SbxULONG
:
146 nRes
= (float) *p
->pULong
; break;
147 case SbxBYREF
| SbxERROR
:
148 case SbxBYREF
| SbxUSHORT
:
149 nRes
= *p
->pUShort
; break;
150 case SbxBYREF
| SbxSINGLE
:
151 nRes
= *p
->pSingle
; break;
152 // from here had to be tested
153 case SbxBYREF
| SbxDATE
:
154 case SbxBYREF
| SbxDOUBLE
:
155 aTmp
.nDouble
= *p
->pDouble
; goto ref
;
156 case SbxBYREF
| SbxSALINT64
:
157 case SbxBYREF
| SbxCURRENCY
:
158 aTmp
.nInt64
= *p
->pnInt64
; goto ref
;
159 case SbxBYREF
| SbxSALUINT64
:
160 aTmp
.uInt64
= *p
->puInt64
; goto ref
;
162 aTmp
.eType
= SbxDataType( p
->eType
& 0x0FFF );
163 p
= &aTmp
; goto start
;
166 SbxBase::SetError( SbxERR_CONVERSION
); nRes
= 0;
171 void ImpPutSingle( SbxValues
* p
, float n
)
178 aTmp
.pChar
= &p
->nChar
; goto direct
;
180 aTmp
.pByte
= &p
->nByte
; goto direct
;
183 aTmp
.pInteger
= &p
->nInteger
; goto direct
;
185 aTmp
.pLong
= &p
->nLong
; goto direct
;
187 aTmp
.pULong
= &p
->nULong
; goto direct
;
190 aTmp
.pUShort
= &p
->nUShort
; goto direct
;
193 aTmp
.pnInt64
= &p
->nInt64
; goto direct
;
195 aTmp
.puInt64
= &p
->uInt64
; goto direct
;
197 case SbxBYREF
| SbxDECIMAL
:
199 SbxDecimal
* pDec
= ImpCreateDecimal( p
);
200 if( !pDec
->setSingle( n
) )
201 SbxBase::SetError( SbxERR_OVERFLOW
);
205 aTmp
.eType
= SbxDataType( p
->eType
| SbxBYREF
);
206 p
= &aTmp
; goto start
;
208 // from here no tests
210 p
->nSingle
= n
; break;
213 p
->nDouble
= n
; break;
215 case SbxBYREF
| SbxSTRING
:
220 p
->pOUString
= new ::rtl::OUString
;
221 ImpCvtNum( (double) n
, 6, *p
->pOUString
);
226 SbxValue
* pVal
= PTR_CAST(SbxValue
,p
->pObj
);
228 pVal
->PutSingle( n
);
230 SbxBase::SetError( SbxERR_NO_OBJECT
);
233 case SbxBYREF
| SbxCHAR
:
236 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXCHAR
;
238 else if( n
< SbxMINCHAR
)
240 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINCHAR
;
242 *p
->pChar
= (sal_Unicode
) n
; break;
243 case SbxBYREF
| SbxBYTE
:
246 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXBYTE
;
250 SbxBase::SetError( SbxERR_OVERFLOW
); n
= 0;
252 *p
->pByte
= (sal_uInt8
) n
; break;
253 case SbxBYREF
| SbxINTEGER
:
254 case SbxBYREF
| SbxBOOL
:
257 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXINT
;
259 else if( n
< SbxMININT
)
261 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMININT
;
263 *p
->pInteger
= (sal_Int16
) n
; break;
264 case SbxBYREF
| SbxERROR
:
265 case SbxBYREF
| SbxUSHORT
:
268 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXUINT
;
272 SbxBase::SetError( SbxERR_OVERFLOW
); n
= 0;
274 *p
->pUShort
= (sal_uInt16
) n
; break;
275 case SbxBYREF
| SbxLONG
:
280 SbxBase::SetError( SbxERR_OVERFLOW
); i
= SbxMAXLNG
;
282 else if( n
< SbxMINLNG
)
284 SbxBase::SetError( SbxERR_OVERFLOW
); i
= SbxMINLNG
;
288 i
= sal::static_int_cast
< sal_Int32
>(n
);
290 *p
->pLong
= i
; break;
292 case SbxBYREF
| SbxULONG
:
297 SbxBase::SetError( SbxERR_OVERFLOW
); i
= SbxMAXULNG
;
301 SbxBase::SetError( SbxERR_OVERFLOW
); i
= 0;
305 i
= sal::static_int_cast
< sal_uInt32
>(n
);
307 *p
->pULong
= i
; break;
309 case SbxBYREF
| SbxSINGLE
:
310 *p
->pSingle
= n
; break;
311 case SbxBYREF
| SbxDATE
:
312 case SbxBYREF
| SbxDOUBLE
:
313 *p
->pDouble
= (double) n
; break;
314 case SbxBYREF
| SbxSALINT64
:
315 *p
->pnInt64
= (sal_Int64
)n
; break;
316 case SbxBYREF
| SbxSALUINT64
:
317 *p
->puInt64
= (sal_uInt64
)n
; break;
318 case SbxBYREF
| SbxCURRENCY
:
322 SbxBase::SetError( SbxERR_OVERFLOW
); d
= SbxMAXCURR
;
324 else if( n
< SbxMINCURR
)
326 SbxBase::SetError( SbxERR_OVERFLOW
); d
= SbxMINCURR
;
332 *p
->pnInt64
= ImpDoubleToCurrency( d
); break;
335 SbxBase::SetError( SbxERR_CONVERSION
);
339 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */