webvttparser: added LineReader class

Previously the Parser class had an internal function to parse
the character stream into separate lines.  This functionality
was separated out into its own class, LineReader, in order
to make this functionality available to clients too.

Change-Id: Ic5a1b0b73d7a253cf21cb6b4804b4941fd69c8ab
This commit is contained in:
Matthew Heaney
2012-09-28 15:32:40 -07:00
parent 4f494f6dd4
commit 49078292b4
2 changed files with 224 additions and 214 deletions

View File

@@ -19,14 +19,26 @@ class Reader {
// Fetch a character from the stream. Return
// negative if error, positive if end-of-stream,
// and 0 if a character is available.
//
virtual int GetChar(char* c) = 0;
protected:
Reader();
virtual ~Reader();
};
class LineReader : protected Reader {
public:
// Consume a line of text from the stream, stripping off
// the line terminator characters. Returns negative if error,
// 0 on success, and positive at end-of-stream.
int GetLine(std::string* line);
protected:
virtual ~LineReader();
// Puts a character back into the stream.
virtual void UngetChar(char c) = 0;
};
// As measured in thousandths of a second,
// e.g. a duration of 1 equals 0.001 seconds,
// and a duration of 1000 equals 1 second.
@@ -72,9 +84,10 @@ struct Cue {
payload_t payload;
};
class Parser {
class Parser : private LineReader {
public:
explicit Parser(Reader* r);
virtual ~Parser();
// Pre-parse enough of the stream to determine whether
// this is really a WEBVTT file. Returns 0 on success,
@@ -88,22 +101,16 @@ class Parser {
private:
// Returns the next character in the stream, using the look-back character
// if present.
int GetChar(char* c);
// if present (as per Reader::GetChar).
virtual int GetChar(char* c);
// Puts a character back into the stream.
void UngetChar(char c);
// Puts a character back into the stream (as per LineReader::UngetChar).
virtual void UngetChar(char c);
// Check for presence of a UTF-8 BOM in the stream. Returns
// negative if error, 0 on success, and positive at end-of-stream.
int ParseBOM();
// Consume a line of text from the stream, stripping off
// the line terminator characters. Returns negative if error,
// 0 on success, and positive at end-of-stream.
//
int ParseLine(std::string* line);
// Parse the distinguished "cue timings" line, which includes the start
// and stop times and settings. Argument |line| contains the complete
// line of text (as returned by ParseLine()), which the function is free