diff options
Diffstat (limited to 'contrib/iostream3/zfstream.cc')
-rw-r--r-- | contrib/iostream3/zfstream.cc | 132 |
1 files changed, 66 insertions, 66 deletions
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()) |