Jpp
 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"
12 
13 
14 /**
15  * \author mdejong
16  */
17 
18 namespace JSUPPORT {}
19 namespace JPP { using namespace JSUPPORT; }
20 
21 namespace JSUPPORT {
22 
25 
26 
27  /**
28  * Object reading from file.
29  *
30  * This class implements the method open of the JLANG::JAccessible interface.
31  * The file format is derived from the file name extension.
32  */
33  template<class T>
34  class JFileScanner :
35  public JAccessibleObjectReader<T>
36  {
37  public:
38 
40 
41  /**
42  * Default constructor.
43  */
45  {}
46 
47 
48  /**
49  * Constructor.
50  *
51  * \param file_name file name
52  */
53  JFileScanner(const char* file_name)
54  {
55  open(file_name);
56  }
57 
58 
59  /**
60  * Open file.
61  *
62  * \param file_name file name
63  */
64  virtual void open(const char* file_name)
65  {
66  using namespace std;
67  using namespace JPP;
68 
69  if (getProtocol(file_name) == ROOT_FILE_FORMAT ||
71  this->reset(new JRootFileReader<T>());
72  else if (getFilenameExtension(file_name) == DAQ_FILE_FORMAT)
73  this->reset(new JDAQFileReader<T>());
74  else if (getFilenameExtension(file_name) == MONTE_CARLO_FILE_FORMAT)
76  else if (getFilenameExtension(file_name) == GZIP_FILE_FORMAT)
77  this->reset(new JMonteCarloGZFileReader<T>());
78  else
79  this->Throw(JProtocolException(string("Protocol not defined: ") + file_name));
80 
81  if (this->is_valid()) {
82  this->get()->open(file_name);
83  }
84  }
85  };
86 }
87 
88 #endif
static const char *const GZIP_FILE_FORMAT
gzip file name extension
Exceptions.
virtual void open(const char *file_name)
Open file.
Definition: JFileScanner.hh:64
JFileScanner()
Default constructor.
Definition: JFileScanner.hh:44
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.
Template definition of Monte Carlo object reader for ASCII formatted file (i.e.
static const char *const DAQ_FILE_FORMAT
DAQ binary file name extension.
JFileScanner(const char *file_name)
Constructor.
Definition: JFileScanner.hh:53
Protocol exception.
Definition: JException.hh:612
std::string getProtocol(const std::string &file_name)
Get protocol, i.e.
Definition: JeepToolkit.hh:245
JAccessibleObjectReader< T >::helper_type helper_type
Definition: JFileScanner.hh:39
DAQ object reading from binary file (i.e.
std::string getFilenameExtension(const std::string &file_name)
Get file name extension, i.e.
Definition: JeepToolkit.hh:66
Object reading from file.
Definition: JFileScanner.hh:34
Auxiliary class for object reading with named access.
static const char *const ROOT_FILE_FORMAT
ROOT file name extension.
Specifications of file name extensions.
static void Throw(const bool option)
Enable/disable throw option.
Definition: JThrow.hh:37