$search
00001 // HOG-Man - Hierarchical Optimization for Pose Graphs on Manifolds 00002 // Copyright (C) 2010 G. Grisetti, R. Kümmerle, C. Stachniss 00003 // 00004 // HOG-Man is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU Lesser General Public License as published 00006 // by the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // HOG-Man is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU Lesser General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU Lesser General Public License 00015 // along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 00017 /*************************************************************************** 00018 * filesysTools.cpp 00019 * 00020 * Fr 02 Mär 2007 23:14:08 CET 00021 * Copyright 2007 Rainer Kümmerle 00022 * Email rk@raikue.net 00023 ****************************************************************************/ 00024 #include "filesys_tools.h" 00025 00026 #include <sys/stat.h> 00027 #include <ctime> 00028 #include <sys/types.h> 00029 #include <dirent.h> 00030 #include <cstdio> 00031 #include <iostream> 00032 00033 #ifdef LINUX 00034 #include <wordexp.h> 00035 #endif //LINUX 00036 00037 #ifdef WINDOWS 00038 typedef unsigned int uint; 00039 #endif //windows 00040 00041 namespace AISNavigation { 00042 00043 std::string getFileExtension(const std::string& filename) 00044 { 00045 std::string::size_type lastDot = filename.find_last_of('.'); 00046 if (lastDot != std::string::npos) 00047 return filename.substr(lastDot + 1); 00048 else 00049 return ""; 00050 } 00051 00052 std::string getPureFilename(const std::string& filename) 00053 { 00054 std::string::size_type lastDot = filename.find_last_of('.'); 00055 if (lastDot != std::string::npos) 00056 return filename.substr(0, lastDot); 00057 else 00058 return filename; 00059 } 00060 00061 std::string getBasename(const std::string& filename) 00062 { 00063 std::string::size_type lastSlash = filename.find_last_of('/'); 00064 if (lastSlash != std::string::npos) 00065 return filename.substr(lastSlash + 1); 00066 else 00067 return filename; 00068 } 00069 00070 std::string getDirname(const std::string& filename) 00071 { 00072 std::string::size_type lastSlash = filename.find_last_of('/'); 00073 if (lastSlash != std::string::npos) 00074 return filename.substr(0, lastSlash); 00075 else 00076 return ""; 00077 } 00078 00079 std::string changeFileExtension(const std::string& filename, const std::string& newExt, bool stripDot) 00080 { 00081 std::string::size_type lastDot = filename.find_last_of('.'); 00082 if (lastDot != std::string::npos) { 00083 if (stripDot) 00084 return filename.substr(0, lastDot) + newExt; 00085 else 00086 return filename.substr(0, lastDot + 1) + newExt; 00087 } else 00088 return filename; 00089 } 00090 00091 bool fileExists(const char* filename) 00092 { 00093 struct stat statInfo; 00094 return (stat(filename, &statInfo) == 0); 00095 } 00096 00097 bool isRegularFile(const char* filename) 00098 { 00099 struct stat statInfo; 00100 return (stat(filename, &statInfo) == 0 && S_ISREG(statInfo.st_mode)); 00101 } 00102 00103 bool isDirectory(const char* filename) 00104 { 00105 struct stat statInfo; 00106 return (stat(filename, &statInfo) == 0 && S_ISDIR(statInfo.st_mode)); 00107 } 00108 00109 bool isSymbolicLink(const char* filename) 00110 { 00111 #ifdef WINDOWS 00112 return false; 00113 #else 00114 struct stat statInfo; 00115 return (lstat(filename, &statInfo) == 0 && S_ISLNK(statInfo.st_mode)); 00116 #endif 00117 } 00118 00119 std::string getCurrentDateAsFilename() 00120 { 00121 time_t t = time(NULL); 00122 const size_t dateStrSize = 1024; 00123 char dateStr[dateStrSize]; 00124 if (strftime(dateStr, dateStrSize, "%Y%m%d_%H%M%S", localtime(&t)) == 0) 00125 fprintf(stderr, "Error (%s: %s) Date: %s\n", __FILE__, __func__, dateStr); 00126 return std::string(dateStr); 00127 } 00128 00129 time_t getLastModificationDate(const char* filename) 00130 { 00131 struct stat statInfo; 00132 if (stat(filename, &statInfo) == 0) { 00133 return statInfo.st_mtime; 00134 } else { 00135 return 0; 00136 } 00137 } 00138 00139 time_t getLastAccessDate(const char* filename) 00140 { 00141 struct stat statInfo; 00142 if (stat(filename, &statInfo) == 0) { 00143 return statInfo.st_atime; 00144 } else { 00145 return 0; 00146 } 00147 } 00148 00149 time_t getLastStatusChangeDate(const char* filename) 00150 { 00151 struct stat statInfo; 00152 if (stat(filename, &statInfo) == 0) { 00153 return statInfo.st_ctime; 00154 } else { 00155 return 0; 00156 } 00157 } 00158 00159 #ifdef LINUX 00160 bool createDirectory(const char* dirName, bool pub) 00161 { 00162 bool status = true; 00163 status &= (mkdir(dirName, 0) == 0); 00164 if (pub) 00165 status &= (0 == chmod(dirName, // set directory to rwxrwxrwx 00166 S_IRUSR | S_IWUSR | S_IXUSR | 00167 S_IRGRP | S_IWGRP | S_IXGRP | 00168 S_IROTH | S_IWOTH | S_IXOTH )); 00169 else 00170 status &= (0 == chmod(dirName, // set directory to rwxr-xr-x 00171 S_IRUSR | S_IWUSR | S_IXUSR | 00172 S_IRGRP | S_IXGRP | 00173 S_IROTH | S_IXOTH )); 00174 return status; 00175 } 00176 #endif 00177 00178 #ifdef WINDOWS 00179 bool createDirectory(const char* dirName, bool pub) 00180 { 00181 bool status = true; 00182 status &= (mkdir(dirName) == 0); 00183 return status; 00184 } 00185 #endif 00186 00187 off_t getFileSize(const char* filename) 00188 { 00189 struct stat statInfo; 00190 if (stat(filename, &statInfo) == 0) { 00191 return statInfo.st_size; 00192 } else { 00193 return -1; 00194 } 00195 } 00196 00197 std::vector<std::string> getDirectoryElements(const char* dir, bool onlyFiles) 00198 { 00199 std::vector<std::string> dirEntries; 00200 DIR* curDir = opendir(dir); 00201 if(curDir == NULL) { 00202 return dirEntries; 00203 } 00204 00205 struct dirent* dirEnt = readdir(curDir); 00206 while(dirEnt != NULL) { 00207 std::string name = dirEnt->d_name; 00208 #ifdef LINUX 00209 if (name != "." && name != ".." && (!onlyFiles || dirEnt->d_type == DT_REG)) 00210 dirEntries.push_back(name); 00211 #endif //linux 00212 00213 #ifdef WINDOWS 00214 if (name != "." && name != ".." && (!onlyFiles)) 00215 dirEntries.push_back(name); 00216 #endif //windows 00217 dirEnt = readdir(curDir); 00218 } 00219 closedir(curDir); 00220 return dirEntries; 00221 } 00222 00223 std::vector<std::string> getFilesByPattern(const char* pattern) 00224 { 00225 00226 #ifdef LINUX 00227 std::vector<std::string> result; 00228 wordexp_t p; 00229 wordexp(pattern, &p, 0); 00230 result.reserve(p.we_wordc); 00231 for (size_t i = 0; i < p.we_wordc; ++i) 00232 result.push_back(p.we_wordv[i]); 00233 wordfree(&p); 00234 return result; 00235 #endif //LINUX 00236 00237 00238 #ifdef WINDOWS 00239 std::cerr << "WARNING: " << __PRETTY_FUNCTION__ << " not implemented on windows target" << std::endl; 00240 return std::vector<std::string>(); 00241 #endif// WINDOWS 00242 00243 } 00244 00245 }