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