ManaPlus
Functions
xmlutils.cpp File Reference

(986a3bf)

#include "utils/xmlutils.h"
#include "logger.h"
#include "utils/xml.h"
#include "debug.h"

Go to the source code of this file.

Functions

void readXmlIntVector (const std::string &fileName, const std::string &rootName, const std::string &sectionName, const std::string &itemName, const std::string &attributeName, std::vector< int > &arr, const SkipError skipError)
 
void readXmlStringMap (const std::string &fileName, const std::string &rootName, const std::string &sectionName, const std::string &itemName, const std::string &attributeKeyName, const std::string &attributeValueName, std::map< std::string, std::string > &arr, const SkipError skipError)
 
void readXmlIntMap (const std::string &fileName, const std::string &rootName, const std::string &sectionName, const std::string &itemName, const std::string &attributeKeyName, const std::string &attributeValueName, std::map< int32_t, int32_t > &arr, const SkipError skipError)
 

Function Documentation

◆ readXmlIntMap()

void readXmlIntMap ( const std::string &  fileName,
const std::string &  rootName,
const std::string &  sectionName,
const std::string &  itemName,
const std::string &  attributeKeyName,
const std::string &  attributeValueName,
std::map< int32_t, int32_t > &  arr,
const SkipError  skipError 
)

Definition at line 134 of file xmlutils.cpp.

142 {
143  XML::Document doc(fileName, UseVirtFs_true, skipError);
144  XmlNodeConstPtrConst rootNode = doc.rootNode();
145 
146  if (rootNode == nullptr || !xmlNameEqual(rootNode, rootName.c_str()))
147  {
148  logger->log("Error while loading %s!", fileName.c_str());
149  return;
150  }
151 
152  for_each_xml_child_node(sectionNode, rootNode)
153  {
154  if (!xmlNameEqual(sectionNode, sectionName.c_str()))
155  continue;
156  for_each_xml_child_node(childNode, sectionNode)
157  {
158  if (xmlNameEqual(childNode, itemName.c_str()))
159  {
160  const std::string key = XML::getProperty(childNode,
161  attributeKeyName.c_str(), "");
162  if (key.empty())
163  continue;
164  const int32_t val = XML::getProperty(childNode,
165  attributeValueName.c_str(), 0);
166  arr[atoi(key.c_str())] = val;
167  }
168  else if (xmlNameEqual(childNode, "include"))
169  {
170  const std::string name = XML::getProperty(
171  childNode, "name", "");
172  if (!name.empty())
173  {
174  readXmlIntMap(name,
175  rootName,
176  sectionName,
177  itemName,
178  attributeKeyName,
179  attributeValueName,
180  arr,
181  skipError);
182  }
183  }
184  }
185  }
186 }
void log(const char *const log_text,...)
Definition: logger.cpp:269
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:161
Logger * logger
Definition: logger.cpp:89
int getProperty(const xmlNodePtr node, const char *const name, int def)
Definition: libxml.cpp:174
std::string fileName
Definition: testmain.cpp:39
const bool UseVirtFs_true
Definition: usevirtfs.h:30
void readXmlIntMap(const std::string &fileName, const std::string &rootName, const std::string &sectionName, const std::string &itemName, const std::string &attributeKeyName, const std::string &attributeValueName, std::map< int32_t, int32_t > &arr, const SkipError skipError)
Definition: xmlutils.cpp:134

References fileName, for_each_xml_child_node, XML::getProperty(), Logger::log(), logger, XML::Document::rootNode(), and UseVirtFs_true.

Referenced by NetworkDb::loadXmlFile().

◆ readXmlIntVector()

void readXmlIntVector ( const std::string &  fileName,
const std::string &  rootName,
const std::string &  sectionName,
const std::string &  itemName,
const std::string &  attributeName,
std::vector< int > &  arr,
const SkipError  skipError 
)

Definition at line 30 of file xmlutils.cpp.

