Jpp  17.3.0-rc.2
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 #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_WILD_CARD = '%'; //!< 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_WILD_CARD) != 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_WILD_CARD);
74 
75  if (pos == string::npos) {
76  THROW(JNoValue, "Method getFilename(): Missing wild card character \'" << FILENAME_WILD_CARD << "\'.");
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  */
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  */
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 string buffer(getenv(variable.c_str()));
225 
226  if (!buffer.empty()) {
227 
228  size_t pos = 0, len;
229 
230  do {
231 
232  len = buffer.substr(pos).find(PATHLIST_SEPARATOR);
233  path = buffer.substr(pos,len);
234 
235  } while (!ifstream(getFilename(path, file_name).c_str()).good() && len != string::npos && (pos += len + 1) != buffer.length());
236  }
237  }
238 
239  if (ifstream(getFilename(path, file_name).c_str()).good())
240  return path;
241  else
242  return "";
243  }
244 
245 
246  /**
247  * Get full file name (see JEEP::getPath).
248  *
249  * \param variable environment variable
250  * \param file_name file name
251  * \return file name
252  */
253  inline std::string getFullFilename(const std::string& variable, const std::string& file_name)
254  {
255  return getFilename(getPath(variable, file_name), file_name);
256  }
257 
258 
259  /**
260  * Get name space, i.e.\ part before JEEP::TYPENAME_SEPARATOR.
261  *
262  * \param type_name type name
263  * \return name space (possibly empty)
264  */
265  inline std::string getNamespace(const std::string& type_name)
266  {
267  using namespace std;
268 
269  const size_t pos = type_name.rfind(TYPENAME_SEPARATOR);
270 
271  if (pos != string::npos)
272  return type_name.substr(0, pos);
273  else
274  return "";
275  }
276 
277 
278  /**
279  * Get type name, i.e.\ part after JEEP::TYPENAME_SEPARATOR.
280  *
281  * \param type_name type name
282  * \return class name
283  */
284  inline std::string getClassname(const std::string& type_name)
285  {
286  using namespace std;
287 
288  const size_t pos = type_name.rfind(TYPENAME_SEPARATOR);
289 
290  if (pos != string::npos)
291  return type_name.substr(pos + 2);
292  else
293  return type_name;
294  }
295 
296 
297  /**
298  * Get protocol, i.e.\ part before first JEEP::PROTOCOL_SEPARATOR if any.
299  *
300  * \param file_name file name
301  * \return protocol (excluding separator)
302  */
303  inline std::string getProtocol(const std::string& file_name)
304  {
305  using namespace std;
306 
307  const size_t pos = file_name.find(PROTOCOL_SEPARATOR);
308 
309  if (pos != string::npos)
310  return file_name.substr(0, pos);
311  else
312  return "";
313  }
314 
315 
316  /**
317  * Get URL of document pages.
318  *
319  * \return URL
320  */
322  {
323  const char* const url = getenv(JPP_PAGES);
324 
325  return std::string(url != NULL ? url : "");
326  }
327 
328 
329  /**
330  * Open file.
331  *
332  * \param file_name file name
333  * \return pointer to input stream
334  */
335  template<class T>
336  inline T* open(const std::string& file_name);
337 
338 
339  /**
340  * Open file.
341  *
342  * \param file_name file name
343  * \return pointer to input stream
344  */
345  template<>
346  inline std::istream* open(const std::string& file_name)
347  {
348  using namespace std;
349  using namespace JPP;
350 
351  if (getFilenameExtension(file_name) == "gz")
352  return new igzstream(file_name.c_str());
353  else if (getFilenameExtension(file_name) == "txt")
354  return new ifstream (file_name.c_str());
355  else
356  return NULL;
357  }
358 
359 
360  /**
361  * Open file.
362  *
363  * \param file_name file name
364  * \return pointer to output stream
365  */
366  template<>
367  inline std::ostream* open(const std::string& file_name)
368  {
369  using namespace std;
370  using namespace JPP;
371 
372  if (getFilenameExtension(file_name) == "gz")
373  return new ogzstream(file_name.c_str());
374  else if (getFilenameExtension(file_name) == "txt")
375  return new ofstream (file_name.c_str());
376  else
377  return NULL;
378  }
379 
380 
381  /**
382  * Close file.
383  *
384  * \param pf pointer to file stream
385  */
386  inline void close(std::istream* pf)
387  {
388  using namespace std;
389  using namespace JPP;
390 
391  if (dynamic_cast<ifstream*> (pf) != NULL) { dynamic_cast<ifstream*> (pf)->close(); return; }
392  if (dynamic_cast<igzstream*>(pf) != NULL) { dynamic_cast<igzstream*>(pf)->close(); return; }
393  }
394 
395 
396  /**
397  * Close file.
398  *
399  * \param pf pointer to file stream
400  */
401  inline void close(std::ostream* pf)
402  {
403  using namespace std;
404  using namespace JPP;
405 
406  if (dynamic_cast<ofstream*> (pf) != NULL) { dynamic_cast<ofstream*> (pf)->close(); return; }
407  if (dynamic_cast<ogzstream*>(pf) != NULL) { dynamic_cast<ogzstream*>(pf)->close(); return; }
408  }
409 }
410 
411 #endif
static const char *const TYPENAME_SEPARATOR
type name separator
Definition: JeepToolkit.hh:42
Exceptions.
static const char *const LD_LIBRARY_PATH
Nick names of environment variables.
Definition: JeepToolkit.hh:34
std::string getPath(const std::string &file_name)
Get path, i.e. part before last JEEP::PATHNAME_SEPARATOR if any.
Definition: JeepToolkit.hh:148
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
std::string getFullPath(const std::string &path)
Get full path, i.e. add JEEP::PATHNAME_SEPARATOR if necessary.
Definition: JeepToolkit.hh:168
std::string getNamespace(const std::string &type_name)
Get name space, i.e. part before JEEP::TYPENAME_SEPARATOR.
Definition: JeepToolkit.hh:265
T * open(const std::string &file_name)
Open file.
Definition: JeepToolkit.hh:346
Exception for missing value.
Definition: JException.hh:198
static const char PROTOCOL_SEPARATOR
protocol separator
Definition: JeepToolkit.hh:43
std::string getClassname(const std::string &type_name)
Get type name, i.e. part after JEEP::TYPENAME_SEPARATOR.
Definition: JeepToolkit.hh:284
void close(std::istream *pf)
Close file.
Definition: JeepToolkit.hh:386
do set_variable OUTPUT_DIRECTORY $WORKDIR T
std::string getURL()
Get URL of document pages.
Definition: JeepToolkit.hh:321
then awk string
$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_DQSIM<$WORKDIR/ev_configure_dqsimulator.txt > RC_DWRT path
std::string strip(const std::string &file_name)
Strip leading and trailing white spaces from file name.
Definition: JeepToolkit.hh:89
std::string getProtocol(const std::string &file_name)
Get protocol, i.e. part before first JEEP::PROTOCOL_SEPARATOR if any.
Definition: JeepToolkit.hh:303
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
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
static const char FILENAME_SEPARATOR
file name separator
Definition: JeepToolkit.hh:41
static const char PATHLIST_SEPARATOR
path list separator
Definition: JeepToolkit.hh:40
static const char FILENAME_WILD_CARD
wild card character for file name substitution
Definition: JeepToolkit.hh:44
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:128
static const char *const JPP_PAGES
Jpp document pages.
Definition: JeepToolkit.hh:37
std::string getFullFilename(const std::string &variable, const std::string &file_name)
Get full file name (see JEEP::getPath).
Definition: JeepToolkit.hh:253
static const char *const SHELL
SHELL.
Definition: JeepToolkit.hh:36
static const char PATHNAME_SEPARATOR
path name separator
Definition: JeepToolkit.hh:39
static const char *const PATH
binary file paths
Definition: JeepToolkit.hh:35
bool hasWildCard(const std::string &file_name)
Check presence of wild card.
Definition: JeepToolkit.hh:53