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