Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / basic / source / sbx / sbxdbl.cxx
blobc68d2909304b3a54e64eee603694fd99d9cf6e56
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 <o3tl/float_int_conversion.hxx>
23 #include <vcl/errcode.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( ERRCODE_BASIC_CONVERSION );
34 [[fallthrough]];
35 case SbxEMPTY:
36 nRes = 0; break;
37 case SbxCHAR:
38 nRes = p->nChar; break;
39 case SbxBYTE:
40 nRes = p->nByte; break;
41 case SbxINTEGER:
42 case SbxBOOL:
43 nRes = p->nInteger; break;
44 case SbxERROR:
45 case SbxUSHORT:
46 nRes = p->nUShort; break;
47 case SbxLONG:
48 nRes = p->nLong; break;
49 case SbxULONG:
50 nRes = p->nULong; break;
51 case SbxSINGLE:
52 nRes = p->nSingle; break;
53 case SbxDATE:
54 case SbxDOUBLE:
55 nRes = p->nDouble; break;
56 case SbxCURRENCY:
57 nRes = ImpCurrencyToDouble( p->nInt64 ); break;
58 case SbxSALINT64:
59 nRes = static_cast< double >(p->nInt64); break;
60 case SbxSALUINT64:
61 nRes = ImpSalUInt64ToDouble( p->uInt64 ); break;
62 case SbxDECIMAL:
63 case SbxBYREF | SbxDECIMAL:
64 if( p->pDecimal )
65 p->pDecimal->getDouble( nRes );
66 else
67 nRes = 0.0;
68 break;
69 case SbxBYREF | SbxSTRING:
70 case SbxSTRING:
71 case SbxLPSTR:
72 if( !p->pOUString )
74 nRes = 0;
75 #if HAVE_FEATURE_SCRIPTING
76 if ( SbiRuntime::isVBAEnabled() )// VBA only behaviour
77 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
78 #endif
80 else
82 double d;
83 SbxDataType t;
84 if( ImpScan( *p->pOUString, d, t, nullptr, false ) != ERRCODE_NONE )
86 nRes = 0;
87 #if HAVE_FEATURE_SCRIPTING
88 if ( SbiRuntime::isVBAEnabled() )// VBA only behaviour
89 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
90 #endif
92 else
93 nRes = d;
95 break;
96 case SbxOBJECT:
98 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
99 if( pVal )
100 nRes = pVal->GetDouble();
101 else
103 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT ); nRes = 0;
105 break;
108 case SbxBYREF | SbxCHAR:
109 nRes = *p->pChar; break;
110 case SbxBYREF | SbxBYTE:
111 nRes = *p->pByte; break;
112 case SbxBYREF | SbxINTEGER:
113 case SbxBYREF | SbxBOOL:
114 nRes = *p->pInteger; break;
115 case SbxBYREF | SbxLONG:
116 nRes = *p->pLong; break;
117 case SbxBYREF | SbxULONG:
118 nRes = *p->pULong; break;
119 case SbxBYREF | SbxERROR:
120 case SbxBYREF | SbxUSHORT:
121 nRes = *p->pUShort; break;
122 case SbxBYREF | SbxSINGLE:
123 nRes = *p->pSingle; break;
124 case SbxBYREF | SbxDATE:
125 case SbxBYREF | SbxDOUBLE:
126 nRes = *p->pDouble; break;
127 case SbxBYREF | SbxCURRENCY:
128 nRes = ImpCurrencyToDouble( *p->pnInt64 ); break;
129 case SbxBYREF | SbxSALINT64:
130 nRes = static_cast< double >(*p->pnInt64); break;
131 case SbxBYREF | SbxSALUINT64:
132 nRes = ImpSalUInt64ToDouble( *p->puInt64 ); break;
134 default:
135 SbxBase::SetError( ERRCODE_BASIC_CONVERSION ); nRes = 0;
137 return nRes;
140 void ImpPutDouble( SbxValues* p, double n, bool bCoreString )
142 SbxValues aTmp;
143 start:
144 switch( +p->eType )
146 // Here are tests necessary
147 case SbxCHAR:
148 aTmp.pChar = &p->nChar; goto direct;
149 case SbxBYTE:
150 aTmp.pByte = &p->nByte; goto direct;
151 case SbxINTEGER:
152 case SbxBOOL:
153 aTmp.pInteger = &p->nInteger; goto direct;
154 case SbxLONG:
155 aTmp.pLong = &p->nLong; goto direct;
156 case SbxULONG:
157 aTmp.pULong = &p->nULong; goto direct;
158 case SbxERROR:
159 case SbxUSHORT:
160 aTmp.pUShort = &p->nUShort; goto direct;
161 case SbxSINGLE:
162 aTmp.pSingle = &p->nSingle; goto direct;
163 case SbxDECIMAL:
164 case SbxBYREF | SbxDECIMAL:
166 SbxDecimal* pDec = ImpCreateDecimal( p );
167 if( !pDec->setDouble( n ) )
168 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
169 break;
171 direct:
172 aTmp.eType = SbxDataType( p->eType | SbxBYREF );
173 p = &aTmp; goto start;
175 case SbxCURRENCY:
176 if( n > SbxMAXCURR )
178 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXCURR;
180 else if( n < SbxMINCURR )
182 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMINCURR;
184 p->nInt64 = ImpDoubleToCurrency( n );
185 break;
187 // from here on no longer
188 case SbxSALINT64:
189 p->nInt64 = ImpDoubleToSalInt64( n ); break;
190 case SbxSALUINT64:
191 p->uInt64 = ImpDoubleToSalUInt64( n ); break;
192 case SbxDATE:
193 case SbxDOUBLE:
194 p->nDouble = n; break;
196 case SbxBYREF | SbxSTRING:
197 case SbxSTRING:
198 case SbxLPSTR:
199 if( !p->pOUString )
200 p->pOUString = new OUString;
201 ImpCvtNum( n, 14, *p->pOUString, bCoreString );
202 break;
203 case SbxOBJECT:
205 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
206 if( pVal )
207 pVal->PutDouble( n );
208 else
209 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT );
210 break;
212 case SbxBYREF | SbxCHAR:
213 if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXCHAR) )
215 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXCHAR;
217 else if( !o3tl::convertsToAtLeast(o3tl::roundAway(n), SbxMINCHAR) )
219 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMINCHAR;
221 *p->pChar = static_cast<sal_Unicode>(n); break;
222 case SbxBYREF | SbxBYTE:
223 if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXBYTE) )
225 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXBYTE;
227 else if( n < 0 )
229 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 0;
231 *p->pByte = static_cast<sal_uInt8>(n); break;
232 case SbxBYREF | SbxINTEGER:
233 case SbxBYREF | SbxBOOL:
234 if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXINT) )
236 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXINT;
238 else if( !o3tl::convertsToAtLeast(o3tl::roundAway(n), SbxMININT) )
240 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMININT;
242 *p->pInteger = static_cast<sal_Int16>(n); break;
243 case SbxBYREF | SbxERROR:
244 case SbxBYREF | SbxUSHORT:
245 if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXUINT) )
247 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXUINT;
249 else if( n < 0 )
251 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 0;
253 *p->pUShort = static_cast<sal_uInt16>(n); break;
254 case SbxBYREF | SbxLONG:
255 if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXLNG) )
257 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXLNG;
259 else if( !o3tl::convertsToAtLeast(o3tl::roundAway(n), SbxMINLNG) )
261 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMINLNG;
263 *p->pLong = static_cast<sal_Int32>(n); break;
264 case SbxBYREF | SbxULONG:
265 if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXULNG) )
267 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXULNG;
269 else if( n < 0 )
271 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 0;
273 *p->pULong = static_cast<sal_uInt32>(n); break;
274 case SbxBYREF | SbxSINGLE:
275 if( n > SbxMAXSNG )
277 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXSNG;
279 else if( n < SbxMINSNG )
281 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMINSNG;
283 else if( n > 0 && n < SbxMAXSNG2 )
285 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXSNG2;
287 else if( n < 0 && n > SbxMINSNG2 )
289 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMINSNG2;
291 *p->pSingle = static_cast<float>(n); break;
292 case SbxBYREF | SbxSALINT64:
293 *p->pnInt64 = ImpDoubleToSalInt64( n ); break;
294 case SbxBYREF | SbxSALUINT64:
295 *p->puInt64 = ImpDoubleToSalUInt64( n ); break;
296 case SbxBYREF | SbxDATE:
297 case SbxBYREF | SbxDOUBLE:
298 *p->pDouble = n; break;
299 case SbxBYREF | SbxCURRENCY:
300 if( n > SbxMAXCURR )
302 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXCURR;
304 else if( n < SbxMINCURR )
306 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMINCURR;
308 *p->pnInt64 = ImpDoubleToCurrency( n ); break;
310 default:
311 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */