Files
itgmania212121/stepmania/src/TitleSubstitution.cpp
T

191 lines
6.3 KiB
C++
Raw Normal View History

2003-02-16 04:01:45 +00:00
#include "global.h"
2003-02-10 22:59:24 +00:00
#include "TitleSubstitution.h"
#include "RageUtil.h"
#include "RageLog.h"
2003-02-10 22:59:24 +00:00
#include "FontCharAliases.h"
2003-07-22 07:47:27 +00:00
#include "RageFile.h"
2005-10-06 07:01:58 +00:00
#include "Foreach.h"
#include "XmlFile.h"
2003-07-22 07:47:27 +00:00
2005-10-06 07:01:58 +00:00
static const CString TRANSLATIONS_PATH = "Data/Translations.xml";
static const CString ERASE_MARKER = "-erase-";
2003-02-11 05:43:03 +00:00
2003-02-10 22:59:24 +00:00
struct TitleTrans
{
Regex TitleFrom, SubFrom, ArtistFrom;
2005-10-06 07:01:58 +00:00
TitleFields Replacement;
2003-03-27 20:15:33 +00:00
/* If this is true, no translit fields will be generated automatically. */
bool translit;
2003-03-27 20:15:33 +00:00
TitleTrans() { translit = true; }
2003-02-10 22:59:24 +00:00
2004-05-31 00:59:33 +00:00
TitleTrans( const TitleFields &tf, bool translit_):
2005-10-06 07:01:58 +00:00
Replacement(tf), translit(translit_) { }
2003-02-10 22:59:24 +00:00
2005-10-06 07:01:58 +00:00
bool Matches( const TitleFields &tf, TitleFields &to );
void LoadFromNode( const XNode* pNode );
2003-02-10 22:59:24 +00:00
};
vector<TitleTrans> ttab;
2005-10-06 07:01:58 +00:00
bool TitleTrans::Matches( const TitleFields &from, TitleFields &to )
2003-02-10 22:59:24 +00:00
{
2005-10-06 07:01:58 +00:00
if( !TitleFrom.Replace(Replacement.Title, from.Title, to.Title) )
2004-05-31 00:59:33 +00:00
return false; /* no match */
2005-10-06 07:01:58 +00:00
if( !SubFrom.Replace(Replacement.Subtitle, from.Subtitle, to.Subtitle) )
2004-05-31 00:59:33 +00:00
return false; /* no match */
2005-10-06 07:01:58 +00:00
if( !ArtistFrom.Replace(Replacement.Artist, from.Artist, to.Artist) )
2004-05-31 00:59:33 +00:00
return false; /* no match */
2003-02-10 22:59:24 +00:00
return true;
}
2005-10-06 07:01:58 +00:00
void TitleTrans::LoadFromNode( const XNode* pNode )
{
ASSERT( pNode->m_sName == "Translation" );
FOREACH_CONST_Attr( pNode, attr )
{
/* Surround each regex with ^(...)$, to force all comparisons to default
* to being a full-line match. (Add ".*" manually if this isn't wanted.) */
const CString &sKeyName = attr->first;
const CString &sValue = attr->second;
2005-10-11 10:08:54 +00:00
if( sKeyName == "DontTransliterate" ) translit = false;
else if( sKeyName == "TitleFrom" ) TitleFrom = "^(" + sValue + ")$";
else if( sKeyName == "ArtistFrom" ) ArtistFrom = "^(" + sValue + ")$";
2005-11-06 12:12:18 +00:00
else if( sKeyName == "SubtitleFrom") SubFrom = "^(" + sValue + ")$";
2005-10-11 10:08:54 +00:00
else if( sKeyName == "TitleTo") Replacement.Title = sValue;
else if( sKeyName == "ArtistTo") Replacement.Artist = sValue;
2005-11-06 12:12:18 +00:00
else if( sKeyName == "SubtitleTo") Replacement.Subtitle = sValue;
2005-10-11 10:08:54 +00:00
else if( sKeyName == "TitleTransTo") Replacement.TitleTranslit = sValue;
else if( sKeyName == "ArtistTransTo") Replacement.ArtistTranslit = sValue;
2005-11-06 12:12:18 +00:00
else if( sKeyName == "SubtitleTransTo") Replacement.SubtitleTranslit= sValue;
2005-10-06 07:01:58 +00:00
else
2005-10-11 10:08:54 +00:00
LOG->Warn( "Unknown TitleSubst tag: \"%s\"", sKeyName.c_str() );
2005-10-06 07:01:58 +00:00
}
}
2003-03-27 20:15:33 +00:00
void TitleSubst::AddTrans(const TitleTrans &tr)
2003-02-10 22:59:24 +00:00
{
2005-10-06 07:01:58 +00:00
ASSERT( tr.TitleFrom.IsSet() || tr.SubFrom.IsSet() || tr.ArtistFrom.IsSet() );
2003-03-27 20:15:33 +00:00
ttab.push_back(new TitleTrans(tr));
2003-02-10 22:59:24 +00:00
}
2004-05-31 00:59:33 +00:00
void TitleSubst::Subst( TitleFields &tf )
2003-02-10 22:59:24 +00:00
{
2005-10-06 07:01:58 +00:00
FOREACH_CONST( TitleTrans*, ttab, iter )
2003-02-10 22:59:24 +00:00
{
2005-10-06 07:01:58 +00:00
TitleTrans* tt = *iter;
TitleFields to;
if(!tt->Matches(tf,to))
2003-02-10 22:59:24 +00:00
continue;
2003-03-27 20:15:33 +00:00
/* The song matches. Replace whichever strings aren't empty. */
2005-10-06 07:01:58 +00:00
if( !tt->Replacement.Title.empty() && tf.Title != tt->Replacement.Title )
2003-02-10 22:59:24 +00:00
{
2005-10-06 07:01:58 +00:00
if( tt->translit )
2004-05-31 00:59:33 +00:00
tf.TitleTranslit = tf.Title;
2005-10-06 07:01:58 +00:00
tf.Title = (tt->Replacement.Title != ERASE_MARKER)? to.Title : CString();
2004-05-31 00:59:33 +00:00
FontCharAliases::ReplaceMarkers( tf.Title );
2003-02-10 22:59:24 +00:00
}
2005-10-06 07:01:58 +00:00
if( !tt->Replacement.Subtitle.empty() && tf.Subtitle != tt->Replacement.Subtitle )
2003-02-10 22:59:24 +00:00
{
2005-10-06 07:01:58 +00:00
if( tt->translit )
2004-05-31 00:59:33 +00:00
tf.SubtitleTranslit = tf.Subtitle;
2005-10-06 07:01:58 +00:00
tf.Subtitle = (tt->Replacement.Subtitle != ERASE_MARKER)? to.Subtitle : CString();
2004-05-31 00:59:33 +00:00
FontCharAliases::ReplaceMarkers( tf.Subtitle );
2003-02-10 22:59:24 +00:00
}
2005-10-06 07:01:58 +00:00
if( !tt->Replacement.Artist.empty() && tf.Artist != tt->Replacement.Artist )
2003-02-10 22:59:24 +00:00
{
2005-10-06 07:01:58 +00:00
if( tt->translit )
2004-05-31 00:59:33 +00:00
tf.ArtistTranslit = tf.Artist;
2005-10-06 07:01:58 +00:00
tf.Artist = (tt->Replacement.Artist != ERASE_MARKER)? to.Artist : CString();
2004-05-31 00:59:33 +00:00
FontCharAliases::ReplaceMarkers( tf.Artist );
2003-02-10 22:59:24 +00:00
}
2003-06-23 03:57:59 +00:00
/* These are used when applying kanji to a field that doesn't have the
* correct data. Should be used sparingly. */
2005-10-06 07:01:58 +00:00
if( !tt->Replacement.TitleTranslit.empty() )
2003-06-23 03:57:59 +00:00
{
2005-10-06 07:01:58 +00:00
tf.TitleTranslit = (tt->Replacement.TitleTranslit != ERASE_MARKER)? tt->Replacement.TitleTranslit : CString();
2004-05-31 00:59:33 +00:00
FontCharAliases::ReplaceMarkers( tf.TitleTranslit );
2003-06-23 03:57:59 +00:00
}
2005-10-06 07:01:58 +00:00
if( !tt->Replacement.SubtitleTranslit.empty() )
2003-06-23 03:57:59 +00:00
{
2005-10-06 07:01:58 +00:00
tf.SubtitleTranslit = (tt->Replacement.SubtitleTranslit != ERASE_MARKER)? tt->Replacement.SubtitleTranslit : CString();
2004-05-31 00:59:33 +00:00
FontCharAliases::ReplaceMarkers( tf.SubtitleTranslit );
2003-06-23 03:57:59 +00:00
}
2005-10-06 07:01:58 +00:00
if( !tt->Replacement.ArtistTranslit.empty() )
2003-06-23 03:57:59 +00:00
{
2005-10-06 07:01:58 +00:00
tf.ArtistTranslit = (tt->Replacement.ArtistTranslit != ERASE_MARKER)? tt->Replacement.ArtistTranslit : CString();
2004-05-31 00:59:33 +00:00
FontCharAliases::ReplaceMarkers( tf.ArtistTranslit );
2003-06-23 03:57:59 +00:00
}
2005-10-06 07:01:58 +00:00
break; // Matched once. Done.
2003-02-10 22:59:24 +00:00
}
}
TitleSubst::TitleSubst(const CString &section)
2003-02-10 22:59:24 +00:00
{
2005-10-06 07:01:58 +00:00
Load( TRANSLATIONS_PATH, section);
2003-02-11 05:43:03 +00:00
}
2003-02-10 22:59:24 +00:00
void TitleSubst::Load(const CString &filename, const CString &section)
2003-02-11 05:43:03 +00:00
{
2005-10-06 07:01:58 +00:00
XNode xml;
if( !xml.LoadFromFile(filename) )
2003-12-13 11:45:26 +00:00
{
2005-10-06 07:01:58 +00:00
// LoadFromFile will show its own error
//LOG->Trace("Error opening %s: %s", filename.c_str(), f.GetError().c_str() );
return;
2003-12-13 11:45:26 +00:00
}
2005-10-06 07:01:58 +00:00
XNode *pGroup = xml.GetChild( section );
if( pGroup == NULL )
return;
FOREACH_Child( pGroup, child )
2003-02-11 05:43:03 +00:00
{
2005-10-06 07:01:58 +00:00
if( child->m_sName != "Translation" )
continue;
2003-02-10 22:59:24 +00:00
2005-10-06 07:01:58 +00:00
TitleTrans tr;
tr.LoadFromNode( child );
AddTrans(tr);
2003-02-11 05:43:03 +00:00
}
2003-02-10 22:59:24 +00:00
}
TitleSubst::~TitleSubst()
{
for(unsigned i = 0; i < ttab.size(); ++i)
delete ttab[i];
}
2003-02-12 01:54:17 +00:00
/*
2004-05-31 00:59:33 +00:00
* (c) 2003-2004 Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
2003-02-12 01:54:17 +00:00
*/