mirror of https://github.com/odrling/Aegisub
Use an enum for the SRT parser states
This commit is contained in:
parent
c1ed667025
commit
c7c270cf12
|
@ -375,6 +375,14 @@ wxArrayString SRTSubtitleFormat::GetWriteWildcards() const {
|
||||||
return GetReadWildcards();
|
return GetReadWildcards();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum ParseState {
|
||||||
|
STATE_INITIAL,
|
||||||
|
STATE_TIMESTAMP,
|
||||||
|
STATE_FIRST_LINE_OF_BODY,
|
||||||
|
STATE_REST_OF_BODY,
|
||||||
|
STATE_LAST_WAS_BLANK
|
||||||
|
};
|
||||||
|
|
||||||
void SRTSubtitleFormat::ReadFile(AssFile *target, wxString const& filename, wxString const& encoding) const {
|
void SRTSubtitleFormat::ReadFile(AssFile *target, wxString const& filename, wxString const& encoding) const {
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
@ -390,7 +398,7 @@ void SRTSubtitleFormat::ReadFile(AssFile *target, wxString const& filename, wxSt
|
||||||
|
|
||||||
SrtTagParser tag_parser;
|
SrtTagParser tag_parser;
|
||||||
|
|
||||||
int state = 1;
|
ParseState state = STATE_INITIAL;
|
||||||
int line_num = 0;
|
int line_num = 0;
|
||||||
int linebreak_debt = 0;
|
int linebreak_debt = 0;
|
||||||
AssDialogue *line = 0;
|
AssDialogue *line = 0;
|
||||||
|
@ -400,24 +408,20 @@ void SRTSubtitleFormat::ReadFile(AssFile *target, wxString const& filename, wxSt
|
||||||
text_line.Trim(true).Trim(false);
|
text_line.Trim(true).Trim(false);
|
||||||
|
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case 1:
|
case STATE_INITIAL:
|
||||||
// start of file, no subtitles found yet
|
// ignore leading blank lines
|
||||||
if (text_line.empty())
|
if (text_line.empty()) break;
|
||||||
// ignore blank lines
|
|
||||||
break;
|
|
||||||
if (text_line.IsNumber()) {
|
if (text_line.IsNumber()) {
|
||||||
// found the line number, throw it away and hope for timestamps
|
// found the line number, throw it away and hope for timestamps
|
||||||
state = 2;
|
state = STATE_TIMESTAMP;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (timestamp_regex.Matches(text_line))
|
if (timestamp_regex.Matches(text_line))
|
||||||
goto found_timestamps;
|
goto found_timestamps;
|
||||||
|
|
||||||
throw SRTParseError(STD_STR(wxString::Format("Parsing SRT: Expected subtitle index at line %d", line_num)), 0);
|
throw SRTParseError(STD_STR(wxString::Format("Parsing SRT: Expected subtitle index at line %d", line_num)), 0);
|
||||||
case 2:
|
case STATE_TIMESTAMP:
|
||||||
// want timestamps
|
if (!timestamp_regex.Matches(text_line))
|
||||||
if (timestamp_regex.Matches(text_line) == false)
|
|
||||||
// bad format
|
|
||||||
throw SRTParseError(STD_STR(wxString::Format("Parsing SRT: Expected timestamp pair at line %d", line_num)), 0);
|
throw SRTParseError(STD_STR(wxString::Format("Parsing SRT: Expected timestamp pair at line %d", line_num)), 0);
|
||||||
found_timestamps:
|
found_timestamps:
|
||||||
if (line) {
|
if (line) {
|
||||||
|
@ -432,41 +436,37 @@ found_timestamps:
|
||||||
// store pointer to subtitle, we'll continue working on it
|
// store pointer to subtitle, we'll continue working on it
|
||||||
target->Line.push_back(*line);
|
target->Line.push_back(*line);
|
||||||
// next we're reading the text
|
// next we're reading the text
|
||||||
state = 3;
|
state = STATE_FIRST_LINE_OF_BODY;
|
||||||
break;
|
break;
|
||||||
case 3:
|
case STATE_FIRST_LINE_OF_BODY:
|
||||||
// reading first line of subtitle text
|
|
||||||
if (text_line.empty()) {
|
if (text_line.empty()) {
|
||||||
// that's not very interesting... blank subtitle?
|
// that's not very interesting... blank subtitle?
|
||||||
state = 5;
|
state = STATE_LAST_WAS_BLANK;
|
||||||
// no previous line that needs a line break after
|
// no previous line that needs a line break after
|
||||||
linebreak_debt = 0;
|
linebreak_debt = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
line->Text.Append(text_line);
|
line->Text.Append(text_line);
|
||||||
state = 4;
|
state = STATE_REST_OF_BODY;
|
||||||
break;
|
break;
|
||||||
case 4:
|
case STATE_REST_OF_BODY:
|
||||||
// reading following line of subtitle text
|
|
||||||
if (text_line.empty()) {
|
if (text_line.empty()) {
|
||||||
// blank line, next may begin a new subtitle
|
// Might be either the gap between two subtitles or just a
|
||||||
state = 5;
|
// blank line in the middle of a subtitle, so defer adding
|
||||||
// previous line needs a line break after
|
// the line break until we check what's on the next line
|
||||||
|
state = STATE_LAST_WAS_BLANK;
|
||||||
linebreak_debt = 1;
|
linebreak_debt = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
line->Text.Append("\\N").Append(text_line);
|
line->Text.Append("\\N").Append(text_line);
|
||||||
break;
|
break;
|
||||||
case 5:
|
case STATE_LAST_WAS_BLANK:
|
||||||
// blank line in subtitle text
|
++linebreak_debt;
|
||||||
linebreak_debt++;
|
if (text_line.empty()) break;
|
||||||
if (text_line.empty())
|
|
||||||
// multiple blank lines in a row, just add a line break...
|
|
||||||
break;
|
|
||||||
if (text_line.IsNumber()) {
|
if (text_line.IsNumber()) {
|
||||||
// must be a subtitle index!
|
// Hopefully it's the start of a new subtitle, and the
|
||||||
// go for timestamps next
|
// previous blank line(s) were the gap between subtitles
|
||||||
state = 2;
|
state = STATE_TIMESTAMP;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (timestamp_regex.Matches(text_line))
|
if (timestamp_regex.Matches(text_line))
|
||||||
|
@ -477,10 +477,8 @@ found_timestamps:
|
||||||
while (linebreak_debt-- > 0)
|
while (linebreak_debt-- > 0)
|
||||||
line->Text.Append("\\N");
|
line->Text.Append("\\N");
|
||||||
line->Text.Append(text_line);
|
line->Text.Append(text_line);
|
||||||
state = 4;
|
state = STATE_REST_OF_BODY;
|
||||||
break;
|
break;
|
||||||
default:
|
|
||||||
throw agi::InternalError(STD_STR(wxString::Format("Parsing SRT: Reached unexpected state %d", state)), 0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue