Files
itgmania212121/src/TitleSubstitution.cpp
T

191 lines
6.4 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"
#include "XmlFileUtil.h"
2003-07-22 07:47:27 +00:00
2006-01-22 01:00:06 +00:00
static const RString TRANSLATIONS_PATH = "Data/Translations.xml";
static const RString 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
};
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 )
{
2006-10-02 06:26:25 +00:00
ASSERT( pNode->GetName() == "Translation" );
2005-10-06 07:01:58 +00:00
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.) */
2006-01-22 01:00:06 +00:00
const RString &sKeyName = attr->first;
2006-10-03 00:07:01 +00:00
const RString sValue = attr->second->GetValue<RString>();
2006-03-19 21:50:21 +00:00
if( sKeyName == "DontTransliterate" ) translit = false;
else if( sKeyName == "TitleFrom" ) TitleFrom = "^(" + sValue + ")$";
else if( sKeyName == "ArtistFrom" ) ArtistFrom = "^(" + sValue + ")$";
else if( sKeyName == "SubtitleFrom") SubFrom = "^(" + sValue + ")$";
else if( sKeyName == "TitleTo") Replacement.Title = sValue;
else if( sKeyName == "ArtistTo") Replacement.Artist = sValue;
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;
2006-03-19 21:50:21 +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;
2006-01-22 01:00:06 +00:00
tf.Title = (tt->Replacement.Title != ERASE_MARKER)? to.Title : RString();
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;
2006-01-22 01:00:06 +00:00
tf.Subtitle = (tt->Replacement.Subtitle != ERASE_MARKER)? to.Subtitle : RString();
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;
2006-01-22 01:00:06 +00:00
tf.Artist = (tt->Replacement.Artist != ERASE_MARKER)? to.Artist : RString();
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
{
2006-01-22 01:00:06 +00:00
tf.TitleTranslit = (tt->Replacement.TitleTranslit != ERASE_MARKER)? tt->Replacement.TitleTranslit : RString();
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
{
2006-01-22 01:00:06 +00:00
tf.SubtitleTranslit = (tt->Replacement.SubtitleTranslit != ERASE_MARKER)? tt->Replacement.SubtitleTranslit : RString();
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
{
2006-01-22 01:00:06 +00:00
tf.ArtistTranslit = (tt->Replacement.ArtistTranslit != ERASE_MARKER)? tt->Replacement.ArtistTranslit : RString();
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
// Matched once. Keep processing to allow multiple matching entries. For example, allow
// one entry to translate a title, and another entry to translate the artist.
2003-02-10 22:59:24 +00:00
}
}
2006-01-22 01:00:06 +00:00
TitleSubst::TitleSubst(const RString &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
2006-01-22 01:00:06 +00:00
void TitleSubst::Load(const RString &filename, const RString &section)
2003-02-11 05:43:03 +00:00
{
2005-10-06 07:01:58 +00:00
XNode xml;
if( !XmlFileUtil::LoadFromFileShowErrors(xml,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;
2007-02-12 01:00:52 +00:00
FOREACH_CONST_Child( pGroup, child )
2003-02-11 05:43:03 +00:00
{
2006-10-02 06:26:25 +00:00
if( child->GetName() != "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
*/