4 gzfilebuf::gzfilebuf() :
10 gzfilebuf::~gzfilebuf() {
13 if ( own_file_descriptor )
18 gzfilebuf *gzfilebuf::open( const char *name,
27 if ( io_mode & ios::in ) {
30 } else if ( io_mode & ios::app ) {
38 if ( io_mode & ios::binary ) {
43 // Hard code the compression level
44 if ( io_mode & (ios::out|ios::app )) {
48 // Put the end-of-string indicator
51 if ( (file = gzopen(name, char_mode)) == NULL )
54 own_file_descriptor = 1;
60 gzfilebuf *gzfilebuf::attach( int file_descriptor,
69 if ( io_mode & ios::in ) {
72 } else if ( io_mode & ios::app ) {
80 if ( io_mode & ios::binary ) {
85 // Hard code the compression level
86 if ( io_mode & (ios::out|ios::app )) {
90 // Put the end-of-string indicator
93 if ( (file = gzdopen(file_descriptor, char_mode)) == NULL )
96 own_file_descriptor = 0;
102 gzfilebuf *gzfilebuf::close() {
116 int gzfilebuf::setcompressionlevel( int comp_level ) {
118 return gzsetparams(file, comp_level, -2);
122 int gzfilebuf::setcompressionstrategy( int comp_strategy ) {
124 return gzsetparams(file, -2, comp_strategy);
129 streampos gzfilebuf::seekoff( streamoff off, ios::seek_dir dir, int which ) {
131 return streampos(EOF);
135 int gzfilebuf::underflow() {
137 // If the file hasn't been opened for reading, error.
138 if ( !is_open() || !(mode & ios::in) )
141 // if a buffer doesn't exists, allocate one.
144 if ( (allocate()) == EOF )
151 return (unsigned char) *gptr();
153 if ( out_waiting() ) {
154 if ( flushbuf() == EOF )
160 // Attempt to fill the buffer.
162 int result = fillbuf();
163 if ( result == EOF ) {
169 return (unsigned char) *gptr();
173 int gzfilebuf::overflow( int c ) {
175 if ( !is_open() || !(mode & ios::out) )
179 if ( allocate() == EOF )
187 if (flushbuf() == EOF)
193 setp( base(), base() + bl);
206 int gzfilebuf::sync() {
218 int gzfilebuf::flushbuf() {
226 if ( gzwrite( file, q, n) < n )
235 int gzfilebuf::fillbuf() {
244 int t = gzread( file, p, required );
246 if ( t <= 0) return EOF;
248 setg( base(), base(), base()+t);
254 gzfilestream_common::gzfilestream_common() :
255 ios( gzfilestream_common::rdbuf() )
258 gzfilestream_common::~gzfilestream_common()
261 void gzfilestream_common::attach( int fd, int io_mode ) {
263 if ( !buffer.attach( fd, io_mode) )
264 clear( ios::failbit | ios::badbit );
270 void gzfilestream_common::open( const char *name, int io_mode ) {
272 if ( !buffer.open( name, io_mode ) )
273 clear( ios::failbit | ios::badbit );
279 void gzfilestream_common::close() {
281 if ( !buffer.close() )
282 clear( ios::failbit | ios::badbit );
286 gzfilebuf *gzfilestream_common::rdbuf()
291 gzifstream::gzifstream() :
292 ios( gzfilestream_common::rdbuf() )
294 clear( ios::badbit );
297 gzifstream::gzifstream( const char *name, int io_mode ) :
298 ios( gzfilestream_common::rdbuf() )
300 gzfilestream_common::open( name, io_mode );
303 gzifstream::gzifstream( int fd, int io_mode ) :
304 ios( gzfilestream_common::rdbuf() )
306 gzfilestream_common::attach( fd, io_mode );
309 gzifstream::~gzifstream() { }
311 gzofstream::gzofstream() :
312 ios( gzfilestream_common::rdbuf() )
314 clear( ios::badbit );
317 gzofstream::gzofstream( const char *name, int io_mode ) :
318 ios( gzfilestream_common::rdbuf() )
320 gzfilestream_common::open( name, io_mode );
323 gzofstream::gzofstream( int fd, int io_mode ) :
324 ios( gzfilestream_common::rdbuf() )
326 gzfilestream_common::attach( fd, io_mode );
329 gzofstream::~gzofstream() { }