📄 myinifile.cpp
字号:
BOOL CMyIniFile::GetSectionValueList(const char *pszSection, STRING_LIST &StrListValue)
{
StrListValue.clear();
MAP_NAME_TO_INI_SECTION::iterator itr=m_MapNameToIniSection.find(pszSection);
if(m_MapNameToIniSection.end()==itr)
{
return FALSE;
}
INI_SECTION* pIniSection=(*itr).second;
for(STRING_LIST::iterator itrKey=pIniSection->IniKeyList.begin();
itrKey!=pIniSection->IniKeyList.end();itrKey++)
{
StrListValue.push_back(pIniSection->MapIniKey2Value[*itrKey]);
}
return TRUE;
}
void CMyIniFile::CheckIniSectionAutoCheck(
const char * pszSection,
INI_META_DATA_CHECK_RULE& CheckRuleAutoCheck
)
{
MAP_NAME_TO_INI_SECTION::iterator itr=m_MapNameToIniSection.find(pszSection);
if(m_MapNameToIniSection.end()==itr)
{
ASSERT(false);
return;
}
if(CheckRuleAutoCheck.Keys.size()<1)
{
ASSERT(false);
return;
}
BOOL bCheckSectionByNamingRuleOK=FALSE;
std::string strErr;
if(CheckRuleAutoCheck.bApplyToSectionByNamingRule)
{
bCheckSectionByNamingRuleOK=CheckStringByRule(pszSection,CheckRuleAutoCheck.strSectionNamingRule.c_str(),"TITLE名",strErr);
if(!bCheckSectionByNamingRuleOK)
{
//不满足规则 则 不检查
return ;
}
}
else
{
if(CheckRuleAutoCheck.ApplyToSectionList.size()>0)
{
if(CheckRuleAutoCheck.ApplyToSectionList.end()==std::find(
CheckRuleAutoCheck.ApplyToSectionList.begin(),CheckRuleAutoCheck.ApplyToSectionList.end(),pszSection))
{
//不在规则列表中,不检查该 TITLE 的 数据
return;
};
}
}
if(!bCheckSectionByNamingRuleOK)
{
bCheckSectionByNamingRuleOK=CheckStringByRule(pszSection,CheckRuleAutoCheck.strSectionNamingRule.c_str(),"TITLE名",strErr);
if(!bCheckSectionByNamingRuleOK)
{
m_pIBasicMsgUI->ShowErrorMsg(FormatString(
"Title名称 %s 无效.错误信息:%s.",strErr.c_str()
).c_str());
return;
};
}
STRING_LIST StrListRule;
INI_SECTION* pIniSection=(*itr).second;
STRING_LIST::iterator itrKeyToCheck=std::find(CheckRuleAutoCheck.Keys.begin(),CheckRuleAutoCheck.Keys.end(),CheckRuleAutoCheck.strAutoCheckBegin);
ASSERT(itrKeyToCheck!=CheckRuleAutoCheck.Keys.end());
BOOL bAllKey=((*itrKeyToCheck)=="ALL_KEY");
BOOL bKeySequenceErrorFound=FALSE;
STRING_LIST::iterator itrValueKey=pIniSection->IniKeyList.begin();
//如果是ALL key则不检查顺序,也不检查存在性,只检查值的合法性
if(bAllKey)
{
CString strKey=(*itrKeyToCheck).c_str();
CString strRule=CheckRuleAutoCheck.MapKeysToRule[(const char*)strKey].c_str();
ParseString((const char*)strRule,";",StrListRule);
if(StrListRule.size()<=0)
{
m_pIBasicMsgUI->ShowErrorMsg(FormatString(
"ALL key 时Title %s 的规则定义 不允许为空",pszSection
).c_str());
return;
}
for(MAP_INI_KEY_2_VALUE::iterator itrValue=pIniSection->MapIniKey2Value.begin();
itrValue!=pIniSection->MapIniKey2Value.end();
itrValue++)
{
CString strVal=(*itrValue).second.c_str();
AutoCheckIniValue(pszSection,pIniSection,(*itrValue).first.c_str(),strRule,bKeySequenceErrorFound,FALSE,itrValueKey);
}
return ;
}
////////////
CString strAmountKey;
CString strPartListOfAmountKey;
STRING_LIST PartListOfAmountKey;
STRING_LIST PartListOfAmountKeyToFound;
DWORD dwAmountValue=0;
STRING_LIST AmountKeyList;
itrValueKey=std::find(pIniSection->IniKeyList.begin(),pIniSection->IniKeyList.end(),CheckRuleAutoCheck.strAutoCheckBegin);
if(itrValueKey==pIniSection->IniKeyList.end())
{
m_pIBasicMsgUI->ShowErrorMsg(FormatString(
"Title %s 的第1个关键字 %s 必须存在",(pszSection==(CString)NO_SECTION_FILE_SECTION_NAME)?"":pszSection,
(const char*)CheckRuleAutoCheck.strAutoCheckBegin.c_str()
).c_str());
return;
}
long l=0;
for(;itrKeyToCheck!=CheckRuleAutoCheck.Keys.end();itrKeyToCheck++,l++)
{
//属性分类:
//1 KEY是否 AmountKey ,默认FALSE
//2 KEY是否 PartOfAmountKey ,默认FALSE
//3 值类型 ValueIsFile/REF_INI/其他 。如果为REF_INI 还可以选择是关联Section REF_SECTION 还是ValueIsIniKeyName
//4 KEY是否可选, Optional,对AmountKey 无效(总是为FALSE) ,默认FALSE
//5 值是否允许为空 ValueNonEmpty,对AmountKey 无效(总是为TRUE) ,默认FALSE
BOOL bIsAmountKey=FALSE;
BOOL bPartOfAmountKey=FALSE;
CString strSectionNameBeginWith;
//
BOOL bValueNonEmpty=FALSE;
BOOL bOptional=FALSE;
long lPos=0;
CString strKey=(*itrKeyToCheck).c_str();
CString strRule=CheckRuleAutoCheck.MapKeysToRule[(const char*)strKey].c_str();
ParseString((const char*)strRule,";",StrListRule);
bOptional=(StrListRule.end()!=std::find(StrListRule.begin(),StrListRule.end(),"Optional"));
bValueNonEmpty=(StrListRule.end()!=std::find(StrListRule.begin(),StrListRule.end(),"ValueNonEmpty"));
if(StrListRule.size()>0)
{
//有规则则先检查规则
bPartOfAmountKey=(StrListRule.end()!=std::find(StrListRule.begin(),StrListRule.end(),"PartOfAmountKey"));
bIsAmountKey=(StrListRule.end()!=std::find(StrListRule.begin(),StrListRule.end(),"IsAmountKey"));
//////////////////
if (bIsAmountKey)
{
strPartListOfAmountKey="";
long lPosPartListOfAmountKey=strRule.Find("PartListOfAmountKey(");
if(lPosPartListOfAmountKey>=0)
{
strPartListOfAmountKey=strRule.Mid(lPosPartListOfAmountKey+strlen("PartListOfAmountKey("));
lPosPartListOfAmountKey=strPartListOfAmountKey.Find(")");
if(lPosPartListOfAmountKey>=0)
{
strPartListOfAmountKey=strPartListOfAmountKey.Left(lPosPartListOfAmountKey);
ParseString((const char*)strPartListOfAmountKey,",",PartListOfAmountKey);
PartListOfAmountKeyToFound=PartListOfAmountKey;
}
}
if(strPartListOfAmountKey.IsEmpty())
{
m_pIBasicMsgUI->ShowErrorMsg(FormatString(
"Title %s 的Amount关键字 %s 没有指定PartListOfAmountKey",(pszSection==(CString)NO_SECTION_FILE_SECTION_NAME)?"":pszSection,
(const char*)strKey
).c_str());
continue;
};
if(bOptional)
{
m_pIBasicMsgUI->ShowErrorMsg(FormatString(
"Title %s 的Amount关键字 %s 不能为 可选",(pszSection==(CString)NO_SECTION_FILE_SECTION_NAME)?"":pszSection,
(const char*)strKey
).c_str());
StrListRule.erase(std::find(StrListRule.begin(),StrListRule.end(),"Optional"));
bOptional=FALSE;
}
if(!bValueNonEmpty)
{
StrListRule.push_back("ValueNonEmpty");
bValueNonEmpty=TRUE;
}
if(AutoCheckIniValue(pszSection,pIniSection,strKey,strRule,bKeySequenceErrorFound,CheckRuleAutoCheck.bRestrictKeySequence,itrValueKey))
{
strAmountKey=strKey;
MAP_INI_KEY_2_VALUE::iterator itrValue=pIniSection->MapIniKey2Value.find((const char*)strKey);
ASSERT(itrValue!=pIniSection->MapIniKey2Value.end());
//取得Amount值
dwAmountValue=atol((*itrValue).second.c_str());
if(dwAmountValue<=0)
{
m_pIBasicMsgUI->ShowErrorMsg(FormatString(
"Title %s 的Amount关键字 %s 的值为0",(pszSection==(CString)NO_SECTION_FILE_SECTION_NAME)?"":pszSection,
(const char*)strKey
).c_str());
//return;
}
AmountKeyList.clear();
//
}
continue;
}
if(bPartOfAmountKey)
{
if(strAmountKey.IsEmpty())
{
m_pIBasicMsgUI->ShowErrorMsg(FormatString(
"Title %s 的Amount关键字 %s 没有关联的AmountKey",(pszSection==(CString)NO_SECTION_FILE_SECTION_NAME)?"":pszSection,
(const char*)strKey
).c_str());
return;
}
STRING_LIST::iterator itrPartListOfAmountKeyToFound=std::find(PartListOfAmountKeyToFound.begin(),PartListOfAmountKeyToFound.end(),(const char*)strKey);
if(PartListOfAmountKeyToFound.end()==itrPartListOfAmountKeyToFound)
{
m_pIBasicMsgUI->ShowErrorMsg(FormatString(
"Title %s 的Amount关键字 %s 与 PartListOfAmountKeyToFound 不匹配",
(pszSection==(CString)NO_SECTION_FILE_SECTION_NAME)?"":pszSection,(const char*)strKey
).c_str());
return;
}
else
{
PartListOfAmountKeyToFound.erase(itrPartListOfAmountKeyToFound);
}
AmountKeyList.push_back((const char*)strKey);
continue;
}
}
//如果在 PartListOfAmountKey 中也是PartOfAmountKey
if(PartListOfAmountKey.end()!=std::find(PartListOfAmountKey.begin(),PartListOfAmountKey.end(),(const char*)strKey))
{
STRING_LIST::iterator itrPartListOfAmountKeyToFound=std::find(PartListOfAmountKeyToFound.begin(),PartListOfAmountKeyToFound.end(),(const char*)strKey);
if(PartListOfAmountKeyToFound.end()==itrPartListOfAmountKeyToFound)
{
m_pIBasicMsgUI->ShowErrorMsg(FormatString(
"Title %s 的Amount关键字 %s 与 PartListOfAmountKeyToFound 不匹配",
(pszSection==(CString)NO_SECTION_FILE_SECTION_NAME)?"":pszSection,(const char*)strKey
).c_str());
return;
}
else
{
PartListOfAmountKeyToFound.erase(itrPartListOfAmountKeyToFound);
}
AmountKeyList.push_back((const char*)strKey);
continue;
}
if(PartListOfAmountKeyToFound.size()>0)
{
m_pIBasicMsgUI->ShowErrorMsg(FormatString(
"Title %s 的Amount关键字 %s 与 PartListOfAmountKeyToFound 不匹配",
(pszSection==(CString)NO_SECTION_FILE_SECTION_NAME)?"":pszSection,(const char*)strKey
).c_str());
return;
}
///
if(!strAmountKey.IsEmpty())
{
//如果已经到达最后一个AmountKey则处理AmountKey的检查
ASSERT(dwAmountValue>0);
//STRING_LIST::iterator itrValueKeyAmount=itrValueKey;
//STRING_LIST::iterator itrValueKeyLoop=itrValueKey;
for(long m=0;m<dwAmountValue;m++)
{
for(long m2=0;m2<AmountKeyList.size();m2++)
{
CString strKeyFull;
strKeyFull.Format("%s%u",AmountKeyList[m2].c_str(),m);
CString strKeyTemp=AmountKeyList[m2].c_str();
CString strRuleTemp=CheckRuleAutoCheck.MapKeysToRule[(const char*)strKeyTemp].c_str();
//////
if(!AutoCheckIniValue(pszSection,pIniSection,strKeyFull,strRuleTemp,bKeySequenceErrorFound,CheckRuleAutoCheck.bRestrictKeySequence,itrValueKey))
{
//return ;
};
}
}
//PartListOfAmountKey :仅仅用于显示 。 由于是工具写.cfg 因此可以确保 PartListOfAmountKey 和 PartListOfAmountKey 总是一致的
//STRING_LIST PartListOfAmountKey;
//ParseString(strPartListOfAmountKey,",",PartListOfAmountKey);
//ASSERT(PartListOfAmountKey==AmountKeyList);
strAmountKey="";
AmountKeyList.clear();
}
AutoCheckIniValue(pszSection,pIniSection,strKey,strRule,bKeySequenceErrorFound,CheckRuleAutoCheck.bRestrictKeySequence,itrValueKey);
}
///
if(!strAmountKey.IsEmpty())
{
//如果已经到达最后一个AmountKey则处理AmountKey的检查
ASSERT(dwAmountValue>0);
//STRING_LIST::iterator itrValueKeyAmount=itrValueKey;
//STRING_LIST::iterator itrValueKeyLoop=itrValueKey;
for(long m=0;m<dwAmountValue;m++)
{
for(long m2=0;m2<AmountKeyList.size();m2++)
{
CString strKeyFull;
strKeyFull.Format("%s%u",AmountKeyList[m2].c_str(),m);
CString strKeyTemp=AmountKeyList[m2].c_str();
CString strRuleTemp=CheckRuleAutoCheck.MapKeysToRule[(const char*)strKeyTemp].c_str();
//////
if(!AutoCheckIniValue(pszSection,pIniSection,strKeyFull,strRuleTemp,bKeySequenceErrorFound,CheckRuleAutoCheck.bRestrictKeySequence,itrValueKey))
{
//return ;
};
}
}
strAmountKey="";
AmountKeyList.clear();
}
}
BOOL CMyIniFile::AutoCheckIniValue(
const char* pszSection,
INI_SECTION* pIniSection,
const char* strKeyFull,
const CString strRule,
BOOL& bKeySequenceErrorFound,
const BOOL bRestrictKeySequence,
STRING_LIST::iterator& itrValueKey
)
{
STRING_LIST StrListRule;
ParseString((const char*)strRule,";",StrListRule);
BOOL bValueNonEmpty=(StrListRule.end()!=std::find(StrListRule.begin(),StrListRule.end(),"ValueNonEmpty"));
BOOL bValueIsInteger=(StrListRule.end()!=std::find(StrListRule.begin(),StrListRule.end(),"ValueIsInteger"));
BOOL bOptional=(StrListRule.end()!=std::find(StrListRule.begin(),StrListRule.end(),"Optional"));
MAP_INI_KEY_2_VALUE::iterator itrValue=pIniSection->MapIniKey2Value.find((const char*)strKeyFull);
//检查顺序
if((!bKeySequenceErrorFound)&&bRestrictKeySequence)
{
//
if(itrValue==pIniSection->MapIniKey2Value.end())
{
//
if(!bOptional)
{
m_pIBasicMsgUI->ShowErrorMsg(FormatString(
"Title %s 的关键字%s 不存在",
(pszSection==(CString)NO_SECTION_FILE_SECTION_NAME)?"":pszSection,(const char*)strKeyFull
).c_str());
//停止检查关键字顺序
bKeySequenceErrorFound=TRUE;
}
}
else
{
if(0==strcmpi((*itrValueKey).c_str(),strKeyFull))
{
//检查下一个关键字
++itrValueKey;
}
else
{
if(!bOptional)
{
//不是期望的关键字
m_pIBasicMsgUI->ShowErrorMsg(FormatString(
"Title %s 的关键字 %s 的顺序不正确。期望的关键字是 %s",
(pszSection==(CString)NO_SECTION_FILE_SECTION_NAME)?"":pszSection,
(*itrValueKey).c_str(),(const char*)strKeyFull
).c_str());
bKeySequenceErrorFound=TRUE;
}
}
}
}
if(pIniSection->MapIniKey2Value.end()==itrValue)
{
//没有值则不检查值有效性
return TRUE;
}
else
{
//
if((*itrValue).second=="")
{
if(bValueNonEmpty)
{
m_pIBasicMsgUI->ShowErrorMsg(FormatString(
"Title %s 的关键字 %s 值不允许为空",
(pszSection==(CString)NO_SECTION_FILE_SECTION_NAME)?"":pszSection,(const char*)strKeyFull
).c_str());
return FALSE;
}
return TRUE;
}
}
//KEY合法性检查
BOOL bKeyMaxLength=(strRule.Find("KeyMaxLength")>=0);
if(bKeyMaxLength)
{
std::string strSubStringKeyMaxLength;
GetSubString(
strRule,
"KeyMaxLength(",
")",
strSubStringKeyMaxLength
);
long lMaxLength=atol(strSubStringKeyMaxLength.c_str());
long lLenKeyFull=strlen(strKeyFull);
if(lLenKeyFull>lMaxLength)
{
m_pIBasicMsgUI->ShowErrorMsg(FormatString(
"Title %s 的关键字 %s 名称长度为 %s 大于最大值 %d",
(pszSection==(CString)NO_SECTION_FILE_SECTION_NAME)?"":pszSection,(const char*)strKeyFull,lLenKeyFull,lMaxLength
).c_str());
return FALSE;
}
}
BOOL bKeyIsInteger=(strRule.Find("KeyIsInteger")>=0);
if(bKeyIsInteger)
{
if(!IsNumber(strKeyFull))
{
m_pIBasicMsgUI->ShowErrorMsg(FormatString(
"Title %s 的关键字 %s 不是整数",(pszSection==(CString)NO_SECTION_FILE_SECTION_NAME)?"":pszSection,(const char*)strKeyFull
).c_str());
return FALSE;
}
}
////value 合法性检查
CString strVal=(*itrValue).second.c_str();
//值在不需要检查的值列表中 则不检查
BOOL bValueNoCheck=(strRule.Find("ValueNoCheck")>=0);
if(bValueNoCheck)
{
std::string strSubStringValueNoCheck;
GetSubString(
strRule,
"ValueNoCheck(",
")",
strSubStringValueNoCheck
);
STRING_LIST FixValueList;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -