File: zstream.h

package info (click to toggle)
regina-normal 4.93-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 28,576 kB
  • sloc: cpp: 86,815; ansic: 13,030; xml: 9,089; perl: 951; sh: 380; python: 273; makefile: 103
file content (508 lines) | stat: -rw-r--r-- 16,384 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508

/**************************************************************************
 *                                                                        *
 *  Regina - A Normal Surface Theory Calculator                           *
 *  Computational Engine                                                  *
 *                                                                        *
 *  Copyright (c) 1999-2011, Ben Burton                                   *
 *  For further details contact Ben Burton (bab@debian.org).              *
 *                                                                        *
 *  This program is free software; you can redistribute it and/or         *
 *  modify it under the terms of the GNU General Public License as        *
 *  published by the Free Software Foundation; either version 2 of the    *
 *  License, or (at your option) any later version.                       *
 *                                                                        *
 *  This program is distributed in the hope that it will be useful, but   *
 *  WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *  General Public License for more details.                              *
 *                                                                        *
 *  You should have received a copy of the GNU General Public             *
 *  License along with this program; if not, write to the Free            *
 *  Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,       *
 *  MA 02110-1301, USA.                                                   *
 *                                                                        *
 **************************************************************************/

/* end stub */

/*! \file utilities/zstream.h
 *  \brief Provides compressed I/O streams.
 */

#ifndef __ZSTREAM_H
#ifndef __DOXYGEN
#define __ZSTREAM_H
#endif

#include <iostream>
#include <zlib.h>
#include "regina-core.h"

namespace regina {

/**
 * \weakgroup utilities
 * @{
 */

/**
 * A common base class for compression/decompression stream buffers.
 * This class should not be instantiated directly; see classes
 * CompressionBuffer and DecompressionBuffer instead.
 *
 * The standard \e zlib compression library is used for
 * compression and decompression.
 *
 * \ifacespython Not present.
 */
class REGINA_API ZBuffer : public std::streambuf {
    public:
        static const int zEOF;
            /**< The end-of-file marker used with this stream buffer. */

    private:
        gzFile file;
            /**< The compressed file being read from or written to. */
        int next;
            /**< The next character that will be read, or -1 if unknown. */

    protected:
        /**
         * Creates a new stream buffer.
         */
        ZBuffer();

    public:
        /**
         * Destroys this stream buffer.
         * Any underlying file that is open will be closed.
         */
        virtual ~ZBuffer();

        /**
         * Writes the given character to the underlying file,
         * compressing en route.
         *
         * @param c the uncompressed character to write.
         * @return the given character on success, or zEOF on error.
         */
        virtual int overflow(int c);
        /**
         * Reads the next character from the underlying file,
         * decompressing en route.  The character is not consumed.
         *
         * @return the next uncompressed character, or zEOF if there is
         * an error or there are no more characters.
         */
        virtual int underflow();
        /**
         * Reads the next character from the underlying file,
         * decompressing en route.  The character is consumed.
         *
         * @return the next uncompressed character, or zEOF if there is
         * an error or there are no more characters.
         */
        virtual int uflow();
        /**
         * Writes the given set of characters to the underlying file,
         * compressing en route.
         *
         * @param s the uncompressed array of characters to write.
         * @param n the number of characters to write.
         * @return the number of uncompressed characters that were
         * actually written, or 0 if an error occurred.
         */
        virtual std::streamsize xsputn(const char* s, std::streamsize n);
        /**
         * Reads a set of characters from the underlying file,
         * decompressing en route.  The characters are all consumed.
         * Reading will stop if end-of-file is reached or an error
         * occurs.
         *
         * @param s the array into which the uncompressed characters
         * should be placed.
         * @param n the number of uncompressed characters to read.
         * @return the number of uncompressed characters that were
         * actually read, or zEOF if an error occurred.
         */
        virtual std::streamsize xsgetn(char* s, std::streamsize n);
        /**
         * Pushes the given character back into the underlying input stream.
         *
         * \warning This routine will only succeed if the next character
         * in the underlying input stream has not already been read.
         *
         * @param c the character to push back.
         * @return the character that was pushed back, or zEOF if an
         * error occurred.
         */
        virtual int pbackfail(int c);
        /**
         * Flushes all input/output buffers.
         *
         * @return 0 on success, or zEOF on error.
         */
        virtual int sync();

        /**
         * Closes the underlying file.  If no file is open, this routine
         * does nothing.
         *
         * @return 0 on success, or zEOF on error.
         */
        int close();

