Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 0 additions & 39 deletions cdoc/Io.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,45 +209,6 @@ struct CDOC_EXPORT MultiDataSource : public DataSource {
result_t next(FileInfo& info) { return next(info.name, info.size); }
};

struct CDOC_EXPORT ChainedConsumer : public DataConsumer {
ChainedConsumer(DataConsumer *dst, bool take_ownership) : _dst(dst), _owned(take_ownership) {}
~ChainedConsumer() {
if (_owned) delete _dst;
}
result_t write(const uint8_t *src, size_t size) noexcept override {
return _dst->write(src, size);
}
result_t close() noexcept override {
if (_owned) return _dst->close();
return OK;
}
bool isError() noexcept override {
return _dst->isError();
}
protected:
DataConsumer *_dst;
bool _owned;
};

struct CDOC_EXPORT ChainedSource : public DataSource {
ChainedSource(DataSource *src, bool take_ownership) : _src(src), _owned(take_ownership) {}
~ChainedSource() {
if (_owned) delete _src;
}
result_t read(uint8_t *dst, size_t size) noexcept override {
return _src->read(dst, size);
}
bool isError() noexcept override {
return _src->isError();
}
bool isEof() noexcept override {
return _src->isEof();
}
protected:
DataSource *_src;
bool _owned;
};

struct CDOC_EXPORT IStreamSource : public DataSource {
IStreamSource(std::istream *ifs, bool take_ownership = false) : _ifs(ifs), _owned(take_ownership) {}
IStreamSource(const std::string& path);
Expand Down
24 changes: 16 additions & 8 deletions cdoc/ZStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,20 @@

namespace libcdoc {

struct ZConsumer : public ChainedConsumer {
struct ZConsumer : public DataConsumer {
static constexpr uint64_t CHUNK = 16LL * 1024LL;
DataConsumer *_dst;
bool _owned;
z_stream _s {};
bool _fail = false;
int flush = Z_NO_FLUSH;
ZConsumer(DataConsumer *dst, bool take_ownership = false) : ChainedConsumer(dst, take_ownership) {
ZConsumer(DataConsumer *dst, bool take_ownership = false)
: _dst(dst), _owned(take_ownership) {
if (deflateInit(&_s, Z_DEFAULT_COMPRESSION) != Z_OK) _fail = true;
}
~ZConsumer() {
if (!_fail) deflateEnd(&_s);
if (_owned) delete _dst;
}

libcdoc::result_t write(const uint8_t *src, size_t size) noexcept final {
Expand All @@ -64,30 +68,34 @@ struct ZConsumer : public ChainedConsumer {
}

virtual bool isError() noexcept final {
return _fail || ChainedConsumer::isError();
return _fail || _dst->isError();
};

libcdoc::result_t close() noexcept final {
flush = Z_FINISH;
write (nullptr, 0);
deflateEnd(&_s);
return ChainedConsumer::close();
if (_owned) return _dst->close();
}
};

struct ZSource : public ChainedSource {
struct ZSource : public DataSource {
static constexpr uint64_t CHUNK = 16LL * 1024LL;
DataSource *_src;
bool _owned;
z_stream _s {};
int64_t _error = OK;
std::vector<uint8_t> buf;
int flush = Z_NO_FLUSH;
ZSource(DataSource *src, bool take_ownership = false) : ChainedSource(src, take_ownership) {
ZSource(DataSource *src, bool take_ownership = false)
: _src(src), _owned(take_ownership) {
if (inflateInit2(&_s, MAX_WBITS) != Z_OK) {
_error = ZLIB_ERROR;
}
}
~ZSource() {
if (!_error) inflateEnd(&_s);
if (_owned) delete _src;
}

libcdoc::result_t read(uint8_t *dst, size_t size) noexcept final try {
Expand Down Expand Up @@ -125,11 +133,11 @@ struct ZSource : public ChainedSource {
}

virtual bool isError() noexcept final {
return (_error != OK) || ChainedSource::isError();
return (_error != OK) || _src->isError();
};

virtual bool isEof() noexcept final {
return (_s.avail_in == 0) && ChainedSource::isEof();
return (_s.avail_in == 0) && _src->isEof();
};
};

Expand Down
2 changes: 0 additions & 2 deletions libcdoc.i
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@

%ignore libcdoc::MultiDataSource;
%ignore libcdoc::MultiDataConsumer;
%ignore libcdoc::ChainedConsumer;
%ignore libcdoc::ChainedSource;
%ignore libcdoc::IStreamSource;
%ignore libcdoc::OStreamConsumer;
%ignore libcdoc::VectorConsumer;
Expand Down