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. #include "hexport.h"
  7. #define DEFAULT_INI_COMMENT "#"
  8. #define DEFAULT_INI_DELIM "="
  9. /**********************************
  10. # div
  11. [section]
  12. key = value # span
  13. # div
  14. ***********************************/
  15. class HV_EXPORT IniNode {
  16. public:
  17. enum Type {
  18. INI_NODE_TYPE_UNKNOWN,
  19. INI_NODE_TYPE_ROOT,
  20. INI_NODE_TYPE_SECTION,
  21. INI_NODE_TYPE_KEY_VALUE,
  22. INI_NODE_TYPE_DIV,
  23. INI_NODE_TYPE_SPAN,
  24. } type;
  25. string label; // section|key|comment
  26. string value;
  27. std::list<IniNode*> children;
  28. virtual ~IniNode() {
  29. for (auto pNode : children) {
  30. if (pNode) {
  31. delete pNode;
  32. }
  33. }
  34. children.clear();
  35. }
  36. void Add(IniNode* pNode) {
  37. children.push_back(pNode);
  38. }
  39. void Del(IniNode* pNode) {
  40. for (auto iter = children.begin(); iter != children.end(); ++iter) {
  41. if ((*iter) == pNode) {
  42. delete (*iter);
  43. children.erase(iter);
  44. return;
  45. }
  46. }
  47. }
  48. IniNode* Get(const string& label, Type type = INI_NODE_TYPE_KEY_VALUE) {
  49. for (auto pNode : children) {
  50. if (pNode->type == type && pNode->label == label) {
  51. return pNode;
  52. }
  53. }
  54. return NULL;
  55. }
  56. };
  57. class HV_EXPORT IniSection : public IniNode {
  58. public:
  59. IniSection() : IniNode(), section(label) {
  60. type = INI_NODE_TYPE_SECTION;
  61. }
  62. string &section;
  63. };
  64. class HV_EXPORT IniKeyValue : public IniNode {
  65. public:
  66. IniKeyValue() : IniNode(), key(label) {
  67. type = INI_NODE_TYPE_KEY_VALUE;
  68. }
  69. string &key;
  70. };
  71. class HV_EXPORT IniComment : public IniNode {
  72. public:
  73. IniComment() : IniNode(), comment(label) {
  74. }
  75. string &comment;
  76. };
  77. class HV_EXPORT IniParser {
  78. public:
  79. IniParser();
  80. ~IniParser();
  81. int LoadFromFile(const char* filepath);
  82. int LoadFromMem(const char* data);
  83. int Unload();
  84. int Reload();
  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. public:
  98. string _comment;
  99. string _delim;
  100. string _filepath;
  101. private:
  102. IniNode* root_;
  103. };
  104. #endif // HV_INI_PARSER_H_