xref: /AOO41X/main/basic/source/sbx/sbxbyte.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_basic.hxx"
30*cdf0e10cSrcweir #include <tools/errcode.hxx>
31*cdf0e10cSrcweir #include <basic/sbx.hxx>
32*cdf0e10cSrcweir #include "sbxconv.hxx"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir sal_uInt8 ImpGetByte( const SbxValues* p )
35*cdf0e10cSrcweir {
36*cdf0e10cSrcweir 	SbxValues aTmp;
37*cdf0e10cSrcweir 	sal_uInt8 nRes;
38*cdf0e10cSrcweir start:
39*cdf0e10cSrcweir 	switch( +p->eType )
40*cdf0e10cSrcweir 	{
41*cdf0e10cSrcweir 		case SbxNULL:
42*cdf0e10cSrcweir 			SbxBase::SetError( SbxERR_CONVERSION );
43*cdf0e10cSrcweir 		case SbxEMPTY:
44*cdf0e10cSrcweir 			nRes = 0; break;
45*cdf0e10cSrcweir 		case SbxCHAR:
46*cdf0e10cSrcweir 			if( p->nChar > SbxMAXBYTE )
47*cdf0e10cSrcweir 			{
48*cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); nRes = 0;
49*cdf0e10cSrcweir 			}
50*cdf0e10cSrcweir 			else
51*cdf0e10cSrcweir 				nRes = (sal_uInt8) p->nChar;
52*cdf0e10cSrcweir 			break;
53*cdf0e10cSrcweir 		case SbxBYTE:
54*cdf0e10cSrcweir 			nRes = (sal_uInt8) p->nByte;	break;
55*cdf0e10cSrcweir 		case SbxINTEGER:
56*cdf0e10cSrcweir 		case SbxBOOL:
57*cdf0e10cSrcweir 			if( p->nInteger > SbxMAXBYTE )
58*cdf0e10cSrcweir 			{
59*cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXBYTE;
60*cdf0e10cSrcweir 			}
61*cdf0e10cSrcweir 			else if( p->nInteger < 0 )
62*cdf0e10cSrcweir 			{
63*cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); nRes = 0;
64*cdf0e10cSrcweir 			}
65*cdf0e10cSrcweir 			else
66*cdf0e10cSrcweir 				nRes = (sal_uInt8) p->nInteger;
67*cdf0e10cSrcweir 			break;
68*cdf0e10cSrcweir 		case SbxERROR:
69*cdf0e10cSrcweir 		case SbxUSHORT:
70*cdf0e10cSrcweir 			if( p->nUShort > (sal_uInt16) SbxMAXBYTE )
71*cdf0e10cSrcweir 			{
72*cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXBYTE;
73*cdf0e10cSrcweir 			}
74*cdf0e10cSrcweir 			else
75*cdf0e10cSrcweir 				nRes = (sal_uInt8) p->nUShort;
76*cdf0e10cSrcweir 			break;
77*cdf0e10cSrcweir 		case SbxLONG:
78*cdf0e10cSrcweir 			if( p->nLong > SbxMAXBYTE )
79*cdf0e10cSrcweir 			{
80*cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXBYTE;
81*cdf0e10cSrcweir 			}
82*cdf0e10cSrcweir 			else if( p->nLong < 0 )
83*cdf0e10cSrcweir 			{
84*cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); nRes = 0;
85*cdf0e10cSrcweir 			}
86*cdf0e10cSrcweir 			else
87*cdf0e10cSrcweir 				nRes = (sal_uInt8) p->nLong;
88*cdf0e10cSrcweir 			break;
89*cdf0e10cSrcweir 		case SbxULONG:
90*cdf0e10cSrcweir 			if( p->nULong > SbxMAXBYTE )
91*cdf0e10cSrcweir 			{
92*cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXBYTE;
93*cdf0e10cSrcweir 			}
94*cdf0e10cSrcweir 			else
95*cdf0e10cSrcweir 				nRes = (sal_uInt8) p->nULong;
96*cdf0e10cSrcweir 			break;
97*cdf0e10cSrcweir 		case SbxSALINT64:
98*cdf0e10cSrcweir 			if( p->nInt64 > SbxMAXBYTE )
99*cdf0e10cSrcweir 			{
100*cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXBYTE;
101*cdf0e10cSrcweir 			}
102*cdf0e10cSrcweir 			else if( p->nInt64 < 0 )
103*cdf0e10cSrcweir 			{
104*cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); nRes = 0;
105*cdf0e10cSrcweir 			}
106*cdf0e10cSrcweir 			else
107*cdf0e10cSrcweir 				nRes = (sal_uInt8) p->nInt64;
108*cdf0e10cSrcweir 			break;
109*cdf0e10cSrcweir 		case SbxSALUINT64:
110*cdf0e10cSrcweir 			if( p->uInt64 > SbxMAXBYTE )
111*cdf0e10cSrcweir 			{
112*cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXBYTE;
113*cdf0e10cSrcweir 			}
114*cdf0e10cSrcweir 			else
115*cdf0e10cSrcweir 				nRes = (sal_uInt8) p->uInt64;
116*cdf0e10cSrcweir 			break;
117*cdf0e10cSrcweir 		case SbxSINGLE:
118*cdf0e10cSrcweir 			if( p->nSingle > SbxMAXBYTE )
119*cdf0e10cSrcweir 			{
120*cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXBYTE;
121*cdf0e10cSrcweir 			}
122*cdf0e10cSrcweir 			else if( p->nSingle < 0 )
123*cdf0e10cSrcweir 			{
124*cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); nRes = 0;
125*cdf0e10cSrcweir 			}
126*cdf0e10cSrcweir 			else
127*cdf0e10cSrcweir 				nRes = (sal_uInt8) ImpRound( p->nSingle );
128*cdf0e10cSrcweir 			break;
129*cdf0e10cSrcweir 		case SbxDATE:
130*cdf0e10cSrcweir 		case SbxDOUBLE:
131*cdf0e10cSrcweir 		case SbxLONG64:
132*cdf0e10cSrcweir 		case SbxULONG64:
133*cdf0e10cSrcweir 		case SbxCURRENCY:
134*cdf0e10cSrcweir 		case SbxDECIMAL:
135*cdf0e10cSrcweir 		case SbxBYREF | SbxDECIMAL:
136*cdf0e10cSrcweir 			{
137*cdf0e10cSrcweir 			double dVal;
138*cdf0e10cSrcweir 			if( p->eType ==	SbxCURRENCY )
139*cdf0e10cSrcweir 				dVal = ImpCurrencyToDouble( p->nLong64 );
140*cdf0e10cSrcweir 			else if( p->eType == SbxLONG64 )
141*cdf0e10cSrcweir 				dVal = ImpINT64ToDouble( p->nLong64 );
142*cdf0e10cSrcweir 			else if( p->eType == SbxULONG64 )
143*cdf0e10cSrcweir 				dVal = ImpUINT64ToDouble( p->nULong64 );
144*cdf0e10cSrcweir 			else if( p->eType == SbxDECIMAL )
145*cdf0e10cSrcweir 			{
146*cdf0e10cSrcweir 				dVal = 0.0;
147*cdf0e10cSrcweir 				if( p->pDecimal )
148*cdf0e10cSrcweir 					p->pDecimal->getDouble( dVal );
149*cdf0e10cSrcweir 			}
150*cdf0e10cSrcweir 			else
151*cdf0e10cSrcweir 				dVal = p->nDouble;
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 			if( dVal > SbxMAXBYTE )
154*cdf0e10cSrcweir 			{
155*cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXBYTE;
156*cdf0e10cSrcweir 			}
157*cdf0e10cSrcweir 			else if( dVal < 0 )
158*cdf0e10cSrcweir 			{
159*cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_OVERFLOW ); nRes = 0;
160*cdf0e10cSrcweir 			}
161*cdf0e10cSrcweir 			else
162*cdf0e10cSrcweir 				nRes = (sal_uInt8) ImpRound( dVal );
163*cdf0e10cSrcweir 			break;
164*cdf0e10cSrcweir 			}
165*cdf0e10cSrcweir 		case SbxBYREF | SbxSTRING:
166*cdf0e10cSrcweir 		case SbxSTRING:
167*cdf0e10cSrcweir 		case SbxLPSTR:
168*cdf0e10cSrcweir 			if( !p->pOUString )
169*cdf0e10cSrcweir 				nRes = 0;
170*cdf0e10cSrcweir 			else
171*cdf0e10cSrcweir 			{
172*cdf0e10cSrcweir 				double d;
173*cdf0e10cSrcweir 				SbxDataType t;
174*cdf0e10cSrcweir 				if( ImpScan( *p->pOUString, d, t, NULL ) != SbxERR_OK )
175*cdf0e10cSrcweir 					nRes = 0;
176*cdf0e10cSrcweir 				else if( d > SbxMAXBYTE )
177*cdf0e10cSrcweir 				{
178*cdf0e10cSrcweir 					SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXBYTE;
179*cdf0e10cSrcweir 				}
180*cdf0e10cSrcweir 				else if( d < 0 )
181*cdf0e10cSrcweir 				{
182*cdf0e10cSrcweir 					SbxBase::SetError( SbxERR_OVERFLOW ); nRes = 0;
183*cdf0e10cSrcweir 				}
184*cdf0e10cSrcweir 				else
185*cdf0e10cSrcweir 					nRes = (sal_uInt8) ( d + 0.5 );
186*cdf0e10cSrcweir 			}
187*cdf0e10cSrcweir 			break;
188*cdf0e10cSrcweir 		case SbxOBJECT:
189*cdf0e10cSrcweir 		{
190*cdf0e10cSrcweir 			SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
191*cdf0e10cSrcweir 			if( pVal )
192*cdf0e10cSrcweir 				nRes = pVal->GetByte();
193*cdf0e10cSrcweir 			else
194*cdf0e10cSrcweir 			{
195*cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_NO_OBJECT ); nRes = 0;
196*cdf0e10cSrcweir 			}
197*cdf0e10cSrcweir 			break;
198*cdf0e10cSrcweir 		}
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir 		case SbxBYREF | SbxBYTE:
201*cdf0e10cSrcweir 			nRes = p->nByte; break;
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir 		// ab hier wird getestet
204*cdf0e10cSrcweir 		case SbxBYREF | SbxCHAR:
205*cdf0e10cSrcweir 			aTmp.nChar = *p->pChar; goto ref;
206*cdf0e10cSrcweir 		case SbxBYREF | SbxINTEGER:
207*cdf0e10cSrcweir 		case SbxBYREF | SbxBOOL:
208*cdf0e10cSrcweir 			aTmp.nInteger = *p->pInteger; goto ref;
209*cdf0e10cSrcweir 		case SbxBYREF | SbxLONG:
210*cdf0e10cSrcweir 			aTmp.nLong = *p->pLong; goto ref;
211*cdf0e10cSrcweir 		case SbxBYREF | SbxULONG:
212*cdf0e10cSrcweir 			aTmp.nULong = *p->pULong; goto ref;
213*cdf0e10cSrcweir 		case SbxBYREF | SbxERROR:
214*cdf0e10cSrcweir 		case SbxBYREF | SbxUSHORT:
215*cdf0e10cSrcweir 			aTmp.nUShort = *p->pUShort; goto ref;
216*cdf0e10cSrcweir 		case SbxBYREF | SbxSINGLE:
217*cdf0e10cSrcweir 			aTmp.nSingle = *p->pSingle; goto ref;
218*cdf0e10cSrcweir 		case SbxBYREF | SbxDATE:
219*cdf0e10cSrcweir 		case SbxBYREF | SbxDOUBLE:
220*cdf0e10cSrcweir 			aTmp.nDouble = *p->pDouble; goto ref;
221*cdf0e10cSrcweir 		case SbxBYREF | SbxULONG64:
222*cdf0e10cSrcweir 			aTmp.nULong64 = *p->pULong64; goto ref;
223*cdf0e10cSrcweir 		case SbxBYREF | SbxLONG64:
224*cdf0e10cSrcweir 		case SbxBYREF | SbxCURRENCY:
225*cdf0e10cSrcweir 			aTmp.nLong64 = *p->pLong64; goto ref;
226*cdf0e10cSrcweir 		case SbxBYREF | SbxSALINT64:
227*cdf0e10cSrcweir 			aTmp.nInt64 = *p->pnInt64; goto ref;
228*cdf0e10cSrcweir 		case SbxBYREF | SbxSALUINT64:
229*cdf0e10cSrcweir 			aTmp.uInt64 = *p->puInt64; goto ref;
230*cdf0e10cSrcweir 		ref:
231*cdf0e10cSrcweir 			aTmp.eType = SbxDataType( p->eType & 0x0FFF );
232*cdf0e10cSrcweir 			p = &aTmp; goto start;
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir 		default:
235*cdf0e10cSrcweir 			SbxBase::SetError( SbxERR_CONVERSION ); nRes = 0;
236*cdf0e10cSrcweir 	}
237*cdf0e10cSrcweir 	return nRes;
238*cdf0e10cSrcweir }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir void ImpPutByte( SbxValues* p, sal_uInt8 n )
241*cdf0e10cSrcweir {
242*cdf0e10cSrcweir 	switch( +p->eType )
243*cdf0e10cSrcweir 	{
244*cdf0e10cSrcweir 		case SbxBYTE:
245*cdf0e10cSrcweir 			p->nByte = n; break;
246*cdf0e10cSrcweir 		case SbxINTEGER:
247*cdf0e10cSrcweir 		case SbxBOOL:
248*cdf0e10cSrcweir 			p->nInteger = n; break;
249*cdf0e10cSrcweir 		case SbxERROR:
250*cdf0e10cSrcweir 		case SbxUSHORT:
251*cdf0e10cSrcweir 			p->nUShort = n; break;
252*cdf0e10cSrcweir 		case SbxLONG:
253*cdf0e10cSrcweir 			p->nLong = n; break;
254*cdf0e10cSrcweir 		case SbxULONG:
255*cdf0e10cSrcweir 			p->nULong = n; break;
256*cdf0e10cSrcweir 		case SbxSINGLE:
257*cdf0e10cSrcweir 			p->nSingle = n; break;
258*cdf0e10cSrcweir 		case SbxDATE:
259*cdf0e10cSrcweir 		case SbxDOUBLE:
260*cdf0e10cSrcweir 			p->nDouble = n; break;
261*cdf0e10cSrcweir 		case SbxSALINT64:
262*cdf0e10cSrcweir 			p->nInt64 = n; break;
263*cdf0e10cSrcweir 		case SbxSALUINT64:
264*cdf0e10cSrcweir 			p->uInt64 = n; break;
265*cdf0e10cSrcweir 		case SbxULONG64:
266*cdf0e10cSrcweir 			p->nULong64 = ImpDoubleToUINT64( (double)n ); break;
267*cdf0e10cSrcweir 		case SbxLONG64:
268*cdf0e10cSrcweir 			p->nLong64 = ImpDoubleToINT64( (double)n ); break;
269*cdf0e10cSrcweir 		case SbxCURRENCY:
270*cdf0e10cSrcweir 			p->nLong64 = ImpDoubleToCurrency( (double)n ); break;
271*cdf0e10cSrcweir 		case SbxDECIMAL:
272*cdf0e10cSrcweir 		case SbxBYREF | SbxDECIMAL:
273*cdf0e10cSrcweir 			ImpCreateDecimal( p )->setByte( n );
274*cdf0e10cSrcweir 			break;
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir 		case SbxCHAR:
277*cdf0e10cSrcweir 			p->nChar = (xub_Unicode) n; break;
278*cdf0e10cSrcweir 
279*cdf0e10cSrcweir 		case SbxBYREF | SbxSTRING:
280*cdf0e10cSrcweir 		case SbxSTRING:
281*cdf0e10cSrcweir 		case SbxLPSTR:
282*cdf0e10cSrcweir 			if( !p->pOUString )
283*cdf0e10cSrcweir 				p->pOUString = new ::rtl::OUString;
284*cdf0e10cSrcweir 			ImpCvtNum( (double) n, 0, *p->pOUString );
285*cdf0e10cSrcweir 			break;
286*cdf0e10cSrcweir 		case SbxOBJECT:
287*cdf0e10cSrcweir 		{
288*cdf0e10cSrcweir 			SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
289*cdf0e10cSrcweir 			if( pVal )
290*cdf0e10cSrcweir 				pVal->PutByte( n );
291*cdf0e10cSrcweir 			else
292*cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_NO_OBJECT );
293*cdf0e10cSrcweir 			break;
294*cdf0e10cSrcweir 		}
295*cdf0e10cSrcweir 		case SbxBYREF | SbxCHAR:
296*cdf0e10cSrcweir 			*p->pChar = (xub_Unicode) n; break;
297*cdf0e10cSrcweir 		case SbxBYREF | SbxBYTE:
298*cdf0e10cSrcweir 			*p->pByte = n; break;
299*cdf0e10cSrcweir 		case SbxBYREF | SbxINTEGER:
300*cdf0e10cSrcweir 		case SbxBYREF | SbxBOOL:
301*cdf0e10cSrcweir 			*p->pInteger = n; break;
302*cdf0e10cSrcweir 		case SbxBYREF | SbxERROR:
303*cdf0e10cSrcweir 		case SbxBYREF | SbxUSHORT:
304*cdf0e10cSrcweir 			*p->pUShort = n; break;
305*cdf0e10cSrcweir 		case SbxBYREF | SbxLONG:
306*cdf0e10cSrcweir 			*p->pLong = n; break;
307*cdf0e10cSrcweir 		case SbxBYREF | SbxULONG:
308*cdf0e10cSrcweir 			*p->pULong = n; break;
309*cdf0e10cSrcweir 		case SbxBYREF | SbxSINGLE:
310*cdf0e10cSrcweir 			*p->pSingle = n; break;
311*cdf0e10cSrcweir 		case SbxBYREF | SbxDATE:
312*cdf0e10cSrcweir 		case SbxBYREF | SbxDOUBLE:
313*cdf0e10cSrcweir 			*p->pDouble = n; break;
314*cdf0e10cSrcweir 		case SbxBYREF | SbxULONG64:
315*cdf0e10cSrcweir 			*p->pULong64 = ImpDoubleToUINT64( (double)n ); break;
316*cdf0e10cSrcweir 		case SbxBYREF | SbxLONG64:
317*cdf0e10cSrcweir 			*p->pLong64 = ImpDoubleToINT64( (double)n ); break;
318*cdf0e10cSrcweir 		case SbxBYREF | SbxSALINT64:
319*cdf0e10cSrcweir 			*p->pnInt64 = n; break;
320*cdf0e10cSrcweir 		case SbxBYREF | SbxSALUINT64:
321*cdf0e10cSrcweir 			*p->puInt64 = n; break;
322*cdf0e10cSrcweir 		case SbxBYREF | SbxCURRENCY:
323*cdf0e10cSrcweir 			*p->pLong64 = ImpDoubleToCurrency( (double)n ); break;
324*cdf0e10cSrcweir 
325*cdf0e10cSrcweir 		default:
326*cdf0e10cSrcweir 			SbxBase::SetError( SbxERR_CONVERSION );
327*cdf0e10cSrcweir 	}
328*cdf0e10cSrcweir }
329*cdf0e10cSrcweir 
330