VC++6.0自定义ini配置文件读写

1,实现功能,自定义读取配置文件,支持多配置,使用键值对模式

CIniConfig.h

#pragma once

//#include "CCommConstDef.h"
#include "StdAfx.h"

#ifndef _CINI_CONFIG_HEADER_FILE_
#define _CINI_CONFIG_HEADER_FILE_
class CIniConfigItem;
class CIniConfig;
class CConfigBean;

class CIniConfigItem
{
public:
	CString strAppName;
	CString strKey;
	CString strValue;
	CString strComment;
	
	CIniConfigItem(CString istrAppName,CString istrKey,CString istrValue,CString istrComment)
	{
		strAppName = istrAppName;
		strKey =  istrKey;
		strValue = istrValue;
		strComment = istrComment;
	}
};

class CIniConfig
{
public:
	static bool IsDirectoryExists(CString const& path);
static CString GetAppPath();
	CString g_strFileName;
	CString GetConfigName();
	CIniConfig(CString strFileName);
	~CIniConfig();
	void LoadIniFile(CString strFileName);
	BOOL SaveIniToFile(CString strSaveName);
	CIniConfigItem * GetItemByKey(CString strKeyName);
	void ClearAllItem();
	//BOOL LoadPar(CConfigBean *pCfg);
	
	void AddNewItemAtTail(CString strAppName,CString strKey,CString strValue,CString strComment);
	bool GetItemValue(CString strKey,CString &strValue);
public:
	CPtrList g_listItems;
	CString strFileName;
	
};
#endif

CIniConfig.cpp

#include "stdafx.h"
#include "CIniConfig.h"
//#include "sqlite\sqliteBean.h"
 bool CIniConfig::IsDirectoryExists(CString const& path)
{ //判断是否存在
	WIN32_FIND_DATA findFileData; 
	HANDLE hFind = ::FindFirstFile(path, &findFileData); 
	if (INVALID_HANDLE_VALUE == hFind) 
	{ 
		return FALSE; 
	}/*else
	{
		CloseHandle(hFind);
	}*/
	
	return true;
}
 CString CIniConfig::GetAppPath()
{
	char szLongPathName[_MAX_PATH];  
	
	GetModuleFileName(NULL, szLongPathName, _MAX_PATH);
	
	CString strRunDir = szLongPathName;
	
	int nPos = strRunDir.ReverseFind('\\');
	
	if(nPos != -1)
	{ 
		strRunDir = strRunDir.Left(nPos+1);
	}
	
	if(strRunDir.IsEmpty())
	{
		
		char szPath[_MAX_PATH];
		GetCurrentDirectory(_MAX_PATH,szPath);
		strcat(szPath,"\\"); 		
		strRunDir=szPath;
	}

	return strRunDir;
	
}
CIniConfig::CIniConfig(CString strFileName)
{

	LoadIniFile(GetAppPath()+strFileName);
}

CIniConfig::~CIniConfig()
{
	ClearAllItem();
}

void CIniConfig::ClearAllItem()
{	
	POSITION pos = NULL,temp = NULL;;
	pos = g_listItems.GetHeadPosition();

	while(pos != NULL)
	{
		temp = pos;
		CIniConfigItem * pItem = (CIniConfigItem*)g_listItems.GetNext(pos);

		if(NULL != pItem)
		{
			delete pItem;                // 删除前应该释放分配的空间
			g_listItems.RemoveAt(temp);      // 删除元素

		}
	}
}
void CIniConfig::LoadIniFile(CString strFileName)
{
	//打开文件
	g_strFileName = strFileName;
	CStdioFile file;
	
	if("" == strFileName)
	{
		return;
	}

	if(!file.Open(strFileName,CFile::modeRead|CFile::typeText))
	{
		return;
	}

	//逐行读取字符串

	CString strReadLine;
	CString strAppName;
	CString strKey;
	CString strValue;
	CString strComment;
	int iFindEquelDot = -1;

	int iFileLen = (int)(file.GetLength() + 1);
	char *pcText = new char[iFileLen];
	*(pcText +iFileLen-1) = 0;
	file.Read(pcText,iFileLen-1);
	
	CString strTotalLine(pcText);
	delete pcText;
	pcText = NULL;
	int iFindNextLine = strTotalLine.Find('\n',0);

	while(-1 != iFindNextLine )
	{
		strReadLine = strTotalLine.Left(iFindNextLine);
		strTotalLine = strTotalLine.Right(strTotalLine.GetLength() - iFindNextLine-1);
	
		iFindNextLine = strTotalLine.Find('\n',0);
		
		//处理一行数据
		//strReadLine = strReadLine.Trim();
		strReadLine.TrimLeft();
		strReadLine.TrimRight();
		if(strReadLine.GetLength()<1)
		{
			continue;
		}

		if('['== strReadLine.GetAt(0) && ']'==strReadLine.GetAt(strReadLine.GetLength()-1))
		{
			strAppName = strReadLine.Left(strReadLine.GetLength() -1);
			strAppName = strAppName.Right(strAppName.GetLength()-1);
			//strAppName = strAppName.Trim();
			strAppName.TrimLeft();
			strAppName.TrimRight();
		}
		else if('#'== strReadLine.GetAt(0))
		{
			strComment += strReadLine;
			strComment += "\r\n";
		}else if(strReadLine.Find('=',1)>0)
		{
			//键值对
			iFindEquelDot = strReadLine.Find('=',1);
			int iLen = strReadLine.GetLength();
			strKey = strReadLine.Left(iFindEquelDot);
			//strKey = strKey.Trim();
			strKey.TrimLeft();
			strKey.TrimRight();
			strValue = strReadLine.Right(strReadLine.GetLength() - iFindEquelDot - 1);
			//strValue = strValue.Trim();
			strValue.TrimLeft();
			strValue.TrimRight();
			if('\"'== strValue.GetAt(0) && '\"'==strValue.GetAt(strValue.GetLength()-1))
			{
				strValue = strValue.Left(strValue.GetLength() -1);
				strValue = strValue.Right(strValue.GetLength()-1);
			//	strValue = strValue.Trim();
				strValue.TrimLeft();
				strValue.TrimRight();
			}


			CIniConfigItem *pItem = new CIniConfigItem(strAppName,strKey,strValue,strComment);
			g_listItems.AddTail(pItem);
			strComment = "";
		}

	}
	//关闭文件

	file.Close();
}
bool CIniConfig::GetItemValue(CString strKey,CString &strValue)
{
	strValue = "";
	CIniConfigItem * pItem = GetItemByKey(strKey);
	if(NULL == pItem)
	{
		return false;
	}else
	{
		strValue=	pItem->strValue ;
		
	}
	return true;

}
void CIniConfig::AddNewItemAtTail(CString strAppName,CString strKey,CString strValue,CString strComment)
{

	CIniConfigItem * pItem = GetItemByKey(strKey);
	if(NULL == pItem)
	{
		pItem = new CIniConfigItem(strAppName,strKey,strValue,strComment);
		g_listItems.AddTail(pItem);
	}else
	{
		pItem->strValue = strValue;		
	}
	
}
BOOL CIniConfig::SaveIniToFile(CString strFileName)
{
	//打开文件

	CStdioFile file;
	
	if(0!=strFileName.Right(4).CompareNoCase(".ini"))
	{
		strFileName+=".ini";
	}

	if(!file.Open(strFileName,CFile::modeCreate|CFile::modeReadWrite|CFile::typeText))
	{
		return FALSE;
	}

	//逐行读取字符串
	CString strLine;
	CString strAppName="";
	CString strKey;
	CString strValue;
	CString strComment;



	POSITION pos = NULL,temp = NULL;;
	CIniConfigItem * pItem = NULL;
	pos = g_listItems.GetHeadPosition();

	while(pos != NULL)
	{
		temp = pos;
		pItem = (CIniConfigItem*)g_listItems.GetNext(pos);

		if(NULL != pItem)
		{
			if(0 != strAppName.CompareNoCase(pItem->strAppName))
			{
				strAppName = pItem->strAppName;
				file.WriteString("["+strAppName + "]\n");
			}

			if(pItem->strComment.GetLength()>0)
			{
				file.WriteString(pItem->strComment +"\n");
				
			}


			file.WriteString(pItem->strKey + "=" +pItem->strValue + "\n");

		}
	}

	file.Close();
	return TRUE;
}