        /**
         * Writes a description of the last (de)compression error that
         * occurred.  For the message to be meaningful, the underlying
         * file must still be open.
         *
         * @param out the output stream to which the error description
         * should be written.
         */
        void showError(std::ostream& out);

    protected:
        /**
         * Opens the given file for (de)compressed reading or writing.
         * If a file is already open, it will be closed before the new
         * file is opened.
         *
         * \i18n This routine makes no assumptions about the
         * \ref i18n "character encoding" used in the given file \e name,
         * and simply passes it through unchanged to low-level C/C++ file
         * I/O routines.
         *
         * @param path the pathname of the new file to open.
         * @param mode the mode in which to open the file; this must be
         * <tt>"rb"</tt> for reading or <tt>"wb"</tt> for writing.
         * @return 0 on success, or zEOF on error.
         */
        int open(const char *path, const char *mode);
};

/**
 * An output stream buffer that compresses data as it is written.
 * The standard \e zlib compression library is used.
 *
 * This buffer is designed for use with standard C++ I/O streams.
 *
 * This stream buffer should \e not be used for input.
 *
 * \ifacespython Not present.
 */
class REGINA_API CompressionBuffer : public ZBuffer {
    public:
        /**
         * Creates a new compression stream buffer.
         */
        CompressionBuffer();
        /**
         * Creates a new compression stream buffer that writes to the
         * given file.  The underlying file will be opened automatically.
         *
         * \i18n This routine makes no assumptions about the
         * \ref i18n "character encoding" used in the given file \e name,
         * and simply passes it through unchanged to low-level C/C++ file
         * I/O routines.
         *
         * @param path the pathname of the new file to open.
         */
        CompressionBuffer(const char* path);

        /**
         * Opens the given file for compressed writing.
         * If a file is already open, it will be closed before the new
         * file is opened.
         *
         * \i18n This routine makes no assumptions about the
         * \ref i18n "character encoding" used in the given file \e name,
         * and simply passes it through unchanged to low-level C/C++ file
         * I/O routines.
         *
         * @param path the pathname of the new file to open.
         * @return 0 on success, or zEOF on error.
         */
        int open(const char* path);
};

/**
 * An input stream buffer that decompresses data as it is read.
 * The standard \e zlib compression library is used.
 *
 * This buffer is designed for use with standard C++ I/O streams.
 *
 * This stream buffer should \e not be used for output.
 *
 * \ifacespython Not present.
 */
class REGINA_API DecompressionBuffer : public ZBuffer {
    public:
        /**
         * Creates a new decompression stream buffer.
         */
        DecompressionBuffer();
        /**
         * Creates a new decompression stream buffer that reads from the
         * given file.  The underlying file will be opened automatically.
         *
         * \i18n This routine makes no assumptions about the
         * \ref i18n "character encoding" used in the given file \e name,
         * and simply passes it through unchanged to low-level C/C++ file
         * I/O routines.
         *
         * @param path the pathname of the new file to open.
         */
        DecompressionBuffer(const char* path);

        /**
         * Opens the given file for decompressed reading.
         * If a file is already open, it will be closed before the new
         * file is opened.
         *
         * \i18n This routine makes no assumptions about the
         * \ref i18n "character encoding" used in the given file \e name,
         * and simply passes it through unchanged to low-level C/C++ file
         * I/O routines.
         *
         * @param path the pathname of the new file to open.
         * @return 0 on success, or zEOF on error.
         */
        int open(const char* path);
};

/**
 * An output stream that compresses data as it is written.
 * The standard \e zlib compression library is used.
 *
 * This stream does its work through a CompressionBuffer.
 *
 * \ifacespython Not present.
 */
class REGINA_API CompressionStream : public std::ostream {
    private:
        CompressionBuffer buf;
            /**< The underlying compression buffer. */

    public:
        /**
         * Creates a new compression stream.
         */
        CompressionStream();
        /**
         * Creates a new compression stream that writes to the given
         * file.  The underlying file will be opened automatically.
         *
         * \i18n This routine makes no assumptions about the
         * \ref i18n "character encoding" used in the given file \e name,
         * and simply passes it through unchanged to low-level C/C++ file
         * I/O routines.
         *
         * @param path the pathname of the new file to open.
         */
        CompressionStream(const char* path);

