string_tools.cpp
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 #include "string_tools.h"
00018 #include "os_specific.h"
00019 #include "macros.h"
00020 
00021 #include <cctype>
00022 #include <string>
00023 #include <cstdarg>
00024 #include <cstring>
00025 #include <algorithm>
00026 #include <cstdio>
00027 #include <iostream>
00028 #include <iterator>
00029 
00030 #ifdef UNIX
00031 #include <wordexp.h>
00032 #endif
00033 
00034 namespace g2o {
00035 
00036 using namespace std;
00037 
00038 std::string trim(const std::string& s)
00039 {
00040   if(s.length() == 0)
00041     return s;
00042   string::size_type b = s.find_first_not_of(" \t\n");
00043   string::size_type e = s.find_last_not_of(" \t\n");
00044   if(b == string::npos)
00045     return "";
00046   return std::string(s, b, e - b + 1);
00047 }
00048 
00049 std::string trimLeft(const std::string& s)
00050 {
00051   if(s.length() == 0)
00052     return s;
00053   string::size_type b = s.find_first_not_of(" \t\n");
00054   string::size_type e = s.length() - 1;
00055   if(b == string::npos)
00056     return "";
00057   return std::string(s, b, e - b + 1);
00058 }
00059 
00060 std::string trimRight(const std::string& s)
00061 {
00062   if(s.length() == 0)
00063     return s;
00064   string::size_type b = 0;
00065   string::size_type e = s.find_last_not_of(" \t\n");
00066   if(b == string::npos)
00067     return "";
00068   return std::string(s, b, e - b + 1);
00069 }
00070 
00071 std::string strToLower(const std::string& s)
00072 {
00073   string ret;
00074   std::transform(s.begin(), s.end(), back_inserter(ret), (int(*)(int)) std::tolower);
00075   return ret;
00076 }
00077 
00078 std::string strToUpper(const std::string& s)
00079 {
00080   string ret;
00081   std::transform(s.begin(), s.end(), back_inserter(ret), (int(*)(int)) std::toupper);
00082   return ret;
00083 }
00084 
00085 std::string formatString(const char* fmt, ...)
00086 {
00087   char* auxPtr = NULL;
00088   va_list arg_list;
00089   va_start(arg_list, fmt);
00090   int numChar = vasprintf(&auxPtr, fmt, arg_list);
00091   va_end(arg_list);
00092   string retString;
00093   if (numChar != -1)
00094     retString = auxPtr;
00095   else
00096     throw RuntimeError("Error while allocating memory");
00097   free(auxPtr);
00098   return retString;
00099 }
00100 
00101 int strPrintf(std::string& str, const char* fmt, ...)
00102 {
00103   char* auxPtr = NULL;
00104   va_list arg_list;
00105   va_start(arg_list, fmt);
00106   int numChars = vasprintf(&auxPtr, fmt, arg_list);
00107   va_end(arg_list);
00108   str = auxPtr;
00109   free(auxPtr);
00110   return numChars;
00111 }
00112 
00113 std::string strExpandFilename(const std::string& filename)
00114 {
00115 
00116   #ifdef UNIX
00117   string result = filename;
00118   wordexp_t p;
00119 
00120   wordexp(filename.c_str(), &p, 0);
00121   if(p.we_wordc > 0) {
00122     result = p.we_wordv[0];
00123   }
00124   wordfree(&p);
00125   return result;
00126   #endif
00127 
00128   #ifdef WINDOWS
00129   (void) filename;
00130   std::cerr << "WARNING: " << __PRETTY_FUNCTION__ << " not implemented" << std::endl;
00131   return std::string();
00132   #endif
00133 
00134 }
00135 
00136 std::vector<std::string> strSplit(const std::string& str, const std::string& delimiters)
00137 {
00138   std::vector<std::string> tokens;
00139   string::size_type lastPos = 0;
00140   string::size_type pos     = 0;
00141 
00142   do {
00143     pos = str.find_first_of(delimiters, lastPos);
00144     tokens.push_back(str.substr(lastPos, pos - lastPos));
00145     lastPos = pos + 1;
00146   }  while (string::npos != pos);
00147 
00148   return tokens;
00149 }
00150 
00151 bool strStartsWith(const std::string& s, const std::string& start)
00152 {
00153   if (s.size() < start.size())
00154     return false;
00155   return equal(start.begin(), start.end(), s.begin());
00156 }
00157 
00158 bool strEndsWith(const std::string& s, const std::string& end)
00159 {
00160   if (s.size() < end.size())
00161     return false;
00162   return equal(end.rbegin(), end.rend(), s.rbegin());
00163 }
00164 
00165 int readLine(std::istream& is, std::stringstream& currentLine)
00166 {
00167   if (is.eof())
00168     return -1;
00169   currentLine.str("");
00170   currentLine.clear();
00171   is.get(*currentLine.rdbuf());
00172   if (is.fail()) // fail is set on empty lines
00173     is.clear();
00174   G2O_FSKIP_LINE(is); // read \n not read by get()
00175   return currentLine.str().size();
00176 }
00177 
00178 } // end namespace


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