string_tools.cpp
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #include "string_tools.h"
28 #include "os_specific.h"
29 #include "macros.h"
30 
31 #include <cctype>
32 #include <string>
33 #include <cstdarg>
34 #include <cstring>
35 #include <algorithm>
36 #include <cstdio>
37 #include <iostream>
38 #include <iterator>
39 
40 #if (defined (UNIX) || defined(CYGWIN)) && !defined(ANDROID)
41 #include <wordexp.h>
42 #endif
43 
44 namespace g2o {
45 
46 using namespace std;
47 
48 std::string trim(const std::string& s)
49 {
50  if(s.length() == 0)
51  return s;
52  string::size_type b = s.find_first_not_of(" \t\n");
53  string::size_type e = s.find_last_not_of(" \t\n");
54  if(b == string::npos)
55  return "";
56  return std::string(s, b, e - b + 1);
57 }
58 
59 std::string trimLeft(const std::string& s)
60 {
61  if(s.length() == 0)
62  return s;
63  string::size_type b = s.find_first_not_of(" \t\n");
64  string::size_type e = s.length() - 1;
65  if(b == string::npos)
66  return "";
67  return std::string(s, b, e - b + 1);
68 }
69 
70 std::string trimRight(const std::string& s)
71 {
72  if(s.length() == 0)
73  return s;
74  string::size_type b = 0;
75  string::size_type e = s.find_last_not_of(" \t\n");
76  if(b == string::npos)
77  return "";
78  return std::string(s, b, e - b + 1);
79 }
80 
81 std::string strToLower(const std::string& s)
82 {
83  string ret;
84  std::transform(s.begin(), s.end(), back_inserter(ret), (int(*)(int)) std::tolower);
85  return ret;
86 }
87 
88 std::string strToUpper(const std::string& s)
89 {
90  string ret;
91  std::transform(s.begin(), s.end(), back_inserter(ret), (int(*)(int)) std::toupper);
92  return ret;
93 }
94 
95 std::string formatString(const char* fmt, ...)
96 {
97  char* auxPtr = NULL;
98  va_list arg_list;
99  va_start(arg_list, fmt);
100  int numChar = vasprintf(&auxPtr, fmt, arg_list);
101  va_end(arg_list);
102  string retString;
103  if (numChar != -1)
104  retString = auxPtr;
105  else {
106  cerr << __PRETTY_FUNCTION__ << ": Error while allocating memory" << endl;
107  }
108  free(auxPtr);
109  return retString;
110 }
111 
112 int strPrintf(std::string& str, const char* fmt, ...)
113 {
114  char* auxPtr = NULL;
115  va_list arg_list;
116  va_start(arg_list, fmt);
117  int numChars = vasprintf(&auxPtr, fmt, arg_list);
118  va_end(arg_list);
119  str = auxPtr;
120  free(auxPtr);
121  return numChars;
122 }
123 
124 std::string strExpandFilename(const std::string& filename)
125 {
126 #if (defined (UNIX) || defined(CYGWIN)) && !defined(ANDROID)
127  string result = filename;
128  wordexp_t p;
129 
130  wordexp(filename.c_str(), &p, 0);
131  if(p.we_wordc > 0) {
132  result = p.we_wordv[0];
133  }
134  wordfree(&p);
135  return result;
136 #else
137  (void) filename;
138  std::cerr << "WARNING: " << __PRETTY_FUNCTION__ << " not implemented" << std::endl;
139  return std::string();
140 #endif
141 }
142 
143 std::vector<std::string> strSplit(const std::string& str, const std::string& delimiters)
144 {
145  std::vector<std::string> tokens;
146  string::size_type lastPos = 0;
147  string::size_type pos = 0;
148 
149  do {
150  pos = str.find_first_of(delimiters, lastPos);
151  tokens.push_back(str.substr(lastPos, pos - lastPos));
152  lastPos = pos + 1;
153  } while (string::npos != pos);
154 
155  return tokens;
156 }
157 
158 bool strStartsWith(const std::string& s, const std::string& start)
159 {
160  if (s.size() < start.size())
161  return false;
162  return equal(start.begin(), start.end(), s.begin());
163 }
164 
165 bool strEndsWith(const std::string& s, const std::string& end)
166 {
167  if (s.size() < end.size())
168  return false;
169  return equal(end.rbegin(), end.rend(), s.rbegin());
170 }
171 
172 int readLine(std::istream& is, std::stringstream& currentLine)
173 {
174  if (is.eof())
175  return -1;
176  currentLine.str("");
177  currentLine.clear();
178  is.get(*currentLine.rdbuf());
179  if (is.fail()) // fail is set on empty lines
180  is.clear();
181  G2O_FSKIP_LINE(is); // read \n not read by get()
182  return static_cast<int>(currentLine.str().size());
183 }
184 
185 } // end namespace
#define __PRETTY_FUNCTION__
Definition: macros.h:95
int readLine(std::istream &is, std::stringstream &currentLine)
std::vector< std::string > strSplit(const std::string &str, const std::string &delimiters)
std::string trim(const std::string &s)
std::string trimLeft(const std::string &s)
bool strStartsWith(const std::string &s, const std::string &start)
std::string strExpandFilename(const std::string &filename)
std::string trimRight(const std::string &s)
std::string strToLower(const std::string &s)
int strPrintf(std::string &str, const char *fmt,...)
std::string strToUpper(const std::string &s)
bool strEndsWith(const std::string &s, const std::string &end)
std::string formatString(const char *fmt,...)


orb_slam2_ros
Author(s):
autogenerated on Wed Apr 21 2021 02:53:05