Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "rtabmap/utilite/UDirectory.h"
00021
00022 #ifdef _WIN32
00023 #include <Windows.h>
00024 #include <direct.h>
00025 #include <algorithm>
00026 #include <conio.h>
00027 #else
00028 #include <dirent.h>
00029 #include <sys/stat.h>
00030 #include <sys/types.h>
00031 #include <sys/param.h>
00032 #include <unistd.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <pwd.h>
00036 #endif
00037
00038 #include "rtabmap/utilite/UStl.h"
00039 #include "rtabmap/utilite/UFile.h"
00040 #include "rtabmap/utilite/UDirectory.h"
00041 #include "rtabmap/utilite/UConversion.h"
00042
00043 #include "rtabmap/utilite/ULogger.h"
00044
00045 #ifdef _WIN32
00046
00047 bool sortCallback(const std::string & a, const std::string & b)
00048 {
00049 return uStrNumCmp(a,b) < 0;
00050 }
00051 #elif __APPLE__
00052 int sortCallback(const struct dirent ** a, const struct dirent ** b)
00053 {
00054 return uStrNumCmp((*a)->d_name, (*b)->d_name);
00055 }
00056 #else
00057 int sortCallback( const dirent ** a, const dirent ** b)
00058 {
00059 return uStrNumCmp((*a)->d_name, (*b)->d_name);
00060 }
00061 #endif
00062
00063 UDirectory::UDirectory(const std::string & path, const std::string & extensions)
00064 {
00065 extensions_ = uListToVector(uSplit(extensions, ' '));
00066 path_ = path;
00067 iFileName_ = fileNames_.begin();
00068 this->update();
00069 }
00070
00071 UDirectory::UDirectory(const UDirectory & dir)
00072 {
00073 *this = dir;
00074 }
00075
00076 UDirectory & UDirectory::operator=(const UDirectory & dir)
00077 {
00078 extensions_ = dir.extensions_;
00079 path_ = dir.path_;
00080 fileNames_ = dir.fileNames_;
00081 for(iFileName_=fileNames_.begin(); iFileName_!=fileNames_.end(); ++iFileName_)
00082 {
00083 if(iFileName_->compare(*dir.iFileName_) == 0)
00084 {
00085 break;
00086 }
00087 }
00088 return *this;
00089 }
00090
00091 UDirectory::~UDirectory()
00092 {
00093 }
00094
00095 void UDirectory::setPath(const std::string & path, const std::string & extensions)
00096 {
00097 extensions_ = uListToVector(uSplit(extensions, ' '));
00098 path_ = path;
00099 fileNames_.clear();
00100 iFileName_ = fileNames_.begin();
00101 this->update();
00102 }
00103
00104 void UDirectory::update()
00105 {
00106 if(exists(path_))
00107 {
00108 std::string lastName;
00109 bool endOfDir = false;
00110 if(iFileName_ != fileNames_.end())
00111 {
00112
00113 lastName = *iFileName_;
00114 }
00115 else if(fileNames_.size())
00116 {
00117 lastName = *fileNames_.rbegin();
00118 endOfDir = true;
00119 }
00120 fileNames_.clear();
00121 #ifdef _WIN32
00122 WIN32_FIND_DATA fileInformation;
00123 #ifdef UNICODE
00124 wchar_t * pathAll = createWCharFromChar((path_+"\\*").c_str());
00125 HANDLE hFile = ::FindFirstFile(pathAll, &fileInformation);
00126 delete [] pathAll;
00127 #else
00128 HANDLE hFile = ::FindFirstFile((path_+"\\*").c_str(), &fileInformation);
00129 #endif
00130 if(hFile != INVALID_HANDLE_VALUE)
00131 {
00132 do
00133 {
00134 #ifdef UNICODE
00135 char * fileName = createCharFromWChar(fileInformation.cFileName);
00136 fileNames_.push_back(fileName);
00137 delete [] fileName;
00138 #else
00139 fileNames_.push_back(fileInformation.cFileName);
00140 #endif
00141 } while(::FindNextFile(hFile, &fileInformation) == TRUE);
00142 ::FindClose(hFile);
00143 std::vector<std::string> vFileNames = uListToVector(fileNames_);
00144 std::sort(vFileNames.begin(), vFileNames.end(), sortCallback);
00145 fileNames_ = uVectorToList(vFileNames);
00146 }
00147 #else
00148 int nameListSize;
00149 struct dirent ** nameList = 0;
00150 nameListSize = scandir(path_.c_str(), &nameList, 0, sortCallback);
00151 if(nameList && nameListSize>0)
00152 {
00153 for (int i=0;i<nameListSize;++i)
00154 {
00155 fileNames_.push_back(nameList[i]->d_name);
00156 free(nameList[i]);
00157 }
00158 free(nameList);
00159 }
00160 #endif
00161
00162
00163 std::list<std::string>::iterator iter = fileNames_.begin();
00164 bool valid;
00165 while(iter!=fileNames_.end())
00166 {
00167 valid = false;
00168 if(extensions_.size() == 0 &&
00169 iter->compare(".") != 0 &&
00170 iter->compare("..") != 0)
00171 {
00172 valid = true;
00173 }
00174 for(unsigned int i=0; i<extensions_.size(); ++i)
00175 {
00176 if(UFile::getExtension(*iter).compare(extensions_[i]) == 0)
00177 {
00178 valid = true;
00179 break;
00180 }
00181 }
00182 if(!valid)
00183 {
00184 iter = fileNames_.erase(iter);
00185 }
00186 else
00187 {
00188 ++iter;
00189 }
00190 }
00191 iFileName_ = fileNames_.begin();
00192 if(!lastName.empty())
00193 {
00194 bool found = false;
00195 for(std::list<std::string>::iterator iter=fileNames_.begin(); iter!=fileNames_.end(); ++iter)
00196 {
00197 if(lastName.compare(*iter) == 0)
00198 {
00199 found = true;
00200 iFileName_ = iter;
00201 break;
00202 }
00203 }
00204 if(endOfDir && found)
00205 {
00206 ++iFileName_;
00207 }
00208 else if(endOfDir && fileNames_.size())
00209 {
00210 iFileName_ = --fileNames_.end();
00211 }
00212 }
00213 }
00214 }
00215
00216 bool UDirectory::isValid()
00217 {
00218 return exists(path_);
00219 }
00220
00221 std::string UDirectory::getNextFileName()
00222 {
00223 std::string fileName;
00224 if(iFileName_ != fileNames_.end())
00225 {
00226 fileName = *iFileName_;
00227 ++iFileName_;
00228 }
00229 return fileName;
00230 }
00231
00232 std::string UDirectory::getNextFilePath()
00233 {
00234 std::string filePath;
00235 if(iFileName_ != fileNames_.end())
00236 {
00237 filePath = path_+separator()+*iFileName_;
00238 ++iFileName_;
00239 }
00240 return filePath;
00241 }
00242
00243 void UDirectory::rewind()
00244 {
00245 iFileName_ = fileNames_.begin();
00246 }
00247
00248
00249 bool UDirectory::exists(const std::string & dirPath)
00250 {
00251 bool r = false;
00252 #ifdef _WIN32
00253 #ifdef UNICODE
00254 wchar_t * wDirPath = createWCharFromChar(dirPath.c_str());
00255 DWORD dwAttrib = GetFileAttributes(wDirPath);
00256 delete [] wDirPath;
00257 #else
00258 DWORD dwAttrib = GetFileAttributes(dirPath.c_str());
00259 #endif
00260 r = (dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
00261 #else
00262 DIR *dp;
00263 if((dp = opendir(dirPath.c_str())) != NULL)
00264 {
00265 r = true;
00266 closedir(dp);
00267 }
00268 #endif
00269 return r;
00270 }
00271
00272
00273 std::string UDirectory::getDir(const std::string & filePath)
00274 {
00275 std::string dir = filePath;
00276 int i=(int)dir.size()-1;
00277 for(; i>=0; --i)
00278 {
00279 if(dir[i] == '/' || dir[i] == '\\')
00280 {
00281
00282 dir[i] = 0;
00283 --i;
00284 while(i>=0 && (dir[i] == '/' || dir[i] == '\\'))
00285 {
00286 dir[i] = 0;
00287 --i;
00288 }
00289 break;
00290 }
00291 else
00292 {
00293 dir[i] = 0;
00294 }
00295 }
00296
00297 if(i<0)
00298 {
00299 dir = ".";
00300 }
00301 else
00302 {
00303 dir.resize(i+1);
00304 }
00305
00306 return dir;
00307 }
00308
00309 std::string UDirectory::currentDir(bool trailingSeparator)
00310 {
00311 std::string dir;
00312 char * buffer;
00313
00314 #ifdef _WIN32
00315 buffer = _getcwd(NULL, 0);
00316 #else
00317 buffer = getcwd(NULL, MAXPATHLEN);
00318 #endif
00319
00320 if( buffer != NULL )
00321 {
00322 dir = buffer;
00323 free(buffer);
00324 if(trailingSeparator)
00325 {
00326 dir += separator();
00327 }
00328 }
00329
00330 return dir;
00331 }
00332
00333 bool UDirectory::makeDir(const std::string & dirPath)
00334 {
00335 int status;
00336 #ifdef _WIN32
00337 status = _mkdir(dirPath.c_str());
00338 #else
00339 status = mkdir(dirPath.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
00340 #endif
00341 return status==0;
00342 }
00343
00344 bool UDirectory::removeDir(const std::string & dirPath)
00345 {
00346 int status;
00347 #ifdef _WIN32
00348 status = _rmdir(dirPath.c_str());
00349 #else
00350 status = rmdir(dirPath.c_str());
00351 #endif
00352 return status==0;
00353 }
00354
00355 std::string UDirectory::homeDir()
00356 {
00357 std::string path;
00358 #ifdef _WIN32
00359 #ifdef UNICODE
00360 wchar_t wProfilePath[250];
00361 ExpandEnvironmentStrings(L"%userprofile%",wProfilePath,250);
00362 char * profilePath = createCharFromWChar(wProfilePath);
00363 path = profilePath;
00364 delete [] profilePath;
00365 #else
00366 char profilePath[250];
00367 ExpandEnvironmentStrings("%userprofile%",profilePath,250);
00368 path = profilePath;
00369 #endif
00370 #else
00371 char * pathstr = getenv("HOME");
00372 if(pathstr)
00373 {
00374 path = pathstr;
00375 }
00376 if(path.empty())
00377 {
00378 struct passwd *pw = getpwuid(getuid());
00379 if(pw) {
00380 path = pw->pw_dir;
00381 }
00382 if(path.empty())
00383 {
00384 UFATAL("Environment variable HOME is not set, cannot get home directory! Please set HOME environment variable to a valid directory.");
00385 }
00386 }
00387 #endif
00388 return path;
00389 }
00390
00391 std::string UDirectory::separator()
00392 {
00393 #ifdef _WIN32
00394 return "\\";
00395 #else
00396 return "/";
00397 #endif
00398 }