📄 mxmlnode.cpp
字号:
#include "MCRT/MXMLNode.h"
#include "tinyxml.h"
MXMLNode::MXMLNode()
:m_pMXMLNodeImpl(NULL)
{
}
MXMLNode::MXMLNode( TiXmlElement* pMXMLNodeImpl )
:m_pMXMLNodeImpl(pMXMLNodeImpl)
{
}
MXMLNode::~MXMLNode()
{
}
MXMLNode* MXMLNode::Clone() const
{
TiXmlElement* pClone = new TiXmlElement( *(this->m_pMXMLNodeImpl) );
if ( pClone == NULL )
{
return NULL;
}
return new MXMLNode( pClone );
}
void MXMLNode::FreeNode()
{
delete this->m_pMXMLNodeImpl;
delete this;
}
const char* MXMLNode::GetName()
{
if ( this->m_pMXMLNodeImpl != NULL )
{
return this->m_pMXMLNodeImpl->Value();
}
else
{
return NULL;
}
}
void MXMLNode::SetName( const std::string& strName )
{
if ( this->m_pMXMLNodeImpl == NULL )
{
this->m_pMXMLNodeImpl = new TiXmlElement( strName.c_str() );
}
else
{
delete this->m_pMXMLNodeImpl;
this->m_pMXMLNodeImpl = NULL;
this->m_pMXMLNodeImpl = new TiXmlElement( strName.c_str() );
}
}
const char* MXMLNode::GetValue()
{
if ( this->m_pMXMLNodeImpl != NULL )
{
return this->m_pMXMLNodeImpl->GetText();
}
else
{
return NULL;
}
}
void MXMLNode::SetValue( const std::string& strValue )
{
if ( this->m_pMXMLNodeImpl != NULL )
{
if ( this->m_pMXMLNodeImpl->GetText() != NULL ) //update node value
{
TiXmlNode* pNode = this->m_pMXMLNodeImpl->FirstChild();
while ( pNode != NULL )
{
if ( pNode->Type() == TiXmlNode::TEXT )
{
this->m_pMXMLNodeImpl->RemoveChild( pNode );
break;
}
pNode = pNode->NextSibling();
}
}
this->m_pMXMLNodeImpl->LinkEndChild( new TiXmlText(strValue.c_str()) );
}
}
const char* MXMLNode::GetAttr( const std::string& strAttrName )
{
if ( this->m_pMXMLNodeImpl != NULL )
{
return this->m_pMXMLNodeImpl->Attribute( strAttrName.c_str() );
}
else
{
return NULL;
}
}
mInt32 MXMLNode::GetIntAttr( const std::string& strAttrName )
{
mInt32 iAttrValue = 0;
if ( this->m_pMXMLNodeImpl != NULL )
{
this->m_pMXMLNodeImpl->QueryIntAttribute( strAttrName.c_str(), &iAttrValue );
}
return iAttrValue;
}
double MXMLNode::GetDoubleAttr( const std::string& strAttrName )
{
double dAttrValue = 0.0;
if ( this->m_pMXMLNodeImpl != NULL )
{
this->m_pMXMLNodeImpl->QueryDoubleAttribute( strAttrName.c_str(), &dAttrValue );
}
return dAttrValue;
}
void MXMLNode::SetAttr( const std::string& strAttrName, const std::string& strAttrValue )
{
if ( this->m_pMXMLNodeImpl != NULL )
{
this->m_pMXMLNodeImpl->SetAttribute( strAttrName.c_str(), strAttrValue.c_str() );
}
}
void MXMLNode::SetIntAttr( const std::string& strAttrName, mInt32 iAttrValue )
{
if ( this->m_pMXMLNodeImpl != NULL )
{
this->m_pMXMLNodeImpl->SetAttribute( strAttrName.c_str(), iAttrValue );
}
}
void MXMLNode::SetDoubleAttr( const std::string& strAttrName, double dAttrValue )
{
if ( this->m_pMXMLNodeImpl != NULL )
{
this->m_pMXMLNodeImpl->SetDoubleAttribute( strAttrName.c_str(), dAttrValue );
}
}
void MXMLNode::DeleteAttr( const std::string& strAttrName )
{
if ( this->m_pMXMLNodeImpl != NULL )
{
this->m_pMXMLNodeImpl->RemoveAttribute( strAttrName.c_str() );
}
}
MXMLNode MXMLNode::FirstChild()
{
if ( this->m_pMXMLNodeImpl == NULL )
{
return MXMLNode( NULL );
}
return MXMLNode( this->m_pMXMLNodeImpl->FirstChildElement() );
}
MXMLNode MXMLNode::Next()
{
if ( this->m_pMXMLNodeImpl == NULL )
{
return MXMLNode( NULL );
}
return MXMLNode( this->m_pMXMLNodeImpl->NextSiblingElement() );
}
MXMLNode MXMLNode::GetChild( const std::string& strChildName, mInt32 index )
{
if ( this->m_pMXMLNodeImpl == NULL )
{
return MXMLNode( NULL );
}
TiXmlElement* pChildImpl = NULL;
if ( index == 0 )
{
pChildImpl = this->m_pMXMLNodeImpl->FirstChildElement( strChildName.c_str() );
}
else
{
TiXmlHandle handle(this->m_pMXMLNodeImpl);
TiXmlHandle handleChild = handle.ChildElement( strChildName.c_str(), index );
pChildImpl = handleChild.Element();
}
return MXMLNode( pChildImpl );
}
MXMLNode MXMLNode::GetNode( const std::string& strNodePath, mInt32 index )
{
if ( strNodePath.empty() || (strNodePath[0] == '/') )
{
return MXMLNode( NULL );
}
std::string::size_type posBegin = 0;
std::string::size_type posEnd = std::string::npos;
std::string strTmpNodePath = strNodePath;
std::string strNodeName;
MXMLNode node = *this;
std::string::size_type lenPath = strTmpNodePath.length();
if ( strTmpNodePath[lenPath-1] == '/' )
{
strTmpNodePath.resize( lenPath - 1 );
}
posEnd = strTmpNodePath.find_first_of( '/', posBegin );
while ( posEnd != std::string::npos )
{
strNodeName = strTmpNodePath.substr( posBegin, posEnd - posBegin );
node = node.GetChild( strNodeName );
if ( ! node.IsValid() )
{
return MXMLNode( NULL );
}
posBegin = posEnd + 1;
posEnd = strTmpNodePath.find_first_of( '/', posBegin );
}
strNodeName = strTmpNodePath.substr( posBegin );
node = node.GetChild( strNodeName, index );
return node;
}
void MXMLNode::AddChild( const MXMLNode& childNode )
{
if ( (this->m_pMXMLNodeImpl != NULL) && (childNode.IsValid()) )
{
this->m_pMXMLNodeImpl->LinkEndChild( childNode.m_pMXMLNodeImpl );
}
}
void MXMLNode::DeleteChild( MXMLNode& childNode )
{
if ( (this->m_pMXMLNodeImpl != NULL) && (childNode.IsValid()) )
{
this->m_pMXMLNodeImpl->RemoveChild( childNode.m_pMXMLNodeImpl );
}
}
bool MXMLNode::IsValid() const
{
return (this->m_pMXMLNodeImpl != NULL);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -