From 9a398504ca869e00de15c713108264e0beead504 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sun, 12 Jan 2020 09:48:22 -0500 Subject: Refactor QUtil::read_lines_from_file This commit adds the preserve_eol flags but doesn't implement EOL preservation yet. --- libqpdf/QUtil.cc | 93 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 73 insertions(+), 20 deletions(-) (limited to 'libqpdf/QUtil.cc') diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index 5fda01c9..42647504 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -234,6 +234,23 @@ static unsigned short mac_roman_to_unicode[] = { 0x02c7, // 0xff }; +class FileCloser +{ + public: + FileCloser(FILE* f) : + f(f) + { + } + + ~FileCloser() + { + fclose(f); + } + + private: + FILE* f; +}; + template static std::string @@ -987,19 +1004,6 @@ QUtil::is_number(char const* p) return found_digit; } -std::list -QUtil::read_lines_from_file(char const* filename) -{ - std::ifstream in(filename, std::ios_base::binary); - if (! in.is_open()) - { - throw_system_error(std::string("open ") + filename); - } - std::list lines = read_lines_from_file(in); - in.close(); - return lines; -} - void QUtil::read_file_into_memory( char const* filename, @@ -1039,19 +1043,70 @@ QUtil::read_file_into_memory( fclose(f); } +std::list +QUtil::read_lines_from_file(char const* filename) +{ + // ABI: remove this method + std::list lines; + FILE* f = safe_fopen(filename, "rb"); + FileCloser fc(f); + auto next_char = [&f](char& ch) { return (fread(&ch, 1, 1, f) > 0); }; + read_lines_from_file(next_char, lines, false); + return lines; +} + +std::list +QUtil::read_lines_from_file(char const* filename, bool preserve_eol) +{ + std::list lines; + FILE* f = safe_fopen(filename, "rb"); + FileCloser fc(f); + auto next_char = [&f](char& ch) { return (fread(&ch, 1, 1, f) > 0); }; + read_lines_from_file(next_char, lines, preserve_eol); + return lines; +} + std::list QUtil::read_lines_from_file(std::istream& in) { - std::list result; - std::string* buf = 0; + // ABI: remove this method + std::list lines; + auto next_char = [&in](char& ch) { return (in.get(ch)) ? true: false; }; + read_lines_from_file(next_char, lines, false); + return lines; +} + +std::list +QUtil::read_lines_from_file(std::istream& in, bool preserve_eol) +{ + std::list lines; + auto next_char = [&in](char& ch) { return (in.get(ch)) ? true: false; }; + read_lines_from_file(next_char, lines, preserve_eol); + return lines; +} + +std::list +QUtil::read_lines_from_file(FILE* f, bool preserve_eol) +{ + std::list lines; + auto next_char = [&f](char& ch) { return (fread(&ch, 1, 1, f) > 0); }; + read_lines_from_file(next_char, lines, preserve_eol); + return lines; +} +void +QUtil::read_lines_from_file(std::function next_char, + std::list& lines, + bool preserve_eol) +{ + std::string* buf = 0; char c; - while (in.get(c)) + while (next_char(c)) { if (buf == 0) { - result.push_back(""); - buf = &(result.back()); + lines.push_back(""); + buf = &(lines.back()); buf->reserve(80); } @@ -1074,8 +1129,6 @@ QUtil::read_lines_from_file(std::istream& in) buf->append(1, c); } } - - return result; } int -- cgit v1.2.3-54-g00ecf