annotate src/zlib-1.2.7/contrib/iostream3/zfstream.cc @ 4:e13257ea84a4

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