merge the formfield patch from ooo-build
[ooovba.git] / basic / source / sbx / sbxlng.cxx
blob537694ecca08f6147a95a3c6b41e36268473c642
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: sbxlng.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_basic.hxx"
33 #include <tools/errcode.hxx>
34 #include <basic/sbx.hxx>
35 #include "sbxconv.hxx"
37 INT32 ImpGetLong( const SbxValues* p )
39 SbxValues aTmp;
40 INT32 nRes;
41 start:
42 switch( +p->eType )
44 case SbxNULL:
45 SbxBase::SetError( SbxERR_CONVERSION );
46 case SbxEMPTY:
47 nRes = 0; break;
48 case SbxCHAR:
49 nRes = p->nChar; break;
50 case SbxBYTE:
51 nRes = p->nByte; break;
52 case SbxINTEGER:
53 case SbxBOOL:
54 nRes = p->nInteger; break;
55 case SbxERROR:
56 case SbxUSHORT:
57 nRes = p->nUShort; break;
58 case SbxLONG:
59 nRes = p->nLong; break;
60 case SbxULONG:
61 if( p->nULong > SbxMAXLNG )
63 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXLNG;
65 else
66 nRes = (INT32) p->nULong;
67 break;
68 case SbxSINGLE:
69 if( p->nSingle > SbxMAXLNG )
71 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXLNG;
73 else if( p->nSingle < SbxMINLNG )
75 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMINLNG;
77 else
78 nRes = (INT32) ImpRound( p->nSingle );
79 break;
80 case SbxDATE:
81 case SbxDOUBLE:
82 case SbxLONG64:
83 case SbxULONG64:
84 case SbxSALINT64:
85 case SbxSALUINT64:
86 case SbxCURRENCY:
87 case SbxDECIMAL:
88 case SbxBYREF | SbxDECIMAL:
90 double dVal;
91 if( p->eType == SbxCURRENCY )
92 dVal = ImpCurrencyToDouble( p->nLong64 );
93 else if( p->eType == SbxLONG64 )
94 dVal = ImpINT64ToDouble( p->nLong64 );
95 else if( p->eType == SbxULONG64 )
96 dVal = ImpUINT64ToDouble( p->nULong64 );
97 else if( p->eType == SbxSALINT64 )
98 dVal = static_cast< double >(p->nInt64);
99 else if( p->eType == SbxSALUINT64 )
100 dVal = ImpSalUInt64ToDouble( p->uInt64 );
101 else if( p->eType == SbxDECIMAL )
103 dVal = 0.0;
104 if( p->pDecimal )
105 p->pDecimal->getDouble( dVal );
107 else
108 dVal = p->nDouble;
110 if( dVal > SbxMAXLNG )
112 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXLNG;
114 else if( dVal < SbxMINLNG )
116 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMINLNG;
118 else
119 nRes = (INT32) ImpRound( dVal );
120 break;
122 case SbxBYREF | SbxSTRING:
123 case SbxSTRING:
124 case SbxLPSTR:
125 if( !p->pString )
126 nRes = 0;
127 else
129 double d;
130 SbxDataType t;
131 if( ImpScan( *p->pString, d, t, NULL ) != SbxERR_OK )
132 nRes = 0;
133 else if( d > SbxMAXLNG )
135 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXLNG;
137 else if( d < SbxMINLNG )
139 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMINLNG;
141 else
142 nRes = (INT32) ImpRound( d );
144 break;
145 case SbxOBJECT:
147 SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
148 if( pVal )
149 nRes = pVal->GetLong();
150 else
152 SbxBase::SetError( SbxERR_NO_OBJECT ); nRes = 0;
154 break;
157 case SbxBYREF | SbxCHAR:
158 nRes = *p->pChar; break;
159 case SbxBYREF | SbxBYTE:
160 nRes = *p->pByte; break;
161 case SbxBYREF | SbxINTEGER:
162 case SbxBYREF | SbxBOOL:
163 nRes = *p->pInteger; break;
164 case SbxBYREF | SbxLONG:
165 nRes = *p->pLong; break;
167 // ab hier muss getestet werden
168 case SbxBYREF | SbxULONG:
169 aTmp.nULong = *p->pULong; goto ref;
170 case SbxBYREF | SbxERROR:
171 case SbxBYREF | SbxUSHORT:
172 aTmp.nUShort = *p->pUShort; goto ref;
173 case SbxBYREF | SbxSINGLE:
174 aTmp.nSingle = *p->pSingle; goto ref;
175 case SbxBYREF | SbxDATE:
176 case SbxBYREF | SbxDOUBLE:
177 aTmp.nDouble = *p->pDouble; goto ref;
178 case SbxBYREF | SbxSALINT64:
179 aTmp.nInt64 = *p->pnInt64; goto ref;
180 case SbxBYREF | SbxSALUINT64:
181 aTmp.uInt64 = *p->puInt64; goto ref;
182 case SbxBYREF | SbxULONG64:
183 aTmp.nULong64 = *p->pULong64; goto ref;
184 case SbxBYREF | SbxLONG64:
185 case SbxBYREF | SbxCURRENCY:
186 aTmp.nLong64 = *p->pLong64; goto ref;
187 ref:
188 aTmp.eType = SbxDataType( p->eType & 0x0FFF );
189 p = &aTmp; goto start;
191 default:
192 SbxBase::SetError( SbxERR_CONVERSION ); nRes = 0;
194 return nRes;
197 void ImpPutLong( SbxValues* p, INT32 n )
199 SbxValues aTmp;
201 start:
202 switch( +p->eType )
204 // Ab hier muss getestet werden
205 case SbxCHAR:
206 aTmp.pChar = &p->nChar; goto direct;
207 case SbxBYTE:
208 aTmp.pByte = &p->nByte; goto direct;
209 case SbxINTEGER:
210 case SbxBOOL:
211 aTmp.pInteger = &p->nInteger; goto direct;
212 case SbxULONG64:
213 aTmp.pULong64 = &p->nULong64; goto direct;
214 case SbxLONG64:
215 case SbxCURRENCY:
216 aTmp.pLong64 = &p->nLong64; goto direct;
217 case SbxULONG:
218 aTmp.pULong = &p->nULong; goto direct;
219 case SbxSALUINT64:
220 aTmp.puInt64 = &p->uInt64; goto direct;
221 case SbxERROR:
222 case SbxUSHORT:
223 aTmp.pUShort = &p->nUShort;
224 direct:
225 aTmp.eType = SbxDataType( p->eType | SbxBYREF );
226 p = &aTmp; goto start;
228 // ab hier nicht mehr
229 case SbxLONG:
230 p->nLong = n; break;
231 case SbxSINGLE:
232 p->nSingle = (float) n; break;
233 case SbxDATE:
234 case SbxDOUBLE:
235 p->nDouble = n; break;
236 case SbxSALINT64:
237 p->nInt64 = n; break;
238 case SbxDECIMAL:
239 case SbxBYREF | SbxDECIMAL:
240 ImpCreateDecimal( p )->setLong( n );
241 break;
243 case SbxBYREF | SbxSTRING:
244 case SbxSTRING:
245 case SbxLPSTR:
246 if( !p->pString )
247 p->pString = new XubString;
248 ImpCvtNum( (double) n, 0, *p->pString );
249 break;
250 case SbxOBJECT:
252 SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
253 if( pVal )
254 pVal->PutLong( n );
255 else
256 SbxBase::SetError( SbxERR_NO_OBJECT );
257 break;
259 case SbxBYREF | SbxCHAR:
260 if( n > SbxMAXCHAR )
262 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXCHAR;
264 else if( n < SbxMINCHAR )
266 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINCHAR;
268 *p->pChar = (xub_Unicode) n; break;
269 case SbxBYREF | SbxBYTE:
270 if( n > SbxMAXBYTE )
272 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXBYTE;
274 else if( n < 0 )
276 SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
278 *p->pByte = (BYTE) n; break;
279 case SbxBYREF | SbxINTEGER:
280 case SbxBYREF | SbxBOOL:
281 if( n > SbxMAXINT )
283 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXINT;
285 else if( n < SbxMININT )
287 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMININT;
289 *p->pInteger = (INT16) n; break;
290 case SbxBYREF | SbxERROR:
291 case SbxBYREF | SbxUSHORT:
292 if( n > SbxMAXUINT )
294 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXUINT;
296 else if( n < 0 )
298 SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
300 *p->pUShort = (UINT16) n; break;
301 case SbxBYREF | SbxLONG:
302 *p->pLong = n; break;
303 case SbxBYREF | SbxULONG:
304 if( n < 0 )
306 SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
308 *p->pULong = (UINT32) n; break;
309 case SbxBYREF | SbxSALINT64:
310 *p->pnInt64 = n; break;
311 case SbxBYREF | SbxSALUINT64:
312 if( n < 0 )
314 SbxBase::SetError( SbxERR_OVERFLOW ); *p->puInt64 = 0;
316 else
317 *p->puInt64 = n;
318 break;
319 case SbxBYREF | SbxSINGLE:
320 *p->pSingle = (float) n; break;
321 case SbxBYREF | SbxDATE:
322 case SbxBYREF | SbxDOUBLE:
323 *p->pDouble = (double) n; break;
324 case SbxBYREF | SbxCURRENCY:
325 double d;
326 if( n > SbxMAXCURR )
328 SbxBase::SetError( SbxERR_OVERFLOW ); d = SbxMAXCURR;
330 else if( n < SbxMINCURR )
332 SbxBase::SetError( SbxERR_OVERFLOW ); d = SbxMINCURR;
334 else
336 d = n;
338 *p->pLong64 = ImpDoubleToCurrency( d ); break;
340 default:
341 SbxBase::SetError( SbxERR_CONVERSION );