GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/deaddb.cpp Lines: 7 42 16.7 %
Date: 2017-11-29 Branches: 0 60 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-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/deaddb.h"
22
23
#include "configuration.h"
24
#include "logger.h"
25
26
#include "utils/translation/podict.h"
27
28
#include "resources/beingcommon.h"
29
30
#include "debug.h"
31
32
namespace
33
{
34
    bool mLoaded = false;
35
2
    STD_VECTOR<std::string> mMessages;
36
}  // namespace
37
38
void DeadDB::load()
39
{
40
    if (mLoaded)
41
        unload();
42
43
    logger->log1("Initializing dead database...");
44
45
    loadXmlFile(paths.getStringValue("deadMessagesFile"), SkipError_false);
46
    loadXmlFile(paths.getStringValue("deadMessagesPatchFile"), SkipError_true);
47
    loadXmlDir("deadMessagesPatchDir", loadXmlFile);
48
    mLoaded = true;
49
}
50
51
void DeadDB::loadXmlFile(const std::string &fileName,
52
                         const SkipError skipError)
53
{
54
    XML::Document *doc = new XML::Document(fileName,
55
        UseVirtFs_true,
56
        skipError);
57
    XmlNodeConstPtrConst root = doc->rootNode();
58
59
    if ((root == nullptr) || !xmlNameEqual(root, "messages"))
60
    {
61
        logger->log("DeadDB: Failed to parse %s.",
62
            paths.getStringValue("deadMessagesFile").c_str());
63
        delete doc;
64
        return;
65
    }
66
67
    for_each_xml_child_node(node, root)
68
    {
69
        if (xmlNameEqual(node, "include"))
70
        {
71
            const std::string name = XML::getProperty(node, "name", "");
72
            if (!name.empty())
73
                loadXmlFile(name, skipError);
74
            continue;
75
        }
76
        else if (xmlNameEqual(node, "message"))
77
        {
78
            XmlChar *const data = reinterpret_cast<XmlChar*>(
79
                XmlNodeGetContent(node));
80
            if (data == nullptr)
81
                continue;
82
            if (*data == 0)
83
            {
84
                XmlFree(data);
85
                continue;
86
            }
87
            mMessages.push_back(data);
88
            XmlFree(data);
89
        }
90
    }
91
92
    delete doc;
93
}
94
95
384
void DeadDB::unload()
96
{
97
384
    logger->log1("Unloading dead database...");
98
384
    mMessages.clear();
99
384
    mLoaded = false;
100
384
}
101
102
std::string DeadDB::getRandomString()
103
{
104
    const size_t sz = mMessages.size();
105
    if (sz == 0u)
106
        return std::string();
107
    return translator->getStr(mMessages[rand() % sz]);
108
4
}