在gitee上找到的一个很好用的ini文件解析器,纯C++代码,移植方便。
项目地址:https://gitee.com/sollyu/IniParser
稍微修改了下,去掉了Windows平台相关定义,改了下类名称。
头文件:
1 #ifndef INIPARSER_H 2 #define INIPARSER_H 3 4 5 #include <map> 6 #include <string> 7 #include <string.h> 8 9 class IniDoc 10 { 11 public: 12 struct IgnoreCaseLT 13 { 14 bool operator()(const std::string& lhs, const std::string& rhs) const 15 { 16 return strcasecmp(lhs.c_str(), rhs.c_str()) < 0; 17 } 18 }; 19 20 public: 21 typedef std::map<std::string, std::string, IgnoreCaseLT> KeyMap; 22 typedef std::map<std::string, KeyMap, IgnoreCaseLT> SectionMap; 23 typedef KeyMap::iterator KeyIterator; 24 typedef SectionMap::iterator SectionIterator; 25 26 public: 27 // 默认的构造函数和析构函数 28 IniDoc(); 29 ~IniDoc(); 30 31 // 构造函数 - 加载文件 32 IniDoc(const std::string& file_name); 33 34 // 加载一个ini文件, 如果之前的文件被修改, 那么之前的ini文件将会被保持。 35 bool load(const std::string& file_name); 36 37 // 从字符串中作为ini文件加载 38 bool loadString(const std::string& str); 39 40 // 保持到加载ini位置 41 bool save(); 42 43 // 另存为一个和加载路径不一样的文件中 44 bool saveAs(const std::string& file_name); 45 46 // 返回ini是否被修改, 或者他最后一次操作是保存 47 bool isModified() const { return m_modified; } 48 49 public: // high level member function. 50 51 // 下面的成员函数是从Section中获得一些值 52 long getInteger(const std::string& section, const std::string& key, long def_val); 53 float getFloat(const std::string& section, const std::string& key, float def_val); 54 long getStruct(const std::string& section, const std::string& key, void* buffer, long size); 55 long getString(const std::string& section, const std::string& key, const std::string& def_val, std::string& buffer); 56 const std::string getString(const std::string& section, const std::string& key, const std::string& def_val); 57 58 void setInteger(const std::string& section, const std::string& key, long value); 59 void setFloat(const std::string& section, const std::string& key, float value); 60 void setStruct(const std::string& section, const std::string& key, const void* buffer, long size); 61 void setString(const std::string& section, const std::string& key, const std::string& value); 62 63 public: 64 bool delSection( const std::string& section ); 65 bool delKey( const std::string& section, const std::string& key ); 66 67 public: 68 // 返回一个section的map键值对 69 const KeyMap& getSection(const std::string& section) const; 70 71 // 返回整个ini的Sections 72 const SectionMap& getIni() const { return m_map; } 73 74 private: 75 void saveBeforeLoad(); 76 const char* key_value(const std::string& section, const std::string& key); 77 78 private: 79 // 禁止复制构造函数和赋值操作符。 80 IniDoc(const IniDoc& copy); 81 IniDoc& operator=(const IniDoc& rhs); 82 83 private: 84 static const KeyMap ms_emptySection; 85 static const char left_tag ; 86 static const char right_tag ; 87 static const char equal ; 88 static const char cr ; 89 static const char new_line ; 90 static const char* empty_str ; 91 static const int BUFFER_LEN ; 92 93 SectionMap m_map; 94 std::string m_file_name; 95 bool m_modified; 96 }; 97 98 #endif // INIPARSER_H