Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifdef _MSC_VER
00020 # pragma warning(disable: 4244)
00021 #endif
00022
00023
00024 #include "rtc/rtcStringTools.h"
00025 #include <stdio.h>
00026 #include <string.h>
00027 #include <sstream>
00028 #include <iostream>
00029 #include <boost/lexical_cast.hpp>
00030
00031
00032 namespace rtc {
00033
00034 bool lexical_cast_bool(const std::string& str, const bool bDefault)
00035 {
00036 if (strcmp(str.c_str(),"true")==0 ||
00037 strcmp(str.c_str(),"True")==0 ||
00038 strcmp(str.c_str(),"TRUE")==0 ||
00039 strcmp(str.c_str(),"1")==0 ||
00040 strcmp(str.c_str(),"-1")==0)
00041 return true;
00042 else if (
00043 strcmp(str.c_str(),"false")==0 ||
00044 strcmp(str.c_str(),"False")==0 ||
00045 strcmp(str.c_str(),"FALSE")==0 ||
00046 strcmp(str.c_str(),"0")==0)
00047 return false;
00048 else
00049 return bDefault;
00050 }
00051
00052 int lexical_cast_int(const std::string& str, const int nDefault)
00053 {
00054 int value;
00055 int res;
00056 if(str.length() == 0)
00057 return nDefault;
00058 res=sscanf(str.c_str(),"%i",&value);
00059 if (res==1)
00060 return value;
00061 return 0;
00062 }
00063
00064 float lexical_cast_float(const std::string& str, const float fDefault)
00065 {
00066 double value;
00067 try { value = boost::lexical_cast<float>(str); }
00068 catch(boost::bad_lexical_cast &) { value = fDefault; }
00069 return value;
00070 }
00071
00072 double lexical_cast_double(const std::string& str, const double dDefault )
00073 {
00074 double value;
00075 try { value = boost::lexical_cast<double>(str); }
00076 catch(boost::bad_lexical_cast &) { value = dDefault; }
00077 return value;
00078 }
00079
00080 long lexical_cas_long(const std::string& str, const long lDefault )
00081 {
00082 int value;
00083 int res;
00084 if(str.length() == 0)
00085 return lDefault;
00086 res=sscanf(str.c_str(),"%i",&value);
00087 if (res!=1)
00088 return lDefault;
00089 return (long)value;
00090 }
00091
00092 std::string lexical_cast_string(const double& var)
00093 {
00094 std::stringstream ss;
00095 ss << var;
00096 return ss.str();
00097 }
00098
00099 std::string lexical_cast_string(const float& var)
00100 {
00101 std::stringstream ss;
00102 ss << var;
00103 return ss.str();
00104 }
00105
00106 std::string lexical_cast_string(const bool& var)
00107 {
00108 if(var)
00109 return std::string("true");
00110 else
00111 return std::string("false");
00112 }
00113
00114 std::string lexical_cast_string(const int& var){
00115 std::stringstream ss;
00116 ss << var;
00117 return ss.str();
00118 }
00119
00120
00121
00122 void split_string(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters)
00123 {
00124
00125 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00126
00127 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
00128
00129 while (std::string::npos != pos || std::string::npos != lastPos)
00130 {
00131
00132 tokens.push_back(str.substr(lastPos, pos - lastPos));
00133
00134 lastPos = str.find_first_not_of(delimiters, pos);
00135
00136 pos = str.find_first_of(delimiters, lastPos);
00137 }
00138 }
00139
00140
00141
00142 std::string& trim(std::string& str)
00143 {
00144
00145 size_t startpos = str.find_first_not_of(" \t");
00146 size_t endpos = str.find_last_not_of(" \t");
00147
00148 if(( std::string::npos == startpos ) || ( std::string::npos == endpos))
00149 str = "";
00150 else
00151 str = str.substr(startpos, endpos-startpos+1);
00152 return str;
00153 }
00154
00155
00156 }
00157