From 553ac7f353c7043806ec0de70d8630f5cd0a7bb8 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sun, 7 Feb 2021 16:54:16 -0500 Subject: Add QUtil::pipe_file and QUtil::file_provider --- libqpdf/QUtil.cc | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'libqpdf/QUtil.cc') diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index 5442b5ff..dc847679 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -612,6 +613,35 @@ QUtil::rename_file(char const* oldname, char const* newname) #endif } +void +QUtil::pipe_file(char const* filename, Pipeline* p) +{ + // Exercised in test suite by testing file_provider. + FILE* f = safe_fopen(filename, "rb"); + FileCloser fc(f); + size_t len = 0; + int constexpr size = 8192; + unsigned char buf[size]; + while ((len = fread(buf, 1, size, f)) > 0) + { + p->write(buf, len); + } + p->finish(); + if (ferror(f)) + { + throw std::runtime_error( + std::string("failure reading file ") + filename); + } +} + +std::function +QUtil::file_provider(std::string const& filename) +{ + return [filename](Pipeline* p) { + pipe_file(filename.c_str(), p); + }; +} + char* QUtil::copy_string(std::string const& str) { @@ -1018,6 +1048,7 @@ QUtil::read_file_into_memory( PointerHolder& file_buf, size_t& size) { FILE* f = safe_fopen(filename, "rb"); + FileCloser fc(f); fseek(f, 0, SEEK_END); size = QIntC::to_size(QUtil::tell(f)); fseek(f, 0, SEEK_SET); @@ -1048,7 +1079,6 @@ QUtil::read_file_into_memory( uint_to_string(size)); } } - fclose(f); } static bool read_char_from_FILE(char& ch, FILE* f) -- cgit v1.2.3-70-g09d2