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

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

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

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

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

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