Jpp  pmt_effective_area_update
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JString.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JSTRING__
2 #define __JLANG__JSTRING__
3 
4 #include <string>
5 #include <sstream>
6 #include <limits>
7 #include <ctype.h>
8 #include <stdio.h>
9 
10 #include "JLang/JStringFacet.hh"
11 #include "JLang/JException.hh"
12 #include "JLang/JLangToolkit.hh"
13 
14 
15 /**
16  * \author mdejong
17  */
18 
19 namespace JLANG {}
20 namespace JPP { using namespace JLANG; }
21 
22 namespace JLANG {
23 
24  /**
25  * Wrapper class around STL string class.
26  */
27  class JString :
28  public std::string
29  {
30  public:
31 
33  using std::string::assign;
34 
35 
36  /**
37  * Default constructor.
38  */
39  JString() :
40  std::string()
41  {}
42 
43 
44  /**
45  * Constructor.
46  *
47  * \param buffer string
48  */
49  explicit JString(const std::string& buffer) :
50  std::string(buffer)
51  {}
52 
53 
54  /**
55  * Constructor.
56  *
57  * \param c char
58  */
59  explicit JString(const char c):
60  std::string(1,c)
61  {}
62 
63 
64  /**
65  * Constructor.
66  *
67  * \param buffer string
68  * \param length length
69  */
70  explicit JString(const char* buffer, const std::string::size_type length) :
71  std::string(buffer, length)
72  {}
73 
74 
75  /**
76  * Constructor.
77  *
78  * \param value value
79  */
80  template<class T>
81  JString(const T& value) :
82  std::string(valueOf(value))
83  {}
84 
85 
86  /**
87  * Constructor.\n
88  * Each '%' in the format string will be replaced by the corresponding argument.
89  *
90  * \param format format
91  * \param args values
92  */
93  template<class ...Args>
94  JString(const char* format, const Args& ...args) :
95  std::string(format)
96  {
97  __set__(args...);
98  }
99 
100 
101  /**
102  * Constructor.\n
103  * This constructor compiles (see below) the input string.
104  *
105  * \param buffer input string
106  * \param facet facet
107  */
108  JString(const JString& buffer, const JStringFacet& facet) :
109  std::string(buffer)
110  {
111  compile(facet);
112  }
113 
114 
115  /**
116  * Compile token with given facet.\n
117  * This method uses the given facet to parse the input string.
118  * The result is then compatible with the definition of a token and may be empty.
119  *
120  * \param facet facet
121  * \result this string
122  */
123  JString& compile(const JStringFacet& facet)
124  {
125  using namespace std;
126 
127  istringstream is(*this);
128 
129  ios_base::iostate state;
130 
131  facet.get(is, istreambuf_iterator<char>(), is, state, *this);
132 
133  return *this;
134  }
135 
136 
137 
138  /**
139  * Test if this string starts with the specified prefix.
140  *
141  * \param prefix prefix
142  * \return true if this string starts with prefix; else false
143  */
144  bool startsWith(const std::string& prefix) const
145  {
146  return this->size() >= prefix.size() && this->substr(0, prefix.size()) == prefix;
147  }
148 
149 
150  /**
151  * Test if this string ends with the specified suffix.
152  *
153  * \param suffix suffix
154  * \return true if this string ends with suffix; else false
155  */
156  bool endsWith(const std::string& suffix) const
157  {
158  return this->size() >= suffix.size() && this->substr(this->size() - suffix.size()) == suffix;
159  }
160 
161 
162  /**
163  * Replace characters.
164  *
165  * \param target target character
166  * \param replacement replacement character
167  * \param max maximum number of replacements
168  * \return this string
169  */
170  JString& replace(const char target,
171  const char replacement,
172  const std::size_t max = std::numeric_limits<std::size_t>::max())
173  {
174  for (std::size_t i = this->find(target), n = max; i != std::string::npos && n != 0; i = find(target,i), --n) {
175  (*this)[i] = replacement;
176  }
177 
178  return *this;
179  }
180 
181 
182  /**
183  * Replace character sequences.
184  *
185  * \param target target string
186  * \param replacement replacement string
187  * \param max maximum number of replacements
188  * \return this string
189  */
190  JString& replace(const std::string& target,
191  const std::string& replacement,
192  const std::size_t max = std::numeric_limits<std::size_t>::max())
193  {
194  for (std::size_t i = this->find(target), n = max; i != std::string::npos && n != 0; i = this->find(target,i), --n) {
195  replace(this->begin() + i, this->begin() + i + target.length(), replacement);
196  }
197 
198  return *this;
199  }
200 
201 
202  /**
203  * Replace character sequence.
204  *
205  * \param target target string
206  * \param value value
207  * \param max maximum number of replacements
208  * \return this string
209  */
210  template<class T>
211  JString& replace(const std::string& target,
212  const T& value,
213  const std::size_t max = std::numeric_limits<std::size_t>::max())
214  {
215  for (std::size_t i = this->find(target), n = max; i != std::string::npos && n != 0; i = this->find(target,i), --n) {
216  replace(this->begin() + i, this->begin() + i + target.length(), JString(value));
217  }
218 
219  return *this;
220  }
221 
222 
223  /**
224  * Trim string.\n
225  * Returns the modified string, with leading and trailing white spaces omitted.
226  *
227  * \return this string
228  */
230  {
231  *this = JLANG::trim(*this);
232 
233  return *this;
234  }
235 
236 
237  /**
238  * Trim string.\n
239  * Returns the modified string, with leading and trailing target characters omitted.
240  *
241  * \param c strip character
242  * \return this string
243  */
244  JString& trim(const char c)
245  {
246  *this = JLANG::trim(*this, c);
247 
248  return *this;
249  }
250 
251 
252  /**
253  * Trim string.\n
254  * Returns the modified string, with leading and trailing target characters omitted.
255  *
256  * \param target character(s) to strip
257  * \return this string
258  */
259  JString trim(const std::string& target)
260  {
261  *this = JLANG::trim(*this, target);
262 
263  return *this;
264  }
265 
266 
267  /**
268  * Convert all character to upper case.
269  *
270  * \return this string
271  */
273  {
274  for (iterator i = begin(); i != end(); ++i) {
275  *i = toupper(*i);
276  }
277 
278  return *this;
279  }
280 
281 
282  /**
283  * Convert all character to lower case.
284  *
285  * \return this string
286  */
288  {
289  for (iterator i = begin(); i != end(); ++i) {
290  *i = tolower(*i);
291  }
292 
293  return *this;
294  }
295 
296 
297  /**
298  * Convert enumeration type to string.
299  *
300  * \param input value
301  * \return string
302  */
303  static JString valueOf(const int input)
304  {
305  std::ostringstream os;
306 
307  if (os << input)
308  return JString(os.str());
309  else
310  throw JException("JString::valueOf()");
311  }
312 
313 
314  /**
315  * Convert value to string.
316  *
317  * \param input value
318  * \return string
319  */
320  template<class T>
321  static JString valueOf(const T& input)
322  {
323  std::ostringstream os;
324 
325  if (os << input)
326  return JString(os.str());
327  else
328  throw JException("JString::valueOf()");
329  }
330 
331 
332  /**
333  * Convert string to value.
334  *
335  * \param input string
336  * \return value
337  */
338  template<class T>
339  static const T& toValue(const JString& input)
340  {
341  static T value;
342 
343  std::istringstream is(input);
344 
345  if (is >> value)
346  return value;
347  else
348  throw JException("JString::toValue<T>()");
349  }
350 
351 
352  /**
353  * Assign (part of) string to value.
354  *
355  * \param output value
356  * \return remaining string
357  */
358  template<class T>
359  JString& assign(T& output)
360  {
361  using namespace std;
362 
363  istringstream is(*this);
364 
365  is >> output;
366 
367  if (!is) {
368  throw JException("JString::assign()");
369  }
370 
371  this->assign(istreambuf_iterator<char>(is),
372  istreambuf_iterator<char>());
373 
374  return *this;
375  }
376 
377 
378  /**
379  * Read string from input stream.
380  *
381  * \param in input stream
382  * \param object string
383  * \return input stream
384  */
385  friend inline std::istream& operator>>(std::istream& in, JString& object)
386  {
387  using namespace std;
388 
389  istream::sentry sentry(in); // skips white spaces
390 
391  if (sentry) {
392 
393  const locale& loc = in.getloc();
394 
395  if (has_facet<JStringFacet>(loc)) {
396 
397  ios_base::iostate state;
398 
399  use_facet<JStringFacet>(loc).get(in, istreambuf_iterator<char>(), in, state, object);
400 
401  if (state != ios_base::goodbit && state != ios_base::eofbit) {
402  in.setstate(state);
403  }
404 
405  } else {
406 
407  in >> static_cast<string&>(object);
408  }
409  }
410 
411  return in;
412  }
413 
414 
415  /**
416  * Write string to output stream.
417  *
418  * \param out output stream
419  * \param object string
420  * \return output stream
421  */
422  friend inline std::ostream& operator<<(std::ostream& out, const JString& object)
423  {
424  using namespace std;
425 
426  const locale& loc = out.getloc();
427 
428  if (has_facet<JStringFacet>(loc)) {
429 
430  use_facet<JStringFacet>(loc).put(out, out, out.fill(), object);
431 
432  } else {
433 
434  out << static_cast<const string&>(object);
435  }
436 
437  return out;
438  }
439 
440  private:
441  /**
442  * Recursive method for formatting string.
443  *
444  * \param value next value
445  * \param args remaining values
446  */
447  template<class T, class ...Args>
448  void __set__(const T& value, const Args& ...args)
449  {
450  using namespace std;
451 
452  const size_t pos = this->find('%');
453 
454  if (pos != string::npos) {
455 
456  replace(pos, 1, JString::valueOf(value));
457 
458  __set__(args...);
459  }
460  }
461 
462 
463  /**
464  * Termination method for formatting string.
465  */
466  void __set__() const
467  {}
468  };
469 
470 
471  /**
472  * Read string from input stream until end of line.
473  *
474  * \param in input stream
475  * \param object string
476  * \return input stream
477  */
478  inline std::istream& getline(std::istream& in, JString& object)
479  {
480  using namespace std;
481 
482  istream::sentry sentry(in, true); // do not skip white spaces
483 
484  if (sentry) {
485 
486  const locale& loc = in.getloc();
487 
488  if (has_facet<JStringFacet>(loc)) {
489 
490  use_facet<JStringFacet>(loc).getline(in, object);
491 
492  } else {
493 
494  getline(in, static_cast<string&>(object));
495  }
496  }
497 
498  return in;
499  }
500 }
501 
502 #endif
JString(const std::string &buffer)
Constructor.
Definition: JString.hh:49
JString(const char *buffer, const std::string::size_type length)
Constructor.
Definition: JString.hh:70
void __set__() const
Termination method for formatting string.
Definition: JString.hh:466
General exception.
Definition: JException.hh:23
static JString valueOf(const T &input)
Convert value to string.
Definition: JString.hh:321
Exceptions.
JString trim(const std::string &target)
Trim string.
Definition: JString.hh:259
Wrapper class around STL string class.
Definition: JString.hh:27
TString replace(const TString &target, const TRegexp &regexp, const T &replacement)
Replace regular expression in input by given replacement.
Definition: JPrintResult.cc:63
JString & replace(const std::string &target, const std::string &replacement, const std::size_t max=std::numeric_limits< std::size_t >::max())
Replace character sequences.
Definition: JString.hh:190
static JString valueOf(const int input)
Convert enumeration type to string.
Definition: JString.hh:303
void __set__(const T &value, const Args &...args)
Recursive method for formatting string.
Definition: JString.hh:448
JString & toLower()
Convert all character to lower case.
Definition: JString.hh:287
friend std::istream & operator>>(std::istream &in, JString &object)
Read string from input stream.
Definition: JString.hh:385
static const T & toValue(const JString &input)
Convert string to value.
Definition: JString.hh:339
is
Definition: JDAQCHSM.chsm:167
JString(const char c)
Constructor.
Definition: JString.hh:59
bool endsWith(const std::string &suffix) const
Test if this string ends with the specified suffix.
Definition: JString.hh:156
JString & compile(const JStringFacet &facet)
Compile token with given facet.
Definition: JString.hh:123
std::string trim(const std::string &buffer)
Trim string.
Definition: JLangToolkit.hh:79
JString(const char *format, const Args &...args)
Constructor.
Definition: JString.hh:94
JString()
Default constructor.
Definition: JString.hh:39
JAssignSequence< typename JContainer_t::value_type > assign(JContainer_t &out)
Helper method to assign sequence of Comma Separated Values to output container.
Definition: JCSV.hh:129
friend std::ostream & operator<<(std::ostream &out, const JString &object)
Write string to output stream.
Definition: JString.hh:422
JString & trim()
Trim string.
Definition: JString.hh:229
bool startsWith(const std::string &prefix) const
Test if this string starts with the specified prefix.
Definition: JString.hh:144
do set_variable OUTPUT_DIRECTORY $WORKDIR T
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:478
JString(const T &value)
Constructor.
Definition: JString.hh:81
JString & assign(T &output)
Assign (part of) string to value.
Definition: JString.hh:359
JString & toUpper()
Convert all character to upper case.
Definition: JString.hh:272
JString(const JString &buffer, const JStringFacet &facet)
Constructor.
Definition: JString.hh:108
JString & trim(const char c)
Trim string.
Definition: JString.hh:244
alias put_queue eval echo n
Definition: qlib.csh:19
istreambuf_iterator get(const istreambuf_iterator __begin, const istreambuf_iterator __end, const std::ios_base &format, std::ios_base::iostate &result, std::string &buffer) const
Get string.
Definition: JStringFacet.hh:70
Facet class to specify parsing of a JLANG::JString object.
Definition: JStringFacet.hh:26
JString & replace(const char target, const char replacement, const std::size_t max=std::numeric_limits< std::size_t >::max())
Replace characters.
Definition: JString.hh:170
char * loc(char *orig)
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:40
JString & replace(const std::string &target, const T &value, const std::size_t max=std::numeric_limits< std::size_t >::max())
Replace character sequence.
Definition: JString.hh:211