123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- #ifndef INCLUDED_CACTI_UTIL_INIFILE_H
- #define INCLUDED_CACTI_UTIL_INIFILE_H
- #include <string>
- #include <list>
- #include <map>
- #include <functional>
- enum LINETYPE {
- TYPE_SECTION = 1,
- TYPE_KEYVALUE = 2,
- TYPE_COMMENT = 3,
- };
- class LineFrame
- {
- public:
- LineFrame()
- : m_valueHasComment(false)
- {}
- LINETYPE m_type;
- std::string m_section; // when TYPE_SECTION
- std::string m_key; // when TYPE_KEYVALUE
- std::string m_value; // when TYPE_KEYVALUE
- bool m_valueHasComment; // value 字段是否包含有注释字符,用引号引住的value可以包含注释字符
- std::string m_comment; // all case
- };
- typedef std::map<std::string, LineFrame*> KeyValueMap; // key--->frame
- typedef std::map<std::string, KeyValueMap> KeyValueIndex;
- class IniFile
- {
- public:
- IniFile();
- ~IniFile(void);
- bool open(const char* filename);
- bool save(const char* filename);
- void clear();
- bool readBool(const char* section, const char* key, bool dft);
- int readInt(const char* section, const char* key, int dft);
- double readDouble(const char* section, const char* key, double dft);
- const char* readString(const char* section, const char* key, const char* dft);
- void writeBool(const char* section, const char* key, bool val);
- void writeInt(const char* section, const char* key, int val);
- void writeDouble(const char* section, const char* key, double val);
- void writeString(const char* section, const char* key, const char* val);
- void writeHex(const char* section, const char* key, std::uint32_t val);
- bool hasSection(const char* section);
- bool deleteSection(const char* section);
- bool deleteKey(const char* section, const char* key);
- void readSection(const char* section, std::map<std::string, std::string>& kvset);
- private:
- void parseLine(const std::string& line);
- std::string::size_type findComment(const std::string& line);
- std::string::size_type findValueComment(const std::string& line);
- bool isCommentLine(const std::string& line);
- bool hasComment(const std::string& line);
- std::list<LineFrame*>::iterator findSection(const char* section);
- std::list<LineFrame*>::iterator findSectionEnd(const char* section);
- LineFrame* findFrame(const char*section, const char* key);
- LineFrame* findNotEmptyFrame(const char*section, const char* key);
- void makeIndex();
- std::string constTrim(const std::string& s)
- {
- if(s.empty())
- return s;
- std::string::size_type b = s.find_first_not_of(" \t");
- if(b == std::string::npos) // No non-spaces
- return "";
- std::string::size_type e = s.find_last_not_of(" \t");
- return std::string(s, b, e - b + 1);
- }
- bool ignoreCaseCompare(const std::string& left, const std::string& right)
- {
- std::string tmpleft = constTrim(left);
- std::string tmpright= constTrim(right);
- toUpper(tmpleft);
- toUpper(tmpright);
- return tmpleft == tmpright;
- }
- void toUpper(std::string& str)
- {
- for(size_t i=0; i<str.length(); i++)
- str[i] = ::toupper(str[i]);
- }
- void toLower(std::string& str)
- {
- for(size_t i=0; i<str.length(); i++)
- str[i] = ::tolower(str[i]);
- }
- private:
- std::list<LineFrame*> m_lines;
- KeyValueIndex m_lineindex; // section --> KeyValueMap
- std::string m_prevSection;
- std::list<LineFrame*>::iterator m_prevInsertIt;
- };
- #endif // INCLUDED_CACTI_UTIL_INIFILE_H
|