PYTHIA  8.312
Streams.h
1 // Streams.h is a part of the PYTHIA event generator.
2 // Copyright (C) 2024 Torbjorn Sjostrand.
3 // PYTHIA is licenced under the GNU GPL v2 or later, see COPYING for details.
4 // Please respect the MCnet Guidelines, see GUIDELINES for details.
5 
6 // Classes to implement reading from or writing to gzipped files.
7 // Adapted for Sherpa by Frank Siegert from:
8 // gzstream, C++ iostream classes wrapping the zlib compression library.
9 // Copyright (C) 2024 Deepak Bandyopadhyay, Lutz Kettner
10 // (http://www.cs.unc.edu/Research/compgeom/gzstream).
11 // Further adapted to PYTHIA by Stefan Prestel.
12 
13 #ifndef Pythia8_Streams_H
14 #define Pythia8_Streams_H
15 
16 #include <cstddef>
17 #include <iostream>
18 #include <iomanip>
19 #include <sstream>
20 #include <fstream>
21 #include <string.h>
22 #ifdef GZIP
23 #include <zlib.h>
24 #endif
25 
26 namespace Pythia8 {
27 
28 #ifdef GZIP
29 
30 //==========================================================================
31 
32 // Internal classes to implement gzstream. See below for user classes.
33 
34 // -------------------------------------------------------------------------
35 
36 class gzstreambuf : public std::streambuf {
37 private:
38  static const int bufferSize = 47+256; // size of data buff
39  // totals 512 bytes under g++ for igzstream at the end.
40 
41  gzFile file{}; // file handle for compressed file
42  char buffer[bufferSize]{}; // data buffer
43  char opened{}; // open/close state of stream
44  int mode{}; // I/O mode
45 
46  int flush_buffer();
47 public:
48  gzstreambuf() : opened(0) {
49  setp( buffer, buffer + (bufferSize-1));
50  setg( buffer + 4, // beginning of putback area
51  buffer + 4, // read position
52  buffer + 4); // end position
53  // ASSERT: both input & output capabilities will not be used together
54  }
55  int is_open() { return opened; }
56  gzstreambuf* open( const char* name, int open_mode);
57  gzstreambuf* close();
58  ~gzstreambuf() { close(); }
59 
60  virtual int overflow( int c = EOF);
61  virtual int underflow();
62  virtual int sync();
63 };
64 
65 // -------------------------------------------------------------------------
66 
67 class gzstreambase : virtual public std::ios {
68 protected:
69  gzstreambuf buf;
70 public:
71  gzstreambase() { init(&buf); }
72  gzstreambase( const char* name, int open_mode);
73  ~gzstreambase();
74  void open( const char* name, int open_mode);
75  void close();
76  gzstreambuf* rdbuf() { return &buf; }
77 };
78 
79 //==========================================================================
80 
81 // User classes. Use igzstream and ogzstream analogously to ifstream and
82 // ofstream respectively. They read and write files based on the gz*
83 // function interface of the zlib. Files are compatible with gzip compression.
84 
85 // -------------------------------------------------------------------------
86 
87 class igzstream : public gzstreambase, public std::istream {
88 public:
89  igzstream() : std::istream( &buf) {}
90  igzstream( const char* name, int mode = std::ios::in)
91  : gzstreambase( name, mode), std::istream( &buf) {}
92  gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
93  void open( const char* name, int mode = std::ios::in) {
94  gzstreambase::open( name, mode);
95  }
96 };
97 // -------------------------------------------------------------------------
98 
99 class ogzstream : public gzstreambase, public std::ostream {
100 public:
101  ogzstream() : std::ostream( &buf) {}
102  ogzstream( const char* name, int mode = std::ios::out)
103  : gzstreambase( name, mode), std::ostream( &buf) {}
104  gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
105  void open( const char* name, int mode = std::ios::out) {
106  gzstreambase::open( name, mode);
107  }
108 };
109 
110 //==========================================================================
111 
112 #else
113 typedef std::ifstream igzstream;
114 typedef std::ofstream ogzstream;
115 #endif
116 
117 // Dummy to avoid harmless compiler warning that Streams.o has no symbols.
119 public:
120  DummyForStreams() {}
121  double xtox(double x);
122 };
123 
124 //==========================================================================
125 
126 } // end namespace Pythia8
127 
128 #endif // end Pythia8_Streams_H
end namespace Pythia8
Definition: PythiaStdlib.h:312
Header for classes to set beam momentum and interaction vertex spread.
Definition: Analysis.h:20
Dummy to avoid harmless compiler warning that Streams.o has no symbols.
Definition: Streams.h:118