tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / basic / source / sbx / sbxlng.cxx
blob7d37473da915ab2b1516d237254ed6b1c337b268
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 #include <rtl/math.hxx>
28 sal_Int32 ImpGetLong( const SbxValues* p )
30 SbxValues aTmp;
31 sal_Int32 nRes;
32 start:
33 switch( +p->eType )
35 case SbxNULL:
36 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
37 [[fallthrough]];
38 case SbxEMPTY:
39 nRes = 0; break;
40 case SbxCHAR:
41 nRes = p->nChar; break;
42 case SbxBYTE:
43 nRes = p->nByte; break;
44 case SbxINTEGER:
45 case SbxBOOL:
46 nRes = p->nInteger; break;
47 case SbxERROR:
48 case SbxUSHORT:
49 nRes = p->nUShort; break;
50 case SbxLONG:
51 nRes = p->nLong; break;
52 case SbxULONG:
53 if( p->nULong > SbxMAXLNG )
55 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXLNG;
57 else
58 nRes = static_cast<sal_Int32>(p->nULong);
59 break;
60 case SbxSINGLE:
61 nRes = ImpDoubleToLong(p->nSingle);
62 break;
63 case SbxSALINT64:
64 nRes = p->nInt64;
65 break;
66 case SbxSALUINT64:
67 nRes = p->uInt64;
68 break;
69 case SbxCURRENCY:
70 nRes = CurTo<sal_Int32>(p->nInt64);
71 break;
72 case SbxDATE:
73 case SbxDOUBLE:
74 case SbxDECIMAL:
75 case SbxBYREF | SbxDECIMAL:
77 double dVal;
78 if( p->eType == SbxDECIMAL )
80 dVal = 0.0;
81 if( p->pDecimal )
82 p->pDecimal->getDouble( dVal );
84 else
85 dVal = p->nDouble;
87 nRes = ImpDoubleToLong(dVal);
88 break;
90 case SbxBYREF | SbxSTRING:
91 case SbxSTRING:
92 case SbxLPSTR:
93 if( !p->pOUString )
94 nRes = 0;
95 else
97 double d;
98 SbxDataType t;
99 if( ImpScan( *p->pOUString, d, t, nullptr ) != ERRCODE_NONE )
100 nRes = 0;
101 else
102 nRes = ImpDoubleToLong(d);
104 break;
105 case SbxOBJECT:
107 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
108 if( pVal )
109 nRes = pVal->GetLong();
110 else
112 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT ); nRes = 0;
114 break;
117 case SbxBYREF | SbxCHAR:
118 nRes = *p->pChar; break;
119 case SbxBYREF | SbxBYTE:
120 nRes = *p->pByte; break;
121 case SbxBYREF | SbxINTEGER:
122 case SbxBYREF | SbxBOOL:
123 nRes = *p->pInteger; break;
124 case SbxBYREF | SbxLONG:
125 nRes = *p->pLong; break;
127 // from here had to be tested
128 case SbxBYREF | SbxULONG:
129 aTmp.nULong = *p->pULong; goto ref;
130 case SbxBYREF | SbxERROR:
131 case SbxBYREF | SbxUSHORT:
132 aTmp.nUShort = *p->pUShort; goto ref;
133 case SbxBYREF | SbxSINGLE:
134 aTmp.nSingle = *p->pSingle; goto ref;
135 case SbxBYREF | SbxDATE:
136 case SbxBYREF | SbxDOUBLE:
137 aTmp.nDouble = *p->pDouble; goto ref;
138 case SbxBYREF | SbxCURRENCY:
139 case SbxBYREF | SbxSALINT64:
140 aTmp.nInt64 = *p->pnInt64; goto ref;
141 case SbxBYREF | SbxSALUINT64:
142 aTmp.uInt64 = *p->puInt64; goto ref;
144 ref:
145 aTmp.eType = SbxDataType( p->eType & 0x0FFF );
146 p = &aTmp; goto start;
148 default:
149 SbxBase::SetError( ERRCODE_BASIC_CONVERSION ); nRes = 0;
151 return nRes;
154 void ImpPutLong( SbxValues* p, sal_Int32 n )
156 SbxValues aTmp;
158 start:
159 switch( +p->eType )
161 // From here had to be tested
162 case SbxCHAR:
163 aTmp.pChar = &p->nChar; goto direct;
164 case SbxBYTE:
165 aTmp.pByte = &p->nByte; goto direct;
166 case SbxINTEGER:
167 case SbxBOOL:
168 aTmp.pInteger = &p->nInteger; goto direct;
169 case SbxULONG:
170 aTmp.pULong = &p->nULong; goto direct;
171 case SbxSALUINT64:
172 aTmp.puInt64 = &p->uInt64; goto direct;
173 case SbxERROR:
174 case SbxUSHORT:
175 aTmp.pUShort = &p->nUShort;
176 direct:
177 aTmp.eType = SbxDataType( p->eType | SbxBYREF );
178 p = &aTmp; goto start;
180 // from here no longer
181 case SbxLONG:
182 p->nLong = n; break;
183 case SbxSINGLE:
184 p->nSingle = static_cast<float>(n); break;
185 case SbxDATE:
186 case SbxDOUBLE:
187 p->nDouble = n; break;
188 case SbxCURRENCY:
189 p->nInt64 = CurFrom(n); break;
190 case SbxSALINT64:
191 p->nInt64 = n; break;
192 case SbxDECIMAL:
193 case SbxBYREF | SbxDECIMAL:
194 ImpCreateDecimal( p )->setLong( n );
195 break;
197 case SbxBYREF | SbxSTRING:
198 case SbxSTRING:
199 case SbxLPSTR:
200 if( !p->pOUString )
201 p->pOUString = new OUString;
202 ImpCvtNum( static_cast<double>(n), 0, *p->pOUString );
203 break;
204 case SbxOBJECT:
206 SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
207 if( pVal )
208 pVal->PutLong( n );
209 else
210 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT );
211 break;
213 case SbxBYREF | SbxCHAR:
214 if( n > SbxMAXCHAR )
216 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXCHAR;
218 else if( n < SbxMINCHAR )
220 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMINCHAR;
222 *p->pChar = static_cast<sal_Unicode>(n); break;
223 case SbxBYREF | SbxBYTE:
224 if( n > SbxMAXBYTE )
226 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXBYTE;
228 else if( n < 0 )
230 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 0;
232 *p->pByte = static_cast<sal_uInt8>(n); break;
233 case SbxBYREF | SbxINTEGER:
234 case SbxBYREF | SbxBOOL:
235 if( n > SbxMAXINT )
237 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXINT;
239 else if( n < SbxMININT )
241 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMININT;
243 *p->pInteger = static_cast<sal_Int16>(n); break;
244 case SbxBYREF | SbxERROR:
245 case SbxBYREF | SbxUSHORT:
246 if( n > SbxMAXUINT )
248 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXUINT;
250 else if( n < 0 )
252 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 0;
254 *p->pUShort = static_cast<sal_uInt16>(n); break;
255 case SbxBYREF | SbxLONG:
256 *p->pLong = n; break;
257 case SbxBYREF | SbxULONG:
258 if( n < 0 )
260 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 0;
262 *p->pULong = static_cast<sal_uInt32>(n); break;
263 case SbxBYREF | SbxSALINT64:
264 *p->pnInt64 = n; break;
265 case SbxBYREF | SbxSALUINT64:
266 if( n < 0 )
268 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); *p->puInt64 = 0;
270 else
271 *p->puInt64 = n;
272 break;
273 case SbxBYREF | SbxSINGLE:
274 *p->pSingle = static_cast<float>(n); break;
275 case SbxBYREF | SbxDATE:
276 case SbxBYREF | SbxDOUBLE:
277 *p->pDouble = static_cast<double>(n); break;
278 case SbxBYREF | SbxCURRENCY:
279 *p->pnInt64 = CurFrom(n); break;
280 default:
281 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
285 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */