merge the formfield patch from ooo-build
[ooovba.git] / basic / source / sbx / sbxsng.cxx
blob80d796495f9c574a9cf8aebf45f524bfd9150d4a
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: sbxsng.cxx,v $
10 * $Revision: 1.8 $
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 float ImpGetSingle( const SbxValues* p )
39 SbxValues aTmp;
40 float 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 = (float) p->nLong; break;
60 case SbxULONG:
61 nRes = (float) p->nULong; break;
62 case SbxSINGLE:
63 nRes = p->nSingle; break;
64 case SbxSALINT64:
65 nRes = (float) p->nInt64; break;
66 case SbxSALUINT64:
67 nRes = (float) ImpSalUInt64ToDouble( p->uInt64 ); break;
68 case SbxDECIMAL:
69 case SbxBYREF | SbxDECIMAL:
70 if( p->pDecimal )
71 p->pDecimal->getSingle( nRes );
72 else
73 nRes = 0.0;
74 break;
75 case SbxDATE:
76 case SbxDOUBLE:
77 case SbxLONG64:
78 case SbxULONG64:
79 case SbxCURRENCY:
81 double dVal;
82 if( p->eType == SbxCURRENCY )
83 dVal = ImpCurrencyToDouble( p->nLong64 );
84 else if( p->eType == SbxLONG64 )
85 dVal = ImpINT64ToDouble( p->nLong64 );
86 else if( p->eType == SbxULONG64 )
87 dVal = ImpUINT64ToDouble( p->nULong64 );
88 else
89 dVal = p->nDouble;
91 if( dVal > SbxMAXSNG )
93 SbxBase::SetError( SbxERR_OVERFLOW );
94 nRes = static_cast< float >(SbxMAXSNG);
96 else if( dVal < SbxMINSNG )
98 SbxBase::SetError( SbxERR_OVERFLOW );
99 nRes = static_cast< float >(SbxMINSNG);
101 else if( dVal > 0 && dVal < SbxMAXSNG2 )
103 SbxBase::SetError( SbxERR_OVERFLOW );
104 nRes = static_cast< float >(SbxMAXSNG2);
106 else if( dVal < 0 && dVal > SbxMINSNG2 )
108 SbxBase::SetError( SbxERR_OVERFLOW );
109 nRes = static_cast< float >(SbxMINSNG2);
111 else
112 nRes = (float) dVal;
113 break;
115 case SbxBYREF | SbxSTRING:
116 case SbxSTRING:
117 case SbxLPSTR:
118 if( !p->pString )
119 nRes = 0;
120 else
122 double d;
123 SbxDataType t;
124 if( ImpScan( *p->pString, d, t, NULL ) != SbxERR_OK )
125 nRes = 0;
126 else if( d > SbxMAXSNG )
128 SbxBase::SetError( SbxERR_OVERFLOW );
129 nRes = static_cast< float >(SbxMAXSNG);
131 else if( d < SbxMINSNG )
133 SbxBase::SetError( SbxERR_OVERFLOW );
134 nRes = static_cast< float >(SbxMINSNG);
136 else
137 nRes = (float) d;
139 break;
140 case SbxOBJECT:
142 SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
143 if( pVal )
144 nRes = pVal->GetSingle();
145 else
147 SbxBase::SetError( SbxERR_NO_OBJECT ); nRes = 0;
149 break;
152 case SbxBYREF | SbxCHAR:
153 nRes = *p->pChar; break;
154 case SbxBYREF | SbxBYTE:
155 nRes = *p->pByte; break;
156 case SbxBYREF | SbxINTEGER:
157 case SbxBYREF | SbxBOOL:
158 nRes = *p->pInteger; break;
159 case SbxBYREF | SbxLONG:
160 nRes = (float) *p->pLong; break;
161 case SbxBYREF | SbxULONG:
162 nRes = (float) *p->pULong; break;
163 case SbxBYREF | SbxERROR:
164 case SbxBYREF | SbxUSHORT:
165 nRes = *p->pUShort; break;
166 case SbxBYREF | SbxSINGLE:
167 nRes = *p->pSingle; break;
168 // ab hier muss getestet werden
169 case SbxBYREF | SbxDATE:
170 case SbxBYREF | SbxDOUBLE:
171 aTmp.nDouble = *p->pDouble; goto ref;
172 case SbxBYREF | SbxULONG64:
173 aTmp.nULong64 = *p->pULong64; goto ref;
174 case SbxBYREF | SbxLONG64:
175 case SbxBYREF | SbxSALINT64:
176 nRes = (float) *p->pnInt64; break;
177 case SbxBYREF | SbxSALUINT64:
178 nRes = (float) ImpSalUInt64ToDouble( *p->puInt64 ); break;
179 case SbxBYREF | SbxCURRENCY:
180 aTmp.nLong64 = *p->pLong64; goto ref;
181 ref:
182 aTmp.eType = SbxDataType( p->eType & 0x0FFF );
183 p = &aTmp; goto start;
185 default:
186 SbxBase::SetError( SbxERR_CONVERSION ); nRes = 0;
188 return nRes;
191 void ImpPutSingle( SbxValues* p, float n )
193 SbxValues aTmp;
194 start:
195 switch( +p->eType )
197 case SbxCHAR:
198 aTmp.pChar = &p->nChar; goto direct;
199 case SbxBYTE:
200 aTmp.pByte = &p->nByte; goto direct;
201 case SbxINTEGER:
202 case SbxBOOL:
203 aTmp.pInteger = &p->nInteger; goto direct;
204 case SbxLONG:
205 aTmp.pLong = &p->nLong; goto direct;
206 case SbxULONG:
207 aTmp.pULong = &p->nULong; goto direct;
208 case SbxERROR:
209 case SbxUSHORT:
210 aTmp.pUShort = &p->nUShort; goto direct;
211 case SbxULONG64:
212 aTmp.pULong64 = &p->nULong64; goto direct;
213 case SbxLONG64:
214 case SbxCURRENCY:
215 aTmp.pLong64 = &p->nLong64; goto direct;
216 case SbxSALINT64:
217 aTmp.pnInt64 = &p->nInt64; goto direct;
218 case SbxSALUINT64:
219 aTmp.puInt64 = &p->uInt64; goto direct;
220 case SbxDECIMAL:
221 case SbxBYREF | SbxDECIMAL:
223 SbxDecimal* pDec = ImpCreateDecimal( p );
224 if( !pDec->setSingle( n ) )
225 SbxBase::SetError( SbxERR_OVERFLOW );
226 break;
228 direct:
229 aTmp.eType = SbxDataType( p->eType | SbxBYREF );
230 p = &aTmp; goto start;
232 // keine Tests ab hier
233 case SbxSINGLE:
234 p->nSingle = n; break;
235 case SbxDATE:
236 case SbxDOUBLE:
237 p->nDouble = n; break;
239 case SbxBYREF | SbxSTRING:
240 case SbxSTRING:
241 case SbxLPSTR:
243 if( !p->pString )
244 p->pString = new XubString;
245 ImpCvtNum( (double) n, 6, *p->pString );
246 break;
248 case SbxOBJECT:
250 SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
251 if( pVal )
252 pVal->PutSingle( n );
253 else
254 SbxBase::SetError( SbxERR_NO_OBJECT );
255 break;
257 case SbxBYREF | SbxCHAR:
258 if( n > SbxMAXCHAR )
260 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXCHAR;
262 else if( n < SbxMINCHAR )
264 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINCHAR;
266 *p->pChar = (xub_Unicode) n; break;
267 case SbxBYREF | SbxBYTE:
268 if( n > SbxMAXBYTE )
270 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXBYTE;
272 else if( n < 0 )
274 SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
276 *p->pByte = (BYTE) n; break;
277 case SbxBYREF | SbxINTEGER:
278 case SbxBYREF | SbxBOOL:
279 if( n > SbxMAXINT )
281 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXINT;
283 else if( n < SbxMININT )
285 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMININT;
287 *p->pInteger = (INT16) n; break;
288 case SbxBYREF | SbxERROR:
289 case SbxBYREF | SbxUSHORT:
290 if( n > SbxMAXUINT )
292 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXUINT;
294 else if( n < 0 )
296 SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
298 *p->pUShort = (UINT16) n; break;
299 case SbxBYREF | SbxLONG:
301 INT32 i;
302 if( n > SbxMAXLNG )
304 SbxBase::SetError( SbxERR_OVERFLOW ); i = SbxMAXLNG;
306 else if( n < SbxMINLNG )
308 SbxBase::SetError( SbxERR_OVERFLOW ); i = SbxMINLNG;
310 else
312 i = sal::static_int_cast< INT32 >(n);
314 *p->pLong = i; break;
316 case SbxBYREF | SbxULONG:
318 UINT32 i;
319 if( n > SbxMAXULNG )
321 SbxBase::SetError( SbxERR_OVERFLOW ); i = SbxMAXULNG;
323 else if( n < 0 )
325 SbxBase::SetError( SbxERR_OVERFLOW ); i = 0;
327 else
329 i = sal::static_int_cast< UINT32 >(n);
331 *p->pULong = i; break;
333 case SbxBYREF | SbxSINGLE:
334 *p->pSingle = n; break;
335 case SbxBYREF | SbxDATE:
336 case SbxBYREF | SbxDOUBLE:
337 *p->pDouble = (double) n; break;
338 case SbxBYREF | SbxSALINT64:
339 *p->pnInt64 = ImpDoubleToSalInt64( (double) n ); break;
340 case SbxBYREF | SbxSALUINT64:
341 *p->puInt64 = ImpDoubleToSalUInt64( (double) n ); break;
342 case SbxBYREF | SbxCURRENCY:
343 double d;
344 if( n > SbxMAXCURR )
346 SbxBase::SetError( SbxERR_OVERFLOW ); d = SbxMAXCURR;
348 else if( n < SbxMINCURR )
350 SbxBase::SetError( SbxERR_OVERFLOW ); d = SbxMINCURR;
352 else
354 d = n;
356 *p->pLong64 = ImpDoubleToCurrency( n ); break;
358 default:
359 SbxBase::SetError( SbxERR_CONVERSION );