Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / basic / source / sbx / sbxdbl.cxx
blobfa6ca01d621958f694a01d7bb77974c16de9b9a5
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 <comphelper/errcode.hxx>
23 #include "sbxconv.hxx"
24 #include <runtime.hxx>
26 double ImpGetDouble( const SbxValues* p )
28 double nRes;
29 switch( +p->eType )
31 case SbxNULL:
32 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
33 [[fallthrough]];
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 || !p->pDecimal->getDouble(nRes))
64 nRes = 0.0;
65 break;
66 case SbxBYREF | SbxSTRING:
67 case SbxSTRING:
68 case SbxLPSTR:
69 if( !p->pOUString )
71 nRes = 0;
72 #if HAVE_FEATURE_SCRIPTING
73 if ( SbiRuntime::isVBAEnabled() )// VBA only behaviour
74 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
75 #endif
77 else
79 double d;
80 SbxDataType t;
81 if( ImpScan( *p->pOUString, d, t, nullptr ) != ERRCODE_NONE )
83 nRes = 0;
84 #if HAVE_FEATURE_SCRIPTING
85 if ( SbiRuntime::isVBAEnabled() )// VBA only behaviour
86 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
87 #endif
89 else
90 nRes = d;
92 break;
93 case SbxOBJECT:
95 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
96 if( pVal )
97 nRes = pVal->GetDouble();
98 else
100 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT ); nRes = 0;
102 break;
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;
131 default:
132 SbxBase::SetError( ERRCODE_BASIC_CONVERSION ); nRes = 0;
134 return nRes;
137 void ImpPutDouble( SbxValues* p, double n, bool bCoreString )
139 SbxValues aTmp;
140 start:
141 switch( +p->eType )
143 // Here are tests necessary
144 case SbxCHAR:
145 aTmp.pChar = &p->nChar; goto direct;
146 case SbxBYTE:
147 aTmp.pByte = &p->nByte; goto direct;
148 case SbxINTEGER:
149 case SbxBOOL:
150 aTmp.pInteger = &p->nInteger; goto direct;
151 case SbxLONG:
152 aTmp.pLong = &p->nLong; goto direct;
153 case SbxULONG:
154 aTmp.pULong = &p->nULong; goto direct;
155 case SbxERROR:
156 case SbxUSHORT:
157 aTmp.pUShort = &p->nUShort; goto direct;
158 case SbxSINGLE:
159 aTmp.pSingle = &p->nSingle; goto direct;
160 case SbxDECIMAL:
161 case SbxBYREF | SbxDECIMAL:
163 SbxDecimal* pDec = ImpCreateDecimal( p );
164 if( !pDec->setDouble( n ) )
165 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
166 break;
168 direct:
169 aTmp.eType = SbxDataType( p->eType | SbxBYREF );
170 p = &aTmp; goto start;
172 case SbxCURRENCY:
173 if( n > SbxMAXCURR )
175 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXCURR;
177 else if( n < SbxMINCURR )
179 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMINCURR;
181 p->nInt64 = ImpDoubleToCurrency( n );
182 break;
184 // from here on no longer
185 case SbxSALINT64:
186 p->nInt64 = ImpDoubleToSalInt64( n ); break;
187 case SbxSALUINT64:
188 p->uInt64 = ImpDoubleToSalUInt64( n ); break;
189 case SbxDATE:
190 case SbxDOUBLE:
191 p->nDouble = n; break;
193 case SbxBYREF | SbxSTRING:
194 case SbxSTRING:
195 case SbxLPSTR:
196 if( !p->pOUString )
197 p->pOUString = new OUString;
198 // tdf#107953 - show 17 significant digits
199 ImpCvtNum( n, 17, *p->pOUString, bCoreString );
200 break;
201 case SbxOBJECT:
203 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
204 if( pVal )
205 pVal->PutDouble( n );
206 else
207 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT );
208 break;
210 case SbxBYREF | SbxCHAR:
211 *p->pChar = ImpDoubleToChar(n); break;
212 case SbxBYREF | SbxBYTE:
213 *p->pByte = ImpDoubleToByte(n); break;
214 case SbxBYREF | SbxINTEGER:
215 case SbxBYREF | SbxBOOL:
216 *p->pInteger = ImpDoubleToInteger(n); break;
217 case SbxBYREF | SbxERROR:
218 case SbxBYREF | SbxUSHORT:
219 *p->pUShort = ImpDoubleToUShort(n); break;
220 case SbxBYREF | SbxLONG:
221 *p->pLong = ImpDoubleToLong(n); break;
222 case SbxBYREF | SbxULONG:
223 *p->pULong = ImpDoubleToULong(n); break;
224 case SbxBYREF | SbxSINGLE:
225 if( n > SbxMAXSNG )
227 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXSNG;
229 else if( n < SbxMINSNG )
231 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMINSNG;
233 else if( n > 0 && n < SbxMAXSNG2 )
235 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXSNG2;
237 else if( n < 0 && n > SbxMINSNG2 )
239 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMINSNG2;
241 *p->pSingle = static_cast<float>(n); break;
242 case SbxBYREF | SbxSALINT64:
243 *p->pnInt64 = ImpDoubleToSalInt64( n ); break;
244 case SbxBYREF | SbxSALUINT64:
245 *p->puInt64 = ImpDoubleToSalUInt64( n ); break;
246 case SbxBYREF | SbxDATE:
247 case SbxBYREF | SbxDOUBLE:
248 *p->pDouble = n; break;
249 case SbxBYREF | SbxCURRENCY:
250 if( n > SbxMAXCURR )
252 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXCURR;
254 else if( n < SbxMINCURR )
256 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMINCURR;
258 *p->pnInt64 = ImpDoubleToCurrency( n ); break;
260 default:
261 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
265 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */