| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 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/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 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 . |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <rtl/ustrbuf.hxx> |
| 21 |
|
| 22 |
#include <basic/sberrors.hxx> |
| 23 |
#include <basic/sbxvar.hxx> |
| 24 |
#include <svl/numformat.hxx> |
| 25 |
#include <vcl/svapp.hxx> |
| 26 |
#include <vcl/settings.hxx> |
| 27 |
#include <sbintern.hxx> |
| 28 |
#include <runtime.hxx> |
| 29 |
#include "sbxconv.hxx" |
| 30 |
|
| 31 |
|
| 32 |
static OUString ImpCurrencyToString( sal_Int64 rVal ) |
| 33 |
{ |
| 34 |
sal_Unicode cDecimalSep, cThousandSepDummy, cDecimalSepAltDummy; |
| 35 |
ImpGetIntntlSep(cDecimalSep, cThousandSepDummy, cDecimalSepAltDummy); |
| 36 |
|
| 37 |
auto strNum = OUString::number(rVal); |
| 38 |
std::u16string_view aAbsStr(strNum); |
| 39 |
OUStringBuffer aBuf(22); |
| 40 |
if (rVal < 0) |
| 41 |
{ |
| 42 |
aBuf.append('-'); |
| 43 |
assert(aAbsStr[0] == '-'); |
| 44 |
aAbsStr = aAbsStr.substr(1); // skip the minus |
| 45 |
} |
| 46 |
size_t hasFractDigits = std::min(aAbsStr.length(), size_t(4)); |
| 47 |
size_t hasWholeDigits = aAbsStr.length() - hasFractDigits; |
| 48 |
|
| 49 |
if (hasWholeDigits > 0) |
| 50 |
aBuf.append(aAbsStr.substr(0, hasWholeDigits)); |
| 51 |
else |
| 52 |
aBuf.append('0'); |
| 53 |
aBuf.append(cDecimalSep); |
| 54 |
// Handle leading 0's to right of decimal separator |
| 55 |
// Note: in VBA the stringification is a little more complex |
| 56 |
// but more natural as only the necessary digits |
| 57 |
// to the right of the decimal places are displayed |
| 58 |
// It would be great to conditionally be able to display like that too |
| 59 |
|
| 60 |
// Val OOo (Cur) VBA (Cur) |
| 61 |
// --- --------- --------- |
| 62 |
// 0 0.0000 0 |
| 63 |
// 0.1 0.1000 0.1 |
| 64 |
for (size_t i = 4; i > hasFractDigits; --i) |
| 65 |
aBuf.append('0'); |
| 66 |
aBuf.append(aAbsStr.substr(aAbsStr.length() - hasFractDigits, hasFractDigits)); |
| 67 |
|
| 68 |
return aBuf.makeStringAndClear(); |
| 69 |
} |
| 70 |
|
| 71 |
static sal_Int64 ImpStringToCurrency(const rtl::OUString& rStr) |
| 72 |
{ |
| 73 |
LanguageType eLangType = Application::GetSettings().GetLanguageTag().getLanguageType(); |
| 74 |
std::shared_ptr<SvNumberFormatter> pFormatter; |
| 75 |
if (GetSbData()->pInst) |
| 76 |
{ |
| 77 |
pFormatter = GetSbData()->pInst->GetNumberFormatter(); |
| 78 |
} |
| 79 |
else |
| 80 |
{ |
| 81 |
sal_uInt32 n; // Dummy |
| 82 |
pFormatter = SbiInstance::PrepareNumberFormatter(/*date index*/ n, /*time index*/ n, |
| 83 |
/*date time index*/ n); |
| 84 |
} |
| 85 |
|
| 86 |
// Passing a locale index switches IsNumberFormat() to use that locale, |
| 87 |
// in case the formatter wasn't default-created with it. |
| 88 |
sal_uInt32 nIndex = pFormatter->GetStandardIndex(eLangType); |
| 89 |
|
| 90 |
double fResult = 0.0; |
| 91 |
bool bSuccess = pFormatter->IsNumberFormat(rStr, nIndex, fResult); |
| 92 |
if (bSuccess) |
| 93 |
{ |
| 94 |
SvNumFormatType nType = pFormatter->GetType(nIndex); |
| 95 |
if (!(nType & (SvNumFormatType::CURRENCY | SvNumFormatType::NUMBER))) |
| 96 |
{ |
| 97 |
bSuccess = false; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
if (!bSuccess) |
| 102 |
{ |
| 103 |
SbxBase::SetError(ERRCODE_BASIC_CONVERSION); |
| 104 |
} |
| 105 |
|
| 106 |
return CurFrom(fResult); |
| 107 |
} |
| 108 |
|
| 109 |
sal_Int64 ImpGetCurrency( const SbxValues* p ) |
| 110 |
{ |
| 111 |
switch( +p->eType ) |
| 112 |
{ |
| 113 |
case SbxERROR: |
| 114 |
case SbxNULL: |
| 115 |
SbxBase::SetError( ERRCODE_BASIC_CONVERSION ); |
| 116 |
return 0; |
| 117 |
case SbxEMPTY: |
| 118 |
return 0; |
| 119 |
case SbxCURRENCY: |
| 120 |
return p->nInt64; |
| 121 |
case SbxBYTE: |
| 122 |
return CurFrom(p->nByte); |
| 123 |
case SbxCHAR: |
| 124 |
return CurFrom(p->nChar); |
| 125 |
case SbxBOOL: |
| 126 |
case SbxINTEGER: |
| 127 |
return CurFrom(p->nInteger); |
| 128 |
case SbxUSHORT: |
| 129 |
return CurFrom(p->nUShort); |
| 130 |
case SbxLONG: |
| 131 |
return CurFrom(p->nLong); |
| 132 |
case SbxULONG: |
| 133 |
return CurFrom(p->nULong); |
| 134 |
case SbxSALINT64: |
| 135 |
return CurFrom(p->nInt64); |
| 136 |
case SbxSALUINT64: |
| 137 |
return CurFrom(p->uInt64); |
| 138 |
case SbxSINGLE: |
| 139 |
return CurFrom(p->nSingle); |
| 140 |
|
| 141 |
case SbxDATE: |
| 142 |
case SbxDOUBLE: |
| 143 |
return CurFrom(p->nDouble); |
| 144 |
|
| 145 |
case SbxDECIMAL: |
| 146 |
case SbxBYREF | SbxDECIMAL: |
| 147 |
if( p->pDecimal ) |
| 148 |
{ |
| 149 |
double d = 0.0; |
| 150 |
p->pDecimal->getDouble( d ); |
| 151 |
return CurFrom(d); |
| 152 |
} |
| 153 |
return 0; |
| 154 |
|
| 155 |
case SbxBYREF | SbxSTRING: |
| 156 |
case SbxSTRING: |
| 157 |
case SbxLPSTR: |
| 158 |
if (p->pOUString) |
| 159 |
return ImpStringToCurrency( *p->pOUString ); |
| 160 |
return 0; |
| 161 |
case SbxOBJECT: |
| 162 |
if (SbxValue* pVal = dynamic_cast<SbxValue*>(p->pObj)) |
| 163 |
return pVal->GetCurrency(); |
| 164 |
SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT ); |
| 165 |
return 0; |
| 166 |
|
| 167 |
case SbxBYREF | SbxCHAR: |
| 168 |
return CurFrom(*p->pChar); |
| 169 |
case SbxBYREF | SbxBYTE: |
| 170 |
return CurFrom(*p->pByte); |
| 171 |
case SbxBYREF | SbxBOOL: |
| 172 |
case SbxBYREF | SbxINTEGER: |
| 173 |
return CurFrom(*p->pInteger); |
| 174 |
case SbxBYREF | SbxERROR: |
| 175 |
case SbxBYREF | SbxUSHORT: |
| 176 |
return CurFrom(*p->pUShort); |
| 177 |
|
| 178 |
case SbxBYREF | SbxLONG: |
| 179 |
return CurFrom(*p->pLong); |
| 180 |
case SbxBYREF | SbxULONG: |
| 181 |
return CurFrom(*p->pULong); |
| 182 |
case SbxBYREF | SbxSINGLE: |
| 183 |
return CurFrom(*p->pSingle); |
| 184 |
case SbxBYREF | SbxDATE: |
| 185 |
case SbxBYREF | SbxDOUBLE: |
| 186 |
return CurFrom(*p->pDouble); |
| 187 |
case SbxBYREF | SbxCURRENCY: |
| 188 |
return *p->pnInt64; |
| 189 |
case SbxBYREF | SbxSALINT64: |
| 190 |
return CurFrom(*p->pnInt64); |
| 191 |
case SbxBYREF | SbxSALUINT64: |
| 192 |
return CurFrom(*p->puInt64); |
| 193 |
|
| 194 |
default: |
| 195 |
SbxBase::SetError( ERRCODE_BASIC_CONVERSION ); |
| 196 |
return 0; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
void ImpPutCurrency( SbxValues* p, const sal_Int64 r ) |
| 201 |
{ |
| 202 |
switch( +p->eType ) |
| 203 |
{ |
| 204 |
case SbxDATE: |
| 205 |
case SbxDOUBLE: |
| 206 |
p->nDouble = CurTo<double>( r ); break; |
| 207 |
case SbxSALUINT64: |
| 208 |
p->uInt64 = CurTo<sal_uInt64>(r); break; |
| 209 |
case SbxSALINT64: |
| 210 |
p->nInt64 = CurTo<sal_Int64>(r); break; |
| 211 |
|
| 212 |
case SbxCURRENCY: |
| 213 |
p->nInt64 = r; break; |
| 214 |
|
| 215 |
case SbxDECIMAL: |
| 216 |
case SbxBYREF | SbxDECIMAL: |
| 217 |
if (!ImpCreateDecimal(p)->setDouble(CurTo<double>(r))) |
| 218 |
SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); |
| 219 |
break; |
| 220 |
case SbxBYREF | SbxSTRING: |
| 221 |
case SbxSTRING: |
| 222 |
case SbxLPSTR: |
| 223 |
if( !p->pOUString ) |
| 224 |
p->pOUString = new OUString; |
| 225 |
|
| 226 |
*p->pOUString = ImpCurrencyToString( r ); |
| 227 |
break; |
| 228 |
case SbxOBJECT: |
| 229 |
if (SbxValue* pVal = dynamic_cast<SbxValue*>(p->pObj)) |
| 230 |
pVal->PutCurrency( r ); |
| 231 |
else |
| 232 |
SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT ); |
| 233 |
break; |
| 234 |
case SbxCHAR: |
| 235 |
p->nChar = CurTo<sal_Unicode>(r); break; |
| 236 |
case SbxBYREF | SbxCHAR: |
| 237 |
*p->pChar = CurTo<sal_Unicode>(r); break; |
| 238 |
case SbxBYTE: |
| 239 |
p->nByte = CurTo<sal_uInt8>(r); break; |
| 240 |
case SbxBYREF | SbxBYTE: |
| 241 |
*p->pByte = CurTo<sal_uInt8>(r); break; |
| 242 |
case SbxINTEGER: |
| 243 |
case SbxBOOL: |
| 244 |
p->nInteger = CurTo<sal_Int16>(r); break; |
| 245 |
case SbxBYREF | SbxINTEGER: |
| 246 |
case SbxBYREF | SbxBOOL: |
| 247 |
*p->pInteger = CurTo<sal_Int16>(r); break; |
| 248 |
case SbxERROR: |
| 249 |
case SbxUSHORT: |
| 250 |
p->nUShort = CurTo<sal_uInt16>(r); break; |
| 251 |
case SbxBYREF | SbxERROR: |
| 252 |
case SbxBYREF | SbxUSHORT: |
| 253 |
*p->pUShort = CurTo<sal_uInt16>(r); break; |
| 254 |
case SbxLONG: |
| 255 |
p->nLong = CurTo<sal_Int32>(r); break; |
| 256 |
case SbxBYREF | SbxLONG: |
| 257 |
*p->pLong = CurTo<sal_Int32>(r); break; |
| 258 |
case SbxULONG: |
| 259 |
p->nULong = CurTo<sal_uInt32>(r); break; |
| 260 |
case SbxBYREF | SbxULONG: |
| 261 |
*p->pULong = CurTo<sal_uInt32>(r); break; |
| 262 |
case SbxBYREF | SbxCURRENCY: |
| 263 |
*p->pnInt64 = r; break; |
| 264 |
case SbxBYREF | SbxSALINT64: |
| 265 |
*p->pnInt64 = CurTo<sal_Int64>(r); break; |
| 266 |
case SbxBYREF | SbxSALUINT64: |
| 267 |
*p->puInt64 = CurTo<sal_uInt64>(r); break; |
| 268 |
case SbxSINGLE: |
| 269 |
p->nSingle = CurTo<float>( r ); break; |
| 270 |
case SbxBYREF | SbxSINGLE: |
| 271 |
*p->pSingle = CurTo<float>( r ); break; |
| 272 |
case SbxBYREF | SbxDATE: |
| 273 |
case SbxBYREF | SbxDOUBLE: |
| 274 |
*p->pDouble = CurTo<double>( r ); break; |
| 275 |
default: |
| 276 |
SbxBase::SetError( ERRCODE_BASIC_CONVERSION ); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |