kusano fc6ab3
/*
kusano fc6ab3
 *
kusano fc6ab3
 * Copyright (c) 1997
kusano fc6ab3
 * Christian Michelsen Research AS
kusano fc6ab3
 * Advanced Computing
kusano fc6ab3
 * Fantoftvegen 38, 5036 BERGEN, Norway
kusano fc6ab3
 * http://www.cmr.no
kusano fc6ab3
 *
kusano fc6ab3
 * Permission to use, copy, modify, distribute and sell this software
kusano fc6ab3
 * and its documentation for any purpose is hereby granted without fee,
kusano fc6ab3
 * provided that the above copyright notice appear in all copies and
kusano fc6ab3
 * that both that copyright notice and this permission notice appear
kusano fc6ab3
 * in supporting documentation.  Christian Michelsen Research AS makes no
kusano fc6ab3
 * representations about the suitability of this software for any
kusano fc6ab3
 * purpose.  It is provided "as is" without express or implied warranty.
kusano fc6ab3
 *
kusano fc6ab3
 */
kusano fc6ab3
kusano fc6ab3
#ifndef ZSTREAM__H
kusano fc6ab3
#define ZSTREAM__H
kusano fc6ab3
kusano fc6ab3
/*
kusano fc6ab3
 * zstream.h - C++ interface to the 'zlib' general purpose compression library
kusano fc6ab3
 * $Id: zstream.h 1.1 1997-06-25 12:00:56+02 tyge Exp tyge $
kusano fc6ab3
 */
kusano fc6ab3
kusano fc6ab3
#include <strstream.h></strstream.h>
kusano fc6ab3
#include <string.h></string.h>
kusano fc6ab3
#include <stdio.h></stdio.h>
kusano fc6ab3
#include "zlib.h"
kusano fc6ab3
kusano fc6ab3
#if defined(_WIN32)
kusano fc6ab3
#   include <fcntl.h></fcntl.h>
kusano fc6ab3
#   include <io.h></io.h>
kusano fc6ab3
#   define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
kusano fc6ab3
#else
kusano fc6ab3
#   define SET_BINARY_MODE(file)
kusano fc6ab3
#endif
kusano fc6ab3
kusano fc6ab3
class zstringlen {
kusano fc6ab3
public:
kusano fc6ab3
    zstringlen(class izstream&);
kusano fc6ab3
    zstringlen(class ozstream&, const char*);
kusano fc6ab3
    size_t value() const { return val.word; }
kusano fc6ab3
private:
kusano fc6ab3
    struct Val { unsigned char byte; size_t word; } val;
kusano fc6ab3
};
kusano fc6ab3
kusano fc6ab3
//  ----------------------------- izstream -----------------------------
kusano fc6ab3
kusano fc6ab3
class izstream
kusano fc6ab3
{
kusano fc6ab3
    public:
kusano fc6ab3
        izstream() : m_fp(0) {}
kusano fc6ab3
        izstream(FILE* fp) : m_fp(0) { open(fp); }
kusano fc6ab3
        izstream(const char* name) : m_fp(0) { open(name); }
kusano fc6ab3
        ~izstream() { close(); }
kusano fc6ab3
kusano fc6ab3
        /* Opens a gzip (.gz) file for reading.
kusano fc6ab3
         * open() can be used to read a file which is not in gzip format;
kusano fc6ab3
         * in this case read() will directly read from the file without
kusano fc6ab3
         * decompression. errno can be checked to distinguish two error
kusano fc6ab3
         * cases (if errno is zero, the zlib error is Z_MEM_ERROR).
kusano fc6ab3
         */
kusano fc6ab3
        void open(const char* name) {
kusano fc6ab3
            if (m_fp) close();
kusano fc6ab3
            m_fp = ::gzopen(name, "rb");
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        void open(FILE* fp) {
kusano fc6ab3
            SET_BINARY_MODE(fp);
kusano fc6ab3
            if (m_fp) close();
kusano fc6ab3
            m_fp = ::gzdopen(fileno(fp), "rb");
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        /* Flushes all pending input if necessary, closes the compressed file
kusano fc6ab3
         * and deallocates all the (de)compression state. The return value is
kusano fc6ab3
         * the zlib error number (see function error() below).
kusano fc6ab3
         */
kusano fc6ab3
        int close() {
kusano fc6ab3
            int r = ::gzclose(m_fp);
kusano fc6ab3
            m_fp = 0; return r;
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        /* Binary read the given number of bytes from the compressed file.
kusano fc6ab3
         */
kusano fc6ab3
        int read(void* buf, size_t len) {
kusano fc6ab3
            return ::gzread(m_fp, buf, len);
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        /* Returns the error message for the last error which occurred on the
kusano fc6ab3
         * given compressed file. errnum is set to zlib error number. If an
kusano fc6ab3
         * error occurred in the file system and not in the compression library,
kusano fc6ab3
         * errnum is set to Z_ERRNO and the application may consult errno
kusano fc6ab3
         * to get the exact error code.
kusano fc6ab3
         */
kusano fc6ab3
        const char* error(int* errnum) {
kusano fc6ab3
            return ::gzerror(m_fp, errnum);
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        gzFile fp() { return m_fp; }
kusano fc6ab3
kusano fc6ab3
    private:
kusano fc6ab3
        gzFile m_fp;
kusano fc6ab3
};
kusano fc6ab3
kusano fc6ab3
/*
kusano fc6ab3
 * Binary read the given (array of) object(s) from the compressed file.
kusano fc6ab3
 * If the input file was not in gzip format, read() copies the objects number
kusano fc6ab3
 * of bytes into the buffer.
kusano fc6ab3
 * returns the number of uncompressed bytes actually read
kusano fc6ab3
 * (0 for end of file, -1 for error).
kusano fc6ab3
 */
kusano fc6ab3
template <class class="" items="" t,=""></class>
kusano fc6ab3
inline int read(izstream& zs, T* x, Items items) {
kusano fc6ab3
    return ::gzread(zs.fp(), x, items*sizeof(T));
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
/*
kusano fc6ab3
 * Binary input with the '>' operator.
kusano fc6ab3
 */
kusano fc6ab3
template <class t=""></class>
kusano fc6ab3
inline izstream& operator>(izstream& zs, T& x) {
kusano fc6ab3
    ::gzread(zs.fp(), &x, sizeof(T));
kusano fc6ab3
    return zs;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
inline zstringlen::zstringlen(izstream& zs) {
kusano fc6ab3
    zs > val.byte;
kusano fc6ab3
    if (val.byte == 255) zs > val.word;
kusano fc6ab3
    else val.word = val.byte;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
/*
kusano fc6ab3
 * Read length of string + the string with the '>' operator.
kusano fc6ab3
 */
kusano fc6ab3
inline izstream& operator>(izstream& zs, char* x) {
kusano fc6ab3
    zstringlen len(zs);
kusano fc6ab3
    ::gzread(zs.fp(), x, len.value());
kusano fc6ab3
    x[len.value()] = '\0';
kusano fc6ab3
    return zs;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
inline char* read_string(izstream& zs) {
kusano fc6ab3
    zstringlen len(zs);
kusano fc6ab3
    char* x = new char[len.value()+1];
kusano fc6ab3
    ::gzread(zs.fp(), x, len.value());
kusano fc6ab3
    x[len.value()] = '\0';
kusano fc6ab3
    return x;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
// ----------------------------- ozstream -----------------------------
kusano fc6ab3
kusano fc6ab3
class ozstream
kusano fc6ab3
{
kusano fc6ab3
    public:
kusano fc6ab3
        ozstream() : m_fp(0), m_os(0) {
kusano fc6ab3
        }
kusano fc6ab3
        ozstream(FILE* fp, int level = Z_DEFAULT_COMPRESSION)
kusano fc6ab3
            : m_fp(0), m_os(0) {
kusano fc6ab3
            open(fp, level);
kusano fc6ab3
        }
kusano fc6ab3
        ozstream(const char* name, int level = Z_DEFAULT_COMPRESSION)
kusano fc6ab3
            : m_fp(0), m_os(0) {
kusano fc6ab3
            open(name, level);
kusano fc6ab3
        }
kusano fc6ab3
        ~ozstream() {
kusano fc6ab3
            close();
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        /* Opens a gzip (.gz) file for writing.
kusano fc6ab3
         * The compression level parameter should be in 0..9
kusano fc6ab3
         * errno can be checked to distinguish two error cases
kusano fc6ab3
         * (if errno is zero, the zlib error is Z_MEM_ERROR).
kusano fc6ab3
         */
kusano fc6ab3
        void open(const char* name, int level = Z_DEFAULT_COMPRESSION) {
kusano fc6ab3
            char mode[4] = "wb\0";
kusano fc6ab3
            if (level != Z_DEFAULT_COMPRESSION) mode[2] = '0'+level;
kusano fc6ab3
            if (m_fp) close();
kusano fc6ab3
            m_fp = ::gzopen(name, mode);
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        /* open from a FILE pointer.
kusano fc6ab3
         */
kusano fc6ab3
        void open(FILE* fp, int level = Z_DEFAULT_COMPRESSION) {
kusano fc6ab3
            SET_BINARY_MODE(fp);
kusano fc6ab3
            char mode[4] = "wb\0";
kusano fc6ab3
            if (level != Z_DEFAULT_COMPRESSION) mode[2] = '0'+level;
kusano fc6ab3
            if (m_fp) close();
kusano fc6ab3
            m_fp = ::gzdopen(fileno(fp), mode);
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        /* Flushes all pending output if necessary, closes the compressed file
kusano fc6ab3
         * and deallocates all the (de)compression state. The return value is
kusano fc6ab3
         * the zlib error number (see function error() below).
kusano fc6ab3
         */
kusano fc6ab3
        int close() {
kusano fc6ab3
            if (m_os) {
kusano fc6ab3
                ::gzwrite(m_fp, m_os->str(), m_os->pcount());
kusano fc6ab3
                delete[] m_os->str(); delete m_os; m_os = 0;
kusano fc6ab3
            }
kusano fc6ab3
            int r = ::gzclose(m_fp); m_fp = 0; return r;
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        /* Binary write the given number of bytes into the compressed file.
kusano fc6ab3
         */
kusano fc6ab3
        int write(const void* buf, size_t len) {
kusano fc6ab3
            return ::gzwrite(m_fp, (voidp) buf, len);
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        /* Flushes all pending output into the compressed file. The parameter
kusano fc6ab3
         * _flush is as in the deflate() function. The return value is the zlib
kusano fc6ab3
         * error number (see function gzerror below). flush() returns Z_OK if
kusano fc6ab3
         * the flush_ parameter is Z_FINISH and all output could be flushed.
kusano fc6ab3
         * flush() should be called only when strictly necessary because it can
kusano fc6ab3
         * degrade compression.
kusano fc6ab3
         */
kusano fc6ab3
        int flush(int _flush) {
kusano fc6ab3
            os_flush();
kusano fc6ab3
            return ::gzflush(m_fp, _flush);
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        /* Returns the error message for the last error which occurred on the
kusano fc6ab3
         * given compressed file. errnum is set to zlib error number. If an
kusano fc6ab3
         * error occurred in the file system and not in the compression library,
kusano fc6ab3
         * errnum is set to Z_ERRNO and the application may consult errno
kusano fc6ab3
         * to get the exact error code.
kusano fc6ab3
         */
kusano fc6ab3
        const char* error(int* errnum) {
kusano fc6ab3
            return ::gzerror(m_fp, errnum);
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        gzFile fp() { return m_fp; }
kusano fc6ab3
kusano fc6ab3
        ostream& os() {
kusano fc6ab3
            if (m_os == 0) m_os = new ostrstream;
kusano fc6ab3
            return *m_os;
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        void os_flush() {
kusano fc6ab3
            if (m_os && m_os->pcount()>0) {
kusano fc6ab3
                ostrstream* oss = new ostrstream;
kusano fc6ab3
                oss->fill(m_os->fill());
kusano fc6ab3
                oss->flags(m_os->flags());
kusano fc6ab3
                oss->precision(m_os->precision());
kusano fc6ab3
                oss->width(m_os->width());
kusano fc6ab3
                ::gzwrite(m_fp, m_os->str(), m_os->pcount());
kusano fc6ab3
                delete[] m_os->str(); delete m_os; m_os = oss;
kusano fc6ab3
            }
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
    private:
kusano fc6ab3
        gzFile m_fp;
kusano fc6ab3
        ostrstream* m_os;
kusano fc6ab3
};
kusano fc6ab3
kusano fc6ab3
/*
kusano fc6ab3
 * Binary write the given (array of) object(s) into the compressed file.
kusano fc6ab3
 * returns the number of uncompressed bytes actually written
kusano fc6ab3
 * (0 in case of error).
kusano fc6ab3
 */
kusano fc6ab3
template <class class="" items="" t,=""></class>
kusano fc6ab3
inline int write(ozstream& zs, const T* x, Items items) {
kusano fc6ab3
    return ::gzwrite(zs.fp(), (voidp) x, items*sizeof(T));
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
/*
kusano fc6ab3
 * Binary output with the '<' operator.
kusano fc6ab3
 */
kusano fc6ab3
template <class t=""></class>
kusano fc6ab3
inline ozstream& operator<(ozstream& zs, const T& x) {
kusano fc6ab3
    ::gzwrite(zs.fp(), (voidp) &x, sizeof(T));
kusano fc6ab3
    return zs;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
inline zstringlen::zstringlen(ozstream& zs, const char* x) {
kusano fc6ab3
    val.byte = 255;  val.word = ::strlen(x);
kusano fc6ab3
    if (val.word < 255) zs < (val.byte = val.word);
kusano fc6ab3
    else zs < val;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
/*
kusano fc6ab3
 * Write length of string + the string with the '<' operator.
kusano fc6ab3
 */
kusano fc6ab3
inline ozstream& operator<(ozstream& zs, const char* x) {
kusano fc6ab3
    zstringlen len(zs, x);
kusano fc6ab3
    ::gzwrite(zs.fp(), (voidp) x, len.value());
kusano fc6ab3
    return zs;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
#ifdef _MSC_VER
kusano fc6ab3
inline ozstream& operator<(ozstream& zs, char* const& x) {
kusano fc6ab3
    return zs < (const char*) x;
kusano fc6ab3
}
kusano fc6ab3
#endif
kusano fc6ab3
kusano fc6ab3
/*
kusano fc6ab3
 * Ascii write with the << operator;
kusano fc6ab3
 */
kusano fc6ab3
template <class t=""></class>
kusano fc6ab3
inline ostream& operator<<(ozstream& zs, const T& x) {
kusano fc6ab3
    zs.os_flush();
kusano fc6ab3
    return zs.os() << x;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
#endif