annotate src/zlib-1.2.8/contrib/iostream3/zfstream.cc @ 128:5b4145a0d408

Current zlib source
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 18 Oct 2016 14:33:52 +0100
parents
children
rev   line source
cannam@128 1 /*
cannam@128 2 * A C++ I/O streams interface to the zlib gz* functions
cannam@128 3 *
cannam@128 4 * by Ludwig Schwardt <schwardt@sun.ac.za>
cannam@128 5 * original version by Kevin Ruland <kevin@rodin.wustl.edu>
cannam@128 6 *
cannam@128 7 * This version is standard-compliant and compatible with gcc 3.x.
cannam@128 8 */
cannam@128 9
cannam@128 10 #include "zfstream.h"
cannam@128 11 #include <cstring> // for strcpy, strcat, strlen (mode strings)
cannam@128 12 #include <cstdio> // for BUFSIZ
cannam@128 13
cannam@128 14 // Internal buffer sizes (default and "unbuffered" versions)
cannam@128 15 #define BIGBUFSIZE BUFSIZ
cannam@128 16 #define SMALLBUFSIZE 1
cannam@128 17
cannam@128 18 /*****************************************************************************/
cannam@128 19
cannam@128 20 // Default constructor
cannam@128 21 gzfilebuf::gzfilebuf()
cannam@128 22 : file(NULL), io_mode(std::ios_base::openmode(0)), own_fd(false),
cannam@128 23 buffer(NULL), buffer_size(BIGBUFSIZE), own_buffer(true)
cannam@128 24 {
cannam@128 25 // No buffers to start with
cannam@128 26 this->disable_buffer();
cannam@128 27 }
cannam@128 28
cannam@128 29 // Destructor
cannam@128 30 gzfilebuf::~gzfilebuf()
cannam@128 31 {
cannam@128 32 // Sync output buffer and close only if responsible for file
cannam@128 33 // (i.e. attached streams should be left open at this stage)
cannam@128 34 this->sync();
cannam@128 35 if (own_fd)
cannam@128 36 this->close();
cannam@128 37 // Make sure internal buffer is deallocated
cannam@128 38 this->disable_buffer();
cannam@128 39 }
cannam@128 40
cannam@128 41 // Set compression level and strategy
cannam@128 42 int
cannam@128 43 gzfilebuf::setcompression(int comp_level,
cannam@128 44 int comp_strategy)
cannam@128 45 {
cannam@128 46 return gzsetparams(file, comp_level, comp_strategy);
cannam@128 47 }
cannam@128 48
cannam@128 49 // Open gzipped file
cannam@128 50 gzfilebuf*
cannam@128 51 gzfilebuf::open(const char *name,
cannam@128 52 std::ios_base::openmode mode)
cannam@128 53 {
cannam@128 54 // Fail if file already open
cannam@128 55 if (this->is_open())
cannam@128 56 return NULL;
cannam@128 57 // Don't support simultaneous read/write access (yet)
cannam@128 58 if ((mode & std::ios_base::in) && (mode & std::ios_base::out))
cannam@128 59 return NULL;
cannam@128 60
cannam@128 61 // Build mode string for gzopen and check it [27.8.1.3.2]
cannam@128 62 char char_mode[6] = "\0\0\0\0\0";
cannam@128 63 if (!this->open_mode(mode, char_mode))
cannam@128 64 return NULL;
cannam@128 65
cannam@128 66 // Attempt to open file
cannam@128 67 if ((file = gzopen(name, char_mode)) == NULL)
cannam@128 68 return NULL;
cannam@128 69
cannam@128 70 // On success, allocate internal buffer and set flags
cannam@128 71 this->enable_buffer();
cannam@128 72 io_mode = mode;
cannam@128 73 own_fd = true;
cannam@128 74 return this;
cannam@128 75 }
cannam@128 76
cannam@128 77 // Attach to gzipped file
cannam@128 78 gzfilebuf*
cannam@128 79 gzfilebuf::attach(int fd,
cannam@128 80 std::ios_base::openmode mode)
cannam@128 81 {
cannam@128 82 // Fail if file already open
cannam@128 83 if (this->is_open())
cannam@128 84 return NULL;
cannam@128 85 // Don't support simultaneous read/write access (yet)
cannam@128 86 if ((mode & std::ios_base::in) && (mode & std::ios_base::out))
cannam@128 87 return NULL;
cannam@128 88
cannam@128 89 // Build mode string for gzdopen and check it [27.8.1.3.2]
cannam@128 90 char char_mode[6] = "\0\0\0\0\0";
cannam@128 91 if (!this->open_mode(mode, char_mode))
cannam@128 92 return NULL;
cannam@128 93
cannam@128 94 // Attempt to attach to file
cannam@128 95 if ((file = gzdopen(fd, char_mode)) == NULL)
cannam@128 96 return NULL;
cannam@128 97
cannam@128 98 // On success, allocate internal buffer and set flags
cannam@128 99 this->enable_buffer();
cannam@128 100 io_mode = mode;
cannam@128 101 own_fd = false;
cannam@128 102 return this;
cannam@128 103 }
cannam@128 104
cannam@128 105 // Close gzipped file
cannam@128 106 gzfilebuf*
cannam@128 107 gzfilebuf::close()
cannam@128 108 {
cannam@128 109 // Fail immediately if no file is open
cannam@128 110 if (!this->is_open())
cannam@128 111 return NULL;
cannam@128 112 // Assume success
cannam@128 113 gzfilebuf* retval = this;
cannam@128 114 // Attempt to sync and close gzipped file
cannam@128 115 if (this->sync() == -1)
cannam@128 116 retval = NULL;
cannam@128 117 if (gzclose(file) < 0)
cannam@128 118 retval = NULL;
cannam@128 119 // File is now gone anyway (postcondition [27.8.1.3.8])
cannam@128 120 file = NULL;
cannam@128 121 own_fd = false;
cannam@128 122 // Destroy internal buffer if it exists
cannam@128 123 this->disable_buffer();
cannam@128 124 return retval;
cannam@128 125 }
cannam@128 126
cannam@128 127 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
cannam@128 128
cannam@128 129 // Convert int open mode to mode string
cannam@128 130 bool
cannam@128 131 gzfilebuf::open_mode(std::ios_base::openmode mode,
cannam@128 132 char* c_mode) const
cannam@128 133 {
cannam@128 134 bool testb = mode & std::ios_base::binary;
cannam@128 135 bool testi = mode & std::ios_base::in;
cannam@128 136 bool testo = mode & std::ios_base::out;
cannam@128 137 bool testt = mode & std::ios_base::trunc;
cannam@128 138 bool testa = mode & std::ios_base::app;
cannam@128 139
cannam@128 140 // Check for valid flag combinations - see [27.8.1.3.2] (Table 92)
cannam@128 141 // Original zfstream hardcoded the compression level to maximum here...
cannam@128 142 // Double the time for less than 1% size improvement seems
cannam@128 143 // excessive though - keeping it at the default level
cannam@128 144 // To change back, just append "9" to the next three mode strings
cannam@128 145 if (!testi && testo && !testt && !testa)
cannam@128 146 strcpy(c_mode, "w");
cannam@128 147 if (!testi && testo && !testt && testa)
cannam@128 148 strcpy(c_mode, "a");
cannam@128 149 if (!testi && testo && testt && !testa)
cannam@128 150 strcpy(c_mode, "w");
cannam@128 151 if (testi && !testo && !testt && !testa)
cannam@128 152 strcpy(c_mode, "r");
cannam@128 153 // No read/write mode yet
cannam@128 154 // if (testi && testo && !testt && !testa)
cannam@128 155 // strcpy(c_mode, "r+");
cannam@128 156 // if (testi && testo && testt && !testa)
cannam@128 157 // strcpy(c_mode, "w+");
cannam@128 158
cannam@128 159 // Mode string should be empty for invalid combination of flags
cannam@128 160 if (strlen(c_mode) == 0)
cannam@128 161 return false;
cannam@128 162 if (testb)
cannam@128 163 strcat(c_mode, "b");
cannam@128 164 return true;
cannam@128 165 }
cannam@128 166
cannam@128 167 // Determine number of characters in internal get buffer
cannam@128 168 std::streamsize
cannam@128 169 gzfilebuf::showmanyc()
cannam@128 170 {
cannam@128 171 // Calls to underflow will fail if file not opened for reading
cannam@128 172 if (!this->is_open() || !(io_mode & std::ios_base::in))
cannam@128 173 return -1;
cannam@128 174 // Make sure get area is in use
cannam@128 175 if (this->gptr() && (this->gptr() < this->egptr()))
cannam@128 176 return std::streamsize(this->egptr() - this->gptr());
cannam@128 177 else
cannam@128 178 return 0;
cannam@128 179 }
cannam@128 180
cannam@128 181 // Fill get area from gzipped file
cannam@128 182 gzfilebuf::int_type
cannam@128 183 gzfilebuf::underflow()
cannam@128 184 {
cannam@128 185 // If something is left in the get area by chance, return it
cannam@128 186 // (this shouldn't normally happen, as underflow is only supposed
cannam@128 187 // to be called when gptr >= egptr, but it serves as error check)
cannam@128 188 if (this->gptr() && (this->gptr() < this->egptr()))
cannam@128 189 return traits_type::to_int_type(*(this->gptr()));
cannam@128 190
cannam@128 191 // If the file hasn't been opened for reading, produce error
cannam@128 192 if (!this->is_open() || !(io_mode & std::ios_base::in))
cannam@128 193 return traits_type::eof();
cannam@128 194
cannam@128 195 // Attempt to fill internal buffer from gzipped file
cannam@128 196 // (buffer must be guaranteed to exist...)
cannam@128 197 int bytes_read = gzread(file, buffer, buffer_size);
cannam@128 198 // Indicates error or EOF
cannam@128 199 if (bytes_read <= 0)
cannam@128 200 {
cannam@128 201 // Reset get area
cannam@128 202 this->setg(buffer, buffer, buffer);
cannam@128 203 return traits_type::eof();
cannam@128 204 }
cannam@128 205 // Make all bytes read from file available as get area
cannam@128 206 this->setg(buffer, buffer, buffer + bytes_read);
cannam@128 207
cannam@128 208 // Return next character in get area
cannam@128 209 return traits_type::to_int_type(*(this->gptr()));
cannam@128 210 }
cannam@128 211
cannam@128 212 // Write put area to gzipped file
cannam@128 213 gzfilebuf::int_type
cannam@128 214 gzfilebuf::overflow(int_type c)
cannam@128 215 {
cannam@128 216 // Determine whether put area is in use
cannam@128 217 if (this->pbase())
cannam@128 218 {
cannam@128 219 // Double-check pointer range
cannam@128 220 if (this->pptr() > this->epptr() || this->pptr() < this->pbase())
cannam@128 221 return traits_type::eof();
cannam@128 222 // Add extra character to buffer if not EOF
cannam@128 223 if (!traits_type::eq_int_type(c, traits_type::eof()))
cannam@128 224 {
cannam@128 225 *(this->pptr()) = traits_type::to_char_type(c);
cannam@128 226 this->pbump(1);
cannam@128 227 }
cannam@128 228 // Number of characters to write to file
cannam@128 229 int bytes_to_write = this->pptr() - this->pbase();
cannam@128 230 // Overflow doesn't fail if nothing is to be written
cannam@128 231 if (bytes_to_write > 0)
cannam@128 232 {
cannam@128 233 // If the file hasn't been opened for writing, produce error
cannam@128 234 if (!this->is_open() || !(io_mode & std::ios_base::out))
cannam@128 235 return traits_type::eof();
cannam@128 236 // If gzipped file won't accept all bytes written to it, fail
cannam@128 237 if (gzwrite(file, this->pbase(), bytes_to_write) != bytes_to_write)
cannam@128 238 return traits_type::eof();
cannam@128 239 // Reset next pointer to point to pbase on success
cannam@128 240 this->pbump(-bytes_to_write);
cannam@128 241 }
cannam@128 242 }
cannam@128 243 // Write extra character to file if not EOF
cannam@128 244 else if (!traits_type::eq_int_type(c, traits_type::eof()))
cannam@128 245 {
cannam@128 246 // If the file hasn't been opened for writing, produce error
cannam@128 247 if (!this->is_open() || !(io_mode & std::ios_base::out))
cannam@128 248 return traits_type::eof();
cannam@128 249 // Impromptu char buffer (allows "unbuffered" output)
cannam@128 250 char_type last_char = traits_type::to_char_type(c);
cannam@128 251 // If gzipped file won't accept this character, fail
cannam@128 252 if (gzwrite(file, &last_char, 1) != 1)
cannam@128 253 return traits_type::eof();
cannam@128 254 }
cannam@128 255
cannam@128 256 // If you got here, you have succeeded (even if c was EOF)
cannam@128 257 // The return value should therefore be non-EOF
cannam@128 258 if (traits_type::eq_int_type(c, traits_type::eof()))
cannam@128 259 return traits_type::not_eof(c);
cannam@128 260 else
cannam@128 261 return c;
cannam@128 262 }
cannam@128 263
cannam@128 264 // Assign new buffer
cannam@128 265 std::streambuf*
cannam@128 266 gzfilebuf::setbuf(char_type* p,
cannam@128 267 std::streamsize n)
cannam@128 268 {
cannam@128 269 // First make sure stuff is sync'ed, for safety
cannam@128 270 if (this->sync() == -1)
cannam@128 271 return NULL;
cannam@128 272 // If buffering is turned off on purpose via setbuf(0,0), still allocate one...
cannam@128 273 // "Unbuffered" only really refers to put [27.8.1.4.10], while get needs at
cannam@128 274 // least a buffer of size 1 (very inefficient though, therefore make it bigger?)
cannam@128 275 // This follows from [27.5.2.4.3]/12 (gptr needs to point at something, it seems)
cannam@128 276 if (!p || !n)
cannam@128 277 {
cannam@128 278 // Replace existing buffer (if any) with small internal buffer
cannam@128 279 this->disable_buffer();
cannam@128 280 buffer = NULL;
cannam@128 281 buffer_size = 0;
cannam@128 282 own_buffer = true;
cannam@128 283 this->enable_buffer();
cannam@128 284 }
cannam@128 285 else
cannam@128 286 {
cannam@128 287 // Replace existing buffer (if any) with external buffer
cannam@128 288 this->disable_buffer();
cannam@128 289 buffer = p;
cannam@128 290 buffer_size = n;
cannam@128 291 own_buffer = false;
cannam@128 292 this->enable_buffer();
cannam@128 293 }
cannam@128 294 return this;
cannam@128 295 }
cannam@128 296
cannam@128 297 // Write put area to gzipped file (i.e. ensures that put area is empty)
cannam@128 298 int
cannam@128 299 gzfilebuf::sync()
cannam@128 300 {
cannam@128 301 return traits_type::eq_int_type(this->overflow(), traits_type::eof()) ? -1 : 0;
cannam@128 302 }
cannam@128 303
cannam@128 304 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
cannam@128 305
cannam@128 306 // Allocate internal buffer
cannam@128 307 void
cannam@128 308 gzfilebuf::enable_buffer()
cannam@128 309 {
cannam@128 310 // If internal buffer required, allocate one
cannam@128 311 if (own_buffer && !buffer)
cannam@128 312 {
cannam@128 313 // Check for buffered vs. "unbuffered"
cannam@128 314 if (buffer_size > 0)
cannam@128 315 {
cannam@128 316 // Allocate internal buffer
cannam@128 317 buffer = new char_type[buffer_size];
cannam@128 318 // Get area starts empty and will be expanded by underflow as need arises
cannam@128 319 this->setg(buffer, buffer, buffer);
cannam@128 320 // Setup entire internal buffer as put area.
cannam@128 321 // The one-past-end pointer actually points to the last element of the buffer,
cannam@128 322 // so that overflow(c) can safely add the extra character c to the sequence.
cannam@128 323 // These pointers remain in place for the duration of the buffer
cannam@128 324 this->setp(buffer, buffer + buffer_size - 1);
cannam@128 325 }
cannam@128 326 else
cannam@128 327 {
cannam@128 328 // Even in "unbuffered" case, (small?) get buffer is still required
cannam@128 329 buffer_size = SMALLBUFSIZE;
cannam@128 330 buffer = new char_type[buffer_size];
cannam@128 331 this->setg(buffer, buffer, buffer);
cannam@128 332 // "Unbuffered" means no put buffer
cannam@128 333 this->setp(0, 0);
cannam@128 334 }
cannam@128 335 }
cannam@128 336 else
cannam@128 337 {
cannam@128 338 // If buffer already allocated, reset buffer pointers just to make sure no
cannam@128 339 // stale chars are lying around
cannam@128 340 this->setg(buffer, buffer, buffer);
cannam@128 341 this->setp(buffer, buffer + buffer_size - 1);
cannam@128 342 }
cannam@128 343 }
cannam@128 344
cannam@128 345 // Destroy internal buffer
cannam@128 346 void
cannam@128 347 gzfilebuf::disable_buffer()
cannam@128 348 {
cannam@128 349 // If internal buffer exists, deallocate it
cannam@128 350 if (own_buffer && buffer)
cannam@128 351 {
cannam@128 352 // Preserve unbuffered status by zeroing size
cannam@128 353 if (!this->pbase())
cannam@128 354 buffer_size = 0;
cannam@128 355 delete[] buffer;
cannam@128 356 buffer = NULL;
cannam@128 357 this->setg(0, 0, 0);
cannam@128 358 this->setp(0, 0);
cannam@128 359 }
cannam@128 360 else
cannam@128 361 {
cannam@128 362 // Reset buffer pointers to initial state if external buffer exists
cannam@128 363 this->setg(buffer, buffer, buffer);
cannam@128 364 if (buffer)
cannam@128 365 this->setp(buffer, buffer + buffer_size - 1);
cannam@128 366 else
cannam@128 367 this->setp(0, 0);
cannam@128 368 }
cannam@128 369 }
cannam@128 370
cannam@128 371 /*****************************************************************************/
cannam@128 372
cannam@128 373 // Default constructor initializes stream buffer
cannam@128 374 gzifstream::gzifstream()
cannam@128 375 : std::istream(NULL), sb()
cannam@128 376 { this->init(&sb); }
cannam@128 377
cannam@128 378 // Initialize stream buffer and open file
cannam@128 379 gzifstream::gzifstream(const char* name,
cannam@128 380 std::ios_base::openmode mode)
cannam@128 381 : std::istream(NULL), sb()
cannam@128 382 {
cannam@128 383 this->init(&sb);
cannam@128 384 this->open(name, mode);
cannam@128 385 }
cannam@128 386
cannam@128 387 // Initialize stream buffer and attach to file
cannam@128 388 gzifstream::gzifstream(int fd,
cannam@128 389 std::ios_base::openmode mode)
cannam@128 390 : std::istream(NULL), sb()
cannam@128 391 {
cannam@128 392 this->init(&sb);
cannam@128 393 this->attach(fd, mode);
cannam@128 394 }
cannam@128 395
cannam@128 396 // Open file and go into fail() state if unsuccessful
cannam@128 397 void
cannam@128 398 gzifstream::open(const char* name,
cannam@128 399 std::ios_base::openmode mode)
cannam@128 400 {
cannam@128 401 if (!sb.open(name, mode | std::ios_base::in))
cannam@128 402 this->setstate(std::ios_base::failbit);
cannam@128 403 else
cannam@128 404 this->clear();
cannam@128 405 }
cannam@128 406
cannam@128 407 // Attach to file and go into fail() state if unsuccessful
cannam@128 408 void
cannam@128 409 gzifstream::attach(int fd,
cannam@128 410 std::ios_base::openmode mode)
cannam@128 411 {
cannam@128 412 if (!sb.attach(fd, mode | std::ios_base::in))
cannam@128 413 this->setstate(std::ios_base::failbit);
cannam@128 414 else
cannam@128 415 this->clear();
cannam@128 416 }
cannam@128 417
cannam@128 418 // Close file
cannam@128 419 void
cannam@128 420 gzifstream::close()
cannam@128 421 {
cannam@128 422 if (!sb.close())
cannam@128 423 this->setstate(std::ios_base::failbit);
cannam@128 424 }
cannam@128 425
cannam@128 426 /*****************************************************************************/
cannam@128 427
cannam@128 428 // Default constructor initializes stream buffer
cannam@128 429 gzofstream::gzofstream()
cannam@128 430 : std::ostream(NULL), sb()
cannam@128 431 { this->init(&sb); }
cannam@128 432
cannam@128 433 // Initialize stream buffer and open file
cannam@128 434 gzofstream::gzofstream(const char* name,
cannam@128 435 std::ios_base::openmode mode)
cannam@128 436 : std::ostream(NULL), sb()
cannam@128 437 {
cannam@128 438 this->init(&sb);
cannam@128 439 this->open(name, mode);
cannam@128 440 }
cannam@128 441
cannam@128 442 // Initialize stream buffer and attach to file
cannam@128 443 gzofstream::gzofstream(int fd,
cannam@128 444 std::ios_base::openmode mode)
cannam@128 445 : std::ostream(NULL), sb()
cannam@128 446 {
cannam@128 447 this->init(&sb);
cannam@128 448 this->attach(fd, mode);
cannam@128 449 }
cannam@128 450
cannam@128 451 // Open file and go into fail() state if unsuccessful
cannam@128 452 void
cannam@128 453 gzofstream::open(const char* name,
cannam@128 454 std::ios_base::openmode mode)
cannam@128 455 {
cannam@128 456 if (!sb.open(name, mode | std::ios_base::out))
cannam@128 457 this->setstate(std::ios_base::failbit);
cannam@128 458 else
cannam@128 459 this->clear();
cannam@128 460 }
cannam@128 461
cannam@128 462 // Attach to file and go into fail() state if unsuccessful
cannam@128 463 void
cannam@128 464 gzofstream::attach(int fd,
cannam@128 465 std::ios_base::openmode mode)
cannam@128 466 {
cannam@128 467 if (!sb.attach(fd, mode | std::ios_base::out))
cannam@128 468 this->setstate(std::ios_base::failbit);
cannam@128 469 else
cannam@128 470 this->clear();
cannam@128 471 }
cannam@128 472
cannam@128 473 // Close file
cannam@128 474 void
cannam@128 475 gzofstream::close()
cannam@128 476 {
cannam@128 477 if (!sb.close())
cannam@128 478 this->setstate(std::ios_base::failbit);
cannam@128 479 }