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

📄 mfcctrlini.cpp

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

#include "stdafx.h"
#include "MFCCtrlIni.h"

#include "ToolKit.h"

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

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

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

// 打开文件
bool MFCCtrlIni::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(CTR_ITEM_COUNT*sizeof(char**));
	this->m_chItems = (char**)malloc(CTR_ITEM_COUNT * sizeof(char**));

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

	char chReading[500];
	char *pchSeparator = NULL;
	while(this->m_nItemCount < CTR_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;
}

// 关闭文件
void MFCCtrlIni::Close()
{
	if (this->m_pf != NULL)
	{
		fclose(this->m_pf);
		this->m_pf = NULL;
	}

	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;
	}

	this->m_nItemCount = 0;
}

⌨️ 快捷键说明

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