Add ability to use arrays in colobot.ini

pyro-refactor
MrSimbax 2020-07-18 18:06:14 +02:00
parent a0635ae400
commit 69d2d39c36
3 changed files with 103 additions and 0 deletions

View File

@ -26,9 +26,15 @@
#include "common/singleton.h"
#include "common/logger.h"
#include <boost/property_tree/ptree.hpp>
#include <boost/lexical_cast.hpp>
#include <string>
#include <sstream>
#include <vector>
#include <stdexcept>
/**
@ -100,6 +106,76 @@ public:
*/
bool GetBoolProperty(std::string section, std::string key, bool &value);
/** Gets an array of values of type T in section under specified key
* The value separator is ','.
* \a array will only be changed if key exists
* \return return true on success
*/
template<typename T>
bool SetArrayProperty(std::string section, std::string key, const std::vector<T>& array)
{
try
{
std::string convertedValue = ArrayToString(array);
m_propertyTree.put(section + "." + key, convertedValue);
m_needsSave = true;
}
catch (std::exception & e)
{
GetLogger()->Error("Error on editing config file: %s\n", e.what());
return false;
}
return true;
}
/** Sets an array of values of type T in section under specified key.
* The value separator is ','.
* \a array will only be changed if key exists
* \return return true on success
*/
template<typename T>
bool GetArrayProperty(std::string section, std::string key, std::vector<T>& array)
{
try
{
std::string readValue = m_propertyTree.get<std::string>(section + "." + key);
std::vector<T> convertedValue = StringToArray<T>(readValue);
array = std::move(convertedValue);
}
catch (std::exception & e)
{
GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing config file: %s\n", e.what());
return false;
}
return true;
}
private:
template<typename T>
std::vector<T> StringToArray(const std::string& s)
{
std::vector<T> result;
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, ','))
{
result.push_back(boost::lexical_cast<T>(item));
}
return result;
}
template<typename T>
std::string ArrayToString(const std::vector<T> &array)
{
std::ostringstream oss;
if (!array.empty())
{
std::copy(array.begin(), array.end() - 1, std::ostream_iterator<T>(oss, ","));
oss << array.back();
}
return oss.str();
}
private:
boost::property_tree::ptree m_propertyTree;
bool m_needsSave;

View File

@ -6,3 +6,8 @@ string_value=Hello world
[test_int]
int_value=42
[test_array]
string_array=AAA,Hello world,Gold Edition
int_array=2,3,1
bool_array=1,0,1

View File

@ -52,3 +52,25 @@ TEST_F(CConfigFileTest, ReadTest)
ASSERT_TRUE(m_configFile.GetFloatProperty("test_float", "float_value", float_value));
ASSERT_FLOAT_EQ(1.5, float_value);
}
TEST_F(CConfigFileTest, ReadArrayTest)
{
m_configFile.SetUseCurrentDirectory(true);
ASSERT_TRUE(m_configFile.Init()); // load colobot.ini file
std::vector<std::string> expected_string_values = { "AAA", "Hello world", "Gold Edition" };
std::vector<std::string> string_values;
ASSERT_TRUE(m_configFile.GetArrayProperty("test_array", "string_array", string_values));
ASSERT_EQ(expected_string_values, string_values);
std::vector<int> expected_int_values = { 2, 3, 1 };
std::vector<int> int_values;
ASSERT_TRUE(m_configFile.GetArrayProperty("test_array", "int_array", int_values));
ASSERT_EQ(expected_int_values, int_values);
std::vector<bool> expected_bool_values = { true, false, true };
std::vector<bool> bool_values;
ASSERT_TRUE(m_configFile.GetArrayProperty("test_array", "bool_array", bool_values));
ASSERT_EQ(expected_bool_values, bool_values);
}