archive/iso9660: use std::span

This commit is contained in:
Max Kellermann
2022-05-20 11:12:42 +02:00
parent f66315d2de
commit ef54b7d9de

View File

@@ -30,7 +30,6 @@
#include "util/RuntimeError.hxx" #include "util/RuntimeError.hxx"
#include "util/StringCompare.hxx" #include "util/StringCompare.hxx"
#include "util/UTF8.hxx" #include "util/UTF8.hxx"
#include "util/WritableBuffer.hxx"
#include <cdio/iso9660.h> #include <cdio/iso9660.h>
@@ -159,10 +158,10 @@ class Iso9660InputStream final : public InputStream {
class BlockBuffer { class BlockBuffer {
size_t position = 0, fill = 0; size_t position = 0, fill = 0;
std::array<uint8_t, ISO_BLOCKSIZE> data; std::array<std::byte, ISO_BLOCKSIZE> data;
public: public:
[[nodiscard]] ConstBuffer<uint8_t> Read() const noexcept { [[nodiscard]] std::span<const std::byte> Read() const noexcept {
assert(fill <= data.size()); assert(fill <= data.size());
assert(position <= fill); assert(position <= fill);
@@ -170,15 +169,15 @@ class Iso9660InputStream final : public InputStream {
} }
void Consume(size_t nbytes) noexcept { void Consume(size_t nbytes) noexcept {
assert(nbytes <= Read().size); assert(nbytes <= Read().size());
position += nbytes; position += nbytes;
} }
WritableBuffer<uint8_t> Write() noexcept { std::span<std::byte> Write() noexcept {
assert(Read().empty()); assert(Read().empty());
return {data.data(), data.size()}; return data;
} }
void Append(size_t nbytes) noexcept { void Append(size_t nbytes) noexcept {
@@ -286,8 +285,8 @@ Iso9660InputStream::Read(std::unique_lock<Mutex> &,
/* fill the buffer */ /* fill the buffer */
auto w = buffer.Write(); auto w = buffer.Write();
auto nbytes = iso->SeekRead(w.data, read_lsn, auto nbytes = iso->SeekRead(w.data(), read_lsn,
w.size / ISO_BLOCKSIZE); w.size() / ISO_BLOCKSIZE);
if (nbytes <= 0) if (nbytes <= 0)
throw std::runtime_error("Failed to read ISO9660 file"); throw std::runtime_error("Failed to read ISO9660 file");
@@ -296,7 +295,7 @@ Iso9660InputStream::Read(std::unique_lock<Mutex> &,
r = buffer.Read(); r = buffer.Read();
if (skip > 0) { if (skip > 0) {
if (skip >= r.size) if (skip >= r.size())
throw std::runtime_error("Premature end of ISO9660 track"); throw std::runtime_error("Premature end of ISO9660 track");
buffer.Consume(skip); buffer.Consume(skip);
@@ -309,8 +308,8 @@ Iso9660InputStream::Read(std::unique_lock<Mutex> &,
assert(!r.empty()); assert(!r.empty());
assert(skip == 0); assert(skip == 0);
size_t nbytes = std::min(read_size, r.size); size_t nbytes = std::min(read_size, r.size());
memcpy(ptr, r.data, nbytes); memcpy(ptr, r.data(), nbytes);
buffer.Consume(nbytes); buffer.Consume(nbytes);
offset += nbytes; offset += nbytes;
return nbytes; return nbytes;