?? inicfgfile.cpp
字號:
// IniCfgFile.cpp : 實現文件
//
#include "stdafx.h"
#include "IniCfgFile.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
// 函數名 : CIniCfgFile()
// 類名 : CIniCfgFile
// 功能描述 : 構造函數
///////////////////////////////////////////////////////////////////////////////////////////////////
CIniCfgFile::CIniCfgFile()
{
m_bFileExist = FALSE;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// 函數名 : ~CIniCfgFile()
// 類名 : CIniCfgFile
// 功能描述 : 析構函數
///////////////////////////////////////////////////////////////////////////////////////////////////
CIniCfgFile::~CIniCfgFile()
{
// 清除字符串隊列
if (m_strTokenArray.GetSize()>0)
m_strTokenArray.RemoveAll();
if(m_strReadArray.GetSize()>0)
m_strReadArray.RemoveAll();
if(m_strWriteArray.GetSize()>0)
m_strWriteArray.RemoveAll();
}
// CIniCfgFile 成員函數
///////////////////////////////////////////////////////////////////////////////////////////////////
// 函數名 : ReadFile()
// 類名 : CIniCfgFile
// 功能描述 : 讀取文件
///////////////////////////////////////////////////////////////////////////////////////////////////
BOOL CIniCfgFile::ReadFile(CString strFileName)
{
m_bFileExist = m_stdioFile.Open(strFileName, CFile::modeRead);
if (!m_bFileExist)
{
return FALSE;
}
CString strLine;
m_strReadArray.RemoveAll();
while (m_stdioFile.ReadString(strLine))
{
m_strReadArray.Add(strLine);
}
m_stdioFile.Close();
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// 函數名 : WriteFile()
// 類名 : CIniCfgFile
// 功能描述 : 寫入文件
///////////////////////////////////////////////////////////////////////////////////////////////////
BOOL CIniCfgFile::WriteFile(CString strFileName)
{
CStdioFile stdFileOut;
BOOL bFileOut = stdFileOut.Open(strFileName, CFile::modeCreate | CFile::modeWrite);
if(!bFileOut)
return FALSE;
CString strLine;
for(int i = 0; i<m_strWriteArray.GetSize(); i++)
{
strLine = m_strWriteArray[i];
stdFileOut.WriteString(strLine+"\n");
}
stdFileOut.Close();
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// 函數名 : SetInt()
// 類名 : CIniCfgFile
// 功能描述 : 設置整型參數值
///////////////////////////////////////////////////////////////////////////////////////////////////
void CIniCfgFile::SetInt(CString strSection, CString strItem, int nValue)
{
CString strTemp;
strTemp.Format("%d", nValue);
SetString(strSection, strItem, strTemp);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// 函數名 : SetHex()
// 類名 : CIniCfgFile
// 功能描述 : 設置十六進制整型參數值
///////////////////////////////////////////////////////////////////////////////////////////////////
void CIniCfgFile::SetHex(CString strSection, CString strItem, DWORD nValue)
{
CString strTemp;
strTemp.Format("0x%X", nValue);
SetString(strSection, strItem, strTemp);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// 函數名 : SetString()
// 類名 : CIniCfgFile
// 功能描述 : 設置字符串型參數值
///////////////////////////////////////////////////////////////////////////////////////////////////
void CIniCfgFile::SetString(CString strSection, CString strItem, CString strValue)
{
int i = 0;
int nFileLines = (int) m_strWriteArray.GetSize();
CString strLine,str;
while(i<nFileLines)
{
strLine = m_strWriteArray.GetAt(i++);
strLine.TrimLeft();
if(strLine.GetAt(0)=='[')//查找Section,第一個必須為[
{
str=strLine.Left(strLine.Find("]"));//去掉]右邊
str=str.Right(str.GetLength()-str.Find("[")-1);//去掉[左邊
str.TrimLeft();
str.TrimRight();
if(strSection == str)//找到Section
{
while(i<nFileLines)
{
strLine = m_strWriteArray.GetAt(i++);
strLine.TrimLeft();
if(strLine.GetAt(0)=='[')//如果到達下一個[],即找不到Item
break;
str = strLine.Left(strLine.Find("="));//去掉=右邊
str.TrimLeft();
str.TrimRight();
if(strItem == str)//找到Item
{
strLine = strItem + "=" + strValue;
m_strWriteArray[i-1] = strLine;
return;
}
}
//找不到Item
strLine = strItem + "=" + strValue;
m_strWriteArray.InsertAt(i-1, strLine);
return;
}
}
}
//找不到Section
//直接在最后加入Section,Item,Value
m_strWriteArray.Add("[" + strSection + "]");
m_strWriteArray.Add(strItem + "=" + strValue);
return;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// 函數名 : GetInt()
// 類名 : CIniCfgFile
// 功能描述 : 讀取整型參數值
///////////////////////////////////////////////////////////////////////////////////////////////////
int CIniCfgFile::GetInt(CString strSection, CString strItem, int nValue)
{
CString strTemp;
strTemp.Format("%d",nValue);
return atoi(GetString(strSection, strItem, strTemp));
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// 函數名 : GetHex()
// 類名 : CIniCfgFile
// 功能描述 : 讀取十六進制參數值
///////////////////////////////////////////////////////////////////////////////////////////////////
DWORD CIniCfgFile::GetHex(CString strSection, CString strItem, DWORD nValue)
{
CString strTemp;
strTemp.Format("0x%x",nValue);
CString strHex = GetString(strSection, strItem, strTemp);
DWORD nRetValue = 0;
sscanf(strHex, "%x", &nRetValue);
return nRetValue;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// 函數名 : GetString()
// 類名 : CIniCfgFile
// 功能描述 : 讀取字符串型參數值
///////////////////////////////////////////////////////////////////////////////////////////////////
CString CIniCfgFile::GetString(CString strSection, CString strItem, CString strValue)
{
// 文件不存在或者內容為空,直接返回默認值
if (m_bFileExist == false || m_strReadArray.GetSize()<=0 )
return strValue;
// 處理部分
int i = 0;
int nFileLines = (int)m_strReadArray.GetSize();
CString strLine, str;
while(i<nFileLines)
{
strLine = m_strReadArray.GetAt(i++);
strLine.TrimLeft();
if(strLine.GetAt(0)=='[') //查找Section,第一個必須為[
{
str=strLine.Left(strLine.Find("]"));//去掉]右邊
str=str.Right(str.GetLength()-str.Find("[")-1);//去掉[左邊
str.TrimLeft();
str.TrimRight();
if(strSection == str)//找到Section
{
while(i<nFileLines)
{
strLine = m_strReadArray.GetAt(i++);
strLine.TrimLeft();
if(strLine.GetAt(0)=='[')
return strValue;//如果到達下一個[],即找不到,返回默認值
str = strLine.Left(strLine.Find("="));//去掉=右邊
str.TrimLeft();
str.TrimRight();
if(strItem == str)//找到Item
{
str=strLine.Right(strLine.GetLength()-strLine.Find("=")-1);//去掉=左邊
str.Trim();
if (str.GetLength()>0)
return str;
else
return strValue;
}
}
return strValue;//找不到,返回默認值
}
}
}
return strValue;//找不到,返回默認值
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// 函數名 : GetReverseString()
// 類名 : CIniCfgFile
// 功能描述 : 讀取字符串型參數值
///////////////////////////////////////////////////////////////////////////////////////////////////
CString CIniCfgFile::GetReverseString(CString strSection, CString strItem, CString strValue)
{
// 文件不存在或者內容為空,直接返回默認值
if (m_bFileExist == false || m_strReadArray.GetSize()<=0 )
return strValue;
// 處理部分
int i = 0;
int nFileLines = (int)m_strReadArray.GetSize();
CString strLine, str;
while(i<nFileLines)
{
strLine = m_strReadArray.GetAt(i++);
strLine.TrimLeft();
if(strLine.GetAt(0)=='[') //查找Section,第一個必須為[
{
str=strLine.Left(strLine.Find("]"));//去掉]右邊
str=str.Right(str.GetLength()-str.Find("[")-1);//去掉[左邊
str.TrimLeft();
str.TrimRight();
if(strSection == str)//找到Section
{
while(i<nFileLines)
{
strLine = m_strReadArray.GetAt(i++);
strLine.TrimLeft();
if(strLine.GetAt(0)=='[')
return strValue;//如果到達下一個[],即找不到,返回默認值
str = strLine.Right(strLine.GetLength() - strLine.Find("=") - 1);//去掉=左邊
str.TrimLeft();
str.TrimRight();
if(strItem == str)//找到Item
{
str=strLine.Left(strLine.Find("="));//去掉=右邊
str.Trim();
if (str.GetLength()>0)
return str;
else
return strValue;
}
}
return strValue;//找不到,返回默認值
}
}
}
return strValue;//找不到,返回默認值
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// 函數名 : GetTokenString()
// 類名 : CIniCfgFile
// 功能描述 :
///////////////////////////////////////////////////////////////////////////////////////////////////
int CIniCfgFile::GetTokenString(CString strInput)
{
if (m_strTokenArray.GetSize()>0)
m_strTokenArray.RemoveAll();
int nCount = 0;
CString resToken;
int curPos= 0;
do
{
resToken= strInput.Tokenize(" , ", curPos).Trim();
if(resToken != "")
{
m_strTokenArray.Add(resToken);
nCount++;
}
} while(resToken != "");
return nCount;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -