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

📄 propertyfile.cpp

📁 运行debug下的cisoca.exe即可。 由于在vc6.0下开发的
💻 CPP
字号:
// PropertyFile.cpp: implementation of the CPropertyFile class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PropertyFile.h"
#include <fstream.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

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

CPropertyFile::CPropertyFile(char *pszFileName)
{
	pHead			= NULL;		
	char *p			= NULL;
	NODE *pCurrent	= NULL;
	unsigned int  i	= 0;	
	char key[256]	= {0};
	char value[256] = {0};	
	char buf[MAX_CONF_LINE] = {0};
	ifstream ifs(pszFileName);
	while(!ifs.eof())
	{
		ifs.getline(buf,MAX_CONF_LINE);
		i = 0;
		p=buf;
		// 去掉前面的空格
		while(isspace(*p))p++;
		// 如果是注释跳过
		if(*p == '#')
		{
			ifs.getline(buf,MAX_CONF_LINE);
			continue;
		}
		// 下面两个拷贝要注意超界,(改进)空格的去除
		if(strstr(p,"=") == NULL ) 
		{
			//ifs.getline(buf,MAX_CONF_LINE);
			continue;
		}
		// 将键值拷到key中
		while(*p != '=' && i < strlen(buf))
		{
			memcpy(&key[i],p,1);
			p++;
			i++;
		}
		key[i] = '\0';
		trim(key);
		p++;
		i=0;
		// 将值拷到value中
		while(*p != '\0')
		{
			memcpy(&value[i],p,1);
			p++;
			i++;
		}
		value[i] = '\0';
		trim(value);
		// 说明现有还没有结点(第一次)
		if(pHead == NULL)
		{
			// 生成新的结点,并赋值
			pCurrent = new NODE;
			pCurrent->pNext  = NULL;
			pCurrent->pKey   = new char[strlen(key)+1];
			pCurrent->pValue = new char[strlen(value)+1];
			if(!pCurrent->pKey || !pCurrent->pValue)return ;
			strcpy(pCurrent->pKey,key);
			strcpy(pCurrent->pValue,value);
			// 保存头结点
			pHead = pCurrent;
		}
		else
		{
			// 生成新的结点
			NODE *tm = new NODE;
			tm->pKey   = new char[strlen(key)+1];
			tm->pValue = new char[strlen(value)+1];	
			if(!tm->pKey || !tm->pValue)return;
			// 并赋值		
			tm->pNext = NULL;			
			strcpy(tm->pKey,key);
			strcpy(tm->pValue,value);
			// 当前工作指针指向新生成的结点
			pCurrent->pNext = tm;
			// 工作指针前移
			pCurrent = pCurrent->pNext;
		}
		//ifs.getline(buf,MAX_CONF_LINE);
	}
	ifs.close();
}

CPropertyFile::~CPropertyFile()
{
	if(pHead)FreeAll();
}

// 取key 对应的value值

int CPropertyFile::GetValue(char *key,char *value)
{	
	strcpy(value,"");
	NODE *ptm = pHead;
	while(ptm != NULL) 
	{
		if(strcmp(ptm ->pKey ,key) == 0)
		{
			strcpy(value,ptm->pValue);
			//CString str;
			//str.Format("ptm->pkey=%s,key=%s,ptm->value=%s,value=%s",
			//	ptm->pKey,key,ptm->pValue,value);
			//AfxMessageBox(str);
			break;
		}
		ptm = ptm->pNext;
	}	
	return 1;
}

// 测试用的程序

int CPropertyFile::Test()
{
	printf("开始编历所有的结点...\n");
	NODE *ptm = pHead;
	while(ptm != NULL) 
	{
		printf("key = %s\tvalue = %s\n",ptm->pKey,ptm->pValue);
		ptm = ptm->pNext;
	}
	printf("测试完毕!\n");
	return 1;
}

// 释放所有申请的内存

void CPropertyFile::FreeAll()
{
	NODE *ptm = pHead;
	NODE *ptm1 = ptm;
	while(ptm != NULL)
	{
		// 指针先移向下一个结点
		ptm = ptm->pNext;
		// 释放上一个结点的空间
		if(ptm1->pKey) 
		{
			delete[] ptm1->pKey;
			ptm1->pKey = NULL;
		}
		if(ptm1->pValue) 
		{
			delete[] ptm1->pValue;
			ptm1->pValue = NULL;
		}
		ptm1->pNext = NULL;
		if(ptm1) delete ptm1;
		ptm1 = ptm;
	}
	pHead = NULL;
}

// 将修改后的内容存盘

int CPropertyFile::SaveProperty(char *strFileName)
{
	NODE *ptm = pHead;
	if(pHead == NULL) return 0;
	ofstream ofs(strFileName);
	while(ptm  != NULL)
	{
		ofs<<ptm->pKey<<"="<<ptm->pValue<<endl;
		ptm = ptm->pNext;
	}
	ofs.flush();
	ofs.close();
	return 1;
}

// 修改某个键的值

int CPropertyFile::SetValue(char *key,char *value)
{
	NODE *ptm = pHead;
	BOOL bFind = FALSE;
	while(ptm != NULL) 
	{
		if(strcmp(ptm ->pKey ,key) == 0)
		{
			bFind = TRUE;
			// 先释放原来的内存
			if(ptm->pValue)delete[] ptm->pValue;
			// 再申请
			ptm->pValue = new char[strlen(value)+1];
			if(!ptm->pValue) return 0;
			strcpy(ptm->pValue,value);
			return 1;
		}
		ptm = ptm->pNext;
	}
	if(bFind == FALSE) 
		AddItem(key,value);
	return 0;	
}

int CPropertyFile::AddItem(char *key, char *value)
{
	NODE *ptm  = pHead;
	while(ptm != NULL)ptm = ptm->pNext;
	// 生成新的结点
	NODE *tm = new NODE;
	tm->pKey   = new char[strlen(key)+1];
	tm->pValue = new char[strlen(value)+1];	
	if(!tm->pKey || !tm->pValue)return 0;
	// 并赋值		
	tm->pNext = NULL;			
	strcpy(tm->pKey,key);
	strcpy(tm->pValue,value);
	// 当前工作指针指向新生成的结点
	ptm = tm;
	// fix 2004/03/22如果头为空,将头指向新生成的结点,
	// 以便保存文件的时候使用。
	if(pHead == NULL)pHead = ptm;
	return 1;
}

void CPropertyFile::trim(char *buf)
{
	char *p = new char[256];
	char *start = buf;
	if(start == NULL || strlen(start) == 0)return;
	while(isspace(*start)) start++;
	strcpy(p,strrev(start));
	start = p;
	while(isspace(*start)) start++;	
	strcpy(buf,strrev(start));
	if(p) delete p;
	start = NULL;
	p     = NULL;
	return;
}

⌨️ 快捷键说明

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