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

📄 myinifile.cpp

📁 SQLBig5BugTool 宽字符操作问题
💻 CPP
📖 第 1 页 / 共 5 页
字号:
// MyIniFile.cpp: implementation of the CMyIniFile class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MyIniFile.h"

#include <algorithm>
#include <io.h>

#define GET_FULLPATH_EX(path) m_strFileRedirectSubFolder.IsEmpty()?GET_FULLPATH(path):GET_FULLPATH((m_strFileRedirectSubFolder+(CString)"\\"+(const char*)path))

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

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

CMyIniFile::CMyIniFile(
					   const char*	pszBaseDir,
					   IBasicMsgUI*	pIBasicMsgUI,
					   const char*	pszFileRedirectSubFolder,
					   const bool	bDuplicatedKeyEnabled
					   ):
m_strBaseDir(pszBaseDir),
m_strFileRedirectSubFolder(pszFileRedirectSubFolder),
m_pIBasicMsgUI(pIBasicMsgUI),
m_bDuplicatedKeyEnabled(bDuplicatedKeyEnabled)
{
	m_bWholeLineAsValue=FALSE;
}

CMyIniFile::~CMyIniFile()
{
	ClearMapFilePath2Ini();
}

BOOL CMyIniFile::IsKeyExistesEx(
								const char* pszIniFile,
								const char* pszSection,
								const char* pszKey
								)
{
	CString strTxt;
	
	if(strcmp(NO_SECTION_FILE_SECTION_NAME,pszSection)==0)
	{
		CStdioFile f;
		if(!f.Open(pszIniFile,CFile::modeRead|CFile::typeText|CFile::shareDenyNone))
		{
			return FALSE;
		}
		
		CString strKey=pszKey;
		
		while(f.ReadString(strTxt))
		{
			if(strTxt.IsEmpty())
			{
				continue;
			}
			strTxt.TrimLeft();strTxt.TrimRight();
			
			if(strTxt.IsEmpty())
			{
				continue;
			}
			long lPos=strTxt.Find("=");
			if(lPos<=0)
			{
				continue;
			}
			
			CString strLeftPart=strTxt.Left(lPos);
			
			strLeftPart.TrimLeft();strLeftPart.TrimRight();
			
			if(strLeftPart.CompareNoCase(strKey)==0)
			{
				return TRUE;
			}
		}
		f.Close();
		
		return FALSE;
	}
	ASSERT(false);
	
	char szTxt[1024]={0,};
	
#define NO_KEY "3341134112341234123412343.t.d.i.t.ta."
	
	GetPrivateProfileString(
		pszSection,
		pszKey,
		NO_KEY,
		szTxt,
		sizeof(szTxt),
		pszIniFile
		);
	//strTxt=szTxt;
	//strTxt.TrimLeft();strTxt.TrimRight();
	
	return strcmp(szTxt,NO_KEY)!=0;
}


BOOL CMyIniFile::IsSectionExistesEx(
									const char* pszIniFile,
									const char* pszSection
									)
{
	CStdioFile f;
	if(!f.Open(pszIniFile,CFile::modeRead|CFile::typeText|CFile::shareDenyNone))
	{
		return FALSE;
	}
	
	CString strSect=pszSection;
	
	CString strTxt;
	while(f.ReadString(strTxt))
	{
		if(strTxt.IsEmpty())
		{
			continue;
		}
		strTxt.TrimLeft();strTxt.TrimRight();
		
		if(strTxt.IsEmpty())
		{
			continue;
		}
		if(strTxt.Left(1)==";")
		{
			continue;
		}
		if(strTxt.Left(2)=="//")
		{
			continue;
		}
		if(strTxt[0]!='[')
		{
			continue;
		}
		strTxt=strTxt.Mid(1);
		if(strTxt[strTxt.GetLength()-1]!=']')
		{
			continue;
		}
		strTxt=strTxt.Left(strTxt.GetLength()-1);
		if(strTxt.CompareNoCase(strSect)==0)
		{
			return TRUE;
		}
	}
	f.Close();
	
	return FALSE;	
}

BOOL CMyIniFile::GetKeyNameList(STRING_LIST& StrKeyList)
{
	StrKeyList.clear();
	
	for(MAP_NAME_TO_INI_SECTION::iterator itr=m_MapNameToIniSection.begin();itr!=m_MapNameToIniSection.end();itr++)
	{
		INI_SECTION* pIniSect=(*itr).second;
		
		AddList(pIniSect->IniKeyList,StrKeyList,TRUE);
	}
	return TRUE;
}

