bump product version to 5.0.4.1
[LibreOffice.git] / basic / source / sbx / sbxdbl.cxx
blobe251336c62c853cd4783fa99258903d329309d0c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 )
29 double nRes;
30 switch( +p->eType )
32 case SbxNULL:
33 SbxBase::SetError( SbxERR_CONVERSION );
34 case SbxEMPTY:
35 nRes = 0; break;
36 case SbxCHAR:
37 nRes = p->nChar; break;
38 case SbxBYTE:
39 nRes = p->nByte; break;
40 case SbxINTEGER:
41 case SbxBOOL:
42 nRes = p->nInteger; break;
43 case SbxERROR:
44 case SbxUSHORT:
45 nRes = p->nUShort; break;
46 case SbxLONG:
47 nRes = p->nLong; break;
48 case SbxULONG:
49 nRes = p->nULong; break;
50 case SbxSINGLE:
51 nRes = p->nSingle; break;
52 case SbxDATE:
53 case SbxDOUBLE:
54 nRes = p->nDouble; break;
55 case SbxCURRENCY:
56 nRes = ImpCurrencyToDouble( p->nInt64 ); break;
57 case SbxSALINT64:
58 nRes = static_cast< double >(p->nInt64); break;
59 case SbxSALUINT64:
60 nRes = ImpSalUInt64ToDouble( p->uInt64 ); break;
61 case SbxDECIMAL:
62 case SbxBYREF | SbxDECIMAL:
63 if( p->pDecimal )
64 p->pDecimal->getDouble( nRes );
65 else
66 nRes = 0.0;
67 break;
68 case SbxBYREF | SbxSTRING:
69 case SbxSTRING:
70 case SbxLPSTR:
71 if( !p->pOUString )
73 nRes = 0;
74 #if HAVE_FEATURE_SCRIPTING
75 if ( SbiRuntime::isVBAEnabled() )// VBA only behaviour
76 SbxBase::SetError( SbxERR_CONVERSION );
77 #endif
79 else
81 double d;
82 SbxDataType t;
83 if( ImpScan( *p->pOUString, d, t, NULL ) != SbxERR_OK )
85 nRes = 0;
86 #if HAVE_FEATURE_SCRIPTING
87 if ( SbiRuntime::isVBAEnabled() )// VBA only behaviour
88 SbxBase::SetError( SbxERR_CONVERSION );
89 #endif
91 else
92 nRes = d;
94 break;
95 case SbxOBJECT:
97 SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
98 if( pVal )
99 nRes = pVal->GetDouble();
100 else
102 SbxBase::SetError( SbxERR_NO_OBJECT ); nRes = 0;
104 break;
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;
133 default:
134 SbxBase::SetError( SbxERR_CONVERSION ); nRes = 0;
136 return nRes;
139 void ImpPutDouble( SbxValues* p, double n, bool bCoreString )
141 SbxValues aTmp;
142 start:
143 switch( +p->eType )
145 // Here are tests necessary
146 case SbxCHAR:
147 aTmp.pChar = &p->nChar; goto direct;
148 case SbxBYTE:
149 aTmp.pByte = &p->nByte; goto direct;
150 case SbxINTEGER:
151 case SbxBOOL:
152 aTmp.pInteger = &p->nInteger; goto direct;
153 case SbxLONG:
154 aTmp.pLong = &p->nLong; goto direct;
155 case SbxULONG:
156 aTmp.pULong = &p->nULong; goto direct;
157 case SbxERROR:
158 case SbxUSHORT:
159 aTmp.pUShort = &p->nUShort; goto direct;
160 case SbxSINGLE:
161 aTmp.pSingle = &p->nSingle; goto direct;
162 case SbxDECIMAL:
163 case SbxBYREF | SbxDECIMAL:
165 SbxDecimal* pDec = ImpCreateDecimal( p );
166 if( !pDec->setDouble( n ) )
167 SbxBase::SetError( SbxERR_OVERFLOW );
168 break;
170 direct:
171 aTmp.eType = SbxDataType( p->eType | SbxBYREF );
172 p = &aTmp; goto start;
174 case SbxCURRENCY:
175 if( n > SbxMAXCURR )
177 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXCURR;
179 else if( n < SbxMINCURR )
181 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINCURR;
183 p->nInt64 = ImpDoubleToCurrency( n );
184 break;
186 // from here on no longer
187 case SbxSALINT64:
188 p->nInt64 = ImpDoubleToSalInt64( n ); break;
189 case SbxSALUINT64:
190 p->uInt64 = ImpDoubleToSalUInt64( n ); break;
191 case SbxDATE:
192 case SbxDOUBLE:
193 p->nDouble = n; break;
195 case SbxBYREF | SbxSTRING:
196 case SbxSTRING:
197 case SbxLPSTR:
198 if( !p->pOUString )
199 p->pOUString = new OUString;
200 ImpCvtNum( (double) n, 14, *p->pOUString, bCoreString );
201 break;
202 case SbxOBJECT:
204 SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
205 if( pVal )
206 pVal->PutDouble( n );
207 else
208 SbxBase::SetError( SbxERR_NO_OBJECT );
209 break;
211 case SbxBYREF | SbxCHAR:
212 if( n > SbxMAXCHAR )
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:
222 if( n > SbxMAXBYTE )
224 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXBYTE;
226 else if( n < 0 )
228 SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
230 *p->pByte = (sal_uInt8) n; break;
231 case SbxBYREF | SbxINTEGER:
232 case SbxBYREF | SbxBOOL:
233 if( n > SbxMAXINT )
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:
244 if( n > SbxMAXUINT )
246 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXUINT;
248 else if( n < 0 )
250 SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
252 *p->pUShort = (sal_uInt16) n; break;
253 case SbxBYREF | SbxLONG:
254 if( n > SbxMAXLNG )
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:
264 if( n > SbxMAXULNG )
266 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXULNG;
268 else if( n < 0 )
270 SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
272 *p->pULong = (sal_uInt32) n; break;
273 case SbxBYREF | SbxSINGLE:
274 if( n > SbxMAXSNG )
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:
299 if( n > SbxMAXCURR )
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;
309 default:
310 SbxBase::SetError( SbxERR_CONVERSION );
314 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */