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