BOOL CMyIniFile::IsKeyExistes(
							  const char* pszSection,
							  const char* pszKey
							  )
{
	MAP_NAME_TO_INI_SECTION::iterator itr=m_MapNameToIniSection.find(pszSection);
	if(m_MapNameToIniSection.end()==itr)
	{
		return FALSE;
	}
	INI_SECTION* pIniSection=(*itr).second;
	
	//STRING_LIST::iterator itrKey=std::find(pIniSection->IniKeyList.begin(),pIniSection->IniKeyList.end(),pszKey);
	//return (itrKey!=pIniSection->IniKeyList.end());
	return pIniSection->MapIniKey2Value.end()!=pIniSection->MapIniKey2Value.find(pszKey);
}

BOOL CMyIniFile::IsSectionExistes(
								  const char* pszSection
								  )
{
	return m_MapNameToIniSection.end()!=m_MapNameToIniSection.find(pszSection);
}

BOOL CMyIniFile::ReadAndParse(
							  const char*	strFilePath
							  )
{
	if(!Read(strFilePath))
	{
		return FALSE;
	}
	
	Parse();
	
	return TRUE;
}

ULONG CMyIniFile::ReplaceAll(const char *pszOld, const char *pszNew,const BOOL bReplaceBackSlashWithSlash)
{
	ULONG ulReplaceCount=0;
	
	for(long l=0;l<m_saIniStringList.GetSize();l++)
	{
		CString str=m_saIniStringList[l];
		CString strLow=str;
		strLow.MakeLower();
		
		CString strOldLow=pszOld;
		strOldLow.MakeLower();
		
		if(bReplaceBackSlashWithSlash)
		{
			strLow.Replace("\\","/");
			strOldLow.Replace("\\","/");
		}
		else
		{
			strLow.Replace("/","\\");
			strOldLow.Replace("/","\\");
		}		
		
		long lPos=strLow.Find(strOldLow);
		if(lPos>=0)
		{
			++ulReplaceCount;
			
			//strLow.Replace(pszOld,pszNew);
			strLow=strLow.Left(lPos)+pszNew+strLow.Mid(lPos+strOldLow.GetLength());
			
			
			m_pIBasicMsgUI->ShowErrorMsg(
				FormatString("将 \"%s\" 修改为 \"%s\" ",
				(const char*)str,(const char*)strLow).c_str()
				);	
			
		}
		m_saIniStringList.SetAt(l,strLow);
	}
	return ulReplaceCount;
}

BOOL CMyIniFile::SaveAs(const char *pszNewIniFilePath)
{
	CStdioFile fSave;
	
	CString strFilePath=GET_FULLPATH(pszNewIniFilePath);
	
	if(!fSave.Open(strFilePath,CFile::modeCreate|CFile::modeWrite|CFile::typeText|CFile::shareExclusive))
	{
		return FALSE;
	}
	
	for(long l=0;l<m_saIniStringList.GetSize();l++)
	{
		fSave.WriteString(m_saIniStringList[l]);
		fSave.WriteString("\n");
	}
	
	return TRUE;
}

BOOL CMyIniFile::Read(const char* strFilePath)
{
	ClearIniSection();
	
	m_strFilePath=GET_FULLPATH(strFilePath);
	
	CStdioFile f;
	if(!f.Open(m_strFilePath,CFile::modeRead|CFile::typeText|CFile::shareDenyNone))
	{
		return FALSE;
	}
	
	CString strTxt;
	while(f.ReadString(strTxt))
	{
		m_saIniStringList.Add(strTxt);
	}
	f.Close();
	return TRUE;
}

