summaryrefslogtreecommitdiff
path: root/sc/source/core/tool/unitconv.cxx
blob: 4a8bc7839be98d68599048ec4bedc66bfe2686b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <com/sun/star/uno/Sequence.hxx>

#include <unitconv.hxx>
#include <global.hxx>
#include <optutil.hxx>

using namespace utl;
using namespace com::sun::star::uno;

const sal_Unicode cDelim = 0x01;        // delimiter between From and To

// ScUnitConverterData
ScUnitConverterData::ScUnitConverterData(
    std::u16string_view rFromUnit, std::u16string_view rToUnit, double fValue ) :
    maIndexString(BuildIndexString(rFromUnit, rToUnit)),
    mfValue(fValue) {}

OUString ScUnitConverterData::BuildIndexString(
    std::u16string_view rFromUnit, std::u16string_view rToUnit )
{
    return rFromUnit + OUStringChar(cDelim) + rToUnit;
}

// ScUnitConverter
#define CFGPATH_UNIT        "Office.Calc/UnitConversion"
#define CFGSTR_UNIT_FROM    "FromUnit"
#define CFGSTR_UNIT_TO      "ToUnit"
#define CFGSTR_UNIT_FACTOR  "Factor"

ScUnitConverter::ScUnitConverter()
{
    // read from configuration - "convert.ini" is no longer used
    //TODO: config item as member to allow change of values

    ScLinkConfigItem aConfigItem( CFGPATH_UNIT );

    // empty node name -> use the config item's path itself
    const Sequence<OUString> aNodeNames = aConfigItem.GetNodeNames( "" );

    tools::Long nNodeCount = aNodeNames.getLength();
    if ( !nNodeCount )
        return;

    Sequence<OUString> aValNames( nNodeCount * 3 );
    OUString* pValNameArray = aValNames.getArray();
    const OUString sSlash('/');

    tools::Long nIndex = 0;
    for (const OUString& rNode : aNodeNames)
    {
        OUString sPrefix = rNode + sSlash;

        pValNameArray[nIndex++] = sPrefix + CFGSTR_UNIT_FROM;
        pValNameArray[nIndex++] = sPrefix + CFGSTR_UNIT_TO;
        pValNameArray[nIndex++] = sPrefix + CFGSTR_UNIT_FACTOR;
    }

    Sequence<Any> aProperties = aConfigItem.GetProperties(aValNames);

    if (aProperties.getLength() != aValNames.getLength())
        return;

    const Any* pProperties = aProperties.getConstArray();

    OUString sFromUnit;
    OUString sToUnit;
    double fFactor = 0;

    nIndex = 0;
    for (tools::Long i=0; i<nNodeCount; i++)
    {
        pProperties[nIndex++] >>= sFromUnit;
        pProperties[nIndex++] >>= sToUnit;
        pProperties[nIndex++] >>= fFactor;

        ScUnitConverterData aNew(sFromUnit, sToUnit, fFactor);
        OUString const aIndex = aNew.GetIndexString();
        maData.insert(std::make_pair(aIndex, aNew));
    }
}

ScUnitConverter::~ScUnitConverter() {}

bool ScUnitConverter::GetValue(
    double& fValue, std::u16string_view rFromUnit, std::u16string_view rToUnit ) const
{
    OUString aIndex = ScUnitConverterData::BuildIndexString(rFromUnit, rToUnit);
    MapType::const_iterator it = maData.find(aIndex);
    if (it == maData.end())
    {
        fValue = 1.0;
        return false;
    }

    fValue = it->second.GetValue();
    return true;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */