| 1 | // Copyright FreeHEP, 2005.
|
|---|
| 2 | #ifndef CHEPREP_DEFLATEOUTPUTSTREAMBUF_H
|
|---|
| 3 | #define CHEPREP_DEFLATEOUTPUTSTREAMBUF_H
|
|---|
| 4 |
|
|---|
| 5 | #include <vector>
|
|---|
| 6 | #include <iostream>
|
|---|
| 7 | #include <string>
|
|---|
| 8 | #include <cstdio>
|
|---|
| 9 |
|
|---|
| 10 | #ifndef CHEPREP_NO_ZLIB
|
|---|
| 11 | #include <zlib.h>
|
|---|
| 12 | #endif // CHEPREP_NO_ZLIB
|
|---|
| 13 |
|
|---|
| 14 | /**
|
|---|
| 15 | * @author Mark Donszelmann
|
|---|
| 16 | * @version $Id: DeflateOutputStreamBuffer.h,v 1.6 2009/05/08 09:26:58 gcosmo Exp $
|
|---|
| 17 | */
|
|---|
| 18 | namespace cheprep {
|
|---|
| 19 |
|
|---|
| 20 | class DeflateOutputStreamBuffer : public std::streambuf {
|
|---|
| 21 |
|
|---|
| 22 | public:
|
|---|
| 23 |
|
|---|
| 24 | DeflateOutputStreamBuffer(std::streambuf *buffer);
|
|---|
| 25 |
|
|---|
| 26 | void init(bool compress);
|
|---|
| 27 | void finish();
|
|---|
| 28 |
|
|---|
| 29 | virtual ~DeflateOutputStreamBuffer();
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 | protected:
|
|---|
| 33 | int overflow(int c = EOF);
|
|---|
| 34 |
|
|---|
| 35 | #ifndef CHEPREP_NO_ZLIB
|
|---|
| 36 | bool flushOut();
|
|---|
| 37 | #endif // CHEPREP_NO_ZLIB
|
|---|
| 38 |
|
|---|
| 39 | inline void putUI(unsigned int ui) {
|
|---|
| 40 | unsigned char* ucp = reinterpret_cast<unsigned char *>(&ui);
|
|---|
| 41 | unsigned int i = (static_cast<unsigned int>(ucp[ 3 ]) << 24) +
|
|---|
| 42 | (static_cast<unsigned int>(ucp[ 2 ]) << 16) +
|
|---|
| 43 | (static_cast<unsigned int>(ucp[ 1 ]) << 8 ) +
|
|---|
| 44 | (static_cast<unsigned int>(ucp[ 0 ]));
|
|---|
| 45 | buffer->sputn(reinterpret_cast<char *>(&i), sizeof(unsigned int));
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | inline void putUS(unsigned short us) {
|
|---|
| 49 | unsigned char* ucp = reinterpret_cast<unsigned char *>(&us);
|
|---|
| 50 | unsigned short s = (static_cast<unsigned short>(ucp[ 1 ]) << 8 ) +
|
|---|
| 51 | (static_cast<unsigned short>(ucp[ 0 ]));
|
|---|
| 52 | buffer->sputn(reinterpret_cast<char *>(&s), sizeof(unsigned short));
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | inline void putUB(unsigned char ub) {
|
|---|
| 56 | buffer->sputc(ub);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | inline void putS(const std::string s) {
|
|---|
| 60 | buffer->sputn(s.c_str(), s.length());
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | inline std::streampos pos() {
|
|---|
| 64 | std::ostream os(buffer);
|
|---|
| 65 | return os.tellp();
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | inline unsigned int getSize() {
|
|---|
| 69 | return size;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | inline unsigned int getCRC() {
|
|---|
| 73 | return crc;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | private:
|
|---|
| 77 | static unsigned long crctable[256];
|
|---|
| 78 | std::streambuf *buffer;
|
|---|
| 79 |
|
|---|
| 80 | unsigned int crc;
|
|---|
| 81 | unsigned int size;
|
|---|
| 82 |
|
|---|
| 83 | #ifndef CHEPREP_NO_ZLIB
|
|---|
| 84 | static const unsigned int inSize = 1000;
|
|---|
| 85 | static const unsigned int outSize = 1000;
|
|---|
| 86 | z_stream zStream;
|
|---|
| 87 | bool zStreamOpen;
|
|---|
| 88 |
|
|---|
| 89 | std::vector<char> in;
|
|---|
| 90 | std::vector<char> out;
|
|---|
| 91 | #endif // CHEPREP_NO_ZLIB
|
|---|
| 92 | };
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 | } // cheprep
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 | #endif // CHEPREP_DEFLATEOUTPUTSTREAMBUF_H
|
|---|