44 #include <sys/types.h> 56 bool matchPattern(
const std::string &pattern,
const std::string &str) {
57 size_t pos1 = 0, pos2 = 0;
60 std::vector<std::string> patternList;
62 pos2 = pattern.find(
"*", pos1);
63 if(pos2 == pattern.npos) {
65 patternList.push_back(pattern.substr(pos1));
68 patternList.push_back(pattern.substr(pos1, pos2 - pos1));
73 if(patternList.empty()) {
74 return pattern == str;
78 std::vector<std::string>::iterator patternListIt = patternList.begin();
79 int result = str.compare(0, patternListIt->length(), *patternListIt);
82 pos1 = patternListIt->length();
85 for(; patternListIt != patternList.end(); ++patternListIt) {
86 pos1 = str.find(*patternListIt, pos1);
89 pos1 += patternListIt->length();
94 std::string
trim(
const std::string& str) {
96 int front_idx, back_idx, len;
99 back_idx = ( len = str.size() ) - 1;
101 while (isspace(str[front_idx]) && front_idx < len ) front_idx++;
102 while (isspace(str[back_idx]) && back_idx > 0 ) back_idx--;
104 if ( front_idx > back_idx )
return "";
105 else return str.substr(front_idx, back_idx-front_idx+1);
112 std::string
pathJoin(
const std::string &path1,
const std::string &path2) {
113 if(path1.empty())
return path2;
114 if(path2.front() ==
'/') {
119 std::string tmp = path1;
120 if(tmp.back() !=
'/') {
131 if((pos = file->rfind(
'/')) != std::string::npos) {
132 *file= file->substr(pos+1);
139 if((pos = file->rfind(
'.')) != std::string::npos) {
140 *file= file->substr(0, pos);
147 if((pos = file.rfind(
'.')) != std::string::npos) {
148 return file.substr(pos);
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" 161 return std::string(buffer);
165 std::string path =
"./";
168 if((pos = filename.rfind(
'/')) != std::string::npos) {
169 path = filename.substr(0, pos+1);
175 std::string s_tmpString = dir;
178 p_Directory = opendir(dir.c_str());
179 if(p_Directory != NULL) {
180 closedir(p_Directory);
184 char tmp[FILENAME_MAX];
186 snprintf(tmp,
sizeof(tmp),
"%s",dir.c_str());
187 size_t len = strlen(tmp);
188 if(tmp[len-1] ==
'/') {
192 for(p=tmp+1; *p; p++) {
201 result = mkdir(tmp,mode);
215 result = mkdir(tmp,mode);
219 fprintf(stderr,
"misc:: could not create dir: %s\n", dir.c_str());
225 std::vector<std::string>
explodeString(
const char c,
const std::string &s) {
226 std::vector<std::string> result;
227 std::stringstream stream(s);
230 while(std::getline(stream,entry,c)) {
231 result.push_back(entry);
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());
249 for(
size_t i=0; i<s.size(); ++i) {
257 for(
size_t i=0; i<s.size(); ++i) {
std::string getFilenameSuffix(const std::string &file)
given a filename "foobar.baz" this will return ".baz"
std::string pathJoin(const std::string &path1, const std::string &path2)
std::string getCurrentWorkingDir()
std::string trim(const std::string &str)
remove leading and trailing whitespaces
void removeFilenamePrefix(std::string *file)
given a filepath "bla/da/fnord/foobar.baz" this will remove everything before and including the last ...
std::string getPathOfFile(const std::string &filename)
Copyright 2012, DFKI GmbH Robotics Innovation Center.
bool matchPattern(const std::string &pattern, const std::string &str)
basic pattern matching function
void removeFilenameSuffix(std::string *file)
given a filename "foobar.baz" this will remove the extension (including the dot ".") resulting in "foobar".
bool pathExists(const std::string &path)
check whether a file or directory exists.
std::string tolower(const std::string &s)
std::string toupper(const std::string &s)
std::string replaceString(const std::string &source, const std::string &s1, const std::string &s2)
std::vector< std::string > explodeString(const char c, const std::string &s)
void handleFilenamePrefix(std::string *file, const std::string &prefix)
unsigned int createDirectory(const std::string &dir, int mode)
creates a directory at the given path.