tdf#153109 Use any_of to check for match in gciterator.cxx
[LibreOffice.git] / basic / source / sbx / sbxulng.cxx
blob5287cba7b9ae0d7db43e5c8de843e839dbf2a2c6
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 <comphelper/errcode.hxx>
23 #include <basic/sberrors.hxx>
24 #include "sbxconv.hxx"
26 sal_uInt32 ImpGetULong( const SbxValues* p )
28 SbxValues aTmp;
29 sal_uInt32 nRes;
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;
40 break;
41 case SbxBYTE:
42 nRes = p->nByte; break;
43 case SbxINTEGER:
44 case SbxBOOL:
45 if( p->nInteger < 0 )
47 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
49 else
50 nRes = p->nInteger;
51 break;
52 case SbxERROR:
53 case SbxUSHORT:
54 nRes = p->nUShort;
55 break;
56 case SbxLONG:
57 if( p->nLong < 0 )
59 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
61 else
62 nRes = p->nLong;
63 break;
64 case SbxULONG:
65 nRes = p->nULong; break;
66 case SbxSINGLE:
67 nRes = ImpDoubleToULong(p->nSingle);
68 break;
69 case SbxDATE:
70 case SbxDOUBLE:
71 case SbxSALINT64:
72 case SbxSALUINT64:
73 case SbxCURRENCY:
74 case SbxDECIMAL:
75 case SbxBYREF | SbxDECIMAL:
77 double dVal;
78 if( p->eType == SbxCURRENCY )
79 dVal = ImpCurrencyToDouble( p->nInt64 );
80 else if( p->eType == SbxSALINT64 )
81 dVal = static_cast< double >(p->nInt64);
82 else if( p->eType == SbxSALUINT64 )
83 dVal = ImpSalUInt64ToDouble( p->uInt64 );
84 else if( p->eType == SbxDECIMAL )
86 dVal = 0.0;
87 if( p->pDecimal )
88 p->pDecimal->getDouble( dVal );
90 else
91 dVal = p->nDouble;
93 nRes = ImpDoubleToULong(dVal);
94 break;
96 case SbxBYREF | SbxSTRING:
97 case SbxSTRING:
98 case SbxLPSTR:
99 if( !p->pOUString )
100 nRes = 0;
101 else
103 double d;
104 SbxDataType t;
105 if( ImpScan( *p->pOUString, d, t, nullptr ) != ERRCODE_NONE )
106 nRes = 0;
107 else
108 nRes = ImpDoubleToULong(d);
110 break;
111 case SbxOBJECT:
113 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
114 if( pVal )
115 nRes = pVal->GetULong();
116 else
118 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT ); nRes = 0;
120 break;
123 case SbxBYREF | SbxBYTE:
124 nRes = *p->pByte; break;
125 case SbxBYREF | SbxERROR:
126 case SbxBYREF | SbxUSHORT:
127 nRes = *p->pUShort; break;
128 case SbxBYREF | SbxULONG:
129 nRes = *p->pULong; break;
131 // from here on tests
132 case SbxBYREF | SbxCHAR:
133 aTmp.nChar = *p->pChar; goto ref;
134 case SbxBYREF | SbxINTEGER:
135 case SbxBYREF | SbxBOOL:
136 aTmp.nInteger = *p->pInteger; goto ref;
137 case SbxBYREF | SbxLONG:
138 aTmp.nLong = *p->pLong; goto ref;
139 case SbxBYREF | SbxSINGLE:
140 aTmp.nSingle = *p->pSingle; goto ref;
141 case SbxBYREF | SbxDATE:
142 case SbxBYREF | SbxDOUBLE:
143 aTmp.nDouble = *p->pDouble; goto ref;
144 case SbxBYREF | SbxCURRENCY:
145 case SbxBYREF | SbxSALINT64:
146 aTmp.nInt64 = *p->pnInt64; goto ref;
147 case SbxBYREF | SbxSALUINT64:
148 aTmp.uInt64 = *p->puInt64; goto ref;
149 ref:
150 aTmp.eType = SbxDataType( p->eType & 0x0FFF );
151 p = &aTmp; goto start;
153 default:
154 SbxBase::SetError( ERRCODE_BASIC_CONVERSION ); nRes = 0;
156 return nRes;
159 void ImpPutULong( SbxValues* p, sal_uInt32 n )
161 SbxValues aTmp;
162 start:
163 switch( +p->eType )
165 case SbxULONG:
166 p->nULong = n; break;
167 case SbxSINGLE:
168 p->nSingle = static_cast<float>(n); break;
169 case SbxDATE:
170 case SbxDOUBLE:
171 p->nDouble = n; break;
172 case SbxCURRENCY:
173 case SbxSALINT64:
174 aTmp.pnInt64 = &p->nInt64; goto direct;
175 case SbxSALUINT64:
176 p->uInt64 = n; break;
177 case SbxDECIMAL:
178 case SbxBYREF | SbxDECIMAL:
179 ImpCreateDecimal( p )->setULong( n );
180 break;
182 // from here on tests
183 case SbxCHAR:
184 aTmp.pChar = &p->nChar; goto direct;
185 case SbxUINT:
186 aTmp.pByte = &p->nByte; goto direct;
187 case SbxINTEGER:
188 case SbxBOOL:
189 aTmp.pInteger = &p->nInteger; goto direct;
190 case SbxLONG:
191 aTmp.pLong = &p->nLong; goto direct;
192 case SbxERROR:
193 case SbxUSHORT:
194 aTmp.pUShort = &p->nUShort; goto direct;
195 direct:
196 aTmp.eType = SbxDataType( p->eType | SbxBYREF );
197 p = &aTmp; goto start;
199 case SbxBYREF | SbxSTRING:
200 case SbxSTRING:
201 case SbxLPSTR:
202 if( !p->pOUString )
203 p->pOUString = new OUString;
204 ImpCvtNum( static_cast<double>(n), 0, *p->pOUString );
205 break;
206 case SbxOBJECT:
208 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
209 if( pVal )
210 pVal->PutULong( n );
211 else
212 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT );
213 break;
215 case SbxBYREF | SbxCHAR:
216 if( n > SbxMAXCHAR )
218 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXCHAR;
220 *p->pChar = static_cast<sal_Unicode>(n); break;
221 case SbxBYREF | SbxBYTE:
222 if( n > SbxMAXBYTE )
224 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXBYTE;
226 *p->pByte = static_cast<sal_uInt8>(n); break;
227 case SbxBYREF | SbxINTEGER:
228 case SbxBYREF | SbxBOOL:
229 if( n > SbxMAXINT )
231 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXINT;
233 *p->pInteger = static_cast<sal_Int16>(n); break;
234 case SbxBYREF | SbxERROR:
235 case SbxBYREF | SbxUSHORT:
236 if( n > SbxMAXUINT )
238 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXUINT;
240 *p->pUShort = static_cast<sal_uInt16>(n); break;
241 case SbxBYREF | SbxLONG:
242 if( n > SbxMAXLNG )
244 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXLNG;
246 *p->pLong = static_cast<sal_Int32>(n); break;
247 case SbxBYREF | SbxULONG:
248 *p->pULong = n; break;
249 case SbxBYREF | SbxSINGLE:
250 *p->pSingle = static_cast<float>(n); break;
251 case SbxBYREF | SbxDATE:
252 case SbxBYREF | SbxDOUBLE:
253 *p->pDouble = n; break;
254 case SbxBYREF | SbxCURRENCY:
255 *p->pnInt64 = CurFrom(n); break;
256 case SbxBYREF | SbxSALINT64:
257 *p->pnInt64 = n; break;
258 case SbxBYREF | SbxSALUINT64:
259 *p->puInt64 = n; break;
261 default:
262 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
266 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */