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

📄 inifile.cpp

📁 能够对INI文件方便的读写
💻 CPP
字号:
// IniFile.cpp: implementation of the CIniFile class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "IniFile.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CIniFile::CIniFile()
{
	memset(m_szFileName, 0, sizeof(m_szFileName) );
	memset(m_szSection, 0, sizeof(m_szSection) );
	memset(m_szTempValue, 0, sizeof(m_szTempValue) );
}

CIniFile::~CIniFile()
{
}

BOOL CIniFile::SetFileName(LPCTSTR pszFileName)
{
	if ( pszFileName == NULL )
	{
		return FALSE;
	}
	_tcsncpy(m_szFileName, pszFileName, MAX_PATH);
	return TRUE;
}

LPCTSTR CIniFile::GetFileName() const
{
	return m_szFileName;
}

BOOL CIniFile::SetFileNameByExeDir(LPCTSTR pszTitleName)
{
	::GetModuleFileName(NULL, m_szFileName, MAX_PATH);
	if ( (pszTitleName == NULL) || ( _tcslen(pszTitleName) < 1 ) )
	{
		TCHAR* pFind = _tcsrchr(m_szFileName, '.');
		if (pFind == NULL)
		{
			return FALSE;
		}
		_tcscpy(pFind + 1, _T("ini") );
	}
	else
	{
		TCHAR* pFind = _tcsrchr(m_szFileName, '\\');
		if (pFind == NULL)
		{
			return FALSE;
		}
		_tcscpy(pFind + 1, pszTitleName);
		pFind = _tcsrchr(pszTitleName, '.');
		if (pFind == NULL)
		{
			_tcscat(m_szFileName, _T(".ini") );
		}
	}
	return TRUE;
}

BOOL CIniFile::SetFileNameByCurDir(LPCTSTR pszTitleName)
{
	TCHAR* pFind = NULL;
	TCHAR pszTemp[MAX_PATH] = _T("\0");
	if ( (pszTitleName == NULL) || ( _tcslen(pszTitleName) < 1 ) )
	{
		::GetModuleFileName(NULL, m_szFileName, MAX_PATH);
		pFind = _tcsrchr(m_szFileName, '\\');
		if (pFind == NULL)
		{
			return FALSE;
		}
		_tcscpy(pszTemp, pFind + 1);
		pFind = _tcsrchr(pszTemp, '.' );
		if (pFind == NULL)
		{
			return FALSE;
		}
		_tcscpy(pFind + 1, _T("ini") );
	}
	else
	{
		_tcscpy(pszTemp, pszTitleName);
		pFind = _tcsrchr(pszTitleName, '.');
		if (pFind == NULL)
		{
			_tcscat(pszTemp, _T(".ini") );
		}
	}
	::GetCurrentDirectory(MAX_PATH, m_szFileName);
	if (m_szFileName[_tcslen(m_szFileName)] != '\\')
	{
		_tcscat(m_szFileName, _T("\\") );
	}
	_tcsncat(m_szFileName, pszTemp, MAX_PATH);
	return TRUE;
}


BOOL CIniFile::SetSection(LPCTSTR pszSection)
{
	if ( pszSection == NULL )
	{
		return FALSE;
	}
	_tcsncpy(m_szSection, pszSection, MAX_PATH);
	return TRUE;
}

LPCTSTR CIniFile::GetSection() const
{
	return m_szSection;
}

BOOL CIniFile::DeleteSection(LPCTSTR pszSection)
{
	return ::WritePrivateProfileString(pszSection, NULL, NULL, m_szFileName);
}

BOOL CIniFile::DeleteKey(LPCTSTR pszKey)
{
	return ::WritePrivateProfileString(m_szSection, pszKey, NULL, m_szFileName);
}

BOOL CIniFile::WriteString(LPCTSTR pszKey, LPCTSTR pszValue)
{
	return ::WritePrivateProfileString(m_szSection, pszKey, pszValue, m_szFileName);
}

BOOL CIniFile::WriteInt(LPCTSTR pszKey, int iValue)
{
	_itot(iValue, m_szTempValue, 10);
	return ::WritePrivateProfileString(m_szSection, pszKey, m_szTempValue, m_szFileName);
}

LPCTSTR CIniFile::GetString(LPCTSTR pszKey, LPCTSTR pszDefult, BOOL bWrite )
{
	memset(m_szTempValue, 0, sizeof(m_szTempValue));
	::GetPrivateProfileString(m_szSection, pszKey, pszDefult, m_szTempValue, MAX_PATH, m_szFileName);
	if (bWrite)
	{
		::WritePrivateProfileString(m_szSection, pszKey, m_szTempValue, m_szFileName);
	}
	return m_szTempValue;
}

BOOL CIniFile::GetString(LPCTSTR pszKey, LPCTSTR pszDefult, LPTSTR pszValue, int iSize)
{
	BOOL bRet = FALSE;
	memset(pszValue, 0, iSize);
	bRet = ::GetPrivateProfileString(m_szSection, pszKey, pszDefult, pszValue, iSize, m_szFileName);
	if ( !bRet)
	{
		return FALSE;
	}
	bRet = ::WritePrivateProfileString(m_szSection, pszKey, pszValue, m_szFileName);
	return bRet;
}

int CIniFile::GetInt(LPCTSTR pszKey, int iDefault, BOOL bWrite )
{
	int iValue = 0;
	iValue = ::GetPrivateProfileInt(m_szSection, pszKey, iDefault, m_szFileName);
	if (bWrite)
	{
		_itot(iValue, m_szTempValue, 10);
		::WritePrivateProfileString(m_szSection, pszKey, m_szTempValue, m_szFileName);
	}
	return iValue;
}

