Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:06:32

0001 // Streams.h is a part of the PYTHIA event generator.
0002 // Copyright (C) 2024 Torbjorn Sjostrand.
0003 // PYTHIA is licenced under the GNU GPL v2 or later, see COPYING for details.
0004 // Please respect the MCnet Guidelines, see GUIDELINES for details.
0005 
0006 // Classes to implement reading from or writing to gzipped files.
0007 // Adapted for Sherpa by Frank Siegert from:
0008 // gzstream, C++ iostream classes wrapping the zlib compression library.
0009 // Copyright (C) 2024  Deepak Bandyopadhyay, Lutz Kettner
0010 // (http://www.cs.unc.edu/Research/compgeom/gzstream).
0011 // Further adapted to PYTHIA by Stefan Prestel.
0012 
0013 #ifndef Pythia8_Streams_H
0014 #define Pythia8_Streams_H
0015 
0016 #include <cstddef>
0017 #include <iostream>
0018 #include <iomanip>
0019 #include <sstream>
0020 #include <fstream>
0021 #include <string.h>
0022 #ifdef GZIP
0023 #include <zlib.h>
0024 #endif
0025 
0026 namespace Pythia8 {
0027 
0028 #ifdef GZIP
0029 
0030 //==========================================================================
0031 
0032 // Internal classes to implement gzstream. See below for user classes.
0033 
0034 // -------------------------------------------------------------------------
0035 
0036 class gzstreambuf : public std::streambuf {
0037 private:
0038     static const int bufferSize = 47+256;    // size of data buff
0039     // totals 512 bytes under g++ for igzstream at the end.
0040 
0041     gzFile           file{};               // file handle for compressed file
0042     char             buffer[bufferSize]{}; // data buffer
0043     char             opened{};             // open/close state of stream
0044     int              mode{};               // I/O mode
0045 
0046     int flush_buffer();
0047 public:
0048     gzstreambuf() : opened(0) {
0049         setp( buffer, buffer + (bufferSize-1));
0050         setg( buffer + 4,     // beginning of putback area
0051               buffer + 4,     // read position
0052               buffer + 4);    // end position
0053         // ASSERT: both input & output capabilities will not be used together
0054     }
0055     int is_open() { return opened; }
0056     gzstreambuf* open( const char* name, int open_mode);
0057     gzstreambuf* close();
0058     ~gzstreambuf() { close(); }
0059 
0060     virtual int     overflow( int c = EOF);
0061     virtual int     underflow();
0062     virtual int     sync();
0063 };
0064 
0065 // -------------------------------------------------------------------------
0066 
0067 class gzstreambase : virtual public std::ios {
0068 protected:
0069     gzstreambuf buf;
0070 public:
0071     gzstreambase() { init(&buf); }
0072     gzstreambase( const char* name, int open_mode);
0073     ~gzstreambase();
0074     void open( const char* name, int open_mode);
0075     void close();
0076     gzstreambuf* rdbuf() { return &buf; }
0077 };
0078 
0079 //==========================================================================
0080 
0081 // User classes. Use igzstream and ogzstream analogously to ifstream and
0082 // ofstream respectively. They read and write files based on the gz*
0083 // function interface of the zlib. Files are compatible with gzip compression.
0084 
0085 // -------------------------------------------------------------------------
0086 
0087 class igzstream : public gzstreambase, public std::istream {
0088 public:
0089     igzstream() : std::istream( &buf) {}
0090     igzstream( const char* name, int mode = std::ios::in)
0091         : gzstreambase( name, mode), std::istream( &buf) {}
0092     gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
0093     void open( const char* name, int mode = std::ios::in) {
0094         gzstreambase::open( name, mode);
0095     }
0096 };
0097 // -------------------------------------------------------------------------
0098 
0099 class ogzstream : public gzstreambase, public std::ostream {
0100 public:
0101     ogzstream() : std::ostream( &buf) {}
0102     ogzstream( const char* name, int mode = std::ios::out)
0103         : gzstreambase( name, mode), std::ostream( &buf) {}
0104     gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
0105     void open( const char* name, int mode = std::ios::out) {
0106         gzstreambase::open( name, mode);
0107     }
0108 };
0109 
0110 //==========================================================================
0111 
0112 #else
0113 typedef std::ifstream igzstream;
0114 typedef std::ofstream ogzstream;
0115 #endif
0116 
0117 // Dummy to avoid harmless compiler warning that Streams.o has no symbols.
0118 class DummyForStreams {
0119 public:
0120   DummyForStreams() {}
0121   double xtox(double x);
0122 };
0123 
0124 //==========================================================================
0125 
0126 } // end namespace Pythia8
0127 
0128 #endif // end Pythia8_Streams_H