g2o
Toggle main menu visibility
Loading...
Searching...
No Matches
g2o
stuff
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
29
#include <algorithm>
30
#include <cctype>
31
#include <cstdarg>
32
#include <cstdio>
33
#include <cstring>
34
#include <iostream>
35
#include <iterator>
36
#include <string>
37
38
#include "
logger.h
"
39
#include "
macros.h
"
40
#include "
os_specific.h
"
41
42
#if (defined(UNIX) || defined(CYGWIN)) && !defined(ANDROID)
43
#include <wordexp.h>
44
#endif
45
46
namespace
g2o
{
47
48
using namespace
std;
49
50
std::string
trim
(
const
std::string& s) {
51
if
(s.length() == 0)
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)
return
""
;
55
return
std::string(s, b, e - b + 1);
56
}
57
58
std::string
trimLeft
(
const
std::string& s) {
59
if
(s.length() == 0)
return
s;
60
string::size_type b = s.find_first_not_of(
" \t\n"
);
61
string::size_type e = s.length() - 1;
62
if
(b == string::npos)
return
""
;
63
return
std::string(s, b, e - b + 1);
64
}
65
66
std::string
trimRight
(
const
std::string& s) {
67
if
(s.length() == 0)
return
s;
68
string::size_type b = 0;
69
string::size_type e = s.find_last_not_of(
" \t\n"
);
70
if
(e == string::npos)
return
""
;
71
return
std::string(s, b, e - b + 1);
72
}
73
74
std::string
strToLower
(
const
std::string& s) {
75
string
ret;
76
ret.reserve(s.size());
77
std::transform(s.begin(), s.end(), back_inserter(ret),
78
[](
unsigned
char
c) {
return
std::tolower(c); });
79
return
ret;
80
}
81
82
std::string
strToUpper
(
const
std::string& s) {
83
string
ret;
84
ret.reserve(s.size());
85
std::transform(s.begin(), s.end(), back_inserter(ret),
86
[](
unsigned
char
c) {
return
std::toupper(c); });
87
return
ret;
88
}
89
90
std::string
formatString
(
const
char
* fmt, ...) {
91
char
* auxPtr = NULL;
92
va_list arg_list;
93
va_start(arg_list, fmt);
94
int
numChar = vasprintf(&auxPtr, fmt, arg_list);
95
va_end(arg_list);
96
string
retString;
97
if
(numChar != -1)
98
retString = auxPtr;
99
else
{
100
G2O_ERROR
(
"{}: Error while allocating memory"
,
__PRETTY_FUNCTION__
);
101
}
102
free(auxPtr);
103
return
retString;
104
}
105
106
int
strPrintf
(std::string& str,
const
char
* fmt, ...) {
107
char
* auxPtr = NULL;
108
va_list arg_list;
109
va_start(arg_list, fmt);
110
int
numChars = vasprintf(&auxPtr, fmt, arg_list);
111
va_end(arg_list);
112
str = auxPtr;
113
free(auxPtr);
114
return
numChars;
115
}
116
117
std::string
strExpandFilename
(
const
std::string& filename) {
118
#if (defined(UNIX) || defined(CYGWIN)) && !defined(ANDROID)
119
string
result = filename;
120
wordexp_t p;
121
122
wordexp(filename.c_str(), &p, 0);
123
if
(p.we_wordc > 0) {
124
result = p.we_wordv[0];
125
}
126
wordfree(&p);
127
return
result;
128
#else
129
(void)filename;
130
G2O_WARN
(
"{} not implemented"
,
__PRETTY_FUNCTION__
);
131
return
std::string();
132
#endif
133
}
134
135
std::vector<std::string>
strSplit
(
const
std::string& str,
136
const
std::string& delimiters) {
137
std::vector<std::string> tokens;
138
if
(str.empty())
return
tokens;
139
string::size_type lastPos = 0;
140
string::size_type pos = 0;
141
142
do
{
143
pos = str.find_first_of(delimiters, lastPos);
144
tokens.push_back(str.substr(lastPos, pos - lastPos));
145
lastPos = pos + 1;
146
}
while
(string::npos != pos);
147
148
return
tokens;
149
}
150
151
bool
strStartsWith
(
const
std::string& s,
const
std::string& start) {
152
if
(s.size() < start.size())
return
false
;
153
return
equal(start.begin(), start.end(), s.begin());
154
}
155
156
bool
strEndsWith
(
const
std::string& s,
const
std::string& end) {
157
if
(s.size() < end.size())
return
false
;
158
return
equal(end.rbegin(), end.rend(), s.rbegin());
159
}
160
161
int
readLine
(std::istream& is, std::stringstream& currentLine) {
162
if
(is.eof())
return
-1;
163
currentLine.str(
""
);
164
currentLine.clear();
165
is.get(*currentLine.rdbuf());
166
if
(is.fail())
// fail is set on empty lines
167
is.clear();
168
skipLine
(is);
// read \n not read by get()
169
if
(currentLine.str().empty() && is.eof()) {
170
return
-1;
171
}
172
return
static_cast<
int
>
(currentLine.str().size());
173
}
174
175
void
skipLine
(std::istream& is) {
176
char
c =
' '
;
177
while
(c !=
'\n'
&& is.good() && !is.eof()) is.get(c);
178
}
179
180
}
// namespace g2o
G2O_ERROR
#define G2O_ERROR(...)
G2O_WARN
#define G2O_WARN(...)
logger.h
macros.h
__PRETTY_FUNCTION__
#define __PRETTY_FUNCTION__
Definition
macros.h:90
g2o
Definition
dl_wrapper.cpp:55
g2o::readLine
int readLine(std::istream &is, std::stringstream ¤tLine)
Definition
string_tools.cpp:161
g2o::strExpandFilename
std::string strExpandFilename(const std::string &filename)
Definition
string_tools.cpp:117
g2o::trimLeft
std::string trimLeft(const std::string &s)
Definition
string_tools.cpp:58
g2o::trimRight
std::string trimRight(const std::string &s)
Definition
string_tools.cpp:66
g2o::strEndsWith
bool strEndsWith(const std::string &s, const std::string &end)
Definition
string_tools.cpp:156
g2o::trim
std::string trim(const std::string &s)
Definition
string_tools.cpp:50
g2o::strSplit
std::vector< std::string > strSplit(const std::string &str, const std::string &delimiters)
Definition
string_tools.cpp:135
g2o::formatString
std::string formatString(const char *fmt,...)
Definition
string_tools.cpp:90
g2o::strToLower
std::string strToLower(const std::string &s)
Definition
string_tools.cpp:74
g2o::strToUpper
std::string strToUpper(const std::string &s)
Definition
string_tools.cpp:82
g2o::strStartsWith
bool strStartsWith(const std::string &s, const std::string &start)
Definition
string_tools.cpp:151
g2o::strPrintf
int strPrintf(std::string &str, const char *fmt,...)
Definition
string_tools.cpp:106
g2o::skipLine
void skipLine(std::istream &is)
Definition
string_tools.cpp:175
os_specific.h
string_tools.h
Generated on
for g2o by
1.17.0