diff options
| author | Carl Hetherington <cth@carlh.net> | 2016-02-11 11:46:13 +0000 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2016-02-11 11:46:13 +0000 |
| commit | 8fd43fface7757bd0d7228ff0490a8c8ad074175 (patch) | |
| tree | 420c5f191afe48cfe1c5094838002e84739aea91 /src | |
| parent | 4e4e5f750fea7bb011c5c12181a55b90b3202cb7 (diff) | |
Extract get_line_{file,stringstream} into standalone methods.
Diffstat (limited to 'src')
| -rw-r--r-- | src/subrip_reader.cc | 29 | ||||
| -rw-r--r-- | src/subrip_reader.h | 2 | ||||
| -rw-r--r-- | src/util.cc | 30 | ||||
| -rw-r--r-- | src/util.h | 3 |
4 files changed, 36 insertions, 28 deletions
diff --git a/src/subrip_reader.cc b/src/subrip_reader.cc index ddaa112..6c0c63a 100644 --- a/src/subrip_reader.cc +++ b/src/subrip_reader.cc @@ -23,6 +23,7 @@ #include "subrip_reader.h" #include "exceptions.h" +#include "util.h" #include <boost/algorithm/string.hpp> #include <boost/lexical_cast.hpp> #include <boost/regex.hpp> @@ -47,37 +48,13 @@ using namespace sub; SubripReader::SubripReader (string const & s) { stringstream str (s); - this->read (boost::bind (&SubripReader::get_line_stringstream, this, &str)); + this->read (boost::bind (&get_line_stringstream, &str)); } /** @param f Subtitle file encoded in UTF-8 */ SubripReader::SubripReader (FILE* f) { - this->read (boost::bind (&SubripReader::get_line_file, this, f)); -} - -optional<string> -SubripReader::get_line_stringstream (stringstream* str) const -{ - string s; - getline (*str, s); - if (!str->good ()) { - return optional<string> (); - } - - return s; -} - -optional<string> -SubripReader::get_line_file (FILE* f) const -{ - char buffer[256]; - char* r = fgets (buffer, sizeof (buffer), f); - if (r == 0 || feof (f)) { - return optional<string> (); - } - - return string (buffer); + this->read (boost::bind (&get_line_file, f)); } void diff --git a/src/subrip_reader.h b/src/subrip_reader.h index 11ce530..e9a9b35 100644 --- a/src/subrip_reader.h +++ b/src/subrip_reader.h @@ -47,8 +47,6 @@ private: static Time convert_time (std::string t); void convert_line (std::string t, int line_number, Time from, Time to); void maybe_content (RawSubtitle& p); - boost::optional<std::string> get_line_stringstream (std::stringstream* str) const; - boost::optional<std::string> get_line_file (FILE* file) const; void read (boost::function<boost::optional<std::string> ()> get_line); }; diff --git a/src/util.cc b/src/util.cc index 5f4cd39..5510d8e 100644 --- a/src/util.cc +++ b/src/util.cc @@ -18,8 +18,14 @@ */ #include "util.h" +#include <string> +#include <sstream> +#include <cstdio> using std::string; +using std::stringstream; +using std::getline; +using boost::optional; /** @param s A string. * @return true if the string contains only space, newline or tab characters, or is empty. @@ -35,3 +41,27 @@ sub::empty_or_white_space (string s) return true; } + +optional<string> +sub::get_line_stringstream (stringstream* str) +{ + string s; + getline (*str, s); + if (!str->good ()) { + return optional<string> (); + } + + return s; +} + +optional<string> +sub::get_line_file (FILE* f) +{ + char buffer[256]; + char* r = fgets (buffer, sizeof (buffer), f); + if (r == 0 || feof (f)) { + return optional<string> (); + } + + return string (buffer); +} @@ -17,11 +17,14 @@ */ +#include <boost/optional.hpp> #include <string> namespace sub { extern bool empty_or_white_space (std::string s); +extern boost::optional<std::string> get_line_stringstream (std::stringstream* str); +extern boost::optional<std::string> get_line_file (FILE* f); } |
