annotate src/zlib-1.2.7/contrib/iostream3/zfstream.cc @ 169:223a55898ab9 tip default

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