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


hogman_minimal
Author(s): Maintained by Juergen Sturm
autogenerated on Wed Dec 26 2012 15:36:49