config.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "config.h"
  2. namespace Config {
  3. std::map<std::string, std::string> configuration;
  4. bool configValid = false;
  5. bool configInitialized = false;
  6. std::string configPath = "configGUI.txt";
  7. std::vector<std::string> configKeys = {"Autofill-IP", "Default-IP", "Autofill-Username", "Default-Username", "CLI-Path", "Keyfile-Path", "Use-SSL", "SSL-Path"};
  8. } // namespace Config
  9. void Config::setupDefaultConfig() {
  10. configuration.clear();
  11. for (std::string s : configKeys)
  12. configuration.insert(std::pair<std::string, std::string>(s, ""));
  13. setValue(configKeys[0], "0");
  14. setValue(configKeys[1], "0.0.0.0");
  15. setValue(configKeys[2], "0");
  16. setValue(configKeys[3], "user");
  17. setValue(configKeys[6], "0");
  18. configInitialized = true;
  19. configValid = true;
  20. }
  21. bool Config::checkConfig() {
  22. if (!configValid || configuration.size() != configKeys.size())
  23. return false;
  24. std::string autofill_ip = getValue("Autofill-IP");
  25. if (autofill_ip != "0" && autofill_ip != "1")
  26. return false;
  27. std::string autofill_user = getValue("Autofill-Username");
  28. if (autofill_user != "0" && autofill_user != "1")
  29. return false;
  30. std::string use_ssl = getValue("Use-SSL");
  31. if (use_ssl != "0" && use_ssl != "1")
  32. return false;
  33. return true;
  34. }
  35. bool Config::loadFile() {
  36. std::string line;
  37. if (!configInitialized)
  38. setupDefaultConfig();
  39. std::ifstream ifile(configPath);
  40. if (ifile.is_open()) {
  41. while (getline(ifile, line)) {
  42. std::stringstream ss(line);
  43. std::string segment;
  44. std::vector<std::string> v;
  45. while (getline(ss, segment, '=')) {
  46. v.push_back(segment);
  47. }
  48. if (v.size() != 2) {
  49. // One line doesn't have the format *=*
  50. configValid = false;
  51. } else {
  52. configuration[v.at(0)] = v.at(1);
  53. }
  54. }
  55. configValid = true;
  56. return true;
  57. }
  58. return false;
  59. }
  60. void Config::saveFile() {
  61. std::ofstream file;
  62. if (!configInitialized)
  63. setupDefaultConfig();
  64. file.open(configPath);
  65. for (auto const &x : configuration) {
  66. file << x.first << "=" << x.second << std::endl;
  67. }
  68. file.close();
  69. }
  70. std::string Config::getValue(const std::string &key) {
  71. auto it = configuration.find(key);
  72. if (it == configuration.end()) {
  73. return "";
  74. } else {
  75. return it->second;
  76. }
  77. }
  78. void Config::setValue(const std::string &key, const std::string &value) {
  79. auto it = configuration.find(key);
  80. if (it == configuration.end()) {
  81. configuration.insert(std::pair<std::string, std::string>(key, value));
  82. } else {
  83. it->second = value;
  84. }
  85. }