Remove building with NOCRYPTO option
[minix.git] / common / dist / zlib / contrib / iostream2 / zstream.h
blobba48b47adb8624dc1e44155897a61d7d40919dd1
1 /* $NetBSD: zstream.h,v 1.1.1.1 2006/01/14 20:10:54 christos Exp $ */
3 /*
5 * Copyright (c) 1997
6 * Christian Michelsen Research AS
7 * Advanced Computing
8 * Fantoftvegen 38, 5036 BERGEN, Norway
9 * http://www.cmr.no
11 * Permission to use, copy, modify, distribute and sell this software
12 * and its documentation for any purpose is hereby granted without fee,
13 * provided that the above copyright notice appear in all copies and
14 * that both that copyright notice and this permission notice appear
15 * in supporting documentation. Christian Michelsen Research AS makes no
16 * representations about the suitability of this software for any
17 * purpose. It is provided "as is" without express or implied warranty.
21 #ifndef ZSTREAM__H
22 #define ZSTREAM__H
25 * zstream.h - C++ interface to the 'zlib' general purpose compression library
26 * Id: zstream.h 1.1 1997-06-25 12:00:56+02 tyge Exp tyge
29 #include <strstream.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include "zlib.h"
34 #if defined(_WIN32)
35 # include <fcntl.h>
36 # include <io.h>
37 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
38 #else
39 # define SET_BINARY_MODE(file)
40 #endif
42 class zstringlen {
43 public:
44 zstringlen(class izstream&);
45 zstringlen(class ozstream&, const char*);
46 size_t value() const { return val.word; }
47 private:
48 struct Val { unsigned char byte; size_t word; } val;
51 // ----------------------------- izstream -----------------------------
53 class izstream
55 public:
56 izstream() : m_fp(0) {}
57 izstream(FILE* fp) : m_fp(0) { open(fp); }
58 izstream(const char* name) : m_fp(0) { open(name); }
59 ~izstream() { close(); }
61 /* Opens a gzip (.gz) file for reading.
62 * open() can be used to read a file which is not in gzip format;
63 * in this case read() will directly read from the file without
64 * decompression. errno can be checked to distinguish two error
65 * cases (if errno is zero, the zlib error is Z_MEM_ERROR).
67 void open(const char* name) {
68 if (m_fp) close();
69 m_fp = ::gzopen(name, "rb");
72 void open(FILE* fp) {
73 SET_BINARY_MODE(fp);
74 if (m_fp) close();
75 m_fp = ::gzdopen(fileno(fp), "rb");
78 /* Flushes all pending input if necessary, closes the compressed file
79 * and deallocates all the (de)compression state. The return value is
80 * the zlib error number (see function error() below).
82 int close() {
83 int r = ::gzclose(m_fp);
84 m_fp = 0; return r;
87 /* Binary read the given number of bytes from the compressed file.
89 int read(void* buf, size_t len) {
90 return ::gzread(m_fp, buf, len);
93 /* Returns the error message for the last error which occurred on the
94 * given compressed file. errnum is set to zlib error number. If an
95 * error occurred in the file system and not in the compression library,
96 * errnum is set to Z_ERRNO and the application may consult errno
97 * to get the exact error code.
99 const char* error(int* errnum) {
100 return ::gzerror(m_fp, errnum);
103 gzFile fp() { return m_fp; }
105 private:
106 gzFile m_fp;
110 * Binary read the given (array of) object(s) from the compressed file.
111 * If the input file was not in gzip format, read() copies the objects number
112 * of bytes into the buffer.
113 * returns the number of uncompressed bytes actually read
114 * (0 for end of file, -1 for error).
116 template <class T, class Items>
117 inline int read(izstream& zs, T* x, Items items) {
118 return ::gzread(zs.fp(), x, items*sizeof(T));
122 * Binary input with the '>' operator.
124 template <class T>
125 inline izstream& operator>(izstream& zs, T& x) {
126 ::gzread(zs.fp(), &x, sizeof(T));
127 return zs;
131 inline zstringlen::zstringlen(izstream& zs) {
132 zs > val.byte;
133 if (val.byte == 255) zs > val.word;
134 else val.word = val.byte;
138 * Read length of string + the string with the '>' operator.
140 inline izstream& operator>(izstream& zs, char* x) {
141 zstringlen len(zs);
142 ::gzread(zs.fp(), x, len.value());
143 x[len.value()] = '\0';
144 return zs;
147 inline char* read_string(izstream& zs) {
148 zstringlen len(zs);
149 char* x = new char[len.value()+1];
150 ::gzread(zs.fp(), x, len.value());
151 x[len.value()] = '\0';
152 return x;
155 // ----------------------------- ozstream -----------------------------
157 class ozstream
159 public:
160 ozstream() : m_fp(0), m_os(0) {
162 ozstream(FILE* fp, int level = Z_DEFAULT_COMPRESSION)
163 : m_fp(0), m_os(0) {
164 open(fp, level);
166 ozstream(const char* name, int level = Z_DEFAULT_COMPRESSION)
167 : m_fp(0), m_os(0) {
168 open(name, level);
170 ~ozstream() {
171 close();
174 /* Opens a gzip (.gz) file for writing.
175 * The compression level parameter should be in 0..9
176 * errno can be checked to distinguish two error cases
177 * (if errno is zero, the zlib error is Z_MEM_ERROR).
179 void open(const char* name, int level = Z_DEFAULT_COMPRESSION) {
180 char mode[4] = "wb\0";
181 if (level != Z_DEFAULT_COMPRESSION) mode[2] = '0'+level;
182 if (m_fp) close();
183 m_fp = ::gzopen(name, mode);
186 /* open from a FILE pointer.
188 void open(FILE* fp, int level = Z_DEFAULT_COMPRESSION) {
189 SET_BINARY_MODE(fp);
190 char mode[4] = "wb\0";
191 if (level != Z_DEFAULT_COMPRESSION) mode[2] = '0'+level;
192 if (m_fp) close();
193 m_fp = ::gzdopen(fileno(fp), mode);
196 /* Flushes all pending output if necessary, closes the compressed file
197 * and deallocates all the (de)compression state. The return value is
198 * the zlib error number (see function error() below).
200 int close() {
201 if (m_os) {
202 ::gzwrite(m_fp, m_os->str(), m_os->pcount());
203 delete[] m_os->str(); delete m_os; m_os = 0;
205 int r = ::gzclose(m_fp); m_fp = 0; return r;
208 /* Binary write the given number of bytes into the compressed file.
210 int write(const void* buf, size_t len) {
211 return ::gzwrite(m_fp, (voidp) buf, len);
214 /* Flushes all pending output into the compressed file. The parameter
215 * _flush is as in the deflate() function. The return value is the zlib
216 * error number (see function gzerror below). flush() returns Z_OK if
217 * the flush_ parameter is Z_FINISH and all output could be flushed.
218 * flush() should be called only when strictly necessary because it can
219 * degrade compression.
221 int flush(int _flush) {
222 os_flush();
223 return ::gzflush(m_fp, _flush);
226 /* Returns the error message for the last error which occurred on the
227 * given compressed file. errnum is set to zlib error number. If an
228 * error occurred in the file system and not in the compression library,
229 * errnum is set to Z_ERRNO and the application may consult errno
230 * to get the exact error code.
232 const char* error(int* errnum) {
233 return ::gzerror(m_fp, errnum);
236 gzFile fp() { return m_fp; }
238 ostream& os() {
239 if (m_os == 0) m_os = new ostrstream;
240 return *m_os;
243 void os_flush() {
244 if (m_os && m_os->pcount()>0) {
245 ostrstream* oss = new ostrstream;
246 oss->fill(m_os->fill());
247 oss->flags(m_os->flags());
248 oss->precision(m_os->precision());
249 oss->width(m_os->width());
250 ::gzwrite(m_fp, m_os->str(), m_os->pcount());
251 delete[] m_os->str(); delete m_os; m_os = oss;
255 private:
256 gzFile m_fp;
257 ostrstream* m_os;
261 * Binary write the given (array of) object(s) into the compressed file.
262 * returns the number of uncompressed bytes actually written
263 * (0 in case of error).
265 template <class T, class Items>
266 inline int write(ozstream& zs, const T* x, Items items) {
267 return ::gzwrite(zs.fp(), (voidp) x, items*sizeof(T));
271 * Binary output with the '<' operator.
273 template <class T>
274 inline ozstream& operator<(ozstream& zs, const T& x) {
275 ::gzwrite(zs.fp(), (voidp) &x, sizeof(T));
276 return zs;
279 inline zstringlen::zstringlen(ozstream& zs, const char* x) {
280 val.byte = 255; val.word = ::strlen(x);
281 if (val.word < 255) zs < (val.byte = val.word);
282 else zs < val;
286 * Write length of string + the string with the '<' operator.
288 inline ozstream& operator<(ozstream& zs, const char* x) {
289 zstringlen len(zs, x);
290 ::gzwrite(zs.fp(), (voidp) x, len.value());
291 return zs;
294 #ifdef _MSC_VER
295 inline ozstream& operator<(ozstream& zs, char* const& x) {
296 return zs < (const char*) x;
298 #endif
301 * Ascii write with the << operator;
303 template <class T>
304 inline ostream& operator<<(ozstream& zs, const T& x) {
305 zs.os_flush();
306 return zs.os() << x;
309 #endif