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