io/FileDescriptor: pass std::span to Full{Read,Write}()
This commit is contained in:
parent
9a0b3775d8
commit
09a2da89a2
|
@ -249,38 +249,32 @@ FileDescriptor::GetSize() const noexcept
|
|||
}
|
||||
|
||||
void
|
||||
FileDescriptor::FullRead(void *_buffer, std::size_t length) const
|
||||
FileDescriptor::FullRead(std::span<std::byte> dest) const
|
||||
{
|
||||
auto buffer = (std::byte *)_buffer;
|
||||
|
||||
while (length > 0) {
|
||||
ssize_t nbytes = Read(buffer, length);
|
||||
while (!dest.empty()) {
|
||||
ssize_t nbytes = Read(dest.data(), dest.size());
|
||||
if (nbytes <= 0) {
|
||||
if (nbytes < 0)
|
||||
throw MakeErrno("Failed to read");
|
||||
throw std::runtime_error("Unexpected end of file");
|
||||
}
|
||||
|
||||
buffer += nbytes;
|
||||
length -= nbytes;
|
||||
dest = dest.subspan(nbytes);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
FileDescriptor::FullWrite(const void *_buffer, std::size_t length) const
|
||||
FileDescriptor::FullWrite(std::span<const std::byte> src) const
|
||||
{
|
||||
auto buffer = (const std::byte *)_buffer;
|
||||
|
||||
while (length > 0) {
|
||||
ssize_t nbytes = Write(buffer, length);
|
||||
while (!src.empty()) {
|
||||
ssize_t nbytes = Write(src.data(), src.size());
|
||||
if (nbytes <= 0) {
|
||||
if (nbytes < 0)
|
||||
throw MakeErrno("Failed to write");
|
||||
throw std::runtime_error("Failed to write");
|
||||
}
|
||||
|
||||
buffer += nbytes;
|
||||
length -= nbytes;
|
||||
src = src.subspan(nbytes);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <span>
|
||||
#include <utility>
|
||||
|
||||
#include <unistd.h>
|
||||
|
@ -224,7 +225,7 @@ public:
|
|||
* Read until all of the given buffer has been filled. Throws
|
||||
* on error.
|
||||
*/
|
||||
void FullRead(void *buffer, std::size_t length) const;
|
||||
void FullRead(std::span<std::byte> dest) const;
|
||||
|
||||
ssize_t Write(const void *buffer, std::size_t length) const noexcept {
|
||||
return ::write(fd, buffer, length);
|
||||
|
@ -234,7 +235,7 @@ public:
|
|||
* Write until all of the given buffer has been written.
|
||||
* Throws on error.
|
||||
*/
|
||||
void FullWrite(const void *buffer, std::size_t length) const;
|
||||
void FullWrite(std::span<const std::byte> src) const;
|
||||
|
||||
#ifndef _WIN32
|
||||
int Poll(short events, int timeout) const noexcept;
|
||||
|
|
|
@ -28,7 +28,7 @@ ReadFrames(FileDescriptor fd, void *_buffer, std::size_t size,
|
|||
const size_t modulo = nbytes % frame_size;
|
||||
if (modulo > 0) {
|
||||
size_t rest = frame_size - modulo;
|
||||
fd.FullRead(buffer + nbytes, rest);
|
||||
fd.FullRead({(std::byte *)buffer + nbytes, rest});
|
||||
nbytes += rest;
|
||||
}
|
||||
|
||||
|
|
|
@ -116,7 +116,7 @@ RunConvert(PcmConvert &convert, size_t in_frame_size,
|
|||
buffer.Consume(src.size());
|
||||
|
||||
auto output = convert.Convert(src);
|
||||
out_fd.FullWrite(output.data(), output.size());
|
||||
out_fd.FullWrite(output);
|
||||
}
|
||||
|
||||
while (true) {
|
||||
|
@ -124,7 +124,7 @@ RunConvert(PcmConvert &convert, size_t in_frame_size,
|
|||
if (output.data() == nullptr)
|
||||
break;
|
||||
|
||||
out_fd.FullWrite(output.data(), output.size());
|
||||
out_fd.FullWrite(output);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -87,14 +87,14 @@ try {
|
|||
break;
|
||||
|
||||
auto dest = filter->FilterPCM(std::span{buffer}.first(nbytes));
|
||||
output_fd.FullWrite(dest.data(), dest.size());
|
||||
output_fd.FullWrite(dest);
|
||||
}
|
||||
|
||||
while (true) {
|
||||
auto dest = filter->Flush();
|
||||
if (dest.data() == nullptr)
|
||||
break;
|
||||
output_fd.FullWrite(dest.data(), dest.size());
|
||||
output_fd.FullWrite(dest);
|
||||
}
|
||||
|
||||
/* cleanup and exit */
|
||||
|
|
|
@ -170,13 +170,13 @@ dump_input_stream(InputStream &is, FileDescriptor out,
|
|||
}
|
||||
}
|
||||
|
||||
char buffer[MAX_CHUNK_SIZE];
|
||||
std::byte buffer[MAX_CHUNK_SIZE];
|
||||
assert(chunk_size <= sizeof(buffer));
|
||||
size_t num_read = is.Read(lock, buffer, chunk_size);
|
||||
if (num_read == 0)
|
||||
break;
|
||||
|
||||
out.FullWrite(buffer, num_read);
|
||||
out.FullWrite({buffer, num_read});
|
||||
}
|
||||
|
||||
is.Check();
|
||||
|
|
Loading…
Reference in New Issue