annotate src/zlib-1.2.7/contrib/iostream2/zstream.h @ 89:8a15ff55d9af

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