GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/languagedb.cpp Lines: 10 48 20.8 %
Date: 2017-11-29 Branches: 2 88 2.3 %

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

6
}