Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / basic / source / sbx / sbxsng.cxx
blob357b9cd10f5d69f737a937be5bc42467533118c6
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 <sal/config.h>
22 #include <o3tl/float_int_conversion.hxx>
23 #include <vcl/errcode.hxx>
24 #include <basic/sberrors.hxx>
25 #include "sbxconv.hxx"
27 float ImpGetSingle( const SbxValues* p )
29 SbxValues aTmp;
30 float nRes;
31 start:
32 switch( +p->eType )
34 case SbxNULL:
35 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
36 [[fallthrough]];
37 case SbxEMPTY:
38 nRes = 0; break;
39 case SbxCHAR:
40 nRes = p->nChar; break;
41 case SbxBYTE:
42 nRes = p->nByte; break;
43 case SbxINTEGER:
44 case SbxBOOL:
45 nRes = p->nInteger; break;
46 case SbxERROR:
47 case SbxUSHORT:
48 nRes = p->nUShort; break;
49 case SbxLONG:
50 nRes = static_cast<float>(p->nLong); break;
51 case SbxULONG:
52 nRes = static_cast<float>(p->nULong); break;
53 case SbxSINGLE:
54 nRes = p->nSingle; break;
55 case SbxDECIMAL:
56 case SbxBYREF | SbxDECIMAL:
57 if( p->pDecimal )
58 p->pDecimal->getSingle( nRes );
59 else
60 nRes = 0.0;
61 break;
62 case SbxDATE:
63 case SbxDOUBLE:
64 case SbxCURRENCY:
65 case SbxSALINT64:
66 case SbxSALUINT64:
68 double dVal;
69 if( p->eType == SbxCURRENCY )
70 dVal = ImpCurrencyToDouble( p->nInt64 );
71 else if( p->eType == SbxSALINT64 )
72 dVal = static_cast<float>(p->nInt64);
73 else if( p->eType == SbxSALUINT64 )
74 dVal = static_cast<float>(p->uInt64);
75 else
76 dVal = p->nDouble;
78 if( dVal > SbxMAXSNG )
80 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
81 nRes = static_cast< float >(SbxMAXSNG);
83 else if( dVal < SbxMINSNG )
85 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
86 nRes = static_cast< float >(SbxMINSNG);
88 // tests for underflow - storing value too small for precision of single
89 else if( dVal > 0 && dVal < SbxMAXSNG2 )
91 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
92 nRes = static_cast< float >(SbxMAXSNG2);
94 else if( dVal < 0 && dVal > SbxMINSNG2 )
96 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
97 nRes = static_cast< float >(SbxMINSNG2);
99 else
100 nRes = static_cast<float>(dVal);
101 break;
103 case SbxBYREF | SbxSTRING:
104 case SbxSTRING:
105 case SbxLPSTR:
106 if( !p->pOUString )
107 nRes = 0;
108 else
110 double d;
111 SbxDataType t;
112 if( ImpScan( *p->pOUString, d, t, nullptr, false ) != ERRCODE_NONE )
113 nRes = 0;
114 else if( d > SbxMAXSNG )
116 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
117 nRes = static_cast< float >(SbxMAXSNG);
119 else if( d < SbxMINSNG )
121 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
122 nRes = static_cast< float >(SbxMINSNG);
124 else
125 nRes = static_cast<float>(d);
127 break;
128 case SbxOBJECT:
130 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
131 if( pVal )
132 nRes = pVal->GetSingle();
133 else
135 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT ); nRes = 0;
137 break;
140 case SbxBYREF | SbxCHAR:
141 nRes = *p->pChar; break;
142 case SbxBYREF | SbxBYTE:
143 nRes = *p->pByte; break;
144 case SbxBYREF | SbxINTEGER:
145 case SbxBYREF | SbxBOOL:
146 nRes = *p->pInteger; break;
147 case SbxBYREF | SbxLONG:
148 nRes = static_cast<float>(*p->pLong); break;
149 case SbxBYREF | SbxULONG:
150 nRes = static_cast<float>(*p->pULong); break;
151 case SbxBYREF | SbxERROR:
152 case SbxBYREF | SbxUSHORT:
153 nRes = *p->pUShort; break;
154 case SbxBYREF | SbxSINGLE:
155 nRes = *p->pSingle; break;
156 // from here had to be tested
157 case SbxBYREF | SbxDATE:
158 case SbxBYREF | SbxDOUBLE:
159 aTmp.nDouble = *p->pDouble; goto ref;
160 case SbxBYREF | SbxSALINT64:
161 case SbxBYREF | SbxCURRENCY:
162 aTmp.nInt64 = *p->pnInt64; goto ref;
163 case SbxBYREF | SbxSALUINT64:
164 aTmp.uInt64 = *p->puInt64; goto ref;
165 ref:
166 aTmp.eType = SbxDataType( p->eType & 0x0FFF );
167 p = &aTmp; goto start;
169 default:
170 SbxBase::SetError( ERRCODE_BASIC_CONVERSION ); nRes = 0;
172 return nRes;
175 void ImpPutSingle( SbxValues* p, float n )
177 SbxValues aTmp;
178 start:
179 switch( +p->eType )
181 case SbxCHAR:
182 aTmp.pChar = &p->nChar; goto direct;
183 case SbxBYTE:
184 aTmp.pByte = &p->nByte; goto direct;
185 case SbxINTEGER:
186 case SbxBOOL:
187 aTmp.pInteger = &p->nInteger; goto direct;
188 case SbxLONG:
189 aTmp.pLong = &p->nLong; goto direct;
190 case SbxULONG:
191 aTmp.pULong = &p->nULong; goto direct;
192 case SbxERROR:
193 case SbxUSHORT:
194 aTmp.pUShort = &p->nUShort; goto direct;
195 case SbxCURRENCY:
196 case SbxSALINT64:
197 aTmp.pnInt64 = &p->nInt64; goto direct;
198 case SbxSALUINT64:
199 aTmp.puInt64 = &p->uInt64; goto direct;
200 case SbxDECIMAL:
201 case SbxBYREF | SbxDECIMAL:
203 SbxDecimal* pDec = ImpCreateDecimal( p );
204 if( !pDec->setSingle( n ) )
205 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
206 break;
208 direct:
209 aTmp.eType = SbxDataType( p->eType | SbxBYREF );
210 p = &aTmp; goto start;
212 // from here no tests
213 case SbxSINGLE:
214 p->nSingle = n; break;
215 case SbxDATE:
216 case SbxDOUBLE:
217 p->nDouble = n; break;
219 case SbxBYREF | SbxSTRING:
220 case SbxSTRING:
221 case SbxLPSTR:
223 if( !p->pOUString )
224 p->pOUString = new OUString;
225 ImpCvtNum( static_cast<double>(n), 6, *p->pOUString );
226 break;
228 case SbxOBJECT:
230 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
231 if( pVal )
232 pVal->PutSingle( n );
233 else
234 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT );
235 break;
237 case SbxBYREF | SbxCHAR:
238 if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXCHAR) )
240 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXCHAR;
242 else if( !o3tl::convertsToAtLeast(o3tl::roundAway(n), SbxMINCHAR) )
244 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMINCHAR;
246 *p->pChar = static_cast<sal_Unicode>(n); break;
247 case SbxBYREF | SbxBYTE:
248 if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXBYTE) )
250 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXBYTE;
252 else if( n < 0 )
254 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 0;
256 *p->pByte = static_cast<sal_uInt8>(n); break;
257 case SbxBYREF | SbxINTEGER:
258 case SbxBYREF | SbxBOOL:
259 if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXINT) )
261 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXINT;
263 else if( !o3tl::convertsToAtLeast(o3tl::roundAway(n), SbxMININT) )
265 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMININT;
267 *p->pInteger = static_cast<sal_Int16>(n); break;
268 case SbxBYREF | SbxERROR:
269 case SbxBYREF | SbxUSHORT:
270 if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXUINT) )
272 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXUINT;
274 else if( n < 0 )
276 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 0;
278 *p->pUShort = static_cast<sal_uInt16>(n); break;
279 case SbxBYREF | SbxLONG:
281 sal_Int32 i;
282 if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXLNG) )
284 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); i = SbxMAXLNG;
286 else if( !o3tl::convertsToAtLeast(o3tl::roundAway(n), SbxMINLNG) )
288 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); i = SbxMINLNG;
290 else
292 i = sal::static_int_cast< sal_Int32 >(n);
294 *p->pLong = i; break;
296 case SbxBYREF | SbxULONG:
298 sal_uInt32 i;
299 if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXULNG) )
301 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); i = SbxMAXULNG;
303 else if( n < 0 )
305 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); i = 0;
307 else
309 i = sal::static_int_cast< sal_uInt32 >(n);
311 *p->pULong = i; break;
313 case SbxBYREF | SbxSINGLE:
314 *p->pSingle = n; break;
315 case SbxBYREF | SbxDATE:
316 case SbxBYREF | SbxDOUBLE:
317 *p->pDouble = static_cast<double>(n); break;
318 case SbxBYREF | SbxSALINT64:
319 *p->pnInt64 = static_cast<sal_Int64>(n); break;
320 case SbxBYREF | SbxSALUINT64:
321 *p->puInt64 = static_cast<sal_uInt64>(n); break;
322 case SbxBYREF | SbxCURRENCY:
323 double d;
324 if( n > SbxMAXCURR )
326 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); d = SbxMAXCURR;
328 else if( n < SbxMINCURR )
330 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); d = SbxMINCURR;
332 else
334 d = n;
336 *p->pnInt64 = ImpDoubleToCurrency( d ); break;
338 default:
339 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
343 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */