2020-08-12 15:48:12 +00:00
|
|
|
|
#ifndef __EX_INI_H__
|
2016-12-14 15:34:44 +00:00
|
|
|
|
#define __EX_INI_H__
|
2016-12-06 17:05:56 +00:00
|
|
|
|
|
2016-12-14 15:34:44 +00:00
|
|
|
|
/*
|
2020-08-12 15:48:12 +00:00
|
|
|
|
特别注意:
|
2016-12-06 17:05:56 +00:00
|
|
|
|
|
2020-08-12 15:48:12 +00:00
|
|
|
|
1. 以 分号';' 或者 井号'#' 作为注释行的第一个字符
|
|
|
|
|
2. 不支持行内注释
|
|
|
|
|
3. 值对以第一个等号分隔,等号前后如果有空格会被忽略,之后的空格会保留,包括行尾空格
|
|
|
|
|
4. 如果有不属于某个小节的值对,可以使用GetDumySection()获取
|
|
|
|
|
DumySection主要是为了能够兼容简单的Python文件做配置文件。
|
2016-12-14 15:34:44 +00:00
|
|
|
|
*/
|
2016-12-06 17:05:56 +00:00
|
|
|
|
|
2016-12-14 15:34:44 +00:00
|
|
|
|
#include "ex_str.h"
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
|
|
typedef std::map<ex_wstr, ex_wstr> ex_ini_kvs;
|
|
|
|
|
|
|
|
|
|
class ExIniSection
|
2016-12-06 17:05:56 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
2021-03-03 17:25:23 +00:00
|
|
|
|
ExIniSection();
|
2016-12-06 17:05:56 +00:00
|
|
|
|
|
2021-03-03 17:25:23 +00:00
|
|
|
|
explicit ExIniSection(const ex_wstr& strSectionName);
|
2016-12-06 17:05:56 +00:00
|
|
|
|
|
2021-03-03 17:25:23 +00:00
|
|
|
|
~ExIniSection();
|
2016-12-06 17:05:56 +00:00
|
|
|
|
|
2021-03-03 17:25:23 +00:00
|
|
|
|
void ClearUp();
|
2016-12-06 17:05:56 +00:00
|
|
|
|
|
2021-03-03 17:25:23 +00:00
|
|
|
|
ex_wstr Name() { return m_strName; }
|
2016-12-06 17:05:56 +00:00
|
|
|
|
|
2021-03-03 17:25:23 +00:00
|
|
|
|
void GetStr(const ex_wstr& strKey, ex_wstr& strValue, const ex_wstr& strDefault);
|
2016-12-06 17:05:56 +00:00
|
|
|
|
|
2021-03-03 17:25:23 +00:00
|
|
|
|
bool GetStr(const ex_wstr& strKey, ex_wstr& strValue);
|
|
|
|
|
|
|
|
|
|
void GetInt(const ex_wstr& strKey, int& iValue, int iDefault);
|
|
|
|
|
|
|
|
|
|
bool GetInt(const ex_wstr& strKey, int& iValue);
|
|
|
|
|
|
|
|
|
|
void GetBool(const ex_wstr& strKey, bool& bValue, bool bDefault);
|
|
|
|
|
|
|
|
|
|
bool GetBool(const ex_wstr& strKey, bool& bValue);
|
|
|
|
|
|
|
|
|
|
bool SetValue(const ex_wstr& strKey, const ex_wstr& strValue, bool bAddIfNotExists = false);
|
|
|
|
|
|
|
|
|
|
ex_ini_kvs& GetKeyValues() { return m_kvs; }
|
|
|
|
|
|
|
|
|
|
int Count() const
|
|
|
|
|
{
|
|
|
|
|
return (int) m_kvs.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Save(FILE* file, int codepage);
|
2016-12-06 17:05:56 +00:00
|
|
|
|
|
2016-12-14 15:34:44 +00:00
|
|
|
|
#ifdef EX_DEBUG
|
2021-03-03 17:25:23 +00:00
|
|
|
|
void Dump(void);
|
2016-12-06 17:05:56 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
protected:
|
2021-03-03 17:25:23 +00:00
|
|
|
|
bool _IsKeyExists(const ex_wstr& strKey);
|
2016-12-06 17:05:56 +00:00
|
|
|
|
|
|
|
|
|
private:
|
2021-03-03 17:25:23 +00:00
|
|
|
|
ex_wstr m_strName;
|
|
|
|
|
ex_ini_kvs m_kvs;
|
2016-12-06 17:05:56 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2016-12-14 15:34:44 +00:00
|
|
|
|
typedef std::map<ex_wstr, ExIniSection*> ex_ini_sections;
|
2016-12-06 17:05:56 +00:00
|
|
|
|
|
|
|
|
|
// Ini file
|
2016-12-14 15:34:44 +00:00
|
|
|
|
class ExIniFile
|
2016-12-06 17:05:56 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
2021-03-03 17:25:23 +00:00
|
|
|
|
enum PARSE_RV
|
|
|
|
|
{
|
|
|
|
|
PARSE_ERROR,
|
|
|
|
|
PARSE_SECTION,
|
|
|
|
|
PARSE_KEYVALUE,
|
|
|
|
|
PARSE_COMMENT,
|
|
|
|
|
PARSE_OTHER
|
|
|
|
|
};
|
2016-12-06 17:05:56 +00:00
|
|
|
|
|
|
|
|
|
public:
|
2021-03-03 17:25:23 +00:00
|
|
|
|
ExIniFile();
|
|
|
|
|
|
|
|
|
|
~ExIniFile();
|
|
|
|
|
|
|
|
|
|
const ex_wstr& get_filename() { return m_file_path; }
|
|
|
|
|
|
|
|
|
|
void ClearUp();
|
2016-12-06 17:05:56 +00:00
|
|
|
|
|
2021-03-03 17:25:23 +00:00
|
|
|
|
// Read and parse special file.
|
|
|
|
|
bool LoadFromFile(const ex_wstr& strFileName, bool bClearOld = true);
|
2017-04-12 18:08:33 +00:00
|
|
|
|
|
2021-03-03 17:25:23 +00:00
|
|
|
|
bool LoadFromMemory(const ex_wstr& strData, bool bClearOld = true);
|
2016-12-06 17:05:56 +00:00
|
|
|
|
|
2021-03-03 17:25:23 +00:00
|
|
|
|
ex_ini_sections& GetAllSections() { return m_secs; }
|
2016-12-06 17:05:56 +00:00
|
|
|
|
|
2021-03-03 17:25:23 +00:00
|
|
|
|
ExIniSection* GetSection(const ex_wstr& strName, bool bCreateIfNotExists = false);
|
2017-01-08 15:53:37 +00:00
|
|
|
|
|
2021-03-03 17:25:23 +00:00
|
|
|
|
ExIniSection* GetDumySection() { return &m_dumy_sec; }
|
|
|
|
|
|
|
|
|
|
int Count() const
|
|
|
|
|
{
|
|
|
|
|
return (int) (m_secs.size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Save(int codepage = EX_CODEPAGE_UTF8);
|
2016-12-06 17:05:56 +00:00
|
|
|
|
|
2016-12-14 15:34:44 +00:00
|
|
|
|
#ifdef EX_DEBUG
|
2021-03-03 17:25:23 +00:00
|
|
|
|
void Dump(void);
|
2016-12-06 17:05:56 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
protected:
|
2021-03-03 17:25:23 +00:00
|
|
|
|
static PARSE_RV parse_line_(const ex_wstr& strLine, ex_wstr& strKey, ex_wstr& strValue);
|
|
|
|
|
|
|
|
|
|
bool process_line_(const ex_wstr& strLine, ExIniSection** pCurSection);
|
2016-12-06 17:05:56 +00:00
|
|
|
|
|
|
|
|
|
private:
|
2021-03-03 17:25:23 +00:00
|
|
|
|
ex_ini_sections m_secs;
|
|
|
|
|
ExIniSection m_dumy_sec;
|
|
|
|
|
ex_wstr m_file_path;
|
2016-12-06 17:05:56 +00:00
|
|
|
|
};
|
|
|
|
|
|
2016-12-14 15:34:44 +00:00
|
|
|
|
#endif // __EX_INI_H__
|