GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/languagedb.cpp Lines: 10 47 21.3 %
Date: 2021-03-17 Branches: 2 86 2.3 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "resources/db/languagedb.h"
23
24
#include "configuration.h"
25
26
#include "utils/checkutils.h"
27
28
#include "resources/beingcommon.h"
29
30
#include "debug.h"
31
32
namespace
33
{
34
1
    std::string mDefaultIcon;
35
1
    std::string mDefaultPo;
36
1
    std::map<int, std::string> mIcons;
37
1
    std::map<int, std::string> mPo;
38
}  // namespace
39
40
void LanguageDb::load()
41
{
42
    unload();
43
    logger->log1("Initializing languages database...");
44
    loadXmlFile(paths.getStringValue("languagesFile"), SkipError_false);
45
    loadXmlFile(paths.getStringValue("languagesPatchFile"), SkipError_true);
46
    loadXmlDir("languagesPatchDir", loadXmlFile)
47
}
48
49
void LanguageDb::loadXmlFile(const std::string &fileName,
50
                             const SkipError skipError)
51
{
52
    XML::Document *doc = new XML::Document(fileName,
53
        UseVirtFs_true,
54
        skipError);
55
    XmlNodeConstPtrConst root = doc->rootNode();
56
57
    if ((root == nullptr) || !xmlNameEqual(root, "languages"))
58
    {
59
        delete doc;
60
        return;
61
    }
62
63
    for_each_xml_child_node(node, root)
64
    {
65
        if (xmlNameEqual(node, "include"))
66
        {
67
            const std::string name = XML::getProperty(node, "name", "");
68
            if (!name.empty())
69
                loadXmlFile(name, skipError);
70
            continue;
71
        }
72
        else if (xmlNameEqual(node, "lang"))
73
        {
74
            const int id = XML::getProperty(node, "id", -1);
75
            if (id < 0)
76
            {
77
                reportAlways("Missing lang id")
78
                continue;
79
            }
80
            const std::string icon = XML::getProperty(node, "icon", "");
81
            const std::string po = XML::getProperty(node, "po", "");
82
            if (icon.empty())
83
            {
84
                reportAlways("LanguageDb: empty icon field")
85
            }
86
            else
87
            {
88
                mIcons[id] = icon;
89
            }
90
            if (po.empty())
91
            {
92
                reportAlways("LanguageDb: empty po field")
93
            }
94
            else
95
            {
96
                mPo[id] = po;
97
            }
98
        }
99
    }
100
101
    delete doc;
102
}
103
104
215
void LanguageDb::unload()
105
{
106
215
    logger->log1("Unloading languages database...");
107
215
    mIcons.clear();
108
215
    mPo.clear();
109
215
}
110
111
const std::string &LanguageDb::getIcon(const int id)
112
{
113
    std::map<int, std::string>::const_iterator it = mIcons.find(id);
114
    if (it == mIcons.end())
115
        return mDefaultIcon;
116
    return (*it).second;
117
}
118
119
const std::string &LanguageDb::getPo(const int id)
120
{
121
    std::map<int, std::string>::const_iterator it = mPo.find(id);
122
    if (it == mPo.end())
123
        return mDefaultPo;
124
    return (*it).second;
125

3
}