Panda3D
|
00001 // Filename: zStreamBuf.h 00002 // Created by: drose (05Aug02) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #ifndef ZSTREAMBUF_H 00016 #define ZSTREAMBUF_H 00017 00018 #include "pandabase.h" 00019 00020 // This module is not compiled if zlib is not available. 00021 #ifdef HAVE_ZLIB 00022 00023 #include <zlib.h> 00024 00025 //////////////////////////////////////////////////////////////////// 00026 // Class : ZStreamBuf 00027 // Description : The streambuf object that implements 00028 // IDecompressStream and OCompressStream. 00029 //////////////////////////////////////////////////////////////////// 00030 class EXPCL_PANDAEXPRESS ZStreamBuf : public streambuf { 00031 public: 00032 ZStreamBuf(); 00033 virtual ~ZStreamBuf(); 00034 00035 void open_read(istream *source, bool owns_source); 00036 void close_read(); 00037 00038 void open_write(ostream *dest, bool owns_dest, int compression_level); 00039 void close_write(); 00040 00041 protected: 00042 virtual int overflow(int c); 00043 virtual int sync(); 00044 virtual int underflow(); 00045 00046 private: 00047 size_t read_chars(char *start, size_t length); 00048 void write_chars(const char *start, size_t length, int flush); 00049 void show_zlib_error(const char *function, int error_code, z_stream &z); 00050 00051 private: 00052 istream *_source; 00053 bool _owns_source; 00054 00055 ostream *_dest; 00056 bool _owns_dest; 00057 00058 z_stream _z_source; 00059 z_stream _z_dest; 00060 00061 char *_buffer; 00062 00063 // We need to store the decompression buffer on the class object, 00064 // because zlib might not consume all of the input characters at 00065 // each call to inflate(). This isn't a problem on output because 00066 // in that case we can afford to wait until it does consume all of 00067 // the characters we give it. 00068 enum { 00069 // It's not clear how large or small this buffer ought to be. It 00070 // doesn't seem to matter much, especially since this is just a 00071 // temporary holding area before getting copied into zlib's own 00072 // internal buffers. 00073 decompress_buffer_size = 128 00074 }; 00075 char decompress_buffer[decompress_buffer_size]; 00076 }; 00077 00078 #endif // HAVE_ZLIB 00079 00080 #endif