Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
DeflateOutputStreamBuffer.h
Go to the documentation of this file.
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 */
17namespace 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
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
int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
Definition: compress.c:67
#define buffer
Definition: xmlparse.cc:628