Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-06 09:38:31

0001 // -*- C++ -*-
0002 //
0003 // CFileLineReader.h is a part of ThePEG - Toolkit for HEP Event Generation
0004 // Copyright (C) 1999-2019 Leif Lonnblad
0005 //
0006 // ThePEG is licenced under version 3 of the GPL, see COPYING for details.
0007 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
0008 //
0009 #ifndef THEPEG_CFileLineReader_H
0010 #define THEPEG_CFileLineReader_H
0011 //
0012 // This is the declaration of the CFileLineReader class.
0013 //
0014 
0015 #include "ThePEG/Config/ThePEG.h"
0016 #include "CFileLineReader.fh"
0017 #include "CFile.h"
0018 #include <cstdio>
0019 #include <string>
0020 
0021 namespace ThePEG {
0022 
0023 /**
0024  * CFileLineReader is a wrapper around a standard C FILE stream. With
0025  * it one reads one line at the time (with readline()) into an
0026  * internal buffer from which one can then read as from a standard
0027  * std::istream with a limited set of operator>> functions. It can be
0028  * thought of as an std::ifstream where the internal buffer must be
0029  * filled by hand one line at the time.
0030  *
0031  * Contrary to std::ifstream the CFileLineReader can also handle
0032  * gipped files and pipes. Gzipped files are automatically handles by
0033  * a pipe using the <code>zcat</code> command if the file name ends
0034  * with <code>.gz</code>. Also if a file name ends with a
0035  * <code>|</code> sign, the preceding string is interpreted as a
0036  * command defining a pipe from which to read.
0037  *
0038  * Since CFileLineReader is very close to the standard C FILE stream
0039  * it is in many cases much faster than eg. reading from lines via
0040  * std::istringstream.
0041  */
0042 class CFileLineReader {
0043 
0044 public:
0045 
0046   /** @name Standard constructors and destructors. */
0047   //@{
0048   /**
0049    * The default constructor.
0050    */
0051   CFileLineReader();
0052 
0053   /**
0054    * Constructor taking a \a filename as argument. Optionally the size
0055    * \a len of the line buffer can be specified. If \a filename ends
0056    * with <code>.gz</code> a pipe is opened where the file is read by
0057    * <code>zcat</code>. If \a filename ends with a <code>|</code>
0058    * sign, the preceding string is interpreted as a command defining a
0059    * pipe from which to read.
0060    */
0061   CFileLineReader(string filename, int len = defsize);
0062 
0063   /**
0064    * The destructor.
0065    */
0066   ~CFileLineReader();
0067   //@}
0068 
0069   /** @name Initialization functions. */
0070   //@{
0071   /**
0072    * Initialize with a \a filename. If \a filename ends with
0073    * <code>.gz</code> a pipe is opened where the file is read by
0074    * <code>zcat</code>. If \a filename ends with a <code>|</code>
0075    * sign, the preceding string is interpreted as a command defining a
0076    * pipe from which to read.
0077    */
0078   void open(string filename);
0079 
0080   /**
0081    * If the file was opened from within this object, close it.
0082    */
0083   void close();
0084   //@}
0085 
0086   /**
0087    * Read a line from the underlying c-file into the line buffer.
0088    */
0089   bool readline();
0090 
0091   /**
0092    * Undo reading from the current line, ie. the next read will be
0093    * from the beginning of the current line. Afterwards the state will
0094    * be not bad.
0095    */
0096   void resetline();
0097 
0098   /**
0099    * Return a string containing what is left of the line buffer.
0100    */
0101   string getline() const;
0102 
0103   /**
0104    * Return the underlying c-file.
0105    */
0106   CFile cFile() const;
0107 
0108   /**
0109    * Return null if a previous read failed.
0110    */
0111   operator void *();
0112 
0113   /**
0114    * Return true if a previous read failed.
0115    */
0116   bool operator!();
0117 
0118   /**
0119    * Scan forward up and until the first occurrence of the given
0120    * character.
0121    * @return true if the given character was found.
0122    */
0123   bool skip(char c);
0124 
0125   /**
0126    * Check if a given string is present in the current line buffer.
0127    */
0128   bool find(string str) const;
0129 
0130   /** @name Operators to read from the line buffer. */
0131   //@{
0132   /**
0133    * Return the next character of the line-buffer.
0134    */
0135   char getc();
0136 
0137   /**
0138    * Read a long from the line buffer.
0139    */
0140   CFileLineReader & operator>>(long & l);
0141 
0142   /**
0143    * Read an int from the line buffer.
0144    */
0145   CFileLineReader & operator>>(int & i);
0146 
0147   /**
0148    * Read an unsigned long from the line buffer.
0149    */
0150   CFileLineReader & operator>>(unsigned long & l);
0151 
0152   /**
0153    * Read an unsigned int from the line buffer.
0154    */
0155   CFileLineReader & operator>>(unsigned int & i);
0156 
0157   /**
0158    * Read a double from the line buffer.
0159    */
0160   CFileLineReader & operator>>(double & d);
0161 
0162   /**
0163    * Read a float from the line buffer.
0164    */
0165   CFileLineReader & operator>>(float & f);
0166 
0167   /**
0168    * Read a (whitespace delimited) string from the line buffer.
0169    */
0170   CFileLineReader & operator>>(std::string & s);
0171   //@}
0172 
0173 private:
0174 
0175   /**
0176    * The c-file to be read from.
0177    */
0178   CFile file;
0179 
0180   /**
0181    * The length of the line buffer.
0182    */
0183   int bufflen;
0184 
0185   /**
0186    * The line buffer.
0187    */
0188   char * buff;
0189 
0190   /**
0191    * The current position in the line buffer.
0192    */
0193   char * pos;
0194 
0195   /**
0196    * The current state is bad if a read has failed.
0197    */
0198   bool bad;
0199 
0200   /**
0201    * The default size of the buffer.
0202    */
0203   static const int defsize = 1024;
0204 
0205 private:
0206 
0207   /**
0208    * The copy constructor is private and not implemented.
0209    */
0210   CFileLineReader(const CFileLineReader &);
0211 
0212   /**
0213    * The assignment operator is private and must never be called.
0214    * In fact, it should not even be implemented.
0215    */
0216   CFileLineReader & operator=(const CFileLineReader &) = delete;
0217 
0218 };
0219 
0220 }
0221 
0222 #endif /* THEPEG_CFileLineReader_H */