diff options
| author | Mark Adler <madler@alumni.caltech.edu> | 2011-09-09 23:22:37 -0700 |
|---|---|---|
| committer | Mark Adler <madler@alumni.caltech.edu> | 2011-09-09 23:22:37 -0700 |
| commit | 4b5a43a219d51066c01ff2ab86af18b967f2d0dd (patch) | |
| tree | 4dcaf0cd18751d04cf638a9a6ec521990d4f2e90 /contrib/iostream3 | |
| parent | 086e982175da84b3db958191031380794315f95f (diff) | |
| download | zlib-1.2.0.5.tar.gz zlib-1.2.0.5.tar.bz2 zlib-1.2.0.5.zip | |
zlib 1.2.0.5v1.2.0.5
Diffstat (limited to 'contrib/iostream3')
| -rw-r--r-- | contrib/iostream3/README | 16 | ||||
| -rw-r--r-- | contrib/iostream3/TODO | 2 | ||||
| -rw-r--r-- | contrib/iostream3/test.cc | 8 | ||||
| -rw-r--r-- | contrib/iostream3/zfstream.cc | 132 | ||||
| -rw-r--r-- | contrib/iostream3/zfstream.h | 274 |
5 files changed, 216 insertions, 216 deletions
diff --git a/contrib/iostream3/README b/contrib/iostream3/README index 6f73d58..f7b319a 100644 --- a/contrib/iostream3/README +++ b/contrib/iostream3/README | |||
| @@ -3,19 +3,19 @@ to do things like: | |||
| 3 | 3 | ||
| 4 | gzofstream outf("blah.gz"); | 4 | gzofstream outf("blah.gz"); |
| 5 | outf << "These go into the gzip file " << 123 << endl; | 5 | outf << "These go into the gzip file " << 123 << endl; |
| 6 | 6 | ||
| 7 | It does this by deriving a specialized stream buffer for gzipped files, which is | 7 | It does this by deriving a specialized stream buffer for gzipped files, which is |
| 8 | the way Stroustrup would have done it. :-> | 8 | the way Stroustrup would have done it. :-> |
| 9 | 9 | ||
| 10 | The gzifstream and gzofstream classes were originally written by Kevin Ruland | 10 | The gzifstream and gzofstream classes were originally written by Kevin Ruland |
| 11 | and made available in the zlib contrib/iostream directory. The older version still | 11 | and made available in the zlib contrib/iostream directory. The older version still |
| 12 | compiles under gcc 2.xx, but not under gcc 3.xx, which sparked the development of | 12 | compiles under gcc 2.xx, but not under gcc 3.xx, which sparked the development of |
| 13 | this version. | 13 | this version. |
| 14 | 14 | ||
| 15 | The new classes are as standard-compliant as possible, closely following the | 15 | The new classes are as standard-compliant as possible, closely following the |
| 16 | approach of the standard library's fstream classes. It compiles under gcc versions | 16 | approach of the standard library's fstream classes. It compiles under gcc versions |
| 17 | 3.2 and 3.3, but not under gcc 2.xx. This is mainly due to changes in the standard | 17 | 3.2 and 3.3, but not under gcc 2.xx. This is mainly due to changes in the standard |
| 18 | library naming scheme. The new version of gzifstream/gzofstream/gzfilebuf differs | 18 | library naming scheme. The new version of gzifstream/gzofstream/gzfilebuf differs |
| 19 | from the previous one in the following respects: | 19 | from the previous one in the following respects: |
| 20 | - added showmanyc | 20 | - added showmanyc |
| 21 | - added setbuf, with support for unbuffered output via setbuf(0,0) | 21 | - added setbuf, with support for unbuffered output via setbuf(0,0) |
| @@ -23,7 +23,7 @@ from the previous one in the following respects: | |||
| 23 | - gzipped output file opened with default compression level instead of maximum level | 23 | - gzipped output file opened with default compression level instead of maximum level |
| 24 | - setcompressionlevel()/strategy() members replaced by single setcompression() | 24 | - setcompressionlevel()/strategy() members replaced by single setcompression() |
| 25 | 25 | ||
| 26 | The code is provided "as is", with the permission to use, copy, modify, distribute | 26 | The code is provided "as is", with the permission to use, copy, modify, distribute |
| 27 | and sell it for any purpose without fee. | 27 | and sell it for any purpose without fee. |
| 28 | 28 | ||
| 29 | Ludwig Schwardt | 29 | Ludwig Schwardt |
diff --git a/contrib/iostream3/TODO b/contrib/iostream3/TODO index 4578326..7032f97 100644 --- a/contrib/iostream3/TODO +++ b/contrib/iostream3/TODO | |||
| @@ -13,5 +13,5 @@ Possible upgrades to gzfilebuf: | |||
| 13 | - Check public interface to see which calls give problems | 13 | - Check public interface to see which calls give problems |
| 14 | (due to dependence on library internals) | 14 | (due to dependence on library internals) |
| 15 | 15 | ||
| 16 | - Override operator<<(ostream&, gzfilebuf*) to allow direct copying | 16 | - Override operator<<(ostream&, gzfilebuf*) to allow direct copying |
| 17 | of stream buffer to stream ( i.e. os << is.rdbuf(); ) | 17 | of stream buffer to stream ( i.e. os << is.rdbuf(); ) |
diff --git a/contrib/iostream3/test.cc b/contrib/iostream3/test.cc index 9ffa8be..9423533 100644 --- a/contrib/iostream3/test.cc +++ b/contrib/iostream3/test.cc | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * Test program for gzifstream and gzofstream | 2 | * Test program for gzifstream and gzofstream |
| 3 | * | 3 | * |
| 4 | * by Ludwig Schwardt <schwardt@sun.ac.za> | 4 | * by Ludwig Schwardt <schwardt@sun.ac.za> |
| 5 | * original version by Kevin Ruland <kevin@rodin.wustl.edu> | 5 | * original version by Kevin Ruland <kevin@rodin.wustl.edu> |
| 6 | */ | 6 | */ |
| @@ -13,15 +13,15 @@ int main() { | |||
| 13 | gzofstream outf; | 13 | gzofstream outf; |
| 14 | gzifstream inf; | 14 | gzifstream inf; |
| 15 | char buf[80]; | 15 | char buf[80]; |
| 16 | 16 | ||
| 17 | outf.open("test1.txt.gz"); | 17 | outf.open("test1.txt.gz"); |
| 18 | outf << "The quick brown fox sidestepped the lazy canine\n" | 18 | outf << "The quick brown fox sidestepped the lazy canine\n" |
| 19 | << 1.3 << "\nPlan " << 9 << std::endl; | 19 | << 1.3 << "\nPlan " << 9 << std::endl; |
| 20 | outf.close(); | 20 | outf.close(); |
| 21 | std::cout << "Wrote the following message to 'test1.txt.gz' (check with zcat or zless):\n" | 21 | std::cout << "Wrote the following message to 'test1.txt.gz' (check with zcat or zless):\n" |
| 22 | << "The quick brown fox sidestepped the lazy canine\n" | 22 | << "The quick brown fox sidestepped the lazy canine\n" |
| 23 | << 1.3 << "\nPlan " << 9 << std::endl; | 23 | << 1.3 << "\nPlan " << 9 << std::endl; |
| 24 | 24 | ||
| 25 | std::cout << "\nReading 'test1.txt.gz' (buffered) produces:\n"; | 25 | std::cout << "\nReading 'test1.txt.gz' (buffered) produces:\n"; |
| 26 | inf.open("test1.txt.gz"); | 26 | inf.open("test1.txt.gz"); |
| 27 | while (inf.getline(buf,80,'\n')) { | 27 | while (inf.getline(buf,80,'\n')) { |
diff --git a/contrib/iostream3/zfstream.cc b/contrib/iostream3/zfstream.cc index 24d15fa..94eb933 100644 --- a/contrib/iostream3/zfstream.cc +++ b/contrib/iostream3/zfstream.cc | |||
| @@ -1,9 +1,9 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * A C++ I/O streams interface to the zlib gz* functions | 2 | * A C++ I/O streams interface to the zlib gz* functions |
| 3 | * | 3 | * |
| 4 | * by Ludwig Schwardt <schwardt@sun.ac.za> | 4 | * by Ludwig Schwardt <schwardt@sun.ac.za> |
| 5 | * original version by Kevin Ruland <kevin@rodin.wustl.edu> | 5 | * original version by Kevin Ruland <kevin@rodin.wustl.edu> |
| 6 | * | 6 | * |
| 7 | * This version is standard-compliant and compatible with gcc 3.x. | 7 | * This version is standard-compliant and compatible with gcc 3.x. |
| 8 | */ | 8 | */ |
| 9 | 9 | ||
| @@ -18,8 +18,8 @@ | |||
| 18 | /*****************************************************************************/ | 18 | /*****************************************************************************/ |
| 19 | 19 | ||
| 20 | // Default constructor | 20 | // Default constructor |
| 21 | gzfilebuf::gzfilebuf() | 21 | gzfilebuf::gzfilebuf() |
| 22 | : file(NULL), io_mode(std::ios_base::openmode(0)), own_fd(false), | 22 | : file(NULL), io_mode(std::ios_base::openmode(0)), own_fd(false), |
| 23 | buffer(NULL), buffer_size(BIGBUFSIZE), own_buffer(true) | 23 | buffer(NULL), buffer_size(BIGBUFSIZE), own_buffer(true) |
| 24 | { | 24 | { |
| 25 | // No buffers to start with | 25 | // No buffers to start with |
| @@ -27,12 +27,12 @@ gzfilebuf::gzfilebuf() | |||
| 27 | } | 27 | } |
| 28 | 28 | ||
| 29 | // Destructor | 29 | // Destructor |
| 30 | gzfilebuf::~gzfilebuf() | 30 | gzfilebuf::~gzfilebuf() |
| 31 | { | 31 | { |
| 32 | // Sync output buffer and close only if responsible for file | 32 | // Sync output buffer and close only if responsible for file |
| 33 | // (i.e. attached streams should be left open at this stage) | 33 | // (i.e. attached streams should be left open at this stage) |
| 34 | this->sync(); | 34 | this->sync(); |
| 35 | if (own_fd) | 35 | if (own_fd) |
| 36 | this->close(); | 36 | this->close(); |
| 37 | // Make sure internal buffer is deallocated | 37 | // Make sure internal buffer is deallocated |
| 38 | this->disable_buffer(); | 38 | this->disable_buffer(); |
| @@ -41,30 +41,30 @@ gzfilebuf::~gzfilebuf() | |||
| 41 | // Set compression level and strategy | 41 | // Set compression level and strategy |
| 42 | int | 42 | int |
| 43 | gzfilebuf::setcompression(int comp_level, | 43 | gzfilebuf::setcompression(int comp_level, |
| 44 | int comp_strategy) | 44 | int comp_strategy) |
| 45 | { | 45 | { |
| 46 | return gzsetparams(file, comp_level, comp_strategy); | 46 | return gzsetparams(file, comp_level, comp_strategy); |
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | // Open gzipped file | 49 | // Open gzipped file |
| 50 | gzfilebuf* | 50 | gzfilebuf* |
| 51 | gzfilebuf::open(const char *name, | 51 | gzfilebuf::open(const char *name, |
| 52 | std::ios_base::openmode mode) | 52 | std::ios_base::openmode mode) |
| 53 | { | 53 | { |
| 54 | // Fail if file already open | 54 | // Fail if file already open |
| 55 | if (this->is_open()) | 55 | if (this->is_open()) |
| 56 | return NULL; | 56 | return NULL; |
| 57 | // Don't support simultaneous read/write access (yet) | 57 | // Don't support simultaneous read/write access (yet) |
| 58 | if ((mode & std::ios_base::in) && (mode & std::ios_base::out)) | 58 | if ((mode & std::ios_base::in) && (mode & std::ios_base::out)) |
| 59 | return NULL; | 59 | return NULL; |
| 60 | 60 | ||
| 61 | // Build mode string for gzopen and check it [27.8.1.3.2] | 61 | // Build mode string for gzopen and check it [27.8.1.3.2] |
| 62 | char char_mode[6] = "\0\0\0\0\0"; | 62 | char char_mode[6] = "\0\0\0\0\0"; |
| 63 | if (!this->open_mode(mode, char_mode)) | 63 | if (!this->open_mode(mode, char_mode)) |
| 64 | return NULL; | 64 | return NULL; |
| 65 | 65 | ||
| 66 | // Attempt to open file | 66 | // Attempt to open file |
| 67 | if ((file = gzopen(name, char_mode)) == NULL) | 67 | if ((file = gzopen(name, char_mode)) == NULL) |
| 68 | return NULL; | 68 | return NULL; |
| 69 | 69 | ||
| 70 | // On success, allocate internal buffer and set flags | 70 | // On success, allocate internal buffer and set flags |
| @@ -77,24 +77,24 @@ gzfilebuf::open(const char *name, | |||
| 77 | // Attach to gzipped file | 77 | // Attach to gzipped file |
| 78 | gzfilebuf* | 78 | gzfilebuf* |
| 79 | gzfilebuf::attach(int fd, | 79 | gzfilebuf::attach(int fd, |
| 80 | std::ios_base::openmode mode) | 80 | std::ios_base::openmode mode) |
| 81 | { | 81 | { |
| 82 | // Fail if file already open | 82 | // Fail if file already open |
| 83 | if (this->is_open()) | 83 | if (this->is_open()) |
| 84 | return NULL; | 84 | return NULL; |
| 85 | // Don't support simultaneous read/write access (yet) | 85 | // Don't support simultaneous read/write access (yet) |
| 86 | if ((mode & std::ios_base::in) && (mode & std::ios_base::out)) | 86 | if ((mode & std::ios_base::in) && (mode & std::ios_base::out)) |
| 87 | return NULL; | 87 | return NULL; |
| 88 | 88 | ||
| 89 | // Build mode string for gzdopen and check it [27.8.1.3.2] | 89 | // Build mode string for gzdopen and check it [27.8.1.3.2] |
| 90 | char char_mode[6] = "\0\0\0\0\0"; | 90 | char char_mode[6] = "\0\0\0\0\0"; |
| 91 | if (!this->open_mode(mode, char_mode)) | 91 | if (!this->open_mode(mode, char_mode)) |
| 92 | return NULL; | 92 | return NULL; |
| 93 | 93 | ||
| 94 | // Attempt to attach to file | 94 | // Attempt to attach to file |
| 95 | if ((file = gzdopen(fd, char_mode)) == NULL) | 95 | if ((file = gzdopen(fd, char_mode)) == NULL) |
| 96 | return NULL; | 96 | return NULL; |
| 97 | 97 | ||
| 98 | // On success, allocate internal buffer and set flags | 98 | // On success, allocate internal buffer and set flags |
| 99 | this->enable_buffer(); | 99 | this->enable_buffer(); |
| 100 | io_mode = mode; | 100 | io_mode = mode; |
| @@ -104,7 +104,7 @@ gzfilebuf::attach(int fd, | |||
| 104 | 104 | ||
| 105 | // Close gzipped file | 105 | // Close gzipped file |
| 106 | gzfilebuf* | 106 | gzfilebuf* |
| 107 | gzfilebuf::close() | 107 | gzfilebuf::close() |
| 108 | { | 108 | { |
| 109 | // Fail immediately if no file is open | 109 | // Fail immediately if no file is open |
| 110 | if (!this->is_open()) | 110 | if (!this->is_open()) |
| @@ -127,16 +127,16 @@ gzfilebuf::close() | |||
| 127 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | 127 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
| 128 | 128 | ||
| 129 | // Convert int open mode to mode string | 129 | // Convert int open mode to mode string |
| 130 | bool | 130 | bool |
| 131 | gzfilebuf::open_mode(std::ios_base::openmode mode, | 131 | gzfilebuf::open_mode(std::ios_base::openmode mode, |
| 132 | char* c_mode) const | 132 | char* c_mode) const |
| 133 | { | 133 | { |
| 134 | bool testb = mode & std::ios_base::binary; | 134 | bool testb = mode & std::ios_base::binary; |
| 135 | bool testi = mode & std::ios_base::in; | 135 | bool testi = mode & std::ios_base::in; |
| 136 | bool testo = mode & std::ios_base::out; | 136 | bool testo = mode & std::ios_base::out; |
| 137 | bool testt = mode & std::ios_base::trunc; | 137 | bool testt = mode & std::ios_base::trunc; |
| 138 | bool testa = mode & std::ios_base::app; | 138 | bool testa = mode & std::ios_base::app; |
| 139 | 139 | ||
| 140 | // Check for valid flag combinations - see [27.8.1.3.2] (Table 92) | 140 | // Check for valid flag combinations - see [27.8.1.3.2] (Table 92) |
| 141 | // Original zfstream hardcoded the compression level to maximum here... | 141 | // Original zfstream hardcoded the compression level to maximum here... |
| 142 | // Double the time for less than 1% size improvement seems | 142 | // Double the time for less than 1% size improvement seems |
| @@ -156,7 +156,7 @@ gzfilebuf::open_mode(std::ios_base::openmode mode, | |||
| 156 | // if (testi && testo && testt && !testa) | 156 | // if (testi && testo && testt && !testa) |
| 157 | // strcpy(c_mode, "w+"); | 157 | // strcpy(c_mode, "w+"); |
| 158 | 158 | ||
| 159 | // Mode string should be empty for invalid combination of flags | 159 | // Mode string should be empty for invalid combination of flags |
| 160 | if (strlen(c_mode) == 0) | 160 | if (strlen(c_mode) == 0) |
| 161 | return false; | 161 | return false; |
| 162 | if (testb) | 162 | if (testb) |
| @@ -165,7 +165,7 @@ gzfilebuf::open_mode(std::ios_base::openmode mode, | |||
| 165 | } | 165 | } |
| 166 | 166 | ||
| 167 | // Determine number of characters in internal get buffer | 167 | // Determine number of characters in internal get buffer |
| 168 | std::streamsize | 168 | std::streamsize |
| 169 | gzfilebuf::showmanyc() | 169 | gzfilebuf::showmanyc() |
| 170 | { | 170 | { |
| 171 | // Calls to underflow will fail if file not opened for reading | 171 | // Calls to underflow will fail if file not opened for reading |
| @@ -180,10 +180,10 @@ gzfilebuf::showmanyc() | |||
| 180 | 180 | ||
| 181 | // Fill get area from gzipped file | 181 | // Fill get area from gzipped file |
| 182 | gzfilebuf::int_type | 182 | gzfilebuf::int_type |
| 183 | gzfilebuf::underflow() | 183 | gzfilebuf::underflow() |
| 184 | { | 184 | { |
| 185 | // If something is left in the get area by chance, return it | 185 | // If something is left in the get area by chance, return it |
| 186 | // (this shouldn't normally happen, as underflow is only supposed | 186 | // (this shouldn't normally happen, as underflow is only supposed |
| 187 | // to be called when gptr >= egptr, but it serves as error check) | 187 | // to be called when gptr >= egptr, but it serves as error check) |
| 188 | if (this->gptr() && (this->gptr() < this->egptr())) | 188 | if (this->gptr() && (this->gptr() < this->egptr())) |
| 189 | return traits_type::to_int_type(*(this->gptr())); | 189 | return traits_type::to_int_type(*(this->gptr())); |
| @@ -204,14 +204,14 @@ gzfilebuf::underflow() | |||
| 204 | } | 204 | } |
| 205 | // Make all bytes read from file available as get area | 205 | // Make all bytes read from file available as get area |
| 206 | this->setg(buffer, buffer, buffer + bytes_read); | 206 | this->setg(buffer, buffer, buffer + bytes_read); |
| 207 | 207 | ||
| 208 | // Return next character in get area | 208 | // Return next character in get area |
| 209 | return traits_type::to_int_type(*(this->gptr())); | 209 | return traits_type::to_int_type(*(this->gptr())); |
| 210 | } | 210 | } |
| 211 | 211 | ||
| 212 | // Write put area to gzipped file | 212 | // Write put area to gzipped file |
| 213 | gzfilebuf::int_type | 213 | gzfilebuf::int_type |
| 214 | gzfilebuf::overflow(int_type c) | 214 | gzfilebuf::overflow(int_type c) |
| 215 | { | 215 | { |
| 216 | // Determine whether put area is in use | 216 | // Determine whether put area is in use |
| 217 | if (this->pbase()) | 217 | if (this->pbase()) |
| @@ -232,10 +232,10 @@ gzfilebuf::overflow(int_type c) | |||
| 232 | { | 232 | { |
| 233 | // If the file hasn't been opened for writing, produce error | 233 | // If the file hasn't been opened for writing, produce error |
| 234 | if (!this->is_open() || !(io_mode & std::ios_base::out)) | 234 | if (!this->is_open() || !(io_mode & std::ios_base::out)) |
| 235 | return traits_type::eof(); | 235 | return traits_type::eof(); |
| 236 | // If gzipped file won't accept all bytes written to it, fail | 236 | // If gzipped file won't accept all bytes written to it, fail |
| 237 | if (gzwrite(file, this->pbase(), bytes_to_write) != bytes_to_write) | 237 | if (gzwrite(file, this->pbase(), bytes_to_write) != bytes_to_write) |
| 238 | return traits_type::eof(); | 238 | return traits_type::eof(); |
| 239 | // Reset next pointer to point to pbase on success | 239 | // Reset next pointer to point to pbase on success |
| 240 | this->pbump(-bytes_to_write); | 240 | this->pbump(-bytes_to_write); |
| 241 | } | 241 | } |
| @@ -250,7 +250,7 @@ gzfilebuf::overflow(int_type c) | |||
| 250 | char_type last_char = traits_type::to_char_type(c); | 250 | char_type last_char = traits_type::to_char_type(c); |
| 251 | // If gzipped file won't accept this character, fail | 251 | // If gzipped file won't accept this character, fail |
| 252 | if (gzwrite(file, &last_char, 1) != 1) | 252 | if (gzwrite(file, &last_char, 1) != 1) |
| 253 | return traits_type::eof(); | 253 | return traits_type::eof(); |
| 254 | } | 254 | } |
| 255 | 255 | ||
| 256 | // If you got here, you have succeeded (even if c was EOF) | 256 | // If you got here, you have succeeded (even if c was EOF) |
| @@ -262,9 +262,9 @@ gzfilebuf::overflow(int_type c) | |||
| 262 | } | 262 | } |
| 263 | 263 | ||
| 264 | // Assign new buffer | 264 | // Assign new buffer |
| 265 | std::streambuf* | 265 | std::streambuf* |
| 266 | gzfilebuf::setbuf(char_type* p, | 266 | gzfilebuf::setbuf(char_type* p, |
| 267 | std::streamsize n) | 267 | std::streamsize n) |
| 268 | { | 268 | { |
| 269 | // First make sure stuff is sync'ed, for safety | 269 | // First make sure stuff is sync'ed, for safety |
| 270 | if (this->sync() == -1) | 270 | if (this->sync() == -1) |
| @@ -295,8 +295,8 @@ gzfilebuf::setbuf(char_type* p, | |||
| 295 | } | 295 | } |
| 296 | 296 | ||
| 297 | // Write put area to gzipped file (i.e. ensures that put area is empty) | 297 | // Write put area to gzipped file (i.e. ensures that put area is empty) |
| 298 | int | 298 | int |
| 299 | gzfilebuf::sync() | 299 | gzfilebuf::sync() |
| 300 | { | 300 | { |
| 301 | return traits_type::eq_int_type(this->overflow(), traits_type::eof()) ? -1 : 0; | 301 | return traits_type::eq_int_type(this->overflow(), traits_type::eof()) ? -1 : 0; |
| 302 | } | 302 | } |
| @@ -304,11 +304,11 @@ gzfilebuf::sync() | |||
| 304 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | 304 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
| 305 | 305 | ||
| 306 | // Allocate internal buffer | 306 | // Allocate internal buffer |
| 307 | void | 307 | void |
| 308 | gzfilebuf::enable_buffer() | 308 | gzfilebuf::enable_buffer() |
| 309 | { | 309 | { |
| 310 | // If internal buffer required, allocate one | 310 | // If internal buffer required, allocate one |
| 311 | if (own_buffer && !buffer) | 311 | if (own_buffer && !buffer) |
| 312 | { | 312 | { |
| 313 | // Check for buffered vs. "unbuffered" | 313 | // Check for buffered vs. "unbuffered" |
| 314 | if (buffer_size > 0) | 314 | if (buffer_size > 0) |
| @@ -335,16 +335,16 @@ gzfilebuf::enable_buffer() | |||
| 335 | } | 335 | } |
| 336 | else | 336 | else |
| 337 | { | 337 | { |
| 338 | // If buffer already allocated, reset buffer pointers just to make sure no | 338 | // If buffer already allocated, reset buffer pointers just to make sure no |
| 339 | // stale chars are lying around | 339 | // stale chars are lying around |
| 340 | this->setg(buffer, buffer, buffer); | 340 | this->setg(buffer, buffer, buffer); |
| 341 | this->setp(buffer, buffer + buffer_size - 1); | 341 | this->setp(buffer, buffer + buffer_size - 1); |
| 342 | } | 342 | } |
| 343 | } | 343 | } |
| 344 | 344 | ||
| 345 | // Destroy internal buffer | 345 | // Destroy internal buffer |
| 346 | void | 346 | void |
| 347 | gzfilebuf::disable_buffer() | 347 | gzfilebuf::disable_buffer() |
| 348 | { | 348 | { |
| 349 | // If internal buffer exists, deallocate it | 349 | // If internal buffer exists, deallocate it |
| 350 | if (own_buffer && buffer) | 350 | if (own_buffer && buffer) |
| @@ -371,13 +371,13 @@ gzfilebuf::disable_buffer() | |||
| 371 | /*****************************************************************************/ | 371 | /*****************************************************************************/ |
| 372 | 372 | ||
| 373 | // Default constructor initializes stream buffer | 373 | // Default constructor initializes stream buffer |
| 374 | gzifstream::gzifstream() | 374 | gzifstream::gzifstream() |
| 375 | : std::istream(NULL), sb() | 375 | : std::istream(NULL), sb() |
| 376 | { this->init(&sb); } | 376 | { this->init(&sb); } |
| 377 | 377 | ||
| 378 | // Initialize stream buffer and open file | 378 | // Initialize stream buffer and open file |
| 379 | gzifstream::gzifstream(const char* name, | 379 | gzifstream::gzifstream(const char* name, |
| 380 | std::ios_base::openmode mode) | 380 | std::ios_base::openmode mode) |
| 381 | : std::istream(NULL), sb() | 381 | : std::istream(NULL), sb() |
| 382 | { | 382 | { |
| 383 | this->init(&sb); | 383 | this->init(&sb); |
| @@ -386,7 +386,7 @@ gzifstream::gzifstream(const char* name, | |||
| 386 | 386 | ||
| 387 | // Initialize stream buffer and attach to file | 387 | // Initialize stream buffer and attach to file |
| 388 | gzifstream::gzifstream(int fd, | 388 | gzifstream::gzifstream(int fd, |
| 389 | std::ios_base::openmode mode) | 389 | std::ios_base::openmode mode) |
| 390 | : std::istream(NULL), sb() | 390 | : std::istream(NULL), sb() |
| 391 | { | 391 | { |
| 392 | this->init(&sb); | 392 | this->init(&sb); |
| @@ -394,9 +394,9 @@ gzifstream::gzifstream(int fd, | |||
| 394 | } | 394 | } |
| 395 | 395 | ||
| 396 | // Open file and go into fail() state if unsuccessful | 396 | // Open file and go into fail() state if unsuccessful |
| 397 | void | 397 | void |
| 398 | gzifstream::open(const char* name, | 398 | gzifstream::open(const char* name, |
| 399 | std::ios_base::openmode mode) | 399 | std::ios_base::openmode mode) |
| 400 | { | 400 | { |
| 401 | if (!sb.open(name, mode | std::ios_base::in)) | 401 | if (!sb.open(name, mode | std::ios_base::in)) |
| 402 | this->setstate(std::ios_base::failbit); | 402 | this->setstate(std::ios_base::failbit); |
| @@ -405,9 +405,9 @@ gzifstream::open(const char* name, | |||
| 405 | } | 405 | } |
| 406 | 406 | ||
| 407 | // Attach to file and go into fail() state if unsuccessful | 407 | // Attach to file and go into fail() state if unsuccessful |
| 408 | void | 408 | void |
| 409 | gzifstream::attach(int fd, | 409 | gzifstream::attach(int fd, |
| 410 | std::ios_base::openmode mode) | 410 | std::ios_base::openmode mode) |
| 411 | { | 411 | { |
| 412 | if (!sb.attach(fd, mode | std::ios_base::in)) | 412 | if (!sb.attach(fd, mode | std::ios_base::in)) |
| 413 | this->setstate(std::ios_base::failbit); | 413 | this->setstate(std::ios_base::failbit); |
| @@ -416,7 +416,7 @@ gzifstream::attach(int fd, | |||
| 416 | } | 416 | } |
| 417 | 417 | ||
| 418 | // Close file | 418 | // Close file |
| 419 | void | 419 | void |
| 420 | gzifstream::close() | 420 | gzifstream::close() |
| 421 | { | 421 | { |
| 422 | if (!sb.close()) | 422 | if (!sb.close()) |
| @@ -426,13 +426,13 @@ gzifstream::close() | |||
| 426 | /*****************************************************************************/ | 426 | /*****************************************************************************/ |
| 427 | 427 | ||
| 428 | // Default constructor initializes stream buffer | 428 | // Default constructor initializes stream buffer |
| 429 | gzofstream::gzofstream() | 429 | gzofstream::gzofstream() |
| 430 | : std::ostream(NULL), sb() | 430 | : std::ostream(NULL), sb() |
| 431 | { this->init(&sb); } | 431 | { this->init(&sb); } |
| 432 | 432 | ||
| 433 | // Initialize stream buffer and open file | 433 | // Initialize stream buffer and open file |
| 434 | gzofstream::gzofstream(const char* name, | 434 | gzofstream::gzofstream(const char* name, |
| 435 | std::ios_base::openmode mode) | 435 | std::ios_base::openmode mode) |
| 436 | : std::ostream(NULL), sb() | 436 | : std::ostream(NULL), sb() |
| 437 | { | 437 | { |
| 438 | this->init(&sb); | 438 | this->init(&sb); |
| @@ -441,7 +441,7 @@ gzofstream::gzofstream(const char* name, | |||
| 441 | 441 | ||
| 442 | // Initialize stream buffer and attach to file | 442 | // Initialize stream buffer and attach to file |
| 443 | gzofstream::gzofstream(int fd, | 443 | gzofstream::gzofstream(int fd, |
| 444 | std::ios_base::openmode mode) | 444 | std::ios_base::openmode mode) |
| 445 | : std::ostream(NULL), sb() | 445 | : std::ostream(NULL), sb() |
| 446 | { | 446 | { |
| 447 | this->init(&sb); | 447 | this->init(&sb); |
| @@ -449,9 +449,9 @@ gzofstream::gzofstream(int fd, | |||
| 449 | } | 449 | } |
| 450 | 450 | ||
| 451 | // Open file and go into fail() state if unsuccessful | 451 | // Open file and go into fail() state if unsuccessful |
| 452 | void | 452 | void |
| 453 | gzofstream::open(const char* name, | 453 | gzofstream::open(const char* name, |
| 454 | std::ios_base::openmode mode) | 454 | std::ios_base::openmode mode) |
| 455 | { | 455 | { |
| 456 | if (!sb.open(name, mode | std::ios_base::out)) | 456 | if (!sb.open(name, mode | std::ios_base::out)) |
| 457 | this->setstate(std::ios_base::failbit); | 457 | this->setstate(std::ios_base::failbit); |
| @@ -460,9 +460,9 @@ gzofstream::open(const char* name, | |||
| 460 | } | 460 | } |
| 461 | 461 | ||
| 462 | // Attach to file and go into fail() state if unsuccessful | 462 | // Attach to file and go into fail() state if unsuccessful |
| 463 | void | 463 | void |
| 464 | gzofstream::attach(int fd, | 464 | gzofstream::attach(int fd, |
| 465 | std::ios_base::openmode mode) | 465 | std::ios_base::openmode mode) |
| 466 | { | 466 | { |
| 467 | if (!sb.attach(fd, mode | std::ios_base::out)) | 467 | if (!sb.attach(fd, mode | std::ios_base::out)) |
| 468 | this->setstate(std::ios_base::failbit); | 468 | this->setstate(std::ios_base::failbit); |
| @@ -471,7 +471,7 @@ gzofstream::attach(int fd, | |||
| 471 | } | 471 | } |
| 472 | 472 | ||
| 473 | // Close file | 473 | // Close file |
| 474 | void | 474 | void |
| 475 | gzofstream::close() | 475 | gzofstream::close() |
| 476 | { | 476 | { |
| 477 | if (!sb.close()) | 477 | if (!sb.close()) |
diff --git a/contrib/iostream3/zfstream.h b/contrib/iostream3/zfstream.h index ad76e8b..8574479 100644 --- a/contrib/iostream3/zfstream.h +++ b/contrib/iostream3/zfstream.h | |||
| @@ -1,9 +1,9 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * A C++ I/O streams interface to the zlib gz* functions | 2 | * A C++ I/O streams interface to the zlib gz* functions |
| 3 | * | 3 | * |
| 4 | * by Ludwig Schwardt <schwardt@sun.ac.za> | 4 | * by Ludwig Schwardt <schwardt@sun.ac.za> |
| 5 | * original version by Kevin Ruland <kevin@rodin.wustl.edu> | 5 | * original version by Kevin Ruland <kevin@rodin.wustl.edu> |
| 6 | * | 6 | * |
| 7 | * This version is standard-compliant and compatible with gcc 3.x. | 7 | * This version is standard-compliant and compatible with gcc 3.x. |
| 8 | */ | 8 | */ |
| 9 | 9 | ||
| @@ -20,204 +20,204 @@ | |||
| 20 | * @brief Gzipped file stream buffer class. | 20 | * @brief Gzipped file stream buffer class. |
| 21 | * | 21 | * |
| 22 | * This class implements basic_filebuf for gzipped files. It doesn't yet support | 22 | * This class implements basic_filebuf for gzipped files. It doesn't yet support |
| 23 | * seeking (allowed by zlib but slow/limited), putback and read/write access | 23 | * seeking (allowed by zlib but slow/limited), putback and read/write access |
| 24 | * (tricky). Otherwise, it attempts to be a drop-in replacement for the standard | 24 | * (tricky). Otherwise, it attempts to be a drop-in replacement for the standard |
| 25 | * file streambuf. | 25 | * file streambuf. |
| 26 | */ | 26 | */ |
| 27 | class gzfilebuf : public std::streambuf | 27 | class gzfilebuf : public std::streambuf |
| 28 | { | 28 | { |
| 29 | public: | 29 | public: |
| 30 | // Default constructor. | 30 | // Default constructor. |
| 31 | gzfilebuf(); | 31 | gzfilebuf(); |
| 32 | 32 | ||
| 33 | // Destructor. | 33 | // Destructor. |
| 34 | virtual | 34 | virtual |
| 35 | ~gzfilebuf(); | 35 | ~gzfilebuf(); |
| 36 | 36 | ||
| 37 | /** | 37 | /** |
| 38 | * @brief Set compression level and strategy on the fly. | 38 | * @brief Set compression level and strategy on the fly. |
| 39 | * @param comp_level Compression level (see zlib.h for allowed values) | 39 | * @param comp_level Compression level (see zlib.h for allowed values) |
| 40 | * @param comp_strategy Compression strategy (see zlib.h for allowed values) | 40 | * @param comp_strategy Compression strategy (see zlib.h for allowed values) |
| 41 | * @return Z_OK on success, Z_STREAM_ERROR otherwise. | 41 | * @return Z_OK on success, Z_STREAM_ERROR otherwise. |
| 42 | * | 42 | * |
| 43 | * Unfortunately, these parameters cannot be modified separately, as the | 43 | * Unfortunately, these parameters cannot be modified separately, as the |
| 44 | * previous zfstream version assumed. Since the strategy is seldom changed, | 44 | * previous zfstream version assumed. Since the strategy is seldom changed, |
| 45 | * it can default and setcompression(level) then becomes like the old | 45 | * it can default and setcompression(level) then becomes like the old |
| 46 | * setcompressionlevel(level). | 46 | * setcompressionlevel(level). |
| 47 | */ | 47 | */ |
| 48 | int | 48 | int |
| 49 | setcompression(int comp_level, | 49 | setcompression(int comp_level, |
| 50 | int comp_strategy = Z_DEFAULT_STRATEGY); | 50 | int comp_strategy = Z_DEFAULT_STRATEGY); |
| 51 | 51 | ||
| 52 | /** | 52 | /** |
| 53 | * @brief Check if file is open. | 53 | * @brief Check if file is open. |
| 54 | * @return True if file is open. | 54 | * @return True if file is open. |
| 55 | */ | 55 | */ |
| 56 | bool | 56 | bool |
| 57 | is_open() const { return (file != NULL); } | 57 | is_open() const { return (file != NULL); } |
| 58 | 58 | ||
| 59 | /** | 59 | /** |
| 60 | * @brief Open gzipped file. | 60 | * @brief Open gzipped file. |
| 61 | * @param name File name. | 61 | * @param name File name. |
| 62 | * @param mode Open mode flags. | 62 | * @param mode Open mode flags. |
| 63 | * @return @c this on success, NULL on failure. | 63 | * @return @c this on success, NULL on failure. |
| 64 | */ | 64 | */ |
| 65 | gzfilebuf* | 65 | gzfilebuf* |
| 66 | open(const char* name, | 66 | open(const char* name, |
| 67 | std::ios_base::openmode mode); | 67 | std::ios_base::openmode mode); |
| 68 | 68 | ||
| 69 | /** | 69 | /** |
| 70 | * @brief Attach to already open gzipped file. | 70 | * @brief Attach to already open gzipped file. |
| 71 | * @param fd File descriptor. | 71 | * @param fd File descriptor. |
| 72 | * @param mode Open mode flags. | 72 | * @param mode Open mode flags. |
| 73 | * @return @c this on success, NULL on failure. | 73 | * @return @c this on success, NULL on failure. |
| 74 | */ | 74 | */ |
| 75 | gzfilebuf* | 75 | gzfilebuf* |
| 76 | attach(int fd, | 76 | attach(int fd, |
| 77 | std::ios_base::openmode mode); | 77 | std::ios_base::openmode mode); |
| 78 | 78 | ||
| 79 | /** | 79 | /** |
| 80 | * @brief Close gzipped file. | 80 | * @brief Close gzipped file. |
| 81 | * @return @c this on success, NULL on failure. | 81 | * @return @c this on success, NULL on failure. |
| 82 | */ | 82 | */ |
| 83 | gzfilebuf* | 83 | gzfilebuf* |
| 84 | close(); | 84 | close(); |
| 85 | 85 | ||
| 86 | protected: | 86 | protected: |
| 87 | /** | 87 | /** |
| 88 | * @brief Convert ios open mode int to mode string used by zlib. | 88 | * @brief Convert ios open mode int to mode string used by zlib. |
| 89 | * @return True if valid mode flag combination. | 89 | * @return True if valid mode flag combination. |
| 90 | */ | 90 | */ |
| 91 | bool | 91 | bool |
| 92 | open_mode(std::ios_base::openmode mode, | 92 | open_mode(std::ios_base::openmode mode, |
| 93 | char* c_mode) const; | 93 | char* c_mode) const; |
| 94 | 94 | ||
| 95 | /** | 95 | /** |
| 96 | * @brief Number of characters available in stream buffer. | 96 | * @brief Number of characters available in stream buffer. |
| 97 | * @return Number of characters. | 97 | * @return Number of characters. |
| 98 | * | 98 | * |
| 99 | * This indicates number of characters in get area of stream buffer. | 99 | * This indicates number of characters in get area of stream buffer. |
| 100 | * These characters can be read without accessing the gzipped file. | 100 | * These characters can be read without accessing the gzipped file. |
| 101 | */ | 101 | */ |
| 102 | virtual std::streamsize | 102 | virtual std::streamsize |
| 103 | showmanyc(); | 103 | showmanyc(); |
| 104 | 104 | ||
| 105 | /** | 105 | /** |
| 106 | * @brief Fill get area from gzipped file. | 106 | * @brief Fill get area from gzipped file. |
| 107 | * @return First character in get area on success, EOF on error. | 107 | * @return First character in get area on success, EOF on error. |
| 108 | * | 108 | * |
| 109 | * This actually reads characters from gzipped file to stream | 109 | * This actually reads characters from gzipped file to stream |
| 110 | * buffer. Always buffered. | 110 | * buffer. Always buffered. |
| 111 | */ | 111 | */ |
| 112 | virtual int_type | 112 | virtual int_type |
| 113 | underflow(); | 113 | underflow(); |
| 114 | 114 | ||
| 115 | /** | 115 | /** |
| 116 | * @brief Write put area to gzipped file. | 116 | * @brief Write put area to gzipped file. |
| 117 | * @param c Extra character to add to buffer contents. | 117 | * @param c Extra character to add to buffer contents. |
| 118 | * @return Non-EOF on success, EOF on error. | 118 | * @return Non-EOF on success, EOF on error. |
| 119 | * | 119 | * |
| 120 | * This actually writes characters in stream buffer to | 120 | * This actually writes characters in stream buffer to |
| 121 | * gzipped file. With unbuffered output this is done one | 121 | * gzipped file. With unbuffered output this is done one |
| 122 | * character at a time. | 122 | * character at a time. |
| 123 | */ | 123 | */ |
| 124 | virtual int_type | 124 | virtual int_type |
| 125 | overflow(int_type c = traits_type::eof()); | 125 | overflow(int_type c = traits_type::eof()); |
| 126 | 126 | ||
| 127 | /** | 127 | /** |
| 128 | * @brief Installs external stream buffer. | 128 | * @brief Installs external stream buffer. |
| 129 | * @param p Pointer to char buffer. | 129 | * @param p Pointer to char buffer. |
| 130 | * @param n Size of external buffer. | 130 | * @param n Size of external buffer. |
| 131 | * @return @c this on success, NULL on failure. | 131 | * @return @c this on success, NULL on failure. |
| 132 | * | 132 | * |
| 133 | * Call setbuf(0,0) to enable unbuffered output. | 133 | * Call setbuf(0,0) to enable unbuffered output. |
| 134 | */ | 134 | */ |
| 135 | virtual std::streambuf* | 135 | virtual std::streambuf* |
| 136 | setbuf(char_type* p, | 136 | setbuf(char_type* p, |
| 137 | std::streamsize n); | 137 | std::streamsize n); |
| 138 | 138 | ||
| 139 | /** | 139 | /** |
| 140 | * @brief Flush stream buffer to file. | 140 | * @brief Flush stream buffer to file. |
| 141 | * @return 0 on success, -1 on error. | 141 | * @return 0 on success, -1 on error. |
| 142 | * | 142 | * |
| 143 | * This calls underflow(EOF) to do the job. | 143 | * This calls underflow(EOF) to do the job. |
| 144 | */ | 144 | */ |
| 145 | virtual int | 145 | virtual int |
| 146 | sync(); | 146 | sync(); |
| 147 | 147 | ||
| 148 | // | 148 | // |
| 149 | // Some future enhancements | 149 | // Some future enhancements |
| 150 | // | 150 | // |
| 151 | // virtual int_type uflow(); | 151 | // virtual int_type uflow(); |
| 152 | // virtual int_type pbackfail(int_type c = traits_type::eof()); | 152 | // virtual int_type pbackfail(int_type c = traits_type::eof()); |
| 153 | // virtual pos_type | 153 | // virtual pos_type |
| 154 | // seekoff(off_type off, | 154 | // seekoff(off_type off, |
| 155 | // std::ios_base::seekdir way, | 155 | // std::ios_base::seekdir way, |
| 156 | // std::ios_base::openmode mode = std::ios_base::in|std::ios_base::out); | 156 | // std::ios_base::openmode mode = std::ios_base::in|std::ios_base::out); |
| 157 | // virtual pos_type | 157 | // virtual pos_type |
| 158 | // seekpos(pos_type sp, | 158 | // seekpos(pos_type sp, |
| 159 | // std::ios_base::openmode mode = std::ios_base::in|std::ios_base::out); | 159 | // std::ios_base::openmode mode = std::ios_base::in|std::ios_base::out); |
| 160 | 160 | ||
| 161 | private: | 161 | private: |
| 162 | /** | 162 | /** |
| 163 | * @brief Allocate internal buffer. | 163 | * @brief Allocate internal buffer. |
| 164 | * | 164 | * |
| 165 | * This function is safe to call multiple times. It will ensure | 165 | * This function is safe to call multiple times. It will ensure |
| 166 | * that a proper internal buffer exists if it is required. If the | 166 | * that a proper internal buffer exists if it is required. If the |
| 167 | * buffer already exists or is external, the buffer pointers will be | 167 | * buffer already exists or is external, the buffer pointers will be |
| 168 | * reset to their original state. | 168 | * reset to their original state. |
| 169 | */ | 169 | */ |
| 170 | void | 170 | void |
| 171 | enable_buffer(); | 171 | enable_buffer(); |
| 172 | 172 | ||
| 173 | /** | 173 | /** |
| 174 | * @brief Destroy internal buffer. | 174 | * @brief Destroy internal buffer. |
| 175 | * | 175 | * |
| 176 | * This function is safe to call multiple times. It will ensure | 176 | * This function is safe to call multiple times. It will ensure |
| 177 | * that the internal buffer is deallocated if it exists. In any | 177 | * that the internal buffer is deallocated if it exists. In any |
| 178 | * case, it will also reset the buffer pointers. | 178 | * case, it will also reset the buffer pointers. |
| 179 | */ | 179 | */ |
| 180 | void | 180 | void |
| 181 | disable_buffer(); | 181 | disable_buffer(); |
| 182 | 182 | ||
| 183 | /** | 183 | /** |
| 184 | * Underlying file pointer. | 184 | * Underlying file pointer. |
| 185 | */ | 185 | */ |
| 186 | gzFile file; | 186 | gzFile file; |
| 187 | 187 | ||
| 188 | /** | 188 | /** |
| 189 | * Mode in which file was opened. | 189 | * Mode in which file was opened. |
| 190 | */ | 190 | */ |
| 191 | std::ios_base::openmode io_mode; | 191 | std::ios_base::openmode io_mode; |
| 192 | 192 | ||
| 193 | /** | 193 | /** |
| 194 | * @brief True if this object owns file descriptor. | 194 | * @brief True if this object owns file descriptor. |
| 195 | * | 195 | * |
| 196 | * This makes the class responsible for closing the file | 196 | * This makes the class responsible for closing the file |
| 197 | * upon destruction. | 197 | * upon destruction. |
| 198 | */ | 198 | */ |
| 199 | bool own_fd; | 199 | bool own_fd; |
| 200 | 200 | ||
| 201 | /** | 201 | /** |
| 202 | * @brief Stream buffer. | 202 | * @brief Stream buffer. |
| 203 | * | 203 | * |
| 204 | * For simplicity this remains allocated on the free store for the | 204 | * For simplicity this remains allocated on the free store for the |
| 205 | * entire life span of the gzfilebuf object, unless replaced by setbuf. | 205 | * entire life span of the gzfilebuf object, unless replaced by setbuf. |
| 206 | */ | 206 | */ |
| 207 | char_type* buffer; | 207 | char_type* buffer; |
| 208 | 208 | ||
| 209 | /** | 209 | /** |
| 210 | * @brief Stream buffer size. | 210 | * @brief Stream buffer size. |
| 211 | * | 211 | * |
| 212 | * Defaults to system default buffer size (typically 8192 bytes). | 212 | * Defaults to system default buffer size (typically 8192 bytes). |
| 213 | * Modified by setbuf. | 213 | * Modified by setbuf. |
| 214 | */ | 214 | */ |
| 215 | std::streamsize buffer_size; | 215 | std::streamsize buffer_size; |
| 216 | 216 | ||
| 217 | /** | 217 | /** |
| 218 | * @brief True if this object owns stream buffer. | 218 | * @brief True if this object owns stream buffer. |
| 219 | * | 219 | * |
| 220 | * This makes the class responsible for deleting the buffer | 220 | * This makes the class responsible for deleting the buffer |
| 221 | * upon destruction. | 221 | * upon destruction. |
| 222 | */ | 222 | */ |
| 223 | bool own_buffer; | 223 | bool own_buffer; |
| @@ -231,49 +231,49 @@ private: | |||
| 231 | * This class implements ifstream for gzipped files. Seeking and putback | 231 | * This class implements ifstream for gzipped files. Seeking and putback |
| 232 | * is not supported yet. | 232 | * is not supported yet. |
| 233 | */ | 233 | */ |
| 234 | class gzifstream : public std::istream | 234 | class gzifstream : public std::istream |
| 235 | { | 235 | { |
| 236 | public: | 236 | public: |
| 237 | // Default constructor | 237 | // Default constructor |
| 238 | gzifstream(); | 238 | gzifstream(); |
| 239 | 239 | ||
| 240 | /** | 240 | /** |
| 241 | * @brief Construct stream on gzipped file to be opened. | 241 | * @brief Construct stream on gzipped file to be opened. |
| 242 | * @param name File name. | 242 | * @param name File name. |
| 243 | * @param mode Open mode flags (forced to contain ios::in). | 243 | * @param mode Open mode flags (forced to contain ios::in). |
| 244 | */ | 244 | */ |
| 245 | explicit | 245 | explicit |
| 246 | gzifstream(const char* name, | 246 | gzifstream(const char* name, |
| 247 | std::ios_base::openmode mode = std::ios_base::in); | 247 | std::ios_base::openmode mode = std::ios_base::in); |
| 248 | 248 | ||
| 249 | /** | 249 | /** |
| 250 | * @brief Construct stream on already open gzipped file. | 250 | * @brief Construct stream on already open gzipped file. |
| 251 | * @param fd File descriptor. | 251 | * @param fd File descriptor. |
| 252 | * @param mode Open mode flags (forced to contain ios::in). | 252 | * @param mode Open mode flags (forced to contain ios::in). |
| 253 | */ | 253 | */ |
| 254 | explicit | 254 | explicit |
| 255 | gzifstream(int fd, | 255 | gzifstream(int fd, |
| 256 | std::ios_base::openmode mode = std::ios_base::in); | 256 | std::ios_base::openmode mode = std::ios_base::in); |
| 257 | 257 | ||
| 258 | /** | 258 | /** |
| 259 | * Obtain underlying stream buffer. | 259 | * Obtain underlying stream buffer. |
| 260 | */ | 260 | */ |
| 261 | gzfilebuf* | 261 | gzfilebuf* |
| 262 | rdbuf() const | 262 | rdbuf() const |
| 263 | { return const_cast<gzfilebuf*>(&sb); } | 263 | { return const_cast<gzfilebuf*>(&sb); } |
| 264 | 264 | ||
| 265 | /** | 265 | /** |
| 266 | * @brief Check if file is open. | 266 | * @brief Check if file is open. |
| 267 | * @return True if file is open. | 267 | * @return True if file is open. |
| 268 | */ | 268 | */ |
| 269 | bool | 269 | bool |
| 270 | is_open() { return sb.is_open(); } | 270 | is_open() { return sb.is_open(); } |
| 271 | 271 | ||
| 272 | /** | 272 | /** |
| 273 | * @brief Open gzipped file. | 273 | * @brief Open gzipped file. |
| 274 | * @param name File name. | 274 | * @param name File name. |
| 275 | * @param mode Open mode flags (forced to contain ios::in). | 275 | * @param mode Open mode flags (forced to contain ios::in). |
| 276 | * | 276 | * |
| 277 | * Stream will be in state good() if file opens successfully; | 277 | * Stream will be in state good() if file opens successfully; |
| 278 | * otherwise in state fail(). This differs from the behavior of | 278 | * otherwise in state fail(). This differs from the behavior of |
| 279 | * ifstream, which never sets the state to good() and therefore | 279 | * ifstream, which never sets the state to good() and therefore |
| @@ -281,34 +281,34 @@ public: | |||
| 281 | * you manually clear() the state. The choice is a matter of | 281 | * you manually clear() the state. The choice is a matter of |
| 282 | * convenience. | 282 | * convenience. |
| 283 | */ | 283 | */ |
| 284 | void | 284 | void |
| 285 | open(const char* name, | 285 | open(const char* name, |
| 286 | std::ios_base::openmode mode = std::ios_base::in); | 286 | std::ios_base::openmode mode = std::ios_base::in); |
| 287 | 287 | ||
| 288 | /** | 288 | /** |
| 289 | * @brief Attach to already open gzipped file. | 289 | * @brief Attach to already open gzipped file. |
| 290 | * @param fd File descriptor. | 290 | * @param fd File descriptor. |
| 291 | * @param mode Open mode flags (forced to contain ios::in). | 291 | * @param mode Open mode flags (forced to contain ios::in). |
| 292 | * | 292 | * |
| 293 | * Stream will be in state good() if attach succeeded; otherwise | 293 | * Stream will be in state good() if attach succeeded; otherwise |
| 294 | * in state fail(). | 294 | * in state fail(). |
| 295 | */ | 295 | */ |
| 296 | void | 296 | void |
| 297 | attach(int fd, | 297 | attach(int fd, |
| 298 | std::ios_base::openmode mode = std::ios_base::in); | 298 | std::ios_base::openmode mode = std::ios_base::in); |
| 299 | 299 | ||
| 300 | /** | 300 | /** |
| 301 | * @brief Close gzipped file. | 301 | * @brief Close gzipped file. |
| 302 | * | 302 | * |
| 303 | * Stream will be in state fail() if close failed. | 303 | * Stream will be in state fail() if close failed. |
| 304 | */ | 304 | */ |
| 305 | void | 305 | void |
| 306 | close(); | 306 | close(); |
| 307 | 307 | ||
| 308 | private: | 308 | private: |
| 309 | /** | 309 | /** |
| 310 | * Underlying stream buffer. | 310 | * Underlying stream buffer. |
| 311 | */ | 311 | */ |
| 312 | gzfilebuf sb; | 312 | gzfilebuf sb; |
| 313 | }; | 313 | }; |
| 314 | 314 | ||
| @@ -325,44 +325,44 @@ class gzofstream : public std::ostream | |||
| 325 | public: | 325 | public: |
| 326 | // Default constructor | 326 | // Default constructor |
| 327 | gzofstream(); | 327 | gzofstream(); |
| 328 | 328 | ||
| 329 | /** | 329 | /** |
| 330 | * @brief Construct stream on gzipped file to be opened. | 330 | * @brief Construct stream on gzipped file to be opened. |
| 331 | * @param name File name. | 331 | * @param name File name. |
| 332 | * @param mode Open mode flags (forced to contain ios::out). | 332 | * @param mode Open mode flags (forced to contain ios::out). |
| 333 | */ | 333 | */ |
| 334 | explicit | 334 | explicit |
| 335 | gzofstream(const char* name, | 335 | gzofstream(const char* name, |
| 336 | std::ios_base::openmode mode = std::ios_base::out); | 336 | std::ios_base::openmode mode = std::ios_base::out); |
| 337 | 337 | ||
| 338 | /** | 338 | /** |
| 339 | * @brief Construct stream on already open gzipped file. | 339 | * @brief Construct stream on already open gzipped file. |
| 340 | * @param fd File descriptor. | 340 | * @param fd File descriptor. |
| 341 | * @param mode Open mode flags (forced to contain ios::out). | 341 | * @param mode Open mode flags (forced to contain ios::out). |
| 342 | */ | 342 | */ |
| 343 | explicit | 343 | explicit |
| 344 | gzofstream(int fd, | 344 | gzofstream(int fd, |
| 345 | std::ios_base::openmode mode = std::ios_base::out); | 345 | std::ios_base::openmode mode = std::ios_base::out); |
| 346 | 346 | ||
| 347 | /** | 347 | /** |
| 348 | * Obtain underlying stream buffer. | 348 | * Obtain underlying stream buffer. |
| 349 | */ | 349 | */ |
| 350 | gzfilebuf* | 350 | gzfilebuf* |
| 351 | rdbuf() const | 351 | rdbuf() const |
| 352 | { return const_cast<gzfilebuf*>(&sb); } | 352 | { return const_cast<gzfilebuf*>(&sb); } |
| 353 | 353 | ||
| 354 | /** | 354 | /** |
| 355 | * @brief Check if file is open. | 355 | * @brief Check if file is open. |
| 356 | * @return True if file is open. | 356 | * @return True if file is open. |
| 357 | */ | 357 | */ |
| 358 | bool | 358 | bool |
| 359 | is_open() { return sb.is_open(); } | 359 | is_open() { return sb.is_open(); } |
| 360 | 360 | ||
| 361 | /** | 361 | /** |
| 362 | * @brief Open gzipped file. | 362 | * @brief Open gzipped file. |
| 363 | * @param name File name. | 363 | * @param name File name. |
| 364 | * @param mode Open mode flags (forced to contain ios::out). | 364 | * @param mode Open mode flags (forced to contain ios::out). |
| 365 | * | 365 | * |
| 366 | * Stream will be in state good() if file opens successfully; | 366 | * Stream will be in state good() if file opens successfully; |
| 367 | * otherwise in state fail(). This differs from the behavior of | 367 | * otherwise in state fail(). This differs from the behavior of |
| 368 | * ofstream, which never sets the state to good() and therefore | 368 | * ofstream, which never sets the state to good() and therefore |
| @@ -370,39 +370,39 @@ public: | |||
| 370 | * you manually clear() the state. The choice is a matter of | 370 | * you manually clear() the state. The choice is a matter of |
| 371 | * convenience. | 371 | * convenience. |
| 372 | */ | 372 | */ |
| 373 | void | 373 | void |
| 374 | open(const char* name, | 374 | open(const char* name, |
| 375 | std::ios_base::openmode mode = std::ios_base::out); | 375 | std::ios_base::openmode mode = std::ios_base::out); |
| 376 | 376 | ||
| 377 | /** | 377 | /** |
| 378 | * @brief Attach to already open gzipped file. | 378 | * @brief Attach to already open gzipped file. |
| 379 | * @param fd File descriptor. | 379 | * @param fd File descriptor. |
| 380 | * @param mode Open mode flags (forced to contain ios::out). | 380 | * @param mode Open mode flags (forced to contain ios::out). |
| 381 | * | 381 | * |
| 382 | * Stream will be in state good() if attach succeeded; otherwise | 382 | * Stream will be in state good() if attach succeeded; otherwise |
| 383 | * in state fail(). | 383 | * in state fail(). |
| 384 | */ | 384 | */ |
| 385 | void | 385 | void |
| 386 | attach(int fd, | 386 | attach(int fd, |
| 387 | std::ios_base::openmode mode = std::ios_base::out); | 387 | std::ios_base::openmode mode = std::ios_base::out); |
| 388 | 388 | ||
| 389 | /** | 389 | /** |
| 390 | * @brief Close gzipped file. | 390 | * @brief Close gzipped file. |
| 391 | * | 391 | * |
| 392 | * Stream will be in state fail() if close failed. | 392 | * Stream will be in state fail() if close failed. |
| 393 | */ | 393 | */ |
| 394 | void | 394 | void |
| 395 | close(); | 395 | close(); |
| 396 | 396 | ||
| 397 | private: | 397 | private: |
| 398 | /** | 398 | /** |
| 399 | * Underlying stream buffer. | 399 | * Underlying stream buffer. |
| 400 | */ | 400 | */ |
| 401 | gzfilebuf sb; | 401 | gzfilebuf sb; |
| 402 | }; | 402 | }; |
| 403 | 403 | ||
| 404 | /*****************************************************************************/ | 404 | /*****************************************************************************/ |
| 405 | 405 | ||
| 406 | /** | 406 | /** |
| 407 | * @brief Gzipped file output stream manipulator class. | 407 | * @brief Gzipped file output stream manipulator class. |
| 408 | * | 408 | * |
| @@ -415,28 +415,28 @@ template<typename T1, typename T2> | |||
| 415 | public: | 415 | public: |
| 416 | // Allows insertor to peek at internals | 416 | // Allows insertor to peek at internals |
| 417 | template <typename Ta, typename Tb> | 417 | template <typename Ta, typename Tb> |
| 418 | friend gzofstream& | 418 | friend gzofstream& |
| 419 | operator<<(gzofstream&, | 419 | operator<<(gzofstream&, |
| 420 | const gzomanip2<Ta,Tb>&); | 420 | const gzomanip2<Ta,Tb>&); |
| 421 | 421 | ||
| 422 | // Constructor | 422 | // Constructor |
| 423 | gzomanip2(gzofstream& (*f)(gzofstream&, T1, T2), | 423 | gzomanip2(gzofstream& (*f)(gzofstream&, T1, T2), |
| 424 | T1 v1, | 424 | T1 v1, |
| 425 | T2 v2); | 425 | T2 v2); |
| 426 | private: | 426 | private: |
| 427 | // Underlying manipulator function | 427 | // Underlying manipulator function |
| 428 | gzofstream& | 428 | gzofstream& |
| 429 | (*func)(gzofstream&, T1, T2); | 429 | (*func)(gzofstream&, T1, T2); |
| 430 | 430 | ||
| 431 | // Arguments for manipulator function | 431 | // Arguments for manipulator function |
| 432 | T1 val1; | 432 | T1 val1; |
| 433 | T2 val2; | 433 | T2 val2; |
| 434 | }; | 434 | }; |
| 435 | 435 | ||
| 436 | /*****************************************************************************/ | 436 | /*****************************************************************************/ |
| 437 | 437 | ||
| 438 | // Manipulator function thunks through to stream buffer | 438 | // Manipulator function thunks through to stream buffer |
| 439 | inline gzofstream& | 439 | inline gzofstream& |
| 440 | setcompression(gzofstream &gzs, int l, int s = Z_DEFAULT_STRATEGY) | 440 | setcompression(gzofstream &gzs, int l, int s = Z_DEFAULT_STRATEGY) |
| 441 | { | 441 | { |
| 442 | (gzs.rdbuf())->setcompression(l, s); | 442 | (gzs.rdbuf())->setcompression(l, s); |
| @@ -445,22 +445,22 @@ setcompression(gzofstream &gzs, int l, int s = Z_DEFAULT_STRATEGY) | |||
| 445 | 445 | ||
| 446 | // Manipulator constructor stores arguments | 446 | // Manipulator constructor stores arguments |
| 447 | template<typename T1, typename T2> | 447 | template<typename T1, typename T2> |
| 448 | inline | 448 | inline |
| 449 | gzomanip2<T1,T2>::gzomanip2(gzofstream &(*f)(gzofstream &, T1, T2), | 449 | gzomanip2<T1,T2>::gzomanip2(gzofstream &(*f)(gzofstream &, T1, T2), |
| 450 | T1 v1, | 450 | T1 v1, |
| 451 | T2 v2) | 451 | T2 v2) |
| 452 | : func(f), val1(v1), val2(v2) | 452 | : func(f), val1(v1), val2(v2) |
| 453 | { } | 453 | { } |
| 454 | 454 | ||
| 455 | // Insertor applies underlying manipulator function to stream | 455 | // Insertor applies underlying manipulator function to stream |
| 456 | template<typename T1, typename T2> | 456 | template<typename T1, typename T2> |
| 457 | inline gzofstream& | 457 | inline gzofstream& |
| 458 | operator<<(gzofstream& s, const gzomanip2<T1,T2>& m) | 458 | operator<<(gzofstream& s, const gzomanip2<T1,T2>& m) |
| 459 | { return (*m.func)(s, m.val1, m.val2); } | 459 | { return (*m.func)(s, m.val1, m.val2); } |
| 460 | 460 | ||
| 461 | // Insert this onto stream to simplify setting of compression level | 461 | // Insert this onto stream to simplify setting of compression level |
| 462 | inline gzomanip2<int,int> | 462 | inline gzomanip2<int,int> |
| 463 | setcompression(int l, int s = Z_DEFAULT_STRATEGY) | 463 | setcompression(int l, int s = Z_DEFAULT_STRATEGY) |
| 464 | { return gzomanip2<int,int>(&setcompression, l, s); } | 464 | { return gzomanip2<int,int>(&setcompression, l, s); } |
| 465 | 465 | ||
| 466 | #endif // ZFSTREAM_H | 466 | #endif // ZFSTREAM_H |
