ManaPlus
languagedb.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 
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 {
34  std::string mDefaultIcon;
35  std::string mDefaultPo;
36  std::map<int, std::string> mIcons;
37  std::map<int, std::string> mPo;
38 } // namespace
39 
41 {
42  unload();
43  logger->log1("Initializing languages database...");
45  loadXmlFile(paths.getStringValue("languagesPatchFile"), SkipError_true);
46  loadXmlDir("languagesPatchDir", loadXmlFile)
47 }
48 
49 void LanguageDb::loadXmlFile(const std::string &fileName,
50  const SkipError skipError)
51 {
54  skipError);
55  XmlNodeConstPtrConst root = doc->rootNode();
56 
57  if ((root == nullptr) || !xmlNameEqual(root, "languages"))
58  {
59  delete doc;
60  return;
61  }
62 
63  for_each_xml_child_node(node, root)
64  {
65  if (xmlNameEqual(node, "include"))
66  {
67  const std::string name = XML::getProperty(node, "name", "");
68  if (!name.empty())
69  loadXmlFile(name, skipError);
70  continue;
71  }
72  else if (xmlNameEqual(node, "lang"))
73  {
74  const int id = XML::getProperty(node, "id", -1);
75  if (id < 0)
76  {
77  reportAlways("Missing lang id")
78  continue;
79  }
80  const std::string icon = XML::getProperty(node, "icon", "");
81  const std::string po = XML::getProperty(node, "po", "");
82  if (icon.empty())
83  {
84  reportAlways("LanguageDb: empty icon field")
85  }
86  else
87  {
88  mIcons[id] = icon;
89  }
90  if (po.empty())
91  {
92  reportAlways("LanguageDb: empty po field")
93  }
94  else
95  {
96  mPo[id] = po;
97  }
98  }
99  }
100 
101  delete doc;
102 }
103 
105 {
106  logger->log1("Unloading languages database...");
107  mIcons.clear();
108  mPo.clear();
109 }
110 
111 const std::string &LanguageDb::getIcon(const int id)
112 {
113  std::map<int, std::string>::const_iterator it = mIcons.find(id);
114  if (it == mIcons.end())
115  return mDefaultIcon;
116  return (*it).second;
117 }
118 
119 const std::string &LanguageDb::getPo(const int id)
120 {
121  std::map<int, std::string>::const_iterator it = mPo.find(id);
122  if (it == mPo.end())
123  return mDefaultPo;
124  return (*it).second;
125 }
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
#define reportAlways(...)
Definition: checkutils.h:253
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
const std::string & getPo(const int id)
Definition: languagedb.cpp:119
void load()
Definition: languagedb.cpp:40
void loadXmlFile(const std::string &fileName, const SkipError skipError)
Definition: languagedb.cpp:49
void unload()
Definition: languagedb.cpp:104
const std::string & getIcon(const int id)
Definition: languagedb.cpp:111
void unload()
Definition: net.cpp:180
int getProperty(const xmlNodePtr node, const char *const name, int def)
Definition: libxml.cpp:174
std::map< int, std::string > mIcons
Definition: languagedb.cpp:36
std::map< int, std::string > mPo
Definition: languagedb.cpp:37
const bool SkipError_false
Definition: skiperror.h:30
const bool SkipError_true
Definition: skiperror.h:30
bool SkipError
Definition: skiperror.h:30
std::string fileName
Definition: testmain.cpp:39
const bool UseVirtFs_true
Definition: usevirtfs.h:30