|
| 1 | +#include <iostream> |
| 2 | +#include <string> |
| 3 | +#include <algorithm> |
| 4 | +#include <vector> |
| 5 | +#include "headers/fileio.hpp" |
| 6 | + |
| 7 | +#define CONFIG_FILE "config.cfg" |
| 8 | + |
| 9 | +std::vector<std::pair<std::string, std::string>> pharseConfig() { |
| 10 | + std::vector<std::string> fileContents = readFile(CONFIG_FILE); |
| 11 | + std::vector<std::pair<std::string, std::string>> config; |
| 12 | + for (int i = 0; i < fileContents.size(); i++) { |
| 13 | + if (fileContents[i][0] == '#') continue; // comments |
| 14 | + if (fileContents[i][0] == '\n') continue; // empty lines |
| 15 | + if (fileContents[i][0] == '\r') continue; // empty lines |
| 16 | + if (fileContents[i].find("=") == std::string::npos) continue; // no = |
| 17 | + fileContents[i].erase(std::remove(fileContents[i].begin(), fileContents[i].end(), ' '), fileContents[i].end()); |
| 18 | + std::string key = fileContents[i].substr(0, fileContents[i].find("=")); |
| 19 | + std::string value = fileContents[i].substr(fileContents[i].find("=") + 1); |
| 20 | + config.push_back(std::make_pair(key, value)); |
| 21 | + } |
| 22 | + std::cout << "Config file loaded" << std::endl; |
| 23 | + // print keys loaded |
| 24 | + std::string keys; |
| 25 | + for (int i = 0; i < config.size(); i++) { |
| 26 | + keys += config[i].first + ", "; |
| 27 | + } |
| 28 | + std::cout << "Loaded Keys: " << keys << std::endl; |
| 29 | + return config; |
| 30 | +} |
| 31 | + |
| 32 | +bool checkKey(std::string key) { |
| 33 | + std::vector<std::pair<std::string, std::string>> config = pharseConfig(); |
| 34 | + for (int i = 0; i < config.size(); i++) { |
| 35 | + if (config[i].first == key) return true; |
| 36 | + } |
| 37 | + return false; |
| 38 | +} |
| 39 | + |
| 40 | +std::string getValue(std::string key) { |
| 41 | + std::vector<std::pair<std::string, std::string>> config = pharseConfig(); |
| 42 | + for (int i = 0; i < config.size(); i++) { |
| 43 | + if (config[i].first == key) return config[i].second; |
| 44 | + } |
| 45 | + return ""; |
| 46 | +} |
| 47 | + |
0 commit comments