g2o
Loading...
Searching...
No Matches
string_tools.h
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#ifndef G2O_STRING_TOOLS_H
28#define G2O_STRING_TOOLS_H
29
30#include <cstdlib>
31#include <sstream>
32#include <string>
33#include <vector>
34
35#include "g2o_stuff_api.h"
36#include "macros.h"
37
38namespace g2o {
39
41// @{
42
50G2O_STUFF_API std::string trim(const std::string& s);
51
55G2O_STUFF_API std::string trimLeft(const std::string& s);
56
60G2O_STUFF_API std::string trimRight(const std::string& s);
61
65G2O_STUFF_API std::string strToLower(const std::string& s);
66
70G2O_STUFF_API std::string strToUpper(const std::string& s);
71
76template <typename OutputIterator>
77OutputIterator readInts(const char* str, OutputIterator out) {
78 char* cl = (char*)str;
79 char* cle = cl;
80 while (1) {
81 long int id = strtol(cl, &cle, 10);
82 if (cl == cle) break;
83 *out++ = id;
84 cl = cle;
85 }
86 return out;
87}
88
93template <typename OutputIterator>
94OutputIterator readFloats(const char* str, OutputIterator out) {
95 char* cl = (char*)str;
96 char* cle = cl;
97 while (1) {
98 double val = strtod(cl, &cle);
99 if (cl == cle) break;
100 *out++ = val;
101 cl = cle;
102 }
103 return out;
104}
105
110G2O_STUFF_API std::string formatString(const char* fmt,
112
116G2O_STUFF_API int strPrintf(std::string& str, const char* fmt,
118
122template <typename T>
123bool convertString(const std::string& s, T& x,
124 bool failIfLeftoverChars = true) {
125 std::istringstream i(s);
126 char c;
127 if (!(i >> x) || (failIfLeftoverChars && i.get(c))) return false;
128 return true;
129}
130
135template <typename T>
136T stringToType(const std::string& s, bool failIfLeftoverChars = true) {
137 T x;
138 convertString(s, x, failIfLeftoverChars);
139 return x;
140}
141
145G2O_STUFF_API bool strStartsWith(const std::string& str,
146 const std::string& substr);
147
151G2O_STUFF_API bool strEndsWith(const std::string& str,
152 const std::string& substr);
153
159G2O_STUFF_API std::string strExpandFilename(const std::string& filename);
160
164G2O_STUFF_API std::vector<std::string> strSplit(const std::string& s,
165 const std::string& delim);
166
172G2O_STUFF_API int readLine(std::istream& is, std::stringstream& currentLine);
173
177G2O_STUFF_API void skipLine(std::istream& is);
178
179// @}
180
181} // namespace g2o
182
183#endif
#define G2O_STUFF_API
#define G2O_ATTRIBUTE_FORMAT12
Definition macros.h:94
#define G2O_ATTRIBUTE_FORMAT23
Definition macros.h:95
void skipLine(std::istream &is)
std::string strToLower(const std::string &s)
int strPrintf(std::string &str, const char *fmt,...)
std::string strExpandFilename(const std::string &filename)
bool convertString(const std::string &s, T &x, bool failIfLeftoverChars=true)
std::string trimRight(const std::string &s)
std::string trim(const std::string &s)
std::string trimLeft(const std::string &s)
int readLine(std::istream &is, std::stringstream &currentLine)
OutputIterator readFloats(const char *str, OutputIterator out)
bool strEndsWith(const std::string &s, const std::string &end)
std::vector< std::string > strSplit(const std::string &str, const std::string &delimiters)
T stringToType(const std::string &s, bool failIfLeftoverChars=true)
bool strStartsWith(const std::string &s, const std::string &start)
std::string formatString(const char *fmt,...)
OutputIterator readInts(const char *str, OutputIterator out)
std::string strToUpper(const std::string &s)
Definition jet.h:876
std::ostream * out
Definition scanner.l:138