$search
00001 // HOG-Man - Hierarchical Optimization for Pose Graphs on Manifolds 00002 // Copyright (C) 2010 G. Grisetti, R. Kümmerle, C. Stachniss 00003 // 00004 // HOG-Man is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU Lesser General Public License as published 00006 // by the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // HOG-Man is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU Lesser General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU Lesser General Public License 00015 // along with this program. If not, see <http://www.gnu.org/licenses/>. 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 00027 // @{ 00028 00036 std::string trim(const std::string& s); 00037 00041 std::string trimLeft(const std::string& s); 00042 00046 std::string trimRight(const std::string& s); 00047 00051 std::string strToLower(const std::string& s); 00052 00056 std::string strToUpper(const std::string& s); 00057 00062 template <typename OutputIterator> 00063 OutputIterator readInts(const char* str, OutputIterator out) 00064 { 00065 char* cl = (char*)str; 00066 char* cle = cl; 00067 while (1) { 00068 int id = strtol(cl, &cle, 10); 00069 if (cl == cle) 00070 break; 00071 *out++ = id; 00072 cl = cle; 00073 } 00074 return out; 00075 } 00076 00081 template <typename OutputIterator> 00082 OutputIterator readFloats(const char* str, OutputIterator out) 00083 { 00084 char* cl = (char*)str; 00085 char* cle = cl; 00086 while (1) { 00087 double val = strtod(cl, &cle); 00088 if (cl == cle) 00089 break; 00090 *out++ = val; 00091 cl = cle; 00092 } 00093 return out; 00094 } 00095 00100 std::string formatString(const char* fmt, ...) __attribute__ ((format (printf, 1, 2))); 00101 00105 int strPrintf(std::string& str, const char* fmt, ...) __attribute__ ((format (printf, 2, 3))); 00106 00110 template<typename T> 00111 void convertString(const std::string& s, T& x, bool failIfLeftoverChars = true) 00112 { 00113 std::istringstream i(s); 00114 char c; 00115 if (!(i >> x) || (failIfLeftoverChars && i.get(c))) 00116 throw RuntimeError("%s: Unable to convert %s", __PRETTY_FUNCTION__, s.c_str()); 00117 } 00118 00123 template<typename T> 00124 T stringToType(const std::string& s, bool failIfLeftoverChars = true) 00125 { 00126 T x; 00127 convertString(s, x, failIfLeftoverChars); 00128 return x; 00129 } 00130 00134 bool strStartsWith(const std::string & str, const std::string& substr); 00135 00139 bool strEndsWith(const std::string & str, const std::string& substr); 00140 00145 std::string strExpandFilename(const std::string& filename); 00146 00150 std::vector<std::string> strSplit(const std::string& s, const std::string& delim); 00151 00152 // @} 00153 00154 #endif 00155