You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.h 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * =====================================================================================
  3. *
  4. * Filename: config.h
  5. *
  6. * Description: Processes command line arguments and config files in header only library
  7. *
  8. * Version: 1.0
  9. * Created: 03/26/2014 09:30:55 PM
  10. * Revision: none
  11. * Compiler: gcc
  12. *
  13. * Author: Michael Porter
  14. *
  15. * =====================================================================================
  16. */
  17. /*
  18. EXAMPLE CONFIG:
  19. ;this is a comment
  20. key = value
  21. integer = 69
  22. float = 3.1415926
  23. bool = 1
  24. anotherbool = false
  25. hierarchything {
  26. subkey = "value as a string"
  27. }
  28. */
  29. #pragma once
  30. #include <string>
  31. #include <map>
  32. #include <sstream>
  33. #include <fstream>
  34. #include <iostream>
  35. #include <vector>
  36. //Config stores values
  37. namespace sol {
  38. class state;
  39. }
  40. class Config {
  41. public:
  42. bool loadConfigFromFile(const std::string& fileName) {
  43. std::ifstream file(fileName);
  44. if (!file) return false;
  45. std::string line;
  46. std::vector<std::string> hierarchyPosition;
  47. while (std::getline(file, line)) {
  48. auto temp = line; //copy line for safety
  49. trimString(temp);
  50. if (temp.find_first_of(";") != std::string::npos) { //contains comment
  51. temp.erase(temp.find_first_of(";"), temp.size() - 1);
  52. }
  53. if (!temp.empty()) { //it's not empty
  54. if (temp[0] != '{' && temp.find_first_of("{") != std::string::npos) {
  55. std::string key = temp.substr(0, temp.find_first_of("{"));
  56. trimString(key);
  57. hierarchyPosition.push_back(key);
  58. } else if (temp[0] == '}') {
  59. hierarchyPosition.pop_back();
  60. } else if (temp[0] != '=' && temp.find_first_of("=") != std::string::npos) { //it's assigning something!
  61. std::string key = temp.substr(0, temp.find_first_of("="));
  62. trimString(key);
  63. std::string value = temp.substr(temp.find_first_of("=") + 1, temp.size() - 1);
  64. trimString(value);
  65. if (!hierarchyPosition.empty()) {
  66. std::string tempKey = key;
  67. key.clear();
  68. for (auto& i : hierarchyPosition) {
  69. key.append(i);
  70. key.append(".");
  71. }
  72. key.append(tempKey);
  73. }
  74. values[key] = value;
  75. }
  76. }
  77. }
  78. return true;
  79. }
  80. template <typename T>
  81. T getValue(const std::string& key, const T& fallback) {
  82. if (values.find(key) != values.end()) {
  83. stream.clear();
  84. stream << values[key];
  85. T value;
  86. stream >> value;
  87. return value;
  88. }
  89. return fallback;
  90. }
  91. template <typename T>
  92. void setValue(const std::string& key, const T& value) {
  93. stream.clear();
  94. stream << value;
  95. stream >> values[key];
  96. }
  97. void addArgumentRule(const std::string& flag, const std::string& key) { //sets a value to be overriden by command line flag
  98. commandRules[flag] = key;
  99. }
  100. bool loadConfigFromArguments(const int& argc, char* argv[]) {
  101. for (int i = 1; i < argc; i += 2) {
  102. auto flag = std::string(argv[i]);
  103. auto value = std::string(argv[i+1]);
  104. if (commandRules.find(flag) != commandRules.end()) {
  105. values[commandRules[flag]] = value;
  106. } else return false;
  107. }
  108. return true;
  109. }
  110. void printConfig() {
  111. for (auto& i : values) {
  112. std::cout << i.first << " = " << i.second << std::endl;
  113. }
  114. }
  115. static void registerScriptInterface(sol::state& lua);
  116. protected:
  117. std::map<std::string, std::string> values;
  118. std::map<std::string, std::string> commandRules;
  119. std::stringstream stream;
  120. void trimString(std::string& string) {
  121. size_t end = string.find_last_not_of(" \t\n\v\f\r");
  122. if ( end != std::string::npos )
  123. string.resize( end + 1 );
  124. size_t start = string.find_first_not_of(" \t\n\v\f\r");
  125. if ( start != std::string::npos )
  126. string = string.substr( start );
  127. }
  128. };