annotate src/zlib-1.2.8/contrib/iostream2/zstream.h @ 155:54abead6ecce

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