Jpp  17.2.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JLangToolkit.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JLANGTOOLKIT__
2 #define __JLANG__JLANGTOOLKIT__
3 
4 #include <string>
5 #include <sstream>
6 #include <iterator>
7 #include <ctype.h>
8 
9 
10 /**
11  * \author mdejong
12  */
13 
14 /**
15  * Auxiliary classes and methods for language specific functionality.
16  */
17 namespace JLANG {}
18 namespace JPP { using namespace JLANG; }
19 
20 namespace JLANG {
21 
22  using std::size_t;
23 
24 
25  /**
26  * Get size of c-array.
27  *
28  * \param array array
29  * \return size
30  */
31  template<class T, size_t N>
32  inline size_t getSize(T (&array)[N])
33  {
34  return N;
35  }
36 
37 
38  /**
39  * Check if two objects are indentical.
40  *
41  * \param first first object
42  * \param second second object
43  * \return true if addresses are equal; else false
44  */
45  template<class JFirst_t, class JSecond_t>
46  inline bool is_identical(JFirst_t& first, JSecond_t& second)
47  {
48  return (void*) &first == (void*) &second;
49  }
50 
51 
52  /**
53  * Check if string is an integer.\n
54  *
55  * \param buffer input string
56  * \return true if integer; else false
57  */
58  inline bool is_integer(const std::string& buffer)
59  {
60  using namespace std;
61 
62  for (string::const_iterator i = buffer. begin(); i != buffer.end(); ++i) {
63  if (!isdigit(*i)) {
64  return false;
65  }
66  }
67 
68  return !buffer.empty();
69  }
70 
71 
72  /**
73  * Trim string.\n
74  * Returns a copy of the string, with leading and trailing white spaces omitted.
75  *
76  * \param buffer input string
77  * \return modified string
78  */
79  inline std::string trim(const std::string& buffer)
80  {
81  using namespace std;
82 
83  string::const_iterator p = buffer. begin();
84  string::const_reverse_iterator q = buffer.rbegin();
85 
86  while (p != q.base() && isspace(*p)) { ++p; }
87  while (p != q.base() && isspace(*q)) { ++q; }
88 
89  return string(p,q.base());
90  }
91 
92 
93  /**
94  * Trim string.\n
95  * Returns a copy of the string, with leading and trailing target characters omitted.
96  *
97  * \param buffer input string
98  * \param c strip character
99  * \return modified string
100  */
101  inline std::string trim(const std::string& buffer, const char c)
102  {
103  using namespace std;
104 
105  string::const_iterator p = buffer. begin();
106  string::const_reverse_iterator q = buffer.rbegin();
107 
108  while (p != q.base() && *p == c) { ++p; }
109  while (p != q.base() && *q == c) { ++q; }
110 
111  return string(p,q.base());
112  }
113 
114 
115  /**
116  * Replace tokens in string.\n
117  * Returns a copy of the string, with all occurrences of <tt>target</tt> replaced by <tt>replacement</tt>.
118  *
119  * \param input input string
120  * \param target target string
121  * \param replacement replacement string
122  * \return modified string
123  */
124  inline std::string replace(const std::string& input, const std::string& target, const std::string& replacement)
125  {
126  using namespace std;
127 
128  string buffer = input;
129 
130  for (size_t i = buffer.find(target); i != string::npos; i = buffer.find(target,i)) {
131  buffer.replace(buffer.begin() + i, buffer.begin() + i + target.length(), replacement);
132  }
133 
134  return buffer;
135  }
136 
137 
138  /**
139  * Replace characters in string.\n
140  * Returns a copy of the string, with all occurrences of <tt>target</tt> replaced by <tt>replacement</tt>.
141  *
142  * \param input input string
143  * \param target target character
144  * \param replacement replacement string
145  * \return modified string
146  */
147  inline std::string replace(const std::string& input, const char target, const std::string& replacement)
148  {
149  return replace(input, std::string(1, target), replacement);
150  }
151 
152 
153  /**
154  * Replace characters in string.\n
155  * Returns a copy of the string, with all occurrences of <tt>target</tt> replaced by <tt>replacement</tt>.
156  *
157  * \param input input string
158  * \param target target character
159  * \param replacement replacement character
160  * \return modified string
161  */
162  inline std::string replace(const std::string& input, const char target, const char replacement)
163  {
164  using namespace std;
165 
166  string buffer = input;
167 
168  for (string::iterator i = buffer.begin(); i != buffer.end(); ++i) {
169  if (*i == target) {
170  *i = replacement;
171  }
172  }
173 
174  return buffer;
175  }
176 
177 
178  /**
179  * Trim string.\n
180  * Returns a copy of the string, with leading and trailing target characters omitted.
181  *
182  * \param buffer input string
183  * \param target strip character(s)
184  * \return modified string
185  */
186  inline std::string trim(const std::string& buffer, const std::string& target)
187  {
188  using namespace std;
189 
190  string::const_iterator p = buffer. begin();
191  string::const_reverse_iterator q = buffer.rbegin();
192 
193  while (p != q.base() && target.find(*p) != string::npos) { ++p; }
194  while (p != q.base() && target.find(*q) != string::npos) { ++q; }
195 
196  return string(p,q.base());
197  }
198 
199 
200  /**
201  * Convert value to string.
202  *
203  * \param value value
204  * \return string
205  */
206  template<class T>
207  inline std::string to_string(const T& value)
208  {
209  using namespace std;
210 
211  ostringstream os;
212 
213  os << value;
214 
215  return os.str();
216  }
217 
218 
219  /**
220  * Convert string to value.
221  *
222  * \param input string
223  * \return value
224  */
225  template<class T>
226  inline T to_value(const std::string& input)
227  {
228  using namespace std;
229 
230  T value;
231 
232  istringstream(input) >> value;
233 
234  return value;
235  }
236 
237 
238  /**
239  * Convert all character to upper case.
240  *
241  * \param value value
242  * \return string
243  */
244  inline std::string to_upper(const std::string& value)
245  {
246  using namespace std;
247 
248  string buffer(value);
249 
250  for (string::iterator i = buffer.begin(); i != buffer.end(); ++i) {
251  *i = toupper(*i);
252  }
253 
254  return buffer;
255  }
256 
257 
258  /**
259  * Convert all character to lower case.
260  *
261  * \param value value
262  * \return string
263  */
264  inline std::string to_lower(const std::string& value)
265  {
266  using namespace std;
267 
268  string buffer(value);
269 
270  for (string::iterator i = buffer.begin(); i != buffer.end(); ++i) {
271  *i = tolower(*i);
272  }
273 
274  return buffer;
275  }
276 
277 
278  /**
279  * Count number of white space separated tokens.
280  *
281  * \param buffer input string
282  * \return number of tokens
283  */
284  inline size_t get_number_of_tokens(const std::string& buffer)
285  {
286  using namespace std;
287 
288  istringstream is(buffer);
289 
290  return distance(istream_iterator<string>(is), istream_iterator<string>());
291  }
292 
293 
294  /**
295  * Check quotation.
296  *
297  * \param value value
298  * \return true if quoted; else false
299  */
300  inline bool is_single_quoted(const std::string& value)
301  {
302  return (value.size() > 1 && *value.begin() == '\'' && *value.rbegin() == '\'');
303  }
304 
305 
306  /**
307  * Check quotation.
308  *
309  * \param value value
310  * \return true if quoted; else false
311  */
312  inline bool is_double_quoted(const std::string& value)
313  {
314  return (value.size() > 1 && *value.begin() == '\"' && *value.rbegin() == '\"');
315  }
316 
317 
318  /**
319  * Quote string.
320  *
321  * \param value value
322  * \return string
323  */
324  inline std::string single_quote(const std::string& value)
325  {
326  if (!is_single_quoted(value))
327  return "\'" + value + "\'";
328  else
329  return value;
330  }
331 
332 
333  /**
334  * Quote string.
335  *
336  * \param value value
337  * \return string
338  */
339  inline std::string double_quote(const std::string& value)
340  {
341  if (!is_double_quoted(value))
342  return "\"" + value + "\"";
343  else
344  return value;
345  }
346 }
347 
348 #endif
TString replace(const TString &target, const TRegexp &regexp, const T &replacement)
Replace regular expression in input by given replacement.
Definition: JPrintResult.cc:63
std::string to_lower(const std::string &value)
Convert all character to lower case.
std::string single_quote(const std::string &value)
Quote string.
bool is_double_quoted(const std::string &value)
Check quotation.
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
std::string double_quote(const std::string &value)
Quote string.
then JShowerPostfit f $INPUT_FILE o $OUTPUT_FILE N
T to_value(const std::string &input)
Convert string to value.
is
Definition: JDAQCHSM.chsm:167
size_t getSize(T(&array)[N])
Get size of c-array.
Definition: JLangToolkit.hh:32
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
std::string trim(const std::string &buffer)
Trim string.
Definition: JLangToolkit.hh:79
bool is_identical(JFirst_t &first, JSecond_t &second)
Check if two objects are indentical.
Definition: JLangToolkit.hh:46
bool is_single_quoted(const std::string &value)
Check quotation.
do set_variable OUTPUT_DIRECTORY $WORKDIR T
size_t get_number_of_tokens(const std::string &buffer)
Count number of white space separated tokens.
bool is_integer(const std::string &buffer)
Check if string is an integer.
Definition: JLangToolkit.hh:58
std::string to_upper(const std::string &value)
Convert all character to upper case.
std::string to_string(const T &value)
Convert value to string.
$WORKDIR ev_configure_domsimulator txt echo process $DOM_SIMULATOR $i $SOURCE_HOST[$index] csh c(setenv ROOTSYS $ROOTSYS &&source $JPP_DIR/setenv.csh $JPP_DIR &&($DOM_SIMULATOR\-u\$NAME\$\-H\$SERVER\$\-M\$LOGGER\$\-d $DEBUG</dev/null > &/dev/null &))'