stringutil.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
20 #include <algorithm>
21 #include <iostream>
22 #include <cstring>
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <limits.h>
26 #include <coil/stringutil.h>
27 #include <string.h>
28 #include <cctype>
29 #include <cstdio>
30 
31 #ifdef __QNX__
32 using std::toupper;
33 using std::isalpha;
34 #endif
35 
36 namespace coil
37 {
45  std::wstring string2wstring(std::string str)
46  {
47  std::wstring wstr(str.length(), L' ');
48  std::copy(str.begin(),str.end(),wstr.begin());
49  return wstr;
50  }
51 
59  std::string wstring2string(std::wstring wstr)
60  {
61  std::string str(wstr.length(), ' ');
62  std::copy(wstr.begin(), wstr.end(), str.begin());
63  return str;
64  }
65 
73  void toUpper(std::string& str)
74  {
75  std::transform(str.begin(), str.end(), str.begin(),
76  (int (*)(int))std::toupper);
77  }
78 
86  void toLower(std::string& str)
87  {
88  std::transform(str.begin(), str.end(), str.begin(),
89  (int (*)(int))std::tolower);
90  }
91 
99  int getlinePortable(std::istream& istr, std::string& line)
100  {
101  char c;
102  std::stringstream s;
103 
104  while (istr.get(c))
105  {
106  if (c == '\n')
107  {
108  break;
109  }
110  else if (c == '\r')
111  {
112  if (istr.peek() == '\n')
113  {
114  istr.ignore();
115  }
116  break;
117  }
118  else
119  {
120  s << c;
121  }
122  }
123  line = s.str();
124  return static_cast<int>(line.size());
125  }
126 
134  bool isEscaped(const std::string& str, std::string::size_type pos)
135  {
136  --pos;
137  unsigned int i;
138  for (i = 0; (pos >= 0) && str[pos] == '\\'; --pos, ++i) ;
139  // If the number of \ is odd, delimiter is escaped.
140  return (i % 2) == 1;
141  }
142 
151  {
153  void operator()(const char c)
154  {
155  if (c == '\t') str += "\\t";
156  else if (c == '\n') str += "\\n";
157  else if (c == '\f') str += "\\f";
158  else if (c == '\r') str += "\\r";
159  else if (c == '\\') str += "\\\\";
160  // else if (c == '\"') str += "\\\"";
161  // else if (c == '\'') str += "\\\'";
162  else str.push_back(c);
163  }
164  std::string str;
165  };
166 
174  std::string escape(const std::string str)
175  {
176  return for_each(str.begin(), str.end(), escape_functor()).str;
177  }
178 
187  {
188  unescape_functor() : count(0) {};
189  void operator()(char c)
190  {
191  if (c == '\\')
192  {
193  ++count;
194  if (!(count % 2))
195  {
196  str.push_back(c);
197  }
198  }
199  else
200  {
201  if (count > 0 && (count % 2))
202  {
203  count = 0;
204  if (c == 't') str.push_back('\t');
205  else if (c == 'n') str.push_back('\n');
206  else if (c == 'f') str.push_back('\f');
207  else if (c == 'r') str.push_back('\r');
208  else if (c == '\"') str.push_back('\"');
209  else if (c == '\'') str.push_back('\'');
210  else str.push_back(c);
211  }
212  else
213  {
214  count = 0;
215  str.push_back(c);
216  }
217  }
218  }
219  std::string str;
220  int count;
221  };
222 
230  std::string unescape(const std::string str)
231  {
232  return for_each(str.begin(), str.end(), unescape_functor()).str;
233  }
234 
242  void eraseBlank(std::string& str)
243  {
244  std::string::iterator it(str.begin());
245 
246  while (it != str.end())
247  {
248  if (*it == ' ' || *it == '\t')
249  {
250  it = str.erase(it);
251  }
252  else
253  {
254  ++it;
255  }
256  }
257 
258  }
259 
267  void eraseHeadBlank(std::string& str)
268  {
269  if (str.empty()) return;
270  while (str[0] == ' ' || str[0] == '\t') str.erase(0, 1);
271  }
272 
280  void eraseTailBlank(std::string& str)
281  {
282  if (str.empty()) return;
283  while ((str[str.size() - 1] == ' ' || str[str.size() - 1] == '\t') &&
284  !isEscaped(str, str.size() - 1))
285  str.erase(str.size() - 1, 1);
286  }
287 
295  void eraseBothEndsBlank(std::string& str)
296  {
297  eraseHeadBlank(str);
298  eraseTailBlank(str);
299  }
300 
308  std::string normalize(std::string& str)
309  {
310  eraseBothEndsBlank(str);
311  toLower(str);
312  return str;
313  }
314 
322  unsigned int replaceString(std::string& str, const std::string from,
323  const std::string to)
324  {
325  std::string::size_type pos(0);
326  unsigned int cnt(0);
327 
328  while (pos != std::string::npos)
329  {
330  pos = str.find(from, pos);
331  if (pos == std::string::npos) break;
332  str.replace(pos, from.size(), to);
333  pos += to.size();
334  ++cnt;
335  }
336  return cnt;
337  }
338 
346  vstring split(const std::string& input,
347  const std::string& delimiter,
348  bool ignore_empty)
349  {
350  typedef std::string::size_type size;
351  vstring results;
352  size delim_size = delimiter.size();
353  size found_pos(0), begin_pos(0), pre_pos(0), substr_size(0);
354 
355  if (input.empty()) return results;
356 
357  // if (input.substr(0, delim_size) == delimiter)
358  // begin_pos = pre_pos = delim_size;
359 
360  while (1)
361  {
362  // REFIND:
363  found_pos = input.find(delimiter, begin_pos);
364  if (found_pos == std::string::npos)
365  {
366  std::string substr(input.substr(pre_pos));
367  eraseHeadBlank(substr);
368  eraseTailBlank(substr);
369  if (!(substr.empty() && ignore_empty)) results.push_back(substr);
370  break;
371  }
372  /*
373  if (isEscaped(input, found_pos))
374  {
375  begin_pos = found_pos + delim_size;
376  goto REFIND;
377  }
378  */
379  substr_size = found_pos - pre_pos;
380  if (substr_size >= 0)
381  {
382  std::string substr(input.substr(pre_pos, substr_size));
383  eraseHeadBlank(substr);
384  eraseTailBlank(substr);
385  if (!(substr.empty() && ignore_empty)) results.push_back(substr);
386  }
387  begin_pos = found_pos + delim_size;
388  pre_pos = found_pos + delim_size;
389  }
390  return results;
391  }
392 
400  struct Toupper
401  {
402  void operator()(char &c)
403  {
404  c = toupper(c);
405  }
406  };
407 
415  bool toBool(std::string str, std::string yes, std::string no,
416  bool default_value)
417  {
418  std::for_each(str.begin(), str.end(), Toupper());
419  std::for_each(yes.begin(), yes.end(), Toupper());
420  std::for_each(no.begin(), no.end(), Toupper());
421 
422  if (str.find(yes) != std::string::npos)
423  return true;
424  else if (str.find(no) != std::string::npos)
425  return false;
426  else
427  return default_value;
428  }
429 
437  bool includes(const vstring& list, std::string value, bool ignore_case)
438  {
439  if (ignore_case) { toLower(value); }
440 
441  for (int i(0), len(static_cast<int>(list.size())); i < len; ++i)
442  {
443  std::string str(list[i]);
444  if (ignore_case) { toLower(str); }
445  if (str == value) return true;
446  }
447  return false;
448  }
449 
457  bool includes(const std::string& list, std::string value, bool ignore_case)
458  {
459  vstring vlist(split(list, ","));
460  return includes(vlist, value, ignore_case);
461  }
462 
470  bool isAbsolutePath(const std::string& str)
471  {
472  // UNIX absolute path is begun from '/'
473  if (str[0] == '/') return true;
474  // Windows absolute path is begun from '[a-zA-Z]:\'
475  if (isalpha(str[0]) && (str[1] == ':') && str[2] == '\\') return true;
476  // Windows network file path is begun from '\\'
477  if (str[0] == '\\' && str[1] == '\\') return true;
478 
479  return false;
480  }
481 
489  bool isURL(const std::string& str)
490  {
491  typedef std::string::size_type size;
492  size pos;
493  if (str.empty()) return false;
494  pos = str.find(":");
495  if ((pos != 0) &&
496  (pos != std::string::npos) &&
497  (str[pos + 1] == '/') &&
498  (str[pos + 2] == '/'))
499  return true;
500  return false;
501  }
502 
511  {
512  void operator()(const std::string& s)
513  {
514  if (std::find(str.begin(), str.end(), s) == str.end())
515  return str.push_back(s);
516  }
518  };
519 
527  template<>
528  bool stringTo<std::string>(std::string& val, const char* str)
529  {
530  if (str == 0) { return false; }
531  val = str;
532  return true;
533  }
534 
543  {
544  return std::for_each(sv.begin(), sv.end(), unique_strvec()).str;
545  }
546 
554  std::string flatten(vstring sv)
555  {
556  if (sv.size() == 0) return "";
557 
558  std::string str;
559  for (size_t i(0), len(sv.size() - 1); i < len; ++i)
560  {
561  str += sv[i] + ", ";
562  }
563  return str + sv.back();
564  }
565 
573  char** toArgv(const vstring& args)
574  {
575  char** argv;
576  size_t argc(args.size());
577 
578  argv = new char*[argc + 1];
579 
580  for (size_t i(0); i < argc; ++i)
581  {
582  size_t sz(args[i].size());
583  argv[i] = new char[sz + 1];
584  strncpy(argv[i], args[i].c_str(), sz);
585  argv[i][sz] = '\0';
586  }
587  argv[argc] = NULL;
588  return argv;
589  }
590 
598  std::string sprintf(char const * __restrict fmt, ...)
599  {
600 #ifndef LINE_MAX
601 #define LINE_MAX 1024
602 #endif
603  char str[LINE_MAX];
604  va_list ap;
605 
606  va_start(ap, fmt);
607 #ifdef WIN32
608  _vsnprintf(str, LINE_MAX - 1, fmt, ap);
609 #else
610  vsnprintf(str, LINE_MAX - 1, fmt, ap);
611 #endif
612  va_end(ap);
613  return std::string(str);
614  }
615 
616 }; // namespace coil
std::string normalize(std::string &str)
Erase the head/tail blank and replace upper case to lower case.
Definition: stringutil.cpp:308
void operator()(const std::string &s)
Definition: stringutil.cpp:512
void toUpper(std::string &str)
Uppercase String Transformation.
Definition: stringutil.cpp:73
void operator()(char c)
Definition: stringutil.cpp:189
void operator()(char &c)
Definition: stringutil.cpp:402
void eraseBlank(std::string &str)
Erase blank characters of string.
Definition: stringutil.cpp:242
void eraseHeadBlank(std::string &str)
Erase the head blank characters of string.
Definition: stringutil.cpp:267
vstring split(const std::string &input, const std::string &delimiter, bool ignore_empty)
Split string by delimiter.
Definition: stringutil.cpp:346
vstring unique_sv(vstring sv)
Eliminate duplication from the given string list.
Definition: stringutil.cpp:542
void operator()(const char c)
Definition: stringutil.cpp:153
void eraseBothEndsBlank(std::string &str)
Erase the head blank and the tail blank characters of string.
Definition: stringutil.cpp:295
bool isAbsolutePath(const std::string &str)
Investigate whether the given string is absolute path or not.
Definition: stringutil.cpp:470
std::vector< std::string > vstring
Definition: stringutil.h:37
void eraseTailBlank(std::string &str)
Erase the tail blank characters of string.
Definition: stringutil.cpp:280
std::string flatten(vstring sv)
Create CSV file from the given string list.
Definition: stringutil.cpp:554
CORBA::Long find(const CorbaSequence &seq, Functor f)
Return the index of CORBA sequence element that functor matches.
char ** toArgv(const vstring &args)
Convert the given string list into the argument list.
Definition: stringutil.cpp:573
bool isURL(const std::string &str)
Investigate whether the given string is URL or not.
Definition: stringutil.cpp:489
Functor to escape string.
Definition: stringutil.cpp:150
unsigned int replaceString(std::string &str, const std::string from, const std::string to)
Replace string.
Definition: stringutil.cpp:322
void toLower(std::string &str)
Lowercase String Transformation.
Definition: stringutil.cpp:86
bool toBool(std::string str, std::string yes, std::string no, bool default_value)
Convert given string into bool value.
Definition: stringutil.cpp:415
std::string wstring2string(std::wstring wstr)
wstring to string conversion
Definition: stringutil.cpp:59
int getlinePortable(std::istream &istr, std::string &line)
Read a line from input stream.
Definition: stringutil.cpp:99
std::string sprintf(char const *__restrict fmt,...)
Convert it into a format given with an argumen.
Definition: stringutil.cpp:598
std::wstring string2wstring(std::string str)
string to wstring conversion
Definition: stringutil.cpp:45
#define LINE_MAX
bool isEscaped(const std::string &str, std::string::size_type pos)
Check whether the character is escaped or not.
Definition: stringutil.cpp:134
Functor to unescape string.
Definition: stringutil.cpp:186
bool includes(const vstring &list, std::string value, bool ignore_case)
Include if a string is included in string list.
Definition: stringutil.cpp:437
std::string unescape(const std::string str)
Unescape string.
Definition: stringutil.cpp:230
Functor for_each(CorbaSequence &seq, Functor f)
Apply the functor to all CORBA sequence elements.
Definition: CORBA_SeqUtil.h:98
std::string escape(const std::string str)
Escape string.
Definition: stringutil.cpp:174
Functor to convert to capital letters.
Definition: stringutil.cpp:400
Common Object Interface Layer.
Functor to find string in a list.
Definition: stringutil.cpp:510


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Feb 28 2022 23:00:45