zlib 1.2.0.5

This commit is contained in:
Mark Adler
2011-09-09 23:22:37 -07:00
parent 086e982175
commit 4b5a43a219
152 changed files with 14540 additions and 12588 deletions

View File

@@ -3,19 +3,19 @@ to do things like:
gzofstream outf("blah.gz");
outf << "These go into the gzip file " << 123 << endl;
It does this by deriving a specialized stream buffer for gzipped files, which is
the way Stroustrup would have done it. :->
the way Stroustrup would have done it. :->
The gzifstream and gzofstream classes were originally written by Kevin Ruland
and made available in the zlib contrib/iostream directory. The older version still
compiles under gcc 2.xx, but not under gcc 3.xx, which sparked the development of
compiles under gcc 2.xx, but not under gcc 3.xx, which sparked the development of
this version.
The new classes are as standard-compliant as possible, closely following the
approach of the standard library's fstream classes. It compiles under gcc versions
3.2 and 3.3, but not under gcc 2.xx. This is mainly due to changes in the standard
library naming scheme. The new version of gzifstream/gzofstream/gzfilebuf differs
The new classes are as standard-compliant as possible, closely following the
approach of the standard library's fstream classes. It compiles under gcc versions
3.2 and 3.3, but not under gcc 2.xx. This is mainly due to changes in the standard
library naming scheme. The new version of gzifstream/gzofstream/gzfilebuf differs
from the previous one in the following respects:
- added showmanyc
- added setbuf, with support for unbuffered output via setbuf(0,0)
@@ -23,7 +23,7 @@ from the previous one in the following respects:
- gzipped output file opened with default compression level instead of maximum level
- setcompressionlevel()/strategy() members replaced by single setcompression()
The code is provided "as is", with the permission to use, copy, modify, distribute
The code is provided "as is", with the permission to use, copy, modify, distribute
and sell it for any purpose without fee.
Ludwig Schwardt

View File

@@ -13,5 +13,5 @@ Possible upgrades to gzfilebuf:
- Check public interface to see which calls give problems
(due to dependence on library internals)
- Override operator<<(ostream&, gzfilebuf*) to allow direct copying
- Override operator<<(ostream&, gzfilebuf*) to allow direct copying
of stream buffer to stream ( i.e. os << is.rdbuf(); )

View File

