Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / basic / source / sbx / sbxchar.cxx
blobef2b13423ee2d444fbdd90edbba8c1bcc6c83030
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/sberrors.hxx>
22 #include "sbxconv.hxx"
24 #include <rtl/math.hxx>
26 sal_Unicode ImpGetChar( const SbxValues* p )
28 SbxValues aTmp;
29 sal_Unicode nRes = 0;
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 = static_cast<sal_Unicode>(p->nByte);
42 break;
43 case SbxINTEGER:
44 case SbxBOOL:
45 if( p->nInteger < SbxMINCHAR )
47 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMINCHAR;
49 else
50 nRes = static_cast<sal_Unicode>(p->nInteger);
51 break;
52 case SbxERROR:
53 case SbxUSHORT:
54 nRes = static_cast<sal_Unicode>(p->nUShort);
55 break;
56 case SbxLONG:
57 if( p->nLong > SbxMAXCHAR )
59 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXCHAR;
61 else if( p->nLong < SbxMINCHAR )
63 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMINCHAR;
65 else
66 nRes = static_cast<sal_Unicode>(p->nLong);
67 break;
68 case SbxULONG:
69 if( p->nULong > SbxMAXCHAR )
71 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXCHAR;
73 else
74 nRes = static_cast<sal_Unicode>(p->nULong);
75 break;
76 case SbxCURRENCY:
77 case SbxSALINT64:
79 sal_Int64 val = p->nInt64;
81 if ( p->eType == SbxCURRENCY )
82 val = val / CURRENCY_FACTOR;
84 if( val > SbxMAXCHAR )
86 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXCHAR;
88 else if( p->nInt64 < SbxMINCHAR )
90 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMINCHAR;
92 else
93 nRes = static_cast<sal_Unicode>(val);
94 break;
96 case SbxSALUINT64:
97 if( p->uInt64 > SbxMAXCHAR )
99 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXCHAR;
101 else
102 nRes = static_cast<sal_Unicode>(p->uInt64);
103 break;
104 case SbxSINGLE:
105 if( p->nSingle > SbxMAXCHAR )
107 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXCHAR;
109 else if( p->nSingle < SbxMINCHAR )
111 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMINCHAR;
113 else
114 nRes = static_cast<sal_Unicode>(rtl::math::round( p->nSingle ));
115 break;
116 case SbxDATE:
117 case SbxDOUBLE:
118 case SbxDECIMAL:
119 case SbxBYREF | SbxDECIMAL:
121 double dVal;
122 if( p->eType == SbxDECIMAL )
124 dVal = 0.0;
125 if( p->pDecimal )
126 p->pDecimal->getDouble( dVal );
128 else
129 dVal = p->nDouble;
131 if( dVal > SbxMAXCHAR )
133 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXCHAR;
135 else if( dVal < SbxMINCHAR )
137 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMINCHAR;
139 else
140 nRes = static_cast<sal_uInt8>(rtl::math::round( dVal ));
141 break;
143 case SbxBYREF | SbxSTRING:
144 case SbxSTRING:
145 case SbxLPSTR:
146 if ( p->pOUString )
148 double d;
149 SbxDataType t;
150 if( ImpScan( *p->pOUString, d, t, nullptr, false ) != ERRCODE_NONE )
151 nRes = 0;
152 else if( d > SbxMAXCHAR )
154 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXCHAR;
156 else if( d < SbxMINCHAR )
158 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMINCHAR;
160 else
161 nRes = static_cast<sal_Unicode>(rtl::math::round( d ));
163 break;
164 case SbxOBJECT:
166 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
167 if( pVal )
168 nRes = pVal->GetChar();
169 else
171 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT ); nRes = 0;
173 break;
176 case SbxBYREF | SbxCHAR:
177 nRes = *p->pChar; break;
178 // from here on will be tested
179 case SbxBYREF | SbxBYTE:
180 aTmp.nByte = *p->pByte; goto ref;
181 case SbxBYREF | SbxINTEGER:
182 case SbxBYREF | SbxBOOL:
183 aTmp.nInteger = *p->pInteger; goto ref;
184 case SbxBYREF | SbxLONG:
185 aTmp.nLong = *p->pLong; goto ref;
186 case SbxBYREF | SbxULONG:
187 aTmp.nULong = *p->pULong; goto ref;
188 case SbxBYREF | SbxERROR:
189 case SbxBYREF | SbxUSHORT:
190 aTmp.nUShort = *p->pUShort; goto ref;
191 case SbxBYREF | SbxSINGLE:
192 aTmp.nSingle = *p->pSingle; goto ref;
193 case SbxBYREF | SbxDATE:
194 case SbxBYREF | SbxDOUBLE:
195 aTmp.nDouble = *p->pDouble; goto ref;
196 case SbxBYREF | SbxCURRENCY:
197 case SbxBYREF | SbxSALINT64:
198 aTmp.nInt64 = *p->pnInt64; goto ref;
199 case SbxBYREF | SbxSALUINT64:
200 aTmp.uInt64 = *p->puInt64; goto ref;
201 ref:
202 aTmp.eType = SbxDataType( p->eType & 0x0FFF );
203 p = &aTmp; goto start;
205 default:
206 SbxBase::SetError( ERRCODE_BASIC_CONVERSION ); nRes = 0;
208 return nRes;
211 void ImpPutChar( SbxValues* p, sal_Unicode n )
213 SbxValues aTmp;
214 start:
215 switch( +p->eType )
217 case SbxCHAR:
218 p->nChar = n; break;
219 case SbxINTEGER:
220 case SbxBOOL:
221 p->nInteger = n; break;
222 case SbxLONG:
223 p->nLong = n; break;
224 case SbxSINGLE:
225 p->nSingle = n; break;
226 case SbxDATE:
227 case SbxDOUBLE:
228 p->nDouble = n; break;
229 case SbxCURRENCY:
230 p->nInt64 = n * CURRENCY_FACTOR; break;
231 case SbxSALINT64:
232 p->nInt64 = n; break;
233 case SbxSALUINT64:
234 p->uInt64 = n; break;
235 case SbxBYREF | SbxDECIMAL:
236 ImpCreateDecimal( p )->setChar( n );
237 break;
239 // from here on will be tested
240 case SbxBYTE:
241 aTmp.pByte = &p->nByte; goto direct;
242 case SbxULONG:
243 aTmp.pULong = &p->nULong; goto direct;
244 case SbxERROR:
245 case SbxUSHORT:
246 aTmp.pUShort = &p->nUShort; goto direct;
247 direct:
248 aTmp.eType = SbxDataType( p->eType | SbxBYREF );
249 p = &aTmp; goto start;
251 case SbxBYREF | SbxSTRING:
252 case SbxSTRING:
253 case SbxLPSTR:
254 if ( !p->pOUString )
255 p->pOUString = new OUString( n );
256 else
257 *p->pOUString = OUString( n );
258 break;
259 case SbxOBJECT:
261 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
262 if( pVal )
263 pVal->PutChar( n );
264 else
265 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT );
266 break;
268 case SbxBYREF | SbxCHAR:
269 *p->pChar = n; break;
270 case SbxBYREF | SbxBYTE:
271 *p->pByte = static_cast<sal_uInt8>(n); break;
272 case SbxBYREF | SbxINTEGER:
273 case SbxBYREF | SbxBOOL:
274 *p->pInteger = n; break;
275 case SbxBYREF | SbxERROR:
276 case SbxBYREF | SbxUSHORT:
277 *p->pUShort = static_cast<sal_uInt16>(n); break;
278 case SbxBYREF | SbxLONG:
279 *p->pLong = static_cast<sal_Int32>(n); break;
280 case SbxBYREF | SbxULONG:
281 *p->pULong = static_cast<sal_uInt32>(n); break;
282 case SbxBYREF | SbxSINGLE:
283 *p->pSingle = static_cast<float>(n); break;
284 case SbxBYREF | SbxDATE:
285 case SbxBYREF | SbxDOUBLE:
286 *p->pDouble = static_cast<double>(n); break;
287 case SbxBYREF | SbxCURRENCY:
288 p->nInt64 = n * CURRENCY_FACTOR; break;
289 case SbxBYREF | SbxSALINT64:
290 *p->pnInt64 = n; break;
291 case SbxBYREF | SbxSALUINT64:
292 *p->puInt64 = n; break;
294 default:
295 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
300 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */