GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/browserboxtools.cpp Lines: 21 66 31.8 %
Date: 2021-03-17 Branches: 24 122 19.7 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-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 "utils/browserboxtools.h"
23
24
#include "itemcolormanager.h"
25
#include "main.h"
26
#include "settings.h"
27
28
#include "input/inputmanager.h"
29
30
#include "utils/stringutils.h"
31
32
#ifndef DYECMD
33
#include "const/resources/item/cards.h"
34
35
#include "resources/beinginfo.h"
36
#include "resources/iteminfo.h"
37
38
#include "resources/db/homunculusdb.h"
39
#include "resources/db/itemdb.h"
40
#include "resources/db/mercenarydb.h"
41
#include "resources/db/monsterdb.h"
42
#include "resources/db/questdb.h"
43
#include "resources/db/petdb.h"
44
#endif  // DYECMD
45
46
#include "debug.h"
47
48
2
void BrowserBoxTools::replaceVars(std::string &data)
49
{
50

16
    data = replaceAll(data, "%VER%", SMALL_VERSION);
51
10
    data = replaceAll(data, "%SUPPORTURL%", settings.supportUrl);
52
2
}
53
54
void BrowserBoxTools::replaceKeys(std::string &data)
55
{
56
    size_t idx1 = data.find("###");
57
    while (idx1 != std::string::npos)
58
    {
59
        const size_t idx2 = data.find(';', idx1);
60
        if (idx2 == std::string::npos)
61
            break;
62
63
        const std::string str = inputManager.getKeyValueByNameLong(
64
            data.substr(idx1 + 3, idx2 - idx1 - 3));
65
        data.replace(idx1, idx2 - idx1 + 1, str);
66
67
        idx1 = data.find("###");
68
    }
69
}
70
71
2
std::string BrowserBoxTools::replaceLinkCommands(const std::string &link)
72
{
73
#ifdef DYECMD
74
    return link;
75
#else  // DYECMD
76
77
2
    std::string data = link;
78
79



10
    if (strStartWith(data, "http://") ||
80


8
        strStartWith(data, "https://"))
81
    {
82
        return data;
83
    }
84
85

2
    if (!link.empty() && link[0] == 'm')
86
    {  // monster link
87
        const BeingTypeId id = static_cast<BeingTypeId>(
88
            atoi(link.substr(1).c_str()));
89
        BeingInfo *const info = MonsterDB::get(id);
90
        if (info != nullptr)
91
            data = info->getName();
92
    }
93

2
    else if (!link.empty() && link[0] == 'p')
94
    {  // pet link
95
        const BeingTypeId id = static_cast<BeingTypeId>(
96
            atoi(link.substr(1).c_str()));
97
        BeingInfo *const info = PETDB::get(id);
98
        if (info != nullptr)
99
            data = info->getName();
100
    }
101

2
    else if (!link.empty() && link[0] == 'h')
102
    {  // homunculus link
103
        const BeingTypeId id = static_cast<BeingTypeId>(
104
            atoi(link.substr(1).c_str()));
105
        BeingInfo *const info = HomunculusDB::get(id);
106
        if (info != nullptr)
107
            data = info->getName();
108
    }
109

2
    else if (!link.empty() && link[0] == 'M')
110
    {  // mercenary link
111
        const BeingTypeId id = static_cast<BeingTypeId>(
112
            atoi(link.substr(1).c_str()));
113
        BeingInfo *const info = MercenaryDB::get(id);
114
        if (info != nullptr)
115
            data = info->getName();
116
    }
117

2
    else if (!link.empty() && link[0] == 'q')
118
    {  // quest link
119
        data = QuestDb::getName(
120
            atoi(link.substr(1).c_str()));
121
    }
122
    else
123
    {  // item link
124
2
        size_t idx = link.find(',');
125
2
        if (idx != std::string::npos)
126
        {
127
            const int id = atoi(link.substr(0, idx).c_str());
128
            if (id != 0)
129
            {
130
                STD_VECTOR<int> parts;
131
                splitToIntVector(parts,
132
                    link.substr(idx), ',');
133
                while (parts.size() < maxCards)
134
                    parts.push_back(0);
135
                const ItemColor itemColor =
136
                    ItemColorManager::getColorFromCards(&parts[0]);
137
                data = ItemDB::get(id).getName(itemColor);
138
            }
139
        }
140
        else
141
        {
142
4
            const int id = atoi(link.c_str());
143
2
            if (id != 0)
144
                data = ItemDB::get(id).getName();
145
        }
146
    }
147
    return data;
148
#endif  // DYECMD
149
}
150
151
2
void BrowserBoxTools::replaceTabs(std::string &data)
152
{
153
2
    size_t idx1 = data.find("\\t");
154
2
    while (idx1 != std::string::npos)
155
    {
156
        const size_t idx2 = data.find(';', idx1);
157
        if (idx2 == std::string::npos)
158
            break;
159
160
        const unsigned int newSize = CAST_U32(
161
            atoi(data.substr(
162
            idx1 + 2, idx2 - idx1 - 2).c_str()));
163
        std::string str = data.substr(0, idx1);
164
        while (str.size() < newSize)
165
            str.append(" ");
166
        str.append(data.substr(idx2 + 1));
167
        data = str;
168
        idx1 = data.find("\\t");
169
    }
170
2
}