📄 xmlconfig2.cpp
字号:
#include "XMLConfig2.h"
#include "MyVersion.h"
#include <string>
#include "MyAssert.h"
#include "OSMacro.h"
#include "XMLDoc.h"
#include "SvrList.h"
#include "StrOperation.h"
#include "SocketUtils.h"
#include <sys/stat.h>
#include <iostream>
//const std::string& CDMS_CONFIG_FILE = "../../config.xml";
#define GET_CONTENT_VALUE(strKey, value, isDynUpdate) { \
if(!GetContent(strKey, value, isDynUpdate)) \
return FALSE; }
#define GET_PROPERTY_VALUE(strKey, value, isDynUpdate) { \
if(!GetProperty(strKey, value, isDynUpdate)) \
return FALSE; }
CXMLConfig::CXMLConfig(const string& strFileName)
: m_strFileName(strFileName), m_pDoc(NULL)
{
m_Isinit = FALSE;
m_IsFirstRead = TRUE;
TransPathDelimiter(m_strFileName);
}
CXMLConfig::~CXMLConfig()
{
if(NULL != m_pDoc)
{
delete m_pDoc;
m_pDoc = NULL;
}
}
void CXMLConfig::TransPathDelimiter(string& strPath)
{
int nPos;
for(nPos=strPath.find(kBadPathDelimiterStr); nPos!=-1; nPos=strPath.find(kBadPathDelimiterStr))
{
strPath.at(nPos) = kPathDelimiterChar;
}
}
Bool CXMLConfig::LoadFile()
{
if(NULL == m_pDoc)
{
if(0 == m_strFileName.length())
{
return FALSE;
}
m_pDoc = new XMLDoc(m_strFileName.c_str());
if(NULL == m_pDoc)
return FALSE;
}
if(!m_pDoc->LoadXMLDoc())
{
return FALSE;
}
m_Isinit = TRUE;
return TRUE;
}
void pr( vector< SIPandPort >&t )
{
for( int i = 0; i < t.size(); i++ )
{
std::cout << t[i].m_IP << " " << t[i].m_Port << std::endl;
}
}
OSMutex g_ReadConfigLock;
Bool CXMLConfig::ReadConfigItem()
{
OSMutexLocker tmpLocker(&g_ReadConfigLock);
if(!m_Isinit)
return FALSE;
GET_PROPERTY_VALUE("config/server/thread/number", SConfigPara::m_ThreadNum, FALSE );
GET_PROPERTY_VALUE("config/server/thread/stacksize", SConfigPara::m_StackSize, FALSE );
GET_CONTENT_VALUE("config/server/directory/mediadir", SConfigPara::m_MoviesDir, TRUE );
CStrOperate::TransPathFormat(SConfigPara::m_MoviesDir);
GET_CONTENT_VALUE("config/server/directory/logdir", SConfigPara::m_LogDir, TRUE );
CStrOperate::TransPathFormat(SConfigPara::m_LogDir);
GET_PROPERTY_VALUE("config/server/portrange/start", SConfigPara::m_StartPortNum, FALSE );
GET_PROPERTY_VALUE("config/server/portrange/end", SConfigPara::m_EndPortNum, FALSE );
GET_PROPERTY_VALUE("config/log/runlog/status", SConfigPara::m_RunLogState, TRUE );
GET_PROPERTY_VALUE("config/log/errlog/status", SConfigPara::m_ErrLogState, TRUE );
if(m_IsFirstRead)
{
m_IsFirstRead = FALSE; //已经Update了一次
}
return TRUE;
}
Bool CXMLConfig::GetItemValue(XMLNode* pNode, const string& strKey, UInt32 *pdwValue, Bool IsDynUpdate/*=TRUE*/)
{
if(NULL == pNode)
return FALSE;
if(!m_IsFirstRead && !IsDynUpdate)
return TRUE;
XMLNode *pLeafNode = NULL;
if(NULL == (pLeafNode = pNode->GetChildren().GetNodeByName(strKey)))
return FALSE;
if(NULL != pdwValue)
{
*pdwValue = atol(pLeafNode->GetContent().c_str());
}
return TRUE;
}
Bool CXMLConfig::GetItemValue(XMLNode* pNode, const string& strKey, char *pcValue, Bool IsDynUpdate/*=TRUE*/)
{
if(NULL == pNode)
return FALSE;
if(!m_IsFirstRead && !IsDynUpdate)
return TRUE;
XMLNode *pLeafNode;
if(NULL == (pLeafNode = pNode->GetChildren().GetNodeByName(strKey)))
return FALSE;
if(NULL != pcValue)
{
strcpy(pcValue, pLeafNode->GetContent().c_str());
}
return TRUE;
}
Bool CXMLConfig::GetContent(const string& strPos, UInt32& outVal, Bool isDynUpdate/*=TRUE*/)
{
if(!m_IsFirstRead && !isDynUpdate)
return TRUE;
string strTmp;
if(!GetContent(strPos, strTmp, isDynUpdate))
{
return FALSE;
}
if( strTmp == "true"
|| strTmp == "yes")
{
outVal = TRUE;
return TRUE;
}
if( strTmp == "false"
|| strTmp == "no")
{
outVal = FALSE;
return TRUE;
}
outVal = atol(strTmp.c_str());
return TRUE;
}
Bool CXMLConfig::GetContent(const string& strPos, string& outVal, Bool isDynUpdate/*=TRUE*/)
{
if(!m_IsFirstRead && !isDynUpdate)
return TRUE;
vector<string> vecKey;
SeperateString(strPos, vecKey);
XMLNode *pNode = m_pDoc->GetRootNode(vecKey[0].c_str());
if(pNode == NULL) return FALSE;
XMLNode *pNode2 = NULL;
for(Int32 i=1; i<vecKey.size(); i++)
{
pNode2 = pNode->GetChild(vecKey[i]);
if(pNode2 == NULL)
return FALSE;
pNode = pNode2;
}
outVal = pNode->GetContent().c_str();
return TRUE;
}
Bool CXMLConfig::GetProperty(const string& strPos, UInt32& outVal, Bool isDynUpdate/* =TRUE */)
{
if(!m_IsFirstRead && !isDynUpdate)
return TRUE;
string strTmp;
if(!GetProperty(strPos, strTmp, isDynUpdate))
{
return FALSE;
}
outVal = atol(strTmp.c_str());
return TRUE;
}
Bool CXMLConfig::GetProperty( const string& strPos, SIPandPort &outIpPort, Bool isDynUpdate )
{
if(!m_IsFirstRead && !isDynUpdate)
return TRUE;
string strIp;
string strPort;
string strValue;
strIp = strPos;
strPort = strPos;
strIp += "/ip";
if( !GetProperty( strIp, strValue, isDynUpdate ) )
{
return FALSE;
}
outIpPort.m_IP = strValue;
strValue = "";
strPort += "/port";
if( !GetProperty( strPort, strValue, isDynUpdate ) )
{
return FALSE;
}
outIpPort.m_Port = atol( strValue.c_str() );
return TRUE;
}
Bool CXMLConfig::GetProperty( const string &strPos, vector<SIPandPort>&outVec, Bool isDynUpdate )
{
if(!m_IsFirstRead && !isDynUpdate)
return TRUE;
vector<string> vecKey;
SeperateString( strPos, vecKey ); //将每个节点存入数组中
XMLNode *pNode = m_pDoc->GetRootNode( vecKey[0].c_str() );
if( pNode == NULL ) return FALSE;
XMLNode *pNode2 = NULL;
Int32 i = 0;
for( i = 1; i < vecKey.size() ; i++ )
{
pNode2 = pNode->GetChild( vecKey[i] );
if( pNode2 == NULL ) return FALSE;
pNode = pNode2;
}
Int32 num = pNode->GetChildren().size();
for( i = 0; i < num; i++ )
{
SIPandPort tmp;
tmp.m_IP = pNode->GetChild(i)->GetProperty( "ip" );
tmp.m_IP = SocketUtils::HostNameToStringIP(tmp.m_IP); //进行域名解析
tmp.m_Port = atol( pNode->GetChild(i)->GetProperty( "port" ).c_str() );
outVec.push_back( tmp );
}
return TRUE;
}
Bool CXMLConfig::GetProperty(const string& strPos, string& outVal, Bool isDynUpdate/* =TRUE */)
{
if(!m_IsFirstRead && !isDynUpdate)
return TRUE;
vector<string> vecKey;
SeperateString(strPos, vecKey); //将每个节点存入数组中
XMLNode *pNode = m_pDoc->GetRootNode(vecKey[0].c_str());
if(pNode == NULL) return FALSE;
XMLNode *pNode2 = NULL;
for(Int32 i=1; i<vecKey.size()-1; i++)
{
pNode2 = pNode->GetChild(vecKey[i]);
if(pNode2 == NULL) return FALSE;
pNode = pNode2;
}
outVal = pNode->GetProperty(vecKey[vecKey.size()-1]).c_str();
return TRUE;
}
void CXMLConfig::SeperateString(const string& inStr, vector<string>& outVec, const string Delimiter/*="/"*/)
{
Int32 Begin = 0;
Int32 End = 0;
string key;
Int32 DelimiterLen = Delimiter.length();
Begin = inStr.find(Delimiter);
Begin = (Begin == 0) ? DelimiterLen : 0;
do{
End = inStr.find(Delimiter, Begin);
End = (End == -1) ? inStr.length() : End;
key = inStr.substr(Begin, End-Begin);
if(!key.empty())
outVec.push_back(key);
Begin = End+DelimiterLen;
}while(End < inStr.length());
}
/////////////////////////////////////////////////////////
/////////CXMLConfigMng
/////////////////////////////////////////////////////////
CXMLConfigMng* CXMLConfigMng::m_Inst = NULL;
CXMLConfigMng::CXMLConfigMng(const string& strXmlFile)
: m_strXmlFile(strXmlFile)
{
m_LastModTime = 0;
m_pXmlConfig = NULL;
}
CXMLConfigMng::~CXMLConfigMng()
{
if(NULL != m_pXmlConfig)
delete m_pXmlConfig;
}
CXMLConfigMng* CXMLConfigMng::GetInst(const char *pFileName)
{
if(m_Inst == NULL)
{
if(pFileName)
m_Inst = new CXMLConfigMng(pFileName);
else
m_Inst = new CXMLConfigMng(RFS_SYS_CONFIGFILE);
}
return m_Inst;
}
Bool CXMLConfigMng::DelInst()
{
if(m_Inst != NULL)
{
delete m_Inst;
m_Inst = NULL;
}
return TRUE;
}
Bool CXMLConfigMng::IsModified()
{
struct stat BufStat;
stat(m_Inst->m_strXmlFile.c_str(), &BufStat);
time_t CurModTime = BufStat.st_mtime;
if(m_Inst->m_LastModTime != CurModTime)
{
m_Inst->m_LastModTime = CurModTime;
return TRUE;
}
return FALSE;
}
Bool CXMLConfigMng::ReadDataFromFile(Bool isRTSPClient)
{
OSMutexLocker cLocker(&m_MutexRun);
if(m_LastModTime == 0)
{
struct stat BufStat;
stat(m_strXmlFile.c_str(), &BufStat);
m_LastModTime = BufStat.st_mtime;
}
if(!LoadXmlConfig())
{
return FALSE;
}
Bool bRet;
if(isRTSPClient)
{
//bRet = m_pXmlConfig->ReadConfigItemForRTSPClient();
printf("Not Surpport reading this kind of Config File!\n");
return FALSE;
}
else
bRet = m_pXmlConfig->ReadConfigItem();
return bRet;
}
Bool CXMLConfigMng::LoadXmlConfig()
{
if(m_strXmlFile.length() == 0)
{
return FALSE;
}
if(NULL == m_pXmlConfig)
{
m_pXmlConfig = new CXMLConfig(m_strXmlFile);
if(NULL == m_pXmlConfig)
return FALSE;
}
if(!m_pXmlConfig->LoadFile())
{
return FALSE;
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -