OpenRTM-aist  1.2.1
stringutil.h
Go to the documentation of this file.
1 // -*- C++ -*-
20 #ifndef COIL_STRINGUTIL_H
21 #define COIL_STRINGUTIL_H
22 
23 #include <string>
24 #include <vector>
25 #include <map>
26 #include <sstream>
27 #include <cassert>
28 #include <errno.h>
29 #include <cstdlib>
30 
31 #if defined (_MSC_VER) && (_MSC_VER <=1500) // VC2008(VC9.0) or before
32 #else
33 #include <stdint.h>
34 #endif
35 // Cygwin's gcc does not provide wstring type
36 #if defined(Cygwin) && ( __GNUC__ < 4 )
37 namespace std
38 {
39  typedef basic_string<wchar_t> wstring;
40 }
41 #endif
42 
43 namespace coil
44 {
45  typedef std::vector<std::string> vstring;
46  typedef std::map<std::string, std::string> mapstring;
66  std::wstring string2wstring(std::string str);
67 
87  std::string wstring2string(std::wstring wstr);
88 
106  void toUpper(std::string& str);
107 
125  void toLower(std::string& str);
126 
153  int getlinePortable(std::istream& istr, std::string& line);
154 
178  bool isEscaped(const std::string& str, std::string::size_type pos);
179 
212  std::string escape(const std::string str);
213 
250  std::string unescape(const std::string str);
251 
271  void eraseBlank(std::string& str);
272 
292  void eraseHeadBlank(std::string& str);
293 
314  void eraseTailBlank(std::string& str);
315 
336  void eraseBothEndsBlank(std::string& str);
337 
359  std::string normalize(std::string& str);
360 
382  unsigned int replaceString(std::string& str, const std::string from,
383  const std::string to);
384 
408  vstring split(const std::string& input,
409  const std::string& delimiter,
410  bool ignore_empty = false);
411 
440  bool toBool(std::string str, std::string yes, std::string no,
441  bool default_value = true);
465  bool includes(const vstring& list, std::string value,
466  bool ignore_case = true);
467 
491  bool includes(const std::string& list, std::string value,
492  bool ignore_case = true);
493 
523  bool isAbsolutePath(const std::string& str);
524 
550  bool isURL(const std::string& str);
551  bool isIPv4(const std::string& str);
552  bool isIPv6(const std::string& str);
578  coil::mapstring urlparam2map(const std::string& str);
579 
601  template <class Printable>
602  std::string otos(Printable n)
603  {
604  std::stringstream str_stream;
605  str_stream << n;
606  return str_stream.str();
607  };
608 
632  template <typename To>
633  bool stringTo(To& val, const char* str)
634  {
635  if (str == 0) { return false; }
636 
637  std::stringstream s;
638  if ((s << str).fail()) { return false; }
639  if ((s >> val).fail()) { return false; }
640  return true;
641  }
642 
666  template<>
667  bool stringTo<std::string>(std::string& val, const char* str);
668 
691  template <>
692  bool stringTo<bool>(bool& val, const char* str);
693 
717  template<class T>
718  std::string ptrToHex(T* n)
719  {
720  std::stringstream ss;
721  ss << std::hex << std::showbase;
722  ss << reinterpret_cast<uintptr_t>(n);
723  return ss.str();
724  };
725 
750  template <class T>
751  bool hexToPtr(T*& ptr, const std::string str)
752  {
753  std::stringstream s;
754  if ((s << std::hex << str).fail()) { return false; }
755  uintptr_t intval;
756  if ((s >> intval).fail()) { return false; }
757  ptr = reinterpret_cast<T*>(intval);
758  if (ptr == NULL) { return false; }
759  return true;
760  }
761 
784  vstring unique_sv(vstring sv);
785 
811  std::string flatten(vstring sv, std::string delimiter = ", ");
812 
836  char** toArgv(const vstring& args);
837 
838 
860  std::string sprintf(char const * __restrict fmt, ...);
861 
884  std::string replaceEnv(std::string str);
885 
886 }; // namepsace coil
887 #endif // COIL_STRINGUTIL_H
std::string normalize(std::string &str)
Erase the head/tail blank and replace upper case to lower case.
void toUpper(std::string &str)
Uppercase String Transformation.
std::string otos(Printable n)
Convert the given object to std::string.
Definition: stringutil.h:602
bool stringTo(To &val, const char *str)
Convert the given std::string to object.
Definition: stringutil.h:633
void eraseBlank(std::string &str)
Erase blank characters of string.
void eraseHeadBlank(std::string &str)
Erase the head blank characters of string.
std::string flatten(vstring sv, std::string delimiter=", ")
Create CSV file from the given string list.
bool isIPv4(const std::string &str)
vstring unique_sv(vstring sv)
Eliminate duplication from the given string list.
void eraseBothEndsBlank(std::string &str)
Erase the head blank and the tail blank characters of string.
bool isAbsolutePath(const std::string &str)
Investigate whether the given string is absolute path or not.
std::vector< std::string > vstring
Definition: stringutil.h:45
char ** toArgv(const vstring &args)
Convert the given string list into the argument list.
void eraseTailBlank(std::string &str)
Erase the tail blank characters of string.
bool toBool(std::string str, std::string yes, std::string no, bool default_value=true)
Convert given string into bool value.
bool isURL(const std::string &str)
Investigate whether the given string is URL or not.
unsigned int replaceString(std::string &str, const std::string from, const std::string to)
Replace string.
bool stringTo< bool >(bool &val, const char *str)
Eliminate duplication from the given string list.
std::map< std::string, std::string > mapstring
Definition: stringutil.h:46
std::string ptrToHex(T *n)
Converting a pointer to hexadecimal string.
Definition: stringutil.h:718
bool includes(const std::string &list, std::string value, bool ignore_case=true)
Include if a string is included in string list.
void toLower(std::string &str)
Lowercase String Transformation.
coil::mapstring urlparam2map(const std::string &str)
Investigate whether the given string is URL or not.
std::string wstring2string(std::wstring wstr)
wstring to string conversion
int getlinePortable(std::istream &istr, std::string &line)
Read a line from input stream.
std::string sprintf(char const *__restrict fmt,...)
Convert it into a format given with an argumen.
std::wstring string2wstring(std::string str)
string to wstring conversion
bool isEscaped(const std::string &str, std::string::size_type pos)
Check whether the character is escaped or not.
bool hexToPtr(T *&ptr, const std::string str)
Converting hexadecimal string to a pointer.
Definition: stringutil.h:751
std::string unescape(const std::string str)
Unescape string.
std::string replaceEnv(std::string str)
std::string escape(const std::string str)
Escape string.
bool isIPv6(const std::string &str)
vstring split(const std::string &input, const std::string &delimiter, bool ignore_empty=false)
Split string by delimiter.
Common Object Interface Layer.
Definition: Affinity.h:28