Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / basic / source / sbx / sbxsng.cxx
blobf48d185e282982cfe0a717217cbd2532716abe47
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <tools/errcode.hxx>
21 #include <basic/sbx.hxx>
22 #include "sbxconv.hxx"
24 float ImpGetSingle( const SbxValues* p )
26 SbxValues aTmp;
27 float nRes;
28 start:
29 switch( +p->eType )
31 case SbxNULL:
32 SbxBase::SetError( SbxERR_CONVERSION );
33 case SbxEMPTY:
34 nRes = 0; break;
35 case SbxCHAR:
36 nRes = p->nChar; break;
37 case SbxBYTE:
38 nRes = p->nByte; break;
39 case SbxINTEGER:
40 case SbxBOOL:
41 nRes = p->nInteger; break;
42 case SbxERROR:
43 case SbxUSHORT:
44 nRes = p->nUShort; break;
45 case SbxLONG:
46 nRes = (float) p->nLong; break;
47 case SbxULONG:
48 nRes = (float) p->nULong; break;
49 case SbxSINGLE:
50 nRes = p->nSingle; break;
51 case SbxDECIMAL:
52 case SbxBYREF | SbxDECIMAL:
53 if( p->pDecimal )
54 p->pDecimal->getSingle( nRes );
55 else
56 nRes = 0.0;
57 break;
58 case SbxDATE:
59 case SbxDOUBLE:
60 case SbxCURRENCY:
61 case SbxSALINT64:
62 case SbxSALUINT64:
64 double dVal;
65 if( p->eType == SbxCURRENCY )
66 dVal = ImpCurrencyToDouble( p->nInt64 );
67 else if( p->eType == SbxSALINT64 )
68 dVal = (float) p->nInt64;
69 else if( p->eType == SbxSALUINT64 )
70 dVal = (float) p->uInt64;
71 else
72 dVal = p->nDouble;
74 if( dVal > SbxMAXSNG )
76 SbxBase::SetError( SbxERR_OVERFLOW );
77 nRes = static_cast< float >(SbxMAXSNG);
79 else if( dVal < SbxMINSNG )
81 SbxBase::SetError( SbxERR_OVERFLOW );
82 nRes = static_cast< float >(SbxMINSNG);
84 // tests for underflow - storing value too small for precision of single
85 else if( dVal > 0 && dVal < SbxMAXSNG2 )
87 SbxBase::SetError( SbxERR_OVERFLOW );
88 nRes = static_cast< float >(SbxMAXSNG2);
90 else if( dVal < 0 && dVal > SbxMINSNG2 )
92 SbxBase::SetError( SbxERR_OVERFLOW );
93 nRes = static_cast< float >(SbxMINSNG2);
95 else
96 nRes = (float) dVal;
97 break;
99 case SbxBYREF | SbxSTRING:
100 case SbxSTRING:
101 case SbxLPSTR:
102 if( !p->pOUString )
103 nRes = 0;
104 else
106 double d;
107 SbxDataType t;
108 if( ImpScan( *p->pOUString, d, t, NULL ) != SbxERR_OK )
109 nRes = 0;
110 else if( d > SbxMAXSNG )
112 SbxBase::SetError( SbxERR_OVERFLOW );
113 nRes = static_cast< float >(SbxMAXSNG);
115 else if( d < SbxMINSNG )
117 SbxBase::SetError( SbxERR_OVERFLOW );
118 nRes = static_cast< float >(SbxMINSNG);
120 else
121 nRes = (float) d;
123 break;
124 case SbxOBJECT:
126 SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
127 if( pVal )
128 nRes = pVal->GetSingle();
129 else
131 SbxBase::SetError( SbxERR_NO_OBJECT ); nRes = 0;
133 break;
136 case SbxBYREF | SbxCHAR:
137 nRes = *p->pChar; break;
138 case SbxBYREF | SbxBYTE:
139 nRes = *p->pByte; break;
140 case SbxBYREF | SbxINTEGER:
141 case SbxBYREF | SbxBOOL:
142 nRes = *p->pInteger; break;
143 case SbxBYREF | SbxLONG:
144 nRes = (float) *p->pLong; break;
145 case SbxBYREF | SbxULONG:
146 nRes = (float) *p->pULong; break;
147 case SbxBYREF | SbxERROR:
148 case SbxBYREF | SbxUSHORT:
149 nRes = *p->pUShort; break;
150 case SbxBYREF | SbxSINGLE:
151 nRes = *p->pSingle; break;
152 // from here had to be tested
153 case SbxBYREF | SbxDATE:
154 case SbxBYREF | SbxDOUBLE:
155 aTmp.nDouble = *p->pDouble; goto ref;
156 case SbxBYREF | SbxSALINT64:
157 case SbxBYREF | SbxCURRENCY:
158 aTmp.nInt64 = *p->pnInt64; goto ref;
159 case SbxBYREF | SbxSALUINT64:
160 aTmp.uInt64 = *p->puInt64; goto ref;
161 ref:
162 aTmp.eType = SbxDataType( p->eType & 0x0FFF );
163 p = &aTmp; goto start;
165 default:
166 SbxBase::SetError( SbxERR_CONVERSION ); nRes = 0;
168 return nRes;
171 void ImpPutSingle( SbxValues* p, float n )
173 SbxValues aTmp;
174 start:
175 switch( +p->eType )
177 case SbxCHAR:
178 aTmp.pChar = &p->nChar; goto direct;
179 case SbxBYTE:
180 aTmp.pByte = &p->nByte; goto direct;
181 case SbxINTEGER:
182 case SbxBOOL:
183 aTmp.pInteger = &p->nInteger; goto direct;
184 case SbxLONG:
185 aTmp.pLong = &p->nLong; goto direct;
186 case SbxULONG:
187 aTmp.pULong = &p->nULong; goto direct;
188 case SbxERROR:
189 case SbxUSHORT:
190 aTmp.pUShort = &p->nUShort; goto direct;
191 case SbxCURRENCY:
192 case SbxSALINT64:
193 aTmp.pnInt64 = &p->nInt64; goto direct;
194 case SbxSALUINT64:
195 aTmp.puInt64 = &p->uInt64; goto direct;
196 case SbxDECIMAL:
197 case SbxBYREF | SbxDECIMAL:
199 SbxDecimal* pDec = ImpCreateDecimal( p );
200 if( !pDec->setSingle( n ) )
201 SbxBase::SetError( SbxERR_OVERFLOW );
202 break;
204 direct:
205 aTmp.eType = SbxDataType( p->eType | SbxBYREF );
206 p = &aTmp; goto start;
208 // from here no tests
209 case SbxSINGLE:
210 p->nSingle = n; break;
211 case SbxDATE:
212 case SbxDOUBLE:
213 p->nDouble = n; break;
215 case SbxBYREF | SbxSTRING:
216 case SbxSTRING:
217 case SbxLPSTR:
219 if( !p->pOUString )
220 p->pOUString = new ::rtl::OUString;
221 ImpCvtNum( (double) n, 6, *p->pOUString );
222 break;
224 case SbxOBJECT:
226 SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
227 if( pVal )
228 pVal->PutSingle( n );
229 else
230 SbxBase::SetError( SbxERR_NO_OBJECT );
231 break;
233 case SbxBYREF | SbxCHAR:
234 if( n > SbxMAXCHAR )
236 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXCHAR;
238 else if( n < SbxMINCHAR )
240 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINCHAR;
242 *p->pChar = (sal_Unicode) n; break;
243 case SbxBYREF | SbxBYTE:
244 if( n > SbxMAXBYTE )
246 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXBYTE;
248 else if( n < 0 )
250 SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
252 *p->pByte = (sal_uInt8) n; break;
253 case SbxBYREF | SbxINTEGER:
254 case SbxBYREF | SbxBOOL:
255 if( n > SbxMAXINT )
257 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXINT;
259 else if( n < SbxMININT )
261 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMININT;
263 *p->pInteger = (sal_Int16) n; break;
264 case SbxBYREF | SbxERROR:
265 case SbxBYREF | SbxUSHORT:
266 if( n > SbxMAXUINT )
268 SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXUINT;
270 else if( n < 0 )
272 SbxBase::SetError( SbxERR_OVERFLOW ); n = 0;
274 *p->pUShort = (sal_uInt16) n; break;
275 case SbxBYREF | SbxLONG:
277 sal_Int32 i;
278 if( n > SbxMAXLNG )
280 SbxBase::SetError( SbxERR_OVERFLOW ); i = SbxMAXLNG;
282 else if( n < SbxMINLNG )
284 SbxBase::SetError( SbxERR_OVERFLOW ); i = SbxMINLNG;
286 else
288 i = sal::static_int_cast< sal_Int32 >(n);
290 *p->pLong = i; break;
292 case SbxBYREF | SbxULONG:
294 sal_uInt32 i;
295 if( n > SbxMAXULNG )
297 SbxBase::SetError( SbxERR_OVERFLOW ); i = SbxMAXULNG;
299 else if( n < 0 )
301 SbxBase::SetError( SbxERR_OVERFLOW ); i = 0;
303 else
305 i = sal::static_int_cast< sal_uInt32 >(n);
307 *p->pULong = i; break;
309 case SbxBYREF | SbxSINGLE:
310 *p->pSingle = n; break;
311 case SbxBYREF | SbxDATE:
312 case SbxBYREF | SbxDOUBLE:
313 *p->pDouble = (double) n; break;
314 case SbxBYREF | SbxSALINT64:
315 *p->pnInt64 = (sal_Int64)n; break;
316 case SbxBYREF | SbxSALUINT64:
317 *p->puInt64 = (sal_uInt64)n; break;
318 case SbxBYREF | SbxCURRENCY:
319 double d;
320 if( n > SbxMAXCURR )
322 SbxBase::SetError( SbxERR_OVERFLOW ); d = SbxMAXCURR;
324 else if( n < SbxMINCURR )
326 SbxBase::SetError( SbxERR_OVERFLOW ); d = SbxMINCURR;
328 else
330 d = n;
332 *p->pnInt64 = ImpDoubleToCurrency( d ); break;
334 default:
335 SbxBase::SetError( SbxERR_CONVERSION );
339 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */