Update ooo320-m1
[ooovba.git] / basic / source / sbx / sbxchar.cxx
blob3e8f9e9e4564bd998480fec52508f6c6cc20821a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: sbxchar.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_basic.hxx"
33 #include <tools/errcode.hxx>
34 #include <basic/sbx.hxx>
35 #include "sbxconv.hxx"
37 // AB 29.10.99 Unicode
38 #ifndef _USE_NO_NAMESPACE
39 using namespace rtl;
40 #endif
42 xub_Unicode ImpGetChar( const SbxValues* p )
44 SbxValues aTmp;
45 xub_Unicode nRes;
46 start:
47 switch( +p->eType )
49 case SbxNULL:
50 SbxBase::SetError( SbxERR_CONVERSION );
51 case SbxEMPTY:
52 nRes = 0; break;
53 case SbxCHAR:
54 nRes = p->nChar; break;
55 case SbxBYTE:
56 nRes = (xub_Unicode) p->nByte;
57 break;
58 case SbxINTEGER:
59 case SbxBOOL:
60 if( p->nInteger < SbxMINCHAR )
62 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMINCHAR;
64 else
65 nRes = (xub_Unicode) p->nInteger;
66 break;
67 case SbxERROR:
68 case SbxUSHORT:
69 nRes = (xub_Unicode) p->nUShort;
70 break;
71 case SbxLONG:
72 if( p->nLong > SbxMAXCHAR )
74 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXCHAR;
76 else if( p->nLong < SbxMINCHAR )
78 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMINCHAR;
80 else
81 nRes = (xub_Unicode) p->nLong;
82 break;
83 case SbxULONG:
84 if( p->nULong > SbxMAXCHAR )
86 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXCHAR;
88 else
89 nRes = (xub_Unicode) p->nULong;
90 break;
91 case SbxSALINT64:
92 if( p->nInt64 > SbxMAXCHAR )
94 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXCHAR;
96 else if( p->nInt64 < SbxMINCHAR )
98 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMINCHAR;
100 else
101 nRes = (xub_Unicode) p->nInt64;
102 break;
103 case SbxSALUINT64:
104 if( p->uInt64 > SbxMAXCHAR )
106 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXCHAR;
108 else
109 nRes = (xub_Unicode) p->uInt64;
110 break;
111 case SbxSINGLE:
112 if( p->nSingle > SbxMAXCHAR )
114 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXCHAR;
116 else if( p->nSingle < SbxMINCHAR )
118 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMINCHAR;
120 else
121 nRes = (xub_Unicode) ImpRound( p->nSingle );
122 break;
123 case SbxDATE:
124 case SbxDOUBLE:
125 case SbxLONG64:
126 case SbxULONG64:
127 case SbxCURRENCY:
128 case SbxDECIMAL:
129 case SbxBYREF | SbxDECIMAL:
131 double dVal;
132 if( p->eType == SbxCURRENCY )
133 dVal = ImpCurrencyToDouble( p->nLong64 );
134 else if( p->eType == SbxLONG64 )
135 dVal = ImpINT64ToDouble( p->nLong64 );
136 else if( p->eType == SbxULONG64 )
137 dVal = ImpUINT64ToDouble( p->nULong64 );
138 else if( p->eType == SbxDECIMAL )
140 dVal = 0.0;
141 if( p->pDecimal )
142 p->pDecimal->getDouble( dVal );
144 else
145 dVal = p->nDouble;
147 if( dVal > SbxMAXCHAR )
149 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXCHAR;
151 else if( dVal < SbxMINCHAR )
153 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMINCHAR;
155 else
156 nRes = (BYTE) ImpRound( dVal );
157 break;
159 case SbxBYREF | SbxSTRING:
160 case SbxSTRING:
161 case SbxLPSTR:
162 if( !p->pString )
163 nRes = 0;
164 else
166 double d;
167 SbxDataType t;
168 if( ImpScan( *p->pString, d, t, NULL ) != SbxERR_OK )
169 nRes = 0;
170 else if( d > SbxMAXCHAR )
172 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMAXCHAR;
174 else if( d < SbxMINCHAR )
176 SbxBase::SetError( SbxERR_OVERFLOW ); nRes = SbxMINCHAR;
178 else
179 nRes = (xub_Unicode) ImpRound( d );
181 break;
182 case SbxOBJECT:
184 SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
185 if( pVal )
186 nRes = pVal->GetChar();
187 else
189 SbxBase::SetError( SbxERR_NO_OBJECT ); nRes = 0;
191 break;
194 case SbxBYREF | SbxCHAR:
195 nRes = *p->pChar; break;
196 // ab hier wird getestet
197 case SbxBYREF | SbxBYTE:
198 aTmp.nByte = *p->pByte; goto ref;
199 case SbxBYREF | SbxINTEGER:
200 case SbxBYREF | SbxBOOL:
201 aTmp.nInteger = *p->pInteger; goto ref;
202 case SbxBYREF | SbxLONG:
203 aTmp.nLong = *p->pLong; goto ref;
204 case SbxBYREF | SbxULONG:
205 aTmp.nULong = *p->pULong; goto ref;
206 case SbxBYREF | SbxERROR:
207 case SbxBYREF | SbxUSHORT:
208 aTmp.nUShort = *p->pUShort; goto ref;
209 case SbxBYREF | SbxSINGLE:
210 aTmp.nSingle = *p->pSingle; goto ref;
211 case SbxBYREF | SbxDATE:
212 case SbxBYREF | SbxDOUBLE:
213 aTmp.nDouble = *p->pDouble; goto ref;
214 case SbxBYREF | SbxULONG64:
215 aTmp.nULong64 = *p->pULong64; goto ref;
216 case SbxBYREF | SbxLONG64:
217 case SbxBYREF | SbxCURRENCY:
218 aTmp.nLong64 = *p->pLong64; goto ref;
219 case SbxBYREF | SbxSALINT64:
220 aTmp.nInt64 = *p->pnInt64; goto ref;
221 case SbxBYREF | SbxSALUINT64:
222 aTmp.uInt64 = *p->puInt64; goto ref;
223 ref:
224 aTmp.eType = SbxDataType( p->eType & 0x0FFF );
225 p = &aTmp; goto start;
227 default:
228 SbxBase::SetError( SbxERR_CONVERSION ); nRes = 0;
230 return nRes;
233 void ImpPutChar( SbxValues* p, xub_Unicode n )
235 SbxValues aTmp;
236 start:
237 switch( +p->eType )
239 case SbxCHAR:
240 p->nChar = n; break;
241 case SbxINTEGER:
242 case SbxBOOL:
243 p->nInteger = n; break;
244 case SbxLONG:
245 p->nLong = n; break;
246 case SbxSINGLE:
247 p->nSingle = n; break;
248 case SbxDATE:
249 case SbxDOUBLE:
250 p->nDouble = n; break;
251 case SbxSALINT64:
252 p->nInt64 = n; break;
253 case SbxSALUINT64:
254 p->uInt64 = n; break;
255 case SbxULONG64:
256 p->nULong64 = ImpDoubleToUINT64( (double)n ); break;
257 case SbxLONG64:
258 p->nLong64 = ImpDoubleToINT64( (double)n ); break;
259 case SbxCURRENCY:
260 p->nLong64 = ImpDoubleToCurrency( (double)n ); break;
261 case SbxBYREF | SbxDECIMAL:
262 ImpCreateDecimal( p )->setChar( n );
263 break;
265 // ab hier wird getestet
266 case SbxBYTE:
267 aTmp.pByte = &p->nByte; goto direct;
268 case SbxULONG:
269 aTmp.pULong = &p->nULong; goto direct;
270 case SbxERROR:
271 case SbxUSHORT:
272 aTmp.pUShort = &p->nUShort; goto direct;
273 direct:
274 aTmp.eType = SbxDataType( p->eType | SbxBYREF );
275 p = &aTmp; goto start;
277 case SbxBYREF | SbxSTRING:
278 case SbxSTRING:
279 case SbxLPSTR:
280 if( !p->pString )
281 p->pString = new XubString;
282 *p->pString = n;
283 break;
284 case SbxOBJECT:
286 SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
287 if( pVal )
288 pVal->PutChar( n );
289 else
290 SbxBase::SetError( SbxERR_NO_OBJECT );
291 break;
293 case SbxBYREF | SbxCHAR:
294 *p->pChar = n; break;
295 case SbxBYREF | SbxBYTE:
296 *p->pByte = (BYTE) n; break;
297 case SbxBYREF | SbxINTEGER:
298 case SbxBYREF | SbxBOOL:
299 *p->pInteger = n; break;
300 case SbxBYREF | SbxERROR:
301 case SbxBYREF | SbxUSHORT:
302 *p->pUShort = (UINT16) n; break;
303 case SbxBYREF | SbxLONG:
304 *p->pLong = (INT32) n; break;
305 case SbxBYREF | SbxULONG:
306 *p->pULong = (UINT32) n; break;
307 case SbxBYREF | SbxSINGLE:
308 *p->pSingle = (float) n; break;
309 case SbxBYREF | SbxDATE:
310 case SbxBYREF | SbxDOUBLE:
311 *p->pDouble = (double) 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;
323 default:
324 SbxBase::SetError( SbxERR_CONVERSION );