IniFile.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #ifndef INCLUDED_CACTI_UTIL_INIFILE_H
  2. #define INCLUDED_CACTI_UTIL_INIFILE_H
  3. #include <string>
  4. #include <list>
  5. #include <map>
  6. #include <functional>
  7. enum LINETYPE {
  8. TYPE_SECTION = 1,
  9. TYPE_KEYVALUE = 2,
  10. TYPE_COMMENT = 3,
  11. };
  12. class LineFrame
  13. {
  14. public:
  15. LineFrame()
  16. : m_valueHasComment(false)
  17. {}
  18. LINETYPE m_type;
  19. std::string m_section; // when TYPE_SECTION
  20. std::string m_key; // when TYPE_KEYVALUE
  21. std::string m_value; // when TYPE_KEYVALUE
  22. bool m_valueHasComment; // value 字段是否包含有注释字符,用引号引住的value可以包含注释字符
  23. std::string m_comment; // all case
  24. };
  25. typedef std::map<std::string, LineFrame*> KeyValueMap; // key--->frame
  26. typedef std::map<std::string, KeyValueMap> KeyValueIndex;
  27. class IniFile
  28. {
  29. public:
  30. IniFile();
  31. ~IniFile(void);
  32. bool open(const char* filename);
  33. bool save(const char* filename);
  34. void clear();
  35. bool readBool(const char* section, const char* key, bool dft);
  36. int readInt(const char* section, const char* key, int dft);
  37. double readDouble(const char* section, const char* key, double dft);
  38. const char* readString(const char* section, const char* key, const char* dft);
  39. void writeBool(const char* section, const char* key, bool val);
  40. void writeInt(const char* section, const char* key, int val);
  41. void writeDouble(const char* section, const char* key, double val);
  42. void writeString(const char* section, const char* key, const char* val);
  43. void writeHex(const char* section, const char* key, std::uint32_t val);
  44. bool hasSection(const char* section);
  45. bool deleteSection(const char* section);
  46. bool deleteKey(const char* section, const char* key);
  47. void readSection(const char* section, std::map<std::string, std::string>& kvset);
  48. private:
  49. void parseLine(const std::string& line);
  50. std::string::size_type findComment(const std::string& line);
  51. std::string::size_type findValueComment(const std::string& line);
  52. bool isCommentLine(const std::string& line);
  53. bool hasComment(const std::string& line);
  54. std::list<LineFrame*>::iterator findSection(const char* section);
  55. std::list<LineFrame*>::iterator findSectionEnd(const char* section);
  56. LineFrame* findFrame(const char*section, const char* key);
  57. LineFrame* findNotEmptyFrame(const char*section, const char* key);
  58. void makeIndex();
  59. std::string constTrim(const std::string& s)
  60. {
  61. if(s.empty())
  62. return s;
  63. std::string::size_type b = s.find_first_not_of(" \t");
  64. if(b == std::string::npos) // No non-spaces
  65. return "";
  66. std::string::size_type e = s.find_last_not_of(" \t");
  67. return std::string(s, b, e - b + 1);
  68. }
  69. bool ignoreCaseCompare(const std::string& left, const std::string& right)
  70. {
  71. std::string tmpleft = constTrim(left);
  72. std::string tmpright= constTrim(right);
  73. toUpper(tmpleft);
  74. toUpper(tmpright);
  75. return tmpleft == tmpright;
  76. }
  77. void toUpper(std::string& str)
  78. {
  79. for(size_t i=0; i<str.length(); i++)
  80. str[i] = ::toupper(str[i]);
  81. }
  82. void toLower(std::string& str)
  83. {
  84. for(size_t i=0; i<str.length(); i++)
  85. str[i] = ::tolower(str[i]);
  86. }
  87. private:
  88. std::list<LineFrame*> m_lines;
  89. KeyValueIndex m_lineindex; // section --> KeyValueMap
  90. std::string m_prevSection;
  91. std::list<LineFrame*>::iterator m_prevInsertIt;
  92. };
  93. #endif // INCLUDED_CACTI_UTIL_INIFILE_H