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