void CMyIniFile::Parse()
{
	CString strSection;
	CString strKey;
	CString strValue;
	long lPos=0;
	
	INI_SECTION *pIniSection=0;
//	
	//	if(m_saIniStringList.GetSize()>10000)
	//	{
	//		m_pIBasicMsgUI->ShowErrorMsg(FormatString(
	//			"%s Ini总行数 %u 超过10000 ",GetFilePath(),m_saIniStringList.GetSize()
	//			).c_str());
	//		
	//	}
			

	BOOL bTitleNameDuplicated=FALSE;
	BOOL bTitleNameInvalid=FALSE;
	
	for(long l=0;l<m_saIniStringList.GetSize();l++)
	{
		//default: Key=Value:
		
		CString strTxt=m_saIniStringList[l];
		
		if(strTxt.IsEmpty())
		{
			continue;
		}
		
		strTxt.TrimLeft();strTxt.TrimRight();
		
		if(strTxt.IsEmpty())
		{
			continue;
		}
		//lines begin with ;
		if(strTxt.Left(1)==";")
		{
			continue;
		}
		//omit C++ style comment:if first 2 chars is // ,we consider it to be a comment line
		if(strTxt.Left(2)=="//")
		{
			continue;
		}
		
		//
		//section tag found
		if((strTxt.Left(1)=="[")&&(strTxt.Right(1)=="]"))
		{
			CString strSectionNew=strTxt.Mid(1,strTxt.GetLength()-2);
			
			strSectionNew.TrimLeft();strSectionNew.TrimRight();
			
			if(strSectionNew.IsEmpty())
			{
				//invalid section name
				strSection="";

				m_pIBasicMsgUI->ShowErrorMsg(
					FormatString("Title名不能为空 行:%d",l).c_str()
					);

				bTitleNameInvalid=TRUE;
				continue;
			}
			//another section found
			//section name: case insensitive
			
			//section already existes
			if(m_MapNameToIniSection.end()!=m_MapNameToIniSection.find((const char*)strSectionNew))
			{
				//
				m_pIBasicMsgUI->ShowErrorMsg(
					FormatString("重复的Title名: %s",(const char*)strSectionNew).c_str()
					);
				
				bTitleNameDuplicated=TRUE;
				strSection="";
			}
			else
			{
				bTitleNameInvalid=FALSE;
				
				bTitleNameDuplicated=FALSE;
				strSection=strSectionNew;
				
				pIniSection=new INI_SECTION;
				m_MapNameToIniSection[(const char*)strSection]=pIniSection;
				
			}
			continue;	
		}
		if(bTitleNameInvalid)
		{
			continue;
		}
		if(bTitleNameDuplicated)
		{
			//如果发现TITLE名重复则忽略后续的非TITLE名
			continue;
		}
		if(!m_bWholeLineAsValue)
		{
			//检查是否有等号
			strKey="";
			lPos=strTxt.Find("=");
			if(lPos>=0)
			{
				strKey=strTxt.Left(lPos);
				
				strValue=strTxt.Mid(lPos+1);
				
				strKey.TrimLeft();strKey.TrimRight();
				
				strValue.TrimLeft();strValue.TrimRight();
			}
		}
		//如果KEY为空则将KEY和VALUE等同
		if(strKey.IsEmpty())
		{
			strValue=strKey=strTxt;
		}

		if(strSection.IsEmpty())
		{
			strSection=NO_SECTION_FILE_SECTION_NAME;
			
			if(m_MapNameToIniSection.end()==m_MapNameToIniSection.find((const char*)strSection))
			{
				pIniSection=new INI_SECTION;
				m_MapNameToIniSection[(const char*)strSection]=pIniSection;
			}
		}
		
		if(pIniSection->MapIniKey2Value.end()==pIniSection->MapIniKey2Value.find((const char*)strKey))
		{
			//key not found yet
			pIniSection->MapIniKey2Value[(const char*)strKey]=(const char*)strValue;
			
			pIniSection->IniKeyList.push_back((const char*)strKey);
		}
		else
		{
			//key found 
			if(m_bDuplicatedKeyEnabled)
			{
				CString strKeyEx;
				strKeyEx.Format("%s_Duplicated_%d",(const char*)strKey,l);
				
				pIniSection->MapIniKey2Value[(const char*)strKeyEx]=(const char*)strValue;
				
				pIniSection->IniKeyList.push_back((const char*)strKeyEx);
			}
			else
			{
				m_pIBasicMsgUI->ShowErrorMsg(
					FormatString("Title %s 的关键字 %s 重复",
					(strSection==(CString)NO_SECTION_FILE_SECTION_NAME)?"":strSection,
					(const char*)strKey).c_str()
					);
			}
		}
	}
}


/////////////

void CMyIniFile::CheckAllIniSectionAutoCheck(INI_META_DATA_CHECK_RULE& CheckRuleAutoCheck)
{
	for(MAP_NAME_TO_INI_SECTION::iterator itr=m_MapNameToIniSection.begin();itr!=m_MapNameToIniSection.end();itr++)
	{
		CheckIniSectionAutoCheck((*itr).first.c_str(),CheckRuleAutoCheck);
	}	
}

void CMyIniFile::ClearIniSection()
{
	
	for(MAP_NAME_TO_INI_SECTION::iterator itr=m_MapNameToIniSection.begin();itr!=m_MapNameToIniSection.end();
	itr++)
	{
		delete (*itr).second;
	}	
	m_MapNameToIniSection.clear();
	
	m_saIniStringList.RemoveAll();
}

BOOL CMyIniFile::SaveIniMetaData(
								 const char *		pszMetaDataFilePath,
								 const char*		pszIniFilePath,
								 INI_META_DATA_MGR&	IniMetaDataMgr
								 )
{
	STRING_LIST RuleSectionList;
	IniMetaDataMgr.GetRuleSectionList(pszIniFilePath,RuleSectionList);
	
	CopyFile(pszMetaDataFilePath,pszMetaDataFilePath+(CString)".bak",FALSE);
	
	DeleteFile(pszMetaDataFilePath);
	
	if(RuleSectionList.size()<1)
	{
		return FALSE;
	}
	
	//
	
	long l=0;
	
	CString strRuleSectionList;
	for(l=0;l<RuleSectionList.size();l++)

⌨️ 快捷键说明

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