2008年04月26日 星期六 上午 10:27/*==================================================================
= 文件名:CIniFile类定义文件 =
= 主要功能:可以读取.修改变量数值,可以设置新的组,新的变量 =
= 修改日期:2002-12-28 =
= 作者:阿皮 =
= E_Mail:peijikui@sd365.com QQ:122281932 = =
====================================================================*/
#ifndef _CINIFILE_H_
#define _CINIFILE_H_
//用户接口说明:在成员函数SetVarStr和SetVarInt函数中,当iType等于零,则如果用户制定的参数在ini文件中不存在,
//则就写入新的变量.当iType不等于零,则如果用户制定的参数在ini文件中不存在,就不写入新的变量,而是直接返回FALSE;
class CIniFile
{
public:
CIniFile();
virtual ~CIniFile();
private:
CIniFile(const CIniFile &);
CIniFile & operator = (const CIniFile &);
public:
//创建函数
BOOL Create(const CString &strFileName);
//得到变量整数型数值
BOOL GetVarInt(const CString &,const CString & ,int &);
//得到变量字符串型数值
BOOL GetVarStr(const CString &,const CString & ,CString & );
//重新设置变量整数型数值
BOOL SetVarInt(const CString &,const CString & ,const int &,const int iType = 1);
//重新设置变量字符串型数值
BOOL SetVarStr(const CString &,const CString &, const CString &,const int iType = 1);
private:
BOOL GetVar(const CString &,const CString &,CString &);
BOOL SetVar(const CString &,const CString &,const CString &,const int iType = 1);
int SearchLine(const CString &,const CString &);
private:
// vector <CString> FileContainer;
CArray <CString,CString> FileContainer;
BOOL bFileExsit;
CStdioFile stfFile;
CString strInIFileName;
};
#endif
/*==================================================================
= 文件名:CIniFile类实现文件 =
= 修改日期:2002-12-28 =
= 作者:阿皮 =
= E_Mail:peijikui@sd365.com msn:peijikui@sina.com =
====================================================================*/
#include "stdafx.h"
#include "CIniFile.h"
CIniFile::CIniFile():bFileExsit(FALSE)
{
}
CIniFile::~CIniFile()
{
if(bFileExsit)
{
if(stfFile.Open(strInIFileName,CFile::modeCreate | CFile::modeWrite) && FileContainer.GetSize() > 0)
{
CString strParam;
for(int i = 0; i< FileContainer.GetSize();i++)
{
strParam = FileContainer[i];
// stfFile.WriteString(strParam);
stfFile.WriteString(strParam+"/n");
}
}
stfFile.Close();
}
if(FileContainer.GetSize() > 0)
{
FileContainer.RemoveAll();
}
}
BOOL CIniFile::Create(const CString & strFileName)
{
bFileExsit = FALSE;
strInIFileName = strFileName;
if(!stfFile.Open(strFileName,CFile::modeRead))
{
return bFileExsit;
}
CString strFileLine;
while(bFileExsit = stfFile.ReadString(strFileLine))
{
if(bFileExsit == FALSE)
return bFileExsit;
FileContainer.Add(strFileLine);
}
stfFile.Close();
bFileExsit = TRUE;
return bFileExsit;
}
BOOL CIniFile::GetVar(const CString & strSection,const CString & strVarName,CString &strReturnValue)
{
if(bFileExsit == FALSE || FileContainer.GetSize() < 0)
return bFileExsit;
int iLine = SearchLine(strSection,strVarName);
if(iLine > 0)
{
CString strParam = FileContainer[iLine -1];
strReturnValue = strParam.Mid(strParam.Find("=") + 1);
return TRUE;
}
return FALSE;
}
BOOL CIniFile::GetVarStr(const CString & strSection,const CString & strVarName,CString &strReturnValue)
{
return(GetVar(strSection,strVarName,strReturnValue));
}
BOOL CIniFile::GetVarInt(const CString & strSection,const CString & strVarName,int & iValue)
{
CString strReturnVar;
if(GetVar(strSection,strVarName,strReturnVar))
{
strReturnVar.TrimLeft();
int iLen = strReturnVar.GetLength();
iValue = atoi(strReturnVar.GetBuffer(iLen));
return TRUE;
}
return TRUE;
}
BOOL CIniFile::SetVar(const CString & strSection,const CString & strVarName,const CString & strVar,const int iType)
{
if(bFileExsit == FALSE )
return bFileExsit;
if(FileContainer.GetSize() == 0)
{
FileContainer.Add("[" + strSection + "]");
FileContainer.Add(strVarName + "=" + strVar);
return TRUE;
}
int i = 0;
int iFileLines = FileContainer.GetSize();
//for(pInterator;pInterator != FileContainer.end();++pInterator)
//{
while(i< iFileLines)
{
CString strValue = FileContainer.GetAt(i++);
strValue.TrimLeft();
if((strValue.Find("[") >=0) && (strValue.Find(strSection) >=0))
{
while(i < iFileLines)
{
CString strSectionList = FileContainer[i++];
strSectionList.TrimLeft();
if(strSectionList.Find("//") >=0)//找到注释行
continue;
if(strSectionList.Find(strVarName)>=0)//找到
{
CString strParam = strVarName + "=" + strVar;
//FileContainer.SetAt(i-1,strParam);
FileContainer[i-1] = strParam;
return TRUE;
}
if(strSectionList.Find("[",0)>=0)//在原来文件的SECTION中,没有相应的变量需要添加而且,这种情况下,下边还有别的section
{
//处理流程是这样的,首先把当前的数值依次向后移动,然后在当前位置加入新出现的数值
if(iType !=0)
return FALSE;
CString strParam;
FileContainer.Add(strParam);
int iPre = FileContainer.GetSize()-1;
while(iPre >= i)
{
CString strBehind = FileContainer[iPre -1];
FileContainer[iPre] = strBehind;
iPre --;
}//*/
strParam = strVarName + "=" + strVar;
FileContainer.SetAt(i-1,strParam);
return TRUE;
}
if(i == iFileLines && iType == 0)
{
FileContainer.Add(strVarName + "=" + strVar);
return TRUE;
}
}
}
}
if(iType == 0)
{
FileContainer.Add("[" + strSection + "]");
FileContainer.Add(strVarName + "=" + strVar);
}
return TRUE;
}
BOOL CIniFile::SetVarStr(const CString & strSection,const CString & strVarName,const CString & strValue,const int iType)
{
return SetVar(strSection,strVarName,strValue,iType);
}
BOOL CIniFile::SetVarInt(const CString & strSection,const CString & strVarName,const int & iValue,const int iType)
{
CString strVar;
strVar.Format("%d",iValue);
return (SetVar(strSection,strVarName,strVar,iType));
}
int CIniFile::SearchLine(const CString & strSection,const CString & strVarName)
{
if(FileContainer.GetSize() > 0)
{
int i = 0;
int iFileLines = FileContainer.GetSize();
while(i< iFileLines)
{
CString strValue = FileContainer[i++];
strValue.TrimLeft();
if(strValue.Find("[") >=0 && strValue.Find(strSection,1)>=0)
{
while(i < iFileLines)
{
CString strSectionList = FileContainer[i++];
strSectionList.TrimLeft();
if(strSectionList.Find("//") >=0)//找到注释行
continue;
if(strSectionList.Find(strVarName)>=0)//找到
{
return i;
}
if(strSectionList.Find("[",0) >= 0)//另外一个段落出现,寻找失败
{
return -2;
}
}
}
}
}
return -1;
}