projects
/
libsub.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Fix vertical position referencing of subs in parse_line.
[libsub.git]
/
src
/
subrip_reader.cc
diff --git
a/src/subrip_reader.cc
b/src/subrip_reader.cc
index 134ca3e7ccd08c380df38cc6fe4418e0fbcc9547..6c0c63a26130877f87adfa6c0bb27847af7e3064 100644
(file)
--- a/
src/subrip_reader.cc
+++ b/
src/subrip_reader.cc
@@
-1,5
+1,5
@@
/*
/*
- Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+ Copyright (C) 2014
-2015
Carl Hetherington <cth@carlh.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@
-17,24
+17,48
@@
*/
*/
+/** @file src/subrip_reader.cc
+ * @brief SubripReader class.
+ */
+
#include "subrip_reader.h"
#include "exceptions.h"
#include "subrip_reader.h"
#include "exceptions.h"
+#include "util.h"
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
+#include <boost/bind.hpp>
#include <cstdio>
#include <vector>
#include <cstdio>
#include <vector>
+#include <iostream>
using std::string;
using std::vector;
using std::list;
using std::cout;
using std::hex;
using std::string;
using std::vector;
using std::list;
using std::cout;
using std::hex;
+using std::stringstream;
using boost::lexical_cast;
using boost::to_upper;
using boost::lexical_cast;
using boost::to_upper;
+using boost::optional;
+using boost::function;
using namespace sub;
using namespace sub;
+/** @param s Subtitle string encoded in UTF-8 */
+SubripReader::SubripReader (string const & s)
+{
+ stringstream str (s);
+ this->read (boost::bind (&get_line_stringstream, &str));
+}
+
+/** @param f Subtitle file encoded in UTF-8 */
SubripReader::SubripReader (FILE* f)
SubripReader::SubripReader (FILE* f)
+{
+ this->read (boost::bind (&get_line_file, f));
+}
+
+void
+SubripReader::read (function<optional<string> ()> get_line)
{
enum {
COUNTER,
{
enum {
COUNTER,
@@
-42,38
+66,35
@@
SubripReader::SubripReader (FILE* f)
CONTENT
} state = COUNTER;
CONTENT
} state = COUNTER;
- char buffer[256];
-
Time from;
Time to;
string line;
int line_number = 0;
Time from;
Time to;
string line;
int line_number = 0;
- while (
!feof (f)
) {
-
char* r = fgets (buffer, sizeof (buffer), f
);
- if (
r == 0 || feof (f)
) {
+ while (
true
) {
+
optional<string> line = get_line (
);
+ if (
!line
) {
break;
}
break;
}
- line = string (buffer);
- trim_right_if (line, boost::is_any_of ("\n\r"));
+ trim_right_if (*line, boost::is_any_of ("\n\r"));
if (
if (
- line
.
length() >= 3 &&
- static_cast<unsigned char> (line[0]) == 0xef &&
- static_cast<unsigned char> (line[1]) == 0xbb &&
- static_cast<unsigned char> (line[2]) == 0xbf
+ line
->
length() >= 3 &&
+ static_cast<unsigned char> (line
.get()
[0]) == 0xef &&
+ static_cast<unsigned char> (line
.get()
[1]) == 0xbb &&
+ static_cast<unsigned char> (line
.get()
[2]) == 0xbf
) {
/* Skip Unicode byte order mark */
) {
/* Skip Unicode byte order mark */
- line = line
.
substr (3);
+ line = line
->
substr (3);
}
switch (state) {
case COUNTER:
{
}
switch (state) {
case COUNTER:
{
- if (line
.
empty ()) {
+ if (line
->
empty ()) {
/* a blank line at the start is ok */
break;
}
/* a blank line at the start is ok */
break;
}
@@
-84,9
+105,9
@@
SubripReader::SubripReader (FILE* f)
case METADATA:
{
vector<string> p;
case METADATA:
{
vector<string> p;
- boost::algorithm::split (p, line, boost::algorithm::is_any_of (" "));
+ boost::algorithm::split (p,
*
line, boost::algorithm::is_any_of (" "));
if (p.size() != 3 && p.size() != 7) {
if (p.size() != 3 && p.size() != 7) {
- throw SubripError (line, "a time/position line");
+ throw SubripError (
*
line, "a time/position line");
}
from = convert_time (p[0]);
}
from = convert_time (p[0]);
@@
-98,11
+119,11
@@
SubripReader::SubripReader (FILE* f)
break;
}
case CONTENT:
break;
}
case CONTENT:
- if (line
.
empty ()) {
+ if (line
->
empty ()) {
state = COUNTER;
line_number = 0;
} else {
state = COUNTER;
line_number = 0;
} else {
- convert_line (line, line_number, from, to);
+ convert_line (
*
line, line_number, from, to);
line_number++;
}
break;
line_number++;
}
break;
@@
-196,6
+217,7
@@
SubripReader::convert_line (string t, int line_number, Time from, Time to)
colours.push_back (p.colour);
}
} else if (tag == "/font") {
colours.push_back (p.colour);
}
} else if (tag == "/font") {
+ maybe_content (p);
colours.pop_back ();
p.colour = colours.back ();
}
colours.pop_back ();
p.colour = colours.back ();
}