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

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

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

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

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

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

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