Otclient  14/8/2020
config.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "config.h"
24 #include "resourcemanager.h"
25 #include "configmanager.h"
26 
27 #include <framework/otml/otml.h>
28 
30 {
31  m_confsDoc = OTMLDocument::create();
32  m_fileName = "";
33 }
34 
35 bool Config::load(const std::string& file)
36 {
37  m_fileName = file;
38 
39  if(!g_resources.fileExists(file))
40  return false;
41 
42  try {
43  OTMLDocumentPtr confsDoc = OTMLDocument::parse(file);
44  if(confsDoc)
45  m_confsDoc = confsDoc;
46  return true;
47  } catch(stdext::exception& e) {
48  g_logger.error(stdext::format("Unable to parse configuration file '%s': ", e.what()));
49  return false;
50  }
51 }
52 
54 {
55  if(isLoaded()) {
56  m_confsDoc = nullptr;
57  m_fileName = "";
58  return true;
59  }
60  return false;
61 }
62 
64 {
65  if(m_fileName.length() == 0)
66  return false;
67  return m_confsDoc->save(m_fileName);
68 }
69 
71 {
72  m_confsDoc->clear();
73 }
74 
75 void Config::setValue(const std::string& key, const std::string& value)
76 {
77  if(value.empty()) {
78  remove(key);
79  return;
80  }
81 
82  OTMLNodePtr child = OTMLNode::create(key, value);
83  m_confsDoc->addChild(child);
84 }
85 
86 void Config::setList(const std::string& key, const std::vector<std::string>& list)
87 {
88  remove(key);
89 
90  if(list.empty())
91  return;
92 
93  OTMLNodePtr child = OTMLNode::create(key, true);
94  for(const std::string& value : list)
95  child->writeIn(value);
96  m_confsDoc->addChild(child);
97 }
98 
99 bool Config::exists(const std::string& key)
100 {
101  return m_confsDoc->hasChildAt(key);
102 }
103 
104 std::string Config::getValue(const std::string& key)
105 {
106  OTMLNodePtr child = m_confsDoc->get(key);
107  if(child)
108  return child->value();
109  else
110  return "";
111 }
112 
113 std::vector<std::string> Config::getList(const std::string& key)
114 {
115  std::vector<std::string> list;
116  OTMLNodePtr child = m_confsDoc->get(key);
117  if(child) {
118  for(const OTMLNodePtr& subchild : child->children())
119  list.push_back(subchild->value());
120  }
121  return list;
122 }
123 
124 void Config::remove(const std::string& key)
125 {
126  OTMLNodePtr child = m_confsDoc->get(key);
127  if(child)
128  m_confsDoc->removeChild(child);
129 }
130 
131 void Config::setNode(const std::string& key, const OTMLNodePtr& node)
132 {
133  remove(key);
134  mergeNode(key, node);
135 }
136 
137 void Config::mergeNode(const std::string& key, const OTMLNodePtr& node)
138 {
139  OTMLNodePtr clone = node->clone();
140  node->setTag(key);
141  node->setUnique(true);
142  m_confsDoc->addChild(node);
143 }
144 
145 OTMLNodePtr Config::getNode(const std::string& key)
146 {
147  return m_confsDoc->get(key);
148 }
149 
151 {
152  return !m_fileName.empty() && m_confsDoc;
153 }
154 
155 std::string Config::getFileName()
156 {
157  return m_fileName;
158 }
Config::isLoaded
bool isLoaded()
Definition: config.cpp:150
Config::Config
Config()
Definition: config.cpp:29
OTMLNode::writeIn
void writeIn(const T &v)
Definition: otmlnode.h:170
OTMLNode::setTag
void setTag(const std::string &tag)
Definition: otmlnode.h:50
Config::load
bool load(const std::string &file)
Definition: config.cpp:35
OTMLDocument::create
static OTMLDocumentPtr create()
Create a new OTML document for filling it with nodes.
Definition: otmldocument.cpp:29
otml.h
configmanager.h
Config::save
bool save()
Definition: config.cpp:63
Config::setList
void setList(const std::string &key, const std::vector< std::string > &list)
Definition: config.cpp:86
OTMLDocument::parse
static OTMLDocumentPtr parse(const std::string &fileName)
Parse OTML from a file.
Definition: otmldocument.cpp:36
OTMLDocument::save
bool save(const std::string &fileName)
Save this document to a file.
Definition: otmldocument.cpp:58
OTMLNode::removeChild
bool removeChild(const OTMLNodePtr &oldChild)
Definition: otmlnode.cpp:124
resourcemanager.h
Logger::error
void error(const std::string &what)
Definition: logger.h:54
Config::setNode
void setNode(const std::string &key, const OTMLNodePtr &node)
Definition: config.cpp:131
OTMLNode::children
OTMLNodeList children()
Definition: otmlnode.cpp:170
stdext::format
std::string format()
Definition: format.h:82
OTMLNode::addChild
void addChild(const OTMLNodePtr &newChild)
Definition: otmlnode.cpp:91
Config::getList
std::vector< std::string > getList(const std::string &key)
Definition: config.cpp:113
Config::clear
void clear()
Definition: config.cpp:70
g_resources
ResourceManager g_resources
Definition: resourcemanager.cpp:32
ResourceManager::fileExists
bool fileExists(const std::string &fileName)
Definition: resourcemanager.cpp:157
g_logger
Logger g_logger
Definition: logger.cpp:35
OTMLNode::setUnique
void setUnique(bool unique)
Definition: otmlnode.h:53
OTMLNode::value
T value()
Definition: otmlnode.h:122
Config::mergeNode
void mergeNode(const std::string &key, const OTMLNodePtr &node)
Definition: config.cpp:137
OTMLNode::clone
OTMLNodePtr clone()
Definition: otmlnode.cpp:179
OTMLNode::get
OTMLNodePtr get(const std::string &childTag)
Definition: otmlnode.cpp:54
Config::getNode
OTMLNodePtr getNode(const std::string &key)
Definition: config.cpp:145
Config::getFileName
std::string getFileName()
Definition: config.cpp:155
stdext::exception::what
virtual const char * what() const
Definition: exception.h:37
Config::exists
bool exists(const std::string &key)
Definition: config.cpp:99
stdext::shared_object_ptr< OTMLDocument >
Config::unload
bool unload()
Definition: config.cpp:53
OTMLNode::hasChildAt
bool hasChildAt(const std::string &childTag)
Definition: otmlnode.h:47
config.h
Config::setValue
void setValue(const std::string &key, const std::string &value)
Definition: config.cpp:75
Config::remove
void remove(const std::string &key)
Definition: config.cpp:124
Config::getValue
std::string getValue(const std::string &key)
Definition: config.cpp:104
OTMLNode::clear
void clear()
Definition: otmlnode.cpp:165
stdext::exception
Definition: exception.h:31
OTMLNode::create
static OTMLNodePtr create(std::string tag="", bool unique=false)
Definition: otmlnode.cpp:27