2012-07-26 00:15:33 +02:00
|
|
|
// Copyright (c) 2012 The WebM project authors. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style license
|
|
|
|
// that can be found in the LICENSE file in the root of the source
|
|
|
|
// tree. An additional intellectual property rights grant can be found
|
|
|
|
// in the file PATENTS. All contributing project authors may
|
|
|
|
// be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
2016-03-09 23:10:22 +01:00
|
|
|
#include "webvttparser.h"
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2012-07-26 00:15:33 +02:00
|
|
|
#include <climits>
|
2016-03-09 23:10:22 +01:00
|
|
|
#include <cstddef>
|
2012-07-26 00:15:33 +02:00
|
|
|
|
|
|
|
namespace libwebvtt {
|
|
|
|
|
2014-04-14 21:12:35 +02:00
|
|
|
// NOLINT'ing this enum because clang-format puts it in a single line which
|
|
|
|
// makes it look really unreadable.
|
2012-07-26 00:15:33 +02:00
|
|
|
enum {
|
|
|
|
kNUL = '\x00',
|
|
|
|
kSPACE = ' ',
|
|
|
|
kTAB = '\x09',
|
|
|
|
kLF = '\x0A',
|
|
|
|
kCR = '\x0D'
|
2014-04-14 21:12:35 +02:00
|
|
|
}; // NOLINT
|
2012-07-26 00:15:33 +02:00
|
|
|
|
2014-04-14 21:12:35 +02:00
|
|
|
Reader::~Reader() {}
|
2012-07-26 00:15:33 +02:00
|
|
|
|
2014-04-14 21:12:35 +02:00
|
|
|
LineReader::~LineReader() {}
|
2012-09-29 00:32:40 +02:00
|
|
|
|
2013-06-12 20:23:35 +02:00
|
|
|
int LineReader::GetLine(std::string* line_ptr) {
|
2012-09-29 00:32:40 +02:00
|
|
|
if (line_ptr == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2013-06-12 20:23:35 +02:00
|
|
|
std::string& ln = *line_ptr;
|
2012-09-29 00:32:40 +02:00
|
|
|
ln.clear();
|
|
|
|
|
|
|
|
// Consume characters from the stream, until we
|
|
|
|
// reach end-of-line (or end-of-stream).
|
|
|
|
|
|
|
|
// The WebVTT spec states that lines may be
|
|
|
|
// terminated in any of these three ways:
|
|
|
|
// LF
|
|
|
|
// CR
|
|
|
|
// CR LF
|
|
|
|
|
|
|
|
// We interrogate each character as we read it from the stream.
|
|
|
|
// If we detect an end-of-line character, we consume the full
|
|
|
|
// end-of-line indication, and we're done; otherwise, accumulate
|
|
|
|
// the character and repeat.
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
char c;
|
|
|
|
const int e = GetChar(&c);
|
|
|
|
|
|
|
|
if (e < 0) // error
|
|
|
|
return e;
|
|
|
|
|
|
|
|
if (e > 0) // EOF
|
|
|
|
return (ln.empty()) ? 1 : 0;
|
|
|
|
|
|
|
|
// We have a character, so we must first determine
|
|
|
|
// whether we have reached end-of-line.
|
|
|
|
|
|
|
|
if (c == kLF)
|
|
|
|
return 0; // handle the easy end-of-line case immediately
|
|
|
|
|
|
|
|
if (c == kCR)
|
|
|
|
break; // handle the hard end-of-line case outside of loop
|
|
|
|
|
2012-10-16 02:10:22 +02:00
|
|
|
if (c == '\xFE' || c == '\xFF') // not UTF-8
|
|
|
|
return -1;
|
|
|
|
|
2012-09-29 00:32:40 +02:00
|
|
|
// To defend against pathological or malicious streams, we
|
|
|
|
// cap the line length at some arbitrarily-large value:
|
|
|
|
enum { kMaxLineLength = 10000 }; // arbitrary
|
|
|
|
|
|
|
|
if (ln.length() >= kMaxLineLength)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// We don't have an end-of-line character, so accumulate
|
|
|
|
// the character in our line buffer.
|
|
|
|
ln.push_back(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We detected a CR. We must interrogate the next character
|
|
|
|
// in the stream, to determine whether we have a LF (which
|
|
|
|
// would make it part of this same line).
|
|
|
|
|
|
|
|
char c;
|
|
|
|
const int e = GetChar(&c);
|
|
|
|
|
|
|
|
if (e < 0) // error
|
|
|
|
return e;
|
|
|
|
|
|
|
|
if (e > 0) // EOF
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// If next character in the stream is not a LF, return it
|
|
|
|
// to the stream (because it's part of the next line).
|
|
|
|
if (c != kLF)
|
|
|
|
UngetChar(c);
|
|
|
|
|
|
|
|
return 0;
|
2012-07-26 00:15:33 +02:00
|
|
|
}
|
|
|
|
|
2014-04-14 21:12:35 +02:00
|
|
|
Parser::Parser(Reader* r) : reader_(r), unget_(-1) {}
|
2012-07-26 00:15:33 +02:00
|
|
|
|
2014-04-14 21:12:35 +02:00
|
|
|
Parser::~Parser() {}
|
2012-09-29 00:32:40 +02:00
|
|
|
|
2012-07-26 00:15:33 +02:00
|
|
|
int Parser::Init() {
|
|
|
|
int e = ParseBOM();
|
|
|
|
|
|
|
|
if (e < 0) // error
|
|
|
|
return e;
|
|
|
|
|
|
|
|
if (e > 0) // EOF
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Parse "WEBVTT". We read from the stream one character at-a-time, in
|
|
|
|
// order to defend against non-WebVTT streams (e.g. binary files) that don't
|
|
|
|
// happen to comprise lines of text demarcated with line terminators.
|
|
|
|
|
2012-08-24 01:16:30 +02:00
|
|
|
const char kId[] = "WEBVTT";
|
2012-07-26 00:15:33 +02:00
|
|
|
|
2012-08-24 01:16:30 +02:00
|
|
|
for (const char* p = kId; *p; ++p) {
|
2012-07-26 00:15:33 +02:00
|
|
|
char c;
|
|
|
|
e = GetChar(&c);
|
|
|
|
|
|
|
|
if (e < 0) // error
|
|
|
|
return e;
|
|
|
|
|
|
|
|
if (e > 0) // EOF
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (c != *p)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-06-12 20:23:35 +02:00
|
|
|
std::string line;
|
2012-07-26 00:15:33 +02:00
|
|
|
|
2012-09-29 00:32:40 +02:00
|
|
|
e = GetLine(&line);
|
2012-07-26 00:15:33 +02:00
|
|
|
|
|
|
|
if (e < 0) // error
|
|
|
|
return e;
|
|
|
|
|
2014-04-29 09:35:56 +02:00
|
|
|
if (e > 0) // EOF
|
2012-07-26 00:15:33 +02:00
|
|
|
return 0; // weird but valid
|
|
|
|
|
|
|
|
if (!line.empty()) {
|
|
|
|
// Parse optional characters that follow "WEBVTT"
|
|
|
|
|
|
|
|
const char c = line[0];
|
|
|
|
|
|
|
|
if (c != kSPACE && c != kTAB)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The WebVTT spec requires that the "WEBVTT" line
|
|
|
|
// be followed by an empty line (to separate it from
|
|
|
|
// first cue).
|
|
|
|
|
2012-09-29 00:32:40 +02:00
|
|
|
e = GetLine(&line);
|
2012-07-26 00:15:33 +02:00
|
|
|
|
|
|
|
if (e < 0) // error
|
|
|
|
return e;
|
|
|
|
|
2014-04-29 09:35:56 +02:00
|
|
|
if (e > 0) // EOF
|
2012-07-26 00:15:33 +02:00
|
|
|
return 0; // weird but we allow it
|
|
|
|
|
|
|
|
if (!line.empty())
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0; // success
|
|
|
|
}
|
|
|
|
|
|
|
|
int Parser::Parse(Cue* cue) {
|
|
|
|
if (cue == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Parse first non-blank line
|
|
|
|
|
2013-06-12 20:23:35 +02:00
|
|
|
std::string line;
|
2012-07-26 00:15:33 +02:00
|
|
|
int e;
|
|
|
|
|
|
|
|
for (;;) {
|
2012-09-29 00:32:40 +02:00
|
|
|
e = GetLine(&line);
|
2012-07-26 00:15:33 +02:00
|
|
|
|
2012-08-24 01:16:30 +02:00
|
|
|
if (e) // EOF is OK here
|
2012-07-26 00:15:33 +02:00
|
|
|
return e;
|
|
|
|
|
|
|
|
if (!line.empty())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A WebVTT cue comprises an optional cue identifier line followed
|
|
|
|
// by a (non-optional) timings line. You determine whether you have
|
|
|
|
// a timings line by scanning for the arrow token, the lexeme of which
|
|
|
|
// may not appear in the cue identifier line.
|
|
|
|
|
2012-08-24 01:16:30 +02:00
|
|
|
const char kArrow[] = "-->";
|
2013-06-12 20:23:35 +02:00
|
|
|
std::string::size_type arrow_pos = line.find(kArrow);
|
2012-08-24 01:16:30 +02:00
|
|
|
|
2013-06-12 20:23:35 +02:00
|
|
|
if (arrow_pos != std::string::npos) {
|
2012-08-24 01:16:30 +02:00
|
|
|
// We found a timings line, which implies that we don't have a cue
|
|
|
|
// identifier.
|
2012-07-26 00:15:33 +02:00
|
|
|
|
|
|
|
cue->identifier.clear();
|
|
|
|
} else {
|
2012-08-24 01:16:30 +02:00
|
|
|
// We did not find a timings line, so we assume that we have a cue
|
|
|
|
// identifier line, and then try again to find the cue timings on
|
|
|
|
// the next line.
|
|
|
|
|
2012-07-26 00:15:33 +02:00
|
|
|
cue->identifier.swap(line);
|
|
|
|
|
2012-09-29 00:32:40 +02:00
|
|
|
e = GetLine(&line);
|
2012-07-26 00:15:33 +02:00
|
|
|
|
2012-08-24 01:16:30 +02:00
|
|
|
if (e < 0) // error
|
2012-07-26 00:15:33 +02:00
|
|
|
return e;
|
|
|
|
|
2012-08-24 01:16:30 +02:00
|
|
|
if (e > 0) // EOF
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
arrow_pos = line.find(kArrow);
|
2012-07-26 00:15:33 +02:00
|
|
|
|
2013-06-12 20:23:35 +02:00
|
|
|
if (arrow_pos == std::string::npos) // not a timings line
|
2012-07-26 00:15:33 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-04-14 21:12:35 +02:00
|
|
|
e = ParseTimingsLine(&line, arrow_pos, &cue->start_time, &cue->stop_time,
|
2012-07-26 00:15:33 +02:00
|
|
|
&cue->settings);
|
|
|
|
|
2012-08-24 01:16:30 +02:00
|
|
|
if (e) // error
|
2012-07-26 00:15:33 +02:00
|
|
|
return e;
|
|
|
|
|
|
|
|
// The cue payload comprises all the non-empty
|
|
|
|
// lines that follow the timings line.
|
|
|
|
|
|
|
|
Cue::payload_t& p = cue->payload;
|
|
|
|
p.clear();
|
|
|
|
|
|
|
|
for (;;) {
|
2012-09-29 00:32:40 +02:00
|
|
|
e = GetLine(&line);
|
2012-07-26 00:15:33 +02:00
|
|
|
|
|
|
|
if (e < 0) // error
|
|
|
|
return e;
|
|
|
|
|
|
|
|
if (line.empty())
|
|
|
|
break;
|
|
|
|
|
|
|
|
p.push_back(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p.empty())
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0; // success
|
|
|
|
}
|
|
|
|
|
|
|
|
int Parser::GetChar(char* c) {
|
|
|
|
if (unget_ >= 0) {
|
|
|
|
*c = static_cast<char>(unget_);
|
|
|
|
unget_ = -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return reader_->GetChar(c);
|
|
|
|
}
|
|
|
|
|
2014-04-14 21:12:35 +02:00
|
|
|
void Parser::UngetChar(char c) { unget_ = static_cast<unsigned char>(c); }
|
2012-07-26 00:15:33 +02:00
|
|
|
|
|
|
|
int Parser::ParseBOM() {
|
|
|
|
// Explanation of UTF-8 BOM:
|
|
|
|
// http://en.wikipedia.org/wiki/Byte_order_mark
|
|
|
|
|
|
|
|
static const char BOM[] = "\xEF\xBB\xBF"; // UTF-8 BOM
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
char c;
|
|
|
|
int e = GetChar(&c);
|
|
|
|
|
|
|
|
if (e < 0) // error
|
|
|
|
return e;
|
|
|
|
|
|
|
|
if (e > 0) // EOF
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (c != BOM[i]) {
|
|
|
|
if (i == 0) { // we don't have a BOM
|
|
|
|
UngetChar(c);
|
|
|
|
return 0; // success
|
|
|
|
}
|
|
|
|
|
|
|
|
// We started a BOM, so we must finish the BOM.
|
|
|
|
return -1; // error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0; // success
|
|
|
|
}
|
|
|
|
|
2014-04-14 21:12:35 +02:00
|
|
|
int Parser::ParseTimingsLine(std::string* line_ptr,
|
|
|
|
std::string::size_type arrow_pos, Time* start_time,
|
|
|
|
Time* stop_time, Cue::settings_t* settings) {
|
2012-08-24 01:16:30 +02:00
|
|
|
if (line_ptr == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2013-06-12 20:23:35 +02:00
|
|
|
std::string& line = *line_ptr;
|
2012-07-26 00:15:33 +02:00
|
|
|
|
2013-06-12 20:23:35 +02:00
|
|
|
if (arrow_pos == std::string::npos || arrow_pos >= line.length())
|
2012-07-26 00:15:33 +02:00
|
|
|
return -1;
|
|
|
|
|
2012-08-24 01:16:30 +02:00
|
|
|
// Place a NUL character at the start of the arrow token, in
|
|
|
|
// order to demarcate the start time from remainder of line.
|
2012-07-26 00:15:33 +02:00
|
|
|
line[arrow_pos] = kNUL;
|
2013-06-12 20:23:35 +02:00
|
|
|
std::string::size_type idx = 0;
|
2012-07-26 00:15:33 +02:00
|
|
|
|
2012-08-24 01:16:30 +02:00
|
|
|
int e = ParseTime(line, &idx, start_time);
|
|
|
|
if (e) // error
|
2012-07-26 00:15:33 +02:00
|
|
|
return e;
|
|
|
|
|
|
|
|
// Detect any junk that follows the start time,
|
|
|
|
// but precedes the arrow symbol.
|
|
|
|
|
|
|
|
while (char c = line[idx]) {
|
|
|
|
if (c != kSPACE && c != kTAB)
|
|
|
|
return -1;
|
|
|
|
++idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Place a NUL character at the end of the line,
|
|
|
|
// so the scanner has a place to stop, and begin
|
|
|
|
// the scan just beyond the arrow token.
|
|
|
|
|
|
|
|
line.push_back(kNUL);
|
|
|
|
idx = arrow_pos + 3;
|
|
|
|
|
2012-08-24 01:16:30 +02:00
|
|
|
e = ParseTime(line, &idx, stop_time);
|
|
|
|
if (e) // error
|
2012-07-26 00:15:33 +02:00
|
|
|
return e;
|
|
|
|
|
|
|
|
e = ParseSettings(line, idx, settings);
|
2012-08-24 01:16:30 +02:00
|
|
|
if (e) // error
|
2012-07-26 00:15:33 +02:00
|
|
|
return e;
|
|
|
|
|
|
|
|
return 0; // success
|
|
|
|
}
|
|
|
|
|
2014-04-14 21:12:35 +02:00
|
|
|
int Parser::ParseTime(const std::string& line, std::string::size_type* idx_ptr,
|
|
|
|
Time* time) {
|
2012-08-24 01:16:30 +02:00
|
|
|
if (idx_ptr == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2013-06-12 20:23:35 +02:00
|
|
|
std::string::size_type& idx = *idx_ptr;
|
2012-07-26 00:15:33 +02:00
|
|
|
|
2013-06-12 20:23:35 +02:00
|
|
|
if (idx == std::string::npos || idx >= line.length())
|
2012-07-26 00:15:33 +02:00
|
|
|
return -1;
|
|
|
|
|
2012-08-24 01:16:30 +02:00
|
|
|
if (time == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2012-07-26 00:15:33 +02:00
|
|
|
// Consume any whitespace that precedes the timestamp.
|
|
|
|
|
|
|
|
while (char c = line[idx]) {
|
|
|
|
if (c != kSPACE && c != kTAB)
|
|
|
|
break;
|
|
|
|
++idx;
|
|
|
|
}
|
|
|
|
|
2012-08-24 01:16:30 +02:00
|
|
|
// WebVTT timestamp syntax comes in three flavors:
|
|
|
|
// SS[.sss]
|
|
|
|
// MM:SS[.sss]
|
|
|
|
// HH:MM:SS[.sss]
|
2012-07-26 00:15:33 +02:00
|
|
|
|
|
|
|
// Parse a generic number value. We don't know which component
|
|
|
|
// of the time we have yet, until we do more parsing.
|
|
|
|
|
2012-08-24 01:16:30 +02:00
|
|
|
int val = ParseNumber(line, &idx);
|
2012-07-26 00:15:33 +02:00
|
|
|
|
|
|
|
if (val < 0) // error
|
|
|
|
return val;
|
|
|
|
|
2012-08-24 01:16:30 +02:00
|
|
|
Time& t = *time;
|
|
|
|
|
2012-07-26 00:15:33 +02:00
|
|
|
// The presence of a colon character indicates that we have
|
|
|
|
// an [HH:]MM:SS style syntax.
|
|
|
|
|
|
|
|
if (line[idx] == ':') {
|
|
|
|
// We have either HH:MM:SS or MM:SS
|
|
|
|
|
|
|
|
// The value we just parsed is either the hours or minutes.
|
|
|
|
// It must be followed by another number value (that is
|
|
|
|
// either minutes or seconds).
|
|
|
|
|
|
|
|
const int first_val = val;
|
|
|
|
|
|
|
|
++idx; // consume colon
|
|
|
|
|
|
|
|
// Parse second value
|
|
|
|
|
2012-08-24 01:16:30 +02:00
|
|
|
val = ParseNumber(line, &idx);
|
2012-07-26 00:15:33 +02:00
|
|
|
|
|
|
|
if (val < 0)
|
|
|
|
return val;
|
|
|
|
|
|
|
|
if (val >= 60) // either MM or SS
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (line[idx] == ':') {
|
|
|
|
// We have HH:MM:SS
|
|
|
|
|
|
|
|
t.hours = first_val;
|
|
|
|
t.minutes = val; // vetted above
|
|
|
|
|
|
|
|
++idx; // consume MM:SS colon
|
|
|
|
|
|
|
|
// We have parsed the hours and minutes.
|
|
|
|
// We must now parse the seconds.
|
|
|
|
|
2012-08-24 01:16:30 +02:00
|
|
|
val = ParseNumber(line, &idx);
|
2012-07-26 00:15:33 +02:00
|
|
|
|
|
|
|
if (val < 0)
|
|
|
|
return val;
|
|
|
|
|
|
|
|
if (val >= 60) // SS part of HH:MM:SS
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
t.seconds = val;
|
|
|
|
} else {
|
|
|
|
// We have MM:SS
|
|
|
|
|
|
|
|
// The implication here is that the hour value was omitted
|
|
|
|
// from the timestamp (because it was 0).
|
|
|
|
|
|
|
|
if (first_val >= 60) // minutes
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
t.hours = 0;
|
|
|
|
t.minutes = first_val;
|
|
|
|
t.seconds = val; // vetted above
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We have SS (only)
|
|
|
|
|
|
|
|
// The time is expressed as total number of seconds,
|
|
|
|
// so the seconds value has no upper bound.
|
|
|
|
|
|
|
|
t.seconds = val;
|
|
|
|
|
|
|
|
// Convert SS to HH:MM:SS
|
|
|
|
|
|
|
|
t.minutes = t.seconds / 60;
|
|
|
|
t.seconds -= t.minutes * 60;
|
|
|
|
|
|
|
|
t.hours = t.minutes / 60;
|
|
|
|
t.minutes -= t.hours * 60;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have parsed the hours, minutes, and seconds.
|
|
|
|
// We must now parse the milliseconds.
|
|
|
|
|
2012-10-16 02:10:22 +02:00
|
|
|
char c = line[idx];
|
|
|
|
|
|
|
|
// TODO(matthewjheaney): one option here is to slightly relax the
|
|
|
|
// syntax rules for WebVTT timestamps, to permit the comma character
|
|
|
|
// to also be used as the seconds/milliseconds separator. This
|
|
|
|
// would handle streams that use localization conventions for
|
|
|
|
// countries in Western Europe. For now we obey the rules specified
|
|
|
|
// in the WebVTT spec (allow "full stop" only).
|
|
|
|
|
|
|
|
const bool have_milliseconds = (c == '.');
|
|
|
|
|
|
|
|
if (!have_milliseconds) {
|
2012-07-26 00:15:33 +02:00
|
|
|
t.milliseconds = 0;
|
|
|
|
} else {
|
|
|
|
++idx; // consume FULL STOP
|
|
|
|
|
2012-08-24 01:16:30 +02:00
|
|
|
val = ParseNumber(line, &idx);
|
2012-07-26 00:15:33 +02:00
|
|
|
|
|
|
|
if (val < 0)
|
|
|
|
return val;
|
|
|
|
|
|
|
|
if (val >= 1000)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (val < 10)
|
|
|
|
t.milliseconds = val * 100;
|
|
|
|
else if (val < 100)
|
|
|
|
t.milliseconds = val * 10;
|
|
|
|
else
|
|
|
|
t.milliseconds = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have parsed the time proper. We must check for any
|
|
|
|
// junk that immediately follows the time specifier.
|
|
|
|
|
2012-10-16 02:10:22 +02:00
|
|
|
c = line[idx];
|
2012-07-26 00:15:33 +02:00
|
|
|
|
|
|
|
if (c != kNUL && c != kSPACE && c != kTAB)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0; // success
|
|
|
|
}
|
|
|
|
|
2014-04-14 21:12:35 +02:00
|
|
|
int Parser::ParseSettings(const std::string& line, std::string::size_type idx,
|
|
|
|
Cue::settings_t* settings) {
|
2012-07-26 00:15:33 +02:00
|
|
|
settings->clear();
|
|
|
|
|
2013-06-12 20:23:35 +02:00
|
|
|
if (idx == std::string::npos || idx >= line.length())
|
2012-07-26 00:15:33 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (;;) {
|
2012-08-24 01:16:30 +02:00
|
|
|
// We must parse a line comprising a sequence of 0 or more
|
|
|
|
// NAME:VALUE pairs, separated by whitespace. The line iself is
|
|
|
|
// terminated with a NUL char (indicating end-of-line).
|
2012-07-26 00:15:33 +02:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
const char c = line[idx];
|
|
|
|
|
2012-08-24 01:16:30 +02:00
|
|
|
if (c == kNUL) // end-of-line
|
2014-04-29 09:35:56 +02:00
|
|
|
return 0; // success
|
2012-07-26 00:15:33 +02:00
|
|
|
|
|
|
|
if (c != kSPACE && c != kTAB)
|
|
|
|
break;
|
|
|
|
|
|
|
|
++idx; // consume whitespace
|
|
|
|
}
|
|
|
|
|
2012-08-24 01:16:30 +02:00
|
|
|
// We have consumed the whitespace, and have not yet reached
|
|
|
|
// end-of-line, so there is something on the line for us to parse.
|
2012-07-26 00:15:33 +02:00
|
|
|
|
|
|
|
settings->push_back(Setting());
|
|
|
|
Setting& s = settings->back();
|
|
|
|
|
|
|
|
// Parse the NAME part of the settings pair.
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
const char c = line[idx];
|
|
|
|
|
|
|
|
if (c == ':') // we have reached end of NAME part
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (c == kNUL || c == kSPACE || c == kTAB)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
s.name.push_back(c);
|
|
|
|
|
|
|
|
++idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s.name.empty())
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
++idx; // consume colon
|
|
|
|
|
|
|
|
// Parse the VALUE part of the settings pair.
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
const char c = line[idx];
|
|
|
|
|
|
|
|
if (c == kNUL || c == kSPACE || c == kTAB)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (c == ':') // suspicious when part of VALUE
|
2014-04-29 09:35:56 +02:00
|
|
|
return -1; // TODO(matthewjheaney): verify this behavior
|
2012-07-26 00:15:33 +02:00
|
|
|
|
|
|
|
s.value.push_back(c);
|
|
|
|
|
|
|
|
++idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s.value.empty())
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-14 21:12:35 +02:00
|
|
|
int Parser::ParseNumber(const std::string& line,
|
|
|
|
std::string::size_type* idx_ptr) {
|
2012-08-24 01:16:30 +02:00
|
|
|
if (idx_ptr == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2013-06-12 20:23:35 +02:00
|
|
|
std::string::size_type& idx = *idx_ptr;
|
2012-08-24 01:16:30 +02:00
|
|
|
|
2013-06-12 20:23:35 +02:00
|
|
|
if (idx == std::string::npos || idx >= line.length())
|
2012-07-26 00:15:33 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!isdigit(line[idx]))
|
|
|
|
return -1;
|
|
|
|
|
2012-10-16 02:10:22 +02:00
|
|
|
int result = 0;
|
2012-07-26 00:15:33 +02:00
|
|
|
|
|
|
|
while (isdigit(line[idx])) {
|
2012-10-16 02:10:22 +02:00
|
|
|
const char c = line[idx];
|
|
|
|
const int i = c - '0';
|
2012-07-26 00:15:33 +02:00
|
|
|
|
2012-10-16 02:10:22 +02:00
|
|
|
if (result > INT_MAX / 10)
|
2012-07-26 00:15:33 +02:00
|
|
|
return -1;
|
|
|
|
|
2012-10-16 02:10:22 +02:00
|
|
|
result *= 10;
|
|
|
|
|
|
|
|
if (result > INT_MAX - i)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
result += i;
|
|
|
|
|
2012-07-26 00:15:33 +02:00
|
|
|
++idx;
|
|
|
|
}
|
|
|
|
|
2012-10-16 02:10:22 +02:00
|
|
|
return result;
|
2012-07-26 00:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Time::operator==(const Time& rhs) const {
|
|
|
|
if (hours != rhs.hours)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (minutes != rhs.minutes)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (seconds != rhs.seconds)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return (milliseconds == rhs.milliseconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Time::operator<(const Time& rhs) const {
|
|
|
|
if (hours < rhs.hours)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (hours > rhs.hours)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (minutes < rhs.minutes)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (minutes > rhs.minutes)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (seconds < rhs.seconds)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (seconds > rhs.seconds)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return (milliseconds < rhs.milliseconds);
|
|
|
|
}
|
|
|
|
|
2014-04-14 21:12:35 +02:00
|
|
|
bool Time::operator>(const Time& rhs) const { return rhs.operator<(*this); }
|
2012-07-26 00:15:33 +02:00
|
|
|
|
2014-04-14 21:12:35 +02:00
|
|
|
bool Time::operator<=(const Time& rhs) const { return !this->operator>(rhs); }
|
2012-07-26 00:15:33 +02:00
|
|
|
|
2014-04-14 21:12:35 +02:00
|
|
|
bool Time::operator>=(const Time& rhs) const { return !this->operator<(rhs); }
|
2012-07-26 00:15:33 +02:00
|
|
|
|
|
|
|
presentation_t Time::presentation() const {
|
|
|
|
const presentation_t h = 1000LL * 3600LL * presentation_t(hours);
|
|
|
|
const presentation_t m = 1000LL * 60LL * presentation_t(minutes);
|
|
|
|
const presentation_t s = 1000LL * presentation_t(seconds);
|
|
|
|
const presentation_t result = h + m + s + milliseconds;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Time& Time::presentation(presentation_t d) {
|
|
|
|
if (d < 0) { // error
|
|
|
|
hours = 0;
|
|
|
|
minutes = 0;
|
|
|
|
seconds = 0;
|
|
|
|
milliseconds = 0;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-01-22 23:03:40 +01:00
|
|
|
seconds = static_cast<int>(d / 1000);
|
|
|
|
milliseconds = static_cast<int>(d - 1000 * seconds);
|
2012-07-26 00:15:33 +02:00
|
|
|
|
|
|
|
minutes = seconds / 60;
|
|
|
|
seconds -= 60 * minutes;
|
|
|
|
|
|
|
|
hours = minutes / 60;
|
|
|
|
minutes -= 60 * hours;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Time& Time::operator+=(presentation_t rhs) {
|
|
|
|
const presentation_t d = this->presentation();
|
|
|
|
const presentation_t dd = d + rhs;
|
|
|
|
this->presentation(dd);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Time Time::operator+(presentation_t d) const {
|
|
|
|
Time t(*this);
|
|
|
|
t += d;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2014-04-14 21:12:35 +02:00
|
|
|
Time& Time::operator-=(presentation_t d) { return this->operator+=(-d); }
|
2012-07-26 00:15:33 +02:00
|
|
|
|
|
|
|
presentation_t Time::operator-(const Time& t) const {
|
|
|
|
const presentation_t rhs = t.presentation();
|
|
|
|
const presentation_t lhs = this->presentation();
|
|
|
|
const presentation_t result = lhs - rhs;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace libwebvtt
|