Factor out a bunch of duplicated code in AssStyle::Parse

Originally committed to SVN as r6037.
This commit is contained in:
Thomas Goyne 2011-12-22 21:15:19 +00:00
parent 9eaad5f0b1
commit eaf436657e
1 changed files with 65 additions and 153 deletions

View File

@ -202,123 +202,64 @@ AssStyle::AssStyle(const AssStyle& s)
SetEntryData(s.GetEntryData()); SetEntryData(s.GetEntryData());
} }
AssStyle::AssStyle(wxString _data,int version) { static wxString get_next_string(wxStringTokenizer &tok) {
if (!Parse(_data,version)) if (!tok.HasMoreTokens()) throw "Malformed style: not enough fields";
throw "[Error] Failed parsing line."; return tok.GetNextToken();
UpdateData();
} }
bool AssStyle::Parse(wxString rawData,int version) { static int get_next_int(wxStringTokenizer &tok) {
// Tokenize long temp;
wxString temp; if (!get_next_string(tok).ToLong(&temp))
long templ; throw "Malformed style: could not parse int field";
wxStringTokenizer tkn(rawData.Trim(false).Mid(6),",",wxTOKEN_RET_EMPTY_ALL); return temp;
}
// Read name static int get_next_double(wxStringTokenizer &tok) {
if (!tkn.HasMoreTokens()) return false; double temp;
name = tkn.GetNextToken(); if (!get_next_string(tok).ToDouble(&temp))
name.Trim(true); throw "Malformed style: could not parse double field";
name.Trim(false); return temp;
}
// Read font name AssStyle::AssStyle(wxString rawData,int version) {
if (!tkn.HasMoreTokens()) return false; wxStringTokenizer tkn(rawData.Trim(false).Mid(6), ",", wxTOKEN_RET_EMPTY_ALL);
font = tkn.GetNextToken();
font.Trim(true);
font.Trim(false);
// Read font size name = get_next_string(tkn).Trim(true).Trim(false);
if (!tkn.HasMoreTokens()) return false; font = get_next_string(tkn).Trim(true).Trim(false);
temp = tkn.GetNextToken(); fontsize = get_next_int(tkn);
temp.ToLong(&templ);
fontsize = templ;
if (version != 0) { if (version != 0) {
// Read primary color primary.Parse(get_next_string(tkn));
if (!tkn.HasMoreTokens()) return false; secondary.Parse(get_next_string(tkn));
primary.Parse(tkn.GetNextToken()); outline.Parse(get_next_string(tkn));
shadow.Parse(get_next_string(tkn));
// Read secondary color
if (!tkn.HasMoreTokens()) return false;
secondary.Parse(tkn.GetNextToken());
// Read outline color
if (!tkn.HasMoreTokens()) return false;
outline.Parse(tkn.GetNextToken());
// Read shadow color
if (!tkn.HasMoreTokens()) return false;
shadow.Parse(tkn.GetNextToken());
} }
else { else {
// Read primary color primary.Parse(get_next_string(tkn));
if (!tkn.HasMoreTokens()) return false; secondary.Parse(get_next_string(tkn));
primary.Parse(tkn.GetNextToken());
// Read secondary color
if (!tkn.HasMoreTokens()) return false;
secondary.Parse(tkn.GetNextToken());
// Read and discard tertiary color // Read and discard tertiary color
if (!tkn.HasMoreTokens()) return false; get_next_string(tkn);
tkn.GetNextToken();
// Read shadow/outline color // Read shadow/outline color
if (!tkn.HasMoreTokens()) return false; outline.Parse(get_next_string(tkn));
outline.Parse(tkn.GetNextToken());
shadow = outline; shadow = outline;
} }
// Read bold bold = !!get_next_int(tkn);
if (!tkn.HasMoreTokens()) return false; italic = !!get_next_int(tkn);
temp = tkn.GetNextToken();
temp.ToLong(&templ);
bold = (templ==0)?false:true;
// Read italics
if (!tkn.HasMoreTokens()) return false;
temp = tkn.GetNextToken(); temp.ToLong(&templ);
italic = (templ==0)?false:true;
if (version != 0) { if (version != 0) {
// Read underline underline = !!get_next_int(tkn);
if (!tkn.HasMoreTokens()) return false; strikeout = !!get_next_int(tkn);
temp = tkn.GetNextToken();
temp.ToLong(&templ);
underline = (templ==0)?false:true;
// Read strikeout scalex = get_next_double(tkn);
if (!tkn.HasMoreTokens()) return false; scaley = get_next_double(tkn);
temp = tkn.GetNextToken(); spacing = get_next_double(tkn);
temp.ToLong(&templ); angle = get_next_double(tkn);
strikeout = (templ==0)?false:true;
// Read scale x
if (!tkn.HasMoreTokens()) return false;
temp = tkn.GetNextToken();
temp.ToDouble(&scalex);
//scalex = templ;
// Read scale y
if (!tkn.HasMoreTokens()) return false;
temp = tkn.GetNextToken();
temp.ToDouble(&scaley);
//scaley = templ;
// Read spacing
if (!tkn.HasMoreTokens()) return false;
temp = tkn.GetNextToken();
temp.ToDouble(&spacing);
//spacing = templ;
// Read angle
if (!tkn.HasMoreTokens()) return false;
temp = tkn.GetNextToken();
temp.ToDouble(&angle);
} }
else { else {
// SSA defaults // SSA defaults
//shadow.a = 128; //Parsed
underline = false; underline = false;
strikeout = false; strikeout = false;
@ -328,28 +269,13 @@ bool AssStyle::Parse(wxString rawData,int version) {
angle = 0.0; angle = 0.0;
} }
// Read border style borderstyle = get_next_int(tkn);
if (!tkn.HasMoreTokens()) return false; outline_w = get_next_double(tkn);
temp = tkn.GetNextToken(); shadow_w = get_next_double(tkn);
temp.ToLong(&templ); alignment = get_next_int(tkn);
borderstyle = templ;
// Read outline width
if (!tkn.HasMoreTokens()) return false;
temp = tkn.GetNextToken();
temp.ToDouble(&outline_w);
// Read shadow width
if (!tkn.HasMoreTokens()) return false;
temp = tkn.GetNextToken();
temp.ToDouble(&shadow_w);
// Read alignment
if (!tkn.HasMoreTokens()) return false;
temp = tkn.GetNextToken();
temp.ToLong(&templ);
if (version == 0) { if (version == 0) {
switch(templ) { switch(alignment) {
case 1: alignment = 1; break; case 1: alignment = 1; break;
case 2: alignment = 2; break; case 2: alignment = 2; break;
case 3: alignment = 3; break; case 3: alignment = 3; break;
@ -362,51 +288,37 @@ bool AssStyle::Parse(wxString rawData,int version) {
default: alignment = 2; break; default: alignment = 2; break;
} }
} }
else alignment = templ;
// Read left margin // Read left margin
if (!tkn.HasMoreTokens()) return false; SetMarginString(get_next_string(tkn), 0);
SetMarginString(tkn.GetNextToken(),0);
// Read right margin // Read right margin
if (!tkn.HasMoreTokens()) return false; SetMarginString(get_next_string(tkn), 1);
SetMarginString(tkn.GetNextToken(),1);
// Read top margin // Read top margin
if (!tkn.HasMoreTokens()) return false; SetMarginString(get_next_string(tkn), 2);
temp = tkn.GetNextToken();
SetMarginString(temp,2);
// Read bottom margin // Read bottom margin
if (version == 2) { if (version == 2)
if (!tkn.HasMoreTokens()) return false; SetMarginString(get_next_string(tkn),3);
SetMarginString(tkn.GetNextToken(),3); else
} SetMarginString(GetMarginString(2), 3);
else SetMarginString(temp,3);
// Read alpha level // Skip alpha level
if (version == 0) { if (version == 0)
if (!tkn.HasMoreTokens()) return false; get_next_string(tkn);
temp = tkn.GetNextToken();
}
// Read encoding // Read encoding
if (!tkn.HasMoreTokens()) return false; encoding = get_next_int(tkn);
temp = tkn.GetNextToken();
temp.ToLong(&templ);
encoding = templ;
// Read relative to // Read relative to
if (version == 2) { if (version == 2)
if (!tkn.HasMoreTokens()) return false; relativeTo = get_next_int(tkn);
temp = tkn.GetNextToken();
temp.ToLong(&templ);
relativeTo = templ;
}
// End if (tkn.HasMoreTokens())
if (tkn.HasMoreTokens()) return false; throw "Malformed style: too many fields";
return true;
UpdateData();
} }
void AssStyle::UpdateData() { void AssStyle::UpdateData() {