Files
itgmania212121/stepmania/src/TitleSubstitution.cpp
T

166 lines
4.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 "FontCharAliases.h"
2003-02-11 05:43:03 +00:00
#include <fstream>
2003-02-10 22:59:24 +00:00
struct TitleTrans
{
Regex TitleFrom, SubFrom, ArtistFrom;
CString TitleTo, SubTo, ArtistTo; /* plain text */
bool translit;
2003-02-10 22:59:24 +00:00
TitleTrans(CString tf, CString sf, CString af, CString tt, CString st, CString at,
bool translit_):
2003-02-10 22:59:24 +00:00
TitleFrom(tf), SubFrom(sf), ArtistFrom(af),
TitleTo(tt), SubTo(st), ArtistTo(at), translit(translit_) { }
2003-02-10 22:59:24 +00:00
bool Matches(CString title, CString sub, CString artist);
};
vector<TitleTrans> ttab;
bool TitleTrans::Matches(CString title, CString sub, CString artist)
{
if(!TitleFrom.Compare(title)) return false; /* no match */
if(!SubFrom.Compare(sub)) return false; /* no match */
if(!ArtistFrom.Compare(artist)) return false; /* no match */
return true;
}
void TitleSubst::AddTrans(const CString &tf, const CString &sf, const CString &af,
const CString &tt, const CString &st, const CString &at,
bool translit)
2003-02-10 22:59:24 +00:00
{
ttab.push_back(new TitleTrans(tf, sf, af, tt, st, at, translit));
2003-02-10 22:59:24 +00:00
}
void TitleSubst::Subst(CString &title, CString &subtitle, CString &artist,
CString &ttitle, CString &tsubtitle, CString &tartist)
{
for(unsigned i = 0; i < ttab.size(); ++i)
{
if(!ttab[i]->Matches(title, subtitle, artist))
continue;
/* The song matches. Replace whichever strings aren't empty.
* Don't replace any that already have a transliteration set. */
if(!ttab[i]->TitleTo.empty() && ttitle.empty())
2003-02-10 22:59:24 +00:00
{
if(ttab[i]->translit) ttitle = title;
2003-02-10 22:59:24 +00:00
title = ttab[i]->TitleTo;
FontCharAliases::ReplaceMarkers( title );
}
if(!ttab[i]->SubTo.empty() && tsubtitle.empty())
2003-02-10 22:59:24 +00:00
{
if(ttab[i]->translit) tsubtitle = subtitle;
2003-02-10 22:59:24 +00:00
subtitle = ttab[i]->SubTo;
FontCharAliases::ReplaceMarkers( subtitle );
}
if(!ttab[i]->ArtistTo.empty() && tartist.empty())
2003-02-10 22:59:24 +00:00
{
if(ttab[i]->translit) tartist = artist;
2003-02-10 22:59:24 +00:00
artist = ttab[i]->ArtistTo;
FontCharAliases::ReplaceMarkers( artist );
}
}
}
TitleSubst::TitleSubst(const CString &section)
2003-02-10 22:59:24 +00:00
{
Load("Translation.dat", 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
{
ifstream f;
f.open(filename);
if(!f.good()) return;
CString TitleFrom, ArtistFrom, SubtitleFrom, TitleTo, ArtistTo, SubtitleTo;
bool translit = true;
CString CurrentSection;
2003-02-11 05:43:03 +00:00
while(!f.eof())
{
2003-02-10 22:59:24 +00:00
CString line;
if(!getline(f, line)) continue;
if(line.size() > 0 && utf8_get_char(line.c_str()) == 0xFEFF)
2003-02-11 05:43:03 +00:00
{
/* Annoying header that Windows puts on UTF-8 plaintext
* files; remove it. */
line.erase(0, utf8_get_char_len(line.c_str()));
}
2003-02-10 22:59:24 +00:00
TrimLeft(line);
TrimRight(line);
2003-02-10 22:59:24 +00:00
if(line.size() == 0) continue; /* blank */
if(line[0] == '#') continue; /* comment */
2003-02-10 22:59:24 +00:00
if(!line.CompareNoCase("DontTransliterate"))
{
translit = false;
continue;
}
2003-02-10 22:59:24 +00:00
unsigned pos = line.find_first_of(':');
if(pos != string::npos)
{
2003-02-11 05:43:03 +00:00
/* x: y */
CString id = line.substr(0, pos);
CString txt = line.substr(pos+1);
TrimLeft(txt);
2003-02-10 22:59:24 +00:00
2003-02-11 05:43:03 +00:00
if(!id.CompareNoCase("TitleFrom")) TitleFrom = txt;
else if(!id.CompareNoCase("ArtistFrom")) ArtistFrom = txt;
else if(!id.CompareNoCase("SubtitleFrom")) SubtitleFrom = txt;
else if(!id.CompareNoCase("TitleTo")) TitleTo = txt;
else if(!id.CompareNoCase("ArtistTo")) ArtistTo = txt;
else if(!id.CompareNoCase("SubtitleTo")) SubtitleTo = txt;
}
2003-02-10 22:59:24 +00:00
/* Add the translation if this is a terminator (*) or section
* marker ([xxx]). */
if(line[0] == '*' || line[0] == '[')
{
/* Surround each regex with ^(...)$, to force all comparisons to default
* to being a full-line match. (Add ".*" manually if htis isn't wanted.) */
if(TitleFrom.size()) TitleFrom = "^(" + TitleFrom + ")$";
if(ArtistFrom.size()) ArtistFrom = "^(" + ArtistFrom + ")$";
if(SubtitleFrom.size()) SubtitleFrom = "^(" + SubtitleFrom + ")$";
2003-02-10 22:59:24 +00:00
if(!CurrentSection.CompareNoCase(section) &&
(TitleFrom.size() || SubtitleFrom.size() || ArtistFrom.size()))
AddTrans(TitleFrom, SubtitleFrom, ArtistFrom, TitleTo, SubtitleTo, ArtistTo, translit);
/* Reset. */
TitleFrom = ArtistFrom = SubtitleFrom = TitleTo = ArtistTo = SubtitleTo = "";
translit = true;
}
if(line[0] == '[' && line[line.size()-1] == ']')
{
CurrentSection = line.substr(1, line.size()-2);
}
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
/*
* Copyright (c) 2003 by the person(s) listed below. All rights reserved.
*
* Glenn Maynard
*/