LibreOffice Module sc (master)  1
unitconv.cxx
Go to the documentation of this file.
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 <com/sun/star/uno/Sequence.hxx>
21 
22 #include <unitconv.hxx>
23 #include <global.hxx>
24 #include <optutil.hxx>
25 
26 using namespace utl;
27 using namespace com::sun::star::uno;
28 
29 const sal_Unicode cDelim = 0x01; // delimiter between From and To
30 
31 // ScUnitConverterData
33  std::u16string_view rFromUnit, std::u16string_view rToUnit, double fValue ) :
34  maIndexString(BuildIndexString(rFromUnit, rToUnit)),
35  mfValue(fValue) {}
36 
38  std::u16string_view rFromUnit, std::u16string_view rToUnit )
39 {
40  return rFromUnit + OUStringChar(cDelim) + rToUnit;
41 }
42 
43 // ScUnitConverter
44 #define CFGPATH_UNIT "Office.Calc/UnitConversion"
45 #define CFGSTR_UNIT_FROM "FromUnit"
46 #define CFGSTR_UNIT_TO "ToUnit"
47 #define CFGSTR_UNIT_FACTOR "Factor"
48 
50 {
51  // read from configuration - "convert.ini" is no longer used
52  //TODO: config item as member to allow change of values
53 
54  ScLinkConfigItem aConfigItem( CFGPATH_UNIT );
55 
56  // empty node name -> use the config item's path itself
57  const Sequence<OUString> aNodeNames = aConfigItem.GetNodeNames( "" );
58 
59  tools::Long nNodeCount = aNodeNames.getLength();
60  if ( !nNodeCount )
61  return;
62 
63  Sequence<OUString> aValNames( nNodeCount * 3 );
64  OUString* pValNameArray = aValNames.getArray();
65  const OUString sSlash('/');
66 
67  tools::Long nIndex = 0;
68  for (const OUString& rNode : aNodeNames)
69  {
70  OUString sPrefix = rNode + sSlash;
71 
72  pValNameArray[nIndex++] = sPrefix + CFGSTR_UNIT_FROM;
73  pValNameArray[nIndex++] = sPrefix + CFGSTR_UNIT_TO;
74  pValNameArray[nIndex++] = sPrefix + CFGSTR_UNIT_FACTOR;
75  }
76 
77  Sequence<Any> aProperties = aConfigItem.GetProperties(aValNames);
78 
79  if (aProperties.getLength() != aValNames.getLength())
80  return;
81 
82  const Any* pProperties = aProperties.getConstArray();
83 
84  OUString sFromUnit;
85  OUString sToUnit;
86  double fFactor = 0;
87 
88  nIndex = 0;
89  for (tools::Long i=0; i<nNodeCount; i++)
90  {
91  pProperties[nIndex++] >>= sFromUnit;
92  pProperties[nIndex++] >>= sToUnit;
93  pProperties[nIndex++] >>= fFactor;
94 
95  ScUnitConverterData aNew(sFromUnit, sToUnit, fFactor);
96  OUString const aIndex = aNew.GetIndexString();
97  maData.insert(std::make_pair(aIndex, aNew));
98  }
99 }
100 
102 
104  double& fValue, std::u16string_view rFromUnit, std::u16string_view rToUnit ) const
105 {
106  OUString aIndex = ScUnitConverterData::BuildIndexString(rFromUnit, rToUnit);
107  MapType::const_iterator it = maData.find(aIndex);
108  if (it == maData.end())
109  {
110  fValue = 1.0;
111  return false;
112  }
113 
114  fValue = it->second.GetValue();
115  return true;
116 }
117 
118 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 nIndex
long Long
MapType maData
Definition: unitconv.hxx:46
sal_uInt16 sal_Unicode
ScUnitConverterData(std::u16string_view rFromUnit, std::u16string_view rToUnit, double fValue)
Definition: unitconv.cxx:32
PropertiesInfo aProperties
#define CFGSTR_UNIT_FACTOR
Definition: unitconv.cxx:47
const sal_Unicode cDelim
Definition: unitconv.cxx:29
OUString sPrefix
int i
#define CFGSTR_UNIT_TO
Definition: unitconv.cxx:46
static OUString BuildIndexString(std::u16string_view rFromUnit, std::u16string_view rToUnit)
Definition: unitconv.cxx:37
std::deque< AttacherIndex_Impl > aIndex
const OUString & GetIndexString() const
Definition: unitconv.hxx:38
css::uno::Sequence< OUString > GetNodeNames(const OUString &rNode)
bool GetValue(double &fValue, std::u16string_view rFromUnit, std::u16string_view rToUnit) const
Definition: unitconv.cxx:103
#define CFGSTR_UNIT_FROM
Definition: unitconv.cxx:45
css::uno::Sequence< css::uno::Any > GetProperties(const css::uno::Sequence< OUString > &rNames)
Definition: optutil.hxx:51
#define CFGPATH_UNIT
Definition: unitconv.cxx:44