annotate src/zlib-1.2.8/contrib/iostream2/zstream.h @ 56:af97cad61ff0

Add updated build of PortAudio for OSX
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 03 Jan 2017 15:10:52 +0000
parents 5ea0608b923f
children
rev   line source
Chris@43 1 /*
Chris@43 2 *
Chris@43 3 * Copyright (c) 1997
Chris@43 4 * Christian Michelsen Research AS
Chris@43 5 * Advanced Computing
Chris@43 6 * Fantoftvegen 38, 5036 BERGEN, Norway
Chris@43 7 * http://www.cmr.no
Chris@43 8 *
Chris@43 9 * Permission to use, copy, modify, distribute and sell this software
Chris@43 10 * and its documentation for any purpose is hereby granted without fee,
Chris@43 11 * provided that the above copyright notice appear in all copies and
Chris@43 12 * that both that copyright notice and this permission notice appear
Chris@43 13 * in supporting documentation. Christian Michelsen Research AS makes no
Chris@43 14 * representations about the suitability of this software for any
Chris@43 15 * purpose. It is provided "as is" without express or implied warranty.
Chris@43 16 *
Chris@43 17 */
Chris@43 18
Chris@43 19 #ifndef ZSTREAM__H
Chris@43 20 #define ZSTREAM__H
Chris@43 21
Chris@43 22 /*
Chris@43 23 * zstream.h - C++ interface to the 'zlib' general purpose compression library
Chris@43 24 * $Id: zstream.h 1.1 1997-06-25 12:00:56+02 tyge Exp tyge $
Chris@43 25 */
Chris@43 26
Chris@43 27 #include <strstream.h>
Chris@43 28 #include <string.h>
Chris@43 29 #include <stdio.h>
Chris@43 30 #include "zlib.h"
Chris@43 31
Chris@43 32 #if defined(_WIN32)
Chris@43 33 # include <fcntl.h>
Chris@43 34 # include <io.h>
Chris@43 35 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
Chris@43 36 #else
Chris@43 37 # define SET_BINARY_MODE(file)
Chris@43 38 #endif
Chris@43 39
Chris@43 40 class zstringlen {
Chris@43 41 public:
Chris@43 42 zstringlen(class izstream&);
Chris@43 43 zstringlen(class ozstream&, const char*);
Chris@43 44 size_t value() const { return val.word; }
Chris@43 45 private:
Chris@43 46 struct Val { unsigned char byte; size_t word; } val;
Chris@43 47 };
Chris@43 48
Chris@43 49 // ----------------------------- izstream -----------------------------
Chris@43 50
Chris@43 51 class izstream
Chris@43 52 {
Chris@43 53 public:
Chris@43 54 izstream() : m_fp(0) {}
Chris@43 55 izstream(FILE* fp) : m_fp(0) { open(fp); }
Chris@43 56 izstream(const char* name) : m_fp(0) { open(name); }
Chris@43 57 ~izstream() { close(); }
Chris@43 58
Chris@43 59 /* Opens a gzip (.gz) file for reading.
Chris@43 60 * open() can be used to read a file which is not in gzip format;
Chris@43 61 * in this case read() will directly read from the file without
Chris@43 62 * decompression. errno can be checked to distinguish two error
Chris@43 63 * cases (if errno is zero, the zlib error is Z_MEM_ERROR).
Chris@43 64 */
Chris@43 65 void open(const char* name) {
Chris@43 66 if (m_fp) close();
Chris@43 67 m_fp = ::gzopen(name, "rb");
Chris@43 68 }
Chris@43 69
Chris@43 70 void open(FILE* fp) {
Chris@43 71 SET_BINARY_MODE(fp);
Chris@43 72 if (m_fp) close();
Chris@43 73 m_fp = ::gzdopen(fileno(fp), "rb");
Chris@43 74 }
Chris@43 75
Chris@43 76 /* Flushes all pending input if necessary, closes the compressed file
Chris@43 77 * and deallocates all the (de)compression state. The return value is
Chris@43 78 * the zlib error number (see function error() below).
Chris@43 79 */
Chris@43 80 int close() {
Chris@43 81 int r = ::gzclose(m_fp);
Chris@43 82 m_fp = 0; return r;
Chris@43 83 }
Chris@43 84
Chris@43 85 /* Binary read the given number of bytes from the compressed file.
Chris@43 86 */
Chris@43 87 int read(void* buf, size_t len) {
Chris@43 88 return ::gzread(m_fp, buf, len);
Chris@43 89 }
Chris@43 90
Chris@43 91 /* Returns the error message for the last error which occurred on the
Chris@43 92 * given compressed file. errnum is set to zlib error number. If an
Chris@43 93 * error occurred in the file system and not in the compression library,
Chris@43 94 * errnum is set to Z_ERRNO and the application may consult errno
Chris@43 95 * to get the exact error code.
Chris@43 96 */
Chris@43 97 const char* error(int* errnum) {
Chris@43 98 return ::gzerror(m_fp, errnum);
Chris@43 99 }
Chris@43 100
Chris@43 101 gzFile fp() { return m_fp; }
Chris@43 102
Chris@43 103 private:
Chris@43 104 gzFile m_fp;
Chris@43 105 };
Chris@43 106
Chris@43 107 /*
Chris@43 108 * Binary read the given (array of) object(s) from the compressed file.
Chris@43 109 * If the input file was not in gzip format, read() copies the objects number
Chris@43 110 * of bytes into the buffer.
Chris@43 111 * returns the number of uncompressed bytes actually read
Chris@43 112 * (0 for end of file, -1 for error).
Chris@43 113 */
Chris@43 114 template <class T, class Items>
Chris@43 115 inline int read(izstream& zs, T* x, Items items) {
Chris@43 116 return ::gzread(zs.fp(), x, items*sizeof(T));
Chris@43 117 }
Chris@43 118
Chris@43 119 /*
Chris@43 120 * Binary input with the '>' operator.
Chris@43 121 */
Chris@43 122 template <class T>
Chris@43 123 inline izstream& operator>(izstream& zs, T& x) {
Chris@43 124 ::gzread(zs.fp(), &x, sizeof(T));
Chris@43 125 return zs;
Chris@43 126 }
Chris@43 127
Chris@43 128
Chris@43 129 inline zstringlen::zstringlen(izstream& zs) {
Chris@43 130 zs > val.byte;
Chris@43 131 if (val.byte == 255) zs > val.word;
Chris@43 132 else val.word = val.byte;
Chris@43 133 }
Chris@43 134
Chris@43 135 /*
Chris@43 136 * Read length of string + the string with the '>' operator.
Chris@43 137 */
Chris@43 138 inline izstream& operator>(izstream& zs, char* x) {
Chris@43 139 zstringlen len(zs);
Chris@43 140 ::gzread(zs.fp(), x, len.value());
Chris@43 141 x[len.value()] = '\0';
Chris@43 142 return zs;
Chris@43 143 }
Chris@43 144
Chris@43 145 inline char* read_string(izstream& zs) {
Chris@43 146 zstringlen len(zs);
Chris@43 147 char* x = new char[len.value()+1];
Chris@43 148 ::gzread(zs.fp(), x, len.value());
Chris@43 149 x[len.value()] = '\0';
Chris@43 150 return x;
Chris@43 151 }
Chris@43 152
Chris@43 153 // ----------------------------- ozstream -----------------------------
Chris@43 154
Chris@43 155 class ozstream
Chris@43 156 {
Chris@43 157 public:
Chris@43 158 ozstream() : m_fp(0), m_os(0) {
Chris@43 159 }
Chris@43 160 ozstream(FILE* fp, int level = Z_DEFAULT_COMPRESSION)
Chris@43 161 : m_fp(0), m_os(0) {
Chris@43 162 open(fp, level);
Chris@43 163 }
Chris@43 164 ozstream(const char* name, int level = Z_DEFAULT_COMPRESSION)
Chris@43 165 : m_fp(0), m_os(0) {
Chris@43 166 open(name, level);
Chris@43 167 }
Chris@43 168 ~ozstream() {
Chris@43 169 close();
Chris@43 170 }
Chris@43 171
Chris@43 172 /* Opens a gzip (.gz) file for writing.
Chris@43 173 * The compression level parameter should be in 0..9
Chris@43 174 * errno can be checked to distinguish two error cases
Chris@43 175 * (if errno is zero, the zlib error is Z_MEM_ERROR).
Chris@43 176 */
Chris@43 177 void open(const char* name, int level = Z_DEFAULT_COMPRESSION) {
Chris@43 178 char mode[4] = "wb\0";
Chris@43 179 if (level != Z_DEFAULT_COMPRESSION) mode[2] = '0'+level;
Chris@43 180 if (m_fp) close();
Chris@43 181 m_fp = ::gzopen(name, mode);
Chris@43 182 }
Chris@43 183
Chris@43 184 /* open from a FILE pointer.
Chris@43 185 */
Chris@43 186 void open(FILE* fp, int level = Z_DEFAULT_COMPRESSION) {
Chris@43 187 SET_BINARY_MODE(fp);
Chris@43 188 char mode[4] = "wb\0";
Chris@43 189 if (level != Z_DEFAULT_COMPRESSION) mode[2] = '0'+level;
Chris@43 190 if (m_fp) close();
Chris@43 191 m_fp = ::gzdopen(fileno(fp), mode);
Chris@43 192 }
Chris@43 193
Chris@43 194 /* Flushes all pending output if necessary, closes the compressed file
Chris@43 195 * and deallocates all the (de)compression state. The return value is
Chris@43 196 * the zlib error number (see function error() below).
Chris@43 197 */
Chris@43 198 int close() {
Chris@43 199 if (m_os) {
Chris@43 200 ::gzwrite(m_fp, m_os->str(), m_os->pcount());
Chris@43 201 delete[] m_os->str(); delete m_os; m_os = 0;
Chris@43 202 }
Chris@43 203 int r = ::gzclose(m_fp); m_fp = 0; return r;
Chris@43 204 }
Chris@43 205
Chris@43 206 /* Binary write the given number of bytes into the compressed file.
Chris@43 207 */
Chris@43 208 int write(const void* buf, size_t len) {
Chris@43 209 return ::gzwrite(m_fp, (voidp) buf, len);
Chris@43 210 }
Chris@43 211
Chris@43 212 /* Flushes all pending output into the compressed file. The parameter
Chris@43 213 * _flush is as in the deflate() function. The return value is the zlib
Chris@43 214 * error number (see function gzerror below). flush() returns Z_OK if
Chris@43 215 * the flush_ parameter is Z_FINISH and all output could be flushed.
Chris@43 216 * flush() should be called only when strictly necessary because it can
Chris@43 217 * degrade compression.
Chris@43 218 */
Chris@43 219 int flush(int _flush) {
Chris@43 220 os_flush();
Chris@43 221 return ::gzflush(m_fp, _flush);
Chris@43 222 }
Chris@43 223
Chris@43 224 /* Returns the error message for the last error which occurred on the
Chris@43 225 * given compressed file. errnum is set to zlib error number. If an
Chris@43 226 * error occurred in the file system and not in the compression library,
Chris@43 227 * errnum is set to Z_ERRNO and the application may consult errno
Chris@43 228 * to get the exact error code.
Chris@43 229 */
Chris@43 230 const char* error(int* errnum) {
Chris@43 231 return ::gzerror(m_fp, errnum);
Chris@43 232 }
Chris@43 233
Chris@43 234 gzFile fp() { return m_fp; }
Chris@43 235
Chris@43 236 ostream& os() {
Chris@43 237 if (m_os == 0) m_os = new ostrstream;
Chris@43 238 return *m_os;
Chris@43 239 }
Chris@43 240
Chris@43 241 void os_flush() {
Chris@43 242 if (m_os && m_os->pcount()>0) {
Chris@43 243 ostrstream* oss = new ostrstream;
Chris@43 244 oss->fill(m_os->fill());
Chris@43 245 oss->flags(m_os->flags());
Chris@43 246 oss->precision(m_os->precision());
Chris@43 247 oss->width(m_os->width());
Chris@43 248 ::gzwrite(m_fp, m_os->str(), m_os->pcount());
Chris@43 249 delete[] m_os->str(); delete m_os; m_os = oss;
Chris@43 250 }
Chris@43 251 }
Chris@43 252
Chris@43 253 private:
Chris@43 254 gzFile m_fp;
Chris@43 255 ostrstream* m_os;
Chris@43 256 };
Chris@43 257
Chris@43 258 /*
Chris@43 259 * Binary write the given (array of) object(s) into the compressed file.
Chris@43 260 * returns the number of uncompressed bytes actually written
Chris@43 261 * (0 in case of error).
Chris@43 262 */
Chris@43 263 template <class T, class Items>
Chris@43 264 inline int write(ozstream& zs, const T* x, Items items) {
Chris@43 265 return ::gzwrite(zs.fp(), (voidp) x, items*sizeof(T));
Chris@43 266 }
Chris@43 267
Chris@43 268 /*
Chris@43 269 * Binary output with the '<' operator.
Chris@43 270 */
Chris@43 271 template <class T>
Chris@43 272 inline ozstream& operator<(ozstream& zs, const T& x) {
Chris@43 273 ::gzwrite(zs.fp(), (voidp) &x, sizeof(T));
Chris@43 274 return zs;
Chris@43 275 }
Chris@43 276
Chris@43 277 inline zstringlen::zstringlen(ozstream& zs, const char* x) {
Chris@43 278 val.byte = 255; val.word = ::strlen(x);
Chris@43 279 if (val.word < 255) zs < (val.byte = val.word);
Chris@43 280 else zs < val;
Chris@43 281 }
Chris@43 282
Chris@43 283 /*
Chris@43 284 * Write length of string + the string with the '<' operator.
Chris@43 285 */
Chris@43 286 inline ozstream& operator<(ozstream& zs, const char* x) {
Chris@43 287 zstringlen len(zs, x);
Chris@43 288 ::gzwrite(zs.fp(), (voidp) x, len.value());
Chris@43 289 return zs;
Chris@43 290 }
Chris@43 291
Chris@43 292 #ifdef _MSC_VER
Chris@43 293 inline ozstream& operator<(ozstream& zs, char* const& x) {
Chris@43 294 return zs < (const char*) x;
Chris@43 295 }
Chris@43 296 #endif
Chris@43 297
Chris@43 298 /*
Chris@43 299 * Ascii write with the << operator;
Chris@43 300 */
Chris@43 301 template <class T>
Chris@43 302 inline ostream& operator<<(ozstream& zs, const T& x) {
Chris@43 303 zs.os_flush();
Chris@43 304 return zs.os() << x;
Chris@43 305 }
Chris@43 306
Chris@43 307 #endif