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