This commit is contained in:
Glenn Maynard
2005-09-03 08:28:43 +00:00
parent 90f55ac9c2
commit e8182daf16
2 changed files with 217 additions and 246 deletions
+63 -85
View File
@@ -1,8 +1,5 @@
#include "global.h"
#include "XmlFile.h"
#include <iostream>
#include <sstream>
#include <string>
#include "RageFile.h"
#include "RageLog.h"
#include "RageUtil.h"
@@ -11,6 +8,10 @@
#include "arch/Dialog/Dialog.h"
#include "RageFileDriverMemory.h"
static inline long XStr2Int( const char* str, long default_value = 0 )
{
return str ? atol(str) : default_value;
}
static const char chXMLTagOpen = '<';
static const char chXMLTagClose = '>';
@@ -42,18 +43,18 @@ static char* tcsskip( const char* psz )
// Name : tcsechr
// Desc : similar with strchr with escape process
// Param : escape - will be escape character
static char* tcsechr( const char* psz, int ch, int escape )
static const char* tcsechr( const char* pch, int ch, char escape )
{
char* pch = (char*)psz;
char* prev_escape = NULL;
bool bInEscape = false;
while( pch && *pch )
{
if( *pch == escape && prev_escape == NULL )
prev_escape = pch;
if( *pch == escape && !bInEscape )
bInEscape = true;
else
{
prev_escape = NULL;
if( *pch == ch ) return (char*)pch;
bInEscape = false;
if( *pch == ch )
return pch;
}
pch++;
}
@@ -62,19 +63,17 @@ static char* tcsechr( const char* psz, int ch, int escape )
// Desc : similar with strlen with escape process
// Param : escape - will be escape character
static int tcselen( int escape, const char *start, const char *end )
static int tcselen( char escape, const char *start, const char *end )
{
int len = 0;
if( end == NULL )
end = (char*) sizeof(long);
const char *prev_escape = NULL;
bool bInEscape = false;
while( start && *start && start<end )
{
if( *start == escape && prev_escape == NULL )
prev_escape = start;
if( *start == escape && !bInEscape )
bInEscape = true;
else
{
prev_escape = NULL;
bInEscape = false;
len++;
}
++start;
@@ -84,7 +83,7 @@ static int tcselen( int escape, const char *start, const char *end )
// Desc : similar with _tcscpy with escape process
// Param : escape - will be escape character
static void unescape( char *psz, int escape, char* srt, char* end = NULL )
static void unescape( char *psz, int escape, const char* srt, const char* end = NULL )
{
const char* pch = srt;
if( end==NULL ) end = (char*)sizeof(long);
@@ -131,16 +130,21 @@ static bool XIsEmptyString( const CString &s )
return s.find_first_not_of( "\r\n\t " ) == s.npos;
}
// Desc : put string of (psz~end) on ps string
static void SetString( char* psz, char* end, CString* ps, bool trim = false, int escape = 0 )
// put string of (psz~end) on ps string
static void SetString( const char* psz, const char* end, CString* ps, bool trim = false, char escape = 0 )
{
if( trim )
{
while( psz && psz < end && isspace(*psz) ) psz++;
while( (end-1) && psz < (end-1) && isspace(*(end-1)) ) end--;
while( psz < end && isspace(*psz) )
psz++;
while( end-1 >= psz && isspace(end[-1]) )
end--;
}
int len = end - psz;
if( len <= 0 ) return;
if( len <= 0 )
return;
if( escape )
{
len = tcselen( escape, psz, end );
@@ -179,14 +183,8 @@ void XNode::Clear()
// Param : pszAttrs - xml of attributes
// pi = parser information
// Return : advanced string pointer. (error return NULL)
//--------------------------------------------------------
// Coder Date Desc
// bro 2002-10-29
//========================================================
char* XNode::LoadAttributes( const char* pszAttrs, PARSEINFO *pi /*= &piDefault*/)
const char* XNode::LoadAttributes( const char* xml, PARSEINFO *pi /*= &piDefault*/)
{
char* xml = (char*)pszAttrs;
while( xml && *xml )
{
xml = tcsskip( xml );
@@ -199,7 +197,7 @@ char* XNode::LoadAttributes( const char* pszAttrs, PARSEINFO *pi /*= &piDefault*
return xml;
// XML Attr Name
char* pEnd = strpbrk( xml, " =" );
const char* pEnd = strpbrk( xml, " =" );
if( pEnd == NULL )
{
// error
@@ -278,16 +276,10 @@ char* XNode::LoadAttributes( const char* pszAttrs, PARSEINFO *pi /*= &piDefault*
// Param : pszXml - plain xml text
// pi = parser information
// Return : advanced string pointer (error return NULL)
//--------------------------------------------------------
// Coder Date Desc
// bro 2002-10-29
//========================================================
char* XNode::Load( const char* pszXml, PARSEINFO *pi /*= &piDefault*/ )
const char* XNode::Load( const char* xml, PARSEINFO *pi /*= &piDefault*/ )
{
Clear();
char* xml = (char*)pszXml;
// <
xml = strchr( xml, chXMLTagOpen );
if( xml == NULL )
@@ -325,7 +317,7 @@ char* XNode::Load( const char* pszXml, PARSEINFO *pi /*= &piDefault*/ )
// XML Node Tag Name Open
xml++;
char* pTagEnd = strpbrk( xml, " \t\r\n/>" );
const char* pTagEnd = strpbrk( xml, " \t\r\n/>" );
SetString( xml, pTagEnd, &m_sName );
xml = pTagEnd;
@@ -345,8 +337,21 @@ char* XNode::Load( const char* pszXml, PARSEINFO *pi /*= &piDefault*/ )
if( *xml == chXMLDash )
xml++;
if( *xml == chXMLTagClose )
if( *xml != chXMLTagClose )
{
// error: <TAG ... / >
if( !pi->error_occur )
{
pi->error_occur = true;
pi->error_pointer = xml;
pi->error_code = PIE_ALONE_NOT_CLOSED;
pi->error_string = "Element must be closed.";
}
// ill-formed tag
return NULL;
}
// well-formed tag
++xml;
@@ -358,30 +363,13 @@ char* XNode::Load( const char* pszXml, PARSEINFO *pi /*= &piDefault*/ )
return xml;
}
else
{
// error: <TAG ... / >
if( !pi->error_occur )
{
pi->error_occur = true;
pi->error_pointer = xml;
pi->error_code = PIE_ALONE_NOT_CLOSED;
pi->error_string = ("Element must be closed.");
}
// not wel-formed tag
return NULL;
}
}
else
// open/close tag <TAG ..> ... </TAG>
// ^- current pointer
{
// text value°¡ ¾øÀ¸¸E³Öµµ·ÏÇÑ´Ù.
//if( this->m_sValue.empty() || this->m_sValue == ("") )
if( XIsEmptyString( m_sValue ) )
{
// Text Value
char* pEnd = tcsechr( ++xml, chXMLTagOpen, chXMLEscape );
const char* pEnd = tcsechr( ++xml, chXMLTagOpen, chXMLEscape );
if( pEnd == NULL )
{
if( !pi->error_occur )
@@ -397,7 +385,6 @@ char* XNode::Load( const char* pszXml, PARSEINFO *pi /*= &piDefault*/ )
bool trim = pi->trim_value;
char escape = pi->escape_value;
//SetString( xml, pEnd, &m_sValue, trim, chXMLEscape );
SetString( xml, pEnd, &m_sValue, trim, escape );
xml = pEnd;
@@ -435,7 +422,7 @@ char* XNode::Load( const char* pszXml, PARSEINFO *pi /*= &piDefault*/ )
return NULL;
CString closename;
char* pEnd = strpbrk( xml, " >" );
const char* pEnd = strpbrk( xml, " >" );
if( pEnd == NULL )
{
if( !pi->error_occur )
@@ -472,14 +459,11 @@ char* XNode::Load( const char* pszXml, PARSEINFO *pi /*= &piDefault*/ )
}
}
else // Alone child Tag Loaded
// else ÇØ¾ßÇÏ´ÂÁE¸»¾Æ¾ßÇÏ´ÂÁEÀǽɰ£´Ù.
{
//if( xml && this->m_sValue.empty() && *xml !=chXMLTagOpen )
if( xml && XIsEmptyString( m_sValue ) && *xml !=chXMLTagOpen )
{
// Text Value
char* pEnd = tcsechr( xml, chXMLTagOpen, chXMLEscape );
const char* pEnd = tcsechr( xml, chXMLTagOpen, chXMLEscape );
if( pEnd == NULL )
{
// error cos not exist CloseTag </TAG>
@@ -495,7 +479,6 @@ char* XNode::Load( const char* pszXml, PARSEINFO *pi /*= &piDefault*/ )
bool trim = pi->trim_value;
char escape = pi->escape_value;
//SetString( xml, pEnd, &m_sValue, trim, chXMLEscape );
SetString( xml, pEnd, &m_sValue, trim, escape );
xml = pEnd;
@@ -505,7 +488,6 @@ char* XNode::Load( const char* pszXml, PARSEINFO *pi /*= &piDefault*/ )
}
}
}
}
return xml;
}
@@ -524,7 +506,6 @@ bool XNode::GetXML( RageFileBasic &f, DISP_OPT *opt ) const
// tab
if( opt && opt->newline )
{
if( opt && opt->newline )
if( f.Write("\r\n") == -1 )
return false;
if( opt->write_tabs )
@@ -634,7 +615,7 @@ void XNode::SetValue(bool v) { m_sValue = ssprintf("%d",v); }
void XNode::SetValue( unsigned v ) { m_sValue = ssprintf("%u",v); }
void XNode::SetValue( const DateTime &v ) { m_sValue = v.GetString(); }
const XAttr *XNode::GetAttr( const char* attrname ) const
const XAttr *XNode::GetAttr( const CString &attrname ) const
{
multimap<CString, XAttr*>::const_iterator it = m_attrs.find( attrname );
if( it != m_attrs.end() )
@@ -645,7 +626,7 @@ const XAttr *XNode::GetAttr( const char* attrname ) const
return NULL;
}
XAttr *XNode::GetAttr( const char* attrname )
XAttr *XNode::GetAttr( const CString &attrname )
{
multimap<CString, XAttr*>::iterator it = m_attrs.find( attrname );
if( it != m_attrs.end() )
@@ -657,7 +638,7 @@ XAttr *XNode::GetAttr( const char* attrname )
}
// Desc : get attribute with attribute name, return its value
const char* XNode::GetAttrValue( const char* attrname )
const char* XNode::GetAttrValue( const CString &attrname )
{
XAttr *attr = GetAttr( attrname );
return attr ? (const char*)attr->m_sValue : NULL;
@@ -665,7 +646,7 @@ const char* XNode::GetAttrValue( const char* attrname )
// get child node count
int XNode::GetChildCount()
int XNode::GetChildCount() const
{
return m_childs.size();
}
@@ -807,12 +788,12 @@ XENTITY *XENTITYS::GetEntity( int entity )
return NULL;
}
XENTITY *XENTITYS::GetEntity( char* entity )
XENTITY *XENTITYS::GetEntity( const char* entity )
{
for( unsigned i = 0 ; i < size(); i ++ )
{
char* ref = (char*)at(i).ref;
char* ps = entity;
const char* ref = at(i).ref;
const char* ps = entity;
while( ref && *ref )
if( *ref++ != *ps++ )
break;
@@ -825,17 +806,16 @@ XENTITY *XENTITYS::GetEntity( char* entity )
int XENTITYS::GetEntityCount( const char* str )
{
int nCount = 0;
char* ps = (char*)str;
while( ps && *ps )
if( GetEntity( *ps++ ) ) nCount ++;
while( str && *str )
if( GetEntity(*str++) )
++nCount;
return nCount;
}
int XENTITYS::Ref2Entity( const char* estr, char* str, int strlen )
int XENTITYS::Ref2Entity( const char* pes, char* str, int len )
{
char* pes = (char*)estr;
char* ps = str;
char* ps_end = ps+strlen;
char* ps_end = ps+len;
while( pes && *pes && ps < ps_end )
{
XENTITY *ent = GetEntity( pes );
@@ -855,9 +835,8 @@ int XENTITYS::Ref2Entity( const char* estr, char* str, int strlen )
return ps-str;
}
int XENTITYS::Entity2Ref( const char* str, char* estr, int estrlen )
int XENTITYS::Entity2Ref( const char* ps, char* estr, int estrlen )
{
char* ps = (char*)str;
char* pes = (char*)estr;
char* pes_end = pes+estrlen;
while( ps && *ps && pes < pes_end )
@@ -866,7 +845,7 @@ int XENTITYS::Entity2Ref( const char* str, char* estr, int estrlen )
if( ent )
{
// copy entity string
char* ref = (char*)ent->ref;
const char* ref = ent->ref;
while( ref && *ref )
*pes++ = *ref++;
}
@@ -907,7 +886,6 @@ CString XENTITYS::Entity2Ref( const char* str )
int len = strlen(str) + nEntityCount*10 ;
//char* sbuf = s.GetBufferSetLength( len+1 );
char* szTemp = new char[len+1];
if( szTemp )
Entity2Ref( str, szTemp, len );
s = szTemp;
delete [] szTemp;
+11 -18
View File
@@ -68,7 +68,7 @@ struct XENTITY
struct XENTITYS : public vector<XENTITY>
{
XENTITY *GetEntity( int entity );
XENTITY *GetEntity( char* entity );
XENTITY *GetEntity( const char* entity );
int GetEntityCount( const char* str );
int Ref2Entity( const char* estr, char* str, int strlen );
int Entity2Ref( const char* str, char* estr, int estrlen );
@@ -102,7 +102,7 @@ struct PARSEINFO
char* xml; // [get] xml source
bool error_occur; // [get] is occurance of error?
char* error_pointer; // [get] error position of xml source
const char* error_pointer; // [get] error position of xml source
PCODE error_code; // [get] error code
CString error_string; // [get] error string
@@ -150,6 +150,9 @@ struct XNode
{
CString m_sName; // a duplicate of the m_sName in the parent's map
CString m_sValue;
XNodes m_childs; // child node
XAttrs m_attrs; // attributes
void GetValue( CString &out ) const;
void GetValue( int &out ) const;
void GetValue( float &out ) const;
@@ -162,13 +165,9 @@ struct XNode
void SetValue( unsigned v );
void SetValue( const DateTime &v );
// internal variables
XNodes m_childs; // child node
XAttrs m_attrs; // attributes
// Load/Save XML
char* Load( const char* pszXml, PARSEINFO *pi );
char* LoadAttributes( const char* pszAttrs, PARSEINFO *pi );
const char *Load( const char* pszXml, PARSEINFO *pi );
const char *LoadAttributes( const char* pszAttrs, PARSEINFO *pi );
bool GetXML( RageFileBasic &f, DISP_OPT *opt ) const;
CString GetXML() const;
@@ -178,9 +177,9 @@ struct XNode
bool SaveToFile( RageFileBasic &f, DISP_OPT *opt ) const;
// in own attribute list
const XAttr *GetAttr( const char* attrname ) const;
XAttr *GetAttr( const char* attrname );
const char* GetAttrValue( const char* attrname );
const XAttr *GetAttr( const CString &attrname ) const;
XAttr *GetAttr( const CString &attrname );
const char* GetAttrValue( const CString &attrname );
bool GetAttrValue( const char* name, CString &out ) const { const XAttr* pAttr=GetAttr(name); if(pAttr==NULL) return false; pAttr->GetValue(out); return true; }
bool GetAttrValue( const char* name, int &out ) const { const XAttr* pAttr=GetAttr(name); if(pAttr==NULL) return false; pAttr->GetValue(out); return true; }
bool GetAttrValue( const char* name, float &out ) const { const XAttr* pAttr=GetAttr(name); if(pAttr==NULL) return false; pAttr->GetValue(out); return true; }
@@ -203,7 +202,7 @@ struct XNode
const char* GetChildAttrValue( const char* name, const char* attrname );
// modify DOM
int GetChildCount();
int GetChildCount() const;
XNode *AppendChild( const char* m_sName = NULL, const char* value = NULL );
XNode *AppendChild( const char* m_sName, float value );
XNode *AppendChild( const char* m_sName, int value );
@@ -229,10 +228,4 @@ struct XNode
void Clear();
};
// Helper Funtion
inline long XStr2Int( const char* str, long default_value = 0 )
{
return str ? atol(str) : default_value;
}
#endif