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 <config_features.h>
22 #include <tools/errcode.hxx>
23 #include <basic/sbx.hxx>
24 #include "sbxconv.hxx"
25 #include "runtime.hxx"
27 double ImpGetDouble( const SbxValues
* p
)
33 SbxBase::SetError( SbxERR_CONVERSION
);
37 nRes
= p
->nChar
; break;
39 nRes
= p
->nByte
; break;
42 nRes
= p
->nInteger
; break;
45 nRes
= p
->nUShort
; break;
47 nRes
= p
->nLong
; break;
49 nRes
= p
->nULong
; break;
51 nRes
= p
->nSingle
; break;
54 nRes
= p
->nDouble
; break;
56 nRes
= ImpCurrencyToDouble( p
->nInt64
); break;
58 nRes
= static_cast< double >(p
->nInt64
); break;
60 nRes
= ImpSalUInt64ToDouble( p
->uInt64
); break;
62 case SbxBYREF
| SbxDECIMAL
:
64 p
->pDecimal
->getDouble( nRes
);
68 case SbxBYREF
| SbxSTRING
:
74 #if HAVE_FEATURE_SCRIPTING
75 if ( SbiRuntime::isVBAEnabled() )// VBA only behaviour
76 SbxBase::SetError( SbxERR_CONVERSION
);
83 if( ImpScan( *p
->pOUString
, d
, t
, NULL
) != SbxERR_OK
)
86 #if HAVE_FEATURE_SCRIPTING
87 if ( SbiRuntime::isVBAEnabled() )// VBA only behaviour
88 SbxBase::SetError( SbxERR_CONVERSION
);
97 SbxValue
* pVal
= PTR_CAST(SbxValue
,p
->pObj
);
99 nRes
= pVal
->GetDouble();
102 SbxBase::SetError( SbxERR_NO_OBJECT
); nRes
= 0;
107 case SbxBYREF
| SbxCHAR
:
108 nRes
= *p
->pChar
; break;
109 case SbxBYREF
| SbxBYTE
:
110 nRes
= *p
->pByte
; break;
111 case SbxBYREF
| SbxINTEGER
:
112 case SbxBYREF
| SbxBOOL
:
113 nRes
= *p
->pInteger
; break;
114 case SbxBYREF
| SbxLONG
:
115 nRes
= *p
->pLong
; break;
116 case SbxBYREF
| SbxULONG
:
117 nRes
= *p
->pULong
; break;
118 case SbxBYREF
| SbxERROR
:
119 case SbxBYREF
| SbxUSHORT
:
120 nRes
= *p
->pUShort
; break;
121 case SbxBYREF
| SbxSINGLE
:
122 nRes
= *p
->pSingle
; break;
123 case SbxBYREF
| SbxDATE
:
124 case SbxBYREF
| SbxDOUBLE
:
125 nRes
= *p
->pDouble
; break;
126 case SbxBYREF
| SbxCURRENCY
:
127 nRes
= ImpCurrencyToDouble( *p
->pnInt64
); break;
128 case SbxBYREF
| SbxSALINT64
:
129 nRes
= static_cast< double >(*p
->pnInt64
); break;
130 case SbxBYREF
| SbxSALUINT64
:
131 nRes
= ImpSalUInt64ToDouble( *p
->puInt64
); break;
134 SbxBase::SetError( SbxERR_CONVERSION
); nRes
= 0;
139 void ImpPutDouble( SbxValues
* p
, double n
, bool bCoreString
)
145 // Here are tests necessary
147 aTmp
.pChar
= &p
->nChar
; goto direct
;
149 aTmp
.pByte
= &p
->nByte
; goto direct
;
152 aTmp
.pInteger
= &p
->nInteger
; goto direct
;
154 aTmp
.pLong
= &p
->nLong
; goto direct
;
156 aTmp
.pULong
= &p
->nULong
; goto direct
;
159 aTmp
.pUShort
= &p
->nUShort
; goto direct
;
161 aTmp
.pSingle
= &p
->nSingle
; goto direct
;
163 case SbxBYREF
| SbxDECIMAL
:
165 SbxDecimal
* pDec
= ImpCreateDecimal( p
);
166 if( !pDec
->setDouble( n
) )
167 SbxBase::SetError( SbxERR_OVERFLOW
);
171 aTmp
.eType
= SbxDataType( p
->eType
| SbxBYREF
);
172 p
= &aTmp
; goto start
;
177 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXCURR
;
179 else if( n
< SbxMINCURR
)
181 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINCURR
;
183 p
->nInt64
= ImpDoubleToCurrency( n
);
186 // from here on no longer
188 p
->nInt64
= ImpDoubleToSalInt64( n
); break;
190 p
->uInt64
= ImpDoubleToSalUInt64( n
); break;
193 p
->nDouble
= n
; break;
195 case SbxBYREF
| SbxSTRING
:
199 p
->pOUString
= new OUString
;
200 ImpCvtNum( (double) n
, 14, *p
->pOUString
, bCoreString
);
204 SbxValue
* pVal
= PTR_CAST(SbxValue
,p
->pObj
);
206 pVal
->PutDouble( n
);
208 SbxBase::SetError( SbxERR_NO_OBJECT
);
211 case SbxBYREF
| SbxCHAR
:
214 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXCHAR
;
216 else if( n
< SbxMINCHAR
)
218 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINCHAR
;
220 *p
->pChar
= (sal_Unicode
) n
; break;
221 case SbxBYREF
| SbxBYTE
:
224 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXBYTE
;
228 SbxBase::SetError( SbxERR_OVERFLOW
); n
= 0;
230 *p
->pByte
= (sal_uInt8
) n
; break;
231 case SbxBYREF
| SbxINTEGER
:
232 case SbxBYREF
| SbxBOOL
:
235 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXINT
;
237 else if( n
< SbxMININT
)
239 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMININT
;
241 *p
->pInteger
= (sal_Int16
) n
; break;
242 case SbxBYREF
| SbxERROR
:
243 case SbxBYREF
| SbxUSHORT
:
246 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXUINT
;
250 SbxBase::SetError( SbxERR_OVERFLOW
); n
= 0;
252 *p
->pUShort
= (sal_uInt16
) n
; break;
253 case SbxBYREF
| SbxLONG
:
256 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXLNG
;
258 else if( n
< SbxMINLNG
)
260 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINLNG
;
262 *p
->pLong
= (sal_Int32
) n
; break;
263 case SbxBYREF
| SbxULONG
:
266 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXULNG
;
270 SbxBase::SetError( SbxERR_OVERFLOW
); n
= 0;
272 *p
->pULong
= (sal_uInt32
) n
; break;
273 case SbxBYREF
| SbxSINGLE
:
276 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXSNG
;
278 else if( n
< SbxMINSNG
)
280 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINSNG
;
282 else if( n
> 0 && n
< SbxMAXSNG2
)
284 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXSNG2
;
286 else if( n
< 0 && n
> SbxMINSNG2
)
288 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINSNG2
;
290 *p
->pSingle
= (float) n
; break;
291 case SbxBYREF
| SbxSALINT64
:
292 *p
->pnInt64
= ImpDoubleToSalInt64( n
); break;
293 case SbxBYREF
| SbxSALUINT64
:
294 *p
->puInt64
= ImpDoubleToSalUInt64( n
); break;
295 case SbxBYREF
| SbxDATE
:
296 case SbxBYREF
| SbxDOUBLE
:
297 *p
->pDouble
= (double) n
; break;
298 case SbxBYREF
| SbxCURRENCY
:
301 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXCURR
;
303 else if( n
< SbxMINCURR
)
305 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINCURR
;
307 *p
->pnInt64
= ImpDoubleToCurrency( n
); break;
310 SbxBase::SetError( SbxERR_CONVERSION
);
314 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */