1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_basic.hxx" 30*cdf0e10cSrcweir #include <tools/errcode.hxx> 31*cdf0e10cSrcweir #include <basic/sbx.hxx> 32*cdf0e10cSrcweir #include "sbxconv.hxx" 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir float ImpGetSingle( const SbxValues* p ) 35*cdf0e10cSrcweir { 36*cdf0e10cSrcweir SbxValues aTmp; 37*cdf0e10cSrcweir float nRes; 38*cdf0e10cSrcweir start: 39*cdf0e10cSrcweir switch( +p->eType ) 40*cdf0e10cSrcweir { 41*cdf0e10cSrcweir case SbxNULL: 42*cdf0e10cSrcweir SbxBase::SetError( SbxERR_CONVERSION ); 43*cdf0e10cSrcweir case SbxEMPTY: 44*cdf0e10cSrcweir nRes = 0; break; 45*cdf0e10cSrcweir case SbxCHAR: 46*cdf0e10cSrcweir nRes = p->nChar; break; 47*cdf0e10cSrcweir case SbxBYTE: 48*cdf0e10cSrcweir nRes = p->nByte; break; 49*cdf0e10cSrcweir case SbxINTEGER: 50*cdf0e10cSrcweir case SbxBOOL: 51*cdf0e10cSrcweir nRes = p->nInteger; break; 52*cdf0e10cSrcweir case SbxERROR: 53*cdf0e10cSrcweir case SbxUSHORT: 54*cdf0e10cSrcweir nRes = p->nUShort; break; 55*cdf0e10cSrcweir case SbxLONG: 56*cdf0e10cSrcweir nRes = (float) p->nLong; break; 57*cdf0e10cSrcweir case SbxULONG: 58*cdf0e10cSrcweir nRes = (float) p->nULong; break; 59*cdf0e10cSrcweir case SbxSINGLE: 60*cdf0e10cSrcweir nRes = p->nSingle; break; 61*cdf0e10cSrcweir case SbxSALINT64: 62*cdf0e10cSrcweir nRes = (float) p->nInt64; break; 63*cdf0e10cSrcweir case SbxSALUINT64: 64*cdf0e10cSrcweir nRes = (float) ImpSalUInt64ToDouble( p->uInt64 ); break; 65*cdf0e10cSrcweir case SbxDECIMAL: 66*cdf0e10cSrcweir case SbxBYREF | SbxDECIMAL: 67*cdf0e10cSrcweir if( p->pDecimal ) 68*cdf0e10cSrcweir p->pDecimal->getSingle( nRes ); 69*cdf0e10cSrcweir else 70*cdf0e10cSrcweir nRes = 0.0; 71*cdf0e10cSrcweir break; 72*cdf0e10cSrcweir case SbxDATE: 73*cdf0e10cSrcweir case SbxDOUBLE: 74*cdf0e10cSrcweir case SbxLONG64: 75*cdf0e10cSrcweir case SbxULONG64: 76*cdf0e10cSrcweir case SbxCURRENCY: 77*cdf0e10cSrcweir { 78*cdf0e10cSrcweir double dVal; 79*cdf0e10cSrcweir if( p->eType == SbxCURRENCY ) 80*cdf0e10cSrcweir dVal = ImpCurrencyToDouble( p->nLong64 ); 81*cdf0e10cSrcweir else if( p->eType == SbxLONG64 ) 82*cdf0e10cSrcweir dVal = ImpINT64ToDouble( p->nLong64 ); 83*cdf0e10cSrcweir else if( p->eType == SbxULONG64 ) 84*cdf0e10cSrcweir dVal = ImpUINT64ToDouble( p->nULong64 ); 85*cdf0e10cSrcweir else 86*cdf0e10cSrcweir dVal = p->nDouble; 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir if( dVal > SbxMAXSNG ) 89*cdf0e10cSrcweir { 90*cdf0e10cSrcweir SbxBase::SetError( SbxERR_OVERFLOW ); 91*cdf0e10cSrcweir nRes = static_cast< float >(SbxMAXSNG); 92*cdf0e10cSrcweir } 93*cdf0e10cSrcweir else if( dVal < SbxMINSNG ) 94*cdf0e10cSrcweir { 95*cdf0e10cSrcweir SbxBase::SetError( SbxERR_OVERFLOW ); 96*cdf0e10cSrcweir nRes = static_cast< float >(SbxMINSNG); 97*cdf0e10cSrcweir } 98*cdf0e10cSrcweir else if( dVal > 0 && dVal < SbxMAXSNG2 ) 99*cdf0e10cSrcweir { 100*cdf0e10cSrcweir SbxBase::SetError( SbxERR_OVERFLOW ); 101*cdf0e10cSrcweir nRes = static_cast< float >(SbxMAXSNG2); 102*cdf0e10cSrcweir } 103*cdf0e10cSrcweir else if( dVal < 0 && dVal > SbxMINSNG2 ) 104*cdf0e10cSrcweir { 105*cdf0e10cSrcweir SbxBase::SetError( SbxERR_OVERFLOW ); 106*cdf0e10cSrcweir nRes = static_cast< float >(SbxMINSNG2); 107*cdf0e10cSrcweir } 108*cdf0e10cSrcweir else 109*cdf0e10cSrcweir nRes = (float) dVal; 110*cdf0e10cSrcweir break; 111*cdf0e10cSrcweir } 112*cdf0e10cSrcweir case SbxBYREF | SbxSTRING: 113*cdf0e10cSrcweir case SbxSTRING: 114*cdf0e10cSrcweir case SbxLPSTR: 115*cdf0e10cSrcweir if( !p->pOUString ) 116*cdf0e10cSrcweir nRes = 0; 117*cdf0e10cSrcweir else 118*cdf0e10cSrcweir { 119*cdf0e10cSrcweir double d; 120*cdf0e10cSrcweir SbxDataType t; 121*cdf0e10cSrcweir if( ImpScan( *p->pOUString, d, t, NULL ) != SbxERR_OK ) 122*cdf0e10cSrcweir nRes = 0; 123*cdf0e10cSrcweir else if( d > SbxMAXSNG ) 124*cdf0e10cSrcweir { 125*cdf0e10cSrcweir SbxBase::SetError( SbxERR_OVERFLOW ); 126*cdf0e10cSrcweir nRes = static_cast< float >(SbxMAXSNG); 127*cdf0e10cSrcweir } 128*cdf0e10cSrcweir else if( d < SbxMINSNG ) 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir SbxBase::SetError( SbxERR_OVERFLOW ); 131*cdf0e10cSrcweir nRes = static_cast< float >(SbxMINSNG); 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir else 134*cdf0e10cSrcweir nRes = (float) d; 135*cdf0e10cSrcweir } 136*cdf0e10cSrcweir break; 137*cdf0e10cSrcweir case SbxOBJECT: 138*cdf0e10cSrcweir { 139*cdf0e10cSrcweir SbxValue* pVal = PTR_CAST(SbxValue,p->pObj); 140*cdf0e10cSrcweir if( pVal ) 141*cdf0e10cSrcweir nRes = pVal->GetSingle(); 142*cdf0e10cSrcweir else 143*cdf0e10cSrcweir { 144*cdf0e10cSrcweir SbxBase::SetError( SbxERR_NO_OBJECT ); nRes = 0; 145*cdf0e10cSrcweir } 146*cdf0e10cSrcweir break; 147*cdf0e10cSrcweir } 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir case SbxBYREF | SbxCHAR: 150*cdf0e10cSrcweir nRes = *p->pChar; break; 151*cdf0e10cSrcweir case SbxBYREF | SbxBYTE: 152*cdf0e10cSrcweir nRes = *p->pByte; break; 153*cdf0e10cSrcweir case SbxBYREF | SbxINTEGER: 154*cdf0e10cSrcweir case SbxBYREF | SbxBOOL: 155*cdf0e10cSrcweir nRes = *p->pInteger; break; 156*cdf0e10cSrcweir case SbxBYREF | SbxLONG: 157*cdf0e10cSrcweir nRes = (float) *p->pLong; break; 158*cdf0e10cSrcweir case SbxBYREF | SbxULONG: 159*cdf0e10cSrcweir nRes = (float) *p->pULong; break; 160*cdf0e10cSrcweir case SbxBYREF | SbxERROR: 161*cdf0e10cSrcweir case SbxBYREF | SbxUSHORT: 162*cdf0e10cSrcweir nRes = *p->pUShort; break; 163*cdf0e10cSrcweir case SbxBYREF | SbxSINGLE: 164*cdf0e10cSrcweir nRes = *p->pSingle; break; 165*cdf0e10cSrcweir // ab hier muss getestet werden 166*cdf0e10cSrcweir case SbxBYREF | SbxDATE: 167*cdf0e10cSrcweir case SbxBYREF | SbxDOUBLE: 168*cdf0e10cSrcweir aTmp.nDouble = *p->pDouble; goto ref; 169*cdf0e10cSrcweir case SbxBYREF | SbxULONG64: 170*cdf0e10cSrcweir aTmp.nULong64 = *p->pULong64; goto ref; 171*cdf0e10cSrcweir case SbxBYREF | SbxLONG64: 172*cdf0e10cSrcweir case SbxBYREF | SbxSALINT64: 173*cdf0e10cSrcweir nRes = (float) *p->pnInt64; break; 174*cdf0e10cSrcweir case SbxBYREF | SbxSALUINT64: 175*cdf0e10cSrcweir nRes = (float) ImpSalUInt64ToDouble( *p->puInt64 ); break; 176*cdf0e10cSrcweir case SbxBYREF | SbxCURRENCY: 177*cdf0e10cSrcweir aTmp.nLong64 = *p->pLong64; goto ref; 178*cdf0e10cSrcweir ref: 179*cdf0e10cSrcweir aTmp.eType = SbxDataType( p->eType & 0x0FFF ); 180*cdf0e10cSrcweir p = &aTmp; goto start; 181*cdf0e10cSrcweir 182*cdf0e10cSrcweir default: 183*cdf0e10cSrcweir SbxBase::SetError( SbxERR_CONVERSION ); nRes = 0; 184*cdf0e10cSrcweir } 185*cdf0e10cSrcweir return nRes; 186*cdf0e10cSrcweir } 187*cdf0e10cSrcweir 188*cdf0e10cSrcweir void ImpPutSingle( SbxValues* p, float n ) 189*cdf0e10cSrcweir { 190*cdf0e10cSrcweir SbxValues aTmp; 191*cdf0e10cSrcweir start: 192*cdf0e10cSrcweir switch( +p->eType ) 193*cdf0e10cSrcweir { 194*cdf0e10cSrcweir case SbxCHAR: 195*cdf0e10cSrcweir aTmp.pChar = &p->nChar; goto direct; 196*cdf0e10cSrcweir case SbxBYTE: 197*cdf0e10cSrcweir aTmp.pByte = &p->nByte; goto direct; 198*cdf0e10cSrcweir case SbxINTEGER: 199*cdf0e10cSrcweir case SbxBOOL: 200*cdf0e10cSrcweir aTmp.pInteger = &p->nInteger; goto direct; 201*cdf0e10cSrcweir case SbxLONG: 202*cdf0e10cSrcweir aTmp.pLong = &p->nLong; goto direct; 203*cdf0e10cSrcweir case SbxULONG: 204*cdf0e10cSrcweir aTmp.pULong = &p->nULong; goto direct; 205*cdf0e10cSrcweir case SbxERROR: 206*cdf0e10cSrcweir case SbxUSHORT: 207*cdf0e10cSrcweir aTmp.pUShort = &p->nUShort; goto direct; 208*cdf0e10cSrcweir case SbxULONG64: 209*cdf0e10cSrcweir aTmp.pULong64 = &p->nULong64; goto direct; 210*cdf0e10cSrcweir case SbxLONG64: 211*cdf0e10cSrcweir case SbxCURRENCY: 212*cdf0e10cSrcweir aTmp.pLong64 = &p->nLong64; goto direct; 213*cdf0e10cSrcweir case SbxSALINT64: 214*cdf0e10cSrcweir aTmp.pnInt64 = &p->nInt64; goto direct; 215*cdf0e10cSrcweir case SbxSALUINT64: 216*cdf0e10cSrcweir aTmp.puInt64 = &p->uInt64; goto direct; 217*cdf0e10cSrcweir case SbxDECIMAL: 218*cdf0e10cSrcweir case SbxBYREF | SbxDECIMAL: 219*cdf0e10cSrcweir { 220*cdf0e10cSrcweir SbxDecimal* pDec = ImpCreateDecimal( p ); 221*cdf0e10cSrcweir if( !pDec->setSingle( n ) ) 222*cdf0e10cSrcweir SbxBase::SetError( SbxERR_OVERFLOW ); 223*cdf0e10cSrcweir break; 224*cdf0e10cSrcweir } 225*cdf0e10cSrcweir direct: 226*cdf0e10cSrcweir aTmp.eType = SbxDataType( p->eType | SbxBYREF ); 227*cdf0e10cSrcweir p = &aTmp; goto start; 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir // keine Tests ab hier 230*cdf0e10cSrcweir case SbxSINGLE: 231*cdf0e10cSrcweir p->nSingle = n; break; 232*cdf0e10cSrcweir case SbxDATE: 233*cdf0e10cSrcweir case SbxDOUBLE: 234*cdf0e10cSrcweir p->nDouble = n; break; 235*cdf0e10cSrcweir 236*cdf0e10cSrcweir case SbxBYREF | SbxSTRING: 237*cdf0e10cSrcweir case SbxSTRING: 238*cdf0e10cSrcweir case SbxLPSTR: 239*cdf0e10cSrcweir { 240*cdf0e10cSrcweir if( !p->pOUString ) 241*cdf0e10cSrcweir p->pOUString = new ::rtl::OUString; 242*cdf0e10cSrcweir ImpCvtNum( (double) n, 6, *p->pOUString ); 243*cdf0e10cSrcweir break; 244*cdf0e10cSrcweir } 245*cdf0e10cSrcweir case SbxOBJECT: 246*cdf0e10cSrcweir { 247*cdf0e10cSrcweir SbxValue* pVal = PTR_CAST(SbxValue,p->pObj); 248*cdf0e10cSrcweir if( pVal ) 249*cdf0e10cSrcweir pVal->PutSingle( n ); 250*cdf0e10cSrcweir else 251*cdf0e10cSrcweir SbxBase::SetError( SbxERR_NO_OBJECT ); 252*cdf0e10cSrcweir break; 253*cdf0e10cSrcweir } 254*cdf0e10cSrcweir case SbxBYREF | SbxCHAR: 255*cdf0e10cSrcweir if( n > SbxMAXCHAR ) 256*cdf0e10cSrcweir { 257*cdf0e10cSrcweir SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXCHAR; 258*cdf0e10cSrcweir } 259*cdf0e10cSrcweir else if( n < SbxMINCHAR ) 260*cdf0e10cSrcweir { 261*cdf0e10cSrcweir SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMINCHAR; 262*cdf0e10cSrcweir } 263*cdf0e10cSrcweir *p->pChar = (xub_Unicode) n; break; 264*cdf0e10cSrcweir case SbxBYREF | SbxBYTE: 265*cdf0e10cSrcweir if( n > SbxMAXBYTE ) 266*cdf0e10cSrcweir { 267*cdf0e10cSrcweir SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXBYTE; 268*cdf0e10cSrcweir } 269*cdf0e10cSrcweir else if( n < 0 ) 270*cdf0e10cSrcweir { 271*cdf0e10cSrcweir SbxBase::SetError( SbxERR_OVERFLOW ); n = 0; 272*cdf0e10cSrcweir } 273*cdf0e10cSrcweir *p->pByte = (sal_uInt8) n; break; 274*cdf0e10cSrcweir case SbxBYREF | SbxINTEGER: 275*cdf0e10cSrcweir case SbxBYREF | SbxBOOL: 276*cdf0e10cSrcweir if( n > SbxMAXINT ) 277*cdf0e10cSrcweir { 278*cdf0e10cSrcweir SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXINT; 279*cdf0e10cSrcweir } 280*cdf0e10cSrcweir else if( n < SbxMININT ) 281*cdf0e10cSrcweir { 282*cdf0e10cSrcweir SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMININT; 283*cdf0e10cSrcweir } 284*cdf0e10cSrcweir *p->pInteger = (sal_Int16) n; break; 285*cdf0e10cSrcweir case SbxBYREF | SbxERROR: 286*cdf0e10cSrcweir case SbxBYREF | SbxUSHORT: 287*cdf0e10cSrcweir if( n > SbxMAXUINT ) 288*cdf0e10cSrcweir { 289*cdf0e10cSrcweir SbxBase::SetError( SbxERR_OVERFLOW ); n = SbxMAXUINT; 290*cdf0e10cSrcweir } 291*cdf0e10cSrcweir else if( n < 0 ) 292*cdf0e10cSrcweir { 293*cdf0e10cSrcweir SbxBase::SetError( SbxERR_OVERFLOW ); n = 0; 294*cdf0e10cSrcweir } 295*cdf0e10cSrcweir *p->pUShort = (sal_uInt16) n; break; 296*cdf0e10cSrcweir case SbxBYREF | SbxLONG: 297*cdf0e10cSrcweir { 298*cdf0e10cSrcweir sal_Int32 i; 299*cdf0e10cSrcweir if( n > SbxMAXLNG ) 300*cdf0e10cSrcweir { 301*cdf0e10cSrcweir SbxBase::SetError( SbxERR_OVERFLOW ); i = SbxMAXLNG; 302*cdf0e10cSrcweir } 303*cdf0e10cSrcweir else if( n < SbxMINLNG ) 304*cdf0e10cSrcweir { 305*cdf0e10cSrcweir SbxBase::SetError( SbxERR_OVERFLOW ); i = SbxMINLNG; 306*cdf0e10cSrcweir } 307*cdf0e10cSrcweir else 308*cdf0e10cSrcweir { 309*cdf0e10cSrcweir i = sal::static_int_cast< sal_Int32 >(n); 310*cdf0e10cSrcweir } 311*cdf0e10cSrcweir *p->pLong = i; break; 312*cdf0e10cSrcweir } 313*cdf0e10cSrcweir case SbxBYREF | SbxULONG: 314*cdf0e10cSrcweir { 315*cdf0e10cSrcweir sal_uInt32 i; 316*cdf0e10cSrcweir if( n > SbxMAXULNG ) 317*cdf0e10cSrcweir { 318*cdf0e10cSrcweir SbxBase::SetError( SbxERR_OVERFLOW ); i = SbxMAXULNG; 319*cdf0e10cSrcweir } 320*cdf0e10cSrcweir else if( n < 0 ) 321*cdf0e10cSrcweir { 322*cdf0e10cSrcweir SbxBase::SetError( SbxERR_OVERFLOW ); i = 0; 323*cdf0e10cSrcweir } 324*cdf0e10cSrcweir else 325*cdf0e10cSrcweir { 326*cdf0e10cSrcweir i = sal::static_int_cast< sal_uInt32 >(n); 327*cdf0e10cSrcweir } 328*cdf0e10cSrcweir *p->pULong = i; break; 329*cdf0e10cSrcweir } 330*cdf0e10cSrcweir case SbxBYREF | SbxSINGLE: 331*cdf0e10cSrcweir *p->pSingle = n; break; 332*cdf0e10cSrcweir case SbxBYREF | SbxDATE: 333*cdf0e10cSrcweir case SbxBYREF | SbxDOUBLE: 334*cdf0e10cSrcweir *p->pDouble = (double) n; break; 335*cdf0e10cSrcweir case SbxBYREF | SbxSALINT64: 336*cdf0e10cSrcweir *p->pnInt64 = ImpDoubleToSalInt64( (double) n ); break; 337*cdf0e10cSrcweir case SbxBYREF | SbxSALUINT64: 338*cdf0e10cSrcweir *p->puInt64 = ImpDoubleToSalUInt64( (double) n ); break; 339*cdf0e10cSrcweir case SbxBYREF | SbxCURRENCY: 340*cdf0e10cSrcweir double d; 341*cdf0e10cSrcweir if( n > SbxMAXCURR ) 342*cdf0e10cSrcweir { 343*cdf0e10cSrcweir SbxBase::SetError( SbxERR_OVERFLOW ); d = SbxMAXCURR; 344*cdf0e10cSrcweir } 345*cdf0e10cSrcweir else if( n < SbxMINCURR ) 346*cdf0e10cSrcweir { 347*cdf0e10cSrcweir SbxBase::SetError( SbxERR_OVERFLOW ); d = SbxMINCURR; 348*cdf0e10cSrcweir } 349*cdf0e10cSrcweir else 350*cdf0e10cSrcweir { 351*cdf0e10cSrcweir d = n; 352*cdf0e10cSrcweir } 353*cdf0e10cSrcweir *p->pLong64 = ImpDoubleToCurrency( n ); break; 354*cdf0e10cSrcweir 355*cdf0e10cSrcweir default: 356*cdf0e10cSrcweir SbxBase::SetError( SbxERR_CONVERSION ); 357*cdf0e10cSrcweir } 358*cdf0e10cSrcweir } 359*cdf0e10cSrcweir 360