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