2019-02-23 09:15:54 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-08-13 09:08:56 +00:00
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <cstdio>
|
2019-02-23 09:15:54 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2020-03-09 08:50:30 +00:00
|
|
|
#include "../files.hpp"
|
2019-11-23 09:57:52 +00:00
|
|
|
|
2019-11-21 02:48:49 +00:00
|
|
|
class stream {
|
|
|
|
public:
|
2021-08-13 09:08:56 +00: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);
|
2021-11-21 12:38:22 +00:00
|
|
|
virtual bool write(const void *buf, size_t len);
|
2021-08-13 09:08:56 +00:00
|
|
|
virtual ssize_t writev(const iovec *iov, int iovcnt);
|
2020-12-31 06:11:24 +00:00
|
|
|
virtual off_t seek(off_t off, int whence);
|
|
|
|
virtual ~stream() = default;
|
2019-11-21 02:48:49 +00:00
|
|
|
};
|
|
|
|
|
2019-12-13 05:37:06 +00:00
|
|
|
using stream_ptr = std::unique_ptr<stream>;
|
|
|
|
|
|
|
|
// Delegates all operations to base stream
|
2019-11-21 02:48:49 +00:00
|
|
|
class filter_stream : public stream {
|
|
|
|
public:
|
2020-12-31 06:11:24 +00:00
|
|
|
filter_stream(stream_ptr &&base) : base(std::move(base)) {}
|
2019-11-21 02:48:49 +00:00
|
|
|
|
2021-08-13 09:08:56 +00:00
|
|
|
ssize_t read(void *buf, size_t len) override;
|
2021-11-21 12:38:22 +00:00
|
|
|
bool write(const void *buf, size_t len) override;
|
2021-11-24 02:08:14 +00:00
|
|
|
virtual bool write(const void *buf, size_t len, bool final);
|
2021-08-13 09:08:56 +00:00
|
|
|
|
|
|
|
// Seeking while filtering does not make sense
|
|
|
|
off_t seek(off_t off, int whence) final { return stream::seek(off, whence); }
|
2019-11-21 02:48:49 +00:00
|
|
|
|
|
|
|
protected:
|
2020-12-31 06:11:24 +00:00
|
|
|
stream_ptr base;
|
2019-11-21 02:48:49 +00:00
|
|
|
};
|
|
|
|
|
2021-11-24 02:08:14 +00:00
|
|
|
using filter_strm_ptr = std::unique_ptr<filter_stream>;
|
|
|
|
|
2021-11-21 10:27:52 +00:00
|
|
|
// Buffered output stream, writing in chunks
|
|
|
|
class chunk_out_stream : public filter_stream {
|
|
|
|
public:
|
|
|
|
chunk_out_stream(stream_ptr &&base, size_t buf_sz, size_t chunk_sz)
|
|
|
|
: filter_stream(std::move(base)), chunk_sz(chunk_sz), buf_sz(buf_sz) {}
|
|
|
|
|
|
|
|
chunk_out_stream(stream_ptr &&base, size_t buf_sz = 4096)
|
|
|
|
: chunk_out_stream(std::move(base), buf_sz, buf_sz) {}
|
|
|
|
|
2021-11-21 12:38:22 +00:00
|
|
|
~chunk_out_stream() override { delete[] _buf; }
|
2021-11-21 10:27:52 +00:00
|
|
|
|
|
|
|
// Reading does not make sense
|
|
|
|
ssize_t read(void *buf, size_t len) final { return stream::read(buf, len); }
|
2021-11-21 12:38:22 +00:00
|
|
|
bool write(const void *buf, size_t len) final;
|
2021-11-24 02:08:14 +00:00
|
|
|
bool write(const void *buf, size_t len, bool final) final;
|
2021-11-21 10:27:52 +00:00
|
|
|
|
|
|
|
protected:
|
2021-11-24 00:50:08 +00:00
|
|
|
// Classes inheriting this class has to call finalize() in its destructor
|
|
|
|
void finalize();
|
2021-11-24 02:08:14 +00:00
|
|
|
virtual bool write_chunk(const void *buf, size_t len, bool final) = 0;
|
2021-11-21 10:27:52 +00:00
|
|
|
|
|
|
|
size_t chunk_sz;
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t buf_sz;
|
|
|
|
size_t buf_off = 0;
|
|
|
|
uint8_t *_buf = nullptr;
|
|
|
|
};
|
|
|
|
|
2019-11-21 11:08:02 +00:00
|
|
|
// Byte stream that dynamically allocates memory
|
2019-12-13 05:37:06 +00:00
|
|
|
class byte_stream : public stream {
|
2019-11-21 02:48:49 +00:00
|
|
|
public:
|
2020-12-31 06:11:24 +00:00
|
|
|
byte_stream(uint8_t *&buf, size_t &len);
|
2021-08-13 09:08:56 +00: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;
|
2021-11-21 12:38:22 +00:00
|
|
|
bool write(const void *buf, size_t len) override;
|
2020-12-31 06:11:24 +00:00
|
|
|
off_t seek(off_t off, int whence) override;
|
2019-11-21 02:48:49 +00:00
|
|
|
|
|
|
|
private:
|
2020-12-31 06:11:24 +00:00
|
|
|
uint8_t *&_buf;
|
|
|
|
size_t &_len;
|
|
|
|
size_t _pos = 0;
|
|
|
|
size_t _cap = 0;
|
2019-11-21 02:48:49 +00:00
|
|
|
|
2020-12-31 06:11:24 +00:00
|
|
|
void resize(size_t new_pos, bool zero = false);
|
2019-11-21 02:48:49 +00:00
|
|
|
};
|
|
|
|
|
2021-11-21 12:38:22 +00:00
|
|
|
class file_stream : public stream {
|
|
|
|
public:
|
|
|
|
bool write(const void *buf, size_t len) final;
|
|
|
|
protected:
|
|
|
|
virtual ssize_t do_write(const void *buf, size_t len) = 0;
|
|
|
|
};
|
|
|
|
|
2019-11-21 11:08:02 +00:00
|
|
|
// File stream but does not close the file descriptor at any time
|
2021-11-21 12:38:22 +00:00
|
|
|
class fd_stream : public file_stream {
|
2019-11-21 02:48:49 +00:00
|
|
|
public:
|
2020-12-31 06:11:24 +00:00
|
|
|
fd_stream(int fd) : fd(fd) {}
|
2021-08-13 09:08:56 +00:00
|
|
|
ssize_t read(void *buf, size_t len) override;
|
|
|
|
ssize_t readv(const iovec *iov, int iovcnt) override;
|
|
|
|
ssize_t writev(const iovec *iov, int iovcnt) override;
|
2020-12-31 06:11:24 +00:00
|
|
|
off_t seek(off_t off, int whence) override;
|
2021-11-21 12:38:22 +00:00
|
|
|
protected:
|
|
|
|
ssize_t do_write(const void *buf, size_t len) override;
|
2019-11-21 02:48:49 +00:00
|
|
|
private:
|
2020-12-31 06:11:24 +00:00
|
|
|
int fd;
|
2019-11-21 02:48:49 +00:00
|
|
|
};
|
2019-12-13 05:37:06 +00:00
|
|
|
|
|
|
|
/* ****************************************
|
|
|
|
* Bridge between stream class and C stdio
|
|
|
|
* ****************************************/
|
|
|
|
|
|
|
|
// sFILE -> stream_ptr
|
2021-11-21 12:38:22 +00:00
|
|
|
class fp_stream final : public file_stream {
|
2019-12-13 05:37:06 +00:00
|
|
|
public:
|
2020-12-31 06:11:24 +00:00
|
|
|
fp_stream(FILE *fp = nullptr) : fp(fp, fclose) {}
|
|
|
|
fp_stream(sFILE &&fp) : fp(std::move(fp)) {}
|
2021-08-13 09:08:56 +00:00
|
|
|
ssize_t read(void *buf, size_t len) override;
|
2020-12-31 06:11:24 +00:00
|
|
|
off_t seek(off_t off, int whence) override;
|
2021-11-21 12:38:22 +00:00
|
|
|
protected:
|
|
|
|
ssize_t do_write(const void *buf, size_t len) override;
|
2019-12-13 05:37:06 +00:00
|
|
|
private:
|
2020-12-31 06:11:24 +00:00
|
|
|
sFILE fp;
|
2019-12-13 05:37:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// stream_ptr -> sFILE
|
|
|
|
sFILE make_stream_fp(stream_ptr &&strm);
|
|
|
|
|
|
|
|
template <class T, class... Args>
|
|
|
|
sFILE make_stream_fp(Args &&... args) {
|
2020-12-31 06:11:24 +00:00
|
|
|
return make_stream_fp(stream_ptr(new T(std::forward<Args>(args)...)));
|
2019-12-13 05:37:06 +00:00
|
|
|
}
|