aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QUtil.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2017-07-30 04:23:21 +0200
committerJay Berkenbilt <ejb@ql.org>2017-07-30 04:23:21 +0200
commit2d5b854468c2612dcfe45a659b85d92db2672cbe (patch)
treee4f8a85b520969d5236bb3f057feea94441a4f92 /libqpdf/QUtil.cc
parent5993c3e83c6f83b36045c75a03ffb1da3d1d283c (diff)
downloadqpdf-2d5b854468c2612dcfe45a659b85d92db2672cbe.tar.zst
Allow reading command-line args from files (fixes #16)
Diffstat (limited to 'libqpdf/QUtil.cc')
-rw-r--r--libqpdf/QUtil.cc53
1 files changed, 53 insertions, 0 deletions
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 <cmath>
#include <iomanip>
#include <sstream>
+#include <fstream>
#include <stdexcept>
#include <stdio.h>
#include <errno.h>
@@ -566,3 +567,55 @@ QUtil::is_number(char const* p)
}
return found_digit;
}
+
+std::list<std::string>
+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<std::string> lines = read_lines_from_file(in);
+ in.close();
+ return lines;
+}
+
+std::list<std::string>
+QUtil::read_lines_from_file(std::istream& in)
+{
+ std::list<std::string> 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;
+}