Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / basic / source / sbx / sbxbyte.cxx
blobab078157b60bb80166ec6119fe4356e8171822cc
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 <vcl/errcode.hxx>
21 //#include <basic/sbx.hxx>
22 #include <basic/sberrors.hxx>
23 #include "sbxconv.hxx"
25 #include <rtl/math.hxx>
27 sal_uInt8 ImpGetByte( const SbxValues* p )
29 SbxValues aTmp;
30 sal_uInt8 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 if( p->nChar > SbxMAXBYTE )
42 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
44 else
45 nRes = static_cast<sal_uInt8>(p->nChar);
46 break;
47 case SbxBYTE:
48 nRes = p->nByte; break;
49 case SbxINTEGER:
50 case SbxBOOL:
51 if( p->nInteger > SbxMAXBYTE )
53 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXBYTE;
55 else if( p->nInteger < 0 )
57 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
59 else
60 nRes = static_cast<sal_uInt8>(p->nInteger);
61 break;
62 case SbxERROR:
63 case SbxUSHORT:
64 if( p->nUShort > sal_uInt16(SbxMAXBYTE) )
66 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXBYTE;
68 else
69 nRes = static_cast<sal_uInt8>(p->nUShort);
70 break;
71 case SbxLONG:
72 if( p->nLong > SbxMAXBYTE )
74 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXBYTE;
76 else if( p->nLong < 0 )
78 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
80 else
81 nRes = static_cast<sal_uInt8>(p->nLong);
82 break;
83 case SbxULONG:
84 if( p->nULong > SbxMAXBYTE )
86 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXBYTE;
88 else
89 nRes = static_cast<sal_uInt8>(p->nULong);
90 break;
91 case SbxCURRENCY:
92 case SbxSALINT64:
94 sal_Int64 val = p->nInt64;
95 if ( p->eType == SbxCURRENCY )
96 val = val / CURRENCY_FACTOR;
97 if( val > SbxMAXBYTE )
99 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXBYTE;
101 else if( p->nInt64 < 0 )
103 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
105 else
106 nRes = static_cast<sal_uInt8>(val);
107 break;
109 case SbxSALUINT64:
110 if( p->uInt64 > SbxMAXBYTE )
112 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXBYTE;
114 else
115 nRes = static_cast<sal_uInt8>(p->uInt64);
116 break;
117 case SbxSINGLE:
118 if( p->nSingle > SbxMAXBYTE )
120 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXBYTE;
122 else if( p->nSingle < 0 )
124 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
126 else
127 nRes = static_cast<sal_uInt8>(rtl::math::round( p->nSingle ));
128 break;
129 case SbxDATE:
130 case SbxDOUBLE:
131 case SbxDECIMAL:
132 case SbxBYREF | SbxDECIMAL:
134 double dVal;
135 if( p->eType == SbxDECIMAL )
137 dVal = 0.0;
138 if( p->pDecimal )
139 p->pDecimal->getDouble( dVal );
141 else
142 dVal = p->nDouble;
144 if( dVal > SbxMAXBYTE )
146 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXBYTE;
148 else if( dVal < 0 )
150 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
152 else
153 nRes = static_cast<sal_uInt8>(rtl::math::round( dVal ));
154 break;
156 case SbxBYREF | SbxSTRING:
157 case SbxSTRING:
158 case SbxLPSTR:
159 if( !p->pOUString )
160 nRes = 0;
161 else
163 double d;
164 SbxDataType t;
165 if( ImpScan( *p->pOUString, d, t, nullptr, false ) != ERRCODE_NONE )
166 nRes = 0;
167 else if( d > SbxMAXBYTE )
169 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXBYTE;
171 else if( d < 0 )
173 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
175 else
176 nRes = static_cast<sal_uInt8>( d + 0.5 );
178 break;
179 case SbxOBJECT:
181 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
182 if( pVal )
183 nRes = pVal->GetByte();
184 else
186 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT ); nRes = 0;
188 break;
191 case SbxBYREF | SbxBYTE:
192 nRes = p->nByte; break;
194 // from here on will be tested
195 case SbxBYREF | SbxCHAR:
196 aTmp.nChar = *p->pChar; goto ref;
197 case SbxBYREF | SbxINTEGER:
198 case SbxBYREF | SbxBOOL:
199 aTmp.nInteger = *p->pInteger; goto ref;
200 case SbxBYREF | SbxLONG:
201 aTmp.nLong = *p->pLong; goto ref;
202 case SbxBYREF | SbxULONG:
203 aTmp.nULong = *p->pULong; goto ref;
204 case SbxBYREF | SbxERROR:
205 case SbxBYREF | SbxUSHORT:
206 aTmp.nUShort = *p->pUShort; goto ref;
207 case SbxBYREF | SbxSINGLE:
208 aTmp.nSingle = *p->pSingle; goto ref;
209 case SbxBYREF | SbxDATE:
210 case SbxBYREF | SbxDOUBLE:
211 aTmp.nDouble = *p->pDouble; goto ref;
212 case SbxBYREF | SbxCURRENCY:
213 case SbxBYREF | SbxSALINT64:
214 aTmp.nInt64 = *p->pnInt64; goto ref;
215 case SbxBYREF | SbxSALUINT64:
216 aTmp.uInt64 = *p->puInt64; goto ref;
217 ref:
218 aTmp.eType = SbxDataType( p->eType & 0x0FFF );
219 p = &aTmp; goto start;
221 default:
222 SbxBase::SetError( ERRCODE_BASIC_CONVERSION ); nRes = 0;
224 return nRes;
227 void ImpPutByte( SbxValues* p, sal_uInt8 n )
229 switch( +p->eType )
231 case SbxBYTE:
232 p->nByte = n; break;
233 case SbxINTEGER:
234 case SbxBOOL:
235 p->nInteger = n; break;
236 case SbxERROR:
237 case SbxUSHORT:
238 p->nUShort = n; break;
239 case SbxLONG:
240 p->nLong = n; break;
241 case SbxULONG:
242 p->nULong = n; break;
243 case SbxSINGLE:
244 p->nSingle = n; break;
245 case SbxDATE:
246 case SbxDOUBLE:
247 p->nDouble = n; break;
248 case SbxCURRENCY:
249 p->nInt64 = n * CURRENCY_FACTOR; break;
250 case SbxSALINT64:
251 p->nInt64 = n; break;
252 case SbxSALUINT64:
253 p->uInt64 = n; break;
254 case SbxDECIMAL:
255 case SbxBYREF | SbxDECIMAL:
256 ImpCreateDecimal( p )->setByte( n );
257 break;
259 case SbxCHAR:
260 p->nChar = static_cast<sal_Unicode>(n); break;
262 case SbxBYREF | SbxSTRING:
263 case SbxSTRING:
264 case SbxLPSTR:
265 if( !p->pOUString )
266 p->pOUString = new OUString;
267 ImpCvtNum( static_cast<double>(n), 0, *p->pOUString );
268 break;
269 case SbxOBJECT:
271 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
272 if( pVal )
273 pVal->PutByte( n );
274 else
275 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT );
276 break;
278 case SbxBYREF | SbxCHAR:
279 *p->pChar = static_cast<sal_Unicode>(n); break;
280 case SbxBYREF | SbxBYTE:
281 *p->pByte = n; break;
282 case SbxBYREF | SbxINTEGER:
283 case SbxBYREF | SbxBOOL:
284 *p->pInteger = n; break;
285 case SbxBYREF | SbxERROR:
286 case SbxBYREF | SbxUSHORT:
287 *p->pUShort = n; break;
288 case SbxBYREF | SbxLONG:
289 *p->pLong = n; break;
290 case SbxBYREF | SbxULONG:
291 *p->pULong = n; break;
292 case SbxBYREF | SbxSINGLE:
293 *p->pSingle = n; break;
294 case SbxBYREF | SbxDATE:
295 case SbxBYREF | SbxDOUBLE:
296 *p->pDouble = n; break;
297 case SbxBYREF | SbxCURRENCY:
298 p->nInt64 = n * CURRENCY_FACTOR; break;
299 case SbxBYREF | SbxSALINT64:
300 *p->pnInt64 = n; break;
301 case SbxBYREF | SbxSALUINT64:
302 *p->puInt64 = n; break;
304 default:
305 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
309 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */