Merge pull request #345 from kostasdizas/issue165-compound-document

Removed duplicate code in compound document
This commit is contained in:
Thomas Fussell 2019-07-06 12:23:09 -06:00 committed by GitHub
commit ba01de47a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -121,65 +121,33 @@ private:
{ {
auto bytes_read = std::streamsize(0); auto bytes_read = std::streamsize(0);
if (entry_.size < document_.header_.threshold) const auto sector_chain = short_stream() ? document_.ssat_ : document_.sat_;
{ const auto chain = document_.follow_chain(entry_.start, sector_chain);
const auto chain = document_.follow_chain(entry_.start, document_.ssat_); const auto sector_size = short_stream() ? document_.short_sector_size() : document_.sector_size();
auto current_sector = chain[position_ / document_.short_sector_size()]; auto current_sector = chain[position_ / sector_size];
auto remaining = std::min(std::size_t(entry_.size) - position_, std::size_t(count)); auto remaining = std::min(std::size_t(entry_.size) - position_, std::size_t(count));
while (remaining) while (remaining)
{ {
if (current_sector_.empty() || chain[position_ / document_.short_sector_size()] != current_sector) if (current_sector_.empty() || chain[position_ / sector_size] != current_sector)
{ {
current_sector = chain[position_ / document_.short_sector_size()]; current_sector = chain[position_ / sector_size];
sector_writer_.reset(); sector_writer_.reset();
document_.read_short_sector(current_sector, sector_writer_); if (short_stream())
}
const auto available = std::min(entry_.size - position_,
document_.short_sector_size() - position_ % document_.short_sector_size());
const auto to_read = std::min(available, std::size_t(remaining));
auto start = current_sector_.begin() + static_cast<std::ptrdiff_t>(position_ % document_.short_sector_size());
auto end = start + static_cast<std::ptrdiff_t>(to_read);
for (auto i = start; i < end; ++i)
{ {
*(c++) = static_cast<char>(*i);
}
remaining -= to_read;
position_ += to_read;
bytes_read += to_read;
}
if (position_ < entry_.size && chain[position_ / document_.short_sector_size()] != current_sector)
{
current_sector = chain[position_ / document_.short_sector_size()];
sector_writer_.reset();
document_.read_short_sector(current_sector, sector_writer_); document_.read_short_sector(current_sector, sector_writer_);
} }
}
else else
{ {
const auto chain = document_.follow_chain(entry_.start, document_.sat_);
auto current_sector = chain[position_ / document_.sector_size()];
auto remaining = std::min(std::size_t(entry_.size) - position_, std::size_t(count));
while (remaining)
{
if (current_sector_.empty() || chain[position_ / document_.sector_size()] != current_sector)
{
current_sector = chain[position_ / document_.sector_size()];
sector_writer_.reset();
document_.read_sector(current_sector, sector_writer_); document_.read_sector(current_sector, sector_writer_);
} }
}
const auto available = std::min(entry_.size - position_, const auto available = std::min(entry_.size - position_,
document_.sector_size() - position_ % document_.sector_size()); sector_size - position_ % sector_size);
const auto to_read = std::min(available, std::size_t(remaining)); const auto to_read = std::min(available, std::size_t(remaining));
auto start = current_sector_.begin() + static_cast<std::ptrdiff_t>(position_ % document_.sector_size()); auto start = current_sector_.begin() + static_cast<std::ptrdiff_t>(position_ % sector_size);
auto end = start + static_cast<std::ptrdiff_t>(to_read); auto end = start + static_cast<std::ptrdiff_t>(to_read);
for (auto i = start; i < end; ++i) for (auto i = start; i < end; ++i)
@ -192,10 +160,16 @@ private:
bytes_read += to_read; bytes_read += to_read;
} }
if (position_ < entry_.size && chain[position_ / document_.sector_size()] != current_sector) if (position_ < entry_.size && chain[position_ / sector_size] != current_sector)
{ {
current_sector = chain[position_ / document_.sector_size()]; current_sector = chain[position_ / sector_size];
sector_writer_.reset(); sector_writer_.reset();
if (short_stream())
{
document_.read_short_sector(current_sector, sector_writer_);
}
else
{
document_.read_sector(current_sector, sector_writer_); document_.read_sector(current_sector, sector_writer_);
} }
} }
@ -203,6 +177,11 @@ private:
return bytes_read; return bytes_read;
} }
bool short_stream()
{
return entry_.size < document_.header_.threshold;
}
int_type underflow() override int_type underflow() override
{ {
if (position_ >= entry_.size) if (position_ >= entry_.size)