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