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