Jpp 21.0.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
JeepToolkit.hh
Go to the documentation of this file.
1#ifndef __JEEP__JEEPTOOLKIT__
2#define __JEEP__JEEPTOOLKIT__
3
4#include <string>
5#include <fstream>
6#include <sstream>
7#include <cstdlib>
8#include <map>
9
10#include "JLang/gzstream.h"
11#include "JLang/JException.hh"
12
13
14/**
15 * \file
16 *
17 * Auxiliary methods for handling file names, type names and environment.
18 * \author mdejong
19 */
20
21/**
22 * General puprpose classes and methods.
23 */
24namespace JEEP {}
25namespace JPP { using namespace JEEP; }
26
27namespace JEEP {
28
29 using JLANG::JNoValue;
30
31 /**
32 * Nick names of environment variables.
33 */
34 static const char* const LD_LIBRARY_PATH = "LD_LIBRARY_PATH"; //!< library file paths
35 static const char* const PATH = "PATH"; //!< binary file paths
36 static const char* const SHELL = "SHELL"; //!< SHELL
37 static const char* const JPP_PAGES = "JPP_PAGES"; //!< Jpp document pages
38
39 static const char PATHNAME_SEPARATOR = '/'; //!< path name separator
40 static const char PATHLIST_SEPARATOR = ':'; //!< path list separator
41 static const char FILENAME_SEPARATOR = '.'; //!< file name separator
42 static const char* const TYPENAME_SEPARATOR = "::"; //!< type name separator
43 static const char PROTOCOL_SEPARATOR = ':'; //!< protocol separator
44 static const char FILENAME_WILDCARD = '%'; //!< wild card character for file name substitution
45
46
47 /**
48 * Check presence of wild card.
49 *
50 * \param file_name file name
51 * \return true if wild card present; else false
52 */
53 inline bool hasWildCard(const std::string& file_name)
54 {
55 return (file_name.find(FILENAME_WILDCARD) != std::string::npos);
56 }
57
58
59 /**
60 * Get file name by setting wild card to given value.
61 *
62 * \param file_name input file name
63 * \param value value
64 * \return output file name
65 */
66 inline std::string setWildCard(const std::string& file_name, const std::string& value)
67 {
68 using namespace std;
69
70 string buffer = file_name;
71
72 string::size_type pos = buffer.find(FILENAME_WILDCARD);
73
74 if (pos == string::npos) {
75 THROW(JNoValue, "Method setWildCard(): Missing wild card character \'" << FILENAME_WILDCARD << "\'.");
76 }
77
78 return buffer.replace(pos, 1, value);
79 }
80
81
82 /**
83 * Strip leading and trailing white spaces from file name.
84 *
85 * \param file_name file name
86 * \return file name
87 */
88 inline std::string strip(const std::string& file_name)
89 {
90 using namespace std;
91
92 string::const_iterator p = file_name. begin();
93 string::const_reverse_iterator q = file_name.rbegin();
94
95 for ( ; p != file_name.end() && q != file_name.rend() && isspace(*p); ++p) {}
96 for ( ; p != file_name.end() && q != file_name.rend() && isspace(*q); ++q) {}
97
98 return string(p,q.base());
99 }
100
101
102 /**
103 * Get file name extension, i.e.\ part after last JEEP::FILENAME_SEPARATOR if any.
104 *
105 * \param file_name file name
106 * \return extension (excluding separator)
107 */
108 inline std::string getFilenameExtension(const std::string& file_name)
109 {
110 using namespace std;
111
112 const size_t pos = file_name.rfind(FILENAME_SEPARATOR);
113
114 if (pos != string::npos)
115 return file_name.substr(pos + 1);
116 else
117 return "";
118 }
119
120
121 /**
122 * Get file name part, i.e.\ part after last JEEP::PATHNAME_SEPARATOR if any.
123 *
124 * \param file_name file name
125 * \return file name part (excluding separator)
126 */
127 inline std::string getFilename(const std::string& file_name)
128 {
129 using namespace std;
130
131 const string buffer = strip(file_name);
132 const size_t pos = buffer.rfind(PATHNAME_SEPARATOR);
133
134 if (pos != string::npos)
135 return buffer.substr(pos + 1);
136 else
137 return buffer;
138 }
139
140
141 /**
142 * Get path, i.e.\ part before last JEEP::PATHNAME_SEPARATOR if any.
143 *
144 * \param file_name file name
145 * \return path (including separator)
146 */
147 inline std::string getPath(const std::string& file_name)
148 {
149 using namespace std;
150
151 const string buffer = strip(file_name);
152 const size_t pos = buffer.rfind(PATHNAME_SEPARATOR);
153
154 if (pos != string::npos)
155 return buffer.substr(0, pos + 1);
156 else
157 return "";
158 }
159
160
161 /**
162 * Get full path, i.e.\ add JEEP::PATHNAME_SEPARATOR if necessary.
163 *
164 * \param path path
165 * \return path (including separator)
166 */
167 inline std::string getFullPath(const std::string& path)
168 {
169 using namespace std;
170
171 const string buffer = strip(path);
172
173 if (buffer.empty() || *buffer.rbegin() == PATHNAME_SEPARATOR) {
174
175 return buffer;
176
177 } else {
178
179 return buffer + PATHNAME_SEPARATOR;
180 }
181 }
182
183
184 /**
185 * Compose full file name and introduce JEEP::PATHNAME_SEPARATOR if needed.
186 *
187 * \param path path
188 * \param file_name file name
189 * \return file name
190 */
191 inline std::string getFilename(const std::string& path, const std::string& file_name)
192 {
193 using namespace std;
194
195 const string buffer = getFullPath(path);
196
197 if (buffer.empty())
198 return strip(file_name);
199 else
200 return buffer + strip(file_name);
201 }
202
203
204 /**
205 * Get selected path from environment variable for given file name.
206 *
207 * The environment variable is parsed according character JEEP::PATHLIST_SEPARATOR.
208 * The first path in which a file exists with the given file name is returned.
209 * If no existing file is found, an empty path is returned.
210 *
211 * \param variable environment variable
212 * \param file_name file name
213 * \return path
214 */
215 inline std::string getPath(const std::string& variable, const std::string& file_name)
216 {
217 using namespace std;
218
219 string path = "";
220
221 if (!file_name.empty() && file_name[0] != PATHNAME_SEPARATOR) {
222
223 const char* const p = getenv(variable.c_str());
224
225 if (p != NULL) {
226
227 const string buffer(p);
228
229 if (!buffer.empty()) {
230
231 size_t pos = 0, len;
232
233 do {
234
235 len = buffer.substr(pos).find(PATHLIST_SEPARATOR);
236 path = buffer.substr(pos,len);
237
238 } while (!ifstream(getFilename(path, file_name).c_str()).good() && len != string::npos && (pos += len + 1) != buffer.length());
239 }
240 }
241 }
242
243 if (ifstream(getFilename(path, file_name).c_str()).good())
244 return path;
245 else
246 return "";
247 }
248
249
250 /**
251 * Get full file name (see JEEP::getPath).
252 *
253 * \param variable environment variable
254 * \param file_name file name
255 * \return file name
256 */
257 inline std::string getFullFilename(const std::string& variable, const std::string& file_name)
258 {
259 return getFilename(getPath(variable, file_name), file_name);
260 }
261
262
263 /**
264 * Get name space, i.e.\ part before JEEP::TYPENAME_SEPARATOR.
265 *
266 * \param type_name type name
267 * \return name space (possibly empty)
268 */
269 inline std::string getNamespace(const std::string& type_name)
270 {
271 using namespace std;
272
273 const size_t pos = type_name.rfind(TYPENAME_SEPARATOR);
274
275 if (pos != string::npos)
276 return type_name.substr(0, pos);
277 else
278 return "";
279 }
280
281
282 /**
283 * Get type name, i.e.\ part after JEEP::TYPENAME_SEPARATOR.
284 *
285 * \param type_name type name
286 * \return class name
287 */
288 inline std::string getClassname(const std::string& type_name)
289 {
290 using namespace std;
291
292 const size_t pos = type_name.rfind(TYPENAME_SEPARATOR);
293
294 if (pos != string::npos)
295 return type_name.substr(pos + 2);
296 else
297 return type_name;
298 }
299
300
301 /**
302 * Get protocol, i.e.\ part before first JEEP::PROTOCOL_SEPARATOR if any.
303 *
304 * \param file_name file name
305 * \return protocol (excluding separator)
306 */
307 inline std::string getProtocol(const std::string& file_name)
308 {
309 using namespace std;
310
311 const size_t pos = file_name.find(PROTOCOL_SEPARATOR);
312
313 if (pos != string::npos)
314 return file_name.substr(0, pos);
315 else
316 return "";
317 }
318
319
320 /**
321 * Get URL of document pages.
322 *
323 * \return URL
324 */
325 inline std::string getURL()
326 {
327 const char* const url = getenv(JPP_PAGES);
328
329 return std::string(url != NULL ? url : "");
330 }
331
332
333 /**
334 * Open file.
335 *
336 * \param file_name file name
337 * \return pointer to input stream
338 */
339 template<class T>
340 inline T* open(const std::string& file_name);
341
342
343 /**
344 * Open file.
345 *
346 * \param file_name file name
347 * \return pointer to input stream
348 */
349 template<>
350 inline std::istream* open(const std::string& file_name)
351 {
352 using namespace std;
353 using namespace JPP;
354
355 if (getFilenameExtension(file_name) == "gz")
356 return new igzstream(file_name.c_str());
357 else if (getFilenameExtension(file_name) == "txt")
358 return new ifstream (file_name.c_str());
359 else
360 return NULL;
361 }
362
363
364 /**
365 * Open file.
366 *
367 * \param file_name file name
368 * \return pointer to output stream
369 */
370 template<>
371 inline std::ostream* open(const std::string& file_name)
372 {
373 using namespace std;
374 using namespace JPP;
375
376 if (getFilenameExtension(file_name) == "gz")
377 return new ogzstream(file_name.c_str());
378 else if (getFilenameExtension(file_name) == "txt")
379 return new ofstream (file_name.c_str());
380 else
381 return NULL;
382 }
383
384
385 /**
386 * Close file.
387 *
388 * \param pf pointer to file stream
389 */
390 inline void close(std::istream* pf)
391 {
392 using namespace std;
393 using namespace JPP;
394
395 if (dynamic_cast<ifstream*> (pf) != NULL) { dynamic_cast<ifstream*> (pf)->close(); return; }
396 if (dynamic_cast<igzstream*>(pf) != NULL) { dynamic_cast<igzstream*>(pf)->close(); return; }
397 }
398
399
400 /**
401 * Close file.
402 *
403 * \param pf pointer to file stream
404 */
405 inline void close(std::ostream* pf)
406 {
407 using namespace std;
408 using namespace JPP;
409
410 if (dynamic_cast<ofstream*> (pf) != NULL) { dynamic_cast<ofstream*> (pf)->close(); return; }
411 if (dynamic_cast<ogzstream*>(pf) != NULL) { dynamic_cast<ogzstream*>(pf)->close(); return; }
412 }
413}
414
415#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Exception for missing value.
General puprpose classes and methods.
static const char *const TYPENAME_SEPARATOR
type name separator
std::string getFullPath(const std::string &path)
Get full path, i.e. add JEEP::PATHNAME_SEPARATOR if necessary.
static const char *const PATH
binary file paths
static const char *const JPP_PAGES
Jpp document pages.
std::string getClassname(const std::string &type_name)
Get type name, i.e. part after JEEP::TYPENAME_SEPARATOR.
void close(std::istream *pf)
Close file.
std::string getFilename(const std::string &file_name)
Get file name part, i.e. part after last JEEP::PATHNAME_SEPARATOR if any.
std::string getFilenameExtension(const std::string &file_name)
Get file name extension, i.e. part after last JEEP::FILENAME_SEPARATOR if any.
static const char FILENAME_SEPARATOR
file name separator
std::string setWildCard(const std::string &file_name, const std::string &value)
Get file name by setting wild card to given value.
static const char PATHNAME_SEPARATOR
path name separator
static const char PROTOCOL_SEPARATOR
protocol separator
static const char *const LD_LIBRARY_PATH
Nick names of environment variables.
std::string getFullFilename(const std::string &variable, const std::string &file_name)
Get full file name (see JEEP::getPath).
bool hasWildCard(const std::string &file_name)
Check presence of wild card.
std::string getPath(const std::string &file_name)
Get path, i.e. part before last JEEP::PATHNAME_SEPARATOR if any.
std::string getProtocol(const std::string &file_name)
Get protocol, i.e. part before first JEEP::PROTOCOL_SEPARATOR if any.
std::string strip(const std::string &file_name)
Strip leading and trailing white spaces from file name.
std::string getURL()
Get URL of document pages.
T * open(const std::string &file_name)
Open file.
static const char FILENAME_WILDCARD
wild card character for file name substitution
static const char PATHLIST_SEPARATOR
path list separator
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).