97 lines
2.7 KiB
C++
Raw Normal View History

2019-02-23 04:15:54 -05:00
#pragma once
2021-08-13 02:08:56 -07:00
#include <sys/uio.h>
#include <cstdio>
2019-02-23 04:15:54 -05:00
#include <memory>
2020-03-09 01:50:30 -07:00
#include "../files.hpp"
2019-11-23 04:57:52 -05:00
class stream {
public:
2021-08-13 02:08:56 -07:00
virtual ssize_t read(void *buf, size_t len);
virtual ssize_t readFully(void *buf, size_t len);
virtual ssize_t readv(const iovec *iov, int iovcnt);
virtual ssize_t write(const void *buf, size_t len);
virtual ssize_t writeFully(void *buf, size_t len);
virtual ssize_t writev(const iovec *iov, int iovcnt);
virtual off_t seek(off_t off, int whence);
virtual ~stream() = default;
};
2019-12-13 00:37:06 -05:00
using stream_ptr = std::unique_ptr<stream>;
// Delegates all operations to base stream
class filter_stream : public stream {
public:
filter_stream(stream_ptr &&base) : base(std::move(base)) {}
2021-08-13 02:08:56 -07:00
ssize_t read(void *buf, size_t len) override;
ssize_t write(const void *buf, size_t len) override;
// Seeking while filtering does not make sense
off_t seek(off_t off, int whence) final { return stream::seek(off, whence); }
protected:
stream_ptr base;
};
// Byte stream that dynamically allocates memory
2019-12-13 00:37:06 -05:00
class byte_stream : public stream {
public:
byte_stream(uint8_t *&buf, size_t &len);
2021-08-13 02:08:56 -07:00
template <class Byte>
byte_stream(Byte *&buf, size_t &len) : byte_stream(reinterpret_cast<uint8_t *&>(buf), len) {}
ssize_t read(void *buf, size_t len) override;
ssize_t write(const void *buf, size_t len) override;
off_t seek(off_t off, int whence) override;
private:
uint8_t *&_buf;
size_t &_len;
size_t _pos = 0;
size_t _cap = 0;
void resize(size_t new_pos, bool zero = false);
};
// File stream but does not close the file descriptor at any time
class fd_stream : public stream {
public:
fd_stream(int fd) : fd(fd) {}
2021-08-13 02:08:56 -07:00
ssize_t read(void *buf, size_t len) override;
ssize_t readv(const iovec *iov, int iovcnt) override;
ssize_t write(const void *buf, size_t len) override;
ssize_t writev(const iovec *iov, int iovcnt) override;
off_t seek(off_t off, int whence) override;
private:
int fd;
};
2019-12-13 00:37:06 -05:00
/* ****************************************
* Bridge between stream class and C stdio
* ****************************************/
// sFILE -> stream_ptr
class fp_stream final : public stream {
public:
fp_stream(FILE *fp = nullptr) : fp(fp, fclose) {}
fp_stream(sFILE &&fp) : fp(std::move(fp)) {}
2021-08-13 02:08:56 -07:00
ssize_t read(void *buf, size_t len) override;
ssize_t write(const void *buf, size_t len) override;
off_t seek(off_t off, int whence) override;
2019-12-13 00:37:06 -05:00
private:
sFILE fp;
2019-12-13 00:37:06 -05:00
};
// stream_ptr -> sFILE
sFILE make_stream_fp(stream_ptr &&strm);
template <class T, class... Args>
sFILE make_stream_fp(Args &&... args) {
return make_stream_fp(stream_ptr(new T(std::forward<Args>(args)...)));
2019-12-13 00:37:06 -05:00
}