• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

contrib/zlib/contrib/iostream3/zfstream.h

00001 /*
00002  * A C++ I/O streams interface to the zlib gz* functions
00003  *
00004  * by Ludwig Schwardt <schwardt@sun.ac.za>
00005  * original version by Kevin Ruland <kevin@rodin.wustl.edu>
00006  *
00007  * This version is standard-compliant and compatible with gcc 3.x.
00008  */
00009 
00010 #ifndef ZFSTREAM_H
00011 #define ZFSTREAM_H
00012 
00013 #include <istream>  // not iostream, since we don't need cin/cout
00014 #include <ostream>
00015 #include "zlib.h"
00016 
00017 /*****************************************************************************/
00018 
00027 class gzfilebuf : public std::streambuf
00028 {
00029 public:
00030   //  Default constructor.
00031   gzfilebuf();
00032 
00033   //  Destructor.
00034   virtual
00035   ~gzfilebuf();
00036 
00048   int
00049   setcompression(int comp_level,
00050                  int comp_strategy = Z_DEFAULT_STRATEGY);
00051 
00056   bool
00057   is_open() const { return (file != NULL); }
00058 
00065   gzfilebuf*
00066   open(const char* name,
00067        std::ios_base::openmode mode);
00068 
00075   gzfilebuf*
00076   attach(int fd,
00077          std::ios_base::openmode mode);
00078 
00083   gzfilebuf*
00084   close();
00085 
00086 protected:
00091   bool
00092   open_mode(std::ios_base::openmode mode,
00093             char* c_mode) const;
00094 
00102   virtual std::streamsize
00103   showmanyc();
00104 
00112   virtual int_type
00113   underflow();
00114 
00124   virtual int_type
00125   overflow(int_type c = traits_type::eof());
00126 
00135   virtual std::streambuf*
00136   setbuf(char_type* p,
00137          std::streamsize n);
00138 
00145   virtual int
00146   sync();
00147 
00148 //
00149 // Some future enhancements
00150 //
00151 //  virtual int_type uflow();
00152 //  virtual int_type pbackfail(int_type c = traits_type::eof());
00153 //  virtual pos_type
00154 //  seekoff(off_type off,
00155 //          std::ios_base::seekdir way,
00156 //          std::ios_base::openmode mode = std::ios_base::in|std::ios_base::out);
00157 //  virtual pos_type
00158 //  seekpos(pos_type sp,
00159 //          std::ios_base::openmode mode = std::ios_base::in|std::ios_base::out);
00160 
00161 private:
00170   void
00171   enable_buffer();
00172 
00180   void
00181   disable_buffer();
00182 
00186   gzFile file;
00187 
00191   std::ios_base::openmode io_mode;
00192 
00199   bool own_fd;
00200 
00207   char_type* buffer;
00208 
00215   std::streamsize buffer_size;
00216 
00223   bool own_buffer;
00224 };
00225 
00226 /*****************************************************************************/
00227 
00234 class gzifstream : public std::istream
00235 {
00236 public:
00237   //  Default constructor
00238   gzifstream();
00239 
00245   explicit
00246   gzifstream(const char* name,
00247              std::ios_base::openmode mode = std::ios_base::in);
00248 
00254   explicit
00255   gzifstream(int fd,
00256              std::ios_base::openmode mode = std::ios_base::in);
00257 
00261   gzfilebuf*
00262   rdbuf() const
00263   { return const_cast<gzfilebuf*>(&sb); }
00264 
00269   bool
00270   is_open() { return sb.is_open(); }
00271 
00284   void
00285   open(const char* name,
00286        std::ios_base::openmode mode = std::ios_base::in);
00287 
00296   void
00297   attach(int fd,
00298          std::ios_base::openmode mode = std::ios_base::in);
00299 
00305   void
00306   close();
00307 
00308 private:
00312   gzfilebuf sb;
00313 };
00314 
00315 /*****************************************************************************/
00316 
00323 class gzofstream : public std::ostream
00324 {
00325 public:
00326   //  Default constructor
00327   gzofstream();
00328 
00334   explicit
00335   gzofstream(const char* name,
00336              std::ios_base::openmode mode = std::ios_base::out);
00337 
00343   explicit
00344   gzofstream(int fd,
00345              std::ios_base::openmode mode = std::ios_base::out);
00346 
00350   gzfilebuf*
00351   rdbuf() const
00352   { return const_cast<gzfilebuf*>(&sb); }
00353 
00358   bool
00359   is_open() { return sb.is_open(); }
00360 
00373   void
00374   open(const char* name,
00375        std::ios_base::openmode mode = std::ios_base::out);
00376 
00385   void
00386   attach(int fd,
00387          std::ios_base::openmode mode = std::ios_base::out);
00388 
00394   void
00395   close();
00396 
00397 private:
00401   gzfilebuf sb;
00402 };
00403 
00404 /*****************************************************************************/
00405 
00412 template<typename T1, typename T2>
00413   class gzomanip2
00414   {
00415   public:
00416     // Allows insertor to peek at internals
00417     template <typename Ta, typename Tb>
00418       friend gzofstream&
00419       operator<<(gzofstream&,
00420                  const gzomanip2<Ta,Tb>&);
00421 
00422     // Constructor
00423     gzomanip2(gzofstream& (*f)(gzofstream&, T1, T2),
00424               T1 v1,
00425               T2 v2);
00426   private:
00427     // Underlying manipulator function
00428     gzofstream&
00429     (*func)(gzofstream&, T1, T2);
00430 
00431     // Arguments for manipulator function
00432     T1 val1;
00433     T2 val2;
00434   };
00435 
00436 /*****************************************************************************/
00437 
00438 // Manipulator function thunks through to stream buffer
00439 inline gzofstream&
00440 setcompression(gzofstream &gzs, int l, int s = Z_DEFAULT_STRATEGY)
00441 {
00442   (gzs.rdbuf())->setcompression(l, s);
00443   return gzs;
00444 }
00445 
00446 // Manipulator constructor stores arguments
00447 template<typename T1, typename T2>
00448   inline
00449   gzomanip2<T1,T2>::gzomanip2(gzofstream &(*f)(gzofstream &, T1, T2),
00450                               T1 v1,
00451                               T2 v2)
00452   : func(f), val1(v1), val2(v2)
00453   { }
00454 
00455 // Insertor applies underlying manipulator function to stream
00456 template<typename T1, typename T2>
00457   inline gzofstream&
00458   operator<<(gzofstream& s, const gzomanip2<T1,T2>& m)
00459   { return (*m.func)(s, m.val1, m.val2); }
00460 
00461 // Insert this onto stream to simplify setting of compression level
00462 inline gzomanip2<int,int>
00463 setcompression(int l, int s = Z_DEFAULT_STRATEGY)
00464 { return gzomanip2<int,int>(&setcompression, l, s); }
00465 
00466 #endif // ZFSTREAM_H

Generated on Wed Oct 20 2010 11:12:17 for APBS by  doxygen 1.7.2