ConfigFileParser.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. void parse(std::istream& is, const std::string& path);
  63. void parseStr(const std::string& str);
  64. void addKV(const std::string& sec, const std::string& k, const std::string& v);
  65. const std::string& getStr(Sec sec, const Key& key) const;
  66. int getInt(Sec sec, const Key& key) const;
  67. double getDouble(Sec sec, const Key& key) const;
  68. double getPosDouble(Sec sec, const Key& key) const;
  69. bool getBool(Sec sec, const Key& key) const;
  70. std::vector<std::string> getStrArr(Sec sec, const Key& key, char del) const;
  71. std::vector<int> getIntArr(Sec sec, const Key& key, char del) const;
  72. std::vector<double> getDoubleArr(Sec sec, const Key& key, char del) const;
  73. std::vector<bool> getBoolArr(Sec sec, const Key& key, char del) const;
  74. std::string toString() const;
  75. const std::unordered_map<std::string, size_t> getSecs() const;
  76. bool hasKey(Sec section, Key key) const;
  77. const Val& getVal(Sec section, Key key) const;
  78. const KeyVals& getKeyVals(Sec section) const;
  79. private:
  80. std::unordered_map<std::string, size_t> _secs;
  81. std::vector<KeyVals> _kvs;
  82. bool isKeyChar(char t) const;
  83. std::string trim(const std::string& str) const;
  84. bool toBool(std::string str) const;
  85. bool isFloat(const std::string& str) const;
  86. void updateVals(size_t sec, const KeyVals& kvs);
  87. std::string relPath(const std::string& file, std::string curFile) const;
  88. };
  89. }