update dev300-m58
[ooovba.git] / basic / source / sbx / sbxdbl.cxx
blob8cccdc54c21de5eb909aa1672a64e743dfc7bad5
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: sbxdbl.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"
36 #include "runtime.hxx"
38 double ImpGetDouble( const SbxValues* p )
40 double nRes;
41 switch( +p->eType )
43 case SbxNULL:
44 SbxBase::SetError( SbxERR_CONVERSION );
45 case SbxEMPTY:
46 nRes = 0; break;
47 case SbxCHAR:
48 nRes = p->nChar; break;
49 case SbxBYTE:
50 nRes = p->nByte; break;
51 case SbxINTEGER:
52 case SbxBOOL:
53 nRes = p->nInteger; break;
54 case SbxERROR:
55 case SbxUSHORT:
56 nRes = p->nUShort; break;
57 case SbxLONG:
58 nRes = p->nLong; break;
59 case SbxULONG:
60 nRes = p->nULong; break;
61 case SbxSINGLE:
62 nRes = p->nSingle; break;
63 case SbxDATE:
64 case SbxDOUBLE:
65 nRes = p->nDouble; break;
66 case SbxCURRENCY:
67 nRes = ImpCurrencyToDouble( p->nLong64 ); break;
68 case SbxSALINT64:
69 nRes = static_cast< double >(p->nInt64); break;
70 case SbxSALUINT64:
71 nRes = ImpSalUInt64ToDouble( p->uInt64 ); break;
72 case SbxDECIMAL:
73 case SbxBYREF | SbxDECIMAL:
74 if( p->pDecimal )
75 p->pDecimal->getDouble( nRes );
76 else
77 nRes = 0.0;
78 break;
79 case SbxBYREF | SbxSTRING:
80 case SbxSTRING:
81 case SbxLPSTR:
82 if( !p->pString )
84 nRes = 0;
85 if ( SbiRuntime::isVBAEnabled() )// VBA only behaviour
86 SbxBase::SetError( SbxERR_CONVERSION );
88 else
90 double d;
91 SbxDataType t;
92 if( ImpScan( *p->pString, d, t, NULL ) != SbxERR_OK )
94 nRes = 0;
95 if ( SbiRuntime::isVBAEnabled() )// VBA only behaviour
96 SbxBase::SetError( SbxERR_CONVERSION );
98 else
99 nRes = d;
101 break;
102 case SbxOBJECT:
104 SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
105 if( pVal )
106 nRes = pVal->GetDouble();
107 else
109 SbxBase::SetError( SbxERR_NO_OBJECT ); nRes = 0;
111 break;
114 case SbxBYREF | SbxCHAR:
115 nRes = *p->pChar; break;
116 case SbxBYREF | SbxBYTE:
117 nRes = *p->pByte; break;
118 case SbxBYREF | SbxINTEGER:
119 case SbxBYREF | SbxBOOL:
120 nRes = *p->pInteger; break;
121 case SbxBYREF | SbxLONG:
122 nRes = *p->pLong; break;
123 case SbxBYREF | SbxULONG:
124 nRes = *p->pULong; break;
125 case SbxBYREF | SbxERROR:
126 case SbxBYREF | SbxUSHORT:
127 nRes = *p->pUShort; break;
128 case SbxBYREF | SbxSINGLE:
129 nRes = *p->pSingle; break;
130 case SbxBYREF | SbxDATE:
131 case SbxBYREF | SbxDOUBLE:
132 nRes = *p->pDouble; break;
133 case SbxBYREF | SbxCURRENCY:
134 nRes = ImpCurrencyToDouble( *p->pLong64 ); break;
135 case SbxBYREF | SbxSALINT64:
136 nRes = static_cast< double >(*p->pnInt64); break;
137 case SbxBYREF | SbxSALUINT64:
138 nRes = ImpSalUInt64ToDouble( *p->puInt64 ); break;
140 default:
141 SbxBase::SetError( SbxERR_CONVERSION ); nRes = 0;
143 return nRes;
146 void ImpPutDouble( SbxValues* p, double n, BOOL bCoreString )
148 SbxValues aTmp;
149 start:
150 switch( +p->eType )
152 // Hier sind Tests notwendig
153 case SbxCHAR:
154 aTmp.pChar = &p->nChar; goto direct;
155 case SbxBYTE:
156 aTmp.pByte = &p->nByte; goto direct;
157 case SbxINTEGER:
158 case SbxBOOL:
159 aTmp.pInteger = &p->nInteger; goto direct;
160 case SbxLONG:
161 case SbxCURRENCY:
162 aTmp.pLong = &p->nLong; goto direct;
163 case SbxULONG:
164 aTmp.pULong = &p->nULong; goto direct;
165 case SbxERROR:
166 case SbxUSHORT:
167 aTmp.pUShort = &p->nUShort; goto direct;
168 case SbxSINGLE:
169 aTmp.pSingle = &p->nSingle; goto direct;
170 case SbxDECIMAL:
171 case SbxBYREF | SbxDECIMAL:
173 SbxDecimal* pDec = ImpCreateDecimal( p );
174 if( !pDec->setDouble( n ) )
175 SbxBase::SetError( SbxERR_OVERFLOW );
176 break;
178 direct:
179 aTmp.eType = SbxDataType( p->eType | SbxBYREF );
180 p = &aTmp; goto start;
182 // ab hier nicht mehr
183 case SbxSALINT64:
184 p->nInt64 = ImpDoubleToSalInt64( n ); break;
185 case SbxSALUINT64:
186 p->uInt64 = ImpDoubleToSalUInt64( n ); break;
187 case SbxDATE:
188 case SbxDOUBLE:
189 p->nDouble = n; break;
191 case SbxBYREF | SbxSTRING:
192 case SbxSTRING:
193 case SbxLPSTR:
194 if( !p->pString )
195 p->pString = new XubString;
196 ImpCvtNum( (double) n, 14, *p->pString, bCoreString );
197 break;
198 case SbxOBJECT:
200 SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
201 if( pVal )
202 pVal->PutDouble( n );
203 else
204 SbxBase::SetError( SbxERR_NO_OBJECT );
205 break;
207 case SbxBYREF | SbxCHAR:
208 if( n > SbxMAXCHAR )
210 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXCHAR;
212 else if( n < SbxMINCHAR )
214 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINCHAR;
216 *p->pChar = (xub_Unicode) n; break;
217 case SbxBYREF | SbxBYTE:
218 if( n > SbxMAXBYTE )
220 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXBYTE;
222 else if( n < 0 )
224 SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
226 *p->pByte = (BYTE) n; break;
227 case SbxBYREF | SbxINTEGER:
228 case SbxBYREF | SbxBOOL:
229 if( n > SbxMAXINT )
231 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXINT;
233 else if( n < SbxMININT )
235 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMININT;
237 *p->pInteger = (INT16) n; break;
238 case SbxBYREF | SbxERROR:
239 case SbxBYREF | SbxUSHORT:
240 if( n > SbxMAXUINT )
242 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXUINT;
244 else if( n < 0 )
246 SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
248 *p->pUShort = (UINT16) n; break;
249 case SbxBYREF | SbxLONG:
250 if( n > SbxMAXLNG )
252 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXLNG;
254 else if( n < SbxMINLNG )
256 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINLNG;
258 *p->pLong = (INT32) n; break;
259 case SbxBYREF | SbxULONG:
260 if( n > SbxMAXULNG )
262 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXULNG;
264 else if( n < 0 )
266 SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
268 *p->pULong = (UINT32) n; break;
269 case SbxBYREF | SbxSINGLE:
270 if( n > SbxMAXSNG )
272 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXSNG;
274 else if( n < SbxMINSNG )
276 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINSNG;
278 else if( n > 0 && n < SbxMAXSNG2 )
280 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXSNG2;
282 else if( n < 0 && n > SbxMINSNG2 )
284 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINSNG2;
286 *p->pSingle = (float) n; break;
287 case SbxBYREF | SbxSALINT64:
288 *p->pnInt64 = ImpDoubleToSalInt64( n ); break;
289 case SbxBYREF | SbxSALUINT64:
290 *p->puInt64 = ImpDoubleToSalUInt64( n ); break;
291 case SbxBYREF | SbxDATE:
292 case SbxBYREF | SbxDOUBLE:
293 *p->pDouble = (double) n; break;
294 case SbxBYREF | SbxCURRENCY:
295 if( n > SbxMAXCURR )
297 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXCURR;
299 else if( n < SbxMINCURR )
301 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINCURR;
303 *p->pLong64 = ImpDoubleToCurrency( n ); break;
305 default:
306 SbxBase::SetError( SbxERR_CONVERSION );