Jpp  16.0.3
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JFileScanner.hh
Go to the documentation of this file.
1 #ifndef __JSUPPORT__JFILESCANNER__
2 #define __JSUPPORT__JFILESCANNER__
3 
4 #include <string>
5 
6 #include "JLang/JObjectReader.hh"
7 #include "JLang/JException.hh"
8 #include "Jeep/JPrint.hh"
13 
14 
15 /**
16  * \author mdejong
17  */
18 
19 namespace JSUPPORT {}
20 namespace JPP { using namespace JSUPPORT; }
21 
22 namespace JSUPPORT {
23 
27 
28 
29  /**
30  * Object reading from file.
31  *
32  * This class implements the method open of the JLANG::JAccessible interface.
33  * The file format is derived from the file name extension.
34  */
35  template<class T>
36  class JFileScanner :
37  public JAccessibleObjectReader<T>
38  {
39  public:
40 
42 
43  /**
44  * Default constructor.
45  */
47  {}
48 
49 
50  /**
51  * Constructor.
52  *
53  * \param file_name file name
54  */
55  JFileScanner(const char* file_name)
56  {
57  open(file_name);
58  }
59 
60 
61  /**
62  * Destructor.
63  */
65  {
66  if (this->is_open()) {
67  this->close();
68  }
69  }
70 
71 
72  /**
73  * Open file.
74  *
75  * \param file_name file name
76  */
77  virtual void open(const char* file_name) override
78  {
79  using namespace std;
80  using namespace JPP;
81 
82  JAccessible::Throw(true);
83 
84  if (getProtocol(file_name) == ROOT_FILE_FORMAT)
85  this->reset(new JRootFileReader<T>());
86  else if (getFilenameExtension(file_name) == ROOT_FILE_FORMAT)
87  this->reset(new JRootFileReader<T>());
88  else if (getFilenameExtension(file_name) == DAQ_FILE_FORMAT)
89  this->reset(new JDAQFileReader<T>());
90  else if (getFilenameExtension(file_name) == MONTE_CARLO_FILE_FORMAT)
92  else if (getFilenameExtension(file_name) == GZIP_FILE_FORMAT)
93  this->reset(new JMonteCarloGZFileReader<T>());
94  else
95  this->Throw(JProtocolException(MAKE_STRING("Protocol not defined: " << file_name)));
96 
97  if (this->is_valid()) {
98 
99  this->get()->open(file_name);
100 
101  if (!this->get()->is_open()) {
102  if (getFilenameExtension(file_name) != ROOT_FILE_FORMAT) {
103  JAccessible::Throw(JFileOpenException("File not opened."));
104  }
105  }
106  }
107  }
108  };
109 }
110 
111 #endif
Exception for opening of file.
Definition: JException.hh:342
static const char *const GZIP_FILE_FORMAT
file name extension gzip format
Exceptions.
JFileScanner()
Default constructor.
Definition: JFileScanner.hh:46
static const char *const MONTE_CARLO_FILE_FORMAT
file name extension ASCII format
Template definition of Monte Carlo object reader for gzipped ASCII formatted file (i...
bool is_valid() const
Check validity of pointer.
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:142
Template definition of Monte Carlo object reader for ASCII formatted file (i.e. &#39;.evt&#39;)
virtual void open(const char *file_name) override
Open file.
Definition: JFileScanner.hh:77
I/O formatting auxiliaries.
~JFileScanner()
Destructor.
Definition: JFileScanner.hh:64
static const char *const DAQ_FILE_FORMAT
file name extension DAQ binary format
JFileScanner(const char *file_name)
Constructor.
Definition: JFileScanner.hh:55
virtual bool is_open() const override
Check is device is open.
Protocol exception.
Definition: JException.hh:648
std::string getProtocol(const std::string &file_name)
Get protocol, i.e. part before first JEEP::PROTOCOL_SEPARATOR if any.
Definition: JeepToolkit.hh:263
JAccessibleObjectReader< T >::helper_type helper_type
Definition: JFileScanner.hh:41
virtual void close() override
Close device.
DAQ object reading from binary file (i.e. .dat).
std::string getFilenameExtension(const std::string &file_name)
Get file name extension, i.e. part after last JEEP::FILENAME_SEPARATOR if any.
Definition: JeepToolkit.hh:69
Object reading from file.
Definition: JFileScanner.hh:36
Auxiliary class for object reading with named access.
virtual void reset() override
Reset pointer.
static const char *const ROOT_FILE_FORMAT
file name extension ROOT format
Specifications of file name extensions.
ROOT file reader.
static void Throw(const bool option)
Enable/disable throw option.
Definition: JThrow.hh:37