Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / basic / source / sbx / sbxlng.cxx
blob90cf5f9e0038db5122308237f84cbfd100d41b17
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 <tools/errcode.hxx>
21 #include <basic/sbx.hxx>
22 #include "sbxconv.hxx"
24 sal_Int32 ImpGetLong( const SbxValues* p )
26 SbxValues aTmp;
27 sal_Int32 nRes;
28 start:
29 switch( +p->eType )
31 case SbxNULL:
32 SbxBase::SetError( SbxERR_CONVERSION );
33 case SbxEMPTY:
34 nRes = 0; break;
35 case SbxCHAR:
36 nRes = p->nChar; break;
37 case SbxBYTE:
38 nRes = p->nByte; break;
39 case SbxINTEGER:
40 case SbxBOOL:
41 nRes = p->nInteger; break;
42 case SbxERROR:
43 case SbxUSHORT:
44 nRes = p->nUShort; break;
45 case SbxLONG:
46 nRes = p->nLong; break;
47 case SbxULONG:
48 if( p->nULong > SbxMAXLNG )
50 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXLNG;
52 else
53 nRes = (sal_Int32) p->nULong;
54 break;
55 case SbxSINGLE:
56 if( p->nSingle > SbxMAXLNG )
58 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXLNG;
60 else if( p->nSingle < SbxMINLNG )
62 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMINLNG;
64 else
65 nRes = (sal_Int32) ImpRound( p->nSingle );
66 break;
67 case SbxSALINT64:
68 nRes = p->nInt64;
69 break;
70 case SbxSALUINT64:
71 nRes = p->uInt64;
72 break;
73 case SbxCURRENCY:
75 sal_Int64 tstVal = p->nInt64 / CURRENCY_FACTOR;
76 nRes = (sal_Int32) (tstVal);
77 if( tstVal < SbxMINLNG || SbxMAXLNG < tstVal ) SbxBase::SetError( SbxERR_OVERFLOW );
78 if( SbxMAXLNG < tstVal ) nRes = SbxMAXLNG;
79 if( tstVal < SbxMINLNG ) nRes = SbxMINLNG;
80 break;
82 case SbxDATE:
83 case SbxDOUBLE:
84 case SbxDECIMAL:
85 case SbxBYREF | SbxDECIMAL:
87 double dVal;
88 if( p->eType == SbxDECIMAL )
90 dVal = 0.0;
91 if( p->pDecimal )
92 p->pDecimal->getDouble( dVal );
94 else
95 dVal = p->nDouble;
97 if( dVal > SbxMAXLNG )
99 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXLNG;
101 else if( dVal < SbxMINLNG )
103 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMINLNG;
105 else
106 nRes = (sal_Int32) ImpRound( dVal );
107 break;
109 case SbxBYREF | SbxSTRING:
110 case SbxSTRING:
111 case SbxLPSTR:
112 if( !p->pOUString )
113 nRes = 0;
114 else
116 double d;
117 SbxDataType t;
118 if( ImpScan( *p->pOUString, d, t, NULL ) != SbxERR_OK )
119 nRes = 0;
120 else if( d > SbxMAXLNG )
122 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXLNG;
124 else if( d < SbxMINLNG )
126 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMINLNG;
128 else
129 nRes = (sal_Int32) ImpRound( d );
131 break;
132 case SbxOBJECT:
134 SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
135 if( pVal )
136 nRes = pVal->GetLong();
137 else
139 SbxBase::SetError( SbxERR_NO_OBJECT ); nRes = 0;
141 break;
144 case SbxBYREF | SbxCHAR:
145 nRes = *p->pChar; break;
146 case SbxBYREF | SbxBYTE:
147 nRes = *p->pByte; break;
148 case SbxBYREF | SbxINTEGER:
149 case SbxBYREF | SbxBOOL:
150 nRes = *p->pInteger; break;
151 case SbxBYREF | SbxLONG:
152 nRes = *p->pLong; break;
154 // from here had to be tested
155 case SbxBYREF | SbxULONG:
156 aTmp.nULong = *p->pULong; goto ref;
157 case SbxBYREF | SbxERROR:
158 case SbxBYREF | SbxUSHORT:
159 aTmp.nUShort = *p->pUShort; goto ref;
160 case SbxBYREF | SbxSINGLE:
161 aTmp.nSingle = *p->pSingle; goto ref;
162 case SbxBYREF | SbxDATE:
163 case SbxBYREF | SbxDOUBLE:
164 aTmp.nDouble = *p->pDouble; goto ref;
165 case SbxBYREF | SbxCURRENCY:
166 case SbxBYREF | SbxSALINT64:
167 aTmp.nInt64 = *p->pnInt64; goto ref;
168 case SbxBYREF | SbxSALUINT64:
169 aTmp.uInt64 = *p->puInt64; goto ref;
171 ref:
172 aTmp.eType = SbxDataType( p->eType & 0x0FFF );
173 p = &aTmp; goto start;
175 default:
176 SbxBase::SetError( SbxERR_CONVERSION ); nRes = 0;
178 return nRes;
181 void ImpPutLong( SbxValues* p, sal_Int32 n )
183 SbxValues aTmp;
185 start:
186 switch( +p->eType )
188 // From here had to be tested
189 case SbxCHAR:
190 aTmp.pChar = &p->nChar; goto direct;
191 case SbxBYTE:
192 aTmp.pByte = &p->nByte; goto direct;
193 case SbxINTEGER:
194 case SbxBOOL:
195 aTmp.pInteger = &p->nInteger; goto direct;
196 case SbxULONG:
197 aTmp.pULong = &p->nULong; goto direct;
198 case SbxSALUINT64:
199 aTmp.puInt64 = &p->uInt64; goto direct;
200 case SbxERROR:
201 case SbxUSHORT:
202 aTmp.pUShort = &p->nUShort;
203 direct:
204 aTmp.eType = SbxDataType( p->eType | SbxBYREF );
205 p = &aTmp; goto start;
207 // from here no longer
208 case SbxLONG:
209 p->nLong = n; break;
210 case SbxSINGLE:
211 p->nSingle = (float) n; break;
212 case SbxDATE:
213 case SbxDOUBLE:
214 p->nDouble = n; break;
215 case SbxCURRENCY:
216 p->nInt64 = n * CURRENCY_FACTOR; break;
217 case SbxSALINT64:
218 p->nInt64 = n; break;
219 case SbxDECIMAL:
220 case SbxBYREF | SbxDECIMAL:
221 ImpCreateDecimal( p )->setLong( n );
222 break;
224 case SbxBYREF | SbxSTRING:
225 case SbxSTRING:
226 case SbxLPSTR:
227 if( !p->pOUString )
228 p->pOUString = new ::rtl::OUString;
229 ImpCvtNum( (double) n, 0, *p->pOUString );
230 break;
231 case SbxOBJECT:
233 SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
234 if( pVal )
235 pVal->PutLong( n );
236 else
237 SbxBase::SetError( SbxERR_NO_OBJECT );
238 break;
240 case SbxBYREF | SbxCHAR:
241 if( n > SbxMAXCHAR )
243 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXCHAR;
245 else if( n < SbxMINCHAR )
247 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINCHAR;
249 *p->pChar = (sal_Unicode) n; break;
250 case SbxBYREF | SbxBYTE:
251 if( n > SbxMAXBYTE )
253 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXBYTE;
255 else if( n < 0 )
257 SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
259 *p->pByte = (sal_uInt8) n; break;
260 case SbxBYREF | SbxINTEGER:
261 case SbxBYREF | SbxBOOL:
262 if( n > SbxMAXINT )
264 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXINT;
266 else if( n < SbxMININT )
268 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMININT;
270 *p->pInteger = (sal_Int16) n; break;
271 case SbxBYREF | SbxERROR:
272 case SbxBYREF | SbxUSHORT:
273 if( n > SbxMAXUINT )
275 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXUINT;
277 else if( n < 0 )
279 SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
281 *p->pUShort = (sal_uInt16) n; break;
282 case SbxBYREF | SbxLONG:
283 *p->pLong = n; break;
284 case SbxBYREF | SbxULONG:
285 if( n < 0 )
287 SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
289 *p->pULong = (sal_uInt32) n; break;
290 case SbxBYREF | SbxSALINT64:
291 *p->pnInt64 = n; break;
292 case SbxBYREF | SbxSALUINT64:
293 if( n < 0 )
295 SbxBase::SetError( SbxERR_OVERFLOW ); *p->puInt64 = 0;
297 else
298 *p->puInt64 = n;
299 break;
300 case SbxBYREF | SbxSINGLE:
301 *p->pSingle = (float) n; break;
302 case SbxBYREF | SbxDATE:
303 case SbxBYREF | SbxDOUBLE:
304 *p->pDouble = (double) n; break;
305 case SbxBYREF | SbxCURRENCY:
306 *p->pnInt64 = (sal_Int64)n * (sal_Int64)CURRENCY_FACTOR; break;
307 default:
308 SbxBase::SetError( SbxERR_CONVERSION );
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */