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

📄 ini.cpp

📁 TabBars的开源源码
💻 CPP
字号:
// Ini.cpp: implementation of the CIni class.
/*********************************************************************
*	class:		class for Ini type file
*	Author:		orbit
*	Date:		1999-09-21
*	Contact us:	support@winmsg.com
*	Web Page: http://www.winmsg.com/cn/orbit.htm (for Chinese version)
*						http://www.winmsg.com/orbit.htm (for English version)
**********************************************************************/
#include "stdafx.h"
#include "Ini.h"

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

#include <stdlib.h>

#ifndef _MAC_
CString CIni::m_csLineEnd = _T("\r\n");
#else
CString CIni::m_csLineEnd = _T("\n");
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CIni::CIni()
{
}

CIni::CIni(LPCTSTR lpszFileName)
{
	Read(lpszFileName);
}

CIni::~CIni()
{
	Clear();
}

/*********************************************************************
 * Function Name : CIni::Read
 * Explain : read all configuration to a buffer
 * Parameter list:
 * LPCTSTR cFileName -- full pathname of ini file
 * Return :
 * BOOL  -- return true if operate successfully
 * Author : orbit 
 * Time : 2002-03-12 15:12:43 
*********************************************************************/
BOOL CIni::Read(LPCTSTR cFileName)
{
	Clear();
	TCHAR szbuf[1024];
	CStdioFile file;
	if(!file.Open(cFileName, CFile::modeRead|CFile::shareDenyNone,NULL))
	   return FALSE;
	while(file.ReadString(szbuf,1023) != NULL)
	{
		CString cs(szbuf);
		cs = cs.Left(cs.GetLength() - 1);
		m_csList.Add(cs);
	}
	file.Close();
	return TRUE;
}

/*********************************************************************
 * Function Name : CIni::Write
 * Explain : write the infomation of buffer to ini file
 * Parameter list:
 * LPCTSTR cFileName -- full pathname of ini file to write
 * return value:
 * BOOL  -- return true if operate successfully
 * Author : orbit 
 * Time : 2002-03-12 15:15:50 
*********************************************************************/
BOOL CIni::Write(LPCTSTR cFileName)
{
	ASSERT(cFileName);
	CStdioFile file;
	if(!file.Open(cFileName, CFile::modeWrite|CFile::modeCreate,NULL))
	   return FALSE;
	int max = m_csList.GetSize();
	for(int i = 0; i < max; i++)
	{
		file.WriteString(m_csList.GetAt(i) + _T("\n"));
	}
	file.Close();

	return TRUE;
}

/*********************************************************************
 * Function Name : CIni::AddRemarkLine
 * Explain:
 * Parameter list:
 * LPCTSTR lpszRemarkLine -- the remark text you want write to ini file
 * int nPos -- line position
 * return value:
 * void  -- void
 * author : orbit 
 * time : 2002-03-12 15:18:01 
*********************************************************************/
void CIni::AddRemarkLine(LPCTSTR lpszRemarkLine,int nPos)
{
	CString strTmp;
	strTmp.Format(_T("//%s"),lpszRemarkLine);
	if(nPos == 0)
		m_csList.InsertAt(0,strTmp,1);
	else
		m_csList.Add(strTmp);
}

void CIni::AddRemarkLine(CStringArray & strARemarkLine,int nPos)
{
	CStringArray strATmp;
	int size = strARemarkLine.GetSize();
	for(int i = 0; i < size; i++)
	{
		strATmp.Add(_T("//") + strARemarkLine[i]);
	}
	if(nPos == 0)
		m_csList.InsertAt(0,&strATmp);
	else
	{
		size = strATmp.GetSize();
		for(i = 0; i < size; i++)
		{
			m_csList.Add(strATmp[i]);
		}
	}

	strATmp.RemoveAll();
}
// **********************************************************************************

void CIni::Clear()
{
	m_csList.RemoveAll();
}

// **********************************************************************************

/*********************************************************************
 * Function Name:CIni::FindSection
 * Explain:get the line position of a section
 * Parameter list:
 * LPCTSTR cSection -- section name
 * return:
 * int  -- line position of this position
 * author: orbit 
 * time : 2002-03-12 15:20:00 
*********************************************************************/
int CIni::FindSection(LPCTSTR cSection)
{
	int t, max = m_csList.GetSize();
	CString csSection;
	csSection.Format(_T("[%s]"), cSection);

	for (t = 0; t < max; t++)
		if (m_csList.GetAt(t) == csSection) return t;

	return -1;
}

/*********************************************************************
 * Function Name : CIni::InsertSection
 * Explain : insert a new section to the end of a ini file
 * Parameter list:
 * LPCTSTR cSection -- section name
 * return value:
 * int  -- the line position you just insert
 * Author : orbit 
 * Time : 2002-03-12 15:21:27 
*********************************************************************/
int CIni::InsertSection(LPCTSTR cSection)
{
	ASSERT(cSection);
	if (!cSection) return -1;

	int idx = FindSection(cSection);
	if (idx < 0)
	{
		CString csSection;
		csSection.Format(_T("[%s]"), cSection);
		m_csList.Add(_T(""));//insert a empty line between Section s
		idx = m_csList.Add(csSection);
	}
	return idx;
}

int CIni::FindItem(const int iSection, LPCTSTR cItem, CString &csVal)
{
	ASSERT(iSection >= 0);
	ASSERT(cItem);

	int max = m_csList.GetSize(), t;
	CString csItem(cItem), csLook;
	csItem += " = ";
	int iLen = csItem.GetLength();

	for (t = iSection; t < max; t++)
	{
		if (!IsSection(t))
		{
			csLook = m_csList.GetAt(t);
			if (csLook.GetLength() >= iLen)
			{
				if (csLook.Left(iLen) == csItem) 
				{
					if (csLook.GetLength() == iLen) 
						csVal = "";
					else 
						csVal = csLook.Right(csLook.GetLength() - iLen);
					return t;
				}
			}
		}
		else 
			return -1;
	}
	return -1;
}

int CIni::FindMultiItem(const int iSection, LPCTSTR cItem, CString &csVal)
{
	ASSERT(iSection >= 0);
	ASSERT(cItem);

	int max = m_csList.GetSize(), t, i;
	CString csItem(cItem), csLook;
	csItem += " = \"";
	int iLen = csItem.GetLength();

	for (t = iSection; t < max; t++)
	{
		if (!IsSection(t))
		{
			csLook = m_csList.GetAt(t);
			if (csLook == csItem)
			{
				csVal = "";
				for (i = t + 1; i < max; i++)
				{
					csLook = m_csList.GetAt(i);
					if (csLook == '\"' || IsSection(i)) 
					{
						i = max; 
					}
					else 
					{
						if (csVal != "") csVal += m_csLineEnd;
						csVal += csLook;
					}
				}
				return t;
			}
		}
		else return -1;
	}
	return -1;
}

BOOL CIni::IsSection(const int iSection)
{
	ASSERT(iSection >= 0 && iSection < m_csList.GetSize());
	if (iSection >= 0 && iSection < m_csList.GetSize())
	{
		CString csItem = m_csList.GetAt(iSection);
		if (csItem.GetLength() > 2 && csItem.Left(1) == '[' && csItem.Right(1) == ']') 
			return TRUE;
	}
	return FALSE;
}

/*********************************************************************
 * Function Name : CIni::RemoveSection
 * Explain : remove a section and all item
 * Parameter list:
 * LPCTSTR cSection -- section name
 * return :
 * BOOL  -- return true if the section was delete successfully
 * Author: orbit 
 * Time : 2002-03-12 15:24:15 
*********************************************************************/
BOOL CIni::RemoveSection(LPCTSTR cSection)
{
	int idx = FindSection(cSection);
	if (idx >= 0)
	{
		for (;;)
		{
			m_csList.RemoveAt(idx);
			if (idx >= m_csList.GetSize()) 
				return TRUE;
			if (IsSection(idx)) 
				return TRUE;
		}
	}
	return TRUE;
}

void CIni::RemoveMultiLineItem(const int idx)
{
	int max = m_csList.GetSize(), t;
	CString csLook;

	for (t = idx; t < max; t++)
	{
		if (!IsSection(t))
		{
			csLook = m_csList.GetAt(t);
			if (csLook == '\"')
			{
				m_csList.RemoveAt(t);
				return;
			}
			m_csList.RemoveAt(t);
		}
		else 
			return;
	}
}

/*********************************************************************
 * Function Name : CIni::GetEndOfSection
 * Explain : get the end line of this section
 * Parameter List:
 * int nSectionIndex -- line position of this section
 * Return:
 * int  -- the end line of this section
 * Author : orbit 
 * Time : 2002-03-12 15:25:36 
*********************************************************************/
int CIni::GetEndOfSection(int nSectionIndex)
{
	int max = m_csList.GetSize();
	CString strTmp;
	int nEndOfSection = max;
	for (int t = nSectionIndex + 1; t < max; t++)
	{
		strTmp = m_csList.GetAt(t);
		int size = strTmp.GetLength();
		if(size <= 0)
		{
			nEndOfSection = t;
			break;
		}

		if ((size > 0) && IsSection(t))
		{
			nEndOfSection = t;
			break;
		}
	}

	return nEndOfSection;
}
// **********************************************************************************

BOOL CIni::SetBoolValue(LPCTSTR cSection, LPCTSTR cItem, const BOOL bVal)
{
	int idx = InsertSection(cSection);
	if (idx >= 0)
	{
		CString csVal;
		int iIdx = FindItem(idx+1, cItem, csVal);
		csVal.Format(_T("%s = %s"), cItem, bVal ? _T("TRUE") : _T("FALSE"));
		if (iIdx >= 0) 
			m_csList.SetAt(iIdx, csVal);
		else
		{
			m_csList.InsertAt(GetEndOfSection(idx), csVal);
//			m_csList.InsertAt(idx+1, csVal);
		}
		return TRUE;
	}
	return FALSE;
}

BOOL CIni::SetValue(LPCTSTR cSection, LPCTSTR cItem, LPCTSTR cVal)
{
	int idx = InsertSection(cSection);
	if (idx >= 0)
	{
		CString csVal;
		int iIdx = FindItem(idx+1, cItem, csVal);
		csVal.Format(_T("%s = %s"), cItem, cVal);
		if (iIdx >= 0) 
			m_csList.SetAt(iIdx, csVal);
		else 
			m_csList.InsertAt(GetEndOfSection(idx), csVal);
		return TRUE;
	}
	return FALSE;
}

BOOL CIni::SetDwordValue(LPCTSTR cSection, LPCTSTR cItem, const DWORD lVal)
{
	int idx = InsertSection(cSection);
	if (idx >= 0)
	{
		CString csVal;
		int iIdx = FindItem(idx+1, cItem, csVal);
		csVal.Format(_T("%s = %d"), cItem, (int)lVal);
		if (iIdx >= 0) 
			m_csList.SetAt(iIdx, csVal);
		else 
			m_csList.InsertAt(GetEndOfSection(idx), csVal);
		return TRUE;
	}
	return FALSE;
}

BOOL CIni::SetValue(LPCTSTR cSection, LPCTSTR cItem, const int iVal)
{
	int idx = InsertSection(cSection);
	if (idx >= 0)
	{
		CString csVal;
		int iIdx = FindItem(idx+1, cItem, csVal);
		csVal.Format(_T("%s = %d"), cItem, iVal);
		if (iIdx >= 0) 
			m_csList.SetAt(iIdx, csVal);
		else 
			m_csList.InsertAt(GetEndOfSection(idx), csVal);
		return TRUE;
	}
	return FALSE;
}