#ifdef _MFC_VER
#define INIFILE_MAXBUFLEN   32767
int CIniFile::GetAllSection(CStringArray& arrSection)
{
	arrSection.RemoveAll();

	TCHAR pszBuffer[INIFILE_MAXBUFLEN] = _T("\0");
	DWORD dwRet = ::GetPrivateProfileSectionNames(pszBuffer, INIFILE_MAXBUFLEN, m_szFileName);
	if (dwRet < 1)
	{
		return 0;
	}
	TCHAR* pValue = pszBuffer;
	TCHAR* pEnd = pszBuffer + dwRet - 1;
	while (pValue < pEnd)
	{
		arrSection.Add(pValue);
		pValue += _tcslen(pValue) + 1;
	}
	return arrSection.GetSize();
}

int CIniFile::GetAllKey(CStringArray& arrKey)
{
	arrKey.RemoveAll();

	TCHAR pszBuffer[INIFILE_MAXBUFLEN] = _T("\0");
	DWORD dwRet = ::GetPrivateProfileSection(m_szSection, pszBuffer, INIFILE_MAXBUFLEN, m_szFileName);
	if (dwRet < 1)
	{
		return 0;
	}
	TCHAR* pValue = pszBuffer;
	TCHAR* pEnd = pszBuffer + dwRet - 1;
	while (pValue < pEnd)
	{
		arrKey.Add(pValue);
		pValue += _tcslen(pValue) + 1;
	}
	return arrKey.GetSize();
}

int CIniFile::StringSplit(const CString& strSource, const CString& strSplit, CStringArray& arrResult, BOOL bTrim)
{
	arrResult.RemoveAll();
	int iSplitLen = strSplit.GetLength();
	if (iSplitLen < 1)
	{
		return -1;
	}

	CString strSourceTemp = strSource;
	strSourceTemp.TrimLeft(strSplit);
	strSourceTemp.TrimRight(strSplit);
	strSourceTemp = strSourceTemp + strSplit;

	CString strTemp;
	int iStart = 0;
	int iFind  = 0;
	while (1)
	{
		iFind = strSourceTemp.Find(strSplit, iStart);
		if (iFind < 0)break;
		strTemp  =  strSourceTemp.Mid(iStart, iFind - iStart);
		iStart = iFind + iSplitLen;

		if (bTrim)
		{
			strTemp.TrimLeft();
			strTemp.TrimRight();
			if (strTemp.GetLength() > 0)
			{
				arrResult.Add(strTemp);
			}
		}
		else
		{
			arrResult.Add(strTemp);
		}
	}
	return arrResult.GetSize();
}

int CIniFile::StringSplitEx(LPCTSTR pszSource, LPCTSTR pszMultiSplit, CStringArray &arrResult, BOOL bTrim)
{
	arrResult.RemoveAll();
	if ( (pszSource == NULL) || (pszMultiSplit == NULL) )
	{
		return -1;
	}

	int iSourceLen = _tcslen(pszSource);
	int iSplitLen = _tcslen(pszMultiSplit);
	if ( (iSourceLen < 1) || (iSplitLen < 1) )
	{
		return -1;
	}

	int i = 0;
	TCHAR* pTemp = 0;
	TCHAR* pFind = NULL;
	TCHAR* pSource = (TCHAR*)pszSource;
	TCHAR* pLastSource = pSource;
	TCHAR* pEnd = pSource + iSourceLen;
	CString strTemp;
	while (1)
	{
		pFind = pEnd;
		for (i = 0; i < iSplitLen; i++)
		{
			pTemp = _tcschr(pSource, (int)(*(pszMultiSplit+i)) );
			if ( ( pTemp != NULL ) && (pTemp < pFind) )
			{
				pFind = pTemp;
			}
		}

		pLastSource = pSource;
		pSource = pFind + 1;
		strTemp = CString(pLastSource, pSource-pLastSource-1);
		if (bTrim)
		{
			strTemp.TrimLeft();
			strTemp.TrimRight();
			if (strTemp.GetLength() > 0)
			{
				arrResult.Add(strTemp);
			}
		}
		else
		{
			arrResult.Add(strTemp);
		}

		if (pSource >= pEnd)
		{
			break;
		}
		arrResult.Add(CString(pFind, 1));
	}
	return arrResult.GetSize();
}

#endif // _MFC_VER


// 创建多级目录,比如 " D:\\aa\\bb\\cc "
BOOL CIniFile::CreateDirectoryEx(LPCTSTR pszPath)
{
	if (pszPath == NULL)
	{
		return FALSE;
	}

	// 判断此目录是否已经存在
	DWORD dwRet = 0;
	dwRet = ::GetFileAttributes(pszPath);
	if ( ( dwRet != 0xFFFFFFFF) && ( dwRet & FILE_ATTRIBUTE_DIRECTORY ) )
	{
		return TRUE;
	}

	// 开始创建目录
	int iLen = _tcslen(pszPath);
	if ( (iLen < 1) || ( iLen > MAX_PATH) )
	{
		return FALSE;
	}

	TCHAR szTemp[MAX_PATH+1] = _T("\0");
	_tcscpy(szTemp, pszPath);
	if (szTemp[iLen-1] != '\\')
	{
		szTemp[iLen] = '\\';
	}

	TCHAR* pFind = NULL;
	pFind = _tcschr(szTemp, '\\');
	while (pFind != NULL)
	{
		*pFind = '\0';
		dwRet = ::GetFileAttributes(szTemp);
		if ( ( dwRet == 0xFFFFFFFF) || ( !(dwRet & FILE_ATTRIBUTE_DIRECTORY )) )
		{
			if ( ! ::CreateDirectory(szTemp, NULL))
			{
				return FALSE;
			}
		}
		*pFind = '\\';
		pFind = _tcschr(pFind+1, '\\');
	}
	return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -