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

📄 mfcini.cpp

📁 上传的是有关mfc的详细介绍
💻 CPP
字号:
/*******************************************************************************
* Copyright (C) 1980-2008 Xumingxsh
* All rights reserved.
*
* 文件名称:	MFCINi.cpp
* 创建日期:	2008-04-28
* 创 建 人:	徐敏荣
* 说    明:	类MFCINi的实现文件
*-------------------------------------------------------------------------------
* 版本		日    期		修改人	修改说明
*-------------------------------------------------------------------------------
* 1.0.0		2008-04-28	徐敏荣		完成初始版本
*******************************************************************************/

#include "stdafx.h"
#include "MFCINi.h"
#include "ToolKit.h"

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

// 构造函数
MFCINi::MFCINi()
{
	this->m_nItemCount = 0;
	this->m_chItems = NULL;
	this->m_chItemValues = NULL;
	this->m_pf = NULL;
}


// 析构函数
MFCINi::~MFCINi()
{
	this->Close();	
}

// 打开文件
bool MFCINi::Open(const CString &strPath)
{
	CString strIniPath = ToolKit::GetAbsPath(strPath);

	this->m_pf = fopen(strIniPath, "a+");
	if (this->m_pf == NULL)
	{
		return false;
	}

	this->m_chItemValues = (char**)malloc(ITEM_COUNT*sizeof(char**));
	this->m_chItems = (char**)malloc(ITEM_COUNT*sizeof(char**));

	for (int i = 0; i < ITEM_COUNT; i++)
	{
		this->m_chItemValues[i] = NULL;
		this->m_chItems[i] = NULL;
	}

	char chReading[500];
	char *pchSeparator = NULL;
	while(this->m_nItemCount < ITEM_COUNT)
	{
		int nEOF = fscanf(this->m_pf, "%s\n", chReading);
		if (nEOF == EOF)
		{
			// 读取成功
			return true;
		}

		pchSeparator = strstr(chReading, "=");
		int nCharCountName = pchSeparator - chReading;
		if (nCharCountName < 0)
		{
			continue;
		}

		int nCharCountValue = strlen(chReading) - nCharCountName - 1;
		chReading[nCharCountName] = 0;

		// 复制数据
		this->m_chItems[this->m_nItemCount] = (char*)malloc(nCharCountName + 1);
		strcpy(this->m_chItems[m_nItemCount], chReading);

		this->m_chItemValues[this->m_nItemCount] = (char*)malloc(nCharCountValue + 1);
		strcpy(this->m_chItemValues[this->m_nItemCount], chReading + nCharCountName + 1);

		this->m_nItemCount++;
	}	

	if (this->m_pf != NULL)
	{
		fclose(this->m_pf);
		this->m_pf = NULL;
	}

	return true;
}

// 读取配置项
char* MFCINi::Read(char *nItem)
{
	if (this->m_nItemCount == 0)
	{
		return NULL;
	}	
	int i = 0;
	while (strcmp(this->m_chItems[i], nItem) != 0)
	{
		i++;
		if (i == this->m_nItemCount)
		{
			return NULL;
		}
	}
	return this->m_chItemValues[i];
}

// 关闭文件
void MFCINi::Close()
{
	for (int i = 0; i < this->m_nItemCount; i++)
	{
		// 释放第i个配置项内存
		if (this->m_chItems[i] != NULL)
		{
			free(this->m_chItems[i]);
			this->m_chItems[i] = NULL;
		}

		// 释放第i个值内存
		if (this->m_chItemValues[i] != NULL)
		{
			free(this->m_chItemValues[i]);
			this->m_chItemValues[i] = NULL;
		}
	}

	// 释放配置项内存
	if (this->m_chItems != NULL)
	{
		free(this->m_chItems);
		this->m_chItems = NULL;
	}

	// 释放值内存
	if (this->m_chItemValues != NULL)
	{
		free(this->m_chItemValues);
		this->m_chItemValues = NULL;
	}
}

⌨️ 快捷键说明

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