From 1a89ddfccdeb5ae2a8a8d6904974c0c4743bc1d4 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Tue, 3 Oct 2006 00:07:01 +0000 Subject: [PATCH] pointer to attributes --- stepmania/src/Actor.cpp | 18 ++++++------- stepmania/src/ActorUtil.cpp | 2 +- stepmania/src/ExportStrings.cpp | 2 +- stepmania/src/Font.cpp | 10 ++++---- stepmania/src/IniFile.cpp | 2 +- stepmania/src/InputMapper.cpp | 2 +- stepmania/src/ThemeManager.cpp | 12 ++++----- stepmania/src/TitleSubstitution.cpp | 2 +- stepmania/src/XmlFile.cpp | 40 ++++++++++++++++++++++++----- stepmania/src/XmlFile.h | 7 ++--- stepmania/src/XmlFileUtil.cpp | 6 ++--- 11 files changed, 66 insertions(+), 37 deletions(-) diff --git a/stepmania/src/Actor.cpp b/stepmania/src/Actor.cpp index 6105698266..a61b5c0746 100644 --- a/stepmania/src/Actor.cpp +++ b/stepmania/src/Actor.cpp @@ -262,17 +262,17 @@ void Actor::LoadFromNode( const RString& sDir, const XNode* pNode ) { // Load Name, if any. const RString &sKeyName = pAttr->first; - const XNodeValue &sValue = pAttr->second; - if( sKeyName == "Name" ) SetName( sValue.GetValue() ); - else if( sKeyName == "BaseRotationX" ) SetBaseRotationX( sValue.GetValue() ); - else if( sKeyName == "BaseRotationY" ) SetBaseRotationY( sValue.GetValue() ); - else if( sKeyName == "BaseRotationZ" ) SetBaseRotationZ( sValue.GetValue() ); - else if( sKeyName == "BaseZoomX" ) SetBaseZoomX( sValue.GetValue() ); - else if( sKeyName == "BaseZoomY" ) SetBaseZoomY( sValue.GetValue() ); - else if( sKeyName == "BaseZoomZ" ) SetBaseZoomZ( sValue.GetValue() ); + const XNodeValue *pValue = pAttr->second; + if( sKeyName == "Name" ) SetName( pValue->GetValue() ); + else if( sKeyName == "BaseRotationX" ) SetBaseRotationX( pValue->GetValue() ); + else if( sKeyName == "BaseRotationY" ) SetBaseRotationY( pValue->GetValue() ); + else if( sKeyName == "BaseRotationZ" ) SetBaseRotationZ( pValue->GetValue() ); + else if( sKeyName == "BaseZoomX" ) SetBaseZoomX( pValue->GetValue() ); + else if( sKeyName == "BaseZoomY" ) SetBaseZoomY( pValue->GetValue() ); + else if( sKeyName == "BaseZoomZ" ) SetBaseZoomZ( pValue->GetValue() ); else if( EndsWith(sKeyName,"Command") ) { - apActorCommands apac = ActorUtil::ParseActorCommands( sValue.GetValue(), ssprintf("%s: %s", sDir.c_str(), sKeyName.c_str()) ); + apActorCommands apac = ActorUtil::ParseActorCommands( pValue->GetValue(), ssprintf("%s: %s", sDir.c_str(), sKeyName.c_str()) ); RString sCmdName = sKeyName.Left( sKeyName.size()-7 ); AddCommand( sCmdName, apac ); diff --git a/stepmania/src/ActorUtil.cpp b/stepmania/src/ActorUtil.cpp index 5e3516826d..46df8d2b8f 100644 --- a/stepmania/src/ActorUtil.cpp +++ b/stepmania/src/ActorUtil.cpp @@ -315,7 +315,7 @@ static void MergeActorXML( XNode *pChild, const XNode *pParent ) RString sWarning = ssprintf( "Overriding \"%s\" (\"%s\") in XML node \"%s\" with \"%s\" in XML node \"%s\"", p->first.c_str(), - p->second.GetValue().c_str(), + p->second->GetValue().c_str(), pChild->GetName().c_str(), sOld.c_str(), pParent->GetName().c_str() ); diff --git a/stepmania/src/ExportStrings.cpp b/stepmania/src/ExportStrings.cpp index a00801e94a..0c083fa9ed 100644 --- a/stepmania/src/ExportStrings.cpp +++ b/stepmania/src/ExportStrings.cpp @@ -36,7 +36,7 @@ void ExportStrings::Nsis() FOREACH_CONST_Attr( child, attr ) { RString sName = attr->first; - RString sValue = attr->second.GetValue(); + RString sValue = attr->second->GetValue(); sValue.Replace( "\\n", "$\\n" ); RString sLine = ssprintf( "LangString %s ${LANG_%s} \"%s\"", sName.c_str(), sLangNameUpper.c_str(), sValue.c_str() ); out.PutLine( sLine ); diff --git a/stepmania/src/Font.cpp b/stepmania/src/Font.cpp index 36372449bf..b68255110e 100644 --- a/stepmania/src/Font.cpp +++ b/stepmania/src/Font.cpp @@ -396,14 +396,14 @@ void Font::LoadFontPageSettings( FontPageSettings &cfg, IniFile &ini, const RStr FOREACH_CONST_Attr( pNode, pAttr ) { RString sName = pAttr->first; - const XNodeValue &sValue = pAttr->second; + const XNodeValue *pValue = pAttr->second; sName.MakeUpper(); /* If val is an integer, it's a width, eg. "10=27". */ if( IsAnInt(sName) ) { - cfg.m_mapGlyphWidths[atoi(sName)] = sValue.GetValue(); + cfg.m_mapGlyphWidths[atoi(sName)] = pValue->GetValue(); continue; } @@ -439,7 +439,7 @@ void Font::LoadFontPageSettings( FontPageSettings &cfg, IniFile &ini, const RStr continue; } - cfg.CharToGlyphNo[c] = sValue.GetValue(); + cfg.CharToGlyphNo[c] = pValue->GetValue(); continue; } @@ -488,7 +488,7 @@ void Font::LoadFontPageSettings( FontPageSettings &cfg, IniFile &ini, const RStr iCount = iLast - iFirst + 1; } - RString sRet = cfg.MapRange( asMatches[0], iFirst, sValue.GetValue(), iCount ); + RString sRet = cfg.MapRange( asMatches[0], iFirst, pValue->GetValue(), iCount ); if( !sRet.empty() ) RageException::Throw( "Font definition \"%s\" has an invalid range \"%s\": %s.", ini.GetPath().c_str(), sName.c_str(), sRet.c_str() ); @@ -514,7 +514,7 @@ void Font::LoadFontPageSettings( FontPageSettings &cfg, IniFile &ini, const RStr ini.GetPath().c_str(), iFirstFrame, iNumFramesHigh ); /* Decode the string. */ - const wstring wdata( RStringToWstring(sValue.GetValue()) ); + const wstring wdata( RStringToWstring(pValue->GetValue()) ); if( int(wdata.size()) > iNumFramesWide ) RageException::Throw( "The font definition \"%s\" assigns %i characters to row %i (\"%ls\"), but the font is only %i characters wide.", diff --git a/stepmania/src/IniFile.cpp b/stepmania/src/IniFile.cpp index 2b8dc0f493..e2aaf18564 100644 --- a/stepmania/src/IniFile.cpp +++ b/stepmania/src/IniFile.cpp @@ -98,7 +98,7 @@ bool IniFile::WriteFile( RageFileBasic &f ) const FOREACH_CONST_Attr( pKey, pAttr ) { const RString &sName = pAttr->first; - const RString &sValue = pAttr->second.GetValue(); + const RString &sValue = pAttr->second->GetValue(); // TODO: Are there esacpe rules for these? DEBUG_ASSERT( sName.find('\n') == sName.npos ); diff --git a/stepmania/src/InputMapper.cpp b/stepmania/src/InputMapper.cpp index 88fb1606bb..c020801370 100644 --- a/stepmania/src/InputMapper.cpp +++ b/stepmania/src/InputMapper.cpp @@ -562,7 +562,7 @@ void InputMapper::ReadMappingsFromDisk() { const RString &name = i->first; RString value; - i->second.GetValue( value ); + i->second->GetValue( value ); GameInput GameI; GameI.FromString( m_pInputScheme, name ); diff --git a/stepmania/src/ThemeManager.cpp b/stepmania/src/ThemeManager.cpp index aaa18f3dec..5111e0163a 100644 --- a/stepmania/src/ThemeManager.cpp +++ b/stepmania/src/ThemeManager.cpp @@ -275,17 +275,17 @@ static void MergeIniUnder( XNode *pFrom, XNode *pTo ) XNode *pChildNode = pTo->GetChild( it->first ); if( pChildNode == NULL ) { - /* We're moving the XNode without copying it, so don't use RemoveChild--it'll - * delete it. */ + pFrom->RemoveChild( it->second, false ); // don't delete pTo->AppendChild( it->second ); - pFrom->m_childs.erase( it ); } else { - /* map::insert will not overwrite existing nodes. */ XNode *pFrom = it->second; - FOREACHM( RString, RString, pFrom->m_attrs, it ) - pChildNode->m_attrs.insert( *it ); + FOREACHM( RString, XNodeValue *, pFrom->m_attrs, it ) + { + /* Don't overwrite existing nodes. */ + pChildNode->AppendAttr( it->first, it->second, false ); + } } it = next; diff --git a/stepmania/src/TitleSubstitution.cpp b/stepmania/src/TitleSubstitution.cpp index d951199a64..e6b48ff089 100644 --- a/stepmania/src/TitleSubstitution.cpp +++ b/stepmania/src/TitleSubstitution.cpp @@ -50,7 +50,7 @@ void TitleTrans::LoadFromNode( const XNode* pNode ) /* Surround each regex with ^(...)$, to force all comparisons to default * to being a full-line match. (Add ".*" manually if this isn't wanted.) */ const RString &sKeyName = attr->first; - const RString &sValue = attr->second.GetValue(); + const RString sValue = attr->second->GetValue(); if( sKeyName == "DontTransliterate" ) translit = false; else if( sKeyName == "TitleFrom" ) TitleFrom = "^(" + sValue + ")$"; else if( sKeyName == "ArtistFrom" ) ArtistFrom = "^(" + sValue + ")$"; diff --git a/stepmania/src/XmlFile.cpp b/stepmania/src/XmlFile.cpp index f6d2e20d64..d6123acac9 100644 --- a/stepmania/src/XmlFile.cpp +++ b/stepmania/src/XmlFile.cpp @@ -30,6 +30,8 @@ void XNode::Clear() { FOREACH_Child( this, p ) SAFE_DELETE( p ); + FOREACH_Attr( this, pAttr ) + SAFE_DELETE( pAttr->second ); m_childs.clear(); m_attrs.clear(); } @@ -51,7 +53,7 @@ const XNodeValue *XNode::GetAttr( const RString &attrname ) const { XAttrs::const_iterator it = m_attrs.find( attrname ); if( it != m_attrs.end() ) - return &it->second; + return it->second; return NULL; } @@ -59,7 +61,7 @@ XNodeValue *XNode::GetAttr( const RString &attrname ) { XAttrs::iterator it = m_attrs.find( attrname ); if( it != m_attrs.end() ) - return &it->second; + return it->second; return NULL; } @@ -122,10 +124,36 @@ bool XNode::RemoveAttr( const RString &sName ) return true; } -void XNode::AppendAttr( const RString &sName, const XNodeValue &val ) +/* If bOverwrite is true and a node already exists with that name, the old value will be deleted. + * If bOverwrite is false and a node already exists with that name, the new value will be deleted. */ +XNodeValue *XNode::AppendAttr( const RString &sName, XNodeValue *pValue, bool bOverwrite ) { DEBUG_ASSERT( sName.size() ); - pair ret = m_attrs.insert( make_pair(sName,val) ); - if( !ret.second ) - ret.first->second = val; // already existed + pair ret = m_attrs.insert( make_pair(sName, (XNodeValue *) NULL) ); + if( !ret.second ) // already existed + { + if( bOverwrite ) + { + delete ret.first->second; + } + else + { + delete pValue; + pValue = ret.first->second; + } + } + + ret.first->second = pValue; + + return ret.first->second; +}; + +XNodeValue *XNode::AppendAttr( const RString &sName ) +{ + DEBUG_ASSERT( sName.size() ); + pair ret = m_attrs.insert( make_pair(sName, (XNodeValue *) NULL) ); + if( ret.second ) + ret.first->second = new XNodeValue(); + return ret.first->second; // already existed } + diff --git a/stepmania/src/XmlFile.h b/stepmania/src/XmlFile.h index 0f83057d77..5e08a98fa9 100644 --- a/stepmania/src/XmlFile.h +++ b/stepmania/src/XmlFile.h @@ -29,7 +29,7 @@ public: void SetValue( const DateTime &v ); }; -typedef map XAttrs; +typedef map XAttrs; class XNode; typedef multimap XNodes; @@ -94,9 +94,10 @@ public: XNode *AppendChild( XNode *node ); bool RemoveChild( XNode *node, bool bDelete = true ); - void AppendAttr( const RString &sName, const XNodeValue &val ); + XNodeValue *AppendAttr( const RString &sName, XNodeValue *pValue, bool bOverwrite = true ); + XNodeValue *AppendAttr( const RString &sName ); template - void AppendAttr( const RString &sName, T value ) { XNodeValue val; val.SetValue( value ); AppendAttr( sName, val ); } + XNodeValue *AppendAttr( const RString &sName, T value ) { XNodeValue *pVal = AppendAttr( sName ); pVal->SetValue( value ); return pVal; } bool RemoveAttr( const RString &sName ); XNode() { } diff --git a/stepmania/src/XmlFileUtil.cpp b/stepmania/src/XmlFileUtil.cpp index 1594eeae94..d0d96a2769 100644 --- a/stepmania/src/XmlFileUtil.cpp +++ b/stepmania/src/XmlFileUtil.cpp @@ -150,8 +150,8 @@ unsigned LoadAttributes( XNode *pNode, const RString &xml, RString &sErrorOut, u // add new attribute DEBUG_ASSERT( sName.size() ); - pair it = pNode->m_attrs.insert( make_pair(sName, XNodeValue()) ); - RString &sValue = it.first->second.m_sValue; + pair it = pNode->m_attrs.insert( make_pair(sName, new XNodeValue()) ); + RString &sValue = it.first->second->m_sValue; iOffset = iEnd; // XML Attr Value @@ -423,7 +423,7 @@ bool GetXMLInternal( const XNode *pNode, RageFileBasic &f, bool bWriteTabs, int if( f.Write(" ") == -1 ) return false; FOREACH_CONST_Attr( pNode, p ) - if( !GetAttrXML(f, p->first, p->second.GetValue()) ) + if( !GetAttrXML(f, p->first, p->second->GetValue()) ) return false; if( pNode->m_childs.empty() && pNode->m_Value.m_sValue.empty() )