Files
itgmania212121/stepmania/src/XmlFile.cpp
T

140 lines
4.5 KiB
C++
Raw Normal View History

2005-10-11 09:37:18 +00:00
// Adapted from http://www.codeproject.com/cpp/xmlite.asp.
// On 2004-02-09 Cho, Kyung-Min gave us permission to use and modify this
// library.
//
// XmlFile : XML Lite Parser Library
// by Cho, Kyung Min: bro@shinbiro.com 2002-10-30
2004-02-09 08:10:01 +00:00
#include "global.h"
#include "XmlFile.h"
2004-02-10 09:42:01 +00:00
#include "RageFile.h"
2004-04-02 05:06:32 +00:00
#include "RageLog.h"
2004-07-18 21:55:49 +00:00
#include "RageUtil.h"
#include "DateTime.h"
2005-01-07 09:09:23 +00:00
#include "Foreach.h"
2004-02-09 08:10:01 +00:00
XNode::XNode( const XNode &cpy ):
m_sName( cpy.m_sName ),
2005-10-11 10:34:27 +00:00
m_sValue( cpy.m_sValue ),
m_attrs( cpy.m_attrs )
{
FOREACH_CONST_Child( &cpy, c )
this->AppendChild( new XNode(*c) );
}
XNode::~XNode()
2004-02-09 08:10:01 +00:00
{
2005-01-07 14:28:00 +00:00
Clear();
2004-02-09 08:10:01 +00:00
}
2005-01-07 14:28:00 +00:00
void XNode::Clear()
2004-02-09 08:10:01 +00:00
{
2005-01-07 09:09:23 +00:00
FOREACH_Child( this, p )
SAFE_DELETE( p );
2005-01-07 14:28:00 +00:00
m_childs.clear();
m_attrs.clear();
2004-02-09 08:10:01 +00:00
}
2005-12-27 23:43:49 +00:00
void XNode::GetValue( RString &out ) const { out = m_sValue; }
2005-09-03 08:28:43 +00:00
void XNode::GetValue( int &out ) const { out = atoi(m_sValue); }
2006-06-12 06:42:25 +00:00
void XNode::GetValue( float &out ) const { out = StringToFloat(m_sValue); }
2005-09-03 08:28:43 +00:00
void XNode::GetValue( bool &out ) const { out = atoi(m_sValue) != 0; }
2006-10-01 14:24:19 +00:00
void XNode::GetValue( unsigned &out ) const { out = strtoul(m_sValue,NULL,0); }
2005-09-03 08:28:43 +00:00
void XNode::GetValue( DateTime &out ) const { out.FromString( m_sValue ); }
2004-07-18 21:55:49 +00:00
2005-12-27 23:43:49 +00:00
bool XNode::GetAttrValue( const RString &sName, RString &out ) const { const RString* pAttr=GetAttr(sName); if(pAttr==NULL) return false; out = *pAttr; return true; }
2006-10-01 14:22:15 +00:00
bool XNode::GetAttrValue( const RString &sName, int &out ) const { const RString* pAttr=GetAttr(sName); if(pAttr==NULL) return false; out = atoi(*pAttr); return true; }
2006-06-12 06:42:25 +00:00
bool XNode::GetAttrValue( const RString &sName, float &out ) const { const RString* pAttr=GetAttr(sName); if(pAttr==NULL) return false; out = StringToFloat(*pAttr); return true; }
2006-10-01 14:22:15 +00:00
bool XNode::GetAttrValue( const RString &sName, bool &out ) const { const RString* pAttr=GetAttr(sName); if(pAttr==NULL) return false; out = atoi(*pAttr) != 0; return true; }
2005-12-27 23:43:49 +00:00
bool XNode::GetAttrValue( const RString &sName, unsigned &out ) const { const RString* pAttr=GetAttr(sName); if(pAttr==NULL) return false; out = 0; sscanf(*pAttr,"%u",&out); return true; }
bool XNode::GetAttrValue( const RString &sName, DateTime &out ) const { const RString* pAttr=GetAttr(sName); if(pAttr==NULL) return false; out.FromString( *pAttr ); return true; }
2004-07-18 21:55:49 +00:00
void XNode::SetValue( const RString &v ) { m_sValue = v; }
2005-09-03 08:28:43 +00:00
void XNode::SetValue( int v ) { m_sValue = ssprintf("%d",v); }
void XNode::SetValue( float v ) { m_sValue = ssprintf("%f",v); }
void XNode::SetValue( unsigned v ) { m_sValue = ssprintf("%u",v); }
2006-10-01 14:22:15 +00:00
void XNode::SetValue( const DateTime &v ) { m_sValue = v.GetString(); }
2004-07-17 21:49:32 +00:00
2005-12-27 23:43:49 +00:00
const RString *XNode::GetAttr( const RString &attrname ) const
2004-02-22 05:12:23 +00:00
{
2005-12-27 23:43:49 +00:00
map<RString, RString>::const_iterator it = m_attrs.find( attrname );
2005-01-07 14:28:00 +00:00
if( it != m_attrs.end() )
return &it->second;
2004-02-22 05:12:23 +00:00
return NULL;
}
2005-12-27 23:43:49 +00:00
RString *XNode::GetAttr( const RString &attrname )
2004-02-09 08:10:01 +00:00
{
2005-12-27 23:43:49 +00:00
map<RString, RString>::iterator it = m_attrs.find( attrname );
2005-01-07 14:28:00 +00:00
if( it != m_attrs.end() )
return &it->second;
2004-02-09 08:10:01 +00:00
return NULL;
}
2005-12-27 23:43:49 +00:00
XNode *XNode::GetChild( const RString &sName )
2004-02-09 08:10:01 +00:00
{
2005-12-27 23:43:49 +00:00
multimap<RString, XNode*>::iterator it = m_childs.find( sName );
2005-01-07 14:28:00 +00:00
if( it != m_childs.end() )
{
2005-09-04 16:45:38 +00:00
DEBUG_ASSERT( sName == it->second->m_sName );
2005-01-07 09:09:23 +00:00
return it->second;
2005-01-07 14:28:00 +00:00
}
2004-02-09 08:10:01 +00:00
return NULL;
}
2005-12-27 23:43:49 +00:00
const XNode *XNode::GetChild( const RString &sName ) const
2004-02-19 03:19:41 +00:00
{
2005-12-27 23:43:49 +00:00
multimap<RString, XNode*>::const_iterator it = m_childs.find( sName );
2005-01-07 14:28:00 +00:00
if( it != m_childs.end() )
{
2005-09-04 16:45:38 +00:00
DEBUG_ASSERT( sName == it->second->m_sName );
2005-01-07 09:09:23 +00:00
return it->second;
2005-01-07 14:28:00 +00:00
}
2004-02-19 03:19:41 +00:00
return NULL;
}
2004-07-23 02:27:37 +00:00
XNode *XNode::AppendChild( XNode *node )
2004-02-09 08:10:01 +00:00
{
2005-01-07 14:28:00 +00:00
DEBUG_ASSERT( node->m_sName.size() );
/* Hinted insert: optimize for alphabetical inserts, for the copy ctor. */
2005-12-27 23:43:49 +00:00
m_childs.insert( m_childs.end(), pair<RString,XNode*>(node->m_sName,node) );
2004-02-09 08:10:01 +00:00
return node;
}
2005-07-15 22:58:11 +00:00
// detach node and delete object
2006-10-02 06:36:07 +00:00
bool XNode::RemoveChild( XNode *node, bool bDelete )
2004-02-09 08:10:01 +00:00
{
2005-12-27 23:43:49 +00:00
FOREACHMM( RString, XNode*, m_childs, p )
2004-02-09 08:10:01 +00:00
{
2005-01-07 09:09:23 +00:00
if( p->second == node )
{
2006-10-02 06:36:07 +00:00
if( bDelete )
SAFE_DELETE( p->second );
2005-01-07 14:28:00 +00:00
m_childs.erase( p );
2005-01-07 09:09:23 +00:00
return true;
}
2004-02-09 08:10:01 +00:00
}
return false;
}
// detach attribute
2005-12-27 23:43:49 +00:00
bool XNode::RemoveAttr( const RString &sName )
2004-02-09 08:10:01 +00:00
{
return m_attrs.erase(sName) > 0;
2004-02-09 08:10:01 +00:00
}
2005-12-27 23:43:49 +00:00
void XNode::AppendAttr( const RString &sName, const RString &sValue )
2004-02-09 08:10:01 +00:00
{
2006-10-01 14:22:15 +00:00
DEBUG_ASSERT( sName.size() );
2005-10-11 11:12:24 +00:00
pair<XAttrs::iterator,bool> ret = m_attrs.insert( make_pair(sName,sValue) );
if( !ret.second )
ret.first->second = sValue; // already existed
2004-02-09 08:10:01 +00:00
}
2006-10-01 14:22:15 +00:00
void XNode::AppendAttr( const RString &sName, float value ) { AppendAttr(sName,ssprintf("%f",value)); }
2005-12-27 23:43:49 +00:00
void XNode::AppendAttr( const RString &sName, int value ) { AppendAttr(sName,ssprintf("%d",value)); }
void XNode::AppendAttr( const RString &sName, unsigned value ) { AppendAttr(sName,ssprintf("%u",value)); }
2004-02-09 08:10:01 +00:00