string_tools.h
Go to the documentation of this file.
00001 // g2o - General Graph Optimization
00002 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
00003 // 
00004 // g2o 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 // g2o 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 
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 } // end namespace
00163 
00164 #endif


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:33:00