diff options
Diffstat (limited to 'libqpdf')
-rw-r--r-- | libqpdf/QPDFJob_config.cc | 6 | ||||
-rw-r--r-- | libqpdf/QUtil.cc | 31 |
2 files changed, 33 insertions, 4 deletions
diff --git a/libqpdf/QPDFJob_config.cc b/libqpdf/QPDFJob_config.cc index 3a6bcdf4..a7f22443 100644 --- a/libqpdf/QPDFJob_config.cc +++ b/libqpdf/QPDFJob_config.cc @@ -760,11 +760,9 @@ QPDFJob::Config::showObject(std::string const& parameter) QPDFJob::Config* QPDFJob::Config::jobJsonFile(std::string const& parameter) { - std::shared_ptr<char> file_buf; - size_t size; - QUtil::read_file_into_memory(parameter.c_str(), file_buf, size); try { - o.initializeFromJson(std::string(file_buf.get(), size), true); + o.initializeFromJson( + QUtil::read_file_into_string(parameter.c_str()), true); } catch (std::exception& e) { throw std::runtime_error( "error with job-json file " + std::string(parameter) + ": " + 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) { |