2019-02-23 09:15:54 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <unistd.h>
|
2019-11-21 02:48:49 +00:00
|
|
|
#include <stdio.h>
|
2019-02-23 09:15:54 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2019-11-21 11:17:28 +00:00
|
|
|
// #include <utils.h>
|
2019-11-21 02:48:49 +00:00
|
|
|
|
|
|
|
class stream;
|
|
|
|
|
|
|
|
FILE *open_stream(stream *strm);
|
|
|
|
|
|
|
|
template <class T, class... Args>
|
|
|
|
FILE *open_stream(Args &&... args) {
|
|
|
|
return open_stream(new T(args...));
|
|
|
|
}
|
|
|
|
|
|
|
|
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 int close();
|
|
|
|
virtual ~stream() = default;
|
|
|
|
};
|
|
|
|
|
2019-11-21 11:08:02 +00:00
|
|
|
// Delegates all operations to the base FILE pointer
|
2019-11-21 02:48:49 +00:00
|
|
|
class filter_stream : public stream {
|
|
|
|
public:
|
|
|
|
filter_stream(FILE *fp) : fp(fp) {}
|
2019-11-21 11:08:02 +00:00
|
|
|
~filter_stream() override { if (fp) close(); }
|
2019-11-21 02:48:49 +00:00
|
|
|
|
2019-11-21 11:08:02 +00:00
|
|
|
int read(void *buf, size_t len) override;
|
|
|
|
int write(const void *buf, size_t len) override;
|
|
|
|
int close() override;
|
2019-11-21 02:48:49 +00:00
|
|
|
|
2019-11-21 11:08:02 +00:00
|
|
|
void set_base(FILE *f);
|
2019-11-21 02:48:49 +00:00
|
|
|
template <class T, class... Args >
|
|
|
|
void set_base(Args&&... args) {
|
|
|
|
set_base(open_stream<T>(args...));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
FILE *fp;
|
|
|
|
};
|
|
|
|
|
2019-11-21 11:08:02 +00:00
|
|
|
// Handy interface for classes that need custom seek logic
|
2019-11-21 02:48:49 +00:00
|
|
|
class seekable_stream : public stream {
|
|
|
|
protected:
|
|
|
|
size_t _pos = 0;
|
|
|
|
|
2019-11-21 11:17:28 +00:00
|
|
|
off_t seek_pos(off_t off, int whence);
|
2019-11-21 02:48:49 +00:00
|
|
|
virtual size_t end_pos() = 0;
|
|
|
|
};
|
|
|
|
|
2019-11-21 11:08:02 +00:00
|
|
|
// Byte stream that dynamically allocates memory
|
2019-11-21 02:48:49 +00:00
|
|
|
class byte_stream : public seekable_stream {
|
|
|
|
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;
|
|
|
|
size_t _cap = 0;
|
|
|
|
|
|
|
|
void resize(size_t new_pos, bool zero = false);
|
|
|
|
size_t end_pos() override { return _len; }
|
|
|
|
};
|
|
|
|
|
2019-11-21 11:08:02 +00:00
|
|
|
// File stream but does not close the file descriptor at any time
|
|
|
|
class fd_stream : public stream {
|
2019-11-21 02:48:49 +00: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;
|
|
|
|
};
|