GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/itemxmlutils.cpp Lines: 41 43 95.3 %
Date: 2018-09-20 Branches: 33 52 63.5 %

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
80
static bool readField(ItemFieldInfos::const_iterator it,
33
                      XmlNodeConstPtr node,
34
                      std::string &combined)
35
{
36
240
    const std::string fieldName = (*it).first;
37
80
    const ItemFieldType *const field = (*it).second;
38
39
    std::string value = XML::getProperty(node,
40
        fieldName.c_str(),
41

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

40
        if (!readField(it, node, field))
92
29
            continue;
93
11
        if (!effect.empty())
94
3
            effect.append(" / ");
95
11
        effect.append(field);
96
    }
97
82
}
98
99
10
void readItemStatsVector(STD_VECTOR<std::string> &effect,
100
                         XmlNodeConstPtr node,
101
                         const ItemFieldInfos &fields)
102
{
103
10
    if (translator == nullptr)
104
    {
105
        reportAlways("called readItemStatsVector without translator");
106
        return;
107
    }
108
109
20
    FOR_EACH (ItemFieldInfos::const_iterator, it, fields)
110
    {
111
51
        std::string field;
112

40
        if (!readField(it, node, field))
113
29
            continue;
114
11
        effect.push_back(field);
115
    }
116
10
}