Tidy up file_body

fix #439
This commit is contained in:
Vinnie Falco
2017-06-08 05:43:57 -07:00
parent 7952578e1d
commit 9af1b35252
2 changed files with 6 additions and 12 deletions

View File

@ -1,6 +1,7 @@
Version 51 Version 51
* Fix operator<< for header * Fix operator<< for header
* Tidy up file_body
API Changes: API Changes:

View File

@ -14,6 +14,7 @@
#include <boost/assert.hpp> #include <boost/assert.hpp>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <algorithm>
#include <cstdio> #include <cstdio>
#include <cstdint> #include <cstdint>
@ -41,7 +42,6 @@ struct file_body
std::string const& path_; std::string const& path_;
FILE* file_ = nullptr; FILE* file_ = nullptr;
char buf_[4096]; char buf_[4096];
std::size_t buf_len_;
public: public:
using is_deferred = std::true_type; using is_deferred = std::true_type;
@ -71,8 +71,7 @@ struct file_body
{ {
file_ = fopen(path_.c_str(), "rb"); file_ = fopen(path_.c_str(), "rb");
if(! file_) if(! file_)
ec = error_code{errno, ec = error_code{errno, system_category()};
system_category()};
else else
size_ = boost::filesystem::file_size(path_); size_ = boost::filesystem::file_size(path_);
} }
@ -80,13 +79,8 @@ struct file_body
boost::optional<std::pair<const_buffers_type, bool>> boost::optional<std::pair<const_buffers_type, bool>>
get(error_code& ec) get(error_code& ec)
{ {
if(size_ - offset_ < sizeof(buf_)) auto const amount = std::min<std::uint64_t>(size_ - offset_, sizeof(buf_));
buf_len_ = static_cast<std::size_t>( auto const nread = fread(buf_, 1, amount, file_);
size_ - offset_);
else
buf_len_ = sizeof(buf_);
auto const nread = fread(
buf_, 1, sizeof(buf_), file_);
if(ferror(file_)) if(ferror(file_))
{ {
ec = error_code(errno, system_category()); ec = error_code(errno, system_category());
@ -94,8 +88,7 @@ struct file_body
} }
BOOST_ASSERT(nread != 0); BOOST_ASSERT(nread != 0);
offset_ += nread; offset_ += nread;
return {{const_buffers_type{buf_, nread}, return {{const_buffers_type{buf_, nread}, offset_ >= size_}};
offset_ >= size_}};
} }
}; };
}; };