Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

zipfstream.h

00001 /*  zipfstream.h - zip iostream wrapper
00002     Copyright (C) 2001-2004 Mark Weaver
00003     Written by Mark Weaver <mark@npsl.co.uk>
00004 
00005     Part of the Open-Win32 library.
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public
00017     License along with this library; if not, write to the
00018     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019     Boston, MA  02111-1307, USA.
00020 */
00021 
00022 #ifndef OW32_zipfstream_h
00023 #define OW32_zipfstream_h
00024 
00025 #include <iostream>
00026 #include <zlib.h>
00027 #include <OW32/OW32Libs.h>
00028 
00029 namespace OW32
00030 {
00031 
00032 using std::streambuf;
00033 using std::ostream;
00034 using std::istream;
00035 using std::streamsize;
00036 using std::ios_base;
00037 
00039 class OW32_LIB_EXPORT zipfilebuf : public streambuf
00040 {
00041 public:
00042     zipfilebuf();
00043     zipfilebuf(const char* fname, 
00044         ios_base::openmode mode = ios_base::out | ios_base::trunc);
00045     ~zipfilebuf();
00046     bool open(const char* fname,
00047         ios_base::openmode mode = ios_base::out | ios_base::trunc);
00048 
00049 protected:
00050     virtual int sync();
00051     virtual int_type underflow();
00052     virtual int_type overflow(int_type c);// = char_traits<char>::eof());
00053 
00054 private:
00055     // TODO: find optimal chunk
00056     enum { CHUNK = 2048 };
00057 
00058     int m_flags;
00059     FILE* m_fp;
00060     z_stream m_flateStream;
00061     char m_in_buf[CHUNK]; // oh dear: dyn alloc?
00062     char m_storage[CHUNK];
00063     
00064     void Init();
00065 };
00066 
00068 class OW32_LIB_EXPORT ofzipstream : public ostream
00069     {   // output stream associated with a character array
00070 public:
00071     ofzipstream()
00072         : ostream(&_Mysb), _Mysb()
00073         {   // construct with empty character array
00074         }
00075 
00076     ofzipstream(const char *fname, 
00077         ios_base::openmode mode = ios_base::out | ios_base::trunc)
00078         : ostream(&_Mysb), _Mysb(fname, mode)
00079     {   // construct with file name
00080     }
00081 
00082 //  _CRTIMP2 virtual ~ofzipstream();    // destroy the object
00083 
00084     zipfilebuf *rdbuf() const
00085         {   // return pointer to character array buffer
00086         return ((zipfilebuf *)&_Mysb);
00087         }
00088 
00089 private:
00090     ofzipstream(const ofzipstream& );
00091     ofzipstream& operator=(const ofzipstream& );
00092 
00093     zipfilebuf _Mysb;   // the character array buffer
00094     };
00095 
00097 class OW32_LIB_EXPORT ifzipstream : public istream
00098     {   // output stream associated with a character array
00099 public:
00100     ifzipstream()
00101         : istream(&_Mysb), _Mysb()
00102         {   // construct with empty character array
00103         }
00104 
00105     ifzipstream(const char *fname, 
00106         ios_base::openmode mode = ios_base::in)
00107         : istream(&_Mysb), _Mysb(fname, mode)
00108     {   // construct with file name
00109     }
00110 
00111     void open(const char *fname, 
00112         ios_base::openmode mode = ios_base::in)
00113     {
00114         if (!_Mysb.open(fname, mode))
00115             setstate(ios_base::failbit);
00116     }
00117 //  _CRTIMP2 virtual ~ofzipstream();    // destroy the object
00118 
00119     zipfilebuf *rdbuf() const
00120         {   // return pointer to character array buffer
00121         return ((zipfilebuf *)&_Mysb);
00122         }
00123 
00124 private:
00125     ifzipstream(const ifzipstream& );
00126     ifzipstream& operator=(const ifzipstream& );
00127     zipfilebuf _Mysb;   // the character array buffer
00128     };
00129 
00130 } // namespace std
00131 
00132 #endif // OW32_zipfstream_h

Generated on Sun Jun 5 01:29:18 2005 for OW32 by  doxygen 1.3.9.1