CIniConfigItem * CIniConfig::GetItemByKey(CString strKeyName)
{
	POSITION pos = NULL,temp = NULL;;
	CIniConfigItem * pItem = NULL;
	pos = g_listItems.GetHeadPosition();

	while(pos != NULL)
	{
		temp = pos;
		pItem = (CIniConfigItem*)g_listItems.GetNext(pos);

		if(NULL != pItem && 0 == pItem->strKey.CompareNoCase(strKeyName))
		{
			return pItem;
		}
	}
	return NULL;
}
CString CIniConfig::GetConfigName()
{
	CString strName ="";
	int iLastFind = g_strFileName.ReverseFind('\\');
	if(-1 !=  iLastFind)
	{
		strName = g_strFileName.Right(g_strFileName.GetLength() - iLastFind);
		strName =strName.Left(strName.GetLength() - 4);
	}
	return strName;
}
#if 0
BOOL CIniConfig::LoadPar(CConfigBean *pCfg)
{
	//加载文件 构造时已经添加
	if(g_listItems.GetSize()<1)
	{
		return FALSE;
	}
	//更新参数
	CIniConfigItem *pItem = GetItemByKey("Type");
	if(NULL != pItem)
	{
		//SFP ,XFP ,X2/XENPAK,Other
		if(0 == pItem->strValue.CompareNoCase("SFP"))
		{
			pCfg->Type = constDeviceTypeSFP;
		}else if(0 == pItem->strValue.CompareNoCase("XFP"))
		{
			pCfg->Type = constDeviceTypeXFP;
		}
		else if(0 == pItem->strValue.CompareNoCase("QSFP"))
		{
			pCfg->Type = constDeviceTypeQSFP;
		}else if(0 == pItem->strValue.CompareNoCase("Ohter"))
		{
			pCfg->Type = constDeviceTypeOther;
		}
		
	}
/*
	pItem = GetItemByKey("classifiedName");
	if(NULL != pItem)
	{
		pCfg->classifiedName = pItem->strValue;
	}

	pItem = GetItemByKey("name");
	if(NULL != pItem)
	{
		pCfg->name = pItem->strValue;
	}
*/
	pItem = GetItemByKey("PageSize");
	if(NULL != pItem)
	{
		pCfg->PageSize = atoi(pItem->strValue);
	}

	pItem = GetItemByKey("I2CRate");
	if(NULL != pItem)
	{
		pCfg->I2CRate = atoi(pItem->strValue);
	}

	pItem = GetItemByKey("SaveAddr");
	if(NULL != pItem)
	{
		pCfg->SaveAddr = strtol(pItem->strValue,NULL,16);;
	}

	pItem = GetItemByKey("EnterAddr");
	if(NULL != pItem)
	{
		pCfg->EnterAddr = strtol(pItem->strValue,NULL,16);;
	}

	pItem = GetItemByKey("EnterLen");
	if(NULL != pItem)
	{
		pCfg->EnterLen = atoi(pItem->strValue);
	}

	pItem = GetItemByKey("SaveLen");
	if(NULL != pItem)
	{
		pCfg->SaveLen = atoi(pItem->strValue);
	}

	pItem = GetItemByKey("EnterPassWord");
	if(NULL != pItem)
	{
		pCfg->EnterPassWord = strtol(pItem->strValue,NULL,16);;
	}

	pItem = GetItemByKey("SavePassWord");
	if(NULL != pItem)
	{
		pCfg->SavePassWord = strtol(pItem->strValue,NULL,16);;
	}

	pItem = GetItemByKey("EnterWait");
	if(NULL != pItem)
	{
		pCfg->EnterWait = atoi(pItem->strValue);
	}

	pItem = GetItemByKey("SaveWait");
	if(NULL != pItem)
	{
		pCfg->SaveWait = atoi(pItem->strValue);
	}

	pItem = GetItemByKey("TableAddr");
	if(NULL != pItem)
	{
		pCfg->TableAddr = strtol(pItem->strValue,NULL,16);;
	}
	pItem = GetItemByKey("CodeAddr");
	if(NULL != pItem)
	{
		pCfg->CodeAddr = strtol(pItem->strValue,NULL,16);;
	}

	pItem = GetItemByKey("CodeLength");
	if(NULL != pItem)
	{
		pCfg->CodeLength = atoi(pItem->strValue);
	}
	return TRUE;
}
#endif

使用案例上资源

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值