From 2d5b854468c2612dcfe45a659b85d92db2672cbe Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sat, 29 Jul 2017 22:23:21 -0400 Subject: Allow reading command-line args from files (fixes #16) --- libqpdf/QUtil.cc | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'libqpdf') diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index eed8d276..ffe69148 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -566,3 +567,55 @@ 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; +} + +std::list +QUtil::read_lines_from_file(std::istream& in) +{ + std::list result; + std::string* buf = 0; + + char c; + while (in.get(c)) + { + if (buf == 0) + { + result.push_back(""); + buf = &(result.back()); + buf->reserve(80); + } + + if (buf->capacity() == buf->size()) + { + buf->reserve(buf->capacity() * 2); + } + if (c == '\n') + { + // Remove any carriage return that preceded the + // newline and discard the newline + if ((! buf->empty()) && ((*(buf->rbegin())) == '\r')) + { + buf->erase(buf->length() - 1); + } + buf = 0; + } + else + { + buf->append(1, c); + } + } + + return result; +} -- cgit v1.2.3-54-g00ecf