GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/palettedb.cpp Lines: 37 40 92.5 %
Date: 2021-03-17 Branches: 28 52 53.8 %

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/palettedb.h"
23
24
#include "configuration.h"
25
26
#include "fs/virtfs/tools.h"
27
28
#include "utils/checkutils.h"
29
30
#include "resources/dye/dyecolor.h"
31
32
#include "debug.h"
33
34
namespace
35
{
36
    bool mLoaded = false;
37
1
    std::map<std::string, DyeColor> mColors;
38
}  // namespace
39
40
24
void PaletteDB::load()
41
{
42
24
    if (mLoaded)
43
23
        unload();
44
45
24
    logger->log1("Initializing palette database...");
46
24
    loadPalette();
47
24
}
48
49
24
void PaletteDB::loadPalette()
50
{
51
24
    mLoaded = true;
52
48
    StringVect lines;
53

144
    VirtFs::loadTextFile(paths.getStringValue("palettesDir") +
54

120
        paths.getStringValue("defaultPaletteFile"),
55
24
        lines);
56
48
    StringVectCIter it = lines.begin();
57
48
    if (it == lines.end())
58
    {
59
        logger->log("missing GIMP palette file");
60
        return;
61
    }
62
48
    if (*it != "GIMP Palette")
63
    {
64
        reportAlways("wrong GIMP palette file")
65
        return;
66
    }
67
    ++ it;
68
    // skip header
69
144
    while (it != lines.end())
70
    {
71
192
        const std::string line = *it;
72

144
        if (!line.empty() && line[0] == '#')
73
            break;
74
48
        ++ it;
75
    }
76
77
    char name[101];
78
79
    // process colors and ignore commets
80
480
    while (it != lines.end())
81
    {
82
624
        const std::string line = *it;
83
216
        ++ it;
84
85

432
        if (line.empty() || line[0] == '#')
86
24
            continue;
87
88
        unsigned int r;
89
        unsigned int g;
90
        unsigned int b;
91
92
192
        if (sscanf(line.c_str(), "%10u %10u %10u\t%100s",
93
            &r, &g, &b, name) == 4)
94
        {
95
192
            name[100] = 0;
96

960
            mColors[name] = DyeColor(CAST_U8(r),
97
                CAST_U8(g),
98
                CAST_U8(b));
99
        }
100
    }
101
}
102
103
227
void PaletteDB::unload()
104
{
105
227
    logger->log1("Unloading palette database...");
106
227
    mColors.clear();
107
227
}
108
109
26
const DyeColor *PaletteDB::getColor(const std::string &name)
110
{
111
    const std::map<std::string, DyeColor>::const_iterator it =
112
52
        mColors.find(name);
113
26
    if (it != mColors.end())
114
14
        return &(*it).second;
115
    return nullptr;
116
2
}