00001 /** @file rutz/gzstreambuf.h handle gzip-encoding through a c++ 00002 iostreams interface */ 00003 00004 /////////////////////////////////////////////////////////////////////// 00005 // 00006 // Copyright (c) 2001-2004 California Institute of Technology 00007 // Copyright (c) 2004-2007 University of Southern California 00008 // Rob Peters <rjpeters at usc dot edu> 00009 // 00010 // created: Wed Jun 20 09:12:51 2001 00011 // commit: $Id: gzstreambuf.h 8249 2007-04-12 06:03:40Z rjpeters $ 00012 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/gzstreambuf.h $ 00013 // 00014 // -------------------------------------------------------------------- 00015 // 00016 // This file is part of GroovX. 00017 // [http://ilab.usc.edu/rjpeters/groovx/] 00018 // 00019 // GroovX is free software; you can redistribute it and/or modify it 00020 // under the terms of the GNU General Public License as published by 00021 // the Free Software Foundation; either version 2 of the License, or 00022 // (at your option) any later version. 00023 // 00024 // GroovX is distributed in the hope that it will be useful, but 00025 // WITHOUT ANY WARRANTY; without even the implied warranty of 00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00027 // General Public License for more details. 00028 // 00029 // You should have received a copy of the GNU General Public License 00030 // along with GroovX; if not, write to the Free Software Foundation, 00031 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 00032 // 00033 /////////////////////////////////////////////////////////////////////// 00034 00035 #ifndef GROOVX_RUTZ_GZSTREAMBUF_H_UTC20050626084020_DEFINED 00036 #define GROOVX_RUTZ_GZSTREAMBUF_H_UTC20050626084020_DEFINED 00037 00038 #include <istream> 00039 #include <ostream> 00040 #include <streambuf> 00041 #include <zlib.h> 00042 00043 namespace rutz 00044 { 00045 class fstring; 00046 00047 template <class T> class shared_ptr; 00048 00049 /// A std::streambuf implementation that handles gzip-encoded data. 00050 class gzstreambuf : public std::streambuf 00051 { 00052 private: 00053 bool m_opened; 00054 int m_mode; 00055 gzFile m_gzfile; 00056 00057 gzstreambuf(const gzstreambuf&); 00058 gzstreambuf& operator=(const gzstreambuf&); 00059 00060 static const int s_buf_size = 4092; 00061 static const int s_pback_size = 4; 00062 char m_buf[s_buf_size]; 00063 00064 int flushoutput(); 00065 00066 public: 00067 gzstreambuf(const char* name, int om, bool throw_exception=false); 00068 ~gzstreambuf() { close(); } 00069 00070 bool is_open() { return m_opened; } 00071 00072 void ensure_open(); 00073 00074 void close(); 00075 00076 virtual int underflow(); 00077 00078 virtual int overflow(int c); 00079 00080 virtual int sync(); 00081 }; 00082 00083 /** Opens a file for writing. An exception will be thrown if the 00084 specified file cannot be opened. The output file will be 00085 gz-compressed if the filename ends with ".gz". */ 00086 shared_ptr<std::ostream> ogzopen(const rutz::fstring& filename, 00087 std::ios::openmode flags = 00088 std::ios::openmode(0)); 00089 00090 /// Overload. 00091 shared_ptr<std::ostream> ogzopen(const char* filename, 00092 std::ios::openmode flags = 00093 std::ios::openmode(0)); 00094 00095 /** Opens a file for reading. An exception will be thrown if the 00096 specified file cannot be opened. If the file is gz-compressed, 00097 this will be automagically detected regardless of the filename 00098 extension. */ 00099 shared_ptr<std::istream> igzopen(const rutz::fstring& filename, 00100 std::ios::openmode flags = 00101 std::ios::openmode(0)); 00102 00103 /// Overload. 00104 shared_ptr<std::istream> igzopen(const char* filename, 00105 std::ios::openmode flags = 00106 std::ios::openmode(0)); 00107 } 00108 00109 static const char __attribute__((used)) vcid_groovx_rutz_gzstreambuf_h_utc20050626084020[] = "$Id: gzstreambuf.h 8249 2007-04-12 06:03:40Z rjpeters $ $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/gzstreambuf.h $"; 00110 #endif // !GROOVX_RUTZ_GZSTREAMBUF_H_UTC20050626084020_DEFINED