        /**
         * Opens the given file for compressed writing.
         * If a file is already open, it will be closed before the new
         * file is opened.
         *
         * \i18n This routine makes no assumptions about the
         * \ref i18n "character encoding" used in the given file \e name,
         * and simply passes it through unchanged to low-level C/C++ file
         * I/O routines.
         *
         * @param path the pathname of the new file to open.
         * @return 0 on success, or ZBuffer::zEOF on error.
         */
        int open(const char* path);
        /**
         * Closes the underlying file.  If no file is open, this routine
         * does nothing.
         *
         * @return 0 on success, or ZBuffer::zEOF on error.
         */
        int close();
};

/**
 * An input stream that decompresses data as it is read.
 * The standard \e zlib compression library is used.
 *
 * This stream does its work through a DecompressionBuffer.
 *
 * \ifacespython Not present.
 */
class REGINA_API DecompressionStream : public std::istream {
    private:
        DecompressionBuffer buf;
            /**< The underlying decompression buffer. */

    public:
        /**
         * Creates a new decompression stream.
         */
        DecompressionStream();
        /**
         * Creates a new decompression stream that reads from the given
         * file.  The underlying file will be opened automatically.
         *
         * \i18n This routine makes no assumptions about the
         * \ref i18n "character encoding" used in the given file \e name,
         * and simply passes it through unchanged to low-level C/C++ file
         * I/O routines.
         *
         * @param path the pathname of the new file to open.
         */
        DecompressionStream(const char* path);

        /**
         * Opens the given file for decompressed reading.
         * If a file is already open, it will be closed before the new
         * file is opened.
         *
         * \i18n This routine makes no assumptions about the
         * \ref i18n "character encoding" used in the given file \e name,
         * and simply passes it through unchanged to low-level C/C++ file
         * I/O routines.
         *
         * @param path the pathname of the new file to open.
         * @return 0 on success, or ZBuffer::zEOF on error.
         */
        int open(const char* path);
        /**
         * Closes the underlying file.  If no file is open, this routine
         * does nothing.
         *
         * @return 0 on success, or ZBuffer::zEOF on error.
         */
        int close();
};

/*@}*/

// Inline functions for ZBuffer

inline ZBuffer::ZBuffer() : file(0), next(-1) {
    setbuf(0, 0);
}

inline ZBuffer::~ZBuffer() {
    close();
}

inline int ZBuffer::overflow(int c) {
    int ans = gzputc(file, c);
    return (ans == -1 ? zEOF : c);
}

inline int ZBuffer::underflow() {
    if (next == -1)
        next = gzgetc(file);
    return (next == -1 ? zEOF : next);
}

inline int ZBuffer::uflow() {
    int ans = underflow();
    next = -1;
    return ans;
}

inline std::streamsize ZBuffer::xsputn(const char* s, std::streamsize n) {
    return gzwrite(file, const_cast<void*>(static_cast<const void*>(s)),
        static_cast<unsigned>(n));
}

inline int ZBuffer::pbackfail(int c) {
    if (c == zEOF)
        return zEOF;
    return (next == -1 ? next = c : zEOF);
}

inline int ZBuffer::sync() {
    return (gzflush(file, Z_SYNC_FLUSH) == Z_OK ? 0 : zEOF);
}

// Inline functions for CompressionBuffer

inline CompressionBuffer::CompressionBuffer() {
}

inline CompressionBuffer::CompressionBuffer(const char* path) {
    open(path);
}

inline int CompressionBuffer::open(const char* path) {
    return ZBuffer::open(path, "wb");
}

// Inline functions for DecompressionBuffer

inline DecompressionBuffer::DecompressionBuffer() {
}

inline DecompressionBuffer::DecompressionBuffer(const char* path) {
    open(path);
}

inline int DecompressionBuffer::open(const char* path) {
    return ZBuffer::open(path, "rb");
}

// Inline functions for CompressionStream

inline CompressionStream::CompressionStream() : std::ostream(0) {
    init(&buf);
}

inline CompressionStream::CompressionStream(const char* path) :
        std::ostream(0) {
    init(&buf);
    open(path);
}

inline int CompressionStream::open(const char* path) {
    int ans = buf.open(path);
    if (ans)
        setstate(std::ios::failbit);
    return ans;
}

inline int CompressionStream::close() {
    int ans = buf.close();
    if (ans)
        setstate(std::ios::failbit);
    return ans;
}

// Inline functions for DecompressionStream

inline DecompressionStream::DecompressionStream() : std::istream(0) {
    init(&buf);
}

inline DecompressionStream::DecompressionStream(const char* path) :
        std::istream(0) {
    init(&buf);
    open(path);
}

inline int DecompressionStream::open(const char* path) {
    int ans = buf.open(path);
    if (ans)
        setstate(std::ios::failbit);
    return ans;
}

inline int DecompressionStream::close() {
    int ans = buf.close();
    if (ans)
        setstate(std::ios::failbit);
    return ans;
}

} // namespace regina

#endif