Last active
March 19, 2020 08:26
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
struct config_t { | |
std::string variable; | |
std::string value; | |
}; | |
struct section_t { | |
std::string name; | |
std::vector<config_t> data; | |
}; | |
class Config { | |
private: | |
std::string filename; | |
std::string trim(const std::string &str); | |
public: | |
std::string delemeter = "="; | |
std::vector<section_t> config; | |
Config(std::string filename); | |
int parse(); | |
std::string get(std::string section_name, std::string variable, std::string value = "None" ); | |
int set(std::string section_name, std::string variable, std::string value); | |
int save(); | |
}; | |
string | |
Config:: | |
trim(const string& str) | |
{ | |
const auto string_begin = str.find_first_not_of(" \t"); | |
if (string_begin == string::npos) | |
return ""; | |
const auto string_end = str.find_last_not_of(" \t"); | |
const auto string_range = string_end - string_begin + 1; | |
return str.substr(string_begin,string_range); | |
} | |
Config:: | |
Config(std::string filename) | |
{ | |
this->filename = filename; | |
} | |
int | |
Config:: | |
parse() | |
{ | |
ifstream fptr; | |
fptr.open(this->filename); | |
if (fptr.good()) { | |
string line; | |
string curr_sec = "Main"; | |
while(getline(fptr, line)) { | |
line = trim(line); | |
if (line[0] == '[' && line[line.length() - 1] == ']') { | |
curr_sec = line.substr(1,line.length() - 2); | |
//std::cout << "Section: " << new_section.name << std::endl; | |
} else { | |
size_t pos = line.find(this->delemeter); | |
if (pos == string::npos) { | |
continue; | |
} | |
string var = trim(line.substr(0,pos)), | |
val = trim(line.substr(pos + 1, line.length() )); | |
//std::cout << "Var: " << d.variable << " | Val: " << d.value << " | From: " << new_section.name << std::endl; | |
this->set(curr_sec, var, val); | |
} | |
} | |
} else { | |
return -1; | |
} | |
return 0; | |
} | |
string | |
Config:: | |
get(std::string section_name, | |
std::string variable, | |
std::string value) | |
{ | |
for(auto s : this->config) { | |
if (s.name == section_name) { | |
for (auto c: s.data) { | |
if (c.variable == variable) { | |
return c.value; | |
} | |
} | |
} | |
} | |
return value; | |
} | |
int | |
Config:: | |
set(std::string section_name, | |
std::string variable, | |
std::string value) | |
{ | |
bool varfound = false, | |
secfound = false; | |
for(auto s = this->config.begin(); s != this->config.end(); s++) { | |
if (s->name == section_name) { | |
//std::cout << "found section name: " << s->name << std::endl; | |
secfound = true; | |
for(auto c = s->data.begin(); c != s->data.end(); c++) { | |
if (c->variable == variable) { | |
varfound = true; | |
c->value = value; | |
return 0; | |
} | |
} | |
} | |
} | |
if (!varfound) { | |
if (!secfound) { | |
section_t s; | |
s.name = section_name; | |
this->config.push_back(s); | |
//std::cout << "new section added: " << s.name << std::endl; | |
} | |
for (auto s = this->config.begin(); s != this->config.end(); s++) { | |
if (s->name == section_name) { | |
//std::cout << "new variable added: " << variable << std::endl; | |
config_t c; | |
c.variable = variable; | |
c.value = value; | |
s->data.push_back(c); | |
} | |
} | |
} | |
return 0; | |
} | |
int | |
Config:: | |
save() | |
{ | |
ofstream fptr; | |
fptr.open(this->filename); | |
if (fptr.good()) { | |
for(auto s : this->config) { | |
string heading = "\n[" + s.name + "]\n"; | |
fptr.write(heading.c_str(), heading.length()); | |
for (auto c: s.data) { | |
string cstr = c.variable + this->delemeter + c.value + "\n"; | |
fptr.write(cstr.c_str(), cstr.length()); | |
} | |
} | |
fptr.close(); | |
} else { | |
return -1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment