Jpp test-rotations-old-533-g2bdbdb559
the software that should make you happy
Loading...
Searching...
No Matches
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
18namespace JGIZMO {}
19namespace JPP { using namespace JGIZMO; }
20
21namespace JGIZMO {
22
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 */
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 * Constructor.
69 *
70 * \param file_name file name
71 * \param dir directory name
72 * \param name object name
73 */
74 JRootObjectID(const std::string& file_name,
75 const std::string& dir,
76 const std::string& name) :
78 directory(dir),
79 name (name)
80 {}
81
82
83 /**
84 * Constructor.
85 *
86 * \param full_name full object name
87 */
88 JRootObjectID(const std::string& full_name)
89 {
90 std::istringstream(full_name) >> *this;
91 }
92
93
94 /**
95 * Get file name.
96 *
97 * \return file name
98 */
99 const std::string& getFilename() const
100 {
101 return file_name;
102 }
103
104
105 /**
106 * Get directory.
107 *
108 * \return directory
109 */
110 TString getDirectory() const
111 {
112 return TString(directory.c_str());
113 }
114
115
116 /**
117 * Get object name.
118 *
119 * \return object name
120 */
121 TString getObjectName() const
122 {
123 return TString(name.c_str());
124 }
125
126
127 /**
128 * Get full file name, including path.
129 *
130 * \return file name
131 */
132 TString getFullFilename() const
133 {
134 if (getDirectory() == "")
135 return getFilename();
136 else
138 }
139
140
141 /**
142 * Get full object name, including path.
143 *
144 * \return object name
145 */
146 TString getFullObjectName() const
147 {
148 if (getDirectory() == "")
149 return getObjectName();
150 else
152 }
153
154
155 /**
156 * Check validity.
157 *
158 * \return true if valid ROOT object identifier; else false
159 */
160 bool is_valid() const
161 {
162 return (file_name != "" &&
163 name != "");
164 }
165
166 /**
167 * Clear.
168 */
169 void clear()
170 {
171 file_name.clear();
172 directory.clear();
173 name .clear();
174 }
175
176
177 /**
178 * Equal operator for object identifiers.
179 *
180 * \param first first object identifier
181 * \param second second object identifier
182 * \return true if first and second object identifier are equal; else false
183 */
184 friend inline bool operator==(const JRootObjectID& first, const JRootObjectID& second)
185 {
186 return (first.getFilename() == second.getFilename() &&
187 first.getDirectory() == second.getDirectory() &&
188 first.getObjectName() == second.getObjectName());
189 }
190
191
192 /**
193 * Read object identifier from input.
194 *
195 * \param in input stream
196 * \param object object identifier
197 * \return input stream
198 */
199 friend inline std::istream& operator>>(std::istream& in, JRootObjectID& object)
200 {
201 using namespace std;
202
203 object.clear();
204
205 string buffer;
206
207 in >> ws;
208
209 for (int bracket = 0; in.peek() != EOF; ) {
210
211 const char c = (char) in.get();
212
213 if (c == LABEL_L_BRACKET) {
214
215 ++bracket;
216
217 } else if (c == LABEL_R_BRACKET) {
218
219 --bracket;
220
221 } else if (isspace(c)) {
222
223 if (bracket <= 0) {
224 break;
225 }
226 }
227
228 buffer.push_back(c);
229 }
230
231 size_t pos = buffer.find(FILENAME_SEPARATOR);
232
233 if (pos != string::npos) {
234
235 object.file_name = buffer.substr(0, pos);
236 object.name = buffer.substr(pos + 1);
237
238 pos = object.name.rfind(PATHNAME_SEPARATOR);
239
240 if (pos != string::npos) {
241
242 object.directory = object.name.substr(0, pos);
243 object.name = object.name.substr(pos + 1);
244 }
245
246 } else if (!buffer.empty()) {
247
248 throw JParseError("JRootObjectID error parsing " + buffer);
249 }
250
251 return in;
252 }
253
254
255 /**
256 * Write object identifier to output.
257 *
258 * \param out output stream
259 * \param object object identifier
260 * \return output stream
261 */
262 friend inline std::ostream& operator<<(std::ostream& out, const JRootObjectID& object)
263 {
264 return out << object.getFilename() << FILENAME_SEPARATOR << object.getFullObjectName();
265 }
266
267 protected:
268 std::string file_name;
269 std::string directory;
270 std::string name;
271 };
272}
273
274#endif
Exceptions.
Auxiliary class to handle file name, ROOT directory and object name.
JRootObjectID(const std::string &file_name, const std::string &dir, const std::string &name)
Constructor.
TString getObjectName() const
Get object name.
friend std::istream & operator>>(std::istream &in, JRootObjectID &object)
Read object identifier from input.
JRootObjectID()
Default constructor.
static const char PATHNAME_SEPARATOR
path name separator
JRootObjectID(const std::string &full_name)
Constructor.
friend bool operator==(const JRootObjectID &first, const JRootObjectID &second)
Equal operator for object identifiers.
static const char LABEL_L_BRACKET
left bracket for label
TString getFullFilename() const
Get full file name, including path.
friend std::ostream & operator<<(std::ostream &out, const JRootObjectID &object)
Write object identifier to output.
static const char LABEL_R_BRACKET
right bracket for label
TString getDirectory() const
Get directory.
const std::string & getFilename() const
Get file name.
bool is_valid() const
Check validity.
static const char FILENAME_SEPARATOR
file name separator
JRootObjectID(const std::string &file_name, const std::string &name)
Constructor.
TString getFullObjectName() const
Get full object name, including path.
Exception for parsing value.
Auxiliary applications for use of ROOT and more.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).