Privacy
An open-source, flexible 3D physical simulation framework
misc.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011, 2012, DFKI GmbH Robotics Innovation Center
3  *
4  * This file is part of the MARS simulation framework.
5  *
6  * MARS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation, either version 3
9  * of the License, or (at your option) any later version.
10  *
11  * MARS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with MARS. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef MARS_UTILS_MISC_H
22 #define MARS_UTILS_MISC_H
23 
24 #ifdef WIN32
25  #include <sys/timeb.h>
26  #include <windows.h>
27  #include <io.h>
28 #else
29  #include <sys/time.h>
30  #include <unistd.h>
31 #endif
32 
33 #include <string>
34 #include <vector>
35 #include <sstream>
36 
37 namespace mars {
38  namespace utils {
39 
45  inline double radToDeg(const double &r)
46  { return r * 57.295779513082320876798154814105; }
47  inline float radToDeg(const float &r)
48  { return r * 57.295779513082320876798154814105f; }
49 
55  inline double degToRad(const double &d)
56  { return d * 0.017453292519943295769236907685; }
57  inline float degToRad(const float &d)
58  { return d * 0.017453292519943295769236907685f; }
59 
60  template <typename T>
61  std::string numToStr ( T Number )
62  {
63  std::ostringstream ss;
64  ss << Number;
65  return ss.str();
66  }
67 
71  inline long long getTime() {
72 #ifdef WIN32
73  struct timeb timer;
74  ftime(&timer);
75  return (long long)(timer.time*1000LL + timer.millitm);
76 #else
77  struct timeval timer;
78  gettimeofday(&timer, NULL);
79  return ((long long)(timer.tv_sec))*1000LL + ((long)(timer.tv_usec))/1000;
80 #endif
81  }
82 
88  inline long long getTimeDiff(long long start) {
89  return getTime() - start;
90  }
91 
96  inline void msleep(unsigned int milliseconds) {
97 #ifdef WIN32
98  ::Sleep(milliseconds);
99 #else
100  static const unsigned int milliToMicro = 1000;
101  ::usleep(milliseconds * milliToMicro);
102 #endif
103  }
104 
112  inline bool pathExists(const std::string &path) {
113 #ifdef WIN32
114  return (_access(path.c_str(), 0) == 0);
115 #else
116  return (access(path.c_str(), F_OK) == 0);
117 #endif
118  }
119 
138  bool matchPattern(const std::string &pattern, const std::string &str);
139 
143  std::string trim(const std::string& str);
144 
145  void handleFilenamePrefix(std::string *file, const std::string &prefix);
146  std::string pathJoin(const std::string &path1, const std::string &path2);
147 
152  void removeFilenamePrefix(std::string *file);
153 
158  void removeFilenameSuffix(std::string *file);
159 
163  std::string getFilenameSuffix(const std::string &file);
164  std::string getPathOfFile(const std::string &filename);
165 
166  std::string getCurrentWorkingDir();
167 
175  unsigned int createDirectory(const std::string &dir, int mode=0755);
176 
177  std::vector<std::string> explodeString(const char c, const std::string &s);
178 
179  std::string replaceString(const std::string &source, const std::string &s1,
180  const std::string &s2);
181 
182  std::string toupper(const std::string &s);
183  std::string tolower(const std::string &s);
184 
185  } // end of namespace utils
186 } // namespace mars
187 
188 #endif /* MARS_UTILS_MISC_H */
std::string getFilenameSuffix(const std::string &file)
given a filename "foobar.baz" this will return ".baz"
Definition: misc.cpp:144
std::string pathJoin(const std::string &path1, const std::string &path2)
Definition: misc.cpp:112
double degToRad(const double &d)
converts an angle given in degrees to radians.
Definition: misc.h:55
std::string getCurrentWorkingDir()
Definition: misc.cpp:153
void msleep(unsigned int milliseconds)
sleeps for at least the specified time.
Definition: misc.h:96
std::string numToStr(T Number)
Definition: misc.h:61
std::string trim(const std::string &str)
remove leading and trailing whitespaces
Definition: misc.cpp:94
double radToDeg(const double &r)
converts an angle given in radians to degrees.
Definition: misc.h:45
u_int8_t r
Definition: CameraSensor.h:41
void removeFilenamePrefix(std::string *file)
given a filepath "bla/da/fnord/foobar.baz" this will remove everything before and including the last ...
Definition: misc.cpp:128
std::string getPathOfFile(const std::string &filename)
Definition: misc.cpp:164
long long getTime()
Definition: misc.h:71
Copyright 2012, DFKI GmbH Robotics Innovation Center.
bool matchPattern(const std::string &pattern, const std::string &str)
basic pattern matching function
Definition: misc.cpp:56
void removeFilenameSuffix(std::string *file)
given a filename "foobar.baz" this will remove the extension (including the dot ".") resulting in "foobar".
Definition: misc.cpp:136
bool pathExists(const std::string &path)
check whether a file or directory exists.
Definition: misc.h:112
std::string tolower(const std::string &s)
Definition: misc.cpp:255
std::string toupper(const std::string &s)
Definition: misc.cpp:247
std::string replaceString(const std::string &source, const std::string &s1, const std::string &s2)
Definition: misc.cpp:236
long long getTimeDiff(long long start)
returns the time difference between now and a given reference.
Definition: misc.h:88
std::vector< std::string > explodeString(const char c, const std::string &s)
Definition: misc.cpp:225
void handleFilenamePrefix(std::string *file, const std::string &prefix)
Definition: misc.cpp:108
unsigned int createDirectory(const std::string &dir, int mode)
creates a directory at the given path.
Definition: misc.cpp:174