ManaPlus
textdb.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2013-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 "resources/db/textdb.h"
23 
24 #include "configuration.h"
25 
26 #include "utils/checkutils.h"
27 
28 #include "resources/beingcommon.h"
29 
30 #include "debug.h"
31 
32 namespace
33 {
35 } // namespace
36 
38 {
39  unload();
40  logger->log1("Initializing text database...");
42  loadXmlFile(paths.getStringValue("textsPatchFile"), SkipError_true);
43  loadXmlDir("textsPatchDir", loadXmlFile)
44 }
45 
46 void TextDb::loadXmlFile(const std::string &fileName,
47  const SkipError skipError)
48 {
51  skipError);
52  XmlNodeConstPtrConst root = doc->rootNode();
53 
54  if ((root == nullptr) || !xmlNameEqual(root, "texts"))
55  {
56  delete doc;
57  return;
58  }
59 
60  for_each_xml_child_node(node, root)
61  {
62  if (xmlNameEqual(node, "include"))
63  {
64  const std::string name = XML::getProperty(node, "name", "");
65  if (!name.empty())
66  loadXmlFile(name, skipError);
67  continue;
68  }
69  else if (xmlNameEqual(node, "text"))
70  {
71  const bool show = XML::getBoolProperty(node, "show", false);
72  if (show == true)
73  {
74  if (!XmlHaveChildContent(node))
75  continue;
76 
77  std::string text = XmlChildContent(node);
78  mTexts.push_back(text);
79  }
80  }
81  }
82 
83  delete doc;
84 }
85 
87 {
88  logger->log1("Unloading text database...");
89  mTexts.clear();
90 }
91 
93 {
94  return mTexts;
95 }
96 
97 std::string TextDb::getByIndex(const int index)
98 {
99  if (index < 0 ||
100  static_cast<size_t>(index) >= mTexts.size())
101  {
102  return std::string();
103  }
104  return mTexts[index];
105 }
static void loadXmlFile(const std::string &file, const std::string &name, BadgesInfos &arr, const SkipError skipError)
Definition: badgesdb.cpp:43
#define loadXmlDir(name, function)
Definition: beingcommon.h:39
std::string getStringValue(const std::string &key) const
void log1(const char *const log_text)
Definition: logger.cpp:238
xmlNodePtr rootNode()
Definition: libxml.cpp:169
Configuration paths
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:161
Logger * logger
Definition: logger.cpp:89
void unload()
Definition: net.cpp:180
void load()
Definition: textdb.cpp:37
void loadXmlFile(const std::string &fileName, const SkipError skipError)
Definition: textdb.cpp:46
std::string getByIndex(const int index)
Definition: textdb.cpp:97
const StringVect & getTexts()
Definition: textdb.cpp:92
void unload()
Definition: textdb.cpp:86
bool getBoolProperty(const xmlNodePtr node, const char *const name, const bool def)
Definition: libxml.cpp:269
int getProperty(const xmlNodePtr node, const char *const name, int def)
Definition: libxml.cpp:174
const bool SkipError_false
Definition: skiperror.h:30
const bool SkipError_true
Definition: skiperror.h:30
bool SkipError
Definition: skiperror.h:30
std::vector< std::string > StringVect
Definition: stringvector.h:29
std::string fileName
Definition: testmain.cpp:39
const bool UseVirtFs_true
Definition: usevirtfs.h:30