⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inicfgfile.cpp

📁 提供数据的实时下载和定时下载
💻 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 + -