zstream.h
Go to the documentation of this file.
1 #ifndef _utl_zstream_h_
2 #define _utl_zstream_h_
3 
4 #include <boost/iostreams/stream.hpp>
5 #include <boost/iostreams/filtering_stream.hpp>
6 #include <boost/iostreams/filter/bzip2.hpp>
7 #include <boost/iostreams/filter/gzip.hpp>
8 #include <boost/shared_ptr.hpp>
9 
10 /*
11  \author Darko Veberic
12  \version $Id: zstream.h 521 2017-01-21 14:53:20Z darko $
13 */
14 
15 
16 namespace utl {
17 
18  enum class zip_type {
19  none = 0,
20  gzip = 1,
21  bzip2 = 2,
22  xzip = 3,
23  automatic = 16,
24  };
25 
26 
27  namespace detail {
28 
29  template<
30  class filtering_stream,
31  class gzip_compressor,
32  class bzip2_compressor
33  >
34  struct zfilter : public filtering_stream {
35  zfilter() : filtering_stream() { }
36 
37  template<class stream>
38  zfilter(stream& strm, const zip_type ztype)
39  { push(strm, ztype); }
40 
41  ~zfilter() { this->reset(); }
42 
43  template<class stream>
44  void
45  push(stream& strm, const zip_type ztype)
46  {
47  this->reset();
48  if (static_cast<int>(ztype) >= 4)
49  throw std::invalid_argument("only zip options allowed for zfilter");
50  switch (static_cast<int>(ztype) & 3) {
51  case static_cast<int>(zip_type::none):
52  break;
53  case static_cast<int>(zip_type::gzip):
54  filtering_stream::push(gzip_compressor());
55  break;
56  case static_cast<int>(zip_type::bzip2):
57  filtering_stream::push(bzip2_compressor());
58  break;
59  case static_cast<int>(zip_type::xzip):
60  throw std::invalid_argument("xzip not implemented yet");
61  }
62  filtering_stream::push(strm);
63  }
64  };
65 
66 
67  typedef zfilter<
68  boost::iostreams::filtering_istream,
69  boost::iostreams::gzip_decompressor,
70  boost::iostreams::bzip2_decompressor
72 
73 
74  typedef zfilter<
75  boost::iostreams::filtering_ostream,
76  boost::iostreams::gzip_compressor,
77  boost::iostreams::bzip2_compressor
79 
80  }
81 
82 
83  //
84 
85 
87  private:
88  struct impl {
89  template<class stream>
90  impl(stream& strm, const zip_type ztype) { _filter.push(strm, ztype); }
92  };
93 
94  public:
95  typedef char char_type;
96  struct category :
97  boost::iostreams::input,
98  boost::iostreams::device_tag,
99  boost::iostreams::closable_tag
100  { };
101 
103 
104  template<class istream>
105  zstream_source(istream& is, const zip_type ztype = zip_type::none)
106  { attach(is, ztype); }
107 
108  template<class istream>
109  void attach(istream& is, const zip_type ztype = zip_type::none)
110  { _impl.reset(new impl(is, ztype)); }
111 
112  void close() { _impl->_filter.reset(); }
113 
114  std::streamsize read(char* const s, const std::streamsize n)
115  { return boost::iostreams::read(_impl->_filter, s, n); }
116 
117  bool is_complete() const { return _impl && _impl->_filter.is_complete(); }
118 
119  private:
120  boost::shared_ptr<impl> _impl;
121  };
122 
123 
124  typedef boost::iostreams::stream<zstream_source> zistream;
125 
126 
127  //
128 
129 
130  class zstream_sink {
131  private:
132  struct impl {
133  template<class stream>
134  impl(stream& strm, const zip_type ztype) { _filter.push(strm, ztype); }
136  };
137 
138  public:
139  typedef char char_type;
140  struct category :
141  boost::iostreams::output,
142  boost::iostreams::device_tag,
143  boost::iostreams::closable_tag,
144  boost::iostreams::flushable_tag
145  { };
146 
148 
149  template<class ostream>
150  zstream_sink(ostream& os, const zip_type ztype = zip_type::none)
151  { attach(os, ztype); }
152 
153  template<class ostream>
154  void attach(ostream& os, const zip_type ztype = zip_type::none)
155  { _impl.reset(new impl(os, ztype)); }
156 
157  void close() { _impl.reset(); }
158 
159  std::streamsize write(const char* const s, const std::streamsize n)
160  { return boost::iostreams::write(_impl->_filter, s, n); }
161 
162  bool flush() { return bool(_impl->_filter.flush()); }
163 
164  bool is_complete() const { return _impl->_filter.is_complete(); }
165 
166  private:
167  boost::shared_ptr<impl> _impl;
168  };
169 
170 
171  typedef boost::iostreams::stream<zstream_sink> zostream;
172 
173 }
174 
175 
176 #endif
boost::iostreams::stream< zstream_source > zistream
Definition: zstream.h:124
std::streamsize read(char *const s, const std::streamsize n)
Definition: zstream.h:114
zstream_sink(ostream &os, const zip_type ztype=zip_type::none)
Definition: zstream.h:150
detail::zofilter _filter
Definition: zstream.h:135
boost::shared_ptr< impl > _impl
Definition: zstream.h:120
void attach(ostream &os, const zip_type ztype=zip_type::none)
Definition: zstream.h:154
bool is(const double a, const double b)
Definition: testlib.cc:113
void push(stream &strm, const zip_type ztype)
Definition: zstream.h:45
bool is_complete() const
Definition: zstream.h:117
constexpr double s
Definition: AugerUnits.h:163
boost::shared_ptr< impl > _impl
Definition: zstream.h:167
zstream_source(istream &is, const zip_type ztype=zip_type::none)
Definition: zstream.h:105
std::streamsize write(const char *const s, const std::streamsize n)
Definition: zstream.h:159
detail::zifilter _filter
Definition: zstream.h:91
void attach(istream &is, const zip_type ztype=zip_type::none)
Definition: zstream.h:109
impl(stream &strm, const zip_type ztype)
Definition: zstream.h:134
zfilter< boost::iostreams::filtering_ostream, boost::iostreams::gzip_compressor, boost::iostreams::bzip2_compressor > zofilter
Definition: zstream.h:78
zfilter< boost::iostreams::filtering_istream, boost::iostreams::gzip_decompressor, boost::iostreams::bzip2_decompressor > zifilter
Definition: zstream.h:71
boost::iostreams::stream< zstream_sink > zostream
Definition: zstream.h:171
zfilter(stream &strm, const zip_type ztype)
Definition: zstream.h:38
bool is_complete() const
Definition: zstream.h:164
zip_type
Definition: zstream.h:18
impl(stream &strm, const zip_type ztype)
Definition: zstream.h:90

, generated on Tue Sep 26 2023.