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