GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/palettedb.cpp Lines: 36 39 92.3 %
Date: 2017-11-29 Branches: 24 48 50.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-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/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
2
    std::map<std::string, DyeColor> mColors;
37
}  // namespace
38
39
48
void PaletteDB::load()
40
{
41
48
    if (mLoaded)
42
46
        unload();
43
44
48
    logger->log1("Initializing palette database...");
45
48
    loadPalette();
46
48
}
47
48
48
void PaletteDB::loadPalette()
49
{
50
48
    mLoaded = true;
51
96
    StringVect lines;
52


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

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

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

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

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