Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / basic / source / sbx / sbxulng.cxx
blob14b37958bba2eb70bd0e591dc22435857e81cf7c
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 sal_uInt32 ImpGetULong( const SbxValues* p )
29 SbxValues aTmp;
30 sal_uInt32 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;
41 break;
42 case SbxBYTE:
43 nRes = p->nByte; break;
44 case SbxINTEGER:
45 case SbxBOOL:
46 if( p->nInteger < 0 )
48 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
50 else
51 nRes = p->nInteger;
52 break;
53 case SbxERROR:
54 case SbxUSHORT:
55 nRes = p->nUShort;
56 break;
57 case SbxLONG:
58 if( p->nLong < 0 )
60 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
62 else
63 nRes = p->nLong;
64 break;
65 case SbxULONG:
66 nRes = p->nULong; break;
67 case SbxSINGLE:
68 if( !o3tl::convertsToAtMost(o3tl::roundAway(p->nSingle), SbxMAXULNG) )
70 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXULNG;
72 else if( p->nSingle < 0 )
74 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
76 else
77 nRes = static_cast<sal_uInt32>( p->nSingle + 0.5 );
78 break;
79 case SbxDATE:
80 case SbxDOUBLE:
81 case SbxSALINT64:
82 case SbxSALUINT64:
83 case SbxCURRENCY:
84 case SbxDECIMAL:
85 case SbxBYREF | SbxDECIMAL:
87 double dVal;
88 if( p->eType == SbxCURRENCY )
89 dVal = ImpCurrencyToDouble( p->nInt64 );
90 else if( p->eType == SbxSALINT64 )
91 dVal = static_cast< double >(p->nInt64);
92 else if( p->eType == SbxSALUINT64 )
93 dVal = ImpSalUInt64ToDouble( p->uInt64 );
94 else if( p->eType == SbxDECIMAL )
96 dVal = 0.0;
97 if( p->pDecimal )
98 p->pDecimal->getDouble( dVal );
100 else
101 dVal = p->nDouble;
103 if( !o3tl::convertsToAtMost(o3tl::roundAway(dVal), SbxMAXULNG) )
105 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXULNG;
107 else if( dVal < 0 )
109 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
111 else
112 nRes = static_cast<sal_uInt32>( dVal + 0.5 );
113 break;
115 case SbxBYREF | SbxSTRING:
116 case SbxSTRING:
117 case SbxLPSTR:
118 if( !p->pOUString )
119 nRes = 0;
120 else
122 double d;
123 SbxDataType t;
124 if( ImpScan( *p->pOUString, d, t, nullptr, false ) != ERRCODE_NONE )
125 nRes = 0;
126 else if( !o3tl::convertsToAtMost(o3tl::roundAway(d), SbxMAXULNG) )
128 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXULNG;
130 else if( d < 0 )
132 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
134 else
135 nRes = static_cast<sal_uInt32>( d + 0.5 );
137 break;
138 case SbxOBJECT:
140 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
141 if( pVal )
142 nRes = pVal->GetULong();
143 else
145 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT ); nRes = 0;
147 break;
150 case SbxBYREF | SbxBYTE:
151 nRes = *p->pByte; break;
152 case SbxBYREF | SbxERROR:
153 case SbxBYREF | SbxUSHORT:
154 nRes = *p->pUShort; break;
155 case SbxBYREF | SbxULONG:
156 nRes = *p->pULong; break;
158 // from here on tests
159 case SbxBYREF | SbxCHAR:
160 aTmp.nChar = *p->pChar; goto ref;
161 case SbxBYREF | SbxINTEGER:
162 case SbxBYREF | SbxBOOL:
163 aTmp.nInteger = *p->pInteger; goto ref;
164 case SbxBYREF | SbxLONG:
165 aTmp.nLong = *p->pLong; goto ref;
166 case SbxBYREF | SbxSINGLE:
167 aTmp.nSingle = *p->pSingle; goto ref;
168 case SbxBYREF | SbxDATE:
169 case SbxBYREF | SbxDOUBLE:
170 aTmp.nDouble = *p->pDouble; goto ref;
171 case SbxBYREF | SbxCURRENCY:
172 case SbxBYREF | SbxSALINT64:
173 aTmp.nInt64 = *p->pnInt64; goto ref;
174 case SbxBYREF | SbxSALUINT64:
175 aTmp.uInt64 = *p->puInt64; goto ref;
176 ref:
177 aTmp.eType = SbxDataType( p->eType & 0x0FFF );
178 p = &aTmp; goto start;
180 default:
181 SbxBase::SetError( ERRCODE_BASIC_CONVERSION ); nRes = 0;
183 return nRes;
186 void ImpPutULong( SbxValues* p, sal_uInt32 n )
188 SbxValues aTmp;
189 start:
190 switch( +p->eType )
192 case SbxULONG:
193 p->nULong = n; break;
194 case SbxSINGLE:
195 p->nSingle = static_cast<float>(n); break;
196 case SbxDATE:
197 case SbxDOUBLE:
198 p->nDouble = n; break;
199 case SbxCURRENCY:
200 case SbxSALINT64:
201 aTmp.pnInt64 = &p->nInt64; goto direct;
202 case SbxSALUINT64:
203 p->uInt64 = n; break;
204 case SbxDECIMAL:
205 case SbxBYREF | SbxDECIMAL:
206 ImpCreateDecimal( p )->setULong( n );
207 break;
209 // from here on tests
210 case SbxCHAR:
211 aTmp.pChar = &p->nChar; goto direct;
212 case SbxUINT:
213 aTmp.pByte = &p->nByte; goto direct;
214 case SbxINTEGER:
215 case SbxBOOL:
216 aTmp.pInteger = &p->nInteger; goto direct;
217 case SbxLONG:
218 aTmp.pLong = &p->nLong; goto direct;
219 case SbxERROR:
220 case SbxUSHORT:
221 aTmp.pUShort = &p->nUShort; goto direct;
222 direct:
223 aTmp.eType = SbxDataType( p->eType | SbxBYREF );
224 p = &aTmp; goto start;
226 case SbxBYREF | SbxSTRING:
227 case SbxSTRING:
228 case SbxLPSTR:
229 if( !p->pOUString )
230 p->pOUString = new OUString;
231 ImpCvtNum( static_cast<double>(n), 0, *p->pOUString );
232 break;
233 case SbxOBJECT:
235 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
236 if( pVal )
237 pVal->PutULong( n );
238 else
239 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT );
240 break;
242 case SbxBYREF | SbxCHAR:
243 if( n > SbxMAXCHAR )
245 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXCHAR;
247 *p->pChar = static_cast<sal_Unicode>(n); break;
248 case SbxBYREF | SbxBYTE:
249 if( n > SbxMAXBYTE )
251 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXBYTE;
253 *p->pByte = static_cast<sal_uInt8>(n); break;
254 case SbxBYREF | SbxINTEGER:
255 case SbxBYREF | SbxBOOL:
256 if( n > SbxMAXINT )
258 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXINT;
260 *p->pInteger = static_cast<sal_Int16>(n); break;
261 case SbxBYREF | SbxERROR:
262 case SbxBYREF | SbxUSHORT:
263 if( n > SbxMAXUINT )
265 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXUINT;
267 *p->pUShort = static_cast<sal_uInt16>(n); break;
268 case SbxBYREF | SbxLONG:
269 if( n > SbxMAXLNG )
271 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXLNG;
273 *p->pLong = static_cast<sal_Int32>(n); break;
274 case SbxBYREF | SbxULONG:
275 *p->pULong = n; break;
276 case SbxBYREF | SbxSINGLE:
277 *p->pSingle = static_cast<float>(n); break;
278 case SbxBYREF | SbxDATE:
279 case SbxBYREF | SbxDOUBLE:
280 *p->pDouble = n; break;
281 case SbxBYREF | SbxCURRENCY:
282 *p->pnInt64 = n * CURRENCY_FACTOR; break;
283 case SbxBYREF | SbxSALINT64:
284 *p->pnInt64 = n; break;
285 case SbxBYREF | SbxSALUINT64:
286 *p->puInt64 = n; break;
288 default:
289 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */