2019-02-23 10:15:54 +01:00
|
|
|
#pragma once
|
|
|
|
|
2019-11-21 03:48:49 +01:00
|
|
|
#include <stdio.h>
|
2019-02-23 10:15:54 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2020-03-09 09:50:30 +01:00
|
|
|
#include "../files.hpp"
|
2019-11-23 10:57:52 +01:00
|
|
|
|
2019-11-21 03:48:49 +01:00
|
|
|
class stream {
|
|
|
|
public:
|
|
|
|
virtual int read(void *buf, size_t len);
|
|
|
|
virtual int write(const void *buf, size_t len);
|
|
|
|
virtual off_t seek(off_t off, int whence);
|
|
|
|
virtual ~stream() = default;
|
|
|
|
};
|
|
|
|
|
2019-12-13 06:37:06 +01:00
|
|
|
using stream_ptr = std::unique_ptr<stream>;
|
|
|
|
|
|
|
|
// Delegates all operations to base stream
|
2019-11-21 03:48:49 +01:00
|
|
|
class filter_stream : public stream {
|
|
|
|
public:
|
2019-12-13 06:37:06 +01:00
|
|
|
filter_stream(stream_ptr &&base) : base(std::move(base)) {}
|
2019-11-21 03:48:49 +01:00
|
|
|
|
2019-11-21 12:08:02 +01:00
|
|
|
int read(void *buf, size_t len) override;
|
|
|
|
int write(const void *buf, size_t len) override;
|
2019-11-21 03:48:49 +01:00
|
|
|
|
|
|
|
protected:
|
2019-12-13 06:37:06 +01:00
|
|
|
stream_ptr base;
|
2019-11-21 03:48:49 +01:00
|
|
|
};
|
|
|
|
|
2019-11-21 12:08:02 +01:00
|
|
|
// Byte stream that dynamically allocates memory
|
2019-12-13 06:37:06 +01:00
|
|
|
class byte_stream : public stream {
|
2019-11-21 03:48:49 +01:00
|
|
|
public:
|
|
|
|
byte_stream(uint8_t *&buf, size_t &len);
|
|
|
|
template <class byte>
|
|
|
|
byte_stream(byte *&buf, size_t &len) : byte_stream(reinterpret_cast<uint8_t *&>(buf), len) {}
|
|
|
|
int read(void *buf, size_t len) override;
|
|
|
|
int write(const void *buf, size_t len) override;
|
|
|
|
off_t seek(off_t off, int whence) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint8_t *&_buf;
|
|
|
|
size_t &_len;
|
2019-12-13 06:37:06 +01:00
|
|
|
size_t _pos = 0;
|
2019-11-21 03:48:49 +01:00
|
|
|
size_t _cap = 0;
|
|
|
|
|
|
|
|
void resize(size_t new_pos, bool zero = false);
|
|
|
|
};
|
|
|
|
|
2019-11-21 12:08:02 +01:00
|
|
|
// File stream but does not close the file descriptor at any time
|
|
|
|
class fd_stream : public stream {
|
2019-11-21 03:48:49 +01:00
|
|
|
public:
|
|
|
|
fd_stream(int fd) : fd(fd) {}
|
|
|
|
int read(void *buf, size_t len) override;
|
|
|
|
int write(const void *buf, size_t len) override;
|
|
|
|
off_t seek(off_t off, int whence) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
int fd;
|
|
|
|
};
|
2019-12-13 06:37:06 +01: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)) {}
|
|
|
|
int read(void *buf, size_t len) override;
|
|
|
|
int write(const void *buf, size_t len) override;
|
|
|
|
off_t seek(off_t off, int whence) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
sFILE fp;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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)...)));
|
|
|
|
}
|