GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/badgesdb.cpp Lines: 14 39 35.9 %
Date: 2017-11-29 Branches: 3 58 5.2 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2014-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/badgesdb.h"
22
23
#include "configuration.h"
24
#include "logger.h"
25
26
#include "fs/virtfs/tools.h"
27
28
#include "utils/foreach.h"
29
#include "utils/xmlutils.h"
30
31
#include "debug.h"
32
33
namespace
34
{
35
2
    BadgesInfos mGuilds;
36
2
    BadgesInfos mNames;
37
2
    BadgesInfos mParties;
38
    bool mLoaded = false;
39
}  // namespace
40
41
static void loadXmlFile(const std::string &file,
42
                        const std::string &name,
43
                        BadgesInfos &arr,
44
                        const SkipError skipError)
45
{
46
    readXmlStringMap(file,
47
        "badges",
48
        name,
49
        "badge",
50
        "name",
51
        "image",
52
        arr,
53
        skipError);
54
}
55
56
static void loadDB(const std::string &name, BadgesInfos &arr)
57
{
58
    loadXmlFile(paths.getStringValue("badgesFile"),
59
        name, arr, SkipError_false);
60
    loadXmlFile(paths.getStringValue("badgesPatchFile"),
61
        name, arr, SkipError_true);
62
63
    StringVect listVect;
64
    VirtFs::getFilesInDir(paths.getStringValue(
65
        "badgesPatchDir"),
66
        listVect,
67
        ".xml");
68
    FOR_EACH (StringVectCIter, itVect, listVect)
69
        loadXmlFile(*itVect, name, arr, SkipError_true);
70
}
71
72
void BadgesDB::load()
73
{
74
    if (mLoaded)
75
        unload();
76
77
    logger->log1("Initializing Badges database...");
78
    loadDB("guild", mGuilds);
79
    loadDB("name", mNames);
80
    loadDB("party", mParties);
81
}
82
83
384
void BadgesDB::unload()
84
{
85
384
    logger->log1("Unloading Badges database...");
86
384
    mParties.clear();
87
384
    mGuilds.clear();
88
384
    mNames.clear();
89
384
    mLoaded = false;
90
384
}
91
92
const std::string BadgesDB::getPartyBadge(const std::string &name)
93
{
94
    const BadgesInfosIter it = mParties.find(name);
95
    if (it == mParties.end())
96
        return std::string();
97
    return (*it).second;
98
}
99
100
12
const std::string BadgesDB::getNameBadge(const std::string &name)
101
{
102
24
    const BadgesInfosIter it = mNames.find(name);
103
12
    if (it == mNames.end())
104
        return std::string();
105
    return (*it).second;
106
}
107
108
const std::string BadgesDB::getGuildBadge(const std::string &name)
109
{
110
    const BadgesInfosIter it = mGuilds.find(name);
111
    if (it == mGuilds.end())
112
        return std::string();
113
    return (*it).second;
114

6
}