53 lines
1.2 KiB
C++
53 lines
1.2 KiB
C++
#include "config.h"
|
|
|
|
#include <fstream>
|
|
#include <iostream>
|
|
|
|
bool Config::load(const char* from) {
|
|
std::ifstream file(from);
|
|
if(!file)
|
|
return false;
|
|
|
|
std::string line, currentCategory;
|
|
while(std::getline(file, line)) {
|
|
//category
|
|
if(line[0] == '[')
|
|
currentCategory = line.substr(1, line.length() - 2);
|
|
|
|
//key/value
|
|
if(line.find('=') != std::string::npos) {
|
|
std::string key, value;
|
|
key = line.substr(0, line.find('='));
|
|
value = line.substr(line.find('=') + 1, line.length());
|
|
|
|
categories[currentCategory][key] = value;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void Config::save(const char* to) {
|
|
std::ofstream file(to);
|
|
if(!file)
|
|
return;
|
|
|
|
std::string currentCategory;
|
|
for(const auto& itr : categories) {
|
|
file << "[" << itr.first << "]\n";
|
|
|
|
for(const auto& itr2 : itr.second) {
|
|
file << itr2.first << "=" << itr2.second << "\n";
|
|
}
|
|
|
|
file << "\n";
|
|
}
|
|
}
|
|
|
|
void Config::set(const char* category, const char* key, const std::string& value) {
|
|
categories[category][key] = value;
|
|
}
|
|
|
|
std::string Config::get(const char* category, const char* key) {
|
|
return categories[category][key];
|
|
}
|