Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / basic / source / sbx / sbxdbl.cxx
blob4add26a22a8a306e85a7dcc53d427cbdbe86c3bf
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"
23 #include "runtime.hxx"
25 double ImpGetDouble( const SbxValues* p )
27 double nRes;
28 switch( +p->eType )
30 case SbxNULL:
31 SbxBase::SetError( SbxERR_CONVERSION );
32 case SbxEMPTY:
33 nRes = 0; break;
34 case SbxCHAR:
35 nRes = p->nChar; break;
36 case SbxBYTE:
37 nRes = p->nByte; break;
38 case SbxINTEGER:
39 case SbxBOOL:
40 nRes = p->nInteger; break;
41 case SbxERROR:
42 case SbxUSHORT:
43 nRes = p->nUShort; break;
44 case SbxLONG:
45 nRes = p->nLong; break;
46 case SbxULONG:
47 nRes = p->nULong; break;
48 case SbxSINGLE:
49 nRes = p->nSingle; break;
50 case SbxDATE:
51 case SbxDOUBLE:
52 nRes = p->nDouble; break;
53 case SbxCURRENCY:
54 nRes = ImpCurrencyToDouble( p->nInt64 ); break;
55 case SbxSALINT64:
56 nRes = static_cast< double >(p->nInt64); break;
57 case SbxSALUINT64:
58 nRes = ImpSalUInt64ToDouble( p->uInt64 ); break;
59 case SbxDECIMAL:
60 case SbxBYREF | SbxDECIMAL:
61 if( p->pDecimal )
62 p->pDecimal->getDouble( nRes );
63 else
64 nRes = 0.0;
65 break;
66 case SbxBYREF | SbxSTRING:
67 case SbxSTRING:
68 case SbxLPSTR:
69 if( !p->pOUString )
71 nRes = 0;
72 #ifndef DISABLE_SCRIPTING
73 if ( SbiRuntime::isVBAEnabled() )// VBA only behaviour
74 SbxBase::SetError( SbxERR_CONVERSION );
75 #endif
77 else
79 double d;
80 SbxDataType t;
81 if( ImpScan( *p->pOUString, d, t, NULL ) != SbxERR_OK )
83 nRes = 0;
84 #ifndef DISABLE_SCRIPTING
85 if ( SbiRuntime::isVBAEnabled() )// VBA only behaviour
86 SbxBase::SetError( SbxERR_CONVERSION );
87 #endif
89 else
90 nRes = d;
92 break;
93 case SbxOBJECT:
95 SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
96 if( pVal )
97 nRes = pVal->GetDouble();
98 else
100 SbxBase::SetError( SbxERR_NO_OBJECT ); nRes = 0;
102 break;
105 case SbxBYREF | SbxCHAR:
106 nRes = *p->pChar; break;
107 case SbxBYREF | SbxBYTE:
108 nRes = *p->pByte; break;
109 case SbxBYREF | SbxINTEGER:
110 case SbxBYREF | SbxBOOL:
111 nRes = *p->pInteger; break;
112 case SbxBYREF | SbxLONG:
113 nRes = *p->pLong; break;
114 case SbxBYREF | SbxULONG:
115 nRes = *p->pULong; break;
116 case SbxBYREF | SbxERROR:
117 case SbxBYREF | SbxUSHORT:
118 nRes = *p->pUShort; break;
119 case SbxBYREF | SbxSINGLE:
120 nRes = *p->pSingle; break;
121 case SbxBYREF | SbxDATE:
122 case SbxBYREF | SbxDOUBLE:
123 nRes = *p->pDouble; break;
124 case SbxBYREF | SbxCURRENCY:
125 nRes = ImpCurrencyToDouble( *p->pnInt64 ); break;
126 case SbxBYREF | SbxSALINT64:
127 nRes = static_cast< double >(*p->pnInt64); break;
128 case SbxBYREF | SbxSALUINT64:
129 nRes = ImpSalUInt64ToDouble( *p->puInt64 ); break;
131 default:
132 SbxBase::SetError( SbxERR_CONVERSION ); nRes = 0;
134 return nRes;
137 void ImpPutDouble( SbxValues* p, double n, bool bCoreString )
139 SbxValues aTmp;
140 start:
141 switch( +p->eType )
143 // Here are tests necessary
144 case SbxCHAR:
145 aTmp.pChar = &p->nChar; goto direct;
146 case SbxBYTE:
147 aTmp.pByte = &p->nByte; goto direct;
148 case SbxINTEGER:
149 case SbxBOOL:
150 aTmp.pInteger = &p->nInteger; goto direct;
151 case SbxLONG:
152 aTmp.pLong = &p->nLong; goto direct;
153 case SbxULONG:
154 aTmp.pULong = &p->nULong; goto direct;
155 case SbxERROR:
156 case SbxUSHORT:
157 aTmp.pUShort = &p->nUShort; goto direct;
158 case SbxSINGLE:
159 aTmp.pSingle = &p->nSingle; goto direct;
160 case SbxDECIMAL:
161 case SbxBYREF | SbxDECIMAL:
163 SbxDecimal* pDec = ImpCreateDecimal( p );
164 if( !pDec->setDouble( n ) )
165 SbxBase::SetError( SbxERR_OVERFLOW );
166 break;
168 direct:
169 aTmp.eType = SbxDataType( p->eType | SbxBYREF );
170 p = &aTmp; goto start;
172 case SbxCURRENCY:
173 if( n > SbxMAXCURR )
175 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXCURR;
177 else if( n < SbxMINCURR )
179 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINCURR;
181 p->nInt64 = ImpDoubleToCurrency( n );
182 break;
184 // from here on no longer
185 case SbxSALINT64:
186 p->nInt64 = ImpDoubleToSalInt64( n ); break;
187 case SbxSALUINT64:
188 p->uInt64 = ImpDoubleToSalUInt64( n ); break;
189 case SbxDATE:
190 case SbxDOUBLE:
191 p->nDouble = n; break;
193 case SbxBYREF | SbxSTRING:
194 case SbxSTRING:
195 case SbxLPSTR:
196 if( !p->pOUString )
197 p->pOUString = new ::rtl::OUString;
198 ImpCvtNum( (double) n, 14, *p->pOUString, bCoreString );
199 break;
200 case SbxOBJECT:
202 SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
203 if( pVal )
204 pVal->PutDouble( n );
205 else
206 SbxBase::SetError( SbxERR_NO_OBJECT );
207 break;
209 case SbxBYREF | SbxCHAR:
210 if( n > SbxMAXCHAR )
212 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXCHAR;
214 else if( n < SbxMINCHAR )
216 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINCHAR;
218 *p->pChar = (sal_Unicode) n; break;
219 case SbxBYREF | SbxBYTE:
220 if( n > SbxMAXBYTE )
222 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXBYTE;
224 else if( n < 0 )
226 SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
228 *p->pByte = (sal_uInt8) n; break;
229 case SbxBYREF | SbxINTEGER:
230 case SbxBYREF | SbxBOOL:
231 if( n > SbxMAXINT )
233 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXINT;
235 else if( n < SbxMININT )
237 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMININT;
239 *p->pInteger = (sal_Int16) n; break;
240 case SbxBYREF | SbxERROR:
241 case SbxBYREF | SbxUSHORT:
242 if( n > SbxMAXUINT )
244 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXUINT;
246 else if( n < 0 )
248 SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
250 *p->pUShort = (sal_uInt16) n; break;
251 case SbxBYREF | SbxLONG:
252 if( n > SbxMAXLNG )
254 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXLNG;
256 else if( n < SbxMINLNG )
258 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINLNG;
260 *p->pLong = (sal_Int32) n; break;
261 case SbxBYREF | SbxULONG:
262 if( n > SbxMAXULNG )
264 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXULNG;
266 else if( n < 0 )
268 SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
270 *p->pULong = (sal_uInt32) n; break;
271 case SbxBYREF | SbxSINGLE:
272 if( n > SbxMAXSNG )
274 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXSNG;
276 else if( n < SbxMINSNG )
278 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINSNG;
280 else if( n > 0 && n < SbxMAXSNG2 )
282 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXSNG2;
284 else if( n < 0 && n > SbxMINSNG2 )
286 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINSNG2;
288 *p->pSingle = (float) n; break;
289 case SbxBYREF | SbxSALINT64:
290 *p->pnInt64 = ImpDoubleToSalInt64( n ); break;
291 case SbxBYREF | SbxSALUINT64:
292 *p->puInt64 = ImpDoubleToSalUInt64( n ); break;
293 case SbxBYREF | SbxDATE:
294 case SbxBYREF | SbxDOUBLE:
295 *p->pDouble = (double) n; break;
296 case SbxBYREF | SbxCURRENCY:
297 if( n > SbxMAXCURR )
299 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXCURR;
301 else if( n < SbxMINCURR )
303 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINCURR;
305 *p->pnInt64 = ImpDoubleToCurrency( n ); break;
307 default:
308 SbxBase::SetError( SbxERR_CONVERSION );
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */