123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479 |
- #include "zfstream.h"
- #include <cstring> // for strcpy, strcat, strlen (mode strings)
- #include <cstdio> // for BUFSIZ
- #define BIGBUFSIZE BUFSIZ
- #define SMALLBUFSIZE 1
- gzfilebuf::gzfilebuf()
- : file(NULL), io_mode(std::ios_base::openmode(0)), own_fd(false),
- buffer(NULL), buffer_size(BIGBUFSIZE), own_buffer(true)
- {
-
- this->disable_buffer();
- }
- gzfilebuf::~gzfilebuf()
- {
-
-
- this->sync();
- if (own_fd)
- this->close();
-
- this->disable_buffer();
- }
- int
- gzfilebuf::setcompression(int comp_level,
- int comp_strategy)
- {
- return gzsetparams(file, comp_level, comp_strategy);
- }
- gzfilebuf*
- gzfilebuf::open(const char *name,
- std::ios_base::openmode mode)
- {
-
- if (this->is_open())
- return NULL;
-
- if ((mode & std::ios_base::in) && (mode & std::ios_base::out))
- return NULL;
-
- char char_mode[6] = "\0\0\0\0\0";
- if (!this->open_mode(mode, char_mode))
- return NULL;
-
- if ((file = gzopen(name, char_mode)) == NULL)
- return NULL;
-
- this->enable_buffer();
- io_mode = mode;
- own_fd = true;
- return this;
- }
- gzfilebuf*
- gzfilebuf::attach(int fd,
- std::ios_base::openmode mode)
- {
-
- if (this->is_open())
- return NULL;
-
- if ((mode & std::ios_base::in) && (mode & std::ios_base::out))
- return NULL;
-
- char char_mode[6] = "\0\0\0\0\0";
- if (!this->open_mode(mode, char_mode))
- return NULL;
-
- if ((file = gzdopen(fd, char_mode)) == NULL)
- return NULL;
-
- this->enable_buffer();
- io_mode = mode;
- own_fd = false;
- return this;
- }
- gzfilebuf*
- gzfilebuf::close()
- {
-
- if (!this->is_open())
- return NULL;
-
- gzfilebuf* retval = this;
-
- if (this->sync() == -1)
- retval = NULL;
- if (gzclose(file) < 0)
- retval = NULL;
-
- file = NULL;
- own_fd = false;
-
- this->disable_buffer();
- return retval;
- }
- bool
- gzfilebuf::open_mode(std::ios_base::openmode mode,
- char* c_mode) const
- {
- bool testb = mode & std::ios_base::binary;
- bool testi = mode & std::ios_base::in;
- bool testo = mode & std::ios_base::out;
- bool testt = mode & std::ios_base::trunc;
- bool testa = mode & std::ios_base::app;
-
-
-
-
-
- if (!testi && testo && !testt && !testa)
- strcpy(c_mode, "w");
- if (!testi && testo && !testt && testa)
- strcpy(c_mode, "a");
- if (!testi && testo && testt && !testa)
- strcpy(c_mode, "w");
- if (testi && !testo && !testt && !testa)
- strcpy(c_mode, "r");
-
-
- if (strlen(c_mode) == 0)
- return false;
- if (testb)
- strcat(c_mode, "b");
- return true;
- }
- std::streamsize
- gzfilebuf::showmanyc()
- {
-
- if (!this->is_open() || !(io_mode & std::ios_base::in))
- return -1;
-
- if (this->gptr() && (this->gptr() < this->egptr()))
- return std::streamsize(this->egptr() - this->gptr());
- else
- return 0;
- }
- gzfilebuf::int_type
- gzfilebuf::underflow()
- {
-
-
-
- if (this->gptr() && (this->gptr() < this->egptr()))
- return traits_type::to_int_type(*(this->gptr()));
-
- if (!this->is_open() || !(io_mode & std::ios_base::in))
- return traits_type::eof();
-
-
- int bytes_read = gzread(file, buffer, buffer_size);
-
- if (bytes_read <= 0)
- {
-
- this->setg(buffer, buffer, buffer);
- return traits_type::eof();
- }
-
- this->setg(buffer, buffer, buffer + bytes_read);
-
- return traits_type::to_int_type(*(this->gptr()));
- }
- gzfilebuf::int_type
- gzfilebuf::overflow(int_type c)
- {
-
- if (this->pbase())
- {
-
- if (this->pptr() > this->epptr() || this->pptr() < this->pbase())
- return traits_type::eof();
-
- if (!traits_type::eq_int_type(c, traits_type::eof()))
- {
- *(this->pptr()) = traits_type::to_char_type(c);
- this->pbump(1);
- }
-
- int bytes_to_write = this->pptr() - this->pbase();
-
- if (bytes_to_write > 0)
- {
-
- if (!this->is_open() || !(io_mode & std::ios_base::out))
- return traits_type::eof();
-
- if (gzwrite(file, this->pbase(), bytes_to_write) != bytes_to_write)
- return traits_type::eof();
-
- this->pbump(-bytes_to_write);
- }
- }
-
- else if (!traits_type::eq_int_type(c, traits_type::eof()))
- {
-
- if (!this->is_open() || !(io_mode & std::ios_base::out))
- return traits_type::eof();
-
- char_type last_char = traits_type::to_char_type(c);
-
- if (gzwrite(file, &last_char, 1) != 1)
- return traits_type::eof();
- }
-
-
- if (traits_type::eq_int_type(c, traits_type::eof()))
- return traits_type::not_eof(c);
- else
- return c;
- }
- std::streambuf*
- gzfilebuf::setbuf(char_type* p,
- std::streamsize n)
- {
-
- if (this->sync() == -1)
- return NULL;
-
-
-
-
- if (!p || !n)
- {
-
- this->disable_buffer();
- buffer = NULL;
- buffer_size = 0;
- own_buffer = true;
- this->enable_buffer();
- }
- else
- {
-
- this->disable_buffer();
- buffer = p;
- buffer_size = n;
- own_buffer = false;
- this->enable_buffer();
- }
- return this;
- }
- int
- gzfilebuf::sync()
- {
- return traits_type::eq_int_type(this->overflow(), traits_type::eof()) ? -1 : 0;
- }
- void
- gzfilebuf::enable_buffer()
- {
-
- if (own_buffer && !buffer)
- {
-
- if (buffer_size > 0)
- {
-
- buffer = new char_type[buffer_size];
-
- this->setg(buffer, buffer, buffer);
-
-
-
-
- this->setp(buffer, buffer + buffer_size - 1);
- }
- else
- {
-
- buffer_size = SMALLBUFSIZE;
- buffer = new char_type[buffer_size];
- this->setg(buffer, buffer, buffer);
-
- this->setp(0, 0);
- }
- }
- else
- {
-
-
- this->setg(buffer, buffer, buffer);
- this->setp(buffer, buffer + buffer_size - 1);
- }
- }
- void
- gzfilebuf::disable_buffer()
- {
-
- if (own_buffer && buffer)
- {
-
- if (!this->pbase())
- buffer_size = 0;
- delete[] buffer;
- buffer = NULL;
- this->setg(0, 0, 0);
- this->setp(0, 0);
- }
- else
- {
-
- this->setg(buffer, buffer, buffer);
- if (buffer)
- this->setp(buffer, buffer + buffer_size - 1);
- else
- this->setp(0, 0);
- }
- }
- gzifstream::gzifstream()
- : std::istream(NULL), sb()
- { this->init(&sb); }
- gzifstream::gzifstream(const char* name,
- std::ios_base::openmode mode)
- : std::istream(NULL), sb()
- {
- this->init(&sb);
- this->open(name, mode);
- }
- gzifstream::gzifstream(int fd,
- std::ios_base::openmode mode)
- : std::istream(NULL), sb()
- {
- this->init(&sb);
- this->attach(fd, mode);
- }
- void
- gzifstream::open(const char* name,
- std::ios_base::openmode mode)
- {
- if (!sb.open(name, mode | std::ios_base::in))
- this->setstate(std::ios_base::failbit);
- else
- this->clear();
- }
- void
- gzifstream::attach(int fd,
- std::ios_base::openmode mode)
- {
- if (!sb.attach(fd, mode | std::ios_base::in))
- this->setstate(std::ios_base::failbit);
- else
- this->clear();
- }
- void
- gzifstream::close()
- {
- if (!sb.close())
- this->setstate(std::ios_base::failbit);
- }
- gzofstream::gzofstream()
- : std::ostream(NULL), sb()
- { this->init(&sb); }
- gzofstream::gzofstream(const char* name,
- std::ios_base::openmode mode)
- : std::ostream(NULL), sb()
- {
- this->init(&sb);
- this->open(name, mode);
- }
- gzofstream::gzofstream(int fd,
- std::ios_base::openmode mode)
- : std::ostream(NULL), sb()
- {
- this->init(&sb);
- this->attach(fd, mode);
- }
- void
- gzofstream::open(const char* name,
- std::ios_base::openmode mode)
- {
- if (!sb.open(name, mode | std::ios_base::out))
- this->setstate(std::ios_base::failbit);
- else
- this->clear();
- }
- void
- gzofstream::attach(int fd,
- std::ios_base::openmode mode)
- {
- if (!sb.attach(fd, mode | std::ios_base::out))
- this->setstate(std::ios_base::failbit);
- else
- this->clear();
- }
- void
- gzofstream::close()
- {
- if (!sb.close())
- this->setstate(std::ios_base::failbit);
- }
|