parent
0b66d839be
commit
097143ecf7
9 changed files with 172 additions and 6 deletions
@ -0,0 +1,72 @@ |
|||||||
|
#include <config/ConfigParser.hpp> |
||||||
|
#include <fstream> |
||||||
|
|
||||||
|
namespace groove { |
||||||
|
namespace config { |
||||||
|
|
||||||
|
void ConfigValue::set(std::string &value) { |
||||||
|
strVal = value; |
||||||
|
boolVal = value == "true"; |
||||||
|
try { |
||||||
|
intVal = std::stoi(value); |
||||||
|
} catch (std::invalid_argument) { |
||||||
|
intVal = 0; |
||||||
|
} |
||||||
|
try { |
||||||
|
floatVal = std::stof(value); |
||||||
|
} catch (std::invalid_argument) { |
||||||
|
floatVal = 0.0f; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void ConfigParser::parse() { |
||||||
|
std::ifstream file(this->filename.c_str()); |
||||||
|
if(file.is_open()) { |
||||||
|
while(!file.eof()) { |
||||||
|
std::string line; |
||||||
|
std::getline(file, line); |
||||||
|
if (line == "" || line.at(0) == '#') |
||||||
|
continue; |
||||||
|
std::vector<std::string> pair = std::explode(line,'='); |
||||||
|
std::string key = pair.at(0); |
||||||
|
std::string value = pair.at(1); |
||||||
|
std::trim(key); |
||||||
|
std::trim(value); |
||||||
|
this->params.emplace(key, ConfigValue(value)); |
||||||
|
} |
||||||
|
} |
||||||
|
else { |
||||||
|
std::cerr << "Could not load config file." << std::endl; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
template<> |
||||||
|
std::string ConfigParser::get<std::string>(const std::string& key) { |
||||||
|
if (this->params.find(key) == this->params.end()) |
||||||
|
return ""; |
||||||
|
return this->params.at(key).strVal; |
||||||
|
} |
||||||
|
|
||||||
|
template<> |
||||||
|
int ConfigParser::get<int>(const std::string& key) { |
||||||
|
if (this->params.find(key) == this->params.end()) |
||||||
|
return 0; |
||||||
|
return this->params.at(key).intVal; |
||||||
|
} |
||||||
|
|
||||||
|
template<> |
||||||
|
float ConfigParser::get<float>(const std::string& key) { |
||||||
|
if (this->params.find(key) == this->params.end()) |
||||||
|
return 0.0f; |
||||||
|
return this->params.at(key).floatVal; |
||||||
|
} |
||||||
|
|
||||||
|
template<> |
||||||
|
bool ConfigParser::get<bool>(const std::string& key) { |
||||||
|
if (this->params.find(key) == this->params.end()) |
||||||
|
return false; |
||||||
|
return this->params.at(key).boolVal; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
} |
@ -0,0 +1,79 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <string> |
||||||
|
#include <unordered_map> |
||||||
|
#include <vector> |
||||||
|
#include <sstream> |
||||||
|
#include <algorithm> |
||||||
|
#include <iostream> |
||||||
|
#include <pwd.h> |
||||||
|
#include <unistd.h> |
||||||
|
#include <sys/types.h> |
||||||
|
|
||||||
|
namespace std { |
||||||
|
static inline std::vector<std::string> explode(const std::string &str, char delimiter) { |
||||||
|
std::vector<std::string> tokens; |
||||||
|
std::stringstream inStream(str); |
||||||
|
std::string tempString; |
||||||
|
|
||||||
|
while (std::getline(inStream, tempString, delimiter)) |
||||||
|
tokens.push_back(tempString); |
||||||
|
|
||||||
|
return tokens; |
||||||
|
} |
||||||
|
|
||||||
|
static inline void ltrim(std::string &string) { |
||||||
|
string.erase(string.begin(), std::find_if(string.begin(), string.end(), [](auto c){return !std::isspace(c);})); |
||||||
|
} |
||||||
|
|
||||||
|
static inline void rtrim(std::string &string) { |
||||||
|
string.erase(std::find_if(string.rbegin(), string.rend(), [](auto c){return !std::isspace(c);}).base(), string.end()); |
||||||
|
} |
||||||
|
|
||||||
|
static inline void trim(std::string &string) { |
||||||
|
rtrim(string); |
||||||
|
ltrim(string); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
namespace groove { |
||||||
|
namespace config { |
||||||
|
|
||||||
|
struct ConfigValue { |
||||||
|
std::string strVal; |
||||||
|
int intVal; |
||||||
|
float floatVal; |
||||||
|
bool boolVal; |
||||||
|
|
||||||
|
ConfigValue(std::string &value) { |
||||||
|
set(value); |
||||||
|
} |
||||||
|
|
||||||
|
void set(std::string &value); |
||||||
|
}; |
||||||
|
|
||||||
|
class ConfigParser { |
||||||
|
private: |
||||||
|
std::unordered_map<std::string, ConfigValue> params; |
||||||
|
std::string filename; |
||||||
|
|
||||||
|
void parse(); |
||||||
|
public: |
||||||
|
ConfigParser() { |
||||||
|
this->filename = getenv("HOME"); |
||||||
|
this->filename += "/.grooverc"; |
||||||
|
this->parse(); |
||||||
|
} |
||||||
|
|
||||||
|
template <typename T> |
||||||
|
T get(const std::string& key) {} |
||||||
|
|
||||||
|
template<typename T> |
||||||
|
void set(std::string key, T value) { |
||||||
|
std::string newValue = std::to_string(value); |
||||||
|
this->params.at(key).set(newValue); |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue