CMS 3D CMS Logo

zstream.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_MVAComputer_zstream_h
2 #define PhysicsTools_MVAComputer_zstream_h
3 // -*- C++ -*-
4 //
5 // Package: MVAComputer
6 //
7 
8 //
9 // Author: Christophe Saout <christophe.saout@cern.ch>
10 // Created: Sat Apr 24 15:18 CEST 2007
11 //
12 
13 #include <iostream>
14 #include <vector>
15 
16 #include <zlib.h>
17 
18 namespace ext {
19 
20  // implements STL stream wrappers around other streams
21  // to provide transparent gzip compression/decompression
22  //
23  // constructors take reference to an existing stream, rest is straightforward
24 
25  template <typename Item_t, typename Traits_t = std::char_traits<Item_t>, typename Allocator_t = std::allocator<Item_t> >
26  class basic_ozstreambuf : public std::basic_streambuf<Item_t, Traits_t> {
27  public:
28  typedef std::basic_ostream<Item_t, Traits_t> OStream_t;
29  typedef std::basic_streambuf<Item_t, Traits_t> StreamBuf_t;
30 
31  typedef Item_t char_type;
32  typedef typename Traits_t::int_type int_type;
33  typedef typename Traits_t::pos_type pos_type;
34  typedef typename Traits_t::off_type off_type;
35  typedef unsigned char byte_type;
36  typedef Traits_t traits_type;
37 
39  ~basic_ozstreambuf() override;
40 
41  using StreamBuf_t::epptr;
42  using StreamBuf_t::pbase;
43  using StreamBuf_t::pptr;
44 
45  int sync() override;
46  int_type overflow(int_type c) override;
47  std::streamsize flush();
48 
49  private:
50  bool zipToStream(char_type *buf, std::streamsize size);
51  size_t fillInputBuffer();
52 
54  z_stream zipStream;
55  int err;
56  std::vector<byte_type> outputBuffer;
57  std::vector<char_type, Allocator_t> buffer;
58  };
59 
60  template <typename Item_t, typename Traits_t = std::char_traits<Item_t>, typename Allocator_t = std::allocator<Item_t> >
61  class basic_izstreambuf : public std::basic_streambuf<Item_t, Traits_t> {
62  public:
63  typedef std::basic_istream<Item_t, Traits_t> IStream_t;
64  typedef std::basic_streambuf<Item_t, Traits_t> StreamBuf_t;
65 
66  typedef Item_t char_type;
67  typedef typename Traits_t::int_type int_type;
68  typedef typename Traits_t::pos_type pos_type;
69  typedef typename Traits_t::off_type off_type;
70  typedef unsigned char byte_type;
71  typedef Traits_t traits_type;
72 
74  ~basic_izstreambuf() override;
75 
76  using StreamBuf_t::eback;
77  using StreamBuf_t::egptr;
78  using StreamBuf_t::gptr;
79 
80  int_type underflow() override;
81 
82  private:
83  void putbackFromZStream();
84  std::streamsize unzipFromStream(char_type *buf, std::streamsize size);
85  size_t fillInputBuffer();
86 
88  z_stream zipStream;
89  int err;
90  std::vector<byte_type> inputBuffer;
91  std::vector<char_type, Allocator_t> buffer;
92  };
93 
94  template <typename Item_t, typename Traits_t = std::char_traits<Item_t>, typename Allocator_t = std::allocator<Item_t> >
95  class basic_ozstreambase : virtual public std::basic_ios<Item_t, Traits_t> {
96  public:
97  typedef std::basic_ostream<Item_t, Traits_t> OStream_t;
99 
100  basic_ozstreambase(OStream_t *os, int level) : buffer(os, level) { this->init(&buffer); }
101 
102  ZOStreamBuf_t *rdbuf() { return &buffer; }
103 
104  private:
106  };
107 
108  template <typename Item_t, typename Traits_t = std::char_traits<Item_t>, typename Allocator_t = std::allocator<Item_t> >
109  class basic_izstreambase : virtual public std::basic_ios<Item_t, Traits_t> {
110  public:
111  typedef std::basic_istream<Item_t, Traits_t> IStream_t;
113 
115 
116  ZIStreamBuf_t *rdbuf() { return &buffer; }
117 
118  private:
120  };
121 
122  template <typename Item_t, typename Traits_t = std::char_traits<Item_t>, typename Allocator_t = std::allocator<Item_t> >
123  class basic_ozstream : public basic_ozstreambase<Item_t, Traits_t, Allocator_t>,
124  public std::basic_ostream<Item_t, Traits_t> {
125  public:
126  typedef std::basic_ostream<Item_t, Traits_t> OStream_t;
128 
129  basic_ozstream(OStream_t *os, int open_mode = std::ios::out, int level = 9)
131  ~basic_ozstream() override {}
132  };
133 
134  template <typename Item_t, typename Traits_t = std::char_traits<Item_t>, typename Allocator_t = std::allocator<Item_t> >
135  class basic_izstream : public basic_izstreambase<Item_t, Traits_t, Allocator_t>,
136  public std::basic_istream<Item_t, Traits_t> {
137  public:
138  typedef std::basic_istream<Item_t, Traits_t> IStream_t;
140 
141  basic_izstream(IStream_t *is, int open_mode = std::ios::in)
143  ~basic_izstream() override {}
144  };
145 
150 
151 } // namespace ext
152 
153 #include "PhysicsTools/MVAComputer/interface/zstream.icc"
154 
155 #endif // PhysicsTools_MVAComputer_zstream_h
size
Write out results.
OStream_t * os
Definition: zstream.h:53
unsigned char byte_type
Definition: zstream.h:35
std::basic_streambuf< Item_t, Traits_t > StreamBuf_t
Definition: zstream.h:64
unsigned char byte_type
Definition: zstream.h:70
~basic_ozstreambuf() override
Traits_t traits_type
Definition: zstream.h:36
std::basic_ostream< Item_t, Traits_t > OStream_t
Definition: zstream.h:97
basic_izstream< char > izstream
Definition: zstream.h:148
ZOStreamBuf_t * rdbuf()
Definition: zstream.h:102
int init
Definition: HydjetWrapper.h:66
basic_ozstream< char > ozstream
Definition: zstream.h:146
int sync() override
Traits_t::pos_type pos_type
Definition: zstream.h:68
int_type overflow(int_type c) override
basic_izstream< wchar_t > wizstream
Definition: zstream.h:149
basic_izstreambuf(IStream_t *is)
basic_ozstream(OStream_t *os, int open_mode=std::ios::out, int level=9)
Definition: zstream.h:129
std::streamsize flush()
basic_izstreambuf< Item_t, Traits_t, Allocator_t > ZIStreamBuf_t
Definition: zstream.h:112
std::basic_istream< Item_t, Traits_t > IStream_t
Definition: zstream.h:63
~basic_izstreambuf() override
Traits_t::int_type int_type
Definition: zstream.h:67
basic_izstreambase< Item_t, Traits_t, Allocator_t > ZIStreamBase_t
Definition: zstream.h:139
std::basic_istream< Item_t, Traits_t > IStream_t
Definition: zstream.h:138
Traits_t::pos_type pos_type
Definition: zstream.h:33
std::streamsize unzipFromStream(char_type *buf, std::streamsize size)
z_stream zipStream
Definition: zstream.h:88
basic_ozstream< wchar_t > wozstream
Definition: zstream.h:147
basic_ozstreambuf< Item_t, Traits_t, Allocator_t > ZOStreamBuf_t
Definition: zstream.h:98
std::vector< char_type, Allocator_t > buffer
Definition: zstream.h:91
basic_izstream(IStream_t *is, int open_mode=std::ios::in)
Definition: zstream.h:141
Traits_t::int_type int_type
Definition: zstream.h:32
basic_ozstreambase(OStream_t *os, int level)
Definition: zstream.h:100
int_type underflow() override
std::basic_ostream< Item_t, Traits_t > OStream_t
Definition: zstream.h:126
Traits_t traits_type
Definition: zstream.h:71
std::vector< byte_type > inputBuffer
Definition: zstream.h:90
ZIStreamBuf_t buffer
Definition: zstream.h:119
~basic_izstream() override
Definition: zstream.h:143
ZIStreamBuf_t * rdbuf()
Definition: zstream.h:116
basic_ozstreambase< Item_t, Traits_t, Allocator_t > ZOStreamBase_t
Definition: zstream.h:127
~basic_ozstream() override
Definition: zstream.h:131
basic_izstreambase(IStream_t *is)
Definition: zstream.h:114
ZOStreamBuf_t buffer
Definition: zstream.h:105
Traits_t::off_type off_type
Definition: zstream.h:34
std::basic_istream< Item_t, Traits_t > IStream_t
Definition: zstream.h:111
basic_ozstreambuf(OStream_t *os, int level)
Traits_t::off_type off_type
Definition: zstream.h:69
std::vector< byte_type > outputBuffer
Definition: zstream.h:56
IStream_t * is
Definition: zstream.h:87
Definition: memstream.h:15
z_stream zipStream
Definition: zstream.h:54
std::basic_ostream< Item_t, Traits_t > OStream_t
Definition: zstream.h:28
std::vector< char_type, Allocator_t > buffer
Definition: zstream.h:57
std::basic_streambuf< Item_t, Traits_t > StreamBuf_t
Definition: zstream.h:29
bool zipToStream(char_type *buf, std::streamsize size)