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

📄 ini.cpp

📁 eVC開發環境
💻 CPP
字号:
// Ini.cpp: implementation of the CIni class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Ini.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


DWORD GetPrivateProfileStringCE(LPCTSTR lpszAppName,
						  LPCTSTR lpszKeyName,
						  LPCTSTR lpszDefault,
						  LPTSTR lpszRtnString,
						  DWORD nSize,
						  LPCTSTR lpszFileName)
{
	CIni ini;
	if(ini.ReadIniFile(lpszFileName) == FALSE)
	{	DWORD len = lstrlen(lpszDefault);
		if(len < (int)nSize)
		{	lstrcpy(lpszRtnString, lpszDefault);
			return len;
		}else
			return 0;
	}
	CString csApp;
	csApp.Format(_T("[%s]"), lpszAppName);
	int nPos = ini.FindText(csApp);
	if(nPos == -1) 
	{	DWORD len = lstrlen(lpszDefault);
		if(len < nSize)
		{	lstrcpy(lpszRtnString, lpszDefault);
			return len;
		}else
			return 0;
	}
	CString csData = ini.GetKeyValueString(lpszKeyName, nPos);
	DWORD len = csData.GetLength();
	if(len < nSize)
	{	lstrcpy(lpszRtnString, csData);
		return len;
	}
	return 0;
};

BOOL WritePrivateProfileStringCE(LPCTSTR lpszAppName,
							LPCTSTR lpszKeyName,
							LPCTSTR lpszString,
							LPCTSTR lpszFileName)
{
	CIni ini;
	if(ini.ReadIniFile(lpszFileName) == FALSE)
		return FALSE;
	CString csApp;
	csApp.Format(_T("[%s]"), lpszAppName);
	int nPos = ini.FindText(csApp);
	if(nPos == -1) 
		return FALSE;
	if(ini.SetKeyValueString(lpszKeyName, lpszString, nPos))
		return FALSE;
	BOOL ok = ini.WriteIniFile(lpszFileName);
	return ok;
};
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//class CIni
CIni::CIni()
{
}

CIni::~CIni()
{

}

BOOL CIni::ReadIniFile(LPCTSTR lpszFileName)
{
// check ini file path
	CString csPath;
	CString csFile = lpszFileName;
	int nPos = csFile.FindOneOf(_T("\\"));
	if(nPos == -1)
		csPath.Format(_T("\\windows\\%s"), lpszFileName);
	else
		csPath = lpszFileName;
//read ini file	
	CFile f;
	if(f.Open(csPath, CFile::modeRead))
	{
		int len = f.GetLength();
		BYTE* pb = new BYTE[len+1];
		f.Read(pb, len);
		f.Close();
		pb[len] = 0;
		WORD* pWD = (WORD*)pb;
		CString csIni;
		if(*pWD == 0xfffe || *pWD == 0xfeff)
		{
			csIni = ((TCHAR*)(pb+2));
		}
		else
			csIni = pb;
		LoadIni(csIni);
		delete pb;
	}else
		return 0;

	return TRUE;
}

void CIni::LoadIni(CString& csIni)
{
	CString sNewLine;
	while(TRUE)
	{	BOOL bEnd = FALSE;
		int iNext = csIni.FindOneOf(_T("\r\a"));
		if(iNext < 0)
		{//end line
			iNext = csIni.GetLength();
			bEnd = TRUE;
		}
		if ( iNext >= 0 )
		{	if ( iNext == 0 )
			{
				csIni.TrimLeft();
				if(bEnd)
					break;
			}
			else
			{	sNewLine = csIni.Left(iNext);
				sNewLine.TrimRight();
				m_csaIni.Add(sNewLine);
				if(!bEnd)
				{	csIni = csIni.Mid(iNext + 2);
					csIni.TrimLeft();
				}else
					break;
			}
		}
		else
			break;
	}
/*	CString sNewLine;
	while(TRUE)
	{	int iNext = csIni.FindOneOf(_T("\r\a"));
		if ( iNext >= 0 )
		{
			if ( iNext == 0 )
			{	sNewLine = csIni.Left(iNext);
				sNewLine.TrimRight();
				m_csaIni.Add(sNewLine);
				csIni = csIni.Mid(iNext + 2);
//				csIni.TrimLeft(_T("\r\n"));
			}
			else
			{	sNewLine = csIni.Left(iNext);
				sNewLine.TrimRight();
				m_csaIni.Add(sNewLine);
				csIni = csIni.Mid(iNext + 2);
//				csIni.TrimLeft(_T("\r\n"));
			}
		}
		else
			break;
	}
*/
}


