Colibre: Fix wrong color, the ones which doesn not follow Monoline
[LibreOffice.git] / basic / source / sbx / sbxlng.cxx
blob7011a13b0da8126588a1785042498e94f13e7730
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 <vcl/errcode.hxx>
23 #include <basic/sberrors.hxx>
24 #include "sbxconv.hxx"
25 #include <rtlproto.hxx>
27 #include <rtl/math.hxx>
29 sal_Int32 ImpGetLong( const SbxValues* p )
31 SbxValues aTmp;
32 sal_Int32 nRes;
33 start:
34 switch( +p->eType )
36 case SbxNULL:
37 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
38 [[fallthrough]];
39 case SbxEMPTY:
40 nRes = 0; break;
41 case SbxCHAR:
42 nRes = p->nChar; break;
43 case SbxBYTE:
44 nRes = p->nByte; break;
45 case SbxINTEGER:
46 case SbxBOOL:
47 nRes = p->nInteger; break;
48 case SbxERROR:
49 case SbxUSHORT:
50 nRes = p->nUShort; break;
51 case SbxLONG:
52 nRes = p->nLong; break;
53 case SbxULONG:
54 if( p->nULong > SbxMAXLNG )
56 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXLNG;
58 else
59 nRes = static_cast<sal_Int32>(p->nULong);
60 break;
61 case SbxSINGLE:
62 nRes = ImpDoubleToLong(p->nSingle);
63 break;
64 case SbxSALINT64:
65 nRes = p->nInt64;
66 break;
67 case SbxSALUINT64:
68 nRes = p->uInt64;
69 break;
70 case SbxCURRENCY:
72 sal_Int64 tstVal = p->nInt64 / CURRENCY_FACTOR;
73 nRes = static_cast<sal_Int32>(tstVal);
74 if( tstVal < SbxMINLNG || SbxMAXLNG < tstVal ) SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
75 if( SbxMAXLNG < tstVal ) nRes = SbxMAXLNG;
76 if( tstVal < SbxMINLNG ) nRes = SbxMINLNG;
77 break;
79 case SbxDATE:
80 case SbxDOUBLE:
81 case SbxDECIMAL:
82 case SbxBYREF | SbxDECIMAL:
84 double dVal;
85 if( p->eType == SbxDECIMAL )
87 dVal = 0.0;
88 if( p->pDecimal )
89 p->pDecimal->getDouble( dVal );
91 else
92 dVal = p->nDouble;
94 nRes = ImpDoubleToLong(dVal);
95 break;
97 case SbxBYREF | SbxSTRING:
98 case SbxSTRING:
99 case SbxLPSTR:
100 if( !p->pOUString )
101 nRes = 0;
102 else
104 double d;
105 SbxDataType t;
106 if( ImpScan( *p->pOUString, d, t, nullptr, !LibreOffice6FloatingPointMode() ) != ERRCODE_NONE )
107 nRes = 0;
108 else
109 nRes = ImpDoubleToLong(d);
111 break;
112 case SbxOBJECT:
114 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
115 if( pVal )
116 nRes = pVal->GetLong();
117 else
119 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT ); nRes = 0;
121 break;
124 case SbxBYREF | SbxCHAR:
125 nRes = *p->pChar; break;
126 case SbxBYREF | SbxBYTE:
127 nRes = *p->pByte; break;
128 case SbxBYREF | SbxINTEGER:
129 case SbxBYREF | SbxBOOL:
130 nRes = *p->pInteger; break;
131 case SbxBYREF | SbxLONG:
132 nRes = *p->pLong; break;
134 // from here had to be tested
135 case SbxBYREF | SbxULONG:
136 aTmp.nULong = *p->pULong; goto ref;
137 case SbxBYREF | SbxERROR:
138 case SbxBYREF | SbxUSHORT:
139 aTmp.nUShort = *p->pUShort; goto ref;
140 case SbxBYREF | SbxSINGLE:
141 aTmp.nSingle = *p->pSingle; goto ref;
142 case SbxBYREF | SbxDATE:
143 case SbxBYREF | SbxDOUBLE:
144 aTmp.nDouble = *p->pDouble; goto ref;
145 case SbxBYREF | SbxCURRENCY:
146 case SbxBYREF | SbxSALINT64:
147 aTmp.nInt64 = *p->pnInt64; goto ref;
148 case SbxBYREF | SbxSALUINT64:
149 aTmp.uInt64 = *p->puInt64; goto ref;
151 ref:
152 aTmp.eType = SbxDataType( p->eType & 0x0FFF );
153 p = &aTmp; goto start;
155 default:
156 SbxBase::SetError( ERRCODE_BASIC_CONVERSION ); nRes = 0;
158 return nRes;
161 void ImpPutLong( SbxValues* p, sal_Int32 n )
163 SbxValues aTmp;
165 start:
166 switch( +p->eType )
168 // From here had to be tested
169 case SbxCHAR:
170 aTmp.pChar = &p->nChar; goto direct;
171 case SbxBYTE:
172 aTmp.pByte = &p->nByte; goto direct;
173 case SbxINTEGER:
174 case SbxBOOL:
175 aTmp.pInteger = &p->nInteger; goto direct;
176 case SbxULONG:
177 aTmp.pULong = &p->nULong; goto direct;
178 case SbxSALUINT64:
179 aTmp.puInt64 = &p->uInt64; goto direct;
180 case SbxERROR:
181 case SbxUSHORT:
182 aTmp.pUShort = &p->nUShort;
183 direct:
184 aTmp.eType = SbxDataType( p->eType | SbxBYREF );
185 p = &aTmp; goto start;
187 // from here no longer
188 case SbxLONG:
189 p->nLong = n; break;
190 case SbxSINGLE:
191 p->nSingle = static_cast<float>(n); break;
192 case SbxDATE:
193 case SbxDOUBLE:
194 p->nDouble = n; break;
195 case SbxCURRENCY:
196 p->nInt64 = n * CURRENCY_FACTOR; break;
197 case SbxSALINT64:
198 p->nInt64 = n; break;
199 case SbxDECIMAL:
200 case SbxBYREF | SbxDECIMAL:
201 ImpCreateDecimal( p )->setLong( n );
202 break;
204 case SbxBYREF | SbxSTRING:
205 case SbxSTRING:
206 case SbxLPSTR:
207 if( !p->pOUString )
208 p->pOUString = new OUString;
209 ImpCvtNum( static_cast<double>(n), 0, *p->pOUString );
210 break;
211 case SbxOBJECT:
213 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
214 if( pVal )
215 pVal->PutLong( n );
216 else
217 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT );
218 break;
220 case SbxBYREF | SbxCHAR:
221 if( n > SbxMAXCHAR )
223 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXCHAR;
225 else if( n < SbxMINCHAR )
227 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMINCHAR;
229 *p->pChar = static_cast<sal_Unicode>(n); break;
230 case SbxBYREF | SbxBYTE:
231 if( n > SbxMAXBYTE )
233 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXBYTE;
235 else if( n < 0 )
237 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 0;
239 *p->pByte = static_cast<sal_uInt8>(n); break;
240 case SbxBYREF | SbxINTEGER:
241 case SbxBYREF | SbxBOOL:
242 if( n > SbxMAXINT )
244 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXINT;
246 else if( n < SbxMININT )
248 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMININT;
250 *p->pInteger = static_cast<sal_Int16>(n); break;
251 case SbxBYREF | SbxERROR:
252 case SbxBYREF | SbxUSHORT:
253 if( n > SbxMAXUINT )
255 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXUINT;
257 else if( n < 0 )
259 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 0;
261 *p->pUShort = static_cast<sal_uInt16>(n); break;
262 case SbxBYREF | SbxLONG:
263 *p->pLong = n; break;
264 case SbxBYREF | SbxULONG:
265 if( n < 0 )
267 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 0;
269 *p->pULong = static_cast<sal_uInt32>(n); break;
270 case SbxBYREF | SbxSALINT64:
271 *p->pnInt64 = n; break;
272 case SbxBYREF | SbxSALUINT64:
273 if( n < 0 )
275 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); *p->puInt64 = 0;
277 else
278 *p->puInt64 = n;
279 break;
280 case SbxBYREF | SbxSINGLE:
281 *p->pSingle = static_cast<float>(n); break;
282 case SbxBYREF | SbxDATE:
283 case SbxBYREF | SbxDOUBLE:
284 *p->pDouble = static_cast<double>(n); break;
285 case SbxBYREF | SbxCURRENCY:
286 *p->pnInt64 = static_cast<sal_Int64>(n) * sal_Int64(CURRENCY_FACTOR); break;
287 default:
288 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
292 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */