INI文件在存储软件的配置信息方面有着良好的作用,为了适应跨平台的功能,我们必须放弃微软的相关API,自己实现一个INI的文件读写操作类。 下面我给出该类的具体实现。我的EMAIL是
wxy3064one@163.com
(一) 头文件 h
//INI文件 以";"表示注释行
class CIniFile
{
bool load_ini_buf(char* buf,int *len);
bool parse_ini_buf(const char* buf,const char* section,const char* key,
int *s_0,int *s_1,int *k_0,int *k_1,int *v_0,int *v_1);
bool ReadStr(const char* section,const char* key,char *value,int len,const char* def_value="");
public:
CIniFile(const char* filename = 0);
~CIniFile();
void ClearContents();
{
bool load_ini_buf(char* buf,int *len);
bool parse_ini_buf(const char* buf,const char* section,const char* key,
int *s_0,int *s_1,int *k_0,int *k_1,int *v_0,int *v_1);
bool ReadStr(const char* section,const char* key,char *value,int len,const char* def_value="");
public:
CIniFile(const char* filename = 0);
~CIniFile();
void ClearContents();
void Load(const char* filename);
char* ReadString(const char* section,const char* key,const char* def_value="");
int ReadInt(const char* section,const char* key,const int def_value);
double ReadFloat(const char* section,const char* key,const double def_value);
bool ReadBool(const char* section,const char* key,const bool def_value);
char* ReadString(const char* section,const char* key,const char* def_value="");
int ReadInt(const char* section,const char* key,const int def_value);
double ReadFloat(const char* section,const char* key,const double def_value);
bool ReadBool(const char* section,const char* key,const bool def_value);
bool WriteString(const char* section,const char* key,const char* value);
bool WriteInt(const char* section,const char* key,const int value);
bool WriteFloat(const char* section,const char* key,const double value);
bool WriteBool(const char* section,const char* key,const bool value);
bool WriteInt(const char* section,const char* key,const int value);
bool WriteFloat(const char* section,const char* key,const double value);
bool WriteBool(const char* section,const char* key,const bool value);
void EraseSection(const char* section);
private:
char m_filename[256];
};
private:
char m_filename[256];
};
(二) 实现文件 cpp
static const int Q_INI_SIZE = 0x4000;
CIniFile::CIniFile(const char* filename)
{
memset(m_filename,0,256);
if (filename != 0)
{
int len = strlen(filename);
memcpy(m_filename,(LPCTSTR)filename,len);
m_filename[len] = '/0';
}
}
{
memset(m_filename,0,256);
if (filename != 0)
{
int len = strlen(filename);
memcpy(m_filename,(LPCTSTR)filename,len);
m_filename[len] = '/0';
}
}
CIniFile::~CIniFile(){
}
}
void CIniFile::Load(const char* filename)
{
int len = strlen(filename);
memset(m_filename,0,256);
memcpy(m_filename,(LPCTSTR)filename,len);
m_filename[len] = '/0';
}
{
int len = strlen(filename);
memset(m_filename,0,256);
memcpy(m_filename,(LPCTSTR)filename,len);
m_filename[len] = '/0';
}