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 <com/sun/star/uno/Sequence.hxx>
22 #include <unitconv.hxx>
24 #include <optutil.hxx>
27 using namespace com::sun::star::uno
;
29 const sal_Unicode cDelim
= 0x01; // delimiter between From and To
31 // ScUnitConverterData
32 ScUnitConverterData::ScUnitConverterData(
33 std::u16string_view rFromUnit
, std::u16string_view rToUnit
, double fValue
) :
34 maIndexString(BuildIndexString(rFromUnit
, rToUnit
)),
37 OUString
ScUnitConverterData::BuildIndexString(
38 std::u16string_view rFromUnit
, std::u16string_view rToUnit
)
40 return rFromUnit
+ OUStringChar(cDelim
) + rToUnit
;
44 constexpr OUStringLiteral CFGPATH_UNIT
= u
"Office.Calc/UnitConversion";
45 constexpr OUStringLiteral CFGSTR_UNIT_FROM
= u
"FromUnit";
46 constexpr OUStringLiteral CFGSTR_UNIT_TO
= u
"ToUnit";
47 constexpr OUStringLiteral CFGSTR_UNIT_FACTOR
= u
"Factor";
49 ScUnitConverter::ScUnitConverter()
51 // read from configuration - "convert.ini" is no longer used
52 //TODO: config item as member to allow change of values
54 ScLinkConfigItem
aConfigItem( CFGPATH_UNIT
);
56 // empty node name -> use the config item's path itself
57 const Sequence
<OUString
> aNodeNames
= aConfigItem
.GetNodeNames( u
""_ustr
);
59 tools::Long nNodeCount
= aNodeNames
.getLength();
63 Sequence
<OUString
> aValNames( nNodeCount
* 3 );
64 OUString
* pValNameArray
= aValNames
.getArray();
65 const OUString
sSlash('/');
67 tools::Long nIndex
= 0;
68 for (const OUString
& rNode
: aNodeNames
)
70 OUString sPrefix
= rNode
+ sSlash
;
72 pValNameArray
[nIndex
++] = sPrefix
+ CFGSTR_UNIT_FROM
;
73 pValNameArray
[nIndex
++] = sPrefix
+ CFGSTR_UNIT_TO
;
74 pValNameArray
[nIndex
++] = sPrefix
+ CFGSTR_UNIT_FACTOR
;
77 Sequence
<Any
> aProperties
= aConfigItem
.GetProperties(aValNames
);
79 if (aProperties
.getLength() != aValNames
.getLength())
82 const Any
* pProperties
= aProperties
.getConstArray();
89 for (tools::Long i
=0; i
<nNodeCount
; i
++)
91 pProperties
[nIndex
++] >>= sFromUnit
;
92 pProperties
[nIndex
++] >>= sToUnit
;
93 pProperties
[nIndex
++] >>= fFactor
;
95 ScUnitConverterData
aNew(sFromUnit
, sToUnit
, fFactor
);
96 OUString
const aIndex
= aNew
.GetIndexString();
97 maData
.insert(std::make_pair(aIndex
, aNew
));
101 ScUnitConverter::~ScUnitConverter() {}
103 bool ScUnitConverter::GetValue(
104 double& fValue
, std::u16string_view rFromUnit
, std::u16string_view rToUnit
) const
106 OUString aIndex
= ScUnitConverterData::BuildIndexString(rFromUnit
, rToUnit
);
107 MapType::const_iterator it
= maData
.find(aIndex
);
108 if (it
== maData
.end())
114 fValue
= it
->second
.GetValue();
118 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */