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

📄 inifile.cpp

📁 EVC(嵌入式VC++)发的用于串口通信的程序
💻 CPP
字号:
// IniFile.cpp: implementation of the CIniFile class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "IniFile.h"
/*
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
*/
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CIniFile::CIniFile()
{
	InitializeCriticalSection(&m_hCS);
	hHeap =  HeapCreate(NULL,0x10000,0x50000); 
}

CIniFile::~CIniFile()
{
	DeleteCriticalSection(&m_hCS);
	if(hHeap)
		HeapDestroy(hHeap); 
}

void CIniFile::RemoveAll()
{
	int i=0,j=0;
	for(i=0;i<m_arSection.size();i++)
	{
		Section *pSection=m_arSection[i];
		if(pSection)
		{
			for(j=0;j<pSection->arKey.size();j++)
			{
				Key* pKey=pSection->arKey[j];
				if(pKey)
				{
					HeapFree(hHeap, HEAP_NO_SERIALIZE,pKey);
					pKey=NULL;
				}
			}
			HeapFree(hHeap,HEAP_NO_SERIALIZE,pSection);
			pSection=NULL;
		}
	}
	m_arSection.clear();
}
//读取Key的值
//strSection:域名  strKey:标签名   strDefault:默认值
bool CIniFile::GetKeyString(WCHAR * strSection, WCHAR * strKey, WCHAR * strDefault,WCHAR *strResult)
{
	EnterCriticalSection(&m_hCS); 

	Key *pKey=NULL;
	Section *pSection=FindSection(strSection);
	WCHAR sTmp[BUFFLINE];
	if(pSection)  //找到Section
	{
		pKey=FindKey(pSection,strKey);
		if(pKey) //找到Key
		{
			if(pKey->strVal[0]==_T('\0'))
			{
				pKey->SetVal(strDefault);
			}
			if(strResult[0]==_T('\0'))
			{
				wcscpy(pKey->strVal,strDefault);
			}
			wcscpy(strResult,pKey->strVal);
			LeaveCriticalSection(&m_hCS); 
			SaveIniFile();
			return true;
		}
		else  //创建Key
		{
			wsprintf(sTmp,_T("%s=%s"),strKey,strDefault);
			pKey=pSection->AddKey(sTmp);
			if(pKey)
			{
				pKey->SetVal(strDefault);
				wcscpy(strResult,pKey->strVal);
				LeaveCriticalSection(&m_hCS); 
				SaveIniFile();
				return true;
			}
			else
			{
				LeaveCriticalSection(&m_hCS); 
				return false;
			}
		}
	}
	else  //创建Section
	{
		pSection=(Section*)HeapAlloc(hHeap, HEAP_ZERO_MEMORY, sizeof(Section));
		if(pSection)
		{
			pSection->m_hHeap=hHeap;
			wsprintf(sTmp,_T("[%s]"),strSection);
			wcscpy(pSection->strLine,sTmp);
			pSection->bValid=true;
			m_arSection.push_back(pSection);
			wsprintf(sTmp,_T("%s=%s"),strKey,strDefault);
			pKey=pSection->AddKey(sTmp);
			if(pKey)
			{
				pKey->SetVal(strDefault);
				wcscpy(strResult,pKey->strVal);
				LeaveCriticalSection(&m_hCS); 
				SaveIniFile();
				return true;

			}
			LeaveCriticalSection(&m_hCS); 
			SaveIniFile();
			return false;

		}
		else
		{
			LeaveCriticalSection(&m_hCS); 
			return false;
		}
	}
	//return true;
}

//写入Key的值
//strSection:域名  strKey:标签名   strValue:写入的值
bool CIniFile::WriteKeyString(WCHAR * strSection, WCHAR * strKey, WCHAR * strValue)
{
	//WritePrivateProfileString(strSection,strKey ,strValue,m_strFile);
	EnterCriticalSection(&m_hCS); 
	Key *pKey=NULL;
	Section *pSection=FindSection(strSection);
	WCHAR sTmp[BUFFLINE];
	if(wcslen(strValue)>BUFFLINE)
	{
		LeaveCriticalSection(&m_hCS);
		return false;
	}
	if(pSection)  //找到Section
	{
		pKey=FindKey(pSection,strKey);
		if(pKey) //找到Key
		{
			pKey->SetVal(strValue);
			LeaveCriticalSection(&m_hCS); 
			SaveIniFile();
			return true;
		}
		else  //创建Key
		{
			wsprintf(sTmp,_T("%s=%s"),strKey,strValue);
			pKey=pSection->AddKey(sTmp);
			if(pKey)
			{
				pKey->SetVal(strValue);
				LeaveCriticalSection(&m_hCS); 
				SaveIniFile();
				return true;
			}
			else
			{
				LeaveCriticalSection(&m_hCS); 
				return false;
			}
		}
	}
	else  //创建Section
	{
		pSection=(Section*)HeapAlloc(hHeap, HEAP_ZERO_MEMORY, sizeof(Section));
		if(pSection)
		{
			pSection->m_hHeap=hHeap;
			wsprintf(sTmp,_T("[%s]"),strSection);
			wcscpy(pSection->strLine,sTmp);
			pSection->bValid=true;
			m_arSection.push_back(pSection);
			wsprintf(sTmp,_T("%s=%s"),strKey,strValue);
			pKey=pSection->AddKey(sTmp);
			if(pKey)
			{
				pKey->SetVal(strValue);
				LeaveCriticalSection(&m_hCS); 
				SaveIniFile();
				return true;

			}
			LeaveCriticalSection(&m_hCS); 
			SaveIniFile();
			return false;

		}
		else
		{
			LeaveCriticalSection(&m_hCS); 
			return false;
		}
	}

	return true;
}

void CIniFile::SaveIniFile()
{
	EnterCriticalSection(&m_hCS); 
//	long nTime=GetTickCount();
	WCHAR sLine[BUFFLINE];
	int i=0,n=0;
	Section *pSection=NULL;
	Key *pKey=NULL;

	// 打开文件
	FILE* pFile = _wfopen(m_strFile,_T("w"));
	if(pFile)
	{
		for(i=0;i<m_arSection.size();i++)
		{
			pSection=m_arSection[i];
			if(pSection)
			{
				wsprintf(sLine,_T("\n%s\n"),pSection->strLine);
				fputws(sLine,pFile);
				for(n=0;n<pSection->arKey.size();n++)
				{
					pKey=pSection->arKey[n];
					if(pKey)
					{
						wsprintf(sLine,_T("%s\n"),pKey->strLine);
						fputws(sLine,pFile);
					}
				}
			}
		}
		fclose(pFile);
	}
//	nTime=GetTickCount()-nTime;
	LeaveCriticalSection(&m_hCS);  
}


CIniFile::Section * CIniFile::FindSection(WCHAR *strSection)
{
	Section *pSection=NULL;
	int i=0;
	for(i=0;i<m_arSection.size();i++)
	{
		pSection=m_arSection[i];
		if(pSection && pSection->bValid)
		{
			if(Find(pSection->strLine,strSection,1)==1)
			{
				return pSection;
			}
		}
	}
	return  NULL;
}

CIniFile::Key * CIniFile::FindKey(Section *pSection,WCHAR *strKey)
{
	Key *pKey=NULL;
	int i=0;
	for(i=0;i<pSection->arKey.size();i++)
	{
		pKey=pSection->arKey[i];
		if(pKey && pKey->bValid)
		{
			if(Find(pKey->strLine,strKey,0)==0)
			{
				return pKey;
			}
		}
	}

	return NULL;
}

//设置INI文件名
bool CIniFile::LoadIniFile(WCHAR * strName)
{
	EnterCriticalSection(&m_hCS); 
	wcscpy(m_strFile,strName);
	WCHAR szDate[BUFFLINE];
	FILE *fh=NULL;
	long nTime=GetTickCount();
	RemoveAll();
	if (fh=_wfopen(strName,_T("rb")))// CFile::modeRead | CFile::typeText | CFile::shareDenyWrite, &fe))
	{
		WCHAR strParseBuff[BUFFLINE];
		int	nSec = 0;
		CHAR s1;//,s2,s3;
		CHAR chr[BUFFLINE];
		long nPos=0;
		bool bSection=false;     //是否
		
		while((s1=getc(fh))!=EOF)
		{
			if(s1==0x0d)//换行
			{
				if((s1=getc(fh))!=EOF)
				{
					if(s1==0x0a)
					{
						memset (strParseBuff, 0, BUFFLINE * sizeof (WCHAR));
						if(nPos>BUFFLINE)
						{
							fclose(fh);
							LeaveCriticalSection(&m_hCS); 
							return false;
						}
						mbstowcs(strParseBuff,chr,nPos);
						if (strParseBuff[0] == _T('\n') || strParseBuff[0] == _T(';'))//注释或空行
						{
							if(!bSection)
							{
								Section *pSection=(Section*)HeapAlloc(hHeap, HEAP_ZERO_MEMORY, sizeof(Section));
								if(pSection)
								{
									pSection->m_hHeap=hHeap;
									wcscpy(pSection->strLine,strParseBuff);
									pSection->bValid=false;
									m_arSection.push_back(pSection);
								}
								else
								{
									fclose(fh);
									LeaveCriticalSection(&m_hCS);  
									return false;
								}
							}
							else
							{
								Section *pSection=m_arSection[m_arSection.size()-1];
								if(pSection->AddKey(strParseBuff)==false)
								{
									fclose(fh);
									LeaveCriticalSection(&m_hCS);  
									return false;
								}

							}
							nPos=0;
							continue;
						}
						else if(strParseBuff[0] == _T('[') && Find(strParseBuff,_T("]"),0)>0) //【】Section
						{
							Section *pSection=(Section*)HeapAlloc(hHeap, HEAP_ZERO_MEMORY, sizeof(Section));
							if(pSection)
							{
								pSection->m_hHeap=hHeap;
								wcscpy(pSection->strLine,strParseBuff);
								pSection->bValid=true;
								m_arSection.push_back(pSection);
							}
							else
							{
								fclose(fh);
								LeaveCriticalSection(&m_hCS);
								return false;
							}
							bSection=true;
							nPos=0;
							continue;
						}
						else
						{
							if(bSection)
							{
								if(Find(strParseBuff,_T("="),1)>0)  //Key 处理子项
								{
									Section *pSection=m_arSection[m_arSection.size()-1];
									if(pSection->AddKey(strParseBuff)==false)
									{
										fclose(fh);
										LeaveCriticalSection(&m_hCS);
										return false;
									}
								}
								nPos=0;
								continue;
							}
							//处理无效行
						}
						nPos=0;	
					}
					continue;
				}
				else
				{
					break; //到文件尾
				}
			}
			else
				chr[nPos]=s1;
			nPos++;

		}
		nTime=GetTickCount()-nTime;
		fclose(fh);
		LeaveCriticalSection(&m_hCS);
		return true;
	}//if
	LeaveCriticalSection(&m_hCS);
	return true;
}

void CIniFile::GetPath(WCHAR *strPath)
{
	wcscpy(strPath,m_strPath);
}

//查找字符串 ,在字符串string中查找子字符串strCharSet,nStart为起始地址
 int  CIniFile::Find( WCHAR *string, WCHAR *strCharSet,int nStart )
{
	int i=-1;
	WCHAR *wstr=wcsstr(string+nStart,strCharSet);
	if(wstr)
		i=wstr-string;
	return i;
}


//去除右面的字符
void CIniFile::TrimRight(WCHAR *string ,int c)
{
	WCHAR*	sTemp	= NULL;
	bool bFind=true;
	while(bFind)
	{
		sTemp=::wcsrchr(string,c);
		int nlen=wcslen(string);
		int nPos=sTemp-string;
		if(nPos+1==nlen && nlen>0)
		{
			wcscpy(string+nPos,_T("\0"));
			bFind=true;
		}
		else
			bFind=false;
	}
	//	::wcscpy(string,sTemp+1);//去除换行

}

//去除左面的字符
void CIniFile::TrimLeft(WCHAR *string ,int c)
{
	WCHAR*	sTemp	= NULL;
	bool bFind=true;
	while(bFind)
	{
		sTemp=::wcschr(string,c);
		int nlen=wcslen(string);
		int nPos=sTemp-string;
		if(nPos==0 && nlen>0)
		{
			//string[sTemp-string]=_T('\0');
			wcscpy(string,string+1);
			bFind=true;
		}
		else
			bFind=false;
	}
	//	::wcscpy(string,sTemp+1);//去除换行

}

int CIniFile::CompareNoCase(WCHAR *string1,WCHAR *string2)
{
	WCHAR s1[512]=_T("\0"),s2[512]=_T("\0");
	wcscpy(s1,string1);
	wcscpy(s2,string2);
	StringToUpper(s1);
	StringToUpper(s2);
	return wcscmp(s1,s2);
}

void CIniFile::StringToUpper(WCHAR *string)
{
	WCHAR *p;
	for( p = string; p < string + wcslen( string ); p++ )
	{
		if( iswlower( *p ) )
		{
			 *p=towupper( *p );
		}
	}

}

void CIniFile::Replace(WCHAR *string,WCHAR* strFind,WCHAR* strCharSet)
{
	WCHAR*	sTemp	= NULL;
	WCHAR s1[256]=_T("\0"),s2[256]=_T("\0"),s3[256]=_T("\0");
	bool bFind=true;
	while(bFind)
	{
		sTemp=wcsstr(string,strFind);
		int nPos=sTemp-string;
		if(nPos>=0)
		{
			wcsncpy(s1,string,nPos);
			wcscpy(s2,string+nPos+wcslen(strFind));
			wsprintf(string,_T("%s%s%s"),s1,strCharSet,s2);
			bFind=true;
		}
		else
			bFind=false;
	}

}

//从后面开始查找字符串
int CIniFile::ReverseFind(WCHAR* string,WCHAR* strFind)
{
	int i=-1;
	WCHAR strTemp[BUFFLINE]=_T("\0");
	WCHAR *wTemp=strTemp;
	wcscpy(wTemp,string);
	WCHAR *pFind=wcsstr(wTemp,strFind);
	if(pFind)
	{
		i=pFind-wTemp+1;
		wTemp=pFind+1;
	}
	while(pFind)
	{
		pFind=wcsstr(wTemp,strFind);
		if(pFind)
		{
			i+=pFind-wTemp+1;
			wTemp=pFind+1;
		}
	}
	return i;
}

⌨️ 快捷键说明

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