3 #include "Misc/zfstream.hxx"
5 gzfilebuf::gzfilebuf() :
11 gzfilebuf::~gzfilebuf() {
14 if ( own_file_descriptor )
19 gzfilebuf *gzfilebuf::open( const char *name,
27 memset(char_mode,'\0',10);
30 if ( io_mode & ios::in ) {
33 } else if ( io_mode & ios::app ) {
41 if ( io_mode & ios::binary ) {
46 // Hard code the compression level
47 if ( io_mode & (ios::out|ios::app )) {
51 if ( (file = gzopen(name, char_mode)) == NULL )
54 own_file_descriptor = 1;
60 gzfilebuf *gzfilebuf::attach( int file_descriptor,
68 memset(char_mode,'\0',10);
71 if ( io_mode & ios::in ) {
74 } else if ( io_mode & ios::app ) {
82 if ( io_mode & ios::binary ) {
87 // Hard code the compression level
88 if ( io_mode & (ios::out|ios::app )) {
92 if ( (file = gzdopen(file_descriptor, char_mode)) == NULL )
95 own_file_descriptor = 0;
101 gzfilebuf *gzfilebuf::close() {
115 int gzfilebuf::setcompressionlevel( short comp_level ) {
117 return gzsetparams(file, comp_level, -2);
121 int gzfilebuf::setcompressionstrategy( short comp_strategy ) {
123 return gzsetparams(file, -2, comp_strategy);
128 streampos gzfilebuf::seekoff( streamoff off, ios::seek_dir dir, int which ) {
130 return streampos(EOF);
134 int gzfilebuf::underflow() {
136 // If the file hasn't been opened for reading, error.
137 if ( !is_open() || !(mode & ios::in) )
140 // if a buffer doesn't exists, allocate one.
143 if ( (allocate()) == EOF )
150 return (unsigned char) *gptr();
152 if ( out_waiting() ) {
153 if ( flushbuf() == EOF )
159 // Attempt to fill the buffer.
161 int result = fillbuf();
162 if ( result == EOF ) {
168 return (unsigned char) *gptr();
172 int gzfilebuf::overflow( int c ) {
174 if ( !is_open() || !(mode & ios::out) )
178 if ( allocate() == EOF )
186 if (flushbuf() == EOF)
192 setp( base(), base() + bl);
205 int gzfilebuf::sync() {
217 int gzfilebuf::flushbuf() {
225 if ( gzwrite( file, q, n) < n )
234 int gzfilebuf::fillbuf() {
243 int t = gzread( file, p, required );
245 if ( t <= 0) return EOF;
247 setg( base(), base(), base()+t);
253 gzfilestream_common::gzfilestream_common() :
254 ios( gzfilestream_common::rdbuf() )
257 gzfilestream_common::~gzfilestream_common()
260 void gzfilestream_common::attach( int fd, int io_mode ) {
262 if ( !buffer.attach( fd, io_mode) )
263 clear( ios::failbit | ios::badbit );
269 void gzfilestream_common::open( const char *name, int io_mode ) {
271 if ( !buffer.open( name, io_mode ) )
272 clear( ios::failbit | ios::badbit );
278 void gzfilestream_common::close() {
280 if ( !buffer.close() )
281 clear( ios::failbit | ios::badbit );
285 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() { }