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