ManaPlus
Functions
itemxmlutils.cpp File Reference

(986a3bf)

#include "utils/itemxmlutils.h"
#include "utils/checkutils.h"
#include "utils/foreach.h"
#include "utils/stringutils.h"
#include "utils/translation/podict.h"
#include "resources/item/itemfieldtype.h"
#include "debug.h"

Go to the source code of this file.

Functions

static void convertSignedValue (std::string &outStr, const std::string &srcStr)
 
static bool readField (ItemFieldInfos::const_iterator it, const xmlNodePtr node, std::string &combined)
 
void readItemStatsString (std::string &effect, const xmlNodePtr node, const ItemFieldInfos &fields)
 
void readItemStatsVector (std::vector< std::string > &effect, const xmlNodePtr node, const ItemFieldInfos &fields)
 

Function Documentation

◆ convertSignedValue()

static void convertSignedValue ( std::string &  outStr,
const std::string &  srcStr 
)
static

Definition at line 33 of file itemxmlutils.cpp.

35 {
36  if (!srcStr.empty() && srcStr[0] != '-')
37  outStr = std::string("+").append(srcStr);
38  else
39  outStr = srcStr;
40 }

Referenced by readField().

◆ readField()

static bool readField ( ItemFieldInfos::const_iterator  it,
const xmlNodePtr  node,
std::string &  combined 
)
static

Definition at line 42 of file itemxmlutils.cpp.

45 {
46  const std::string fieldName = (*it).first;
47  const ItemFieldType *const field = (*it).second;
48 
49  std::string value = XML::getProperty(node,
50  fieldName.c_str(),
51  "");
52  if (value.empty())
53  return false;
54 
55  StringVect tokens;
56  splitToStringVector(tokens,
57  value,
58  '-');
59  if (tokens.size() > 1)
60  {
61  std::string value1;
62  std::string value2;
63  if (field->sign)
64  {
65  convertSignedValue(value1, tokens[0]);
66  convertSignedValue(value2, tokens[1]);
67  }
68  else
69  {
70  value1 = tokens[0];
71  value2 = tokens[1];
72  }
73  value = strprintf("%s - %s",
74  value1.c_str(),
75  value2.c_str());
76  }
77  else
78  {
79  if (field->sign)
80  convertSignedValue(value, value);
81  }
82  const std::string format = translator->getStr(field->description);
83  combined = strprintf(format.c_str(),
84  value.c_str());
85  return true;
86 }
const std::string getStr(const std::string &str)
Definition: podict.cpp:45
static void convertSignedValue(std::string &outStr, const std::string &srcStr)
int getProperty(const xmlNodePtr node, const char *const name, int def)
Definition: libxml.cpp:174
PoDict * translator
Definition: podict.cpp:28
std::string strprintf(const char *const format,...)
void splitToStringVector(StringVect &tokens, const std::string &text, const char separator)
std::vector< std::string > StringVect
Definition: stringvector.h:29
const bool sign
Definition: itemfieldtype.h:43
const std::string description
Definition: itemfieldtype.h:42

References convertSignedValue(), ItemFieldType::description, XML::getProperty(), PoDict::getStr(), ItemFieldType::sign, splitToStringVector(), strprintf(), and translator.

Referenced by readItemStatsString(), and readItemStatsVector().

◆ readItemStatsString()

void readItemStatsString ( std::string &  effect,
const xmlNodePtr  node,
const ItemFieldInfos fields 
)

Definition at line 88 of file itemxmlutils.cpp.

91 {
92  if (translator == nullptr)
93  {
94  reportAlways("called readItemStatsString without translator")
95  return;
96  }
97 
98  FOR_EACH (ItemFieldInfos::const_iterator, it, fields)
99  {
100  std::string field;
101  if (!readField(it, node, field))
102  continue;
103  if (!effect.empty())
104  effect.append(" / ");
105  effect.append(field);
106  }
107 }
#define reportAlways(...)
Definition: checkutils.h:253
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
std::map< std::string, ItemFieldType * > ItemFieldInfos
static bool readField(ItemFieldInfos::const_iterator it, const xmlNodePtr node, std::string &combined)

References FOR_EACH, readField(), reportAlways, and translator.

Referenced by ItemDB::loadXmlFile().

◆ readItemStatsVector()

void readItemStatsVector ( std::vector< std::string > &  effect,
const xmlNodePtr  node,
const ItemFieldInfos fields 
)

Definition at line 109 of file itemxmlutils.cpp.

112 {
113  if (translator == nullptr)
114  {
115  reportAlways("called readItemStatsVector without translator")
116  return;
117  }
118 
119  FOR_EACH (ItemFieldInfos::const_iterator, it, fields)
120  {
121  std::string field;
122  if (!readField(it, node, field))
123  continue;
124  effect.push_back(field);
125  }
126 }

References FOR_EACH, readField(), reportAlways, and translator.

Referenced by ClanDb::loadXmlFile().