Privacy
An open-source, flexible 3D physical simulation framework
misc.cpp
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 /*
22  * utils.cpp
23  * Simulator
24  *
25  * Created by Malte Roemmermann
26  *
27  */
28 
29 #include "misc.h"
30 
31 #include <cstdlib>
32 #include <cstring>
33 #include <cstdio>
34 #include <cmath>
35 #include <cctype>
36 #include <vector>
37 #include <sstream>
38 #include <iostream>
39 #include <cassert>
40 
41 #ifdef WIN32
42 #include <dirent.h>
43 #else
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <unistd.h>
47 #include <dirent.h>
48 #include <stdio.h>
49 #endif
50 
51 namespace mars {
52  namespace utils {
53 
54  using namespace std;
55 
56  bool matchPattern(const std::string &pattern, const std::string &str) {
57  size_t pos1 = 0, pos2 = 0;
58 
59  // chop up pattern
60  std::vector<std::string> patternList;
61  while(1) {
62  pos2 = pattern.find("*", pos1);
63  if(pos2 == pattern.npos) {
64  if(pos1 != 0)
65  patternList.push_back(pattern.substr(pos1));
66  break;
67  }
68  patternList.push_back(pattern.substr(pos1, pos2 - pos1));
69  pos1 = pos2 + 1;
70  }
71 
72  // no wildcards. do direct test
73  if(patternList.empty()) {
74  return pattern == str;
75  }
76 
77  // special case the first pattern because it must match at pos == 0
78  std::vector<std::string>::iterator patternListIt = patternList.begin();
79  int result = str.compare(0, patternListIt->length(), *patternListIt);
80  if(result != 0)
81  return false;
82  pos1 = patternListIt->length();
83  ++patternListIt;
84  // do the matching
85  for(/*nothing*/; patternListIt != patternList.end(); ++patternListIt) {
86  pos1 = str.find(*patternListIt, pos1);
87  if(pos1 == str.npos)
88  return false;
89  pos1 += patternListIt->length();
90  }
91  return true;
92  }
93 
94  std::string trim(const std::string& str) {
95 
96  int front_idx, back_idx, len;
97 
98  front_idx = 0;
99  back_idx = ( len = str.size() ) - 1;
100 
101  while (isspace(str[front_idx]) && front_idx < len ) front_idx++;
102  while (isspace(str[back_idx]) && back_idx > 0 ) back_idx--;
103 
104  if ( front_idx > back_idx ) return "";
105  else return str.substr(front_idx, back_idx-front_idx+1);
106  }
107 
108  void handleFilenamePrefix(std::string *file, const std::string &prefix) {
109  *file = pathJoin(prefix, *file);
110  }
111 
112  std::string pathJoin(const std::string &path1, const std::string &path2) {
113  if(path1.empty()) return path2;
114  if(path2.front() == '/') {
115  // we have an absolute path
116  return path2;
117  }
118 
119  std::string tmp = path1;
120  if(tmp.back() != '/') {
121  tmp += "/";
122  }
123 
124  tmp += path2;
125  return tmp;
126  }
127 
128  void removeFilenamePrefix(std::string *file) {
129  size_t pos;
130 
131  if((pos = file->rfind('/')) != std::string::npos) {
132  *file= file->substr(pos+1);
133  }
134  }
135 
136  void removeFilenameSuffix(std::string *file) {
137  size_t pos;
138 
139  if((pos = file->rfind('.')) != std::string::npos) {
140  *file= file->substr(0, pos);
141  }
142  }
143 
144  std::string getFilenameSuffix(const std::string &file) {
145  size_t pos;
146 
147  if((pos = file.rfind('.')) != std::string::npos) {
148  return file.substr(pos);
149  }
150  return "";
151  }
152 
153  std::string getCurrentWorkingDir() {
154  const int BUFFER_SIZE = 512;
155  char buffer[BUFFER_SIZE];
156  if(!getcwd(buffer, BUFFER_SIZE)) {
157  std::cerr << "misc:: error while getting current working dir"
158  << std::endl;
159  return "";
160  }
161  return std::string(buffer);
162  }
163 
164  std::string getPathOfFile(const std::string &filename) {
165  std::string path = "./";
166  size_t pos;
167 
168  if((pos = filename.rfind('/')) != std::string::npos) {
169  path = filename.substr(0, pos+1);
170  }
171  return path;
172  }
173 
174  unsigned int createDirectory(const std::string &dir, int mode) {
175  std::string s_tmpString = dir;
176  DIR *p_Directory;
177 
178  p_Directory = opendir(dir.c_str());
179  if(p_Directory != NULL) {
180  closedir(p_Directory);
181  return 2;
182  }
183 
184  char tmp[FILENAME_MAX];
185  char *p=0;
186  snprintf(tmp,sizeof(tmp),"%s",dir.c_str());
187  size_t len = strlen(tmp);
188  if(tmp[len-1] == '/') {
189  tmp[len-1] = 0;
190  }
191  int result=0;
192  for(p=tmp+1; *p; p++) {
193  if(*p == '/') {
194  *p=0;
195  if(pathExists(tmp)) {
196  //Directory already exists, assuming this is a first already existing part of the path
197  } else {
198 #ifdef WIN32
199  result = mkdir(tmp);
200 #else
201  result = mkdir(tmp,mode);
202 #endif
203  }
204  if(result == -1) {
205  break;
206  }
207  *p='/';
208  }
209  }
210 
211  if(result != -1) {
212 #ifdef WIN32
213  result = mkdir(tmp);
214 #else
215  result = mkdir(tmp,mode);
216 #endif
217  }
218  if(result == -1) {
219  fprintf(stderr, "misc:: could not create dir: %s\n", dir.c_str());
220  return 0;
221  }
222  return 1;
223  }
224 
225  std::vector<std::string> explodeString(const char c, const std::string &s) {
226  std::vector<std::string> result;
227  std::stringstream stream(s);
228  std::string entry;
229 
230  while(std::getline(stream,entry,c)) {
231  result.push_back(entry);
232  }
233  return result;
234  }
235 
236  std::string replaceString(const std::string &source, const std::string &s1,
237  const std::string &s2) {
238  std::string back = source;
239  size_t found = back.find(s1);
240  while(found != std::string::npos) {
241  back.replace(found, s1.size(), s2);
242  found = back.find(s1, found+s2.size());
243  }
244  return back;
245  }
246 
247  std::string toupper(const std::string &s) {
248  std::string result;
249  for(size_t i=0; i<s.size(); ++i) {
250  result += std::toupper(s[i]);
251  }
252  return result;
253  }
254 
255  std::string tolower(const std::string &s) {
256  std::string result;
257  for(size_t i=0; i<s.size(); ++i) {
258  result += std::tolower(s[i]);
259  }
260  return result;
261  }
262 
263  } // end of namespace utils
264 
265 } // end of namespace mars
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
std::string getCurrentWorkingDir()
Definition: misc.cpp:153
std::string trim(const std::string &str)
remove leading and trailing whitespaces
Definition: misc.cpp:94
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
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
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