CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/PhysicsTools/MVAComputer/interface/zstream.h

Go to the documentation of this file.
00001 #ifndef PhysicsTools_MVAComputer_zstream_h
00002 #define PhysicsTools_MVAComputer_zstream_h
00003 // -*- C++ -*-
00004 //
00005 // Package:     MVAComputer
00006 //
00007 
00008 //
00009 // Author:      Christophe Saout <christophe.saout@cern.ch>
00010 // Created:     Sat Apr 24 15:18 CEST 2007
00011 // $Id: zstream.h,v 1.3 2007/07/15 22:31:46 saout Exp $
00012 //
00013 
00014 #include <iostream>
00015 #include <vector>
00016 
00017 #include <zlib.h>
00018 
00019 namespace ext {
00020 
00021 // implements STL stream wrappers around other streams
00022 // to provide transparent gzip compression/decompression
00023 //
00024 // constructors take reference to an existing stream, rest is straightforward
00025 
00026 template<typename Item_t, typename Traits_t = std::char_traits<Item_t>,
00027          typename Allocator_t = std::allocator<Item_t> >
00028 class basic_ozstreambuf : public std::basic_streambuf<Item_t, Traits_t> {
00029     public:
00030         typedef std::basic_ostream<Item_t, Traits_t>    OStream_t;
00031         typedef std::basic_streambuf<Item_t, Traits_t>  StreamBuf_t;
00032 
00033         typedef Item_t                                  char_type;
00034         typedef typename Traits_t::int_type             int_type;
00035         typedef typename Traits_t::pos_type             pos_type;
00036         typedef typename Traits_t::off_type             off_type;
00037         typedef unsigned char                           byte_type;
00038         typedef Traits_t                                traits_type;
00039 
00040         basic_ozstreambuf(OStream_t *os, int level);
00041         ~basic_ozstreambuf();
00042 
00043         using StreamBuf_t::pbase;
00044         using StreamBuf_t::pptr;
00045         using StreamBuf_t::epptr;
00046 
00047         int sync();
00048         int_type overflow(int_type c);
00049         std::streamsize flush();
00050 
00051     private:
00052         bool zipToStream(char_type *buf, std::streamsize size);
00053         size_t fillInputBuffer();
00054 
00055         OStream_t                               *os;
00056         z_stream                                zipStream;
00057         int                                     err;
00058         std::vector<byte_type>                  outputBuffer;
00059         std::vector<char_type, Allocator_t>     buffer;
00060 };
00061 
00062 template<typename Item_t, typename Traits_t = std::char_traits<Item_t>,
00063          typename Allocator_t = std::allocator<Item_t> >
00064 class basic_izstreambuf : public std::basic_streambuf<Item_t, Traits_t> {
00065     public:
00066         typedef std::basic_istream<Item_t, Traits_t>    IStream_t;
00067         typedef std::basic_streambuf<Item_t, Traits_t>  StreamBuf_t;
00068 
00069         typedef Item_t                                  char_type;
00070         typedef typename Traits_t::int_type             int_type;
00071         typedef typename Traits_t::pos_type             pos_type;
00072         typedef typename Traits_t::off_type             off_type;
00073         typedef unsigned char                           byte_type;
00074         typedef Traits_t                                traits_type;
00075 
00076         basic_izstreambuf(IStream_t *is);
00077         ~basic_izstreambuf();
00078 
00079         using StreamBuf_t::gptr;
00080         using StreamBuf_t::egptr;
00081         using StreamBuf_t::eback;
00082 
00083         int_type underflow();
00084 
00085     private:
00086         void putbackFromZStream();
00087         std::streamsize unzipFromStream(char_type *buf, std::streamsize size);
00088         size_t fillInputBuffer();
00089 
00090         IStream_t                               *is;
00091         z_stream                                zipStream;
00092         int                                     err;
00093         std::vector<byte_type>                  inputBuffer;
00094         std::vector<char_type, Allocator_t>     buffer;
00095 };
00096 
00097 template<typename Item_t, typename Traits_t = std::char_traits<Item_t>,
00098          typename Allocator_t = std::allocator<Item_t> >
00099 class basic_ozstreambase : virtual public std::basic_ios<Item_t, Traits_t> {
00100     public:
00101         typedef std::basic_ostream<Item_t, Traits_t> OStream_t;
00102         typedef basic_ozstreambuf<Item_t, Traits_t, Allocator_t> ZOStreamBuf_t;
00103 
00104         basic_ozstreambase(OStream_t *os, int level) :
00105                 buffer(os, level) { init(&buffer); }
00106 
00107         ZOStreamBuf_t *rdbuf() { return &buffer; }
00108 
00109     private:
00110         ZOStreamBuf_t buffer;
00111 };
00112 
00113 template<typename Item_t, typename Traits_t = std::char_traits<Item_t>,
00114          typename Allocator_t = std::allocator<Item_t> >
00115 class basic_izstreambase : virtual public std::basic_ios<Item_t, Traits_t> {
00116     public:
00117         typedef std::basic_istream<Item_t, Traits_t> IStream_t;
00118         typedef basic_izstreambuf<Item_t, Traits_t, Allocator_t> ZIStreamBuf_t;
00119 
00120         basic_izstreambase(IStream_t *is) : buffer(is) { init(&buffer); }
00121 
00122         ZIStreamBuf_t *rdbuf() { return &buffer; }
00123 
00124     private:
00125         ZIStreamBuf_t buffer;
00126 };
00127 
00128 template<typename Item_t, typename Traits_t = std::char_traits<Item_t>,
00129          typename Allocator_t = std::allocator<Item_t> >
00130 class basic_ozstream : public basic_ozstreambase<Item_t, Traits_t, Allocator_t>,
00131                        public std::basic_ostream<Item_t, Traits_t> {
00132     public:
00133         typedef std::basic_ostream<Item_t, Traits_t> OStream_t;
00134         typedef basic_ozstreambase<Item_t, Traits_t, Allocator_t> ZOStreamBase_t;
00135 
00136         basic_ozstream(OStream_t *os, int open_mode = std::ios::out,
00137                        int level = 9) :
00138                 ZOStreamBase_t(os, level),
00139                 OStream_t(ZOStreamBase_t::rdbuf()) {}
00140         ~basic_ozstream() {}
00141 };
00142 
00143 template<typename Item_t, typename Traits_t = std::char_traits<Item_t>,
00144          typename Allocator_t = std::allocator<Item_t> >
00145 class basic_izstream : public basic_izstreambase<Item_t, Traits_t, Allocator_t>,
00146                        public std::basic_istream<Item_t, Traits_t> {
00147     public:
00148         typedef std::basic_istream<Item_t, Traits_t> IStream_t;
00149         typedef basic_izstreambase<Item_t, Traits_t, Allocator_t> ZIStreamBase_t;
00150 
00151         basic_izstream(IStream_t *is, int open_mode = std::ios::in) :
00152                 ZIStreamBase_t(is), IStream_t(ZIStreamBase_t::rdbuf()) {}
00153         ~basic_izstream() {}
00154 };
00155 
00156 typedef basic_ozstream<char> ozstream;
00157 typedef basic_ozstream<wchar_t> wozstream;
00158 typedef basic_izstream<char> izstream;
00159 typedef basic_izstream<wchar_t> wizstream;
00160 
00161 } // namespace ext
00162 
00163 #include "PhysicsTools/MVAComputer/interface/zstream.icc"
00164 
00165 #endif // PhysicsTools_MVAComputer_zstream_h