xref: /trunk/main/basic/source/sbx/sbxsng.cxx (revision e1f63238)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_basic.hxx"
26 #include <tools/errcode.hxx>
27 #include <basic/sbx.hxx>
28 #include "sbxconv.hxx"
29 
ImpGetSingle(const SbxValues * p)30 float ImpGetSingle( const SbxValues* p )
31 {
32 	SbxValues aTmp;
33 	float nRes;
34 start:
35 	switch( +p->eType )
36 	{
37 		case SbxNULL:
38 			SbxBase::SetError( SbxERR_CONVERSION );
39 		case SbxEMPTY:
40 			nRes = 0; break;
41 		case SbxCHAR:
42 			nRes = p->nChar; break;
43 		case SbxBYTE:
44 			nRes = p->nByte; break;
45 		case SbxINTEGER:
46 		case SbxBOOL:
47 			nRes = p->nInteger; break;
48 		case SbxERROR:
49 		case SbxUSHORT:
50 			nRes = p->nUShort; break;
51 		case SbxLONG:
52 			nRes = (float) p->nLong; break;
53 		case SbxULONG:
54 			nRes = (float) p->nULong; break;
55 		case SbxSINGLE:
56 			nRes = p->nSingle; break;
57 		case SbxSALINT64:
58             nRes = (float) p->nInt64; break;
59 		case SbxSALUINT64:
60             nRes = (float) ImpSalUInt64ToDouble( p->uInt64 ); break;
61 		case SbxDECIMAL:
62 		case SbxBYREF | SbxDECIMAL:
63 			if( p->pDecimal )
64 				p->pDecimal->getSingle( nRes );
65 			else
66 				nRes = 0.0;
67 			break;
68 		case SbxDATE:
69 		case SbxDOUBLE:
70 		case SbxLONG64:
71 		case SbxULONG64:
72 		case SbxCURRENCY:
73 			{
74 			double dVal;
75 			if( p->eType ==	SbxCURRENCY )
76 				dVal = ImpCurrencyToDouble( p->nLong64 );
77 			else if( p->eType == SbxLONG64 )
78 				dVal = ImpINT64ToDouble( p->nLong64 );
79 			else if( p->eType == SbxULONG64 )
80 				dVal = ImpUINT64ToDouble( p->nULong64 );
81 			else
82 				dVal = p->nDouble;
83 
84 			if( dVal > SbxMAXSNG )
85 			{
86 				SbxBase::SetError( SbxERR_OVERFLOW );
87                 nRes = static_cast< float >(SbxMAXSNG);
88 			}
89 			else if( dVal < SbxMINSNG )
90 			{
91 				SbxBase::SetError( SbxERR_OVERFLOW );
92                 nRes = static_cast< float >(SbxMINSNG);
93 			}
94 			else if( dVal > 0 && dVal < SbxMAXSNG2 )
95 			{
96 				SbxBase::SetError( SbxERR_OVERFLOW );
97                 nRes = static_cast< float >(SbxMAXSNG2);
98 			}
99 			else if( dVal < 0 && dVal > SbxMINSNG2 )
100 			{
101 				SbxBase::SetError( SbxERR_OVERFLOW );
102                 nRes = static_cast< float >(SbxMINSNG2);
103 			}
104 			else
105 				nRes = (float) dVal;
106 			break;
107 			}
108 		case SbxBYREF | SbxSTRING:
109 		case SbxSTRING:
110 		case SbxLPSTR:
111 			if( !p->pOUString )
112 				nRes = 0;
113 			else
114 			{
115 				double d;
116 				SbxDataType t;
117 				if( ImpScan( *p->pOUString, d, t, NULL ) != SbxERR_OK )
118 					nRes = 0;
119 				else if( d > SbxMAXSNG )
120 				{
121 					SbxBase::SetError( SbxERR_OVERFLOW );
122                     nRes = static_cast< float >(SbxMAXSNG);
123 				}
124 				else if( d < SbxMINSNG )
125 				{
126 					SbxBase::SetError( SbxERR_OVERFLOW );
127                     nRes = static_cast< float >(SbxMINSNG);
128 				}
129 				else
130 					nRes = (float) d;
131 			}
132 			break;
133 		case SbxOBJECT:
134 		{
135 			SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
136 			if( pVal )
137 				nRes = pVal->GetSingle();
138 			else
139 			{
140 				SbxBase::SetError( SbxERR_NO_OBJECT ); nRes = 0;
141 			}
142 			break;
143 		}
144 
145 		case SbxBYREF | SbxCHAR:
146 			nRes = *p->pChar; break;
147 		case SbxBYREF | SbxBYTE:
148 			nRes = *p->pByte; break;
149 		case SbxBYREF | SbxINTEGER:
150 		case SbxBYREF | SbxBOOL:
151 			nRes = *p->pInteger; break;
152 		case SbxBYREF | SbxLONG:
153 			nRes = (float) *p->pLong; break;
154 		case SbxBYREF | SbxULONG:
155 			nRes = (float) *p->pULong; break;
156 		case SbxBYREF | SbxERROR:
157 		case SbxBYREF | SbxUSHORT:
158 			nRes = *p->pUShort; break;
159 		case SbxBYREF | SbxSINGLE:
160 			nRes = *p->pSingle; break;
161 		// ab hier muss getestet werden
162 		case SbxBYREF | SbxDATE:
163 		case SbxBYREF | SbxDOUBLE:
164 			aTmp.nDouble = *p->pDouble; goto ref;
165 		case SbxBYREF | SbxULONG64:
166 			aTmp.nULong64 = *p->pULong64; goto ref;
167 		case SbxBYREF | SbxLONG64:
168 		case SbxBYREF | SbxSALINT64:
169             nRes = (float) *p->pnInt64; break;
170 		case SbxBYREF | SbxSALUINT64:
171             nRes = (float) ImpSalUInt64ToDouble( *p->puInt64 ); break;
172 		case SbxBYREF | SbxCURRENCY:
173 			aTmp.nLong64 = *p->pLong64; goto ref;
174 		ref:
175 			aTmp.eType = SbxDataType( p->eType & 0x0FFF );
176 			p = &aTmp; goto start;
177 
178 		default:
179 			SbxBase::SetError( SbxERR_CONVERSION ); nRes = 0;
180 	}
181 	return nRes;
182 }
183 
ImpPutSingle(SbxValues * p,float n)184 void ImpPutSingle( SbxValues* p, float n )
185 {
186 	SbxValues aTmp;
187 start:
188 	switch( +p->eType )
189 	{
190 		case SbxCHAR:
191 			aTmp.pChar = &p->nChar; goto direct;
192 		case SbxBYTE:
193 			aTmp.pByte = &p->nByte; goto direct;
194 		case SbxINTEGER:
195 		case SbxBOOL:
196 			aTmp.pInteger = &p->nInteger; goto direct;
197 		case SbxLONG:
198 			aTmp.pLong = &p->nLong; goto direct;
199 		case SbxULONG:
200 			aTmp.pULong = &p->nULong; goto direct;
201 		case SbxERROR:
202 		case SbxUSHORT:
203 			aTmp.pUShort = &p->nUShort; goto direct;
204 		case SbxULONG64:
205 			aTmp.pULong64 = &p->nULong64; goto direct;
206 		case SbxLONG64:
207 		case SbxCURRENCY:
208 			aTmp.pLong64 = &p->nLong64; goto direct;
209 		case SbxSALINT64:
210 			aTmp.pnInt64 = &p->nInt64; goto direct;
211 		case SbxSALUINT64:
212 			aTmp.puInt64 = &p->uInt64; goto direct;
213 		case SbxDECIMAL:
214 		case SbxBYREF | SbxDECIMAL:
215 			{
216 			SbxDecimal* pDec = ImpCreateDecimal( p );
217 			if( !pDec->setSingle( n ) )
218 				SbxBase::SetError( SbxERR_OVERFLOW );
219 			break;
220 			}
221 		direct:
222 			aTmp.eType = SbxDataType( p->eType | SbxBYREF );
223 			p = &aTmp; goto start;
224 
225 		// keine Tests ab hier
226 		case SbxSINGLE:
227 			p->nSingle = n; break;
228 		case SbxDATE:
229 		case SbxDOUBLE:
230 			p->nDouble = n; break;
231 
232 		case SbxBYREF | SbxSTRING:
233 		case SbxSTRING:
234 		case SbxLPSTR:
235 		{
236 			if( !p->pOUString )
237 				p->pOUString = new ::rtl::OUString;
238 			ImpCvtNum( (double) n, 6, *p->pOUString );
239 			break;
240 		}
241 		case SbxOBJECT:
242 		{
243 			SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
244 			if( pVal )
245 				pVal->PutSingle( n );
246 			else
247 				SbxBase::SetError( SbxERR_NO_OBJECT );
248 			break;
249 		}
250 		case SbxBYREF | SbxCHAR:
251 			if( n > SbxMAXCHAR )
252 			{
253 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXCHAR;
254 			}
255 			else if( n < SbxMINCHAR )
256 			{
257 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINCHAR;
258 			}
259 			*p->pChar = (xub_Unicode) n; break;
260 		case SbxBYREF | SbxBYTE:
261 			if( n > SbxMAXBYTE )
262 			{
263 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXBYTE;
264 			}
265 			else if( n < 0 )
266 			{
267 				SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
268 			}
269 			*p->pByte = (sal_uInt8) n; break;
270 		case SbxBYREF | SbxINTEGER:
271 		case SbxBYREF | SbxBOOL:
272 			if( n > SbxMAXINT )
273 			{
274 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXINT;
275 			}
276 			else if( n < SbxMININT )
277 			{
278 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMININT;
279 			}
280 			*p->pInteger = (sal_Int16) n; break;
281 		case SbxBYREF | SbxERROR:
282 		case SbxBYREF | SbxUSHORT:
283 			if( n > SbxMAXUINT )
284 			{
285 				SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXUINT;
286 			}
287 			else if( n < 0 )
288 			{
289 				SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
290 			}
291 			*p->pUShort = (sal_uInt16) n; break;
292 		case SbxBYREF | SbxLONG:
293         {
294             sal_Int32 i;
295 			if( n > SbxMAXLNG )
296 			{
297 				SbxBase::SetError( SbxERR_OVERFLOW ); i = SbxMAXLNG;
298 			}
299 			else if( n < SbxMINLNG )
300 			{
301 				SbxBase::SetError( SbxERR_OVERFLOW ); i = SbxMINLNG;
302 			}
303             else
304             {
305                 i = sal::static_int_cast< sal_Int32 >(n);
306             }
307 			*p->pLong = i; break;
308         }
309 		case SbxBYREF | SbxULONG:
310         {
311             sal_uInt32 i;
312 			if( n > SbxMAXULNG )
313 			{
314 				SbxBase::SetError( SbxERR_OVERFLOW ); i = SbxMAXULNG;
315 			}
316 			else if( n < 0 )
317 			{
318 				SbxBase::SetError( SbxERR_OVERFLOW ); i = 0;
319 			}
320             else
321             {
322                 i = sal::static_int_cast< sal_uInt32 >(n);
323             }
324 			*p->pULong = i; break;
325         }
326 		case SbxBYREF | SbxSINGLE:
327 			*p->pSingle = n; break;
328 		case SbxBYREF | SbxDATE:
329 		case SbxBYREF | SbxDOUBLE:
330 			*p->pDouble = (double) n; break;
331 		case SbxBYREF | SbxSALINT64:
332             *p->pnInt64 = ImpDoubleToSalInt64( (double) n ); break;
333 		case SbxBYREF | SbxSALUINT64:
334 			*p->puInt64 = ImpDoubleToSalUInt64( (double) n ); break;
335 		case SbxBYREF | SbxCURRENCY:
336             double d;
337 			if( n > SbxMAXCURR )
338 			{
339 				SbxBase::SetError( SbxERR_OVERFLOW ); d = SbxMAXCURR;
340 			}
341 			else if( n < SbxMINCURR )
342 			{
343 				SbxBase::SetError( SbxERR_OVERFLOW ); d = SbxMINCURR;
344 			}
345             else
346             {
347                 d = n;
348             }
349 			*p->pLong64 = ImpDoubleToCurrency( n ); break;
350 
351 		default:
352 			SbxBase::SetError( SbxERR_CONVERSION );
353 	}
354 }
355 
356