From 8fd43fface7757bd0d7228ff0490a8c8ad074175 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Thu, 11 Feb 2016 11:46:13 +0000 Subject: Extract get_line_{file,stringstream} into standalone methods. --- src/util.cc | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/util.cc') 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 +#include +#include 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 +sub::get_line_stringstream (stringstream* str) +{ + string s; + getline (*str, s); + if (!str->good ()) { + return optional (); + } + + return s; +} + +optional +sub::get_line_file (FILE* f) +{ + char buffer[256]; + char* r = fgets (buffer, sizeof (buffer), f); + if (r == 0 || feof (f)) { + return optional (); + } + + return string (buffer); +} -- cgit v1.2.3