Jpp
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 occurences 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 occurences of <tt>target</tt> replaced by <tt>replacement</tt>.
141  *
142  * \param input input string
143  * \param target target character
144  * \param replacement replacement character
145  * \return modified string
146  */
147  inline std::string replace(const std::string& input, const char target, const char replacement)
148  {
149  using namespace std;
150 
151  string buffer = input;
152 
153  for (string::iterator i = buffer.begin(); i != buffer.end(); ++i) {
154  if (*i == target) {
155  *i = replacement;
156  }
157  }
158 
159  return buffer;
160  }
161 
162 
163  /**
164  * Trim string.\n
165  * Returns a copy of the string, with leading and trailing target characters omitted.
166  *
167  * \param buffer input string
168  * \param target strip character(s)
169  * \return modified string
170  */
171  inline std::string trim(const std::string& buffer, const std::string& target)
172  {
173  using namespace std;
174 
175  string::const_iterator p = buffer. begin();
176  string::const_reverse_iterator q = buffer.rbegin();
177 
178  while (p != q.base() && target.find(*p) != string::npos) { ++p; }
179  while (p != q.base() && target.find(*q) != string::npos) { ++q; }
180 
181  return string(p,q.base());
182  }
183 
184 
185  /**
186  * Convert value to string.
187  *
188  * \param value value
189  * \return string
190  */
191  template<class T>
192  inline std::string to_string(const T& value)
193  {
194  using namespace std;
195 
196  ostringstream os;
197 
198  os << value;
199 
200  return os.str();
201  }
202 
203 
204  /**
205  * Convert string to value.
206  *
207  * \param input string
208  * \return value
209  */
210  template<class T>
211  inline T to_value(const std::string& input)
212  {
213  using namespace std;
214 
215  T value;
216 
217  istringstream(input) >> value;
218 
219  return value;
220  }
221 
222 
223  /**
224  * Convert all character to upper case.
225  *
226  * \param value value
227  * \return string
228  */
229  inline std::string to_upper(const std::string& value)
230  {
231  using namespace std;
232 
233  string buffer(value);
234 
235  for (string::iterator i = buffer.begin(); i != buffer.end(); ++i) {
236  *i = toupper(*i);
237  }
238 
239  return buffer;
240  }
241 
242 
243  /**
244  * Convert all character to lower case.
245  *
246  * \param value value
247  * \return string
248  */
249  inline std::string to_lower(const std::string& value)
250  {
251  using namespace std;
252 
253  string buffer(value);
254 
255  for (string::iterator i = buffer.begin(); i != buffer.end(); ++i) {
256  *i = tolower(*i);
257  }
258 
259  return buffer;
260  }
261 
262 
263  /**
264  * Count number of white space separated tokens.
265  *
266  * \param buffer input string
267  * \return number of tokens
268  */
269  inline size_t get_number_of_tokens(const std::string& buffer)
270  {
271  using namespace std;
272 
273  istringstream is(buffer);
274 
275  return distance(istream_iterator<string>(is), istream_iterator<string>());
276  }
277 
278 
279  /**
280  * Check quotation.
281  *
282  * \param value value
283  * \return true if quoted; else false
284  */
285  inline bool is_single_quoted(const std::string& value)
286  {
287  return (value.size() > 1 && *value.begin() == '\'' && *value.rbegin() == '\'');
288  }
289 
290 
291  /**
292  * Check quotation.
293  *
294  * \param value value
295  * \return true if quoted; else false
296  */
297  inline bool is_double_quoted(const std::string& value)
298  {
299  return (value.size() > 1 && *value.begin() == '\"' && *value.rbegin() == '\"');
300  }
301 
302 
303  /**
304  * Quote string.
305  *
306  * \param value value
307  * \return string
308  */
309  inline std::string single_quote(const std::string& value)
310  {
311  if (!is_single_quoted(value))
312  return "\'" + value + "\'";
313  else
314  return value;
315  }
316 
317 
318  /**
319  * Quote string.
320  *
321  * \param value value
322  * \return string
323  */
324  inline std::string double_quote(const std::string& value)
325  {
326  if (!is_double_quoted(value))
327  return "\"" + value + "\"";
328  else
329  return value;
330  }
331 }
332 
333 #endif
JLANG::single_quote
std::string single_quote(const std::string &value)
Quote string.
Definition: JLangToolkit.hh:309
JLANG::is_integer
bool is_integer(const std::string &buffer)
Check if string is an integer.
Definition: JLangToolkit.hh:58
JLANG::to_upper
std::string to_upper(const std::string &value)
Convert all character to upper case.
Definition: JLangToolkit.hh:229
JLANG::double_quote
std::string double_quote(const std::string &value)
Quote string.
Definition: JLangToolkit.hh:324
JLANG::trim
std::string trim(const std::string &buffer, const std::string &target)
Trim string.
Definition: JLangToolkit.hh:171
distance
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
Definition: PhysicsEvent.hh:434
JLANG::get_number_of_tokens
size_t get_number_of_tokens(const std::string &buffer)
Count number of white space separated tokens.
Definition: JLangToolkit.hh:269
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JLANG::is_single_quoted
bool is_single_quoted(const std::string &value)
Check quotation.
Definition: JLangToolkit.hh:285
JLANG::replace
std::string replace(const std::string &input, const char target, const char replacement)
Replace characters in string.
Definition: JLangToolkit.hh:147
JLANG::getSize
size_t getSize(T(&array)[N])
Get size of c-array.
Definition: JLangToolkit.hh:32
JLANG::to_lower
std::string to_lower(const std::string &value)
Convert all character to lower case.
Definition: JLangToolkit.hh:249
JLANG::to_string
std::string to_string(const T &value)
Convert value to string.
Definition: JLangToolkit.hh:192
JLANG::is_double_quoted
bool is_double_quoted(const std::string &value)
Check quotation.
Definition: JLangToolkit.hh:297
std
Definition: jaanetDictionary.h:36
JLANG::to_value
T to_value(const std::string &input)
Convert string to value.
Definition: JLangToolkit.hh:211
JLANG
Auxiliary classes and methods for language specific functionality.
Definition: JAbstractClass.hh:10
JLANG::is_identical
bool is_identical(JFirst_t &first, JSecond_t &second)
Check if two objects are indentical.
Definition: JLangToolkit.hh:46