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