00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <cstring>
00030 #include "gzstream.h"
00031
00032 #ifdef GZSTREAM_NAMESPACE
00033 namespace GZSTREAM_NAMESPACE {
00034 #endif
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 gzstreambuf* gzstreambuf::open( const char* name, int open_mode) {
00045 if ( is_open())
00046 return (gzstreambuf*)0;
00047 mode = open_mode;
00048
00049 if ((mode & std::ios::ate) || (mode & std::ios::app)
00050 || ((mode & std::ios::in) && (mode & std::ios::out)))
00051 return (gzstreambuf*)0;
00052 char fmode[10];
00053 char* fmodeptr = fmode;
00054 if ( mode & std::ios::in)
00055 *fmodeptr++ = 'r';
00056 else if ( mode & std::ios::out)
00057 *fmodeptr++ = 'w';
00058 *fmodeptr++ = 'b';
00059 *fmodeptr = '\0';
00060 file = gzopen( name, fmode);
00061 if (file == 0)
00062 return (gzstreambuf*)0;
00063 opened = 1;
00064 return this;
00065 }
00066
00067 gzstreambuf * gzstreambuf::close() {
00068 if ( is_open()) {
00069 sync();
00070 opened = 0;
00071 if ( gzclose( file) == Z_OK)
00072 return this;
00073 }
00074 return (gzstreambuf*)0;
00075 }
00076
00077 int gzstreambuf::underflow() {
00078 if ( gptr() && ( gptr() < egptr()))
00079 return * reinterpret_cast<unsigned char *>( gptr());
00080
00081 if ( ! (mode & std::ios::in) || ! opened)
00082 return EOF;
00083
00084 int n_putback = (int)(gptr() - eback());
00085 if ( n_putback > 4)
00086 n_putback = 4;
00087 memcpy( buffer + (4 - n_putback), gptr() - n_putback, n_putback);
00088
00089 int num = gzread( file, buffer+4, bufferSize-4);
00090 if (num <= 0)
00091 return EOF;
00092
00093
00094 setg( buffer + (4 - n_putback),
00095 buffer + 4,
00096 buffer + 4 + num);
00097
00098
00099 return * reinterpret_cast<unsigned char *>( gptr());
00100 }
00101
00102 int gzstreambuf::flush_buffer() {
00103
00104
00105 int w = (int)(pptr() - pbase());
00106 if ( gzwrite( file, pbase(), w) != w)
00107 return EOF;
00108 pbump( -w);
00109 return w;
00110 }
00111
00112 int gzstreambuf::overflow( int c) {
00113 if ( ! ( mode & std::ios::out) || ! opened)
00114 return EOF;
00115 if (c != EOF) {
00116 *pptr() = (char)c;
00117 pbump(1);
00118 }
00119 if ( flush_buffer() == EOF)
00120 return EOF;
00121 return c;
00122 }
00123
00124 int gzstreambuf::sync() {
00125
00126
00127
00128 if ( pptr() && pptr() > pbase()) {
00129 if ( flush_buffer() == EOF)
00130 return -1;
00131 }
00132 return 0;
00133 }
00134
00135
00136
00137
00138
00139 gzstreambase::gzstreambase( const char* name, int mode) {
00140 init( &buf);
00141 open( name, mode);
00142 }
00143
00144 gzstreambase::~gzstreambase() {
00145 buf.close();
00146 }
00147
00148 void gzstreambase::open( const char* name, int gz_open_mode) {
00149 if ( ! buf.open( name, gz_open_mode))
00150 clear( rdstate() | std::ios::badbit);
00151 }
00152
00153 void gzstreambase::close() {
00154 if ( buf.is_open())
00155 if ( ! buf.close())
00156 clear( rdstate() | std::ios::badbit);
00157 }
00158
00159 #ifdef GZSTREAM_NAMESPACE
00160 }
00161 #endif
00162
00163
00164