Split input and output streams

This commit is contained in:
topjohnwu
2023-05-19 04:19:43 -07:00
parent 582cad1b8d
commit 57bd450798
6 changed files with 104 additions and 124 deletions

View File

@@ -7,40 +7,35 @@
using namespace std;
static int strm_read(void *v, char *buf, int len) {
auto strm = static_cast<stream *>(v);
auto strm = static_cast<channel *>(v);
return strm->read(buf, len);
}
static int strm_write(void *v, const char *buf, int len) {
auto strm = static_cast<stream *>(v);
auto strm = static_cast<channel *>(v);
if (!strm->write(buf, len))
return -1;
return len;
}
static fpos_t strm_seek(void *v, fpos_t off, int whence) {
auto strm = static_cast<stream *>(v);
auto strm = static_cast<channel *>(v);
return strm->seek(off, whence);
}
static int strm_close(void *v) {
auto strm = static_cast<stream *>(v);
auto strm = static_cast<channel *>(v);
delete strm;
return 0;
}
sFILE make_stream_fp(stream_ptr &&strm) {
sFILE make_channel_fp(channel_ptr &&strm) {
auto fp = make_file(funopen(strm.release(), strm_read, strm_write, strm_seek, strm_close));
setbuf(fp.get(), nullptr);
return fp;
}
ssize_t stream::read(void *buf, size_t len) {
LOGE("This stream does not implement read\n");
return -1;
}
ssize_t stream::readFully(void *buf, size_t len) {
ssize_t in_stream::readFully(void *buf, size_t len) {
size_t read_sz = 0;
ssize_t ret;
do {
@@ -55,7 +50,7 @@ ssize_t stream::readFully(void *buf, size_t len) {
return read_sz;
}
ssize_t stream::readv(const iovec *iov, int iovcnt) {
ssize_t in_stream::readv(const iovec *iov, int iovcnt) {
size_t read_sz = 0;
for (int i = 0; i < iovcnt; ++i) {
auto ret = readFully(iov[i].iov_base, iov[i].iov_len);
@@ -66,12 +61,7 @@ ssize_t stream::readv(const iovec *iov, int iovcnt) {
return read_sz;
}
bool stream::write(const void *buf, size_t len) {
LOGE("This stream does not implement write\n");
return false;
}
ssize_t stream::writev(const iovec *iov, int iovcnt) {
ssize_t out_stream::writev(const iovec *iov, int iovcnt) {
size_t write_sz = 0;
for (int i = 0; i < iovcnt; ++i) {
if (!write(iov[i].iov_base, iov[i].iov_len))
@@ -81,33 +71,24 @@ ssize_t stream::writev(const iovec *iov, int iovcnt) {
return write_sz;
}
off_t stream::seek(off_t off, int whence) {
LOGE("This stream does not implement seek\n");
return -1;
}
ssize_t fp_stream::read(void *buf, size_t len) {
ssize_t fp_channel::read(void *buf, size_t len) {
auto ret = fread(buf, 1, len, fp.get());
return ret ? ret : (ferror(fp.get()) ? -1 : 0);
}
ssize_t fp_stream::do_write(const void *buf, size_t len) {
ssize_t fp_channel::do_write(const void *buf, size_t len) {
return fwrite(buf, 1, len, fp.get());
}
off_t fp_stream::seek(off_t off, int whence) {
off_t fp_channel::seek(off_t off, int whence) {
return fseek(fp.get(), off, whence);
}
ssize_t filter_stream::read(void *buf, size_t len) {
return base->read(buf, len);
}
bool filter_stream::write(const void *buf, size_t len) {
bool filter_out_stream::write(const void *buf, size_t len) {
return base->write(buf, len);
}
bool filter_stream::write(const void *buf, size_t len, bool final) {
bool filter_out_stream::write(const void *buf, size_t len, bool final) {
return write(buf, len);
}
@@ -172,18 +153,18 @@ void chunk_out_stream::finalize() {
}
}
byte_stream::byte_stream(uint8_t *&buf, size_t &len) : _buf(buf), _len(len) {
byte_channel::byte_channel(uint8_t *&buf, size_t &len) : _buf(buf), _len(len) {
buf = nullptr;
len = 0;
}
ssize_t byte_stream::read(void *buf, size_t len) {
ssize_t byte_channel::read(void *buf, size_t len) {
len = std::min((size_t) len, _len - _pos);
memcpy(buf, _buf + _pos, len);
return len;
}
bool byte_stream::write(const void *buf, size_t len) {
bool byte_channel::write(const void *buf, size_t len) {
resize(_pos + len);
memcpy(_buf + _pos, buf, len);
_pos += len;
@@ -191,7 +172,7 @@ bool byte_stream::write(const void *buf, size_t len) {
return true;
}
off_t byte_stream::seek(off_t off, int whence) {
off_t byte_channel::seek(off_t off, int whence) {
off_t np;
switch (whence) {
case SEEK_CUR:
@@ -211,7 +192,7 @@ off_t byte_stream::seek(off_t off, int whence) {
return np;
}
void byte_stream::resize(size_t new_pos, bool zero) {
void byte_channel::resize(size_t new_pos, bool zero) {
bool resize = false;
size_t old_cap = _cap;
while (new_pos > _cap) {
@@ -225,27 +206,27 @@ void byte_stream::resize(size_t new_pos, bool zero) {
}
}
ssize_t fd_stream::read(void *buf, size_t len) {
ssize_t fd_channel::read(void *buf, size_t len) {
return ::read(fd, buf, len);
}
ssize_t fd_stream::readv(const iovec *iov, int iovcnt) {
ssize_t fd_channel::readv(const iovec *iov, int iovcnt) {
return ::readv(fd, iov, iovcnt);
}
ssize_t fd_stream::do_write(const void *buf, size_t len) {
ssize_t fd_channel::do_write(const void *buf, size_t len) {
return ::write(fd, buf, len);
}
ssize_t fd_stream::writev(const iovec *iov, int iovcnt) {
ssize_t fd_channel::writev(const iovec *iov, int iovcnt) {
return ::writev(fd, iov, iovcnt);
}
off_t fd_stream::seek(off_t off, int whence) {
off_t fd_channel::seek(off_t off, int whence) {
return lseek(fd, off, whence);
}
bool file_stream::write(const void *buf, size_t len) {
bool file_channel::write(const void *buf, size_t len) {
size_t write_sz = 0;
ssize_t ret;
do {