GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/textdb.cpp Lines: 6 37 16.2 %
Date: 2017-11-29 Branches: 0 56 0.0 %

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