#include "../lib/config.hpp" marcelb::config::config(const string _configFilePath, const vector _necessary): configFilePath(_configFilePath), necessary(_necessary) { if(!init(_configFilePath)) { throw string("[ERROR] Init config file "); } if (!isHaveNecessary()) { throw string("[ERROR] Configuration file does not have all the required fields "); } } string marcelb::config::operator[](const string& key) { return element[key]; } void marcelb::config::update(const string& key, const string& value) { element[key] = value; update_file(key); } bool marcelb::config::init(const string _configFilePath) { ifstream configFile; configFile.open(_configFilePath, ios::in); if(!configFile) { //printf ("[CRITICAL ERROR] Nema konfiguracijske datoteke!"); return false; } else { for (string line; getline(configFile, line); ) { clearWhiteSpaces(line); if(clearComments(line) && !line.empty()) { // samo ako nije komentar string key, value; parseConfigLine(line, key, value); element[key] = value; } } } configFile.close(); return true; } void marcelb::config::print() { for(auto i : element) { cout << i.first << " " << i.second << "\n"; } } bool marcelb::config::isHaveNecessary() { bool necessaryHave = true; for (int i=0; i lines; string line; bool update = false; while (getline(configFile, line)) { size_t pos = line.find(key + "="); if (pos != string::npos) { size_t eqPos = line.find("=", pos); size_t semicolonPos = line.find(";", eqPos); if (eqPos != string::npos && semicolonPos != string::npos) { line = key + "=" + element[key] + ";"; update = true; } } lines.push_back(line); } if (!update) { line = key + "=" + element[key] + ";"; lines.push_back(line); } configFile.close(); ofstream configFileOut(configFilePath); if (!configFileOut.is_open()) { throw invalid_argument("[ERROR] Cant update config file!"); } for (const string& updatedLine : lines) { configFileOut << updatedLine << endl; } configFileOut.close(); } void marcelb::clearWhiteSpaces(string &a) { const char whitespaces[] = {' ', '\t'}; for (int i=0; i