xref: /aoo42x/main/basic/source/sbx/sbxsng.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 
ImpGetSingle(const SbxValues * p)30cdf0e10cSrcweir float ImpGetSingle( const SbxValues* p )
31cdf0e10cSrcweir {
32cdf0e10cSrcweir 	SbxValues aTmp;
33cdf0e10cSrcweir 	float nRes;
34cdf0e10cSrcweir start:
35cdf0e10cSrcweir 	switch( +p->eType )
36cdf0e10cSrcweir 	{
37cdf0e10cSrcweir 		case SbxNULL:
38cdf0e10cSrcweir 			SbxBase::SetError( SbxERR_CONVERSION );
39cdf0e10cSrcweir 		case SbxEMPTY:
40cdf0e10cSrcweir 			nRes = 0; break;
41cdf0e10cSrcweir 		case SbxCHAR:
42cdf0e10cSrcweir 			nRes = p->nChar; break;
43cdf0e10cSrcweir 		case SbxBYTE:
44cdf0e10cSrcweir 			nRes = p->nByte; break;
45cdf0e10cSrcweir 		case SbxINTEGER:
46cdf0e10cSrcweir 		case SbxBOOL:
47cdf0e10cSrcweir 			nRes = p->nInteger; break;
48cdf0e10cSrcweir 		case SbxERROR:
49cdf0e10cSrcweir 		case SbxUSHORT:
50cdf0e10cSrcweir 			nRes = p->nUShort; break;
51cdf0e10cSrcweir 		case SbxLONG:
52cdf0e10cSrcweir 			nRes = (float) p->nLong; break;
53cdf0e10cSrcweir 		case SbxULONG:
54cdf0e10cSrcweir 			nRes = (float) p->nULong; break;
55cdf0e10cSrcweir 		case SbxSINGLE:
56cdf0e10cSrcweir 			nRes = p->nSingle; break;
57cdf0e10cSrcweir 		case SbxSALINT64:
58cdf0e10cSrcweir             nRes = (float) p->nInt64; break;
59cdf0e10cSrcweir 		case SbxSALUINT64:
60cdf0e10cSrcweir             nRes = (float) ImpSalUInt64ToDouble( p->uInt64 ); break;
61cdf0e10cSrcweir 		case SbxDECIMAL:
62cdf0e10cSrcweir 		case SbxBYREF | SbxDECIMAL:
63cdf0e10cSrcweir 			if( p->pDecimal )
64cdf0e10cSrcweir 				p->pDecimal->getSingle( nRes );
65cdf0e10cSrcweir 			else
66cdf0e10cSrcweir 				nRes = 0.0;
67cdf0e10cSrcweir 			break;
68cdf0e10cSrcweir 		case SbxDATE:
69cdf0e10cSrcweir 		case SbxDOUBLE:
70cdf0e10cSrcweir 		case SbxLONG64:
71cdf0e10cSrcweir 		case SbxULONG64:
72cdf0e10cSrcweir 		case SbxCURRENCY:
73cdf0e10cSrcweir 			{
74cdf0e10cSrcweir 			double dVal;
75cdf0e10cSrcweir 			if( p->eType ==	SbxCURRENCY )
76cdf0e10cSrcweir 				dVal = ImpCurrencyToDouble( p->nLong64 );
77cdf0e10cSrcweir 			else if( p->eType == SbxLONG64 )
78cdf0e10cSrcweir 				dVal = ImpINT64ToDouble( p->nLong64 );
79cdf0e10cSrcweir 			else if( p->eType == SbxULONG64 )
80cdf0e10cSrcweir 				dVal = ImpUINT64ToDouble( p->nULong64 );
81cdf0e10cSrcweir 			else
82cdf0e10cSrcweir 				dVal = p->nDouble;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 			if( dVal > SbxMAXSNG )
85cdf0e10cSrcweir 			{
86cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW );
87cdf0e10cSrcweir                 nRes = static_cast< float >(SbxMAXSNG);
88cdf0e10cSrcweir 			}
89cdf0e10cSrcweir 			else if( dVal < SbxMINSNG )
90cdf0e10cSrcweir 			{
91cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW );
92cdf0e10cSrcweir                 nRes = static_cast< float >(SbxMINSNG);
93cdf0e10cSrcweir 			}
94cdf0e10cSrcweir 			else if( dVal > 0 && dVal < SbxMAXSNG2 )
95cdf0e10cSrcweir 			{
96cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW );
97cdf0e10cSrcweir                 nRes = static_cast< float >(SbxMAXSNG2);
98cdf0e10cSrcweir 			}
99cdf0e10cSrcweir 			else if( dVal < 0 && dVal > SbxMINSNG2 )
100cdf0e10cSrcweir 			{
101cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW );
102cdf0e10cSrcweir                 nRes = static_cast< float >(SbxMINSNG2);
103cdf0e10cSrcweir 			}
104cdf0e10cSrcweir 			else
105cdf0e10cSrcweir 				nRes = (float) dVal;
106cdf0e10cSrcweir 			break;
107cdf0e10cSrcweir 			}
108cdf0e10cSrcweir 		case SbxBYREF | SbxSTRING:
109cdf0e10cSrcweir 		case SbxSTRING:
110cdf0e10cSrcweir 		case SbxLPSTR:
111cdf0e10cSrcweir 			if( !p->pOUString )
112cdf0e10cSrcweir 				nRes = 0;
113cdf0e10cSrcweir 			else
114cdf0e10cSrcweir 			{
115cdf0e10cSrcweir 				double d;
116cdf0e10cSrcweir 				SbxDataType t;
117cdf0e10cSrcweir 				if( ImpScan( *p->pOUString, d, t, NULL ) != SbxERR_OK )
118cdf0e10cSrcweir 					nRes = 0;
119cdf0e10cSrcweir 				else if( d > SbxMAXSNG )
120cdf0e10cSrcweir 				{
121cdf0e10cSrcweir 					SbxBase::SetError( SbxERR_OVERFLOW );
122cdf0e10cSrcweir                     nRes = static_cast< float >(SbxMAXSNG);
123cdf0e10cSrcweir 				}
124cdf0e10cSrcweir 				else if( d < SbxMINSNG )
125cdf0e10cSrcweir 				{
126cdf0e10cSrcweir 					SbxBase::SetError( SbxERR_OVERFLOW );
127cdf0e10cSrcweir                     nRes = static_cast< float >(SbxMINSNG);
128cdf0e10cSrcweir 				}
129cdf0e10cSrcweir 				else
130cdf0e10cSrcweir 					nRes = (float) d;
131cdf0e10cSrcweir 			}
132cdf0e10cSrcweir 			break;
133cdf0e10cSrcweir 		case SbxOBJECT:
134cdf0e10cSrcweir 		{
135cdf0e10cSrcweir 			SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
136cdf0e10cSrcweir 			if( pVal )
137cdf0e10cSrcweir 				nRes = pVal->GetSingle();
138cdf0e10cSrcweir 			else
139cdf0e10cSrcweir 			{
140cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_NO_OBJECT ); nRes = 0;
141cdf0e10cSrcweir 			}
142cdf0e10cSrcweir 			break;
143cdf0e10cSrcweir 		}
144cdf0e10cSrcweir 
145cdf0e10cSrcweir 		case SbxBYREF | SbxCHAR:
146cdf0e10cSrcweir 			nRes = *p->pChar; break;
147cdf0e10cSrcweir 		case SbxBYREF | SbxBYTE:
148cdf0e10cSrcweir 			nRes = *p->pByte; break;
149cdf0e10cSrcweir 		case SbxBYREF | SbxINTEGER:
150cdf0e10cSrcweir 		case SbxBYREF | SbxBOOL:
151cdf0e10cSrcweir 			nRes = *p->pInteger; break;
152cdf0e10cSrcweir 		case SbxBYREF | SbxLONG:
153cdf0e10cSrcweir 			nRes = (float) *p->pLong; break;
154cdf0e10cSrcweir 		case SbxBYREF | SbxULONG:
155cdf0e10cSrcweir 			nRes = (float) *p->pULong; break;
156cdf0e10cSrcweir 		case SbxBYREF | SbxERROR:
157cdf0e10cSrcweir 		case SbxBYREF | SbxUSHORT:
158cdf0e10cSrcweir 			nRes = *p->pUShort; break;
159cdf0e10cSrcweir 		case SbxBYREF | SbxSINGLE:
160cdf0e10cSrcweir 			nRes = *p->pSingle; break;
161cdf0e10cSrcweir 		// ab hier muss getestet werden
162cdf0e10cSrcweir 		case SbxBYREF | SbxDATE:
163cdf0e10cSrcweir 		case SbxBYREF | SbxDOUBLE:
164cdf0e10cSrcweir 			aTmp.nDouble = *p->pDouble; goto ref;
165cdf0e10cSrcweir 		case SbxBYREF | SbxULONG64:
166cdf0e10cSrcweir 			aTmp.nULong64 = *p->pULong64; goto ref;
167cdf0e10cSrcweir 		case SbxBYREF | SbxLONG64:
168cdf0e10cSrcweir 		case SbxBYREF | SbxSALINT64:
169cdf0e10cSrcweir             nRes = (float) *p->pnInt64; break;
170cdf0e10cSrcweir 		case SbxBYREF | SbxSALUINT64:
171cdf0e10cSrcweir             nRes = (float) ImpSalUInt64ToDouble( *p->puInt64 ); break;
172cdf0e10cSrcweir 		case SbxBYREF | SbxCURRENCY:
173cdf0e10cSrcweir 			aTmp.nLong64 = *p->pLong64; goto ref;
174cdf0e10cSrcweir 		ref:
175cdf0e10cSrcweir 			aTmp.eType = SbxDataType( p->eType & 0x0FFF );
176cdf0e10cSrcweir 			p = &aTmp; goto start;
177cdf0e10cSrcweir 
178cdf0e10cSrcweir 		default:
179cdf0e10cSrcweir 			SbxBase::SetError( SbxERR_CONVERSION ); nRes = 0;
180cdf0e10cSrcweir 	}
181cdf0e10cSrcweir 	return nRes;
182cdf0e10cSrcweir }
183cdf0e10cSrcweir 
ImpPutSingle(SbxValues * p,float n)184cdf0e10cSrcweir void ImpPutSingle( SbxValues* p, float n )
185cdf0e10cSrcweir {
186cdf0e10cSrcweir 	SbxValues aTmp;
187cdf0e10cSrcweir start:
188cdf0e10cSrcweir 	switch( +p->eType )
189cdf0e10cSrcweir 	{
190cdf0e10cSrcweir 		case SbxCHAR:
191cdf0e10cSrcweir 			aTmp.pChar = &p->nChar; goto direct;
192cdf0e10cSrcweir 		case SbxBYTE:
193cdf0e10cSrcweir 			aTmp.pByte = &p->nByte; goto direct;
194cdf0e10cSrcweir 		case SbxINTEGER:
195cdf0e10cSrcweir 		case SbxBOOL:
196cdf0e10cSrcweir 			aTmp.pInteger = &p->nInteger; goto direct;
197cdf0e10cSrcweir 		case SbxLONG:
198cdf0e10cSrcweir 			aTmp.pLong = &p->nLong; goto direct;
199cdf0e10cSrcweir 		case SbxULONG:
200cdf0e10cSrcweir 			aTmp.pULong = &p->nULong; goto direct;
201cdf0e10cSrcweir 		case SbxERROR:
202cdf0e10cSrcweir 		case SbxUSHORT:
203cdf0e10cSrcweir 			aTmp.pUShort = &p->nUShort; goto direct;
204cdf0e10cSrcweir 		case SbxULONG64:
205cdf0e10cSrcweir 			aTmp.pULong64 = &p->nULong64; goto direct;
206cdf0e10cSrcweir 		case SbxLONG64:
207cdf0e10cSrcweir 		case SbxCURRENCY:
208cdf0e10cSrcweir 			aTmp.pLong64 = &p->nLong64; goto direct;
209cdf0e10cSrcweir 		case SbxSALINT64:
210cdf0e10cSrcweir 			aTmp.pnInt64 = &p->nInt64; goto direct;
211cdf0e10cSrcweir 		case SbxSALUINT64:
212cdf0e10cSrcweir 			aTmp.puInt64 = &p->uInt64; goto direct;
213cdf0e10cSrcweir 		case SbxDECIMAL:
214cdf0e10cSrcweir 		case SbxBYREF | SbxDECIMAL:
215cdf0e10cSrcweir 			{
216cdf0e10cSrcweir 			SbxDecimal* pDec = ImpCreateDecimal( p );
217cdf0e10cSrcweir 			if( !pDec->setSingle( n ) )
218cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW );
219cdf0e10cSrcweir 			break;
220cdf0e10cSrcweir 			}
221cdf0e10cSrcweir 		direct:
222cdf0e10cSrcweir 			aTmp.eType = SbxDataType( p->eType | SbxBYREF );
223cdf0e10cSrcweir 			p = &aTmp; goto start;
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 		// keine Tests ab hier
226cdf0e10cSrcweir 		case SbxSINGLE:
227cdf0e10cSrcweir 			p->nSingle = n; break;
228cdf0e10cSrcweir 		case SbxDATE:
229cdf0e10cSrcweir 		case SbxDOUBLE:
230cdf0e10cSrcweir 			p->nDouble = n; break;
231cdf0e10cSrcweir 
232cdf0e10cSrcweir 		case SbxBYREF | SbxSTRING:
233cdf0e10cSrcweir 		case SbxSTRING:
234cdf0e10cSrcweir 		case SbxLPSTR:
235cdf0e10cSrcweir 		{
236cdf0e10cSrcweir 			if( !p->pOUString )
237cdf0e10cSrcweir 				p->pOUString = new ::rtl::OUString;
238cdf0e10cSrcweir 			ImpCvtNum( (double) n, 6, *p->pOUString );
239cdf0e10cSrcweir 			break;
240cdf0e10cSrcweir 		}
241cdf0e10cSrcweir 		case SbxOBJECT:
242cdf0e10cSrcweir 		{
243cdf0e10cSrcweir 			SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
244cdf0e10cSrcweir 			if( pVal )
245cdf0e10cSrcweir 				pVal->PutSingle( n );
246cdf0e10cSrcweir 			else
247cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_NO_OBJECT );
248cdf0e10cSrcweir 			break;
249cdf0e10cSrcweir 		}
250cdf0e10cSrcweir 		case SbxBYREF | SbxCHAR:
251cdf0e10cSrcweir 			if( n > SbxMAXCHAR )
252cdf0e10cSrcweir 			{
253cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXCHAR;
254cdf0e10cSrcweir 			}
255cdf0e10cSrcweir 			else if( n < SbxMINCHAR )
256cdf0e10cSrcweir 			{
257cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINCHAR;
258cdf0e10cSrcweir 			}
259cdf0e10cSrcweir 			*p->pChar = (xub_Unicode) n; break;
260cdf0e10cSrcweir 		case SbxBYREF | SbxBYTE:
261cdf0e10cSrcweir 			if( n > SbxMAXBYTE )
262cdf0e10cSrcweir 			{
263cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXBYTE;
264cdf0e10cSrcweir 			}
265cdf0e10cSrcweir 			else if( n < 0 )
266cdf0e10cSrcweir 			{
267cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
268cdf0e10cSrcweir 			}
269cdf0e10cSrcweir 			*p->pByte = (sal_uInt8) n; break;
270cdf0e10cSrcweir 		case SbxBYREF | SbxINTEGER:
271cdf0e10cSrcweir 		case SbxBYREF | SbxBOOL:
272cdf0e10cSrcweir 			if( n > SbxMAXINT )
273cdf0e10cSrcweir 			{
274cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXINT;
275cdf0e10cSrcweir 			}
276cdf0e10cSrcweir 			else if( n < SbxMININT )
277cdf0e10cSrcweir 			{
278cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMININT;
279cdf0e10cSrcweir 			}
280cdf0e10cSrcweir 			*p->pInteger = (sal_Int16) n; break;
281cdf0e10cSrcweir 		case SbxBYREF | SbxERROR:
282cdf0e10cSrcweir 		case SbxBYREF | SbxUSHORT:
283cdf0e10cSrcweir 			if( n > SbxMAXUINT )
284cdf0e10cSrcweir 			{
285cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXUINT;
286cdf0e10cSrcweir 			}
287cdf0e10cSrcweir 			else if( n < 0 )
288cdf0e10cSrcweir 			{
289cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
290cdf0e10cSrcweir 			}
291cdf0e10cSrcweir 			*p->pUShort = (sal_uInt16) n; break;
292cdf0e10cSrcweir 		case SbxBYREF | SbxLONG:
293cdf0e10cSrcweir         {
294cdf0e10cSrcweir             sal_Int32 i;
295cdf0e10cSrcweir 			if( n > SbxMAXLNG )
296cdf0e10cSrcweir 			{
297cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); i = SbxMAXLNG;
298cdf0e10cSrcweir 			}
299cdf0e10cSrcweir 			else if( n < SbxMINLNG )
300cdf0e10cSrcweir 			{
301cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); i = SbxMINLNG;
302cdf0e10cSrcweir 			}
303cdf0e10cSrcweir             else
304cdf0e10cSrcweir             {
305cdf0e10cSrcweir                 i = sal::static_int_cast< sal_Int32 >(n);
306cdf0e10cSrcweir             }
307cdf0e10cSrcweir 			*p->pLong = i; break;
308cdf0e10cSrcweir         }
309cdf0e10cSrcweir 		case SbxBYREF | SbxULONG:
310cdf0e10cSrcweir         {
311cdf0e10cSrcweir             sal_uInt32 i;
312cdf0e10cSrcweir 			if( n > SbxMAXULNG )
313cdf0e10cSrcweir 			{
314cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); i = SbxMAXULNG;
315cdf0e10cSrcweir 			}
316cdf0e10cSrcweir 			else if( n < 0 )
317cdf0e10cSrcweir 			{
318cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); i = 0;
319cdf0e10cSrcweir 			}
320cdf0e10cSrcweir             else
321cdf0e10cSrcweir             {
322cdf0e10cSrcweir                 i = sal::static_int_cast< sal_uInt32 >(n);
323cdf0e10cSrcweir             }
324cdf0e10cSrcweir 			*p->pULong = i; break;
325cdf0e10cSrcweir         }
326cdf0e10cSrcweir 		case SbxBYREF | SbxSINGLE:
327cdf0e10cSrcweir 			*p->pSingle = n; break;
328cdf0e10cSrcweir 		case SbxBYREF | SbxDATE:
329cdf0e10cSrcweir 		case SbxBYREF | SbxDOUBLE:
330cdf0e10cSrcweir 			*p->pDouble = (double) n; break;
331cdf0e10cSrcweir 		case SbxBYREF | SbxSALINT64:
332cdf0e10cSrcweir             *p->pnInt64 = ImpDoubleToSalInt64( (double) n ); break;
333cdf0e10cSrcweir 		case SbxBYREF | SbxSALUINT64:
334cdf0e10cSrcweir 			*p->puInt64 = ImpDoubleToSalUInt64( (double) n ); break;
335cdf0e10cSrcweir 		case SbxBYREF | SbxCURRENCY:
336cdf0e10cSrcweir             double d;
337cdf0e10cSrcweir 			if( n > SbxMAXCURR )
338cdf0e10cSrcweir 			{
339cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); d = SbxMAXCURR;
340cdf0e10cSrcweir 			}
341cdf0e10cSrcweir 			else if( n < SbxMINCURR )
342cdf0e10cSrcweir 			{
343cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); d = SbxMINCURR;
344cdf0e10cSrcweir 			}
345cdf0e10cSrcweir             else
346cdf0e10cSrcweir             {
347cdf0e10cSrcweir                 d = n;
348cdf0e10cSrcweir             }
349cdf0e10cSrcweir 			*p->pLong64 = ImpDoubleToCurrency( n ); break;
350cdf0e10cSrcweir 
351cdf0e10cSrcweir 		default:
352cdf0e10cSrcweir 			SbxBase::SetError( SbxERR_CONVERSION );
353cdf0e10cSrcweir 	}
354cdf0e10cSrcweir }
355cdf0e10cSrcweir 
356