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