GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/textdb.cpp Lines: 6 36 16.7 %
Date: 2021-03-17 Branches: 0 58 0.0 %

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/textdb.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
    StringVect mTexts;
35
}  // namespace
36
37
void TextDb::load()
38
{
39
    unload();
40
    logger->log1("Initializing text database...");
41
    loadXmlFile(paths.getStringValue("textsFile"), SkipError_false);
42
    loadXmlFile(paths.getStringValue("textsPatchFile"), SkipError_true);
43
    loadXmlDir("textsPatchDir", loadXmlFile)
44
}
45
46
void TextDb::loadXmlFile(const std::string &fileName,
47
                         const SkipError skipError)
48
{
49
    XML::Document *doc = new XML::Document(fileName,
50
        UseVirtFs_true,
51
        skipError);
52
    XmlNodeConstPtrConst root = doc->rootNode();
53
54
    if ((root == nullptr) || !xmlNameEqual(root, "texts"))
55
    {
56
        delete doc;
57
        return;
58
    }
59
60
    for_each_xml_child_node(node, root)
61
    {
62
        if (xmlNameEqual(node, "include"))
63
        {
64
            const std::string name = XML::getProperty(node, "name", "");
65
            if (!name.empty())
66
                loadXmlFile(name, skipError);
67
            continue;
68
        }
69
        else if (xmlNameEqual(node, "text"))
70
        {
71
            const bool show = XML::getBoolProperty(node, "show", false);
72
            if (show == true)
73
            {
74
                if (!XmlHaveChildContent(node))
75
                    continue;
76
77
                std::string text = XmlChildContent(node);
78
                mTexts.push_back(text);
79
            }
80
        }
81
    }
82
83
    delete doc;
84
}
85
86
204
void TextDb::unload()
87
{
88
204
    logger->log1("Unloading text database...");
89
204
    mTexts.clear();
90
204
}
91
92
const StringVect &TextDb::getTexts()
93
{
94
    return mTexts;
95
}
96
97
std::string TextDb::getByIndex(const int index)
98
{
99
    if (index < 0 ||
100
        static_cast<size_t>(index) >= mTexts.size())
101
    {
102
        return std::string();
103
    }
104
    return mTexts[index];
105
2
}