You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

169 lines
3.7 KiB

#include "Config.h"
#include <fstream>
#include <iostream>
#include <any>
#include <algorithm>
#include <stdexcept>
/***************
* CONFIG *
***************/
namespace Config
{
namespace
{
std::map<std::string, ConfigValue> values; // < name, (int, float, double, string) value>
}
ConfigValue const& GetValue(std::string key)
{
return values[key];
}
template<class T>
void SaveValue(std::string /*key*/, T* /*val*/)
{
// TODO
}
void LoadConfigFile()
{
std::ifstream configFile(CONFIG_FILE_NAME);
if (!configFile.good())
{
std::cerr << "ERROR : Couldn't open config file : " << CONFIG_FILE_NAME << std::endl;
return;
}
int cptLine = 1;
for (std::string line; std::getline(configFile, line); ++cptLine)
{
// Get rid of commentaries
std::string::iterator lineIt = std::find (line.begin(), line.end(), '#');
if (lineIt == line.begin())
continue;
else if(lineIt != line.end())
line = std::string(line.begin(), lineIt);
// Removing white spaces
line.erase(std::remove_if(line.begin(), line.end(), isspace), line.end());
// Parsing line
std::string key, val;
std::string::iterator equalIt = std::find(line.begin(), line.end(), '=');
if (equalIt == line.end())
{
std::cout << "No = found on line : " << cptLine << std::endl;
continue;
}
if (equalIt == line.begin())
{
std::cout << "No key on line : " << cptLine << std::endl;
continue;
}
else
key = std::string(line.begin(), equalIt);
if (equalIt+1 == line.end())
{
std::cout << "No val on line : " << cptLine << std::endl;
continue;
}
else
val = std::string(equalIt+1, line.end());
// Registers std::map[key] = val
values[key] = ConfigValue(val);
}
}
}
/***************
* ConfigValue *
***************/
/*** Constructors ***/
ConfigValue::ConfigValue()
: pureValue("")
{ }
ConfigValue::ConfigValue(std::string s)
: pureValue(s)
{ }
ConfigValue::ConfigValue(char const* c)
: pureValue(c)
{ }
ConfigValue::ConfigValue(int i)
: pureValue(std::to_string(i))
{ }
ConfigValue::ConfigValue(double d)
: pureValue(std::to_string(d))
{ }
ConfigValue::ConfigValue(float f)
: pureValue(std::to_string(f))
{ }
/*** For conversion ***/
ConfigValue::operator float() const
{
float ret(0.0f);
try
{
std::string::size_type sz;
ret = std::stof(pureValue, &sz);
}
catch (const std::invalid_argument& ia)
{
std::cerr << "Invalid argument for ConfigValue ( " << pureValue << " ) Conversion : " << ia.what() << '\n';
}
return ret;
}
ConfigValue::operator double() const
{
double ret(0.0);
try
{
std::string::size_type sz;
ret = std::stod(pureValue, &sz);
}
catch (const std::invalid_argument& ia)
{
std::cerr << "Invalid argument for ConfigValue ( " << pureValue << " ) Conversion : " << ia.what() << '\n';
}
return ret;
}
ConfigValue::operator int() const
{
int ret(0.0);
try
{
std::string::size_type sz;
ret = std::stoi(pureValue, &sz);
}
catch (const std::invalid_argument& ia)
{
std::cerr << "Invalid argument for ConfigValue ( " << pureValue << " ) Conversion : " << ia.what() << '\n';
}
return ret;
}
ConfigValue::operator std::string() const
{
return pureValue;
}