📄 tzprofile.cpp
字号:
// tzProfile.cpp: implementation of the CProfile class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "tzProfile.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
/////////////////////////////////////////////////////////////
//Class CProfile
CProfile::CProfile()
{
m_bModified=FALSE;
}
CProfile::CProfile(LPCTSTR lpszFileName,LPCTSTR lpszSection)
{
m_bModified=FALSE;
Open(lpszFileName,lpszSection);
}
CProfile::~CProfile()
{
Close();
}
BOOL CProfile::Open(LPCTSTR lpszFileName,LPCTSTR lpszSection)
{
TRY{
BOOL bOpen;
bOpen = m_File.Open(lpszFileName,CFile::modeRead|CFile::shareDenyNone);
if (!bOpen) {
return FALSE;
}
m_strSection.Format(_T("%s"),lpszSection);
//remove UNICODE tag
TCHAR c;
m_File.Read(&c,CHARWIDTH);
//parse every line,get keyname and value
CString strLine,strName,strValue,strSection;
while(GetLine(strLine)>0){
ParseLine(strLine,strName,strValue);
if(strName.FindOneOf(_T("]"))>0){
strSection=strName;
strSection.Remove(TCHAR('['));
strSection.Remove(TCHAR(']'));
}
if(strValue.GetLength()>0){
strName=strSection+_T(".")+strName;
}
m_rgKeyName.Add(strName);
m_rgValue.Add(strValue);
}
}
CATCH(CFileException,e){
return FALSE;
}
END_CATCH
return TRUE;
}
INT CProfile::GetLine(CString &strLine)
{
TCHAR c[1];
strLine.Empty();
while(m_File.Read(c,CHARWIDTH)>0){
if(c[0] == KEY_ENTER){
m_File.Read(c,CHARWIDTH);
return 1;
}
strLine+=c[0];
}
if(strLine.GetLength()>0){
return 1;
}
return 0;
}
VOID CProfile::ParseLine(CString strLine,CString &strName,CString &strValue)
{
INT nLen;
strLine.TrimLeft();
strName.Empty();
strValue.Empty();
nLen=strLine.GetLength();
//new line
if(nLen==0){
strName += TCHAR(13);
strName +=TCHAR(10);
return;
}
//section title
if(strLine.GetAt(0) == TCHAR('[')){
strName=strLine;
return;
}
//normal line
INT nPos;
nPos=strLine.FindOneOf(_T("="));
if(nPos<0){
strName=strLine;
}else{
strName=strLine.Left(nPos);
++nPos;
strValue=strLine.Right(nLen-nPos);
}
strName.TrimLeft();
strName.TrimRight();
strValue.TrimLeft();
strValue.TrimRight();
}
CString CProfile::GetKeyName(LPCTSTR lpszKeyName,CString &strKeyName)
{
strKeyName.Format(_T("%s"),lpszKeyName);
strKeyName=m_strSection+_T(".")+strKeyName;
return strKeyName;
}
INT CProfile::LookUp(LPCTSTR lpszKeyName)
{
CString strKeyName;
GetKeyName(lpszKeyName,strKeyName);
INT nCount;
nCount=m_rgKeyName.GetSize();
for(INT i=0; i<nCount; ++i){
if(strKeyName == m_rgKeyName.GetAt(i)){
return i;
}
}
return -1;
}
CString CProfile::GetString(LPCTSTR lpszKeyName, LPCTSTR lpszDefault)
{
INT nPos;
CString strValue;
nPos = LookUp(lpszKeyName);
if(nPos>=0){
strValue=m_rgValue.GetAt(nPos);
}
if(strValue.GetLength()==0){
strValue.Format(_T("%s"),lpszDefault);
}
return strValue;
}
INT CProfile::GetInt(LPCTSTR lpszKeyName, INT nDefault)
{
INT nValue;
TCHAR *p;
CString strValue;
strValue=GetString(lpszKeyName);
if(strValue.GetLength() == 0){
return nDefault;
}else{
nValue=_tcstol(LPCTSTR(strValue),&p,10);
return nValue;
}
}
VOID CProfile::WriteString(LPCTSTR lpszKeyName, LPCTSTR lpszValue)
{
INT nPos;
nPos=LookUp(lpszKeyName);
//key not exist
if(nPos<0){
return;
}
CString strValue;
strValue.Format(_T("%s"),lpszValue);
m_rgValue.SetAt(nPos,strValue);
//m_rgValue.GetAt(nPos).Format(_T("%s"),lpszValue);
m_bModified = TRUE;
}
VOID CProfile::WriteInt(LPCTSTR lpszKeyName, INT nValue)
{
CString strValue;
strValue.Format(_T("%d"),nValue);
WriteString(lpszKeyName,LPCTSTR(strValue));
}
INT CProfile::Save()
{
if(!m_bModified){
return 1;
}
CFile dupFile;
CString strFileName,strTempName;
TRY{
strFileName=m_File.GetFilePath();
strTempName=strFileName+_T("tmp");
dupFile.Open(LPCTSTR(strTempName),CFile::modeWrite|CFile::modeCreate);
//write UNICODE tag
TCHAR chTmp;
chTmp = 0xFEFF;
dupFile.Write(&chTmp, CHARWIDTH);
INT nCount;
CString strKeyName,strName,strValue,strLine;
nCount=m_rgKeyName.GetSize();
for(INT i=0; i<nCount; ++i){
strKeyName=m_rgKeyName.GetAt(i);
strValue=m_rgValue.GetAt(i);
INT nPos;
nPos=strKeyName.FindOneOf(_T("."));
if(nPos < 0){
strLine=strKeyName;
}else{
strName=strKeyName.Right(strKeyName.GetLength()-nPos-1);
strLine=strName+_T("=")+strValue;
}
if(strLine.GetAt(0)!=TCHAR(KEY_ENTER)){
strLine+=TCHAR(KEY_ENTER);
strLine+=TCHAR(KEY_NEWLINE);
}
dupFile.Write(LPCTSTR(strLine),strLine.GetLength()*CHARWIDTH);
}
dupFile.Flush();
dupFile.Close();
}
CATCH(CFileException,e){
if(!dupFile){
dupFile.Close();
CFile::Remove(strTempName);
}
return -1;
}
END_CATCH
m_File.Close();
CFile::Remove(strFileName);
CFile::Rename(strTempName,strFileName);
return 1;
}
VOID CProfile::Close()
{
Save();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -