12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- #include "config.h"
- #include <QDebug>
- namespace Config {
- std::map<std::string, std::string> configuration;
- bool configValid = false;
- bool configInitialized = false;
- std::string configPath = "configGUI.txt";
- std::vector<std::string> configKeys = {"Autofill-IP", "Default-IP", "Autofill-Username", "Default-Username", "CLI-Path", "Keyfile-Path"};
- } // namespace Config
- void Config::setupDefaultConfig() {
- configuration.clear();
- for (std::string s : configKeys)
- configuration.insert(std::pair<std::string, std::string>(s, ""));
- setValue(configKeys[0], "0");
- setValue(configKeys[1], "0.0.0.0");
- setValue(configKeys[2], "0");
- setValue(configKeys[3], "user");
- configInitialized = true;
- configValid = true;
- }
- bool Config::checkConfig() {
- if (!configValid || configuration.size() != configKeys.size())
- return false;
- std::string autofill_ip = getValue("Autofill-IP");
- if (autofill_ip != "0" && autofill_ip != "1")
- return false;
- std::string autofill_user = getValue("Autofill-Username");
- if (autofill_user != "0" && autofill_user != "1")
- return false;
- return true;
- }
- bool Config::loadFile() {
- std::string line;
- if (!configInitialized)
- setupDefaultConfig();
- std::ifstream ifile(configPath);
- if (ifile.is_open()) {
- while (getline(ifile, line)) {
- std::stringstream ss(line);
- std::string segment;
- std::vector<std::string> v;
- while (getline(ss, segment, '=')) {
- v.push_back(segment);
- }
- if (v.size() != 2) {
- // One line doesn't have the format *=*
- configValid = false;
- } else {
- configuration[v.at(0)] = v.at(1);
- }
- }
- configValid = true;
- return true;
- }
- return false;
- }
- void Config::saveFile() {
- std::ofstream file;
- if (!configInitialized)
- setupDefaultConfig();
- file.open(configPath);
- for (auto const &x : configuration) {
- file << x.first << "=" << x.second << std::endl;
- }
- file.close();
- }
- std::string Config::getValue(const std::string &key) {
- auto it = configuration.find(key);
- if (it == configuration.end()) {
- return "";
- } else {
- return it->second;
- }
- }
- void Config::setValue(const std::string &key, const std::string &value) {
- auto it = configuration.find(key);
- if (it == configuration.end()) {
- configuration.insert(std::pair<std::string, std::string>(key, value));
- } else {
- it->second = value;
- }
- }
|