diff --git a/CHANGELOG.md b/CHANGELOG.md index 849ac0c9..9686068f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ Version 51 * Fix operator<< for header +* Tidy up file_body API Changes: diff --git a/examples/file_body.hpp b/examples/file_body.hpp index 7a9d7eb5..860513ee 100644 --- a/examples/file_body.hpp +++ b/examples/file_body.hpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -41,7 +42,6 @@ struct file_body std::string const& path_; FILE* file_ = nullptr; char buf_[4096]; - std::size_t buf_len_; public: using is_deferred = std::true_type; @@ -71,8 +71,7 @@ struct file_body { file_ = fopen(path_.c_str(), "rb"); if(! file_) - ec = error_code{errno, - system_category()}; + ec = error_code{errno, system_category()}; else size_ = boost::filesystem::file_size(path_); } @@ -80,13 +79,8 @@ struct file_body boost::optional> get(error_code& ec) { - if(size_ - offset_ < sizeof(buf_)) - buf_len_ = static_cast( - size_ - offset_); - else - buf_len_ = sizeof(buf_); - auto const nread = fread( - buf_, 1, sizeof(buf_), file_); + auto const amount = std::min(size_ - offset_, sizeof(buf_)); + auto const nread = fread(buf_, 1, amount, file_); if(ferror(file_)) { ec = error_code(errno, system_category()); @@ -94,8 +88,7 @@ struct file_body } BOOST_ASSERT(nread != 0); offset_ += nread; - return {{const_buffers_type{buf_, nread}, - offset_ >= size_}}; + return {{const_buffers_type{buf_, nread}, offset_ >= size_}}; } }; };