int CIni::FindText(LPCTSTR lpszKeyword, int nFrom, int nSearchType)
{
	ASSERT(lpszKeyword!=NULL);
	for (int i = nFrom; i < m_csaIni.GetSize(); i++ )
	{
		CString strCur = m_csaIni.GetAt(i);
		if ( nSearchType == 0 ) //extractly match
		{
			if ( strCur.CompareNoCase(lpszKeyword) == 0 )
			{
				return i;
			}
		}
		else if ( nSearchType == 1 ) // Find left partial
		{
			if ( strCur.Find(lpszKeyword)==0 )
			{
				int nLen = _tcslen(lpszKeyword);
				if ( nLen < strCur.GetLength() )
				{
					TCHAR tEnd = strCur.GetAt(nLen);
					if ( (tEnd == _T('=')) || (tEnd == _T(' ')) ||
						(tEnd == _T('\t')) )
						return i;
				}
			}
		}
	}
	return -1;
}

CString CIni::GetKeyValueString(LPCTSTR lpszTag, int nFrom)
{
	ASSERT(lpszTag!=NULL);
	CString strTemp;
	// Note: maybe we should consider more tolerent condition here
	int nFound = FindText(lpszTag, nFrom, 1);
	if ( nFound > -1 )
	{
		strTemp = m_csaIni.GetAt(nFound);
		int nEq = strTemp.Find(_T("="));
		if ( nEq > -1 )
		{
			strTemp = strTemp.Mid(nEq+1);
			strTemp.TrimLeft();
			return strTemp;
		}
	}
	return _T("");
}

BOOL CIni::SetKeyValueString(LPCTSTR lpszTag, LPCTSTR lpszString, int nFrom)
{
	ASSERT(lpszTag!=NULL);
	ASSERT(lpszString);

	CString strTemp;
	// Note: maybe we should consider more tolerent condition here
	int nFound = FindText(lpszTag, nFrom, 1);
	if ( nFound > -1 )
	{
		strTemp.Format(_T("%s=%s"), lpszTag, lpszString);
		m_csaIni.SetAt(nFound, strTemp);
	}
	return FALSE;
}

BOOL CIni::WriteIniFile(LPCTSTR lpszFileName)
{
	ASSERT(lpszFileName);
	if(lpszFileName == NULL) return FALSE;
	CFile f;
	if(f.Open(lpszFileName, CFile::modeWrite|CFile::modeCreate|CFile::modeNoTruncate))
	{
#ifdef _UNICODE
		WORD wd = 0xfeff;
		f.Write(&wd, sizeof(WORD));
#endif
		int nSize = m_csaIni.GetSize();
		for(int i = 0; i < nSize; ++i)
		{
			CString csData = m_csaIni.GetAt(i);
			CString csBuf;
			csBuf.Format(_T("%s\r\n"), csData);
			f.Write(csBuf.GetBuffer(0), sizeof(TCHAR)*csBuf.GetLength());
		}
		CString csEnd = _T("\r\n");
		f.Write(csEnd.GetBuffer(0), sizeof(TCHAR)*csEnd.GetLength());
		f.Close();
		return TRUE;
	}
	return FALSE;
}

⌨️ 快捷键说明

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