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