aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QUtil.cc
diff options
context:
space:
mode:
authorm-holger <m-holger@kubitscheck.org>2023-03-06 15:41:43 +0100
committerm-holger <m-holger@kubitscheck.org>2023-03-08 22:00:26 +0100
commit4ee6ff0a738374290a79ba280be44c01e33354cf (patch)
tree2eec9b731fdd5393004bc12707b96bf39b48c9dd /libqpdf/QUtil.cc
parent4359de903866b2753273ddfefc12f00d06c83668 (diff)
downloadqpdf-4ee6ff0a738374290a79ba280be44c01e33354cf.tar.zst
Add new procedure QUtil::read_file_into_string
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)
{