GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/badgesdb.cpp Lines: 16 50 32.0 %
Date: 2018-09-20 Branches: 3 50 6.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2014-2018  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
1
    BadgesInfos mGuilds;
36
1
    BadgesInfos mNames;
37
1
    BadgesInfos mParties;
38
1
    BadgesInfos mClans;
39
    bool mLoaded = false;
40
}  // namespace
41
42
static void loadXmlFile(const std::string &file,
43
                        const std::string &name,
44
                        BadgesInfos &arr,
45
                        const SkipError skipError)
46
{
47
    readXmlStringMap(file,
48
        "badges",
49
        name,
50
        "badge",
51
        "name",
52
        "image",
53
        arr,
54
        skipError);
55
}
56
57
static void loadDB(const std::string &name, BadgesInfos &arr)
58
{
59
    loadXmlFile(paths.getStringValue("badgesFile"),
60
        name, arr, SkipError_false);
61
    loadXmlFile(paths.getStringValue("badgesPatchFile"),
62
        name, arr, SkipError_true);
63
64
    StringVect listVect;
65
    VirtFs::getFilesInDir(paths.getStringValue(
66
        "badgesPatchDir"),
67
        listVect,
68
        ".xml");
69
    FOR_EACH (StringVectCIter, itVect, listVect)
70
        loadXmlFile(*itVect, name, arr, SkipError_true);
71
}
72
73
void BadgesDB::load()
74
{
75
    if (mLoaded)
76
        unload();
77
78
    logger->log1("Initializing Badges database...");
79
    loadDB("guild", mGuilds);
80
    loadDB("name", mNames);
81
    loadDB("party", mParties);
82
    loadDB("clan", mClans);
83
}
84
85
215
void BadgesDB::unload()
86
{
87
215
    logger->log1("Unloading Badges database...");
88
215
    mParties.clear();
89
215
    mClans.clear();
90
215
    mGuilds.clear();
91
215
    mNames.clear();
92
215
    mLoaded = false;
93
215
}
94
95
const std::string BadgesDB::getPartyBadge(const std::string &name)
96
{
97
    const BadgesInfosIter it = mParties.find(name);
98
    if (it == mParties.end())
99
        return std::string();
100
    return (*it).second;
101
}
102
103
6
const std::string BadgesDB::getNameBadge(const std::string &name)
104
{
105
12
    const BadgesInfosIter it = mNames.find(name);
106
6
    if (it == mNames.end())
107
        return std::string();
108
    return (*it).second;
109
}
110
111
const std::string BadgesDB::getGuildBadge(const std::string &name)
112
{
113
    const BadgesInfosIter it = mGuilds.find(name);
114
    if (it == mGuilds.end())
115
        return std::string();
116
    return (*it).second;
117
}
118
119
const std::string BadgesDB::getClanBadge(const std::string &name)
120
{
121
    const BadgesInfosIter it = mClans.find(name);
122
    if (it == mClans.end())
123
        return std::string();
124
    return (*it).second;
125

3
}