GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/browserboxtools.cpp Lines: 21 67 31.3 %
Date: 2017-11-29 Branches: 21 116 18.1 %

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

32
    data = replaceAll(data, "%VER%", SMALL_VERSION);
50

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



8
    if (strStartWith(data, "http://") ||
79


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

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

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

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

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

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