zfstream.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2005-2012 Ludwig Schwardt, Kevin Ruland
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 /*
00024 
00025  This file is adapted from the zlib 1.2.2 contrib/iostream3 code,
00026  written by
00027 
00028    Ludwig Schwardt <schwardt@sun.ac.za>
00029    original version by Kevin Ruland <kevin@rodin.wustl.edu>
00030 
00031 */
00032 
00033 #ifndef ZFSTREAM_H
00034 #define ZFSTREAM_H
00035 
00036 #ifdef HAVE_ZLIB
00037 
00038 #include <iosfwd>
00039 
00040 #include "zlib.h"
00041 
00042 /*****************************************************************************/
00043 
00052 class gzfilebuf : public std::streambuf
00053 {
00054 public:
00055   //  Default constructor.
00056   gzfilebuf();
00057 
00058   //  Destructor.
00059   virtual
00060   ~gzfilebuf();
00061 
00073   int
00074   setcompression(int comp_level,
00075                  int comp_strategy = Z_DEFAULT_STRATEGY);
00076 
00081   bool
00082   is_open() const { return (file != 0); }
00083 
00090   gzfilebuf*
00091   open(const char* name,
00092        std::ios_base::openmode mode);
00093 
00100   gzfilebuf*
00101   attach(int fd,
00102          std::ios_base::openmode mode);
00103 
00108   gzfilebuf*
00109   close();
00110 
00111 protected:
00116   bool
00117   open_mode(std::ios_base::openmode mode,
00118             char* c_mode) const;
00119 
00127   virtual std::streamsize
00128   showmanyc();
00129 
00137   virtual int_type
00138   underflow();
00139 
00149   virtual int_type
00150   overflow(int_type c = traits_type::eof());
00151 
00160   virtual std::streambuf*
00161   setbuf(char_type* p,
00162          std::streamsize n);
00163 
00170   virtual int
00171   sync();
00172 
00178   virtual pos_type
00179   seekoff(off_type off, std::ios_base::seekdir way,
00180           std::ios_base::openmode mode =
00181           std::ios_base::in|std::ios_base::out);
00182 
00188   virtual pos_type
00189   seekpos(pos_type sp, std::ios_base::openmode mode =
00190           std::ios_base::in|std::ios_base::out);
00191 
00192   virtual int_type
00193   pbackfail (int_type c = traits_type::eof());
00194 
00195 //
00196 // Some future enhancements
00197 //
00198 //  virtual int_type uflow();
00199 //  virtual int_type pbackfail(int_type c = traits_type::eof());
00200 
00201 private:
00202 
00203   // No copying!
00204 
00205   gzfilebuf (const gzfilebuf&);
00206 
00207   gzfilebuf& operator = (const gzfilebuf&);
00208 
00217   void
00218   enable_buffer();
00219 
00227   void
00228   disable_buffer();
00229 
00233   gzFile file;
00234 
00238   std::ios_base::openmode io_mode;
00239 
00246   bool own_fd;
00247 
00254   char_type* buffer;
00255 
00262   std::streamsize buffer_size;
00263 
00270   bool own_buffer;
00271 };
00272 
00273 /*****************************************************************************/
00274 
00281 class gzifstream : public std::istream
00282 {
00283 public:
00284   //  Default constructor
00285   gzifstream();
00286 
00292   explicit
00293   gzifstream(const char* name,
00294              std::ios_base::openmode mode = std::ios_base::in);
00295 
00301   explicit
00302   gzifstream(int fd,
00303              std::ios_base::openmode mode = std::ios_base::in);
00304 
00308   gzfilebuf*
00309   rdbuf() const
00310   { return const_cast<gzfilebuf*>(&sb); }
00311 
00316   bool
00317   is_open() { return sb.is_open(); }
00318 
00331   void
00332   open(const char* name,
00333        std::ios_base::openmode mode = std::ios_base::in);
00334 
00343   void
00344   attach(int fd,
00345          std::ios_base::openmode mode = std::ios_base::in);
00346 
00352   void
00353   close();
00354 
00355 private:
00359   gzfilebuf sb;
00360 };
00361 
00362 /*****************************************************************************/
00363 
00370 class gzofstream : public std::ostream
00371 {
00372 public:
00373   //  Default constructor
00374   gzofstream();
00375 
00381   explicit
00382   gzofstream(const char* name,
00383              std::ios_base::openmode mode = std::ios_base::out);
00384 
00390   explicit
00391   gzofstream(int fd,
00392              std::ios_base::openmode mode = std::ios_base::out);
00393 
00397   gzfilebuf*
00398   rdbuf() const
00399   { return const_cast<gzfilebuf*>(&sb); }
00400 
00405   bool
00406   is_open() { return sb.is_open(); }
00407 
00420   void
00421   open(const char* name,
00422        std::ios_base::openmode mode = std::ios_base::out);
00423 
00432   void
00433   attach(int fd,
00434          std::ios_base::openmode mode = std::ios_base::out);
00435 
00441   void
00442   close();
00443 
00444 private:
00448   gzfilebuf sb;
00449 };
00450 
00451 /*****************************************************************************/
00452 
00459 template<typename T1, typename T2>
00460   class gzomanip2
00461   {
00462   public:
00463     // Allows insertor to peek at internals
00464     template <typename Ta, typename Tb>
00465       friend gzofstream&
00466       operator<<(gzofstream&,
00467                  const gzomanip2<Ta,Tb>&);
00468 
00469     // Constructor
00470     gzomanip2(gzofstream& (*f)(gzofstream&, T1, T2),
00471               T1 v1,
00472               T2 v2);
00473   private:
00474     // Underlying manipulator function
00475     gzofstream&
00476     (*func)(gzofstream&, T1, T2);
00477 
00478     // Arguments for manipulator function
00479     T1 val1;
00480     T2 val2;
00481   };
00482 
00483 /*****************************************************************************/
00484 
00485 // Manipulator function thunks through to stream buffer
00486 inline gzofstream&
00487 setcompression(gzofstream &gzs, int l, int s = Z_DEFAULT_STRATEGY)
00488 {
00489   (gzs.rdbuf())->setcompression(l, s);
00490   return gzs;
00491 }
00492 
00493 // Manipulator constructor stores arguments
00494 template<typename T1, typename T2>
00495   inline
00496   gzomanip2<T1,T2>::gzomanip2(gzofstream &(*f)(gzofstream &, T1, T2),
00497                               T1 v1,
00498                               T2 v2)
00499   : func(f), val1(v1), val2(v2)
00500   { }
00501 
00502 // Insertor applies underlying manipulator function to stream
00503 template<typename T1, typename T2>
00504   inline gzofstream&
00505   operator<<(gzofstream& s, const gzomanip2<T1,T2>& m)
00506   { return (*m.func)(s, m.val1, m.val2); }
00507 
00508 // Insert this onto stream to simplify setting of compression level
00509 inline gzomanip2<int,int>
00510 setcompression(int l, int s = Z_DEFAULT_STRATEGY)
00511 { return gzomanip2<int,int>(&setcompression, l, s); }
00512 
00513 #endif // HAVE_ZLIB
00514 
00515 #endif // ZFSTREAM_H
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines