1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <vcl/errcode.hxx>
21 #include <basic/sberrors.hxx>
22 #include "sbxconv.hxx"
24 #include <rtl/math.hxx>
26 sal_Unicode
ImpGetChar( const SbxValues
* p
)
34 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
39 nRes
= p
->nChar
; break;
41 nRes
= static_cast<sal_Unicode
>(p
->nByte
);
45 if( p
->nInteger
< SbxMINCHAR
)
47 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMINCHAR
;
50 nRes
= static_cast<sal_Unicode
>(p
->nInteger
);
54 nRes
= static_cast<sal_Unicode
>(p
->nUShort
);
57 if( p
->nLong
> SbxMAXCHAR
)
59 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXCHAR
;
61 else if( p
->nLong
< SbxMINCHAR
)
63 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMINCHAR
;
66 nRes
= static_cast<sal_Unicode
>(p
->nLong
);
69 if( p
->nULong
> SbxMAXCHAR
)
71 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXCHAR
;
74 nRes
= static_cast<sal_Unicode
>(p
->nULong
);
79 sal_Int64 val
= p
->nInt64
;
81 if ( p
->eType
== SbxCURRENCY
)
82 val
= val
/ CURRENCY_FACTOR
;
84 if( val
> SbxMAXCHAR
)
86 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXCHAR
;
88 else if( p
->nInt64
< SbxMINCHAR
)
90 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMINCHAR
;
93 nRes
= static_cast<sal_Unicode
>(val
);
97 if( p
->uInt64
> SbxMAXCHAR
)
99 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXCHAR
;
102 nRes
= static_cast<sal_Unicode
>(p
->uInt64
);
105 if( p
->nSingle
> SbxMAXCHAR
)
107 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXCHAR
;
109 else if( p
->nSingle
< SbxMINCHAR
)
111 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMINCHAR
;
114 nRes
= static_cast<sal_Unicode
>(rtl::math::round( p
->nSingle
));
119 case SbxBYREF
| SbxDECIMAL
:
122 if( p
->eType
== SbxDECIMAL
)
126 p
->pDecimal
->getDouble( dVal
);
131 if( dVal
> SbxMAXCHAR
)
133 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXCHAR
;
135 else if( dVal
< SbxMINCHAR
)
137 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMINCHAR
;
140 nRes
= static_cast<sal_uInt8
>(rtl::math::round( dVal
));
143 case SbxBYREF
| SbxSTRING
:
150 if( ImpScan( *p
->pOUString
, d
, t
, nullptr, false ) != ERRCODE_NONE
)
152 else if( d
> SbxMAXCHAR
)
154 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMAXCHAR
;
156 else if( d
< SbxMINCHAR
)
158 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); nRes
= SbxMINCHAR
;
161 nRes
= static_cast<sal_Unicode
>(rtl::math::round( d
));
166 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
168 nRes
= pVal
->GetChar();
171 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
); nRes
= 0;
176 case SbxBYREF
| SbxCHAR
:
177 nRes
= *p
->pChar
; break;
178 // from here on will be tested
179 case SbxBYREF
| SbxBYTE
:
180 aTmp
.nByte
= *p
->pByte
; goto ref
;
181 case SbxBYREF
| SbxINTEGER
:
182 case SbxBYREF
| SbxBOOL
:
183 aTmp
.nInteger
= *p
->pInteger
; goto ref
;
184 case SbxBYREF
| SbxLONG
:
185 aTmp
.nLong
= *p
->pLong
; goto ref
;
186 case SbxBYREF
| SbxULONG
:
187 aTmp
.nULong
= *p
->pULong
; goto ref
;
188 case SbxBYREF
| SbxERROR
:
189 case SbxBYREF
| SbxUSHORT
:
190 aTmp
.nUShort
= *p
->pUShort
; goto ref
;
191 case SbxBYREF
| SbxSINGLE
:
192 aTmp
.nSingle
= *p
->pSingle
; goto ref
;
193 case SbxBYREF
| SbxDATE
:
194 case SbxBYREF
| SbxDOUBLE
:
195 aTmp
.nDouble
= *p
->pDouble
; goto ref
;
196 case SbxBYREF
| SbxCURRENCY
:
197 case SbxBYREF
| SbxSALINT64
:
198 aTmp
.nInt64
= *p
->pnInt64
; goto ref
;
199 case SbxBYREF
| SbxSALUINT64
:
200 aTmp
.uInt64
= *p
->puInt64
; goto ref
;
202 aTmp
.eType
= SbxDataType( p
->eType
& 0x0FFF );
203 p
= &aTmp
; goto start
;
206 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
); nRes
= 0;
211 void ImpPutChar( SbxValues
* p
, sal_Unicode n
)
221 p
->nInteger
= n
; break;
225 p
->nSingle
= n
; break;
228 p
->nDouble
= n
; break;
230 p
->nInt64
= n
* CURRENCY_FACTOR
; break;
232 p
->nInt64
= n
; break;
234 p
->uInt64
= n
; break;
235 case SbxBYREF
| SbxDECIMAL
:
236 ImpCreateDecimal( p
)->setChar( n
);
239 // from here on will be tested
241 aTmp
.pByte
= &p
->nByte
; goto direct
;
243 aTmp
.pULong
= &p
->nULong
; goto direct
;
246 aTmp
.pUShort
= &p
->nUShort
; goto direct
;
248 aTmp
.eType
= SbxDataType( p
->eType
| SbxBYREF
);
249 p
= &aTmp
; goto start
;
251 case SbxBYREF
| SbxSTRING
:
255 p
->pOUString
= new OUString( n
);
257 *p
->pOUString
= OUString( n
);
261 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
265 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
);
268 case SbxBYREF
| SbxCHAR
:
269 *p
->pChar
= n
; break;
270 case SbxBYREF
| SbxBYTE
:
271 *p
->pByte
= static_cast<sal_uInt8
>(n
); break;
272 case SbxBYREF
| SbxINTEGER
:
273 case SbxBYREF
| SbxBOOL
:
274 *p
->pInteger
= n
; break;
275 case SbxBYREF
| SbxERROR
:
276 case SbxBYREF
| SbxUSHORT
:
277 *p
->pUShort
= static_cast<sal_uInt16
>(n
); break;
278 case SbxBYREF
| SbxLONG
:
279 *p
->pLong
= static_cast<sal_Int32
>(n
); break;
280 case SbxBYREF
| SbxULONG
:
281 *p
->pULong
= static_cast<sal_uInt32
>(n
); break;
282 case SbxBYREF
| SbxSINGLE
:
283 *p
->pSingle
= static_cast<float>(n
); break;
284 case SbxBYREF
| SbxDATE
:
285 case SbxBYREF
| SbxDOUBLE
:
286 *p
->pDouble
= static_cast<double>(n
); break;
287 case SbxBYREF
| SbxCURRENCY
:
288 p
->nInt64
= n
* CURRENCY_FACTOR
; break;
289 case SbxBYREF
| SbxSALINT64
:
290 *p
->pnInt64
= n
; break;
291 case SbxBYREF
| SbxSALUINT64
:
292 *p
->puInt64
= n
; break;
295 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
300 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */