ConfigFileParser.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2015 Patrick Brosi
  2. // info@patrickbrosi.de
  3. #pragma once
  4. #include <exception>
  5. #include <fstream>
  6. #include <sstream>
  7. #include <string>
  8. #include <unordered_map>
  9. #include <vector>
  10. namespace configparser {
  11. struct Val {
  12. std::string val;
  13. size_t line;
  14. size_t pos;
  15. std::string file;
  16. };
  17. typedef std::string Key;
  18. typedef std::string Sec;
  19. typedef std::unordered_map<std::string, Val> KeyVals;
  20. enum State {
  21. NONE,
  22. IN_HEAD,
  23. IN_INC,
  24. IN_HEAD_KEY,
  25. IN_HEAD_AW_COM_OR_END,
  26. IN_KEY_VAL_KEY,
  27. IN_KEY_VAL_VAL,
  28. AW_KEY_VAL_VAL,
  29. IN_KEY_VAL_VAL_HANG,
  30. IN_KEY_VAL_VAL_HANG_END
  31. };
  32. class ParseFileExc : public std::exception {
  33. public:
  34. ParseFileExc(const std::string file) : _file(file) {
  35. std::stringstream ss;
  36. ss << _file << ": Could not open file.";
  37. _msg = ss.str();
  38. };
  39. virtual const char* what() const throw() { return _msg.c_str(); }
  40. private:
  41. std::string _file, _msg;
  42. };
  43. class ParseExc : public std::exception {
  44. public:
  45. ParseExc(size_t l, size_t p, std::string exc, std::string f, std::string file)
  46. : _l(l), _p(p), _exc(exc), _f(f), _file(file), _msg() {
  47. std::stringstream ss;
  48. ss << _file << ":" << _l << ", at pos " << _p << ": Expected " << _exc
  49. << ", found " << _f;
  50. _msg = ss.str();
  51. };
  52. virtual const char* what() const throw() { return _msg.c_str(); }
  53. private:
  54. size_t _l;
  55. size_t _p;
  56. std::string _exc, _f, _file, _msg;
  57. };
  58. class ConfigFileParser {
  59. public:
  60. ConfigFileParser();
  61. void parse(const std::string& path);
  62. const std::string& getStr(Sec sec, const Key& key) const;
  63. int getInt(Sec sec, const Key& key) const;
  64. double getDouble(Sec sec, const Key& key) const;
  65. bool getBool(Sec sec, const Key& key) const;
  66. std::vector<std::string> getStrArr(Sec sec, const Key& key, char del) const;
  67. std::vector<int> getIntArr(Sec sec, const Key& key, char del) const;
  68. std::vector<double> getDoubleArr(Sec sec, const Key& key, char del) const;
  69. std::vector<bool> getBoolArr(Sec sec, const Key& key, char del) const;
  70. std::string toString() const;
  71. const std::unordered_map<std::string, size_t> getSecs() const;
  72. bool hasKey(Sec section, Key key) const;
  73. const Val& getVal(Sec section, Key key) const;
  74. const KeyVals& getKeyVals(Sec section) const;
  75. private:
  76. std::unordered_map<std::string, size_t> _secs;
  77. std::vector<KeyVals> _kvs;
  78. bool isKeyChar(char t) const;
  79. std::string trim(const std::string& str) const;
  80. bool toBool(std::string str) const;
  81. void updateVals(size_t sec, const KeyVals& kvs);
  82. };
  83. }