nss: upgrade to release 3.73
[LibreOffice.git] / basic / source / sbx / sbxbyte.cxx
blob4974213dc7a1265c47dc9ea23858f7097d66ef57
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/safeint.hxx>
23 #include <vcl/errcode.hxx>
24 //#include <basic/sbx.hxx>
25 #include <basic/sberrors.hxx>
26 #include "sbxconv.hxx"
28 #include <rtl/math.hxx>
30 sal_uInt8 ImpGetByte( const SbxValues* p )
32 SbxValues aTmp;
33 sal_uInt8 nRes;
34 start:
35 switch( +p->eType )
37 case SbxNULL:
38 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
39 [[fallthrough]];
40 case SbxEMPTY:
41 nRes = 0; break;
42 case SbxCHAR:
43 if( p->nChar > SbxMAXBYTE )
45 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
47 else
48 nRes = static_cast<sal_uInt8>(p->nChar);
49 break;
50 case SbxBYTE:
51 nRes = p->nByte; break;
52 case SbxINTEGER:
53 case SbxBOOL:
54 if( p->nInteger > SbxMAXBYTE )
56 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXBYTE;
58 else if( p->nInteger < 0 )
60 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
62 else
63 nRes = static_cast<sal_uInt8>(p->nInteger);
64 break;
65 case SbxERROR:
66 case SbxUSHORT:
67 if( p->nUShort > o3tl::make_unsigned(SbxMAXBYTE) )
69 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXBYTE;
71 else
72 nRes = static_cast<sal_uInt8>(p->nUShort);
73 break;
74 case SbxLONG:
75 if( p->nLong > SbxMAXBYTE )
77 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXBYTE;
79 else if( p->nLong < 0 )
81 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
83 else
84 nRes = static_cast<sal_uInt8>(p->nLong);
85 break;
86 case SbxULONG:
87 if( p->nULong > SbxMAXBYTE )
89 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXBYTE;
91 else
92 nRes = static_cast<sal_uInt8>(p->nULong);
93 break;
94 case SbxCURRENCY:
95 case SbxSALINT64:
97 sal_Int64 val = p->nInt64;
98 if ( p->eType == SbxCURRENCY )
99 val = val / CURRENCY_FACTOR;
100 if( val > SbxMAXBYTE )
102 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXBYTE;
104 else if( p->nInt64 < 0 )
106 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
108 else
109 nRes = static_cast<sal_uInt8>(val);
110 break;
112 case SbxSALUINT64:
113 if( p->uInt64 > SbxMAXBYTE )
115 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXBYTE;
117 else
118 nRes = static_cast<sal_uInt8>(p->uInt64);
119 break;
120 case SbxSINGLE:
121 if( p->nSingle > SbxMAXBYTE )
123 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXBYTE;
125 else if( p->nSingle < 0 )
127 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
129 else
130 nRes = static_cast<sal_uInt8>(rtl::math::round( p->nSingle ));
131 break;
132 case SbxDATE:
133 case SbxDOUBLE:
134 case SbxDECIMAL:
135 case SbxBYREF | SbxDECIMAL:
137 double dVal;
138 if( p->eType == SbxDECIMAL )
140 dVal = 0.0;
141 if( p->pDecimal )
142 p->pDecimal->getDouble( dVal );
144 else
145 dVal = p->nDouble;
147 if( dVal > SbxMAXBYTE )
149 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXBYTE;
151 else if( dVal < 0 )
153 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
155 else
156 nRes = static_cast<sal_uInt8>(rtl::math::round( dVal ));
157 break;
159 case SbxBYREF | SbxSTRING:
160 case SbxSTRING:
161 case SbxLPSTR:
162 if( !p->pOUString )
163 nRes = 0;
164 else
166 double d;
167 SbxDataType t;
168 if( ImpScan( *p->pOUString, d, t, nullptr, true ) != ERRCODE_NONE )
169 nRes = 0;
170 else if( d > SbxMAXBYTE )
172 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXBYTE;
174 else if( d < 0 )
176 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
178 else
179 nRes = static_cast<sal_uInt8>( d + 0.5 );
181 break;
182 case SbxOBJECT:
184 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
185 if( pVal )
186 nRes = pVal->GetByte();
187 else
189 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT ); nRes = 0;
191 break;
194 case SbxBYREF | SbxBYTE:
195 nRes = p->nByte; break;
197 // from here on will be tested
198 case SbxBYREF | SbxCHAR:
199 aTmp.nChar = *p->pChar; goto ref;
200 case SbxBYREF | SbxINTEGER:
201 case SbxBYREF | SbxBOOL:
202 aTmp.nInteger = *p->pInteger; goto ref;
203 case SbxBYREF | SbxLONG:
204 aTmp.nLong = *p->pLong; goto ref;
205 case SbxBYREF | SbxULONG:
206 aTmp.nULong = *p->pULong; goto ref;
207 case SbxBYREF | SbxERROR:
208 case SbxBYREF | SbxUSHORT:
209 aTmp.nUShort = *p->pUShort; goto ref;
210 case SbxBYREF | SbxSINGLE:
211 aTmp.nSingle = *p->pSingle; goto ref;
212 case SbxBYREF | SbxDATE:
213 case SbxBYREF | SbxDOUBLE:
214 aTmp.nDouble = *p->pDouble; goto ref;
215 case SbxBYREF | SbxCURRENCY:
216 case SbxBYREF | SbxSALINT64:
217 aTmp.nInt64 = *p->pnInt64; goto ref;
218 case SbxBYREF | SbxSALUINT64:
219 aTmp.uInt64 = *p->puInt64; goto ref;
220 ref:
221 aTmp.eType = SbxDataType( p->eType & 0x0FFF );
222 p = &aTmp; goto start;
224 default:
225 SbxBase::SetError( ERRCODE_BASIC_CONVERSION ); nRes = 0;
227 return nRes;
230 void ImpPutByte( SbxValues* p, sal_uInt8 n )
232 switch( +p->eType )
234 case SbxBYTE:
235 p->nByte = n; break;
236 case SbxINTEGER:
237 case SbxBOOL:
238 p->nInteger = n; break;
239 case SbxERROR:
240 case SbxUSHORT:
241 p->nUShort = n; break;
242 case SbxLONG:
243 p->nLong = n; break;
244 case SbxULONG:
245 p->nULong = n; break;
246 case SbxSINGLE:
247 p->nSingle = n; break;
248 case SbxDATE:
249 case SbxDOUBLE:
250 p->nDouble = n; break;
251 case SbxCURRENCY:
252 p->nInt64 = n * CURRENCY_FACTOR; break;
253 case SbxSALINT64:
254 p->nInt64 = n; break;
255 case SbxSALUINT64:
256 p->uInt64 = n; break;
257 case SbxDECIMAL:
258 case SbxBYREF | SbxDECIMAL:
259 ImpCreateDecimal( p )->setByte( n );
260 break;
262 case SbxCHAR:
263 p->nChar = static_cast<sal_Unicode>(n); break;
265 case SbxBYREF | SbxSTRING:
266 case SbxSTRING:
267 case SbxLPSTR:
268 if( !p->pOUString )
269 p->pOUString = new OUString;
270 ImpCvtNum( static_cast<double>(n), 0, *p->pOUString );
271 break;
272 case SbxOBJECT:
274 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
275 if( pVal )
276 pVal->PutByte( n );
277 else
278 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT );
279 break;
281 case SbxBYREF | SbxCHAR:
282 *p->pChar = static_cast<sal_Unicode>(n); break;
283 case SbxBYREF | SbxBYTE:
284 *p->pByte = n; break;
285 case SbxBYREF | SbxINTEGER:
286 case SbxBYREF | SbxBOOL:
287 *p->pInteger = n; break;
288 case SbxBYREF | SbxERROR:
289 case SbxBYREF | SbxUSHORT:
290 *p->pUShort = n; break;
291 case SbxBYREF | SbxLONG:
292 *p->pLong = n; break;
293 case SbxBYREF | SbxULONG:
294 *p->pULong = n; break;
295 case SbxBYREF | SbxSINGLE:
296 *p->pSingle = n; break;
297 case SbxBYREF | SbxDATE:
298 case SbxBYREF | SbxDOUBLE:
299 *p->pDouble = n; break;
300 case SbxBYREF | SbxCURRENCY:
301 p->nInt64 = n * CURRENCY_FACTOR; break;
302 case SbxBYREF | SbxSALINT64:
303 *p->pnInt64 = n; break;
304 case SbxBYREF | SbxSALUINT64:
305 *p->puInt64 = n; break;
307 default:
308 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */