ConfigFileParser.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_HEAD_KEY,
  24. IN_HEAD_AW_COM_OR_END,
  25. IN_KEY_VAL_KEY,
  26. IN_KEY_VAL_VAL,
  27. AW_KEY_VAL_VAL,
  28. IN_KEY_VAL_VAL_HANG,
  29. IN_KEY_VAL_VAL_HANG_END
  30. };
  31. class ParseFileExc : public std::exception {
  32. public:
  33. ParseFileExc(const std::string file) : _file(file) {
  34. std::stringstream ss;
  35. ss << _file << ": Could not open file.";
  36. _msg = ss.str();
  37. };
  38. virtual const char* what() const throw() { return _msg.c_str(); }
  39. private:
  40. std::string _file, _msg;
  41. };
  42. class ParseExc : public std::exception {
  43. public:
  44. ParseExc(size_t l, size_t p, std::string exc, std::string f, std::string file)
  45. : _l(l), _p(p), _exc(exc), _f(f), _file(file), _msg() {
  46. std::stringstream ss;
  47. ss << _file << ":" << _l << ", at pos " << _p << ": Expected " << _exc
  48. << ", found " << _f;
  49. _msg = ss.str();
  50. };
  51. virtual const char* what() const throw() { return _msg.c_str(); }
  52. private:
  53. size_t _l;
  54. size_t _p;
  55. std::string _exc, _f, _file, _msg;
  56. };
  57. class ConfigFileParser {
  58. public:
  59. ConfigFileParser();
  60. void parse(const std::string& path);
  61. const std::string& getStr(Sec sec, const Key& key) const;
  62. int getInt(Sec sec, const Key& key) const;
  63. double getDouble(Sec sec, const Key& key) const;
  64. bool getBool(Sec sec, const Key& key) const;
  65. std::vector<std::string> getStrArr(Sec sec, const Key& key, char del) const;
  66. std::vector<int> getIntArr(Sec sec, const Key& key, char del) const;
  67. std::vector<double> getDoubleArr(Sec sec, const Key& key, char del) const;
  68. std::vector<bool> getBoolArr(Sec sec, const Key& key, char del) const;
  69. std::string toString() const;
  70. const std::unordered_map<std::string, size_t> getSecs() const;
  71. bool hasKey(Sec section, Key key) const;
  72. const Val& getVal(Sec section, Key key) const;
  73. private:
  74. std::unordered_map<std::string, size_t> _secs;
  75. std::vector<KeyVals> _kvs;
  76. bool isKeyChar(char t) const;
  77. std::string trim(const std::string& str) const;
  78. bool toBool(std::string str) const;
  79. void updateVals(size_t sec, const KeyVals& kvs);
  80. };
  81. }