1 /*
2 * This file is part of the Colobot: Gold Edition source code
3 * Copyright (C) 2001-2020, Daniel Roux, EPSITEC SA & TerranovaTeam
4 * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see http://gnu.org/licenses
18 */
19
20 /**
21 * \file common/config_file.h
22 * \brief Class for loading profile (currently for loading ini config file)
23 */
24
25 #pragma once
26
27 #include "common/singleton.h"
28
29 #include "common/logger.h"
30
31 #include <boost/property_tree/ptree.hpp>
32 #include <boost/lexical_cast.hpp>
33
34 #include <string>
35 #include <sstream>
36 #include <vector>
37 #include <stdexcept>
38
39
40 /**
41 * \class CConfigFile
42 *
43 * \brief Class for loading config file
44 *
45 */
46 class CConfigFile : public CSingleton<CConfigFile>
47 {
48 public:
49 CConfigFile();
50 virtual ~CConfigFile();
51
52 /** Set flag to force using ini file from current directory */
53 void SetUseCurrentDirectory(bool useCurrentDirectory);
54
55 /** Loads colobot.ini
56 * \return return true on success
57 */
58 bool Init();
59
60 /** Saves colobot.ini
61 * \return return true on success
62 */
63 bool Save();
64
65 /** Sets string value in section under specified key
66 * \return return true on success
67 */
68 bool SetStringProperty(std::string section, std::string key, std::string value);
69
70 /** Gets string value in section under specified key
71 * \return return true on success
72 */
73 bool GetStringProperty(std::string section, std::string key, std::string& value);
74
75 /** Sets int value in section under specified key
76 * \return return true on success
77 */
78 bool SetIntProperty(std::string section, std::string key, int value);
79
80 /** Sets bool value in section under specified key
81 * \return return true on success
82 */
83 bool SetBoolProperty(std::string section, std::string key, bool value);
84
85 /** Gets int value in section under specified key
86 * \a value will only be changed if key exists
87 * \return return true on success
88 */
89 bool GetIntProperty(std::string section, std::string key, int &value);
90
91 /** Sets float value in section under specified key
92 * \a value will only be changed if key exists
93 * \return return true on success
94 */
95 bool SetFloatProperty(std::string section, std::string key, float value);
96
97 /** Gets float value in section under specified key
98 * \a value will only be changed if key exists
99 * \return return true on success
100 */
101 bool GetFloatProperty(std::string section, std::string key, float &value);
102
103 /** Gets bool value in section under specified key
104 * \a value will only be changed if key exists
105 * \return return true on success
106 */
107 bool GetBoolProperty(std::string section, std::string key, bool &value);
108
109 /** Gets an array of values of type T in section under specified key
110 * The value separator is ','.
111 * \a array will only be changed if key exists
112 * \return return true on success
113 */
114 template<typename T>
SetArrayProperty(std::string section,std::string key,const std::vector<T> & array)115 bool SetArrayProperty(std::string section, std::string key, const std::vector<T>& array)
116 {
117 try
118 {
119 std::string convertedValue = ArrayToString(array);
120 m_propertyTree.put(section + "." + key, convertedValue);
121 m_needsSave = true;
122 }
123 catch (std::exception & e)
124 {
125 GetLogger()->Error("Error on editing config file: %s\n", e.what());
126 return false;
127 }
128 return true;
129 }
130
131 /** Sets an array of values of type T in section under specified key.
132 * The value separator is ','.
133 * \a array will only be changed if key exists
134 * \return return true on success
135 */
136 template<typename T>
GetArrayProperty(std::string section,std::string key,std::vector<T> & array)137 bool GetArrayProperty(std::string section, std::string key, std::vector<T>& array)
138 {
139 try
140 {
141 std::string readValue = m_propertyTree.get<std::string>(section + "." + key);
142 std::vector<T> convertedValue = StringToArray<T>(readValue);
143 array = std::move(convertedValue);
144 }
145 catch (std::exception & e)
146 {
147 GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing config file: %s\n", e.what());
148 return false;
149 }
150 return true;
151 }
152
153 private:
154 template<typename T>
StringToArray(const std::string & s)155 std::vector<T> StringToArray(const std::string& s)
156 {
157 std::vector<T> result;
158 std::stringstream ss(s);
159 std::string item;
160 while (std::getline(ss, item, ','))
161 {
162 result.push_back(boost::lexical_cast<T>(item));
163 }
164 return result;
165 }
166
167 template<typename T>
ArrayToString(const std::vector<T> & array)168 std::string ArrayToString(const std::vector<T> &array)
169 {
170 std::ostringstream oss;
171 if (!array.empty())
172 {
173 std::copy(array.begin(), array.end() - 1, std::ostream_iterator<T>(oss, ","));
174 oss << array.back();
175 }
176 return oss.str();
177 }
178
179 private:
180 boost::property_tree::ptree m_propertyTree;
181 bool m_needsSave;
182 bool m_useCurrentDirectory;
183 bool m_loaded;
184 };
185
186 //! Global function to get config file instance
GetConfigFile()187 inline CConfigFile & GetConfigFile()
188 {
189 return CConfigFile::GetInstance();
190 }
191