Chris@4: /* Chris@4: * A C++ I/O streams interface to the zlib gz* functions Chris@4: * Chris@4: * by Ludwig Schwardt Chris@4: * original version by Kevin Ruland Chris@4: * Chris@4: * This version is standard-compliant and compatible with gcc 3.x. Chris@4: */ Chris@4: Chris@4: #include "zfstream.h" Chris@4: #include // for strcpy, strcat, strlen (mode strings) Chris@4: #include // for BUFSIZ Chris@4: Chris@4: // Internal buffer sizes (default and "unbuffered" versions) Chris@4: #define BIGBUFSIZE BUFSIZ Chris@4: #define SMALLBUFSIZE 1 Chris@4: Chris@4: /*****************************************************************************/ Chris@4: Chris@4: // Default constructor Chris@4: gzfilebuf::gzfilebuf() Chris@4: : file(NULL), io_mode(std::ios_base::openmode(0)), own_fd(false), Chris@4: buffer(NULL), buffer_size(BIGBUFSIZE), own_buffer(true) Chris@4: { Chris@4: // No buffers to start with Chris@4: this->disable_buffer(); Chris@4: } Chris@4: Chris@4: // Destructor Chris@4: gzfilebuf::~gzfilebuf() Chris@4: { Chris@4: // Sync output buffer and close only if responsible for file Chris@4: // (i.e. attached streams should be left open at this stage) Chris@4: this->sync(); Chris@4: if (own_fd) Chris@4: this->close(); Chris@4: // Make sure internal buffer is deallocated Chris@4: this->disable_buffer(); Chris@4: } Chris@4: Chris@4: // Set compression level and strategy Chris@4: int Chris@4: gzfilebuf::setcompression(int comp_level, Chris@4: int comp_strategy) Chris@4: { Chris@4: return gzsetparams(file, comp_level, comp_strategy); Chris@4: } Chris@4: Chris@4: // Open gzipped file Chris@4: gzfilebuf* Chris@4: gzfilebuf::open(const char *name, Chris@4: std::ios_base::openmode mode) Chris@4: { Chris@4: // Fail if file already open Chris@4: if (this->is_open()) Chris@4: return NULL; Chris@4: // Don't support simultaneous read/write access (yet) Chris@4: if ((mode & std::ios_base::in) && (mode & std::ios_base::out)) Chris@4: return NULL; Chris@4: Chris@4: // Build mode string for gzopen and check it [27.8.1.3.2] Chris@4: char char_mode[6] = "\0\0\0\0\0"; Chris@4: if (!this->open_mode(mode, char_mode)) Chris@4: return NULL; Chris@4: Chris@4: // Attempt to open file Chris@4: if ((file = gzopen(name, char_mode)) == NULL) Chris@4: return NULL; Chris@4: Chris@4: // On success, allocate internal buffer and set flags Chris@4: this->enable_buffer(); Chris@4: io_mode = mode; Chris@4: own_fd = true; Chris@4: return this; Chris@4: } Chris@4: Chris@4: // Attach to gzipped file Chris@4: gzfilebuf* Chris@4: gzfilebuf::attach(int fd, Chris@4: std::ios_base::openmode mode) Chris@4: { Chris@4: // Fail if file already open Chris@4: if (this->is_open()) Chris@4: return NULL; Chris@4: // Don't support simultaneous read/write access (yet) Chris@4: if ((mode & std::ios_base::in) && (mode & std::ios_base::out)) Chris@4: return NULL; Chris@4: Chris@4: // Build mode string for gzdopen and check it [27.8.1.3.2] Chris@4: char char_mode[6] = "\0\0\0\0\0"; Chris@4: if (!this->open_mode(mode, char_mode)) Chris@4: return NULL; Chris@4: Chris@4: // Attempt to attach to file Chris@4: if ((file = gzdopen(fd, char_mode)) == NULL) Chris@4: return NULL; Chris@4: Chris@4: // On success, allocate internal buffer and set flags Chris@4: this->enable_buffer(); Chris@4: io_mode = mode; Chris@4: own_fd = false; Chris@4: return this; Chris@4: } Chris@4: Chris@4: // Close gzipped file Chris@4: gzfilebuf* Chris@4: gzfilebuf::close() Chris@4: { Chris@4: // Fail immediately if no file is open Chris@4: if (!this->is_open()) Chris@4: return NULL; Chris@4: // Assume success Chris@4: gzfilebuf* retval = this; Chris@4: // Attempt to sync and close gzipped file Chris@4: if (this->sync() == -1) Chris@4: retval = NULL; Chris@4: if (gzclose(file) < 0) Chris@4: retval = NULL; Chris@4: // File is now gone anyway (postcondition [27.8.1.3.8]) Chris@4: file = NULL; Chris@4: own_fd = false; Chris@4: // Destroy internal buffer if it exists Chris@4: this->disable_buffer(); Chris@4: return retval; Chris@4: } Chris@4: Chris@4: /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ Chris@4: Chris@4: // Convert int open mode to mode string Chris@4: bool Chris@4: gzfilebuf::open_mode(std::ios_base::openmode mode, Chris@4: char* c_mode) const Chris@4: { Chris@4: bool testb = mode & std::ios_base::binary; Chris@4: bool testi = mode & std::ios_base::in; Chris@4: bool testo = mode & std::ios_base::out; Chris@4: bool testt = mode & std::ios_base::trunc; Chris@4: bool testa = mode & std::ios_base::app; Chris@4: Chris@4: // Check for valid flag combinations - see [27.8.1.3.2] (Table 92) Chris@4: // Original zfstream hardcoded the compression level to maximum here... Chris@4: // Double the time for less than 1% size improvement seems Chris@4: // excessive though - keeping it at the default level Chris@4: // To change back, just append "9" to the next three mode strings Chris@4: if (!testi && testo && !testt && !testa) Chris@4: strcpy(c_mode, "w"); Chris@4: if (!testi && testo && !testt && testa) Chris@4: strcpy(c_mode, "a"); Chris@4: if (!testi && testo && testt && !testa) Chris@4: strcpy(c_mode, "w"); Chris@4: if (testi && !testo && !testt && !testa) Chris@4: strcpy(c_mode, "r"); Chris@4: // No read/write mode yet Chris@4: // if (testi && testo && !testt && !testa) Chris@4: // strcpy(c_mode, "r+"); Chris@4: // if (testi && testo && testt && !testa) Chris@4: // strcpy(c_mode, "w+"); Chris@4: Chris@4: // Mode string should be empty for invalid combination of flags Chris@4: if (strlen(c_mode) == 0) Chris@4: return false; Chris@4: if (testb) Chris@4: strcat(c_mode, "b"); Chris@4: return true; Chris@4: } Chris@4: Chris@4: // Determine number of characters in internal get buffer Chris@4: std::streamsize Chris@4: gzfilebuf::showmanyc() Chris@4: { Chris@4: // Calls to underflow will fail if file not opened for reading Chris@4: if (!this->is_open() || !(io_mode & std::ios_base::in)) Chris@4: return -1; Chris@4: // Make sure get area is in use Chris@4: if (this->gptr() && (this->gptr() < this->egptr())) Chris@4: return std::streamsize(this->egptr() - this->gptr()); Chris@4: else Chris@4: return 0; Chris@4: } Chris@4: Chris@4: // Fill get area from gzipped file Chris@4: gzfilebuf::int_type Chris@4: gzfilebuf::underflow() Chris@4: { Chris@4: // If something is left in the get area by chance, return it Chris@4: // (this shouldn't normally happen, as underflow is only supposed Chris@4: // to be called when gptr >= egptr, but it serves as error check) Chris@4: if (this->gptr() && (this->gptr() < this->egptr())) Chris@4: return traits_type::to_int_type(*(this->gptr())); Chris@4: Chris@4: // If the file hasn't been opened for reading, produce error Chris@4: if (!this->is_open() || !(io_mode & std::ios_base::in)) Chris@4: return traits_type::eof(); Chris@4: Chris@4: // Attempt to fill internal buffer from gzipped file Chris@4: // (buffer must be guaranteed to exist...) Chris@4: int bytes_read = gzread(file, buffer, buffer_size); Chris@4: // Indicates error or EOF Chris@4: if (bytes_read <= 0) Chris@4: { Chris@4: // Reset get area Chris@4: this->setg(buffer, buffer, buffer); Chris@4: return traits_type::eof(); Chris@4: } Chris@4: // Make all bytes read from file available as get area Chris@4: this->setg(buffer, buffer, buffer + bytes_read); Chris@4: Chris@4: // Return next character in get area Chris@4: return traits_type::to_int_type(*(this->gptr())); Chris@4: } Chris@4: Chris@4: // Write put area to gzipped file Chris@4: gzfilebuf::int_type Chris@4: gzfilebuf::overflow(int_type c) Chris@4: { Chris@4: // Determine whether put area is in use Chris@4: if (this->pbase()) Chris@4: { Chris@4: // Double-check pointer range Chris@4: if (this->pptr() > this->epptr() || this->pptr() < this->pbase()) Chris@4: return traits_type::eof(); Chris@4: // Add extra character to buffer if not EOF Chris@4: if (!traits_type::eq_int_type(c, traits_type::eof())) Chris@4: { Chris@4: *(this->pptr()) = traits_type::to_char_type(c); Chris@4: this->pbump(1); Chris@4: } Chris@4: // Number of characters to write to file Chris@4: int bytes_to_write = this->pptr() - this->pbase(); Chris@4: // Overflow doesn't fail if nothing is to be written Chris@4: if (bytes_to_write > 0) Chris@4: { Chris@4: // If the file hasn't been opened for writing, produce error Chris@4: if (!this->is_open() || !(io_mode & std::ios_base::out)) Chris@4: return traits_type::eof(); Chris@4: // If gzipped file won't accept all bytes written to it, fail Chris@4: if (gzwrite(file, this->pbase(), bytes_to_write) != bytes_to_write) Chris@4: return traits_type::eof(); Chris@4: // Reset next pointer to point to pbase on success Chris@4: this->pbump(-bytes_to_write); Chris@4: } Chris@4: } Chris@4: // Write extra character to file if not EOF Chris@4: else if (!traits_type::eq_int_type(c, traits_type::eof())) Chris@4: { Chris@4: // If the file hasn't been opened for writing, produce error Chris@4: if (!this->is_open() || !(io_mode & std::ios_base::out)) Chris@4: return traits_type::eof(); Chris@4: // Impromptu char buffer (allows "unbuffered" output) Chris@4: char_type last_char = traits_type::to_char_type(c); Chris@4: // If gzipped file won't accept this character, fail Chris@4: if (gzwrite(file, &last_char, 1) != 1) Chris@4: return traits_type::eof(); Chris@4: } Chris@4: Chris@4: // If you got here, you have succeeded (even if c was EOF) Chris@4: // The return value should therefore be non-EOF Chris@4: if (traits_type::eq_int_type(c, traits_type::eof())) Chris@4: return traits_type::not_eof(c); Chris@4: else Chris@4: return c; Chris@4: } Chris@4: Chris@4: // Assign new buffer Chris@4: std::streambuf* Chris@4: gzfilebuf::setbuf(char_type* p, Chris@4: std::streamsize n) Chris@4: { Chris@4: // First make sure stuff is sync'ed, for safety Chris@4: if (this->sync() == -1) Chris@4: return NULL; Chris@4: // If buffering is turned off on purpose via setbuf(0,0), still allocate one... Chris@4: // "Unbuffered" only really refers to put [27.8.1.4.10], while get needs at Chris@4: // least a buffer of size 1 (very inefficient though, therefore make it bigger?) Chris@4: // This follows from [27.5.2.4.3]/12 (gptr needs to point at something, it seems) Chris@4: if (!p || !n) Chris@4: { Chris@4: // Replace existing buffer (if any) with small internal buffer Chris@4: this->disable_buffer(); Chris@4: buffer = NULL; Chris@4: buffer_size = 0; Chris@4: own_buffer = true; Chris@4: this->enable_buffer(); Chris@4: } Chris@4: else Chris@4: { Chris@4: // Replace existing buffer (if any) with external buffer Chris@4: this->disable_buffer(); Chris@4: buffer = p; Chris@4: buffer_size = n; Chris@4: own_buffer = false; Chris@4: this->enable_buffer(); Chris@4: } Chris@4: return this; Chris@4: } Chris@4: Chris@4: // Write put area to gzipped file (i.e. ensures that put area is empty) Chris@4: int Chris@4: gzfilebuf::sync() Chris@4: { Chris@4: return traits_type::eq_int_type(this->overflow(), traits_type::eof()) ? -1 : 0; Chris@4: } Chris@4: Chris@4: /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ Chris@4: Chris@4: // Allocate internal buffer Chris@4: void Chris@4: gzfilebuf::enable_buffer() Chris@4: { Chris@4: // If internal buffer required, allocate one Chris@4: if (own_buffer && !buffer) Chris@4: { Chris@4: // Check for buffered vs. "unbuffered" Chris@4: if (buffer_size > 0) Chris@4: { Chris@4: // Allocate internal buffer Chris@4: buffer = new char_type[buffer_size]; Chris@4: // Get area starts empty and will be expanded by underflow as need arises Chris@4: this->setg(buffer, buffer, buffer); Chris@4: // Setup entire internal buffer as put area. Chris@4: // The one-past-end pointer actually points to the last element of the buffer, Chris@4: // so that overflow(c) can safely add the extra character c to the sequence. Chris@4: // These pointers remain in place for the duration of the buffer Chris@4: this->setp(buffer, buffer + buffer_size - 1); Chris@4: } Chris@4: else Chris@4: { Chris@4: // Even in "unbuffered" case, (small?) get buffer is still required Chris@4: buffer_size = SMALLBUFSIZE; Chris@4: buffer = new char_type[buffer_size]; Chris@4: this->setg(buffer, buffer, buffer); Chris@4: // "Unbuffered" means no put buffer Chris@4: this->setp(0, 0); Chris@4: } Chris@4: } Chris@4: else Chris@4: { Chris@4: // If buffer already allocated, reset buffer pointers just to make sure no Chris@4: // stale chars are lying around Chris@4: this->setg(buffer, buffer, buffer); Chris@4: this->setp(buffer, buffer + buffer_size - 1); Chris@4: } Chris@4: } Chris@4: Chris@4: // Destroy internal buffer Chris@4: void Chris@4: gzfilebuf::disable_buffer() Chris@4: { Chris@4: // If internal buffer exists, deallocate it Chris@4: if (own_buffer && buffer) Chris@4: { Chris@4: // Preserve unbuffered status by zeroing size Chris@4: if (!this->pbase()) Chris@4: buffer_size = 0; Chris@4: delete[] buffer; Chris@4: buffer = NULL; Chris@4: this->setg(0, 0, 0); Chris@4: this->setp(0, 0); Chris@4: } Chris@4: else Chris@4: { Chris@4: // Reset buffer pointers to initial state if external buffer exists Chris@4: this->setg(buffer, buffer, buffer); Chris@4: if (buffer) Chris@4: this->setp(buffer, buffer + buffer_size - 1); Chris@4: else Chris@4: this->setp(0, 0); Chris@4: } Chris@4: } Chris@4: Chris@4: /*****************************************************************************/ Chris@4: Chris@4: // Default constructor initializes stream buffer Chris@4: gzifstream::gzifstream() Chris@4: : std::istream(NULL), sb() Chris@4: { this->init(&sb); } Chris@4: Chris@4: // Initialize stream buffer and open file Chris@4: gzifstream::gzifstream(const char* name, Chris@4: std::ios_base::openmode mode) Chris@4: : std::istream(NULL), sb() Chris@4: { Chris@4: this->init(&sb); Chris@4: this->open(name, mode); Chris@4: } Chris@4: Chris@4: // Initialize stream buffer and attach to file Chris@4: gzifstream::gzifstream(int fd, Chris@4: std::ios_base::openmode mode) Chris@4: : std::istream(NULL), sb() Chris@4: { Chris@4: this->init(&sb); Chris@4: this->attach(fd, mode); Chris@4: } Chris@4: Chris@4: // Open file and go into fail() state if unsuccessful Chris@4: void Chris@4: gzifstream::open(const char* name, Chris@4: std::ios_base::openmode mode) Chris@4: { Chris@4: if (!sb.open(name, mode | std::ios_base::in)) Chris@4: this->setstate(std::ios_base::failbit); Chris@4: else Chris@4: this->clear(); Chris@4: } Chris@4: Chris@4: // Attach to file and go into fail() state if unsuccessful Chris@4: void Chris@4: gzifstream::attach(int fd, Chris@4: std::ios_base::openmode mode) Chris@4: { Chris@4: if (!sb.attach(fd, mode | std::ios_base::in)) Chris@4: this->setstate(std::ios_base::failbit); Chris@4: else Chris@4: this->clear(); Chris@4: } Chris@4: Chris@4: // Close file Chris@4: void Chris@4: gzifstream::close() Chris@4: { Chris@4: if (!sb.close()) Chris@4: this->setstate(std::ios_base::failbit); Chris@4: } Chris@4: Chris@4: /*****************************************************************************/ Chris@4: Chris@4: // Default constructor initializes stream buffer Chris@4: gzofstream::gzofstream() Chris@4: : std::ostream(NULL), sb() Chris@4: { this->init(&sb); } Chris@4: Chris@4: // Initialize stream buffer and open file Chris@4: gzofstream::gzofstream(const char* name, Chris@4: std::ios_base::openmode mode) Chris@4: : std::ostream(NULL), sb() Chris@4: { Chris@4: this->init(&sb); Chris@4: this->open(name, mode); Chris@4: } Chris@4: Chris@4: // Initialize stream buffer and attach to file Chris@4: gzofstream::gzofstream(int fd, Chris@4: std::ios_base::openmode mode) Chris@4: : std::ostream(NULL), sb() Chris@4: { Chris@4: this->init(&sb); Chris@4: this->attach(fd, mode); Chris@4: } Chris@4: Chris@4: // Open file and go into fail() state if unsuccessful Chris@4: void Chris@4: gzofstream::open(const char* name, Chris@4: std::ios_base::openmode mode) Chris@4: { Chris@4: if (!sb.open(name, mode | std::ios_base::out)) Chris@4: this->setstate(std::ios_base::failbit); Chris@4: else Chris@4: this->clear(); Chris@4: } Chris@4: Chris@4: // Attach to file and go into fail() state if unsuccessful Chris@4: void Chris@4: gzofstream::attach(int fd, Chris@4: std::ios_base::openmode mode) Chris@4: { Chris@4: if (!sb.attach(fd, mode | std::ios_base::out)) Chris@4: this->setstate(std::ios_base::failbit); Chris@4: else Chris@4: this->clear(); Chris@4: } Chris@4: Chris@4: // Close file Chris@4: void Chris@4: gzofstream::close() Chris@4: { Chris@4: if (!sb.close()) Chris@4: this->setstate(std::ios_base::failbit); Chris@4: }