xmlsecurity: fix --without-system-nss build
[LibreOffice.git] / basic / source / sbx / sbxuint.cxx
blob4593f72d22d387b83c73999967ec487f70087b3b
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_uInt16 ImpGetUShort( const SbxValues* p )
28 SbxValues aTmp;
29 sal_uInt16 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 > SbxMAXUINT )
59 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXUINT;
61 else if( p->nLong < 0 )
63 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
65 else
66 nRes = static_cast<sal_uInt16>(p->nLong);
67 break;
68 case SbxULONG:
69 if( p->nULong > SbxMAXUINT )
71 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXUINT;
73 else
74 nRes = static_cast<sal_uInt16>(p->nULong);
75 break;
76 case SbxCURRENCY:
77 nRes = CurTo<sal_uInt16>(p->nInt64);
78 break;
79 case SbxSALINT64:
80 if( p->nInt64 > SbxMAXUINT )
82 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXUINT;
84 else if( p->nInt64 < 0 )
86 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
88 else
89 nRes = static_cast<sal_uInt16>(p->nInt64);
90 break;
91 case SbxSALUINT64:
92 if( p->uInt64 > SbxMAXUINT )
94 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXUINT;
96 else
97 nRes = static_cast<sal_uInt16>(p->uInt64);
98 break;
99 case SbxSINGLE:
100 nRes = ImpDoubleToUShort(p->nSingle);
101 break;
102 case SbxDATE:
103 case SbxDOUBLE:
104 case SbxDECIMAL:
105 case SbxBYREF | SbxDECIMAL:
107 double dVal;
108 if( p->eType == SbxDECIMAL )
110 dVal = 0.0;
111 if( p->pDecimal )
112 p->pDecimal->getDouble( dVal );
114 else
115 dVal = p->nDouble;
117 nRes = ImpDoubleToUShort(dVal);
118 break;
120 case SbxBYREF | SbxSTRING:
121 case SbxSTRING:
122 case SbxLPSTR:
123 if( !p->pOUString )
124 nRes = 0;
125 else
127 double d;
128 SbxDataType t;
129 if( ImpScan( *p->pOUString, d, t, nullptr ) != ERRCODE_NONE )
130 nRes = 0;
131 else
132 nRes = ImpDoubleToUShort(d);
134 break;
135 case SbxOBJECT:
137 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
138 if( pVal )
139 nRes = pVal->GetUShort();
140 else
142 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT ); nRes = 0;
144 break;
147 case SbxBYREF | SbxBYTE:
148 nRes = *p->pByte; break;
149 case SbxBYREF | SbxERROR:
150 case SbxBYREF | SbxUSHORT:
151 nRes = *p->pUShort; break;
153 // from here on will be tested
154 case SbxBYREF | SbxCHAR:
155 aTmp.nChar = *p->pChar; goto ref;
156 case SbxBYREF | SbxINTEGER:
157 case SbxBYREF | SbxBOOL:
158 aTmp.nInteger = *p->pInteger; goto ref;
159 case SbxBYREF | SbxLONG:
160 aTmp.nLong = *p->pLong; goto ref;
161 case SbxBYREF | SbxULONG:
162 aTmp.nULong = *p->pULong; goto ref;
163 case SbxBYREF | SbxSINGLE:
164 aTmp.nSingle = *p->pSingle; goto ref;
165 case SbxBYREF | SbxDATE:
166 case SbxBYREF | SbxDOUBLE:
167 aTmp.nDouble = *p->pDouble; goto ref;
168 case SbxBYREF | SbxCURRENCY:
169 case SbxBYREF | SbxSALINT64:
170 aTmp.nInt64 = *p->pnInt64; goto ref;
171 case SbxBYREF | SbxSALUINT64:
172 aTmp.uInt64 = *p->puInt64; goto ref;
173 ref:
174 aTmp.eType = SbxDataType( p->eType & 0x0FFF );
175 p = &aTmp; goto start;
177 default:
178 SbxBase::SetError( ERRCODE_BASIC_CONVERSION ); nRes = 0;
180 return nRes;
183 void ImpPutUShort( SbxValues* p, sal_uInt16 n )
185 SbxValues aTmp;
187 start:
188 switch( +p->eType )
190 case SbxERROR:
191 case SbxUSHORT:
192 p->nUShort = n; break;
193 case SbxLONG:
194 p->nLong = n; break;
195 case SbxULONG:
196 p->nULong = n; break;
197 case SbxSINGLE:
198 p->nSingle = n; break;
199 case SbxDATE:
200 case SbxDOUBLE:
201 p->nDouble = n; break;
202 case SbxCURRENCY:
203 p->nInt64 = CurFrom(n); break;
204 case SbxSALINT64:
205 p->nInt64 = n; break;
206 case SbxSALUINT64:
207 p->uInt64 = n; break;
208 case SbxDECIMAL:
209 case SbxBYREF | SbxDECIMAL:
210 ImpCreateDecimal( p )->setUInt( n );
211 break;
213 // from here on tests
214 case SbxCHAR:
215 aTmp.pChar = &p->nChar; goto direct;
216 case SbxBYTE:
217 aTmp.pByte = &p->nByte; goto direct;
218 case SbxINTEGER:
219 case SbxBOOL:
220 aTmp.pInteger = &p->nInteger;
221 direct:
222 aTmp.eType = SbxDataType( p->eType | SbxBYREF );
223 p = &aTmp; goto start;
225 case SbxBYREF | SbxSTRING:
226 case SbxSTRING:
227 case SbxLPSTR:
228 if( !p->pOUString )
229 p->pOUString = new OUString;
230 ImpCvtNum( static_cast<double>(n), 0, *p->pOUString );
231 break;
232 case SbxOBJECT:
234 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
235 if( pVal )
236 pVal->PutUShort( n );
237 else
238 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT );
239 break;
242 case SbxBYREF | SbxCHAR:
243 *p->pChar = static_cast<sal_Unicode>(n); break;
244 case SbxBYREF | SbxBYTE:
245 if( n > SbxMAXBYTE )
247 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXBYTE;
249 *p->pByte = static_cast<sal_uInt8>(n); break;
250 case SbxBYREF | SbxINTEGER:
251 case SbxBYREF | SbxBOOL:
252 if( n > SbxMAXINT )
254 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXINT;
256 *p->pInteger = static_cast<sal_Int16>(n); break;
257 case SbxBYREF | SbxERROR:
258 case SbxBYREF | SbxUSHORT:
259 *p->pUShort = n; break;
260 case SbxBYREF | SbxLONG:
261 *p->pLong = n; break;
262 case SbxBYREF | SbxULONG:
263 *p->pULong = n; break;
264 case SbxBYREF | SbxSINGLE:
265 *p->pSingle = n; break;
266 case SbxBYREF | SbxDATE:
267 case SbxBYREF | SbxDOUBLE:
268 *p->pDouble = n; break;
269 case SbxBYREF | SbxCURRENCY:
270 *p->pnInt64 = CurFrom(n); break;
271 case SbxBYREF | SbxSALINT64:
272 *p->pnInt64 = n; break;
273 case SbxBYREF | SbxSALUINT64:
274 *p->puInt64 = n; break;
276 default:
277 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
281 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */