xref: /aoo42x/main/basic/source/sbx/sbxdbl.cxx (revision e1f63238)
1*e1f63238SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*e1f63238SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*e1f63238SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*e1f63238SAndrew Rist  * distributed with this work for additional information
6*e1f63238SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*e1f63238SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*e1f63238SAndrew Rist  * "License"); you may not use this file except in compliance
9*e1f63238SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*e1f63238SAndrew Rist  *
11*e1f63238SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*e1f63238SAndrew Rist  *
13*e1f63238SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*e1f63238SAndrew Rist  * software distributed under the License is distributed on an
15*e1f63238SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e1f63238SAndrew Rist  * KIND, either express or implied.  See the License for the
17*e1f63238SAndrew Rist  * specific language governing permissions and limitations
18*e1f63238SAndrew Rist  * under the License.
19*e1f63238SAndrew Rist  *
20*e1f63238SAndrew Rist  *************************************************************/
21*e1f63238SAndrew Rist 
22*e1f63238SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_basic.hxx"
26cdf0e10cSrcweir #include <tools/errcode.hxx>
27cdf0e10cSrcweir #include <basic/sbx.hxx>
28cdf0e10cSrcweir #include "sbxconv.hxx"
29cdf0e10cSrcweir #include "runtime.hxx"
30cdf0e10cSrcweir 
ImpGetDouble(const SbxValues * p)31cdf0e10cSrcweir double ImpGetDouble( const SbxValues* p )
32cdf0e10cSrcweir {
33cdf0e10cSrcweir 	double nRes;
34cdf0e10cSrcweir 	switch( +p->eType )
35cdf0e10cSrcweir 	{
36cdf0e10cSrcweir 		case SbxNULL:
37cdf0e10cSrcweir 			SbxBase::SetError( SbxERR_CONVERSION );
38cdf0e10cSrcweir 		case SbxEMPTY:
39cdf0e10cSrcweir 			nRes = 0; break;
40cdf0e10cSrcweir 		case SbxCHAR:
41cdf0e10cSrcweir 			nRes = p->nChar; break;
42cdf0e10cSrcweir 		case SbxBYTE:
43cdf0e10cSrcweir 			nRes = p->nByte; break;
44cdf0e10cSrcweir 		case SbxINTEGER:
45cdf0e10cSrcweir 		case SbxBOOL:
46cdf0e10cSrcweir 			nRes = p->nInteger; break;
47cdf0e10cSrcweir 		case SbxERROR:
48cdf0e10cSrcweir 		case SbxUSHORT:
49cdf0e10cSrcweir 			nRes = p->nUShort; break;
50cdf0e10cSrcweir 		case SbxLONG:
51cdf0e10cSrcweir 			nRes = p->nLong; break;
52cdf0e10cSrcweir 		case SbxULONG:
53cdf0e10cSrcweir 			nRes = p->nULong; break;
54cdf0e10cSrcweir 		case SbxSINGLE:
55cdf0e10cSrcweir 			nRes = p->nSingle; break;
56cdf0e10cSrcweir 		case SbxDATE:
57cdf0e10cSrcweir 		case SbxDOUBLE:
58cdf0e10cSrcweir 			nRes = p->nDouble; break;
59cdf0e10cSrcweir 		case SbxCURRENCY:
60cdf0e10cSrcweir 			nRes = ImpCurrencyToDouble( p->nLong64 ); break;
61cdf0e10cSrcweir 		case SbxSALINT64:
62cdf0e10cSrcweir             nRes = static_cast< double >(p->nInt64); break;
63cdf0e10cSrcweir 		case SbxSALUINT64:
64cdf0e10cSrcweir             nRes = ImpSalUInt64ToDouble( p->uInt64 ); break;
65cdf0e10cSrcweir 		case SbxDECIMAL:
66cdf0e10cSrcweir 		case SbxBYREF | SbxDECIMAL:
67cdf0e10cSrcweir 			if( p->pDecimal )
68cdf0e10cSrcweir 				p->pDecimal->getDouble( nRes );
69cdf0e10cSrcweir 			else
70cdf0e10cSrcweir 				nRes = 0.0;
71cdf0e10cSrcweir 			break;
72cdf0e10cSrcweir 		case SbxBYREF | SbxSTRING:
73cdf0e10cSrcweir 		case SbxSTRING:
74cdf0e10cSrcweir 		case SbxLPSTR:
75cdf0e10cSrcweir 			if( !p->pOUString )
76cdf0e10cSrcweir 			{
77cdf0e10cSrcweir 				nRes = 0;
78cdf0e10cSrcweir 				if ( SbiRuntime::isVBAEnabled() )// VBA only behaviour
79cdf0e10cSrcweir 					SbxBase::SetError( SbxERR_CONVERSION );
80cdf0e10cSrcweir 			}
81cdf0e10cSrcweir 			else
82cdf0e10cSrcweir 			{
83cdf0e10cSrcweir 				double d;
84cdf0e10cSrcweir 				SbxDataType t;
85cdf0e10cSrcweir 				if( ImpScan( *p->pOUString, d, t, NULL ) != SbxERR_OK )
86cdf0e10cSrcweir 				{
87cdf0e10cSrcweir 					nRes = 0;
88cdf0e10cSrcweir 					if ( SbiRuntime::isVBAEnabled() )// VBA only behaviour
89cdf0e10cSrcweir 						SbxBase::SetError( SbxERR_CONVERSION );
90cdf0e10cSrcweir 				}
91cdf0e10cSrcweir 				else
92cdf0e10cSrcweir 					nRes = d;
93cdf0e10cSrcweir 			}
94cdf0e10cSrcweir 			break;
95cdf0e10cSrcweir 		case SbxOBJECT:
96cdf0e10cSrcweir 		{
97cdf0e10cSrcweir 			SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
98cdf0e10cSrcweir 			if( pVal )
99cdf0e10cSrcweir 				nRes = pVal->GetDouble();
100cdf0e10cSrcweir 			else
101cdf0e10cSrcweir 			{
102cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_NO_OBJECT ); nRes = 0;
103cdf0e10cSrcweir 			}
104cdf0e10cSrcweir 			break;
105cdf0e10cSrcweir 		}
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 		case SbxBYREF | SbxCHAR:
108cdf0e10cSrcweir 			nRes = *p->pChar; break;
109cdf0e10cSrcweir 		case SbxBYREF | SbxBYTE:
110cdf0e10cSrcweir 			nRes = *p->pByte; break;
111cdf0e10cSrcweir 		case SbxBYREF | SbxINTEGER:
112cdf0e10cSrcweir 		case SbxBYREF | SbxBOOL:
113cdf0e10cSrcweir 			nRes = *p->pInteger; break;
114cdf0e10cSrcweir 		case SbxBYREF | SbxLONG:
115cdf0e10cSrcweir 			nRes = *p->pLong; break;
116cdf0e10cSrcweir 		case SbxBYREF | SbxULONG:
117cdf0e10cSrcweir 			nRes = *p->pULong; break;
118cdf0e10cSrcweir 		case SbxBYREF | SbxERROR:
119cdf0e10cSrcweir 		case SbxBYREF | SbxUSHORT:
120cdf0e10cSrcweir 			nRes = *p->pUShort; break;
121cdf0e10cSrcweir 		case SbxBYREF | SbxSINGLE:
122cdf0e10cSrcweir 			nRes = *p->pSingle; break;
123cdf0e10cSrcweir 		case SbxBYREF | SbxDATE:
124cdf0e10cSrcweir 		case SbxBYREF | SbxDOUBLE:
125cdf0e10cSrcweir 			nRes = *p->pDouble; break;
126cdf0e10cSrcweir 		case SbxBYREF | SbxCURRENCY:
127cdf0e10cSrcweir 			nRes = ImpCurrencyToDouble( *p->pLong64 ); break;
128cdf0e10cSrcweir 		case SbxBYREF | SbxSALINT64:
129cdf0e10cSrcweir             nRes = static_cast< double >(*p->pnInt64); break;
130cdf0e10cSrcweir 		case SbxBYREF | SbxSALUINT64:
131cdf0e10cSrcweir             nRes = ImpSalUInt64ToDouble( *p->puInt64 ); break;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 		default:
134cdf0e10cSrcweir 			SbxBase::SetError( SbxERR_CONVERSION ); nRes = 0;
135cdf0e10cSrcweir 	}
136cdf0e10cSrcweir 	return nRes;
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
ImpPutDouble(SbxValues * p,double n,sal_Bool bCoreString)139cdf0e10cSrcweir void ImpPutDouble( SbxValues* p, double n, sal_Bool bCoreString )
140cdf0e10cSrcweir {
141cdf0e10cSrcweir 	SbxValues aTmp;
142cdf0e10cSrcweir start:
143cdf0e10cSrcweir 	switch( +p->eType )
144cdf0e10cSrcweir 	{
145cdf0e10cSrcweir 		// Hier sind Tests notwendig
146cdf0e10cSrcweir 		case SbxCHAR:
147cdf0e10cSrcweir 			aTmp.pChar = &p->nChar; goto direct;
148cdf0e10cSrcweir 		case SbxBYTE:
149cdf0e10cSrcweir 			aTmp.pByte = &p->nByte; goto direct;
150cdf0e10cSrcweir 		case SbxINTEGER:
151cdf0e10cSrcweir 		case SbxBOOL:
152cdf0e10cSrcweir 			aTmp.pInteger = &p->nInteger; goto direct;
153cdf0e10cSrcweir 		case SbxLONG:
154cdf0e10cSrcweir 		case SbxCURRENCY:
155cdf0e10cSrcweir 			aTmp.pLong = &p->nLong; goto direct;
156cdf0e10cSrcweir 		case SbxULONG:
157cdf0e10cSrcweir 			aTmp.pULong = &p->nULong; goto direct;
158cdf0e10cSrcweir 		case SbxERROR:
159cdf0e10cSrcweir 		case SbxUSHORT:
160cdf0e10cSrcweir 			aTmp.pUShort = &p->nUShort; goto direct;
161cdf0e10cSrcweir 		case SbxSINGLE:
162cdf0e10cSrcweir 			aTmp.pSingle = &p->nSingle; goto direct;
163cdf0e10cSrcweir 		case SbxDECIMAL:
164cdf0e10cSrcweir 		case SbxBYREF | SbxDECIMAL:
165cdf0e10cSrcweir 			{
166cdf0e10cSrcweir 			SbxDecimal* pDec = ImpCreateDecimal( p );
167cdf0e10cSrcweir 			if( !pDec->setDouble( n ) )
168cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW );
169cdf0e10cSrcweir 			break;
170cdf0e10cSrcweir 			}
171cdf0e10cSrcweir 		direct:
172cdf0e10cSrcweir 			aTmp.eType = SbxDataType( p->eType | SbxBYREF );
173cdf0e10cSrcweir 			p = &aTmp; goto start;
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 			// ab hier nicht mehr
176cdf0e10cSrcweir 		case SbxSALINT64:
177cdf0e10cSrcweir             p->nInt64 = ImpDoubleToSalInt64( n ); break;
178cdf0e10cSrcweir 		case SbxSALUINT64:
179cdf0e10cSrcweir 			p->uInt64 = ImpDoubleToSalUInt64( n ); break;
180cdf0e10cSrcweir 		case SbxDATE:
181cdf0e10cSrcweir 		case SbxDOUBLE:
182cdf0e10cSrcweir 			p->nDouble = n; break;
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 		case SbxBYREF | SbxSTRING:
185cdf0e10cSrcweir 		case SbxSTRING:
186cdf0e10cSrcweir 		case SbxLPSTR:
187cdf0e10cSrcweir 			if( !p->pOUString )
188cdf0e10cSrcweir 				p->pOUString = new ::rtl::OUString;
189cdf0e10cSrcweir 			ImpCvtNum( (double) n, 14, *p->pOUString, bCoreString );
190cdf0e10cSrcweir 			break;
191cdf0e10cSrcweir 		case SbxOBJECT:
192cdf0e10cSrcweir 		{
193cdf0e10cSrcweir 			SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
194cdf0e10cSrcweir 			if( pVal )
195cdf0e10cSrcweir 				pVal->PutDouble( n );
196cdf0e10cSrcweir 			else
197cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_NO_OBJECT );
198cdf0e10cSrcweir 			break;
199cdf0e10cSrcweir 		}
200cdf0e10cSrcweir 		case SbxBYREF | SbxCHAR:
201cdf0e10cSrcweir 			if( n > SbxMAXCHAR )
202cdf0e10cSrcweir 			{
203cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXCHAR;
204cdf0e10cSrcweir 			}
205cdf0e10cSrcweir 			else if( n < SbxMINCHAR )
206cdf0e10cSrcweir 			{
207cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINCHAR;
208cdf0e10cSrcweir 			}
209cdf0e10cSrcweir 			*p->pChar = (xub_Unicode) n; break;
210cdf0e10cSrcweir 		case SbxBYREF | SbxBYTE:
211cdf0e10cSrcweir 			if( n > SbxMAXBYTE )
212cdf0e10cSrcweir 			{
213cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXBYTE;
214cdf0e10cSrcweir 			}
215cdf0e10cSrcweir 			else if( n < 0 )
216cdf0e10cSrcweir 			{
217cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
218cdf0e10cSrcweir 			}
219cdf0e10cSrcweir 			*p->pByte = (sal_uInt8) n; break;
220cdf0e10cSrcweir 		case SbxBYREF | SbxINTEGER:
221cdf0e10cSrcweir 		case SbxBYREF | SbxBOOL:
222cdf0e10cSrcweir 			if( n > SbxMAXINT )
223cdf0e10cSrcweir 			{
224cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXINT;
225cdf0e10cSrcweir 			}
226cdf0e10cSrcweir 			else if( n < SbxMININT )
227cdf0e10cSrcweir 			{
228cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMININT;
229cdf0e10cSrcweir 			}
230cdf0e10cSrcweir 			*p->pInteger = (sal_Int16) n; break;
231cdf0e10cSrcweir 		case SbxBYREF | SbxERROR:
232cdf0e10cSrcweir 		case SbxBYREF | SbxUSHORT:
233cdf0e10cSrcweir 			if( n > SbxMAXUINT )
234cdf0e10cSrcweir 			{
235cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXUINT;
236cdf0e10cSrcweir 			}
237cdf0e10cSrcweir 			else if( n < 0 )
238cdf0e10cSrcweir 			{
239cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
240cdf0e10cSrcweir 			}
241cdf0e10cSrcweir 			*p->pUShort = (sal_uInt16) n; break;
242cdf0e10cSrcweir 		case SbxBYREF | SbxLONG:
243cdf0e10cSrcweir 			if( n > SbxMAXLNG )
244cdf0e10cSrcweir 			{
245cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXLNG;
246cdf0e10cSrcweir 			}
247cdf0e10cSrcweir 			else if( n < SbxMINLNG )
248cdf0e10cSrcweir 			{
249cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINLNG;
250cdf0e10cSrcweir 			}
251cdf0e10cSrcweir 			*p->pLong = (sal_Int32) n; break;
252cdf0e10cSrcweir 		case SbxBYREF | SbxULONG:
253cdf0e10cSrcweir 			if( n > SbxMAXULNG )
254cdf0e10cSrcweir 			{
255cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXULNG;
256cdf0e10cSrcweir 			}
257cdf0e10cSrcweir 			else if( n < 0 )
258cdf0e10cSrcweir 			{
259cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
260cdf0e10cSrcweir 			}
261cdf0e10cSrcweir 			*p->pULong = (sal_uInt32) n; break;
262cdf0e10cSrcweir 		case SbxBYREF | SbxSINGLE:
263cdf0e10cSrcweir 			if( n > SbxMAXSNG )
264cdf0e10cSrcweir 			{
265cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXSNG;
266cdf0e10cSrcweir 			}
267cdf0e10cSrcweir 			else if( n < SbxMINSNG )
268cdf0e10cSrcweir 			{
269cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINSNG;
270cdf0e10cSrcweir 			}
271cdf0e10cSrcweir 			else if( n > 0 && n < SbxMAXSNG2 )
272cdf0e10cSrcweir 			{
273cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXSNG2;
274cdf0e10cSrcweir 			}
275cdf0e10cSrcweir 			else if( n < 0 && n > SbxMINSNG2 )
276cdf0e10cSrcweir 			{
277cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINSNG2;
278cdf0e10cSrcweir 			}
279cdf0e10cSrcweir 			*p->pSingle = (float) n; break;
280cdf0e10cSrcweir 		case SbxBYREF | SbxSALINT64:
281cdf0e10cSrcweir             *p->pnInt64 = ImpDoubleToSalInt64( n ); break;
282cdf0e10cSrcweir 		case SbxBYREF | SbxSALUINT64:
283cdf0e10cSrcweir 			*p->puInt64 = ImpDoubleToSalUInt64( n ); break;
284cdf0e10cSrcweir 		case SbxBYREF | SbxDATE:
285cdf0e10cSrcweir 		case SbxBYREF | SbxDOUBLE:
286cdf0e10cSrcweir 			*p->pDouble = (double) n; break;
287cdf0e10cSrcweir 		case SbxBYREF | SbxCURRENCY:
288cdf0e10cSrcweir 			if( n > SbxMAXCURR )
289cdf0e10cSrcweir 			{
290cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXCURR;
291cdf0e10cSrcweir 			}
292cdf0e10cSrcweir 			else if( n < SbxMINCURR )
293cdf0e10cSrcweir 			{
294cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINCURR;
295cdf0e10cSrcweir 			}
296cdf0e10cSrcweir 			*p->pLong64 = ImpDoubleToCurrency( n ); break;
297cdf0e10cSrcweir 
298cdf0e10cSrcweir 		default:
299cdf0e10cSrcweir 			SbxBase::SetError( SbxERR_CONVERSION );
300cdf0e10cSrcweir 	}
301cdf0e10cSrcweir }
302cdf0e10cSrcweir 
303