Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:41

0001 #ifndef PhysicsTools_MVAComputer_zstream_h
0002 #define PhysicsTools_MVAComputer_zstream_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     MVAComputer
0006 //
0007 
0008 //
0009 // Author:  Christophe Saout <christophe.saout@cern.ch>
0010 // Created:     Sat Apr 24 15:18 CEST 2007
0011 //
0012 
0013 #include <iostream>
0014 #include <vector>
0015 
0016 #include <zlib.h>
0017 
0018 namespace ext {
0019 
0020   // implements STL stream wrappers around other streams
0021   // to provide transparent gzip compression/decompression
0022   //
0023   // constructors take reference to an existing stream, rest is straightforward
0024 
0025   template <typename Item_t, typename Traits_t = std::char_traits<Item_t>, typename Allocator_t = std::allocator<Item_t> >
0026   class basic_ozstreambuf : public std::basic_streambuf<Item_t, Traits_t> {
0027   public:
0028     typedef std::basic_ostream<Item_t, Traits_t> OStream_t;
0029     typedef std::basic_streambuf<Item_t, Traits_t> StreamBuf_t;
0030 
0031     typedef Item_t char_type;
0032     typedef typename Traits_t::int_type int_type;
0033     typedef typename Traits_t::pos_type pos_type;
0034     typedef typename Traits_t::off_type off_type;
0035     typedef unsigned char byte_type;
0036     typedef Traits_t traits_type;
0037 
0038     basic_ozstreambuf(OStream_t *os, int level);
0039     ~basic_ozstreambuf() override;
0040 
0041     using StreamBuf_t::epptr;
0042     using StreamBuf_t::pbase;
0043     using StreamBuf_t::pptr;
0044 
0045     int sync() override;
0046     int_type overflow(int_type c) override;
0047     std::streamsize flush();
0048 
0049   private:
0050     bool zipToStream(char_type *buf, std::streamsize size);
0051     size_t fillInputBuffer();
0052 
0053     OStream_t *os;
0054     z_stream zipStream;
0055     int err;
0056     std::vector<byte_type> outputBuffer;
0057     std::vector<char_type, Allocator_t> buffer;
0058   };
0059 
0060   template <typename Item_t, typename Traits_t = std::char_traits<Item_t>, typename Allocator_t = std::allocator<Item_t> >
0061   class basic_izstreambuf : public std::basic_streambuf<Item_t, Traits_t> {
0062   public:
0063     typedef std::basic_istream<Item_t, Traits_t> IStream_t;
0064     typedef std::basic_streambuf<Item_t, Traits_t> StreamBuf_t;
0065 
0066     typedef Item_t char_type;
0067     typedef typename Traits_t::int_type int_type;
0068     typedef typename Traits_t::pos_type pos_type;
0069     typedef typename Traits_t::off_type off_type;
0070     typedef unsigned char byte_type;
0071     typedef Traits_t traits_type;
0072 
0073     basic_izstreambuf(IStream_t *is);
0074     ~basic_izstreambuf() override;
0075 
0076     using StreamBuf_t::eback;
0077     using StreamBuf_t::egptr;
0078     using StreamBuf_t::gptr;
0079 
0080     int_type underflow() override;
0081 
0082   private:
0083     void putbackFromZStream();
0084     std::streamsize unzipFromStream(char_type *buf, std::streamsize size);
0085     size_t fillInputBuffer();
0086 
0087     IStream_t *is;
0088     z_stream zipStream;
0089     int err;
0090     std::vector<byte_type> inputBuffer;
0091     std::vector<char_type, Allocator_t> buffer;
0092   };
0093 
0094   template <typename Item_t, typename Traits_t = std::char_traits<Item_t>, typename Allocator_t = std::allocator<Item_t> >
0095   class basic_ozstreambase : virtual public std::basic_ios<Item_t, Traits_t> {
0096   public:
0097     typedef std::basic_ostream<Item_t, Traits_t> OStream_t;
0098     typedef basic_ozstreambuf<Item_t, Traits_t, Allocator_t> ZOStreamBuf_t;
0099 
0100     basic_ozstreambase(OStream_t *os, int level) : buffer(os, level) { this->init(&buffer); }
0101 
0102     ZOStreamBuf_t *rdbuf() { return &buffer; }
0103 
0104   private:
0105     ZOStreamBuf_t buffer;
0106   };
0107 
0108   template <typename Item_t, typename Traits_t = std::char_traits<Item_t>, typename Allocator_t = std::allocator<Item_t> >
0109   class basic_izstreambase : virtual public std::basic_ios<Item_t, Traits_t> {
0110   public:
0111     typedef std::basic_istream<Item_t, Traits_t> IStream_t;
0112     typedef basic_izstreambuf<Item_t, Traits_t, Allocator_t> ZIStreamBuf_t;
0113 
0114     basic_izstreambase(IStream_t *is) : buffer(is) { this->init(&buffer); }
0115 
0116     ZIStreamBuf_t *rdbuf() { return &buffer; }
0117 
0118   private:
0119     ZIStreamBuf_t buffer;
0120   };
0121 
0122   template <typename Item_t, typename Traits_t = std::char_traits<Item_t>, typename Allocator_t = std::allocator<Item_t> >
0123   class basic_ozstream : public basic_ozstreambase<Item_t, Traits_t, Allocator_t>,
0124                          public std::basic_ostream<Item_t, Traits_t> {
0125   public:
0126     typedef std::basic_ostream<Item_t, Traits_t> OStream_t;
0127     typedef basic_ozstreambase<Item_t, Traits_t, Allocator_t> ZOStreamBase_t;
0128 
0129     basic_ozstream(OStream_t *os, int open_mode = std::ios::out, int level = 9)
0130         : ZOStreamBase_t(os, level), OStream_t(ZOStreamBase_t::rdbuf()) {}
0131     ~basic_ozstream() override {}
0132   };
0133 
0134   template <typename Item_t, typename Traits_t = std::char_traits<Item_t>, typename Allocator_t = std::allocator<Item_t> >
0135   class basic_izstream : public basic_izstreambase<Item_t, Traits_t, Allocator_t>,
0136                          public std::basic_istream<Item_t, Traits_t> {
0137   public:
0138     typedef std::basic_istream<Item_t, Traits_t> IStream_t;
0139     typedef basic_izstreambase<Item_t, Traits_t, Allocator_t> ZIStreamBase_t;
0140 
0141     basic_izstream(IStream_t *is, int open_mode = std::ios::in)
0142         : ZIStreamBase_t(is), IStream_t(ZIStreamBase_t::rdbuf()) {}
0143     ~basic_izstream() override {}
0144   };
0145 
0146   typedef basic_ozstream<char> ozstream;
0147   typedef basic_ozstream<wchar_t> wozstream;
0148   typedef basic_izstream<char> izstream;
0149   typedef basic_izstream<wchar_t> wizstream;
0150 
0151 }  // namespace ext
0152 
0153 #include "PhysicsTools/MVAComputer/interface/zstream.icc"
0154 
0155 #endif  // PhysicsTools_MVAComputer_zstream_h