1 #ifndef _utl_zfstream_h_
2 #define _utl_zfstream_h_
4 #include <utl/zstream.h>
5 #include <boost/iostreams/positioning.hpp>
6 #include <boost/iostreams/optimal_buffer_size.hpp>
7 #include <boost/shared_ptr.hpp>
8 #include <boost/iostreams/stream.hpp>
9 #include <boost/filesystem.hpp>
31 const auto ext = boost::filesystem::extension(filename);
69 boost::iostreams::input_seekable,
70 boost::iostreams::device_tag,
71 boost::iostreams::closable_tag
77 {
open(filename, ztype); }
80 const std::ios_base::openmode openmode = std::ios_base::in,
82 {
open(filename, openmode, ztype); }
85 {
open(filename, std::ios_base::in, ztype); }
89 const std::ios_base::openmode openmode = std::ios_base::in,
102 read(
char*
const s,
const std::streamsize n)
104 const auto size = boost::iostreams::read(
_impl->_filter, s, n);
106 _impl->_position += size;
110 boost::iostreams::stream_offset
111 seek(
const boost::iostreams::stream_offset off,
const std::ios_base::seekdir way)
113 const auto old =
_impl->_position;
116 if (way == std::ios_base::beg) {
118 throw std::ios_base::failure(
"bad seek offset");
120 }
else if (way == std::ios_base::cur)
123 throw std::ios_base::failure(
"bad seek direction");
126 return _impl->_position;
133 _impl->_filter.reset();
134 _impl->_stream.close();
139 const std::streamsize chunk = boost::iostreams::optimal_buffer_size(
_impl->_filter);
142 for (std::size_t i = 0, n = skip/chunk; i < n; ++i)
143 if (
read(buff, chunk) != chunk)
146 const std::streamsize rest = skip % chunk;
147 if (rest &&
read(buff, rest) != rest)
150 if (
_impl->_position != next)
151 throw std::ios_base::failure(
"bad seek result");
153 return _impl->_position;
192 class zifstream :
public boost::iostreams::stream<zfstream_source> {
194 typedef boost::iostreams::stream<zfstream_source>
base;
203 const std::ios_base::openmode om = std::ios_base::in,
208 { (**this).open(filename, std::ios_base::in, ztype); }
211 const std::ios_base::openmode om = std::ios_base::in,
213 { (**this).open(filename, om, ztype); }
217 bool is_complete() {
return this->is_open() && (**this).is_complete(); }
230 const std::ios_base::openmode om,
241 boost::iostreams::output,
242 boost::iostreams::device_tag,
243 boost::iostreams::closable_tag,
244 boost::iostreams::flushable_tag
250 {
open(filename, ztype); }
255 {
open(filename, om, ztype); }
266 const std::ios_base::openmode om =
268 _impl.reset(
new impl(filename, om, ztype));
273 std::streamsize
write(
const char*
const s,
const std::streamsize n)
274 {
return boost::iostreams::write(
_impl->_filter, s, n); }
307 class zofstream :
public boost::iostreams::stream<zfstream_sink> {
309 typedef boost::iostreams::stream<zfstream_sink>
base;
328 { (**this).open(filename, omode, ztype); }
330 bool is_complete() {
return this->is_open() && (**this).is_complete(); }
int get_n_open_calls() const
void open(const std::string &filename, const std::ios_base::openmode omode=std::ios_base::out, const zip_type ztype=zip_type::automatic)
zfstream_sink(const std::string &filename, const zip_type ztype)
std::ios_base::openmode _openmode
unsigned int _n_open_calls
boost::iostreams::stream_offset seek(const boost::iostreams::stream_offset off, const std::ios_base::seekdir way)
boost::iostreams::stream< zfstream_source > base
zifstream(const std::string &filename, const zip_type ztype)
zfstream_sink(const std::string &filename, const std::ios_base::openmode om=std::ios_base::out, const zip_type ztype=zip_type::automatic)
void open(const std::string &filename, const zip_type ztype)
zfstream_source(const std::string &filename, const std::ios_base::openmode openmode=std::ios_base::in, const zip_type ztype=zip_type::automatic)
vector< t2list > out
output of the algorithm: a list of clusters
void push(stream &strm, const zip_type ztype)
zip_type deduce_zip_type(const std::string &filename, const zip_type ztype)
std::size_t tellg() const
Iterator next(Iterator it)
boost::shared_ptr< impl > _impl
void open(const std::string &filename, const zip_type ztype)
void open(const std::string &filename, const std::ios_base::openmode omode=std::ios_base::out, zip_type ztype=zip_type::automatic)
zofstream(const std::string &filename, const std::ios_base::openmode omode=std::ios_base::out, const zip_type ztype=zip_type::automatic)
void open(const std::string &filename, const zip_type ztype)
impl(const std::string &filename, const std::ios_base::openmode om, const zip_type ztype)
void open(const std::string &filename, const zip_type ztype)
zip_type get_zip_type() const
std::streamsize read(char *const s, const std::streamsize n)
zofstream(const std::string &filename, const zip_type ztype)
bool is_automatic(const zip_type ztype)
zip_type get_zip_type() const
zfstream_source(const std::string &filename, const zip_type ztype)
boost::shared_ptr< impl > _impl
void open(const std::string &filename, const std::ios_base::openmode om=std::ios_base::in, const zip_type ztype=zip_type::automatic)
std::streamsize write(const char *const s, const std::streamsize n)
zifstream(const std::string &filename, const std::ios_base::openmode om=std::ios_base::in, const zip_type ztype=zip_type::automatic)
boost::iostreams::stream< zfstream_sink > base
void open(const std::string &filename, const std::ios_base::openmode openmode=std::ios_base::in, const zip_type ztype=zip_type::automatic)