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

📄 settings.cpp

📁 用于程序中设置一个配置文件。设置和得到字符串名和串值
💻 CPP
字号:
// Settings.cpp: implementation of the CSettings class.
//
//////////////////////////////////////////////////////////////////////

#include <stdio.h>

#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <iostream.h>



#include "sync.h"

#include "ITcpMng.h"

/*#include "DawnMacro.h"
#include "DawnStruct.h"
#include "DawnCmd.h"
*/
#include "CapSvrMacro.h"

#include "Settings.h"

using namespace std;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
using namespace std;

CSettings::CSettings()
{
	memset(m_szSettings,0,M_SETTING_FILE_SIZE);
	memset(m_szFile,0,M_FILE_NAME_SIZE);
}

CSettings::~CSettings()
{

}

bool CSettings::SetSettingFile(char* szFile)
{
	CSyncDawn Obj(&m_Cs);
	memset(m_szSettings,0,M_SETTING_FILE_SIZE);
	memset(m_szFile,0,M_FILE_NAME_SIZE);

	FILE* pFile = NULL;
	pFile = fopen(szFile,"r+");
	if(NULL != pFile)
	{
//		printf("\n Open File r+ ok! \n");
		strcpy(m_szFile,szFile);
		fclose(pFile);
		
		return true;
	}
	else
	{
		pFile = fopen(szFile,"w+");
		if(NULL != pFile)
		{
//			printf("\n Open File w+ ok! \n");
			strcpy(m_szFile,szFile);
			fclose(pFile);
			return true;
		}
	}
	return false;
}

bool CSettings::GetInt(char *szKey,int *pValue)
{
	CSyncDawn Obj(&m_Cs);
	memset(m_szSettings,0,M_SETTING_FILE_SIZE);

	FILE* pFile = NULL;

	string strFind;
	strFind = szKey;
	strFind += M_SETTINGS_FILIALE;
	pFile = fopen(m_szFile,"r");
	if(NULL != pFile)
	{
		int nFileLen = 0;
		nFileLen = fread(m_szSettings,1,M_SETTING_FILE_SIZE,pFile);
		if(nFileLen > 0)
		{
			string strTmp;
			int nPos = 0;
			
			strTmp = m_szSettings;
			nPos = strTmp.find(strFind,0);
			if(nPos >= 0)
			{
				int nEnd = 0;
				nEnd = strTmp.find(M_SETTINGS_PARAM_END,nPos);
				if(nEnd >= 0)
				{
					string strParam;
					strParam = strTmp.substr(nPos,nEnd-nPos);
					nPos = strParam.find(M_SETTINGS_FILIALE,0);
					if(nPos > 0)
					{
						string strValue;
						strValue = strParam.substr(nPos+1);
						*pValue = atoi(strValue.c_str());
						fclose(pFile);
						return true;
					}
				}
			}
		}
		fclose(pFile);
	}
	return false;
}

bool CSettings::GetString(char *szKey,char *pValue)
{
	CSyncDawn Obj(&m_Cs);
	memset(m_szSettings,0,M_SETTING_FILE_SIZE);

	FILE* pFile = NULL;

	string strFind;
	strFind = szKey;
	strFind += M_SETTINGS_FILIALE;
	pFile = fopen(m_szFile,"r");
	if(NULL != pFile)
	{
//		printf("\n Open File OK! \n");
		int nFileLen = 0;
		nFileLen = fread(m_szSettings,1,M_SETTING_FILE_SIZE,pFile);
//		printf("\n nFileLen = %d \n",nFileLen);
		if(nFileLen > 0)
		{
//			printf("\n %s \n",m_szSettings);
			string strTmp;
			int nPos = 0;
			
			strTmp = m_szSettings;
			nPos = strTmp.find(strFind,0);
			if(nPos >= 0)
			{
				int nEnd = 0;
				nEnd = strTmp.find(M_SETTINGS_PARAM_END,nPos);
				if(nEnd >= 0)
				{
					string strParam;
					strParam = strTmp.substr(nPos,nEnd-nPos);
					nPos = strParam.find(M_SETTINGS_FILIALE,0);
					if(nPos > 0)
					{
						string strValue;
						strValue = strParam.substr(nPos+1);
						strcpy(pValue,strValue.c_str());
						fclose(pFile);
						return true;
					}
				}
			}
		}
		fclose(pFile);
	}
	return false;
}

bool CSettings::SetInt(char *szKey,int nValue)
{
	CSyncDawn Obj(&m_Cs);
	memset(m_szSettings,0,M_SETTING_FILE_SIZE);

	FILE* pFile = NULL;
	string strFind;
	string strSave;
	string strParam;

	char	szValue[20] = {0};
	bool bFinded = false;
	string strTmp;
	int nReplaceBegin = 0;
	int nReplaceSize = 0;

	
	strFind = szKey;
	strFind += M_SETTINGS_FILIALE;

	sprintf(szValue,"%d",nValue);
	strSave = szKey;
	strSave += M_SETTINGS_FILIALE;
	strSave += szValue;
	strSave += M_SETTINGS_PARAM_END;

	pFile = fopen(m_szFile,"r+");
	if(NULL != pFile)
	{
		int nFileLen = 0;
		nFileLen = fread(m_szSettings,1,M_SETTING_FILE_SIZE,pFile);
		if(nFileLen > 0)
		{
			int nPos = 0;
			strTmp = m_szSettings;
			nPos = strTmp.find(strFind,0);
			if(nPos >= 0)
			{
				nReplaceBegin = nPos;
				int nEnd = 0;
				nEnd = strTmp.find(M_SETTINGS_PARAM_END,nPos);
				if(nEnd >= 0)
				{
					strParam = strTmp.substr(nPos,nEnd-nPos);
					nReplaceSize = strParam.length()+strlen(M_SETTINGS_PARAM_END);
					nPos = strParam.find(M_SETTINGS_FILIALE,0);
					if(nPos > 0)
					{
						string strValue;
						int nOldValue = 0;
						strValue = strParam.substr(nPos+1);
						nOldValue = atoi(strValue.c_str());
						if(nOldValue == nValue)
						{
							fclose(pFile);
							return true;
						}
					}

					bFinded = true;
				}
			}
		}
		if(bFinded)
		{
			strTmp.replace(nReplaceBegin,nReplaceSize,strSave);
			memset(m_szSettings,0,M_SETTING_FILE_SIZE);
			strcpy(m_szSettings,strTmp.c_str());
		}
		else
		{
			strcat(m_szSettings,strSave.c_str());
		}
		fclose(pFile);

		pFile = fopen(m_szFile,"w+");
		if(NULL != pFile)
		{
			if(fwrite(m_szSettings,1,strlen(m_szSettings),pFile) == M_SETTING_FILE_SIZE)
			{
				fclose(pFile);
				return true;
			}
			fclose(pFile);
		}
	}
	return false;
}

bool CSettings::SetString(char *szKey,char *pValue)
{
	CSyncDawn Obj(&m_Cs);
	memset(m_szSettings,0,M_SETTING_FILE_SIZE);

	FILE* pFile = NULL;
	string strFind;
	string strSave;
	string strParam;

	bool bFinded = false;
	string strTmp;
	int nReplaceBegin = 0;
	int nReplaceSize = 0;

	
	strFind = szKey;
	strFind += M_SETTINGS_FILIALE;

	strSave = szKey;
	strSave += M_SETTINGS_FILIALE;
	strSave += pValue;
	strSave += M_SETTINGS_PARAM_END;

	pFile = fopen(m_szFile,"r+");
	if(NULL != pFile)
	{
		int nFileLen = 0;
		nFileLen = fread(m_szSettings,1,M_SETTING_FILE_SIZE,pFile);
		if(nFileLen > 0)
		{
			int nPos = 0;
			strTmp = m_szSettings;
			nPos = strTmp.find(strFind,0);
			if(nPos >= 0)
			{
				nReplaceBegin = nPos;
				int nEnd = 0;
				nEnd = strTmp.find(M_SETTINGS_PARAM_END,nPos);
				if(nEnd >= 0)
				{
					strParam = strTmp.substr(nPos,nEnd-nPos);
					nReplaceSize = strParam.length()+strlen(M_SETTINGS_PARAM_END);
					nPos = strParam.find(M_SETTINGS_FILIALE,0);
					if(nPos > 0)
					{
						string strValue;
						strValue = strParam.substr(nPos+1);
						if(0 == strValue.compare(pValue))
						{
							fclose(pFile);
							return true;
						}
					}

					bFinded = true;
				}
			}
		}
		if(bFinded)
		{
			strTmp.replace(nReplaceBegin,nReplaceSize,strSave);
			memset(m_szSettings,0,M_SETTING_FILE_SIZE);
			strcpy(m_szSettings,strTmp.c_str());
		}
		else
		{
			strcat(m_szSettings,strSave.c_str());
		}
		fclose(pFile);

		pFile = fopen(m_szFile,"w+");
		if(NULL != pFile)
		{
			if(fwrite(m_szSettings,1,strlen(m_szSettings),pFile) == M_SETTING_FILE_SIZE)
			{
				fclose(pFile);
				return true;
			}
			fclose(pFile);
		}
	}
	return false;
}

⌨️ 快捷键说明

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