37 {
38  XML::Document doc(fileName, UseVirtFs_true, skipError);
39  XmlNodeConstPtrConst rootNode = doc.rootNode();
40 
41  if (rootNode == nullptr || !xmlNameEqual(rootNode, rootName.c_str()))
42  {
43  logger->log("Error while loading %s!", fileName.c_str());
44  return;
45  }
46 
47  for_each_xml_child_node(sectionNode, rootNode)
48  {
49  if (!xmlNameEqual(sectionNode, sectionName.c_str()))
50  continue;
51  for_each_xml_child_node(childNode, sectionNode)
52  {
53  if (xmlNameEqual(childNode, itemName.c_str()))
54  {
55  const int val = XML::getProperty(childNode,
56  attributeName.c_str(), -1);
57  if (val == -1)
58  continue;
59  arr.push_back(val);
60  }
61  else if (xmlNameEqual(childNode, "include"))
62  {
63  const std::string name = XML::getProperty(
64  childNode, "name", "");
65  if (!name.empty())
66  {
67  readXmlIntVector(name,
68  rootName,
69  sectionName,
70  itemName,
71  attributeName,
72  arr,
73  skipError);
74  }
75  }
76  }
77  }
78 }
void readXmlIntVector(const std::string &fileName, const std::string &rootName, const std::string &sectionName, const std::string &itemName, const std::string &attributeName, std::vector< int > &arr, const SkipError skipError)
Definition: xmlutils.cpp:30

References fileName, for_each_xml_child_node, XML::getProperty(), Logger::log(), logger, XML::Document::rootNode(), and UseVirtFs_true.

Referenced by loadDB(), and NetworkDb::loadXmlFile().

◆ readXmlStringMap()

void readXmlStringMap ( const std::string &  fileName,
const std::string &  rootName,
const std::string &  sectionName,
const std::string &  itemName,
const std::string &  attributeKeyName,
const std::string &  attributeValueName,
std::map< std::string, std::string > &  arr,
const SkipError  skipError 
)

Definition at line 80 of file xmlutils.cpp.

88 {
89  XML::Document doc(fileName, UseVirtFs_true, skipError);
90  XmlNodeConstPtrConst rootNode = doc.rootNode();
91 
92  if (rootNode == nullptr || !xmlNameEqual(rootNode, rootName.c_str()))
93  {
94  logger->log("Error while loading %s!", fileName.c_str());
95  return;
96  }
97 
98  for_each_xml_child_node(sectionNode, rootNode)
99  {
100  if (!xmlNameEqual(sectionNode, sectionName.c_str()))
101  continue;
102  for_each_xml_child_node(childNode, sectionNode)
103  {
104  if (xmlNameEqual(childNode, itemName.c_str()))
105  {
106  const std::string key = XML::getProperty(childNode,
107  attributeKeyName.c_str(), "");
108  if (key.empty())
109  continue;
110  const std::string val = XML::getProperty(childNode,
111  attributeValueName.c_str(), "");
112  arr[key] = val;
113  }
114  else if (xmlNameEqual(childNode, "include"))
115  {
116  const std::string name = XML::getProperty(
117  childNode, "name", "");
118  if (!name.empty())
119  {
120  readXmlStringMap(name,
121  rootName,
122  sectionName,
123  itemName,
124  attributeKeyName,
125  attributeValueName,
126  arr,
127  skipError);
128  }
129  }
130  }
131  }
132 }
void readXmlStringMap(const std::string &fileName, const std::string &rootName, const std::string &sectionName, const std::string &itemName, const std::string &attributeKeyName, const std::string &attributeValueName, std::map< std::string, std::string > &arr, const SkipError skipError)
Definition: xmlutils.cpp:80

References fileName, for_each_xml_child_node, XML::getProperty(), Logger::log(), logger, XML::Document::rootNode(), and UseVirtFs_true.

Referenced by loadXmlFile().