merge the formfield patch from ooo-build
[ooovba.git] / basic / source / sbx / sbxuint.cxx
blob5674fd6a5fdd5967df407e1289483234291de280
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: sbxuint.cxx,v $
10 * $Revision: 1.8 $
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 UINT16 ImpGetUShort( const SbxValues* p )
39 SbxValues aTmp;
40 UINT16 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 nRes = p->nChar;
50 break;
51 case SbxBYTE:
52 nRes = p->nByte; break;
53 case SbxINTEGER:
54 case SbxBOOL:
55 if( p->nInteger < 0 )
57 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = 0;
59 else
60 nRes = p->nInteger;
61 break;
62 case SbxERROR:
63 case SbxUSHORT:
64 nRes = p->nUShort;
65 break;
66 case SbxLONG:
67 if( p->nLong > SbxMAXUINT )
69 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXUINT;
71 else if( p->nLong < 0 )
73 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = 0;
75 else
76 nRes = (UINT16) p->nLong;
77 break;
78 case SbxULONG:
79 if( p->nULong > SbxMAXUINT )
81 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXUINT;
83 else
84 nRes = (UINT16) p->nULong;
85 break;
86 case SbxSALINT64:
87 if( p->nInt64 > SbxMAXUINT )
89 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXUINT;
91 else if( p->nInt64 < 0 )
93 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = 0;
95 else
96 nRes = (UINT16) p->nInt64;
97 break;
98 case SbxSALUINT64:
99 if( p->uInt64 > SbxMAXUINT )
101 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXUINT;
103 else
104 nRes = (UINT16) p->uInt64;
105 break;
106 case SbxSINGLE:
107 if( p->nSingle > SbxMAXUINT )
109 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXUINT;
111 else if( p->nSingle < 0 )
113 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = 0;
115 else
116 nRes = (UINT16) ( p->nSingle + 0.5 );
117 break;
118 case SbxDATE:
119 case SbxDOUBLE:
120 case SbxLONG64:
121 case SbxULONG64:
122 case SbxCURRENCY:
123 case SbxDECIMAL:
124 case SbxBYREF | SbxDECIMAL:
126 double dVal;
127 if( p->eType == SbxCURRENCY )
128 dVal = ImpCurrencyToDouble( p->nLong64 );
129 else if( p->eType == SbxLONG64 )
130 dVal = ImpINT64ToDouble( p->nLong64 );
131 else if( p->eType == SbxULONG64 )
132 dVal = ImpUINT64ToDouble( p->nULong64 );
133 else if( p->eType == SbxDECIMAL )
135 dVal = 0.0;
136 if( p->pDecimal )
137 p->pDecimal->getDouble( dVal );
139 else
140 dVal = p->nDouble;
142 if( dVal > SbxMAXUINT )
144 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXUINT;
146 else if( dVal < 0 )
148 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = 0;
150 else
151 nRes = (UINT16) ( dVal + 0.5 );
152 break;
154 case SbxBYREF | SbxSTRING:
155 case SbxSTRING:
156 case SbxLPSTR:
157 if( !p->pString )
158 nRes = 0;
159 else
161 double d;
162 SbxDataType t;
163 if( ImpScan( *p->pString, d, t, NULL ) != SbxERR_OK )
164 nRes = 0;
165 else if( d > SbxMAXUINT )
167 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXUINT;
169 else if( d < 0 )
171 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = 0;
173 else
174 nRes = (UINT16) ( d + 0.5 );
176 break;
177 case SbxOBJECT:
179 SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
180 if( pVal )
181 nRes = pVal->GetUShort();
182 else
184 SbxBase::SetError( SbxERR_NO_OBJECT ); nRes = 0;
186 break;
189 case SbxBYREF | SbxBYTE:
190 nRes = *p->pByte; break;
191 case SbxBYREF | SbxERROR:
192 case SbxBYREF | SbxUSHORT:
193 nRes = *p->pUShort; break;
195 // ab hier wird getestet
196 case SbxBYREF | SbxCHAR:
197 aTmp.nChar = *p->pChar; goto ref;
198 case SbxBYREF | SbxINTEGER:
199 case SbxBYREF | SbxBOOL:
200 aTmp.nInteger = *p->pInteger; goto ref;
201 case SbxBYREF | SbxLONG:
202 aTmp.nLong = *p->pLong; goto ref;
203 case SbxBYREF | SbxULONG:
204 aTmp.nULong = *p->pULong; goto ref;
205 case SbxBYREF | SbxSINGLE:
206 aTmp.nSingle = *p->pSingle; goto ref;
207 case SbxBYREF | SbxDATE:
208 case SbxBYREF | SbxDOUBLE:
209 aTmp.nDouble = *p->pDouble; goto ref;
210 case SbxBYREF | SbxULONG64:
211 aTmp.nULong64 = *p->pULong64; goto ref;
212 case SbxBYREF | SbxLONG64:
213 case SbxBYREF | SbxCURRENCY:
214 aTmp.nLong64 = *p->pLong64; goto ref;
215 case SbxBYREF | SbxSALINT64:
216 aTmp.nInt64 = *p->pnInt64; goto ref;
217 case SbxBYREF | SbxSALUINT64:
218 aTmp.uInt64 = *p->puInt64; goto ref;
219 ref:
220 aTmp.eType = SbxDataType( p->eType & 0x0FFF );
221 p = &aTmp; goto start;
223 default:
224 SbxBase::SetError( SbxERR_CONVERSION ); nRes = 0;
226 return nRes;
229 void ImpPutUShort( SbxValues* p, UINT16 n )
231 SbxValues aTmp;
233 start:
234 switch( +p->eType )
236 case SbxERROR:
237 case SbxUSHORT:
238 p->nUShort = n; break;
239 case SbxLONG:
240 p->nLong = n; break;
241 case SbxULONG:
242 p->nULong = n; break;
243 case SbxSINGLE:
244 p->nSingle = n; break;
245 case SbxDATE:
246 case SbxDOUBLE:
247 p->nDouble = n; break;
248 case SbxSALINT64:
249 p->nInt64 = n; break;
250 case SbxSALUINT64:
251 p->uInt64 = n; break;
252 case SbxULONG64:
253 p->nULong64 = ImpDoubleToUINT64( (double)n ); break;
254 case SbxLONG64:
255 p->nLong64 = ImpDoubleToINT64( (double)n ); break;
256 case SbxCURRENCY:
257 p->nLong64 = ImpDoubleToCurrency( (double)n ); break;
258 case SbxDECIMAL:
259 case SbxBYREF | SbxDECIMAL:
260 ImpCreateDecimal( p )->setUInt( n );
261 break;
263 // Tests ab hier
264 case SbxCHAR:
265 aTmp.pChar = &p->nChar; goto direct;
266 case SbxBYTE:
267 aTmp.pByte = &p->nByte; goto direct;
268 case SbxINTEGER:
269 case SbxBOOL:
270 aTmp.pInteger = &p->nInteger;
271 direct:
272 aTmp.eType = SbxDataType( p->eType | SbxBYREF );
273 p = &aTmp; goto start;
275 case SbxBYREF | SbxSTRING:
276 case SbxSTRING:
277 case SbxLPSTR:
278 if( !p->pString )
279 p->pString = new XubString;
280 ImpCvtNum( (double) n, 0, *p->pString );
281 break;
282 case SbxOBJECT:
284 SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
285 if( pVal )
286 pVal->PutUShort( n );
287 else
288 SbxBase::SetError( SbxERR_NO_OBJECT );
289 break;
292 case SbxBYREF | SbxCHAR:
293 *p->pChar = (xub_Unicode) n; break;
294 case SbxBYREF | SbxBYTE:
295 if( n > SbxMAXBYTE )
297 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXBYTE;
299 *p->pByte = (BYTE) n; break;
300 case SbxBYREF | SbxINTEGER:
301 case SbxBYREF | SbxBOOL:
302 if( n > SbxMAXINT )
304 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXINT;
306 *p->pInteger = (INT16) n; break;
307 case SbxBYREF | SbxERROR:
308 case SbxBYREF | SbxUSHORT:
309 *p->pUShort = n; break;
310 case SbxBYREF | SbxLONG:
311 *p->pLong = n; break;
312 case SbxBYREF | SbxULONG:
313 *p->pULong = n; break;
314 case SbxBYREF | SbxSINGLE:
315 *p->pSingle = n; break;
316 case SbxBYREF | SbxDATE:
317 case SbxBYREF | SbxDOUBLE:
318 *p->pDouble = n; break;
319 case SbxBYREF | SbxSALINT64:
320 *p->pnInt64 = n; break;
321 case SbxBYREF | SbxSALUINT64:
322 *p->puInt64 = n; break;
323 case SbxBYREF | SbxULONG64:
324 *p->pULong64 = ImpDoubleToUINT64( (double)n ); break;
325 case SbxBYREF | SbxLONG64:
326 *p->pLong64 = ImpDoubleToINT64( (double)n ); break;
327 case SbxBYREF | SbxCURRENCY:
328 *p->pLong64 = ImpDoubleToCurrency( (double)n ); break;
330 default:
331 SbxBase::SetError( SbxERR_CONVERSION );