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>,
26  typename Allocator_t = std::allocator<Item_t> >
27 class basic_ozstreambuf : public std::basic_streambuf<Item_t, Traits_t> {
28  public:
29  typedef std::basic_ostream<Item_t, Traits_t> OStream_t;
30  typedef std::basic_streambuf<Item_t, Traits_t> StreamBuf_t;
31 
32  typedef Item_t char_type;
33  typedef typename Traits_t::int_type int_type;
34  typedef typename Traits_t::pos_type pos_type;
35  typedef typename Traits_t::off_type off_type;
36  typedef unsigned char byte_type;
37  typedef Traits_t traits_type;
38 
39  basic_ozstreambuf(OStream_t *os, int level);
41 
42  using StreamBuf_t::pbase;
43  using StreamBuf_t::pptr;
44  using StreamBuf_t::epptr;
45 
46  int sync();
47  int_type overflow(int_type c);
48  std::streamsize flush();
49 
50  private:
51  bool zipToStream(char_type *buf, std::streamsize size);
52  size_t fillInputBuffer();
53 
54  OStream_t *os;
55  z_stream zipStream;
56  int err;
57  std::vector<byte_type> outputBuffer;
58  std::vector<char_type, Allocator_t> buffer;
59 };
60 
61 template<typename Item_t, typename Traits_t = std::char_traits<Item_t>,
62  typename Allocator_t = std::allocator<Item_t> >
63 class basic_izstreambuf : public std::basic_streambuf<Item_t, Traits_t> {
64  public:
65  typedef std::basic_istream<Item_t, Traits_t> IStream_t;
66  typedef std::basic_streambuf<Item_t, Traits_t> StreamBuf_t;
67 
68  typedef Item_t char_type;
69  typedef typename Traits_t::int_type int_type;
70  typedef typename Traits_t::pos_type pos_type;
71  typedef typename Traits_t::off_type off_type;
72  typedef unsigned char byte_type;
73  typedef Traits_t traits_type;
74 
75  basic_izstreambuf(IStream_t *is);
77 
78  using StreamBuf_t::gptr;
79  using StreamBuf_t::egptr;
80  using StreamBuf_t::eback;
81 
82  int_type underflow();
83 
84  private:
85  void putbackFromZStream();
86  std::streamsize unzipFromStream(char_type *buf, std::streamsize size);
87  size_t fillInputBuffer();
88 
89  IStream_t *is;
90  z_stream zipStream;
91  int err;
92  std::vector<byte_type> inputBuffer;
93  std::vector<char_type, Allocator_t> buffer;
94 };
95 
96 template<typename Item_t, typename Traits_t = std::char_traits<Item_t>,
97  typename Allocator_t = std::allocator<Item_t> >
98 class basic_ozstreambase : virtual public std::basic_ios<Item_t, Traits_t> {
99  public:
100  typedef std::basic_ostream<Item_t, Traits_t> OStream_t;
102 
103  basic_ozstreambase(OStream_t *os, int level) :
104  buffer(os, level) { this->init(&buffer); }
105 
106  ZOStreamBuf_t *rdbuf() { return &buffer; }
107 
108  private:
109  ZOStreamBuf_t buffer;
110 };
111 
112 template<typename Item_t, typename Traits_t = std::char_traits<Item_t>,
113  typename Allocator_t = std::allocator<Item_t> >
114 class basic_izstreambase : virtual public std::basic_ios<Item_t, Traits_t> {
115  public:
116  typedef std::basic_istream<Item_t, Traits_t> IStream_t;
118 
119  basic_izstreambase(IStream_t *is) : buffer(is) { this->init(&buffer); }
120 
121  ZIStreamBuf_t *rdbuf() { return &buffer; }
122 
123  private:
124  ZIStreamBuf_t buffer;
125 };
126 
127 template<typename Item_t, typename Traits_t = std::char_traits<Item_t>,
128  typename Allocator_t = std::allocator<Item_t> >
129 class basic_ozstream : public basic_ozstreambase<Item_t, Traits_t, Allocator_t>,
130  public std::basic_ostream<Item_t, Traits_t> {
131  public:
132  typedef std::basic_ostream<Item_t, Traits_t> OStream_t;
134 
135  basic_ozstream(OStream_t *os, int open_mode = std::ios::out,
136  int level = 9) :
137  ZOStreamBase_t(os, level),
138  OStream_t(ZOStreamBase_t::rdbuf()) {}
140 };
141 
142 template<typename Item_t, typename Traits_t = std::char_traits<Item_t>,
143  typename Allocator_t = std::allocator<Item_t> >
144 class basic_izstream : public basic_izstreambase<Item_t, Traits_t, Allocator_t>,
145  public std::basic_istream<Item_t, Traits_t> {
146  public:
147  typedef std::basic_istream<Item_t, Traits_t> IStream_t;
149 
150  basic_izstream(IStream_t *is, int open_mode = std::ios::in) :
151  ZIStreamBase_t(is), IStream_t(ZIStreamBase_t::rdbuf()) {}
153 };
154 
159 
160 } // namespace ext
161 
162 #include "PhysicsTools/MVAComputer/interface/zstream.icc"
163 
164 #endif // PhysicsTools_MVAComputer_zstream_h
size
Write out results.
OStream_t * os
Definition: zstream.h:54
unsigned char byte_type
Definition: zstream.h:36
std::basic_streambuf< Item_t, Traits_t > StreamBuf_t
Definition: zstream.h:66
unsigned char byte_type
Definition: zstream.h:72
Traits_t traits_type
Definition: zstream.h:37
std::basic_ostream< Item_t, Traits_t > OStream_t
Definition: zstream.h:100
basic_izstream< char > izstream
Definition: zstream.h:157
ZOStreamBuf_t * rdbuf()
Definition: zstream.h:106
int init
Definition: HydjetWrapper.h:67
basic_ozstream< char > ozstream
Definition: zstream.h:155
Traits_t::pos_type pos_type
Definition: zstream.h:70
basic_izstream< wchar_t > wizstream
Definition: zstream.h:158
basic_ozstream(OStream_t *os, int open_mode=std::ios::out, int level=9)
Definition: zstream.h:135
std::streamsize flush()
basic_izstreambuf< Item_t, Traits_t, Allocator_t > ZIStreamBuf_t
Definition: zstream.h:117
std::basic_istream< Item_t, Traits_t > IStream_t
Definition: zstream.h:65
Traits_t::int_type int_type
Definition: zstream.h:69
basic_izstreambase< Item_t, Traits_t, Allocator_t > ZIStreamBase_t
Definition: zstream.h:148
std::basic_istream< Item_t, Traits_t > IStream_t
Definition: zstream.h:147
Traits_t::pos_type pos_type
Definition: zstream.h:34
z_stream zipStream
Definition: zstream.h:90
basic_ozstream< wchar_t > wozstream
Definition: zstream.h:156
basic_ozstreambuf< Item_t, Traits_t, Allocator_t > ZOStreamBuf_t
Definition: zstream.h:101
std::vector< char_type, Allocator_t > buffer
Definition: zstream.h:93
basic_izstream(IStream_t *is, int open_mode=std::ios::in)
Definition: zstream.h:150
Traits_t::int_type int_type
Definition: zstream.h:33
int_type overflow(int_type c)
basic_ozstreambase(OStream_t *os, int level)
Definition: zstream.h:103
std::basic_ostream< Item_t, Traits_t > OStream_t
Definition: zstream.h:132
Traits_t traits_type
Definition: zstream.h:73
std::vector< byte_type > inputBuffer
Definition: zstream.h:92
ZIStreamBuf_t buffer
Definition: zstream.h:124
ZIStreamBuf_t * rdbuf()
Definition: zstream.h:121
basic_ozstreambase< Item_t, Traits_t, Allocator_t > ZOStreamBase_t
Definition: zstream.h:133
basic_izstreambase(IStream_t *is)
Definition: zstream.h:119
ZOStreamBuf_t buffer
Definition: zstream.h:109
Traits_t::off_type off_type
Definition: zstream.h:35
std::basic_istream< Item_t, Traits_t > IStream_t
Definition: zstream.h:116
basic_ozstreambuf(OStream_t *os, int level)
Traits_t::off_type off_type
Definition: zstream.h:71
std::vector< byte_type > outputBuffer
Definition: zstream.h:57
IStream_t * is
Definition: zstream.h:89
Definition: memstream.h:15
z_stream zipStream
Definition: zstream.h:55
std::basic_ostream< Item_t, Traits_t > OStream_t
Definition: zstream.h:29
std::vector< char_type, Allocator_t > buffer
Definition: zstream.h:58
std::basic_streambuf< Item_t, Traits_t > StreamBuf_t
Definition: zstream.h:30
bool zipToStream(char_type *buf, std::streamsize size)