GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/itemxmlutils.cpp Lines: 45 47 95.7 %
Date: 2021-03-17 Branches: 36 58 62.1 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2014-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/itemxmlutils.h"
23
24
#include "utils/checkutils.h"
25
#include "utils/foreach.h"
26
#include "utils/stringutils.h"
27
#include "utils/translation/podict.h"
28
29
#include "resources/item/itemfieldtype.h"
30
31
#include "debug.h"
32
33
12
static void convertSignedValue(std::string &outStr,
34
                               const std::string &srcStr)
35
{
36

24
    if (!srcStr.empty() && srcStr[0] != '-')
37
72
        outStr = std::string("+").append(srcStr);
38
    else
39
        outStr = srcStr;
40
12
}
41
42
80
static bool readField(ItemFieldInfos::const_iterator it,
43
                      XmlNodeConstPtr node,
44
                      std::string &combined)
45
{
46
240
    const std::string fieldName = (*it).first;
47
80
    const ItemFieldType *const field = (*it).second;
48
49
    std::string value = XML::getProperty(node,
50
        fieldName.c_str(),
51

480
        "");
52
80
    if (value.empty())
53
        return false;
54
55
44
    StringVect tokens;
56
    splitToStringVector(tokens,
57
        value,
58
22
        '-');
59
44
    if (tokens.size() > 1)
60
    {
61
8
        std::string value1;
62
8
        std::string value2;
63
4
        if (field->sign)
64
        {
65
2
            convertSignedValue(value1, tokens[0]);
66
4
            convertSignedValue(value2, tokens[1]);
67
        }
68
        else
69
        {
70
4
            value1 = tokens[0];
71
4
            value2 = tokens[1];
72
        }
73
12
        value = strprintf("%s - %s",
74
            value1.c_str(),
75
4
            value2.c_str());
76
    }
77
    else
78
    {
79
18
        if (field->sign)
80
8
            convertSignedValue(value, value);
81
    }
82
44
    const std::string format = translator->getStr(field->description);
83
66
    combined = strprintf(format.c_str(),
84
22
        value.c_str());
85
22
    return true;
86
}
87
88
82
void readItemStatsString(std::string &effect,
89
                         XmlNodeConstPtr node,
90
                         const ItemFieldInfos &fields)
91
{
92
82
    if (translator == nullptr)
93
    {
94
        reportAlways("called readItemStatsString without translator")
95
        return;
96
    }
97
98
164
    FOR_EACH (ItemFieldInfos::const_iterator, it, fields)
99
    {
100
51
        std::string field;
101

40
        if (!readField(it, node, field))
102
29
            continue;
103
11
        if (!effect.empty())
104
3
            effect.append(" / ");
105
11
        effect.append(field);
106
    }
107
82
}
108
109
10
void readItemStatsVector(STD_VECTOR<std::string> &effect,
110
                         XmlNodeConstPtr node,
111
                         const ItemFieldInfos &fields)
112
{
113
10
    if (translator == nullptr)
114
    {
115
        reportAlways("called readItemStatsVector without translator")
116
        return;
117
    }
118
119
20
    FOR_EACH (ItemFieldInfos::const_iterator, it, fields)
120
    {
121
51
        std::string field;
122

40
        if (!readField(it, node, field))
123
29
            continue;
124
11
        effect.push_back(field);
125
    }
126
10
}