BOOL CIni::SetMultiValue(LPCTSTR cSection, LPCTSTR cItem, LPCTSTR cVal)
{
	int idx = InsertSection(cSection);
	if (idx >= 0)
	{
		CString csVal;
		int iIdx = FindItem(idx+1, cItem, csVal);
		csVal.Format(_T("%s = %s"), cItem, cVal);
		char * c = csVal.LockBuffer();

		int i = csVal.Find(_T('\r'));
		while (i >= 0)
		{
			c[i] = (char)128;
			i = csVal.Find(_T('\r'));
		}
		i = csVal.Find(_T('\n'));
		while (i >= 0)
		{
			c[i] = '|';
			i = csVal.Find(_T('\n'));
		}

		csVal.UnlockBuffer();
		if (iIdx >= 0) 
			m_csList.SetAt(iIdx, csVal);
		else 
			m_csList.InsertAt(GetEndOfSection(idx), csVal);
		return TRUE;
	}
	return FALSE;
}

// **********************************************************************************

BOOL CIni::GetBoolValue(LPCTSTR cSection, LPCTSTR cItem, BOOL &bVal, const BOOL bDefault)
{
	int idx = FindSection(cSection);
	if (idx >= 0)
	{
		CString csVal;
		if (FindItem(idx+1, cItem, csVal) > 0)
		{
			if (csVal.Find(_T("TRUE")) >= 0) 
				bVal = TRUE; 
			else 
				bVal = FALSE;
			return TRUE;
		}
	}
	bVal = bDefault;
	return FALSE;
}

BOOL CIni::GetValue(LPCTSTR cSection, LPCTSTR cItem, CString &cVal,const CString strDefault)
{
	int idx = FindSection(cSection);
	if (idx >= 0)
	{
		if (FindItem(idx+1, cItem, cVal) > 0)
			return TRUE;
	}
	cVal = strDefault;
	return FALSE;
}

//PSTR
BOOL CIni::GetValue(LPCTSTR cSection, LPCTSTR cItem, LPTSTR szVal, LPCTSTR szDefault)
{
	int idx = FindSection(cSection);
	if (idx >= 0)
	{
		CString cVal;
		if (FindItem(idx+1, cItem, cVal) > 0)
		{
			wsprintf(szVal,_T("%s"),cVal);
			return TRUE;
		}
	}
	_tcscpy(szVal,szDefault);
	return FALSE;
}

BOOL CIni::GetDwordValue(LPCTSTR cSection, LPCTSTR cItem, DWORD &lVal, const DWORD dwDefault)
{
	int idx = FindSection(cSection);
	if (idx >= 0)
	{
		CString csVal;
		if (FindItem(idx+1, cItem, csVal) > 0)
		{
			lVal = (DWORD) atol(csVal);
			return TRUE;
		}
	}
	lVal = dwDefault;
	return FALSE;
}

BOOL CIni::GetValue(LPCTSTR cSection, LPCTSTR cItem, int &iVal, const int iDefault)
{
	int idx = FindSection(cSection);
	if (idx >= 0)
	{
		CString csVal;
		if (FindItem(idx+1, cItem, csVal) > 0)
		{
			iVal = (int) atoi(csVal);
			return TRUE;
		}
	}
	iVal = iDefault;
	return FALSE;
}

BOOL CIni::GetMultiValue(LPCTSTR cSection, LPCTSTR cItem, CString &cVal)
{
	int idx = FindSection(cSection);
	if (idx >= 0)
	{
		if (FindItem(idx+1, cItem, cVal) > 0)
		{
			char * ch = cVal.LockBuffer();
			int i = cVal.Find((char)128);
			while (i >= 0)
			{
				ch[i] = _T('\r');
				i = cVal.Find((char)128);
			}
			i = cVal.Find(_T('|'));
			while (i >= 0)
			{
				ch[i] = _T('\n');
				i = cVal.Find(_T('|'));
			}
			cVal.UnlockBuffer();
			return TRUE;
		}
	}
	return FALSE;
}

⌨️ 快捷键说明

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