1
0

iniparser.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #ifndef HV_INI_PARSER_H_
  2. #define HV_INI_PARSER_H_
  3. #include <list>
  4. #include <string>
  5. using std::string;
  6. #define DEFAULT_INI_COMMENT "#"
  7. #define DEFAULT_INI_DELIM "="
  8. /**********************************
  9. # div
  10. [section]
  11. key = value # span
  12. # div
  13. ***********************************/
  14. class IniNode {
  15. public:
  16. enum Type {
  17. INI_NODE_TYPE_UNKNOWN,
  18. INI_NODE_TYPE_ROOT,
  19. INI_NODE_TYPE_SECTION,
  20. INI_NODE_TYPE_KEY_VALUE,
  21. INI_NODE_TYPE_DIV,
  22. INI_NODE_TYPE_SPAN,
  23. } type;
  24. string label; // section|key|comment
  25. string value;
  26. std::list<IniNode*> children;
  27. virtual ~IniNode() {
  28. for (auto pNode : children) {
  29. if (pNode) {
  30. delete pNode;
  31. }
  32. }
  33. children.clear();
  34. }
  35. void Add(IniNode* pNode) {
  36. children.push_back(pNode);
  37. }
  38. void Del(IniNode* pNode) {
  39. for (auto iter = children.begin(); iter != children.end(); ++iter) {
  40. if ((*iter) == pNode) {
  41. delete (*iter);
  42. children.erase(iter);
  43. return;
  44. }
  45. }
  46. }
  47. IniNode* Get(const string& label, Type type = INI_NODE_TYPE_KEY_VALUE) {
  48. for (auto pNode : children) {
  49. if (pNode->type == type && pNode->label == label) {
  50. return pNode;
  51. }
  52. }
  53. return NULL;
  54. }
  55. };
  56. class IniSection : public IniNode {
  57. public:
  58. IniSection() : IniNode(), section(label) {
  59. type = INI_NODE_TYPE_SECTION;
  60. }
  61. string &section;
  62. };
  63. class IniKeyValue : public IniNode {
  64. public:
  65. IniKeyValue() : IniNode(), key(label) {
  66. type = INI_NODE_TYPE_KEY_VALUE;
  67. }
  68. string &key;
  69. };
  70. class IniComment : public IniNode {
  71. public:
  72. IniComment() : IniNode(), comment(label) {
  73. }
  74. string &comment;
  75. };
  76. class IniParser {
  77. public:
  78. IniParser();
  79. ~IniParser();
  80. void SetIniComment(const string& comment) {_comment = comment;}
  81. void SetIniDilim(const string& delim) {_delim = delim;}
  82. int LoadFromFile(const char* filepath);
  83. int LoadFromMem(const char* data);
  84. int Unload();
  85. void DumpString(IniNode* pNode, string& str);
  86. string DumpString();
  87. int Save();
  88. int SaveAs(const char* filepath);
  89. string GetValue(const string& key, const string& section = "");
  90. void SetValue(const string& key, const string& value, const string& section = "");
  91. // T = [bool, int, float]
  92. template<typename T>
  93. T Get(const string& key, const string& section = "", T defvalue = 0);
  94. // T = [bool, int, float]
  95. template<typename T>
  96. void Set(const string& key, const T& value, const string& section = "");
  97. private:
  98. string _comment;
  99. string _delim;
  100. string _filepath;
  101. IniNode* root_;
  102. };
  103. #endif // HV_INI_PARSER_H_