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"
23 #include "runtime.hxx"
25 double ImpGetDouble( const SbxValues
* p
)
31 SbxBase::SetError( SbxERR_CONVERSION
);
35 nRes
= p
->nChar
; break;
37 nRes
= p
->nByte
; break;
40 nRes
= p
->nInteger
; break;
43 nRes
= p
->nUShort
; break;
45 nRes
= p
->nLong
; break;
47 nRes
= p
->nULong
; break;
49 nRes
= p
->nSingle
; break;
52 nRes
= p
->nDouble
; break;
54 nRes
= ImpCurrencyToDouble( p
->nInt64
); break;
56 nRes
= static_cast< double >(p
->nInt64
); break;
58 nRes
= ImpSalUInt64ToDouble( p
->uInt64
); break;
60 case SbxBYREF
| SbxDECIMAL
:
62 p
->pDecimal
->getDouble( nRes
);
66 case SbxBYREF
| SbxSTRING
:
72 #ifndef DISABLE_SCRIPTING
73 if ( SbiRuntime::isVBAEnabled() )// VBA only behaviour
74 SbxBase::SetError( SbxERR_CONVERSION
);
81 if( ImpScan( *p
->pOUString
, d
, t
, NULL
) != SbxERR_OK
)
84 #ifndef DISABLE_SCRIPTING
85 if ( SbiRuntime::isVBAEnabled() )// VBA only behaviour
86 SbxBase::SetError( SbxERR_CONVERSION
);
95 SbxValue
* pVal
= PTR_CAST(SbxValue
,p
->pObj
);
97 nRes
= pVal
->GetDouble();
100 SbxBase::SetError( SbxERR_NO_OBJECT
); nRes
= 0;
105 case SbxBYREF
| SbxCHAR
:
106 nRes
= *p
->pChar
; break;
107 case SbxBYREF
| SbxBYTE
:
108 nRes
= *p
->pByte
; break;
109 case SbxBYREF
| SbxINTEGER
:
110 case SbxBYREF
| SbxBOOL
:
111 nRes
= *p
->pInteger
; break;
112 case SbxBYREF
| SbxLONG
:
113 nRes
= *p
->pLong
; break;
114 case SbxBYREF
| SbxULONG
:
115 nRes
= *p
->pULong
; break;
116 case SbxBYREF
| SbxERROR
:
117 case SbxBYREF
| SbxUSHORT
:
118 nRes
= *p
->pUShort
; break;
119 case SbxBYREF
| SbxSINGLE
:
120 nRes
= *p
->pSingle
; break;
121 case SbxBYREF
| SbxDATE
:
122 case SbxBYREF
| SbxDOUBLE
:
123 nRes
= *p
->pDouble
; break;
124 case SbxBYREF
| SbxCURRENCY
:
125 nRes
= ImpCurrencyToDouble( *p
->pnInt64
); break;
126 case SbxBYREF
| SbxSALINT64
:
127 nRes
= static_cast< double >(*p
->pnInt64
); break;
128 case SbxBYREF
| SbxSALUINT64
:
129 nRes
= ImpSalUInt64ToDouble( *p
->puInt64
); break;
132 SbxBase::SetError( SbxERR_CONVERSION
); nRes
= 0;
137 void ImpPutDouble( SbxValues
* p
, double n
, bool bCoreString
)
143 // Here are tests necessary
145 aTmp
.pChar
= &p
->nChar
; goto direct
;
147 aTmp
.pByte
= &p
->nByte
; goto direct
;
150 aTmp
.pInteger
= &p
->nInteger
; goto direct
;
152 aTmp
.pLong
= &p
->nLong
; goto direct
;
154 aTmp
.pULong
= &p
->nULong
; goto direct
;
157 aTmp
.pUShort
= &p
->nUShort
; goto direct
;
159 aTmp
.pSingle
= &p
->nSingle
; goto direct
;
161 case SbxBYREF
| SbxDECIMAL
:
163 SbxDecimal
* pDec
= ImpCreateDecimal( p
);
164 if( !pDec
->setDouble( n
) )
165 SbxBase::SetError( SbxERR_OVERFLOW
);
169 aTmp
.eType
= SbxDataType( p
->eType
| SbxBYREF
);
170 p
= &aTmp
; goto start
;
175 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXCURR
;
177 else if( n
< SbxMINCURR
)
179 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINCURR
;
181 p
->nInt64
= ImpDoubleToCurrency( n
);
184 // from here on no longer
186 p
->nInt64
= ImpDoubleToSalInt64( n
); break;
188 p
->uInt64
= ImpDoubleToSalUInt64( n
); break;
191 p
->nDouble
= n
; break;
193 case SbxBYREF
| SbxSTRING
:
197 p
->pOUString
= new ::rtl::OUString
;
198 ImpCvtNum( (double) n
, 14, *p
->pOUString
, bCoreString
);
202 SbxValue
* pVal
= PTR_CAST(SbxValue
,p
->pObj
);
204 pVal
->PutDouble( n
);
206 SbxBase::SetError( SbxERR_NO_OBJECT
);
209 case SbxBYREF
| SbxCHAR
:
212 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXCHAR
;
214 else if( n
< SbxMINCHAR
)
216 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINCHAR
;
218 *p
->pChar
= (sal_Unicode
) n
; break;
219 case SbxBYREF
| SbxBYTE
:
222 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXBYTE
;
226 SbxBase::SetError( SbxERR_OVERFLOW
); n
= 0;
228 *p
->pByte
= (sal_uInt8
) n
; break;
229 case SbxBYREF
| SbxINTEGER
:
230 case SbxBYREF
| SbxBOOL
:
233 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXINT
;
235 else if( n
< SbxMININT
)
237 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMININT
;
239 *p
->pInteger
= (sal_Int16
) n
; break;
240 case SbxBYREF
| SbxERROR
:
241 case SbxBYREF
| SbxUSHORT
:
244 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXUINT
;
248 SbxBase::SetError( SbxERR_OVERFLOW
); n
= 0;
250 *p
->pUShort
= (sal_uInt16
) n
; break;
251 case SbxBYREF
| SbxLONG
:
254 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXLNG
;
256 else if( n
< SbxMINLNG
)
258 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINLNG
;
260 *p
->pLong
= (sal_Int32
) n
; break;
261 case SbxBYREF
| SbxULONG
:
264 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXULNG
;
268 SbxBase::SetError( SbxERR_OVERFLOW
); n
= 0;
270 *p
->pULong
= (sal_uInt32
) n
; break;
271 case SbxBYREF
| SbxSINGLE
:
274 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXSNG
;
276 else if( n
< SbxMINSNG
)
278 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINSNG
;
280 else if( n
> 0 && n
< SbxMAXSNG2
)
282 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXSNG2
;
284 else if( n
< 0 && n
> SbxMINSNG2
)
286 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINSNG2
;
288 *p
->pSingle
= (float) n
; break;
289 case SbxBYREF
| SbxSALINT64
:
290 *p
->pnInt64
= ImpDoubleToSalInt64( n
); break;
291 case SbxBYREF
| SbxSALUINT64
:
292 *p
->puInt64
= ImpDoubleToSalUInt64( n
); break;
293 case SbxBYREF
| SbxDATE
:
294 case SbxBYREF
| SbxDOUBLE
:
295 *p
->pDouble
= (double) n
; break;
296 case SbxBYREF
| SbxCURRENCY
:
299 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXCURR
;
301 else if( n
< SbxMINCURR
)
303 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINCURR
;
305 *p
->pnInt64
= ImpDoubleToCurrency( n
); break;
308 SbxBase::SetError( SbxERR_CONVERSION
);
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */