简单封装的一个ini解析处理类(支持跨平台)。支持功能:
- 加载并解析指定ini文件中的配置;
- 读取指定 section 下的指定 key 的值。提示:支持按数值型读取,或按文本类型读取;
使用示例:1 auto fWidth = 480.0f; 2 auto fHeight = 320.0f; 3 4 ns_ini::IniParser ini; 5 ini.open(strConfigFile.c_str()); 6 fWidth = ini.readFloat("glview", "screen_width", fWidth); 7 fHeight = ini.readFloat("glview", "screen_height", fHeight);
- 支持新段、项、值写入;提示:如果写入的项是已经存在的,则为修改旧项值;
示例:1 auto fWidth = 480.0f; 2 auto fHeight = 320.0f; 3 4 ns_ini::IniParser ini; 5 ini.open(strConfigFile.c_str()); 6 fWidth = ini.readFloat("glview", "screen_width", fWidth); 7 fHeight = ini.readFloat("glview", "screen_height", fHeight); 8 9 ini.writeFloat("glview", "screen_height, 777.77f); // 将旧值修改为 777.77f 10 ini.writeInt("glview", "screen_height, 666); // 再次将旧值修改为 666 11 ini.writeText("glview", "screen_height, "jacc.kim"); // 再次将旧值修改为 文本"jacc.kim"
源码如下:
1 /****************************************************************************** 2 3 I'm jacc.kim 4 5 CreateDate: 2017-06-13 17:12:16 6 FileName : JKIniParser.h 7 Version : 1.00 8 Author : jacc.kim 9 Summary : ini parser wrapper 10 11 ******************************************************************************/ 12 #pragma once 13 14 #include <list> 15 #include <map> 16 17 namespace ns_ini 18 { 19 20 class Section; 21 22 /****************************************************************************** 23 * create : (jacc.kim) [06-13-2017] 24 * summary : class IniParser 25 ******************************************************************************/ 26 class IniParser 27 { 28 public: 29 void setIniFile(const char* const pcszIniFile); 30 bool open(const char* const pcszIniFile = nullptr); 31 bool save(const char* const pcszIniFile = nullptr); 32 void close(); 33 34 unsigned int getSectionAmount() const; 35 bool isSectionExisted(const char* const pcszSection) const; 36 bool isParameterExisted(const char* const pcszSection, const char* const pcszParameter) const; 37 38 int readInt(const char* const pcszSection, const char* const pcszKey, const int def = 0) const; 39 unsigned int readUnsignedInt(const char* const pcszSection, const char* const pcszKey, const unsigned int def = 0u) const; 40 bool readBool(const char* const pcszSection, const char* const pcszKey, const bool def = false) const; 41 float readFloat(const char* const pcszSection, const char* const pcszKey, const float def = 0.0f) const; 42 double readDouble(const char* const pcszSection, const char* const pcszKey, const double def = 0.0) const; 43 const char* readText(const char* const pcszSection, const char* const pcszKey, const char* const def = nullptr) const; 44 45 // !!!note: write existed section & key, the old value will be replaced. 46 void writeInt(const char* const pcszSection, const char* const pcszKey, const int value); 47 void writeUnsignedInt(const char* const pcszSection, const char* const pcszKey, const unsigned int value); 48 void writeBool(const char* const pcszSection, const char* const pcszKey, const bool value); 49 void writeFloat(const char* const pcszSection, const char* const pcszKey, const float value); 50 void writeDouble(const char* const pcszSection, const char* const pcszKey, const double value); 51 void writeText(const char* const pcszSection, const char* const pcszKey, const char* const value); 52 53 bool remove(const char* const pcszSection, const char* const pcszKey); 54 bool remove(const char* const pcszSection); 55 void clear(); 56 57 public: 58 IniParser(); 59 ~IniParser(); 60 61 Section* getSection(const char* const pcszSection) const; 62 Section& operator[](const char* const pcszSection) const; 63 64 private: 65 IniParser(const IniParser& rhs)/* = delete*/; 66 IniParser& operator=(const IniParser& rhs)/* = delete*/; 67 68 void trimLeft(char*& p, char*& q); 69 void trimRight(char*& p, char*& q); 70 void trim(char*& p, char*& q); 71 void trimRN0(char*& p, char*& q); 72 bool isEndWithN(const char* p, const size_t nLength); 73 bool stringEqual(const char* p, const char* q, int nChar = INT_MAX) const; 74 bool isSectionName(char* p, char* q); 75 char* splitKeyValue(char* p, char* q); 76 void parseIniLine(char* szIniLine); 77 void addSection(char* szSectionName); 78 void addParameter(char* szKey, char* szValue); 79 80 private: 81 typedef std::list<Section*> SectionList; 82 typedef SectionList::iterator SectionListIter; 83 84 // key = section name, value = section's ptr 85 typedef std::map <std::string, SectionListIter> SectionMap; 86 87 private: 88 std::string m_strIniFile; // ini file name 89 SectionList m_listSections; // all sections. 90 SectionMap m_mapSectionName; // section <--> index mapping.(only reference.) 91 92 };//class IniParser 93 94 }//namespace ns_ini