aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QUtil.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <jberkenbilt@users.noreply.github.com>2023-03-18 18:59:47 +0100
committerGitHub <noreply@github.com>2023-03-18 18:59:47 +0100
commita2c7471e66d87751312cd10dba4905307202b56f (patch)
treeb4ae927ef1dea72cb6555de5bc71df09da66bf0a /libqpdf/QUtil.cc
parent84e29026f5ac9bb63180da92944fd8aa1cd0e8d4 (diff)
parentcfcceff6aa921c45c2a3f0fa7a486ed9f02ccc4a (diff)
downloadqpdf-a2c7471e66d87751312cd10dba4905307202b56f.tar.zst
Merge pull request #920 from m-holger/fixqdf_rl
Refactor QdfFixer::processLines
Diffstat (limited to 'libqpdf/QUtil.cc')
-rw-r--r--libqpdf/QUtil.cc31
1 files changed, 31 insertions, 0 deletions
diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc
index 7f23bd03..bae067b6 100644
--- a/libqpdf/QUtil.cc
+++ b/libqpdf/QUtil.cc
@@ -1243,6 +1243,37 @@ QUtil::read_file_into_memory(
}
}
+std::string
+QUtil::read_file_into_string(char const* filename)
+{
+ FILE* f = safe_fopen(filename, "rb");
+ FileCloser fc(f);
+ return read_file_into_string(f, filename);
+}
+
+std::string
+QUtil::read_file_into_string(FILE* f, std::string_view filename)
+{
+ fseek(f, 0, SEEK_END);
+ auto size = QIntC::to_size(QUtil::tell(f));
+ fseek(f, 0, SEEK_SET);
+ std::string result(size, '\0');
+ if (auto read = fread(result.data(), 1, size, f); read != size) {
+ if (ferror(f)) {
+ throw std::runtime_error(
+ std::string("failure reading file ") + std::string(filename) +
+ " into memory: read " + uint_to_string(read) + "; wanted " +
+ uint_to_string(size));
+ } else {
+ throw std::runtime_error(
+ std::string("premature eof reading file ") +
+ std::string(filename) + " into memory: read " +
+ uint_to_string(read) + "; wanted " + uint_to_string(size));
+ }
+ }
+ return result;
+}
+
static bool
read_char_from_FILE(char& ch, FILE* f)
{