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