g2o
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 <string>
31 #include <sstream>
32 #include <cstdlib>
33 #include <vector>
34 
35 #include "macros.h"
36 #include "g2o_stuff_api.h"
37 
38 namespace g2o {
39 
41 // @{
42 
50 G2O_STUFF_API std::string trim(const std::string& s);
51 
55 G2O_STUFF_API std::string trimLeft(const std::string& s);
56 
60 G2O_STUFF_API std::string trimRight(const std::string& s);
61 
65 G2O_STUFF_API std::string strToLower(const std::string& s);
66 
70 G2O_STUFF_API std::string strToUpper(const std::string& s);
71 
76 template <typename OutputIterator>
77 OutputIterator readInts(const char* str, OutputIterator out)
78 {
79  char* cl = (char*)str;
80  char* cle = cl;
81  while (1) {
82  long int id = strtol(cl, &cle, 10);
83  if (cl == cle)
84  break;
85  *out++ = id;
86  cl = cle;
87  }
88  return out;
89 }
90 
95 template <typename OutputIterator>
96 OutputIterator readFloats(const char* str, OutputIterator out)
97 {
98  char* cl = (char*)str;
99  char* cle = cl;
100  while (1) {
101  double val = strtod(cl, &cle);
102  if (cl == cle)
103  break;
104  *out++ = val;
105  cl = cle;
106  }
107  return out;
108 }
109 
114 G2O_STUFF_API std::string formatString(const char* fmt, ...) G2O_ATTRIBUTE_FORMAT12;
115 
119 G2O_STUFF_API int strPrintf(std::string& str, const char* fmt, ...) G2O_ATTRIBUTE_FORMAT23;
120 
124 template<typename T>
125 bool convertString(const std::string& s, T& x, bool failIfLeftoverChars = true)
126 {
127  std::istringstream i(s);
128  char c;
129  if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
130  return false;
131  return true;
132 }
133 
138 template<typename T>
139 T stringToType(const std::string& s, bool failIfLeftoverChars = true)
140 {
141  T x;
142  convertString(s, x, failIfLeftoverChars);
143  return x;
144 }
145 
149 G2O_STUFF_API bool strStartsWith(const std::string & str, const std::string& substr);
150 
154 G2O_STUFF_API bool strEndsWith(const std::string & str, const std::string& substr);
155 
160 G2O_STUFF_API std::string strExpandFilename(const std::string& filename);
161 
165 G2O_STUFF_API std::vector<std::string> strSplit(const std::string& s, const std::string& delim);
166 
171 G2O_STUFF_API int readLine(std::istream& is, std::stringstream& currentLine);
172 
173 // @}
174 
175 } // end namespace
176 
177 #endif
OutputIterator readFloats(const char *str, OutputIterator out)
Definition: string_tools.h:96
std::string strExpandFilename(const std::string &filename)
T stringToType(const std::string &s, bool failIfLeftoverChars=true)
Definition: string_tools.h:139
std::string trimLeft(const std::string &s)
#define G2O_ATTRIBUTE_FORMAT12
Definition: macros.h:93
std::string trimRight(const std::string &s)
std::string trim(const std::string &s)
std::vector< std::string > strSplit(const std::string &str, const std::string &delimiters)
int strPrintf(std::string &str, const char *fmt,...)
bool convertString(const std::string &s, T &x)
std::string formatString(const char *fmt,...)
bool strStartsWith(const std::string &s, const std::string &start)
int readLine(std::istream &is, std::stringstream &currentLine)
OutputIterator readInts(const char *str, OutputIterator out)
Definition: string_tools.h:77
std::string strToUpper(const std::string &s)
#define G2O_STUFF_API
Definition: g2o_stuff_api.h:30
#define G2O_ATTRIBUTE_FORMAT23
Definition: macros.h:94
std::string strToLower(const std::string &s)
bool strEndsWith(const std::string &s, const std::string &end)