@@ -1,6 +1,6 @@
/*
* Test program for gzifstream and gzofstream
*
*
* by Ludwig Schwardt <schwardt@sun.ac.za>
* original version by Kevin Ruland <kevin@rodin.wustl.edu>
*/
@@ -13,15 +13,15 @@ int main() {
gzofstream outf;
gzifstream inf;
char buf[80];
outf.open("test1.txt.gz");
outf << "The quick brown fox sidestepped the lazy canine\n"
outf << "The quick brown fox sidestepped the lazy canine\n"
<< 1.3 << "\nPlan " << 9 << std::endl;
outf.close();
std::cout << "Wrote the following message to 'test1.txt.gz' (check with zcat or zless):\n"
<< "The quick brown fox sidestepped the lazy canine\n"
<< 1.3 << "\nPlan " << 9 << std::endl;
std::cout << "\nReading 'test1.txt.gz' (buffered) produces:\n";
inf.open("test1.txt.gz");
while (inf.getline(buf,80,'\n')) {

View File

@@ -1,9 +1,9 @@
/*
* 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.
*/
@@ -18,8 +18,8 @@
/*****************************************************************************/
// Default constructor
gzfilebuf::gzfilebuf()
: file(NULL), io_mode(std::ios_base::openmode(0)), own_fd(false),
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
@@ -27,12 +27,12 @@ gzfilebuf::gzfilebuf()
}
// Destructor
gzfilebuf::~gzfilebuf()
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)
if (own_fd)
this->close();
// Make sure internal buffer is deallocated
this->disable_buffer();
@@ -41,30 +41,30 @@ gzfilebuf::~gzfilebuf()
// Set compression level and strategy
int
gzfilebuf::setcompression(int comp_level,
int comp_strategy)
int comp_strategy)
{
return gzsetparams(file, comp_level, comp_strategy);
}
// Open gzipped file
gzfilebuf*
gzfilebuf::open(const char *name,
std::ios_base::openmode mode)
gzfilebuf*
gzfilebuf::open(const char *name,
std::ios_base::openmode mode)
{
// Fail if file already open
if (this->is_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))
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] = "\0\0\0\0\0";
if (!this->open_mode(mode, char_mode))
return NULL;
// Attempt to open file
if ((file = gzopen(name, char_mode)) == NULL)
if ((file = gzopen(name, char_mode)) == NULL)
return NULL;
// On success, allocate internal buffer and set flags
@@ -77,24 +77,24 @@ gzfilebuf::open(const char *name,
// Attach to gzipped file
gzfilebuf*
gzfilebuf::attach(int fd,
std::ios_base::openmode mode)
std::ios_base::openmode mode)
{
// Fail if file already open
if (this->is_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))
if ((mode & std::ios_base::in) && (mode & std::ios_base::out))
return NULL;
// Build mode string for gzdopen and check it [27.8.1.3.2]
char char_mode[6] = "\0\0\0\0\0";
if (!this->open_mode(mode, char_mode))
return NULL;
// Attempt to attach to file
if ((file = gzdopen(fd, char_mode)) == NULL)
return NULL;
// On success, allocate internal buffer and set flags
this->enable_buffer();
io_mode = mode;
@@ -104,7 +104,7 @@ gzfilebuf::attach(int fd,
// Close gzipped file
gzfilebuf*
gzfilebuf::close()
gzfilebuf::close()
{
// Fail immediately if no file is open
if (!this->is_open())
@@ -127,16 +127,16 @@ gzfilebuf::close()
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// Convert int open mode to mode string
bool
gzfilebuf::open_mode(std::ios_base::openmode mode,
char* c_mode) const
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;
// Check for valid flag combinations - see [27.8.1.3.2] (Table 92)
// Original zfstream hardcoded the compression level to maximum here...
// Double the time for less than 1% size improvement seems
@@ -156,7 +156,7 @@ gzfilebuf::open_mode(std::ios_base::openmode mode,
// if (testi && testo && testt && !testa)
// strcpy(c_mode, "w+");
// Mode string should be empty for invalid combination of flags
// Mode string should be empty for invalid combination of flags
if (strlen(c_mode) == 0)
return false;
if (testb)
@@ -165,7 +165,7 @@ gzfilebuf::open_mode(std::ios_base::openmode mode,
}
// Determine number of characters in internal get buffer
std::streamsize
std::streamsize
gzfilebuf::showmanyc()
{
// Calls to underflow will fail if file not opened for reading
@@ -180,10 +180,10 @@ gzfilebuf::showmanyc()
// Fill get area from gzipped file
gzfilebuf::int_type
gzfilebuf::underflow()
gzfilebuf::underflow()
{
// If something is left in the get area by chance, return it
// (this shouldn't normally happen, as underflow is only supposed
// (this shouldn't normally happen, as underflow is only supposed
// to be called when gptr >= egptr, but it serves as error check)
if (this->gptr() && (this->gptr() < this->egptr()))
return traits_type::to_int_type(*(this->gptr()));
@@ -204,14 +204,14 @@ gzfilebuf::underflow()
}
// Make all bytes read from file available as get area
this->setg(buffer, buffer, buffer + bytes_read);
// Return next character in get area
return traits_type::to_int_type(*(this->gptr()));
}
// Write put area to gzipped file
gzfilebuf::int_type
gzfilebuf::overflow(int_type c)
gzfilebuf::overflow(int_type c)
{
// Determine whether put area is in use
if (this->pbase())
@@ -232,10 +232,10 @@ gzfilebuf::overflow(int_type c)
{
// If the file hasn't been opened for writing, produce error
if (!this->is_open() || !(io_mode & std::ios_base::out))
return traits_type::eof();
return traits_type::eof();
// If gzipped file won't accept all bytes written to it, fail
if (gzwrite(file, this->pbase(), bytes_to_write) != bytes_to_write)
return traits_type::eof();
return traits_type::eof();
// Reset next pointer to point to pbase on success
this->pbump(-bytes_to_write);
}
@@ -250,7 +250,7 @@ gzfilebuf::overflow(int_type c)
char_type last_char = traits_type::to_char_type(c);
// If gzipped file won't accept this character, fail
if (gzwrite(file, &last_char, 1) != 1)
return traits_type::eof();
return traits_type::eof();
}
// If you got here, you have succeeded (even if c was EOF)
@@ -262,9 +262,9 @@ gzfilebuf::overflow(int_type c)
}
// Assign new buffer
std::streambuf*
std::streambuf*
gzfilebuf::setbuf(char_type* p,
std::streamsize n)
std::streamsize n)
{
// First make sure stuff is sync'ed, for safety
if (this->sync() == -1)
@@ -295,8 +295,8 @@ gzfilebuf::setbuf(char_type* p,
}
// Write put area to gzipped file (i.e. ensures that put area is empty)
int
gzfilebuf::sync()
int
gzfilebuf::sync()
{
return traits_type::eq_int_type(this->overflow(), traits_type::eof()) ? -1 : 0;
}
@@ -304,11 +304,11 @@ gzfilebuf::sync()
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// Allocate internal buffer
void
gzfilebuf::enable_buffer()
void
gzfilebuf::enable_buffer()
{
// If internal buffer required, allocate one
if (own_buffer && !buffer)
if (own_buffer && !buffer)
{
// Check for buffered vs. "unbuffered"
if (buffer_size > 0)
@@ -335,16 +335,16 @@ gzfilebuf::enable_buffer()
}
else
{
// If buffer already allocated, reset buffer pointers just to make sure no
// If buffer already allocated, reset buffer pointers just to make sure no
// stale chars are lying around
this->setg(buffer, buffer, buffer);
this->setp(buffer, buffer + buffer_size - 1);
}
}
}
// Destroy internal buffer
void
gzfilebuf::disable_buffer()
void
gzfilebuf::disable_buffer()
{
// If internal buffer exists, deallocate it
if (own_buffer && buffer)
@@ -371,13 +371,13 @@ gzfilebuf::disable_buffer()
/*****************************************************************************/
// Default constructor initializes stream buffer
gzifstream::gzifstream()
gzifstream::gzifstream()
: std::istream(NULL), sb()
{ this->init(&sb); }
// Initialize stream buffer and open file
gzifstream::gzifstream(const char* name,
std::ios_base::openmode mode)
std::ios_base::openmode mode)
: std::istream(NULL), sb()
{
this->init(&sb);
@@ -386,7 +386,7 @@ gzifstream::gzifstream(const char* name,
// Initialize stream buffer and attach to file
gzifstream::gzifstream(int fd,
std::ios_base::openmode mode)
std::ios_base::openmode mode)
: std::istream(NULL), sb()
{
this->init(&sb);
@@ -394,9 +394,9 @@ gzifstream::gzifstream(int fd,
}
// Open file and go into fail() state if unsuccessful
void
gzifstream::open(const char* name,
std::ios_base::openmode 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);
@@ -405,9 +405,9 @@ gzifstream::open(const char* name,
}
// Attach to file and go into fail() state if unsuccessful
void
gzifstream::attach(int fd,
std::ios_base::openmode mode)
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);
@@ -416,7 +416,7 @@ gzifstream::attach(int fd,
}
// Close file
void
void
gzifstream::close()
{
if (!sb.close())
@@ -426,13 +426,13 @@ gzifstream::close()
/*****************************************************************************/
// Default constructor initializes stream buffer
gzofstream::gzofstream()
gzofstream::gzofstream()
: std::ostream(NULL), sb()
{ this->init(&sb); }
// Initialize stream buffer and open file
gzofstream::gzofstream(const char* name,
std::ios_base::openmode mode)
std::ios_base::openmode mode)
: std::ostream(NULL), sb()
{
this->init(&sb);
@@ -441,7 +441,7 @@ gzofstream::gzofstream(const char* name,
// Initialize stream buffer and attach to file
gzofstream::gzofstream(int fd,
std::ios_base::openmode mode)
std::ios_base::openmode mode)
: std::ostream(NULL), sb()
{
this->init(&sb);
@@ -449,9 +449,9 @@ gzofstream::gzofstream(int fd,
}
// Open file and go into fail() state if unsuccessful
void
gzofstream::open(const char* name,
std::ios_base::openmode 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);
@@ -460,9 +460,9 @@ gzofstream::open(const char* name,
}
// Attach to file and go into fail() state if unsuccessful
void
gzofstream::attach(int fd,
std::ios_base::openmode mode)
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);
@@ -471,7 +471,7 @@ gzofstream::attach(int fd,
}
// Close file
void
void
gzofstream::close()
{
if (!sb.close())

View File

@@ -1,9 +1,9 @@
/*
* 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.
*/
@@ -20,204 +20,204 @@
* @brief Gzipped file stream buffer class.
*
* This class implements basic_filebuf for gzipped files. It doesn't yet support
* seeking (allowed by zlib but slow/limited), putback and read/write access
* (tricky). Otherwise, it attempts to be a drop-in replacement for the standard
* seeking (allowed by zlib but slow/limited), putback and read/write access
* (tricky). Otherwise, it attempts to be a drop-in replacement for the standard
* file streambuf.
*/
class gzfilebuf : public std::streambuf
class gzfilebuf : public std::streambuf
{
public:
public:
// Default constructor.
gzfilebuf();
// Destructor.
virtual
virtual
~gzfilebuf();
/**
* @brief Set compression level and strategy on the fly.
* @param comp_level Compression level (see zlib.h for allowed values)
* @param comp_strategy Compression strategy (see zlib.h for allowed values)
* @param comp_level Compression level (see zlib.h for allowed values)
* @param comp_strategy Compression strategy (see zlib.h for allowed values)
* @return Z_OK on success, Z_STREAM_ERROR otherwise.
*
*
* Unfortunately, these parameters cannot be modified separately, as the
* previous zfstream version assumed. Since the strategy is seldom changed,
* it can default and setcompression(level) then becomes like the old
* setcompressionlevel(level).
*/
int
setcompression(int comp_level,
int comp_strategy = Z_DEFAULT_STRATEGY);
int
setcompression(int comp_level,
int comp_strategy = Z_DEFAULT_STRATEGY);
/**
* @brief Check if file is open.
* @return True if file is open.
*/
bool
bool
is_open() const { return (file != NULL); }
/**
* @brief Open gzipped file.
* @param name File name.
* @param mode Open mode flags.
* @return @c this on success, NULL on failure.
*/
gzfilebuf*
open(const char* name,
gzfilebuf*
open(const char* name,
std::ios_base::openmode mode);
/**
* @brief Attach to already open gzipped file.
* @param fd File descriptor.
* @param mode Open mode flags.
* @return @c this on success, NULL on failure.
*/
gzfilebuf*
attach(int fd,
std::ios_base::openmode mode);
gzfilebuf*
attach(int fd,
std::ios_base::openmode mode);
/**
* @brief Close gzipped file.
* @return @c this on success, NULL on failure.
*/
gzfilebuf*
gzfilebuf*
close();
protected:
/**
* @brief Convert ios open mode int to mode string used by zlib.
* @return True if valid mode flag combination.
*/
bool
open_mode(std::ios_base::openmode mode,
char* c_mode) const;
bool
open_mode(std::ios_base::openmode mode,
char* c_mode) const;
/**
* @brief Number of characters available in stream buffer.
* @return Number of characters.
*
*
* This indicates number of characters in get area of stream buffer.
* These characters can be read without accessing the gzipped file.
*/
virtual std::streamsize
showmanyc();
/**
* @brief Fill get area from gzipped file.
* @return First character in get area on success, EOF on error.
*
*
* This actually reads characters from gzipped file to stream
* buffer. Always buffered.
*/
virtual int_type
underflow();
/**
* @brief Write put area to gzipped file.
* @param c Extra character to add to buffer contents.
* @return Non-EOF on success, EOF on error.
*
* This actually writes characters in stream buffer to
* gzipped file. With unbuffered output this is done one
*
* This actually writes characters in stream buffer to
* gzipped file. With unbuffered output this is done one
* character at a time.
*/
virtual int_type
*/
virtual int_type
overflow(int_type c = traits_type::eof());
/**
* @brief Installs external stream buffer.
* @param p Pointer to char buffer.
* @param n Size of external buffer.
* @return @c this on success, NULL on failure.
*
*
* Call setbuf(0,0) to enable unbuffered output.
*/
virtual std::streambuf*
setbuf(char_type* p,
std::streamsize n);
virtual std::streambuf*
setbuf(char_type* p,
std::streamsize n);
/**
* @brief Flush stream buffer to file.
* @return 0 on success, -1 on error.
*
*
* This calls underflow(EOF) to do the job.
*/
virtual int
virtual int
sync();
//
// Some future enhancements
//
//
// virtual int_type uflow();
// virtual int_type pbackfail(int_type c = traits_type::eof());
// virtual pos_type
// seekoff(off_type off,
// std::ios_base::seekdir way,
// std::ios_base::openmode mode = std::ios_base::in|std::ios_base::out);
// virtual pos_type
// seekpos(pos_type sp,
// std::ios_base::openmode mode = std::ios_base::in|std::ios_base::out);
// virtual pos_type
// seekoff(off_type off,
// std::ios_base::seekdir way,
// std::ios_base::openmode mode = std::ios_base::in|std::ios_base::out);
// virtual pos_type
// seekpos(pos_type sp,
// std::ios_base::openmode mode = std::ios_base::in|std::ios_base::out);
private:
/**
* @brief Allocate internal buffer.
*
*
* This function is safe to call multiple times. It will ensure
* that a proper internal buffer exists if it is required. If the
* buffer already exists or is external, the buffer pointers will be
* reset to their original state.
*/
void
void
enable_buffer();
/**
* @brief Destroy internal buffer.
*
*
* This function is safe to call multiple times. It will ensure
* that the internal buffer is deallocated if it exists. In any
* case, it will also reset the buffer pointers.
*/
void
void
disable_buffer();
/**
* Underlying file pointer.
*/
gzFile file;
/**
* Mode in which file was opened.
*/
std::ios_base::openmode io_mode;
/**
* @brief True if this object owns file descriptor.
*
* This makes the class responsible for closing the file
* This makes the class responsible for closing the file
* upon destruction.
*/
bool own_fd;
/**
* @brief Stream buffer.
*
* For simplicity this remains allocated on the free store for the
*
* For simplicity this remains allocated on the free store for the
* entire life span of the gzfilebuf object, unless replaced by setbuf.
*/
char_type* buffer;
/**
* @brief Stream buffer size.
*
*
* Defaults to system default buffer size (typically 8192 bytes).
* Modified by setbuf.
*/
std::streamsize buffer_size;
/**
* @brief True if this object owns stream buffer.
*
* This makes the class responsible for deleting the buffer
* This makes the class responsible for deleting the buffer
* upon destruction.
*/
bool own_buffer;
@@ -231,49 +231,49 @@ private:
* This class implements ifstream for gzipped files. Seeking and putback
* is not supported yet.
*/
class gzifstream : public std::istream
class gzifstream : public std::istream
{
public:
// Default constructor
gzifstream();
/**
* @brief Construct stream on gzipped file to be opened.
* @param name File name.
* @param mode Open mode flags (forced to contain ios::in).
*/
explicit
gzifstream(const char* name,
std::ios_base::openmode mode = std::ios_base::in);
gzifstream(const char* name,
std::ios_base::openmode mode = std::ios_base::in);
/**
* @brief Construct stream on already open gzipped file.
* @param fd File descriptor.
* @param mode Open mode flags (forced to contain ios::in).
*/
explicit
gzifstream(int fd,
std::ios_base::openmode mode = std::ios_base::in);
explicit
gzifstream(int fd,
std::ios_base::openmode mode = std::ios_base::in);
/**
* Obtain underlying stream buffer.
*/
gzfilebuf*
*/
gzfilebuf*
rdbuf() const
{ return const_cast<gzfilebuf*>(&sb); }
{ return const_cast<gzfilebuf*>(&sb); }
/**
* @brief Check if file is open.
* @return True if file is open.
*/
bool
bool
is_open() { return sb.is_open(); }
/**
* @brief Open gzipped file.
* @param name File name.
* @param mode Open mode flags (forced to contain ios::in).
*
*
* Stream will be in state good() if file opens successfully;
* otherwise in state fail(). This differs from the behavior of
* ifstream, which never sets the state to good() and therefore
@@ -281,34 +281,34 @@ public:
* you manually clear() the state. The choice is a matter of
* convenience.
*/
void
open(const char* name,
void
open(const char* name,
std::ios_base::openmode mode = std::ios_base::in);
/**
* @brief Attach to already open gzipped file.
* @param fd File descriptor.
* @param mode Open mode flags (forced to contain ios::in).
*
*
* Stream will be in state good() if attach succeeded; otherwise
* in state fail().
*/
void
attach(int fd,
std::ios_base::openmode mode = std::ios_base::in);
void
attach(int fd,
std::ios_base::openmode mode = std::ios_base::in);
/**
* @brief Close gzipped file.
*
*
* Stream will be in state fail() if close failed.
*/
void
void
close();
private:
/**
* Underlying stream buffer.
*/
*/
gzfilebuf sb;
};
@@ -325,44 +325,44 @@ class gzofstream : public std::ostream
public:
// Default constructor
gzofstream();
/**
* @brief Construct stream on gzipped file to be opened.
* @param name File name.
* @param mode Open mode flags (forced to contain ios::out).
*/
explicit
gzofstream(const char* name,
std::ios_base::openmode mode = std::ios_base::out);
gzofstream(const char* name,
std::ios_base::openmode mode = std::ios_base::out);
/**
* @brief Construct stream on already open gzipped file.
* @param fd File descriptor.
* @param mode Open mode flags (forced to contain ios::out).
*/
explicit
gzofstream(int fd,
std::ios_base::openmode mode = std::ios_base::out);
explicit
gzofstream(int fd,
std::ios_base::openmode mode = std::ios_base::out);
/**
* Obtain underlying stream buffer.
*/
gzfilebuf*
*/
gzfilebuf*
rdbuf() const
{ return const_cast<gzfilebuf*>(&sb); }
{ return const_cast<gzfilebuf*>(&sb); }
/**
* @brief Check if file is open.
* @return True if file is open.
*/
bool
bool
is_open() { return sb.is_open(); }
/**
* @brief Open gzipped file.
* @param name File name.
* @param mode Open mode flags (forced to contain ios::out).
*
*
* Stream will be in state good() if file opens successfully;
* otherwise in state fail(). This differs from the behavior of
* ofstream, which never sets the state to good() and therefore
@@ -370,39 +370,39 @@ public:
* you manually clear() the state. The choice is a matter of
* convenience.
*/
void
open(const char* name,
void
open(const char* name,
std::ios_base::openmode mode = std::ios_base::out);
/**
* @brief Attach to already open gzipped file.
* @param fd File descriptor.
* @param mode Open mode flags (forced to contain ios::out).
*
*
* Stream will be in state good() if attach succeeded; otherwise
* in state fail().
*/
void
attach(int fd,
std::ios_base::openmode mode = std::ios_base::out);
void
attach(int fd,
std::ios_base::openmode mode = std::ios_base::out);
/**
* @brief Close gzipped file.
*
*
* Stream will be in state fail() if close failed.
*/
void
void
close();
private:
/**
* Underlying stream buffer.
*/
*/
gzfilebuf sb;
};
/*****************************************************************************/
/**
* @brief Gzipped file output stream manipulator class.
*
@@ -415,28 +415,28 @@ template<typename T1, typename T2>
public:
// Allows insertor to peek at internals
template <typename Ta, typename Tb>
friend gzofstream&
operator<<(gzofstream&,
const gzomanip2<Ta,Tb>&);
friend gzofstream&
operator<<(gzofstream&,
const gzomanip2<Ta,Tb>&);
// Constructor
gzomanip2(gzofstream& (*f)(gzofstream&, T1, T2),
T1 v1,
T2 v2);
T1 v1,
T2 v2);
private:
// Underlying manipulator function
gzofstream&
(*func)(gzofstream&, T1, T2);
// Arguments for manipulator function
// Arguments for manipulator function
T1 val1;
T2 val2;
};
/*****************************************************************************/
// Manipulator function thunks through to stream buffer
inline gzofstream&
inline gzofstream&
setcompression(gzofstream &gzs, int l, int s = Z_DEFAULT_STRATEGY)
{
(gzs.rdbuf())->setcompression(l, s);
@@ -445,22 +445,22 @@ setcompression(gzofstream &gzs, int l, int s = Z_DEFAULT_STRATEGY)
// Manipulator constructor stores arguments
template<typename T1, typename T2>
inline
inline
gzomanip2<T1,T2>::gzomanip2(gzofstream &(*f)(gzofstream &, T1, T2),
T1 v1,
T2 v2)
T1 v1,
T2 v2)
: func(f), val1(v1), val2(v2)
{ }
// Insertor applies underlying manipulator function to stream
template<typename T1, typename T2>
inline gzofstream&
operator<<(gzofstream& s, const gzomanip2<T1,T2>& m)
inline gzofstream&
operator<<(gzofstream& s, const gzomanip2<T1,T2>& m)
{ return (*m.func)(s, m.val1, m.val2); }
// Insert this onto stream to simplify setting of compression level
inline gzomanip2<int,int>
setcompression(int l, int s = Z_DEFAULT_STRATEGY)
inline gzomanip2<int,int>
setcompression(int l, int s = Z_DEFAULT_STRATEGY)
{ return gzomanip2<int,int>(&setcompression, l, s); }
#endif // ZFSTREAM_H