Jpp
JRootObjectID.hh
Go to the documentation of this file.
1 #ifndef __JGIZMO__JROOTOBJECTID__
2 #define __JGIZMO__JROOTOBJECTID__
3 
4 #include <string>
5 #include <istream>
6 #include <ostream>
7 #include <cctype>
8 
9 #include "TString.h"
10 
11 #include "JLang/JException.hh"
12 
13 
14 /**
15  * \author mdejong
16  */
17 
18 namespace JGIZMO {}
19 namespace JPP { using namespace JGIZMO; }
20 
21 namespace JGIZMO {
22 
23  using JLANG::JParseError;
24 
25  /**
26  * Auxiliary class to handle file name, ROOT directory and object name.
27  *
28  * The general syntax is as follows:\n
29  * <pre>
30  * <file name>:[<directory/>]<object name>
31  * </pre>
32  * where
33  * <tt><directory></tt> is optional and
34  * <tt><object name></tt> may be a regular expression (TRegexp).
35  */
37  {
38  public:
39 
40  static const char LABEL_L_BRACKET = '['; //!< left bracket for label
41  static const char LABEL_R_BRACKET = ']'; //!< right bracket for label
42  static const char FILENAME_SEPARATOR = ':'; //!< file name separator
43  static const char PATHNAME_SEPARATOR = '/'; //!< path name separator
44 
45 
46  /**
47  * Default constructor.
48  */
50  {}
51 
52 
53  /**
54  * Constructor.
55  *
56  * \param file_name file name
57  * \param name object name
58  */
59  JRootObjectID(const std::string& file_name,
60  const std::string& name) :
62  directory(""),
63  name (name)
64  {}
65 
66 
67  /**
68  * Get file name.
69  *
70  * \return file name
71  */
72  const std::string& getFilename() const
73  {
74  return file_name;
75  }
76 
77 
78  /**
79  * Get directory.
80  *
81  * \return directory
82  */
83  TString getDirectory() const
84  {
85  return TString(directory.c_str());
86  }
87 
88 
89  /**
90  * Get object name.
91  *
92  * \return object name
93  */
94  TString getObjectName() const
95  {
96  return TString(name.c_str());
97  }
98 
99 
100  /**
101  * Get full file name, including path.
102  *
103  * \return file name
104  */
105  TString getFullFilename() const
106  {
107  if (getDirectory() == "")
108  return getFilename();
109  else
111  }
112 
113 
114  /**
115  * Get full object name, including path.
116  *
117  * \return object name
118  */
119  TString getFullObjectName() const
120  {
121  if (getDirectory() == "")
122  return getObjectName();
123  else
125  }
126 
127 
128  /**
129  * Check validity.
130  *
131  * \return true if valid ROOT object identifier; else false
132  */
133  bool is_valid() const
134  {
135  return (file_name != "" &&
136  name != "");
137  }
138 
139  /**
140  * Clear.
141  */
142  void clear()
143  {
144  file_name.clear();
145  directory.clear();
146  name .clear();
147  }
148 
149 
150  /**
151  * Equal operator for object identifiers.
152  *
153  * \param first first object identifier
154  * \param second second object identifier
155  * \return true if first and second object identifier are equal; else false
156  */
157  friend inline bool operator==(const JRootObjectID& first, const JRootObjectID& second)
158  {
159  return (first.getFilename() == second.getFilename() &&
160  first.getDirectory() == second.getDirectory() &&
161  first.getObjectName() == second.getObjectName());
162  }
163 
164 
165  /**
166  * Read object identifier from input.
167  *
168  * \param in input stream
169  * \param object object identifier
170  * \return input stream
171  */
172  friend inline std::istream& operator>>(std::istream& in, JRootObjectID& object)
173  {
174  using namespace std;
175 
176  object.clear();
177 
178  string buffer;
179 
180  for (int bracket = 0; in.peek() != EOF; ) {
181 
182  const char c = (char) in.get();
183 
184  if (c == LABEL_L_BRACKET) {
185 
186  ++bracket;
187 
188  } else if (c == LABEL_R_BRACKET) {
189 
190  --bracket;
191 
192  } else if (isspace(c)) {
193 
194  if (bracket <= 0) {
195  break;
196  }
197  }
198 
199  buffer.push_back(c);
200  }
201 
202  size_t pos = buffer.find(FILENAME_SEPARATOR);
203 
204  if (pos != string::npos) {
205 
206  object.file_name = buffer.substr(0, pos);
207  object.name = buffer.substr(pos + 1);
208 
209  pos = object.name.rfind(PATHNAME_SEPARATOR);
210 
211  if (pos != string::npos) {
212 
213  object.directory = object.name.substr(0, pos);
214  object.name = object.name.substr(pos + 1);
215  }
216 
217  } else if (!buffer.empty()) {
218 
219  throw JParseError("JRootObjectID error parsing " + buffer);
220  }
221 
222  return in;
223  }
224 
225 
226  /**
227  * Write object identifier to output.
228  *
229  * \param out output stream
230  * \param object object identifier
231  * \return output stream
232  */
233  friend inline std::ostream& operator<<(std::ostream& out, const JRootObjectID& object)
234  {
235  return out << object.getFilename() << FILENAME_SEPARATOR << object.getFullObjectName();
236  }
237 
238  protected:
239  std::string file_name;
240  std::string directory;
241  std::string name;
242  };
243 }
244 
245 #endif
JException.hh
JGIZMO::JRootObjectID::name
std::string name
Definition: JRootObjectID.hh:241
JGIZMO::JRootObjectID::getFilename
const std::string & getFilename() const
Get file name.
Definition: JRootObjectID.hh:72
JGIZMO::JRootObjectID::JRootObjectID
JRootObjectID(const std::string &file_name, const std::string &name)
Constructor.
Definition: JRootObjectID.hh:59
JGIZMO::JRootObjectID::getFullFilename
TString getFullFilename() const
Get full file name, including path.
Definition: JRootObjectID.hh:105
JGIZMO::JRootObjectID::getFullObjectName
TString getFullObjectName() const
Get full object name, including path.
Definition: JRootObjectID.hh:119
JGIZMO::JRootObjectID::clear
void clear()
Clear.
Definition: JRootObjectID.hh:142
JGIZMO::JRootObjectID::directory
std::string directory
Definition: JRootObjectID.hh:240
JGIZMO::JRootObjectID::LABEL_L_BRACKET
static const char LABEL_L_BRACKET
left bracket for label
Definition: JRootObjectID.hh:40
JGIZMO::JRootObjectID::getObjectName
TString getObjectName() const
Get object name.
Definition: JRootObjectID.hh:94
JGIZMO::JRootObjectID::LABEL_R_BRACKET
static const char LABEL_R_BRACKET
right bracket for label
Definition: JRootObjectID.hh:41
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JGIZMO::JRootObjectID::is_valid
bool is_valid() const
Check validity.
Definition: JRootObjectID.hh:133
JGIZMO::JRootObjectID::PATHNAME_SEPARATOR
static const char PATHNAME_SEPARATOR
path name separator
Definition: JRootObjectID.hh:43
JGIZMO::JRootObjectID::FILENAME_SEPARATOR
static const char FILENAME_SEPARATOR
file name separator
Definition: JRootObjectID.hh:42
JGIZMO::JRootObjectID
Auxiliary class to handle file name, ROOT directory and object name.
Definition: JRootObjectID.hh:36
JGIZMO::JRootObjectID::operator>>
friend std::istream & operator>>(std::istream &in, JRootObjectID &object)
Read object identifier from input.
Definition: JRootObjectID.hh:172
JGIZMO::JRootObjectID::getDirectory
TString getDirectory() const
Get directory.
Definition: JRootObjectID.hh:83
JLANG::JParseError
Exception for parsing value.
Definition: JException.hh:180
JGIZMO::JRootObjectID::file_name
std::string file_name
Definition: JRootObjectID.hh:239
JGIZMO
Auxiliary applications for use of ROOT and more.
Definition: JCanvas.hh:15
std
Definition: jaanetDictionary.h:36
JGIZMO::JRootObjectID::operator==
friend bool operator==(const JRootObjectID &first, const JRootObjectID &second)
Equal operator for object identifiers.
Definition: JRootObjectID.hh:157
JGIZMO::JRootObjectID::JRootObjectID
JRootObjectID()
Default constructor.
Definition: JRootObjectID.hh:49
JGIZMO::JRootObjectID::operator<<
friend std::ostream & operator<<(std::ostream &out, const JRootObjectID &object)
Write object identifier to output.
Definition: JRootObjectID.hh:233