Archived
1
Fork 0
This repository has been archived on 2025-04-12. You can view files and clone it, but cannot push or open issues or pull requests.
graph/src/config.cpp

54 lines
1.2 KiB
C++
Raw Normal View History

2018-11-08 12:51:32 -05:00
#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(auto itr : categories) {
file << "[" << itr.first << "]\n";
for(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];
}