Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef STRING_TOOLS_H
00018 #define STRING_TOOLS_H
00019
00020 #include <string>
00021 #include <sstream>
00022 #include <cstdlib>
00023 #include <vector>
00024 #include "runtime_error.h"
00025
00026 namespace g2o {
00027
00029
00030
00038 std::string trim(const std::string& s);
00039
00043 std::string trimLeft(const std::string& s);
00044
00048 std::string trimRight(const std::string& s);
00049
00053 std::string strToLower(const std::string& s);
00054
00058 std::string strToUpper(const std::string& s);
00059
00064 template <typename OutputIterator>
00065 OutputIterator readInts(const char* str, OutputIterator out)
00066 {
00067 char* cl = (char*)str;
00068 char* cle = cl;
00069 while (1) {
00070 int id = strtol(cl, &cle, 10);
00071 if (cl == cle)
00072 break;
00073 *out++ = id;
00074 cl = cle;
00075 }
00076 return out;
00077 }
00078
00083 template <typename OutputIterator>
00084 OutputIterator readFloats(const char* str, OutputIterator out)
00085 {
00086 char* cl = (char*)str;
00087 char* cle = cl;
00088 while (1) {
00089 double val = strtod(cl, &cle);
00090 if (cl == cle)
00091 break;
00092 *out++ = val;
00093 cl = cle;
00094 }
00095 return out;
00096 }
00097
00102 std::string formatString(const char* fmt, ...) G2O_ATTRIBUTE_FORMAT12;
00103
00107 int strPrintf(std::string& str, const char* fmt, ...) G2O_ATTRIBUTE_FORMAT23;
00108
00112 template<typename T>
00113 void convertString(const std::string& s, T& x, bool failIfLeftoverChars = true)
00114 {
00115 std::istringstream i(s);
00116 char c;
00117 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
00118 throw RuntimeError("%s: Unable to convert %s", __PRETTY_FUNCTION__, s.c_str());
00119 }
00120
00125 template<typename T>
00126 T stringToType(const std::string& s, bool failIfLeftoverChars = true)
00127 {
00128 T x;
00129 convertString(s, x, failIfLeftoverChars);
00130 return x;
00131 }
00132
00136 bool strStartsWith(const std::string & str, const std::string& substr);
00137
00141 bool strEndsWith(const std::string & str, const std::string& substr);
00142
00147 std::string strExpandFilename(const std::string& filename);
00148
00152 std::vector<std::string> strSplit(const std::string& s, const std::string& delim);
00153
00158 int readLine(std::istream& is, std::stringstream& currentLine);
00159
00160
00161
00162 }
00163
00164 #endif