LibreOffice Module basic (master) 1
sbxbyte.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <o3tl/safeint.hxx>
24//#include <basic/sbx.hxx>
25#include <basic/sberrors.hxx>
26#include "sbxconv.hxx"
27#include <rtlproto.hxx>
28
29#include <rtl/math.hxx>
30
32{
33 SbxValues aTmp;
34 sal_uInt8 nRes;
35start:
36 switch( +p->eType )
37 {
38 case SbxNULL:
40 [[fallthrough]];
41 case SbxEMPTY:
42 nRes = 0; break;
43 case SbxCHAR:
44 if( p->nChar > SbxMAXBYTE )
45 {
47 }
48 else
49 nRes = static_cast<sal_uInt8>(p->nChar);
50 break;
51 case SbxBYTE:
52 nRes = p->nByte; break;
53 case SbxINTEGER:
54 case SbxBOOL:
55 if( p->nInteger > SbxMAXBYTE )
56 {
58 }
59 else if( p->nInteger < 0 )
60 {
62 }
63 else
64 nRes = static_cast<sal_uInt8>(p->nInteger);
65 break;
66 case SbxERROR:
67 case SbxUSHORT:
68 if( p->nUShort > o3tl::make_unsigned(SbxMAXBYTE) )
69 {
71 }
72 else
73 nRes = static_cast<sal_uInt8>(p->nUShort);
74 break;
75 case SbxLONG:
76 if( p->nLong > SbxMAXBYTE )
77 {
79 }
80 else if( p->nLong < 0 )
81 {
83 }
84 else
85 nRes = static_cast<sal_uInt8>(p->nLong);
86 break;
87 case SbxULONG:
88 if( p->nULong > SbxMAXBYTE )
89 {
91 }
92 else
93 nRes = static_cast<sal_uInt8>(p->nULong);
94 break;
95 case SbxCURRENCY:
96 case SbxSALINT64:
97 {
98 sal_Int64 val = p->nInt64;
99 if ( p->eType == SbxCURRENCY )
100 val = val / CURRENCY_FACTOR;
101 if( val > SbxMAXBYTE )
102 {
104 }
105 else if( p->nInt64 < 0 )
106 {
108 }
109 else
110 nRes = static_cast<sal_uInt8>(val);
111 break;
112 }
113 case SbxSALUINT64:
114 if( p->uInt64 > SbxMAXBYTE )
115 {
117 }
118 else
119 nRes = static_cast<sal_uInt8>(p->uInt64);
120 break;
121 case SbxSINGLE:
122 if( p->nSingle > SbxMAXBYTE )
123 {
125 }
126 else if( p->nSingle < 0 )
127 {
129 }
130 else
131 nRes = static_cast<sal_uInt8>(rtl::math::round( p->nSingle ));
132 break;
133 case SbxDATE:
134 case SbxDOUBLE:
135 case SbxDECIMAL:
136 case SbxBYREF | SbxDECIMAL:
137 {
138 double dVal;
139 if( p->eType == SbxDECIMAL )
140 {
141 dVal = 0.0;
142 if( p->pDecimal )
143 p->pDecimal->getDouble( dVal );
144 }
145 else
146 dVal = p->nDouble;
147
148 if( dVal > SbxMAXBYTE )
149 {
151 }
152 else if( dVal < 0 )
153 {
155 }
156 else
157 nRes = static_cast<sal_uInt8>(rtl::math::round( dVal ));
158 break;
159 }
160 case SbxBYREF | SbxSTRING:
161 case SbxSTRING:
162 case SbxLPSTR:
163 if( !p->pOUString )
164 nRes = 0;
165 else
166 {
167 double d;
169 if( ImpScan( *p->pOUString, d, t, nullptr, !LibreOffice6FloatingPointMode() ) != ERRCODE_NONE )
170 nRes = 0;
171 else if( d > SbxMAXBYTE )
172 {
174 }
175 else if( d < 0 )
176 {
178 }
179 else
180 nRes = static_cast<sal_uInt8>( d + 0.5 );
181 }
182 break;
183 case SbxOBJECT:
184 {
185 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
186 if( pVal )
187 nRes = pVal->GetByte();
188 else
189 {
191 }
192 break;
193 }
194
195 case SbxBYREF | SbxBYTE:
196 nRes = p->nByte; break;
197
198 // from here on will be tested
199 case SbxBYREF | SbxCHAR:
200 aTmp.nChar = *p->pChar; goto ref;
201 case SbxBYREF | SbxINTEGER:
202 case SbxBYREF | SbxBOOL:
203 aTmp.nInteger = *p->pInteger; goto ref;
204 case SbxBYREF | SbxLONG:
205 aTmp.nLong = *p->pLong; goto ref;
206 case SbxBYREF | SbxULONG:
207 aTmp.nULong = *p->pULong; goto ref;
208 case SbxBYREF | SbxERROR:
209 case SbxBYREF | SbxUSHORT:
210 aTmp.nUShort = *p->pUShort; goto ref;
211 case SbxBYREF | SbxSINGLE:
212 aTmp.nSingle = *p->pSingle; goto ref;
213 case SbxBYREF | SbxDATE:
214 case SbxBYREF | SbxDOUBLE:
215 aTmp.nDouble = *p->pDouble; goto ref;
216 case SbxBYREF | SbxCURRENCY:
217 case SbxBYREF | SbxSALINT64:
218 aTmp.nInt64 = *p->pnInt64; goto ref;
219 case SbxBYREF | SbxSALUINT64:
220 aTmp.uInt64 = *p->puInt64; goto ref;
221 ref:
222 aTmp.eType = SbxDataType( p->eType & 0x0FFF );
223 p = &aTmp; goto start;
224
225 default:
227 }
228 return nRes;
229}
230
232{
233 switch( +p->eType )
234 {
235 case SbxBYTE:
236 p->nByte = n; break;
237 case SbxINTEGER:
238 case SbxBOOL:
239 p->nInteger = n; break;
240 case SbxERROR:
241 case SbxUSHORT:
242 p->nUShort = n; break;
243 case SbxLONG:
244 p->nLong = n; break;
245 case SbxULONG:
246 p->nULong = n; break;
247 case SbxSINGLE:
248 p->nSingle = n; break;
249 case SbxDATE:
250 case SbxDOUBLE:
251 p->nDouble = n; break;
252 case SbxCURRENCY:
253 p->nInt64 = n * CURRENCY_FACTOR; break;
254 case SbxSALINT64:
255 p->nInt64 = n; break;
256 case SbxSALUINT64:
257 p->uInt64 = n; break;
258 case SbxDECIMAL:
259 case SbxBYREF | SbxDECIMAL:
261 break;
262
263 case SbxCHAR:
264 p->nChar = static_cast<sal_Unicode>(n); break;
265
266 case SbxBYREF | SbxSTRING:
267 case SbxSTRING:
268 case SbxLPSTR:
269 if( !p->pOUString )
270 p->pOUString = new OUString;
271 ImpCvtNum( static_cast<double>(n), 0, *p->pOUString );
272 break;
273 case SbxOBJECT:
274 {
275 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
276 if( pVal )
277 pVal->PutByte( n );
278 else
280 break;
281 }
282 case SbxBYREF | SbxCHAR:
283 *p->pChar = static_cast<sal_Unicode>(n); break;
284 case SbxBYREF | SbxBYTE:
285 *p->pByte = n; break;
286 case SbxBYREF | SbxINTEGER:
287 case SbxBYREF | SbxBOOL:
288 *p->pInteger = n; break;
289 case SbxBYREF | SbxERROR:
290 case SbxBYREF | SbxUSHORT:
291 *p->pUShort = n; break;
292 case SbxBYREF | SbxLONG:
293 *p->pLong = n; break;
294 case SbxBYREF | SbxULONG:
295 *p->pULong = n; break;
296 case SbxBYREF | SbxSINGLE:
297 *p->pSingle = n; break;
298 case SbxBYREF | SbxDATE:
299 case SbxBYREF | SbxDOUBLE:
300 *p->pDouble = n; break;
301 case SbxBYREF | SbxCURRENCY:
302 p->nInt64 = n * CURRENCY_FACTOR; break;
303 case SbxBYREF | SbxSALINT64:
304 *p->pnInt64 = n; break;
305 case SbxBYREF | SbxSALUINT64:
306 *p->puInt64 = n; break;
307
308 default:
310 }
311}
312
313/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XPropertyListType t
double d
static void SetError(ErrCode)
Definition: sbxbase.cxx:116
void setByte(sal_uInt8 val)
Definition: sbxdec.cxx:319
bool PutByte(sal_uInt8)
sal_uInt8 GetByte() const
Definition: sbxvar.hxx:158
#define ERRCODE_NONE
void * p
sal_Int64 n
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
bool LibreOffice6FloatingPointMode()
Definition: methods1.cxx:2946
#define ERRCODE_BASIC_NO_OBJECT
Definition: sberrors.hxx:34
#define ERRCODE_BASIC_MATH_OVERFLOW
Definition: sberrors.hxx:27
#define ERRCODE_BASIC_CONVERSION
Definition: sberrors.hxx:30
void ImpPutByte(SbxValues *p, sal_uInt8 n)
Definition: sbxbyte.cxx:231
sal_uInt8 ImpGetByte(const SbxValues *p)
Definition: sbxbyte.cxx:31
ErrCode ImpScan(const OUString &rSrc, double &nVal, SbxDataType &rType, sal_uInt16 *pLen, bool bOnlyIntntl)
Definition: sbxscan.cxx:71
SbxDecimal * ImpCreateDecimal(SbxValues *p)
Definition: sbxdec.cxx:376
void ImpCvtNum(double nNum, short nPrec, OUString &rRes, bool bCoreString=false)
Definition: sbxscan.cxx:290
SbxBOOL
Definition: sbxdef.hxx:215
SbxDataType
Definition: sbxdef.hxx:37
@ SbxOBJECT
Definition: sbxdef.hxx:47
@ SbxSALINT64
Definition: sbxdef.hxx:75
@ SbxLONG
Definition: sbxdef.hxx:41
@ SbxSALUINT64
Definition: sbxdef.hxx:76
@ SbxNULL
Definition: sbxdef.hxx:39
@ SbxBYTE
Definition: sbxdef.hxx:55
@ SbxEMPTY
Definition: sbxdef.hxx:38
@ SbxDECIMAL
Definition: sbxdef.hxx:77
@ SbxULONG
Definition: sbxdef.hxx:57
@ SbxUSHORT
Definition: sbxdef.hxx:56
@ SbxERROR
Definition: sbxdef.hxx:48
@ SbxDATE
Definition: sbxdef.hxx:45
@ SbxCURRENCY
Definition: sbxdef.hxx:44
@ SbxLPSTR
Definition: sbxdef.hxx:68
@ SbxSINGLE
Definition: sbxdef.hxx:42
@ SbxBYREF
Definition: sbxdef.hxx:81
@ SbxCHAR
Definition: sbxdef.hxx:54
@ SbxSTRING
Definition: sbxdef.hxx:46
@ SbxINTEGER
Definition: sbxdef.hxx:40
@ SbxDOUBLE
Definition: sbxdef.hxx:43
constexpr auto CURRENCY_FACTOR
Definition: sbxdef.hxx:197
constexpr auto SbxMAXBYTE
Definition: sbxdef.hxx:186
float nSingle
Definition: sbxvar.hxx:55
sal_Int32 nLong
Definition: sbxvar.hxx:49
sal_Int16 nInteger
Definition: sbxvar.hxx:47
sal_Unicode nChar
Definition: sbxvar.hxx:46
sal_Int64 nInt64
Definition: sbxvar.hxx:53
double nDouble
Definition: sbxvar.hxx:56
sal_uInt16 nUShort
Definition: sbxvar.hxx:45
sal_uInt64 uInt64
Definition: sbxvar.hxx:52
SbxDataType eType
Definition: sbxvar.hxx:77
sal_uInt32 nULong
Definition: sbxvar.hxx:48
unsigned char sal_uInt8
sal_uInt16 sal_Unicode