zfstream.cc
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:13k
- /*
- * A C++ I/O streams interface to the zlib gz* functions
- *
- * by Ludwig Schwardt <schwardt@sun.ac.za>
- * original version by Kevin Ruland <kevin@rodin.wustl.edu>
- *
- * This version is standard-compliant and compatible with gcc 3.x.
- */
- #include "zfstream.h"
- #include <cstring> // for strcpy, strcat, strlen (mode strings)
- #include <cstdio> // for BUFSIZ
- // Internal buffer sizes (default and "unbuffered" versions)
- #define BIGBUFSIZE BUFSIZ
- #define SMALLBUFSIZE 1
- /*****************************************************************************/
- // Default constructor
- gzfilebuf::gzfilebuf()
- : file(NULL), io_mode(std::ios_base::openmode(0)), own_fd(false),
- buffer(NULL), buffer_size(BIGBUFSIZE), own_buffer(true)
- {
- // No buffers to start with
- this->disable_buffer();
- }
- // Destructor
- gzfilebuf::~gzfilebuf()
- {
- // Sync output buffer and close only if responsible for file
- // (i.e. attached streams should be left open at this stage)
- this->sync();
- if (own_fd)
- this->close();
- // Make sure internal buffer is deallocated
- this->disable_buffer();
- }
- // Set compression level and strategy
- int
- gzfilebuf::setcompression(int comp_level,
- int comp_strategy)
- {
- return gzsetparams(file, comp_level, comp_strategy);
- }
- // Open gzipped file
- gzfilebuf*
- gzfilebuf::open(const char *name,
- std::ios_base::openmode mode)
- {
- // Fail if file already open
- if (this->is_open())
- return NULL;
- // Don't support simultaneous read/write access (yet)
- if ((mode & std::ios_base::in) && (mode & std::ios_base::out))
- return NULL;
- // Build mode string for gzopen and check it [27.8.1.3.2]
- char char_mode[6] = "