Jpp  15.0.1
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  * Open file.
63  *
64  * \param file_name file name
65  */
66  virtual void open(const char* file_name) override
67  {
68  using namespace std;
69  using namespace JPP;
70 
71  JAccessible::Throw(true);
72 
73  if (getProtocol(file_name) == ROOT_FILE_FORMAT)
74  this->reset(new JRootFileReader<T>());
75  else if (getFilenameExtension(file_name) == ROOT_FILE_FORMAT)
76  this->reset(new JRootFileReader<T>());
77  else if (getFilenameExtension(file_name) == DAQ_FILE_FORMAT)
78  this->reset(new JDAQFileReader<T>());
79  else if (getFilenameExtension(file_name) == MONTE_CARLO_FILE_FORMAT)
81  else if (getFilenameExtension(file_name) == GZIP_FILE_FORMAT)
82  this->reset(new JMonteCarloGZFileReader<T>());
83  else
84  this->Throw(JProtocolException(MAKE_STRING("Protocol not defined: " << file_name)));
85 
86  if (this->is_valid()) {
87 
88  this->get()->open(file_name);
89 
90  if (!this->get()->is_open()) {
91  if (getFilenameExtension(file_name) != ROOT_FILE_FORMAT) {
92  JAccessible::Throw(JFileOpenException("File not opened."));
93  }
94  }
95  }
96  }
97  };
98 }
99 
100 #endif
Exception for opening of file.
Definition: JException.hh:342
static const char *const GZIP_FILE_FORMAT
gzip file name extension
Exceptions.
JFileScanner()
Default constructor.
Definition: JFileScanner.hh:46
static const char *const MONTE_CARLO_FILE_FORMAT
ASCII file name extension.
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:66
I/O formatting auxiliaries.
static const char *const DAQ_FILE_FORMAT
DAQ binary file name extension.
JFileScanner(const char *file_name)
Constructor.
Definition: JFileScanner.hh:55
Protocol exception.
Definition: JException.hh:630
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
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
ROOT file name extension.
Specifications of file name extensions.
ROOT file reader.
static void Throw(const bool option)
Enable/disable throw option.
Definition: JThrow.hh:37