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

📄 profile.cpp

📁 联通接收发送新程序
💻 CPP
字号:
/**************************************************************************/
/*	Copyright(C) 2001 by Nanjing XINWANG TEC CO.,LTD.					  */
/**************************************************************************/
/*	Name:		profile.cpp				Version: 1.0.0					  */
/*	Created by:	Zhangbo					Date: 2001-12-04				  */
/*	Content:	implementation of class Profile							  */
/*	Comment:	read and write configuration							  */
/*	Modified:															  */
/**************************************************************************/

#include "profile.hpp"

//Profile g_clProfile;	//global variable
void Profile::SetFileName(const char* sProfile)
{
	assert((NULL != sProfile) && (0 != strlen(sProfile)));

	if (NULL != m_sFileName)
	{
		delete []m_sFileName;
	}

	if (NULL != m_pFile) 
	{
		fclose(m_pFile);
		m_pFile = NULL;
	}

	m_sFileName = new char[strlen(sProfile) + 1];
	if (NULL == m_sFileName)
	{
		g_clLog.Panic(0, "Memory exhausted !");
	}
	strcpy(m_sFileName, sProfile);

	m_pFile = fopen(m_sFileName, "r+");
	if (NULL == m_pFile)
	{
		g_clLog.Panic(errno, "Unable to open profile %s !", m_sFileName);
	}
}

int Profile::Read(const char* sSection, const char* sEntry, char* sValue)
{
	assert((NULL != sSection) && (NULL != sEntry) && (NULL != sValue));

	strcpy(m_sSection, sSection);
	strcpy(m_sEntry, sEntry);

	if (NULL == m_pFile)
	{
		g_clLog.Error(0, "Profile %s is not opened !", m_sFileName);
		return(-1);
	}

	if (SearchSection() < 0)
	{
		g_clLog.Error(0, "No section's name is %s in profile %s !",
			m_sSection, m_sFileName);
		return(-1);
	}

	if (SearchEntry(sValue) < 0)
	{
		g_clLog.Error(0, "No entry's name is %s in profile %s !",
			m_sEntry, m_sFileName);
		return(-1);
	}

	return(0);
}

int Profile::ReadNext(char* sValue)
{
	if (SearchEntry(sValue) < 0)
	{
		return(-1);
	}

	return(0);
}

long Profile::SearchSection()
{
	char* sRealSection = TrimLeftRight(m_sSection);
	char* sSec = new char[strlen(sRealSection)+3];
	sprintf(sSec, "[%s]", sRealSection);

	char sLine[LINE_LEN+1];
	fseek(m_pFile, 0, SEEK_SET);
	long lFoundPos = -1;
	while (!feof(m_pFile))
	{
		if (NULL == fgets(sLine, LINE_LEN, m_pFile))
		{
			//read file error
			g_clLog.Error(0, "Read profile %s Error !", m_sFileName);
			delete []sSec;
			return(-1);
		}
		if ( strstr( sLine, sSec ) == sLine )
		{
			//section found
			lFoundPos = ftell(m_pFile);
			break;
		}
	}

	delete []sSec;

	return(lFoundPos);
}

long Profile::SearchEntry(char* sValue)
{
	if (NULL == m_pFile)
		return(-1);

	char* sRealEntry = TrimAll(m_sEntry);
	char sLine[LINE_LEN+1];
	bool bFound = false;
	long lFoundPos = -1;
	while (!feof(m_pFile))
	{
		lFoundPos = ftell(m_pFile);
		if (NULL == fgets(sLine, LINE_LEN, m_pFile))
		{
			//read file error
			g_clLog.Error(0, "Read profile %s Error !", m_sFileName);
			return(-1);
		}

		//trim white spaces
		char* sRealLine = TrimAll(sLine);
		if ('[' == *sRealLine)
		{
			break;
		}

		char* pcValue ;
		pcValue = strchr(sRealLine, '=');
		if (!pcValue)
		{
			break;
		}

		*(pcValue++) = '\0';
		if (0 == strcmp(sRealLine, sRealEntry)) 
		{
			if(!strlen(pcValue))
			{
				break;
			}
			bFound = true;
			strcpy(sValue, pcValue);
			break;
		}
	}
	if (!bFound)
	{
		lFoundPos = -1;
	}

	return(lFoundPos);
}

⌨️ 快捷键说明

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