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

zipstrstream.h

00001 /*  zipstrstream.cpp - 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_zipstrstream_h
00023 #define OW32_zipstrstream_h
00024 
00025 #include <iostream>
00026 #include <zlib.h>
00027 #include <OW32/auto_buf.h>
00028 #include <OW32/OW32Libs.h>
00029 
00030 namespace OW32
00031 {
00032 
00033 // Unfortunately VC++ can't cope without these using declarations because 
00034 // if you try deriving from a class with an explicit namespace then you
00035 // can't initialise it in the constructor like normal - just a bug i guess
00036 using std::streambuf;
00037 using std::ostream;
00038 using std::istream;
00039 using std::streamsize;
00040 
00041 #ifdef _MSC_VER
00042 #pragma warning(disable: 4251)
00043 #endif
00044 
00046 class OW32_LIB_EXPORT zipstrstreambuf : public streambuf
00047 {
00048 public:
00049     void freeze(bool frz = true) const;
00050     char* str() { return (char*)m_storage.str(); }
00051     streamsize pcount() const { return (streamsize)m_flateStream.total_out; }
00052 
00053     zipstrstreambuf(const char* in, int length);
00054     zipstrstreambuf();
00055     ~zipstrstreambuf();
00056 
00057 protected:
00058     virtual int sync();
00059     virtual streamsize xsgetn(char *s, streamsize n);
00060     virtual int_type underflow();
00061     virtual streamsize xsputn(const char *s, streamsize n);
00062     virtual int_type overflow(int_type c);// = char_traits<char>::eof());
00063 
00064 private:
00065     enum { CHUNK = 2048 }; // TODO: find optimal value
00066 
00067     int m_flags;
00068     z_stream m_flateStream;
00069     OW32::auto_byte_buf m_storage;
00070 
00071     void expandFlateStream();
00072     void Init();
00073 };
00074 #ifdef _MSC_VER
00075 #pragma warning(default: 4251)
00076 #endif
00077 
00079 class OW32_LIB_EXPORT ozipstrstream
00080     : public ostream
00081     {   // output stream associated with a character array
00082 public:
00083     ozipstrstream()
00084         : ostream(&_Mysb), _Mysb()
00085         {   // construct with empty character array
00086         }
00087 
00088     ozipstrstream(char *, streamsize,
00089         ios_base::openmode =
00090             ios_base::out); // construct with static array
00091 
00092 //  _CRTIMP2 virtual ~ozipstrstream();  // destroy the object
00093 
00094     zipstrstreambuf *rdbuf() const
00095         {   // return pointer to character array buffer
00096         return ((zipstrstreambuf *)&_Mysb);
00097         }
00098 
00099     void freeze(bool _Freezeit = true)
00100         {   // freeze or unfreeze writing
00101         _Mysb.freeze(_Freezeit);
00102         }
00103 
00104     char *str()
00105         {   // freeze and return pointer to character array 
00106         flush();
00107         return (_Mysb.str());
00108         }
00109 
00110     streamsize pcount() const
00111         {   // return size of writable character array
00112         return (_Mysb.pcount());
00113         }
00114 
00115 private:
00116     ozipstrstream(const ozipstrstream& );
00117     ozipstrstream& operator=(const ozipstrstream& );
00118 
00119     zipstrstreambuf _Mysb;  // the character array buffer
00120     };
00121 
00123 class OW32_LIB_EXPORT izipstrstream
00124     : public istream
00125     {   // input stream associated with a character array
00126 public:
00127 
00128     /*explicit istrstream(const char *_Ptr)
00129         : istream(&_Mysb), _Mysb(_Ptr, 0)
00130         {   // construct with NTBS
00131         }*/
00132 
00133     izipstrstream(const char *_Ptr, streamsize _Count)
00134         : istream(&_Mysb), _Mysb(_Ptr, _Count)
00135         {   // construct with [_Ptr, _Ptr + _Count)
00136         }
00137 
00138 /*  explicit istrstream(char *_Ptr)
00139         : istream(&_Mysb), _Mysb((const char *)_Ptr, 0)
00140         {   // construct with NTBS
00141         }*/
00142 
00143     izipstrstream(char *_Ptr, int _Count)
00144         : istream(&_Mysb), _Mysb((const char *)_Ptr, _Count)
00145         {   // construct with [_Ptr, _Ptr + _Count)
00146         }
00147 
00148 //  _CRTIMP2 virtual ~istrstream(); // destroy the object
00149 
00150     zipstrstreambuf *rdbuf() const
00151         {   // return pointer to character array buffer
00152         return ((zipstrstreambuf *)&_Mysb);
00153         }
00154 
00155     char *str()
00156         {   // freeze and return pointer to character array
00157         return (_Mysb.str());
00158         }
00159 
00160 private:
00161     izipstrstream(const izipstrstream& );
00162     izipstrstream& operator=(const izipstrstream& );
00163 
00164     zipstrstreambuf _Mysb;  // the string buffer
00165     };
00166 
00167 };
00168 
00169 #endif // OW32_zipstrstream_h

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