Jpp  18.0.0-rc.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"
10 #include "JIO/JBinaryFileReader.hh"
14 
15 
16 /**
17  * \author mdejong
18  */
19 
20 namespace JSUPPORT {}
21 namespace JPP { using namespace JSUPPORT; }
22 
23 namespace JSUPPORT {
24 
28 
29 
30  /**
31  * Object reading from file.
32  *
33  * This class implements the method open of the JLANG::JAccessible interface.
34  * The file format is derived from the file name extension.
35  */
36  template<class T>
37  class JFileScanner :
38  public JAccessibleObjectReader<T>
39  {
40  public:
41 
43 
44  /**
45  * Default constructor.
46  */
48  {}
49 
50 
51  /**
52  * Constructor.
53  *
54  * \param file_name file name
55  */
56  JFileScanner(const char* file_name)
57  {
58  open(file_name);
59  }
60 
61 
62  /**
63  * Destructor.
64  */
66  {
67  if (this->is_open()) {
68  this->close();
69  }
70  }
71 
72 
73  /**
74  * Open file.
75  *
76  * \param file_name file name
77  */
78  virtual void open(const char* file_name) override
79  {
80  using namespace std;
81  using namespace JPP;
82 
83  JAccessible::Throw(true);
84 
85  if (getProtocol(file_name) == ROOT_FILE_FORMAT)
86  this->reset(new JRootFileReader<T>());
87  else if (getFilenameExtension(file_name) == ROOT_FILE_FORMAT)
88  this->reset(new JRootFileReader<T>());
89  else if (getFilenameExtension(file_name) == DAQ_FILE_FORMAT)
90  this->reset(new JDAQFileReader<T>());
91  else if (getFilenameExtension(file_name) == JPP_FILE_FORMAT)
92  this->reset(new JBinaryFileReader<T>());
93  else if (getFilenameExtension(file_name) == MONTE_CARLO_FILE_FORMAT)
95  else if (getFilenameExtension(file_name) == GZIP_FILE_FORMAT)
96  this->reset(new JMonteCarloGZFileReader<T>());
97  else
98  this->Throw(JProtocolException(MAKE_STRING("Protocol not defined: " << file_name)));
99 
100  if (this->is_valid()) {
101 
102  this->get()->open(file_name);
103 
104  if (!this->get()->is_open()) {
105  if (getFilenameExtension(file_name) != ROOT_FILE_FORMAT) {
106  JAccessible::Throw(JFileOpenException("File not opened."));
107  }
108  }
109  }
110  }
111  };
112 }
113 
114 #endif
Exception for opening of file.
Definition: JException.hh:342
static const char *const GZIP_FILE_FORMAT
file name extension gzip format
Exceptions.
Object reading from binary file (i.e. .jpp).
JFileScanner()
Default constructor.
Definition: JFileScanner.hh:47
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:127
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:78
static const char *const JPP_FILE_FORMAT
file name extension Jpp binary format
I/O formatting auxiliaries.
~JFileScanner()
Destructor.
Definition: JFileScanner.hh:65
static const char *const DAQ_FILE_FORMAT
file name extension DAQ binary format
JFileScanner(const char *file_name)
Constructor.
Definition: JFileScanner.hh:56
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:303
JAccessibleObjectReader< T >::helper_type helper_type
Definition: JFileScanner.hh:42
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:109
Object reading from file.
Definition: JFileScanner.hh:37
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