xmlsecurity: fix --without-system-nss build
[LibreOffice.git] / basic / source / sbx / sbxsng.cxx
blob706cab89a3a4fec76e9b8cac818426ac7eb7f28c
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 <comphelper/errcode.hxx>
23 #include <basic/sberrors.hxx>
24 #include "sbxconv.hxx"
26 float ImpGetSingle( const SbxValues* p )
28 SbxValues aTmp;
29 float nRes;
30 start:
31 switch( +p->eType )
33 case SbxNULL:
34 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
35 [[fallthrough]];
36 case SbxEMPTY:
37 nRes = 0; break;
38 case SbxCHAR:
39 nRes = p->nChar; break;
40 case SbxBYTE:
41 nRes = p->nByte; break;
42 case SbxINTEGER:
43 case SbxBOOL:
44 nRes = p->nInteger; break;
45 case SbxERROR:
46 case SbxUSHORT:
47 nRes = p->nUShort; break;
48 case SbxLONG:
49 nRes = static_cast<float>(p->nLong); break;
50 case SbxULONG:
51 nRes = static_cast<float>(p->nULong); break;
52 case SbxSINGLE:
53 nRes = p->nSingle; break;
54 case SbxDECIMAL:
55 case SbxBYREF | SbxDECIMAL:
56 if (!p->pDecimal || !p->pDecimal->getSingle(nRes))
57 nRes = 0.0;
58 break;
59 case SbxDATE:
60 case SbxDOUBLE:
61 case SbxCURRENCY:
62 case SbxSALINT64:
63 case SbxSALUINT64:
65 double dVal;
66 if( p->eType == SbxCURRENCY )
67 dVal = ImpCurrencyToDouble( p->nInt64 );
68 else if( p->eType == SbxSALINT64 )
69 dVal = static_cast<float>(p->nInt64);
70 else if( p->eType == SbxSALUINT64 )
71 dVal = static_cast<float>(p->uInt64);
72 else
73 dVal = p->nDouble;
75 if( dVal > SbxMAXSNG )
77 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
78 nRes = static_cast< float >(SbxMAXSNG);
80 else if( dVal < SbxMINSNG )
82 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
83 nRes = static_cast< float >(SbxMINSNG);
85 // tests for underflow - storing value too small for precision of single
86 else if( dVal > 0 && dVal < SbxMAXSNG2 )
88 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
89 nRes = static_cast< float >(SbxMAXSNG2);
91 else if( dVal < 0 && dVal > SbxMINSNG2 )
93 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
94 nRes = static_cast< float >(SbxMINSNG2);
96 else
97 nRes = static_cast<float>(dVal);
98 break;
100 case SbxBYREF | SbxSTRING:
101 case SbxSTRING:
102 case SbxLPSTR:
103 if( !p->pOUString )
104 nRes = 0;
105 else
107 double d;
108 SbxDataType t;
109 if( ImpScan( *p->pOUString, d, t, nullptr ) != ERRCODE_NONE )
110 nRes = 0;
111 else if( d > SbxMAXSNG )
113 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
114 nRes = static_cast< float >(SbxMAXSNG);
116 else if( d < SbxMINSNG )
118 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
119 nRes = static_cast< float >(SbxMINSNG);
121 else
122 nRes = static_cast<float>(d);
124 break;
125 case SbxOBJECT:
127 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
128 if( pVal )
129 nRes = pVal->GetSingle();
130 else
132 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT ); nRes = 0;
134 break;
137 case SbxBYREF | SbxCHAR:
138 nRes = *p->pChar; break;
139 case SbxBYREF | SbxBYTE:
140 nRes = *p->pByte; break;
141 case SbxBYREF | SbxINTEGER:
142 case SbxBYREF | SbxBOOL:
143 nRes = *p->pInteger; break;
144 case SbxBYREF | SbxLONG:
145 nRes = static_cast<float>(*p->pLong); break;
146 case SbxBYREF | SbxULONG:
147 nRes = static_cast<float>(*p->pULong); break;
148 case SbxBYREF | SbxERROR:
149 case SbxBYREF | SbxUSHORT:
150 nRes = *p->pUShort; break;
151 case SbxBYREF | SbxSINGLE:
152 nRes = *p->pSingle; break;
153 // from here had to be tested
154 case SbxBYREF | SbxDATE:
155 case SbxBYREF | SbxDOUBLE:
156 aTmp.nDouble = *p->pDouble; goto ref;
157 case SbxBYREF | SbxSALINT64:
158 case SbxBYREF | SbxCURRENCY:
159 aTmp.nInt64 = *p->pnInt64; goto ref;
160 case SbxBYREF | SbxSALUINT64:
161 aTmp.uInt64 = *p->puInt64; goto ref;
162 ref:
163 aTmp.eType = SbxDataType( p->eType & 0x0FFF );
164 p = &aTmp; goto start;
166 default:
167 SbxBase::SetError( ERRCODE_BASIC_CONVERSION ); nRes = 0;
169 return nRes;
172 void ImpPutSingle( SbxValues* p, float n )
174 SbxValues aTmp;
175 start:
176 switch( +p->eType )
178 case SbxCHAR:
179 aTmp.pChar = &p->nChar; goto direct;
180 case SbxBYTE:
181 aTmp.pByte = &p->nByte; goto direct;
182 case SbxINTEGER:
183 case SbxBOOL:
184 aTmp.pInteger = &p->nInteger; goto direct;
185 case SbxLONG:
186 aTmp.pLong = &p->nLong; goto direct;
187 case SbxULONG:
188 aTmp.pULong = &p->nULong; goto direct;
189 case SbxERROR:
190 case SbxUSHORT:
191 aTmp.pUShort = &p->nUShort; goto direct;
192 case SbxCURRENCY:
193 case SbxSALINT64:
194 aTmp.pnInt64 = &p->nInt64; goto direct;
195 case SbxSALUINT64:
196 aTmp.puInt64 = &p->uInt64; goto direct;
197 case SbxDECIMAL:
198 case SbxBYREF | SbxDECIMAL:
200 SbxDecimal* pDec = ImpCreateDecimal( p );
201 if( !pDec->setSingle( n ) )
202 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
203 break;
205 direct:
206 aTmp.eType = SbxDataType( p->eType | SbxBYREF );
207 p = &aTmp; goto start;
209 // from here no tests
210 case SbxSINGLE:
211 p->nSingle = n; break;
212 case SbxDATE:
213 case SbxDOUBLE:
214 p->nDouble = n; break;
216 case SbxBYREF | SbxSTRING:
217 case SbxSTRING:
218 case SbxLPSTR:
220 if( !p->pOUString )
221 p->pOUString = new OUString;
222 // tdf#107953 - show 9 significant digits
223 ImpCvtNum( static_cast<double>(n), 9, *p->pOUString );
224 break;
226 case SbxOBJECT:
228 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
229 if( pVal )
230 pVal->PutSingle( n );
231 else
232 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT );
233 break;
235 case SbxBYREF | SbxCHAR:
236 *p->pChar = ImpDoubleToChar(n); break;
237 case SbxBYREF | SbxBYTE:
238 *p->pByte = ImpDoubleToByte(n); break;
239 case SbxBYREF | SbxINTEGER:
240 case SbxBYREF | SbxBOOL:
241 *p->pInteger = ImpDoubleToInteger(n); break;
242 case SbxBYREF | SbxERROR:
243 case SbxBYREF | SbxUSHORT:
244 *p->pUShort = ImpDoubleToUShort(n); break;
245 case SbxBYREF | SbxLONG:
246 *p->pLong = ImpDoubleToLong(n); break;
247 case SbxBYREF | SbxULONG:
248 *p->pULong = ImpDoubleToULong(n); break;
249 case SbxBYREF | SbxSINGLE:
250 *p->pSingle = n; break;
251 case SbxBYREF | SbxDATE:
252 case SbxBYREF | SbxDOUBLE:
253 *p->pDouble = static_cast<double>(n); break;
254 case SbxBYREF | SbxSALINT64:
255 *p->pnInt64 = ImpDoubleToSalInt64(n); break;
256 case SbxBYREF | SbxSALUINT64:
257 *p->puInt64 = ImpDoubleToSalUInt64(n); break;
258 case SbxBYREF | SbxCURRENCY:
259 double d;
260 if( n > SbxMAXCURR )
262 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); d = SbxMAXCURR;
264 else if( n < SbxMINCURR )
266 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); d = SbxMINCURR;
268 else
270 d = n;
272 *p->pnInt64 = ImpDoubleToCurrency( d ); break;
274 default:
275 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
279 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */