g2o
Files | Namespaces | Macros | Functions
Utils
Collaboration diagram for Utils:

Files

file  misc.h
 some general case utility functions
 
file  timeutil.h
 utility functions for handling time related stuff
 

Namespaces

 g2o
 

Macros

#define DO_EVERY_TS(secs, currentTime, code)
 
#define DO_EVERY(secs, code)   DO_EVERY_TS(secs, g2o::get_time(), code)
 Executes code, only if secs are gone since last exec. More...
 
#define MEASURE_TIME(text, code)
 
#define MEASURE_FUNCTION_TIME   g2o::ScopeTime scopeTime(__PRETTY_FUNCTION__)
 

Functions

std::string g2o::trim (const std::string &s)
 
std::string g2o::trimLeft (const std::string &s)
 
std::string g2o::trimRight (const std::string &s)
 
std::string g2o::strToLower (const std::string &s)
 
std::string g2o::strToUpper (const std::string &s)
 
template<typename OutputIterator >
OutputIterator g2o::readInts (const char *str, OutputIterator out)
 
template<typename OutputIterator >
OutputIterator g2o::readFloats (const char *str, OutputIterator out)
 
std::string g2o::formatString (const char *fmt,...)
 
int g2o::strPrintf (std::string &str, const char *fmt,...)
 
template<typename T >
bool g2o::convertString (const std::string &s, T &x, bool failIfLeftoverChars=true)
 
template<typename T >
g2o::stringToType (const std::string &s, bool failIfLeftoverChars=true)
 
bool g2o::strStartsWith (const std::string &s, const std::string &start)
 
bool g2o::strEndsWith (const std::string &s, const std::string &end)
 
std::string g2o::strExpandFilename (const std::string &filename)
 
std::vector< std::string > g2o::strSplit (const std::string &str, const std::string &delimiters)
 
int g2o::readLine (std::istream &is, std::stringstream &currentLine)
 

Detailed Description

Macro Definition Documentation

#define DO_EVERY (   secs,
  code 
)    DO_EVERY_TS(secs, g2o::get_time(), code)

Executes code, only if secs are gone since last exec.

Definition at line 66 of file timeutil.h.

#define DO_EVERY_TS (   secs,
  currentTime,
  code 
)
Value:
if (1) {\
static double s_lastDone_ = (currentTime); \
double s_now_ = (currentTime); \
if (s_lastDone_ > s_now_) \
s_lastDone_ = s_now_; \
if (s_now_ - s_lastDone_ > (secs)) { \
code; \
s_lastDone_ = s_now_; \
}\
} else \
(void)0
if(POLICY CMP0020) cmake_policy(SET CMP0020 OLD) endif() if(Qt4_FOUND) endif() if(Qt5_FOUND) endif() ADD_LIBRARY(viewer_library $
Definition: CMakeLists.txt:2

Executes code, only if secs are gone since last exec. extended version, in which the current time is given, e.g., timestamp of IPC message

Definition at line 50 of file timeutil.h.

#define MEASURE_FUNCTION_TIME   g2o::ScopeTime scopeTime(__PRETTY_FUNCTION__)

Definition at line 127 of file timeutil.h.

#define MEASURE_TIME (   text,
  code 
)
Value:
if(1) { \
double _start_time_ = g2o::get_time(); \
code; \
fprintf(stderr, "%s took %f sec\n", text, g2o::get_time() - _start_time_); \
} else \
(void) 0
double get_time()
Definition: timeutil.h:92

Definition at line 70 of file timeutil.h.

Function Documentation

template<typename T >
bool g2o::convertString ( const std::string &  s,
T &  x,
bool  failIfLeftoverChars = true 
)

convert a string into an other type.

Definition at line 125 of file string_tools.h.

126 {
127  std::istringstream i(s);
128  char c;
129  if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
130  return false;
131  return true;
132 }
G2O_STUFF_API std::string g2o::formatString ( const char *  fmt,
  ... 
)

format a string and return a std::string. Format is just like printf, see man 3 printf

Definition at line 95 of file string_tools.cpp.

References __PRETTY_FUNCTION__.

Referenced by gnudump_edges(), gnudump_features(), and g2o::readFloats().

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 }
#define __PRETTY_FUNCTION__
Definition: macros.h:89
template<typename OutputIterator >
OutputIterator g2o::readFloats ( const char *  str,
OutputIterator  out 
)

read float values (seperated by spaces) from a string and store them in the given OutputIterator.

Definition at line 96 of file string_tools.h.

References g2o::formatString(), G2O_ATTRIBUTE_FORMAT12, G2O_ATTRIBUTE_FORMAT23, G2O_STUFF_API, and g2o::strPrintf().

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 }
template<typename OutputIterator >
OutputIterator g2o::readInts ( const char *  str,
OutputIterator  out 
)

read integer values (seperated by spaces) from a string and store them in the given OutputIterator.

Definition at line 77 of file string_tools.h.

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 }
G2O_STUFF_API int g2o::readLine ( std::istream &  is,
std::stringstream &  currentLine 
)

read a line from is into currentLine.

Returns
the number of characters read into currentLine (excluding newline), -1 on eof()

Definition at line 172 of file string_tools.cpp.

Referenced by main(), g2o::ParameterContainer::read(), g2o::Gm2dlIO::readGm2dl(), g2o::Gm2dlIO::readRobotLaser(), g2o::OptimizableGraph::setFixed(), and g2o::stringToType().

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 }
G2O_STUFF_API bool g2o::strEndsWith ( const std::string &  str,
const std::string &  substr 
)

return true, if str ends with substr

Definition at line 165 of file string_tools.cpp.

Referenced by str2solver(), and g2o::stringToType().

166 {
167  if (s.size() < end.size())
168  return false;
169  return equal(end.rbegin(), end.rend(), s.rbegin());
170 }
G2O_STUFF_API std::string g2o::strExpandFilename ( const std::string &  filename)

expand the given filename like a posix shell, e.g., ~ $CARMEN_HOME and other will get expanded. Also command substitution, e.g. pwd will give the current directory.

Definition at line 124 of file string_tools.cpp.

References __PRETTY_FUNCTION__.

Referenced by g2o::stringToType().

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 }
#define __PRETTY_FUNCTION__
Definition: macros.h:89
template<typename T >
T g2o::stringToType ( const std::string &  s,
bool  failIfLeftoverChars = true 
)

convert a string into an other type. Return the converted value. Throw error if parsing is wrong.

Definition at line 139 of file string_tools.h.

References g2o::convertString(), G2O_STUFF_API, g2o::readLine(), g2o::strEndsWith(), g2o::strExpandFilename(), g2o::strSplit(), and g2o::strStartsWith().

140 {
141  T x;
142  convertString(s, x, failIfLeftoverChars);
143  return x;
144 }
bool convertString(const std::string &s, T &x, bool failIfLeftoverChars=true)
Definition: string_tools.h:125
G2O_STUFF_API int g2o::strPrintf ( std::string &  str,
const char *  fmt,
  ... 
)

replacement function for sprintf which fills a std::string instead of a char*

Definition at line 112 of file string_tools.cpp.

Referenced by g2o::readFloats().

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 }
G2O_STUFF_API std::vector< std::string > g2o::strSplit ( const std::string &  s,
const std::string &  delim 
)

split a string into token based on the characters given in delim

Definition at line 143 of file string_tools.cpp.

Referenced by g2o::loadStandardSolver(), g2o::loadStandardTypes(), g2o::OptimizableGraph::setRenamedTypesFromString(), g2o::stringToType(), and g2o::PropertyMap::updateMapFromString().

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 }
G2O_STUFF_API bool g2o::strStartsWith ( const std::string &  str,
const std::string &  substr 
)

return true, if str starts with substr

Definition at line 158 of file string_tools.cpp.

Referenced by str2solver(), and g2o::stringToType().

159 {
160  if (s.size() < start.size())
161  return false;
162  return equal(start.begin(), start.end(), s.begin());
163 }
G2O_STUFF_API std::string g2o::strToLower ( const std::string &  s)

convert the string to lower case

Definition at line 81 of file string_tools.cpp.

References transform().

Referenced by str2method().

82 {
83  string ret;
84  std::transform(s.begin(), s.end(), back_inserter(ret), (int(*)(int)) std::tolower);
85  return ret;
86 }
void transform(PlaneList &l, const SE3Quat &t)
Definition: plane_test.cpp:17
G2O_STUFF_API std::string g2o::strToUpper ( const std::string &  s)

convert a string to upper case

Definition at line 88 of file string_tools.cpp.

References transform().

89 {
90  string ret;
91  std::transform(s.begin(), s.end(), back_inserter(ret), (int(*)(int)) std::toupper);
92  return ret;
93 }
void transform(PlaneList &l, const SE3Quat &t)
Definition: plane_test.cpp:17
G2O_STUFF_API std::string g2o::trim ( const std::string &  s)

remove whitespaces from the start/end of a string

Definition at line 48 of file string_tools.cpp.

Referenced by g2o::OptimizableGraph::setRenamedTypesFromString(), and g2o::PropertyMap::updateMapFromString().

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 }
G2O_STUFF_API std::string g2o::trimLeft ( const std::string &  s)

remove whitespaces from the left side of the string

Definition at line 59 of file string_tools.cpp.

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 }
G2O_STUFF_API std::string g2o::trimRight ( const std::string &  s)

remove whitespaced from the right side of the string

Definition at line 70 of file string_tools.cpp.

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 }