UDirectory.cpp
Go to the documentation of this file.
00001 /*
00002 *  utilite is a cross-platform library with
00003 *  useful utilities for fast and small developing.
00004 *  Copyright (C) 2010  Mathieu Labbe
00005 *
00006 *  utilite is free library: you can redistribute it and/or modify
00007 *  it under the terms of the GNU Lesser General Public License as published by
00008 *  the Free Software Foundation, either version 3 of the License, or
00009 *  (at your option) any later version.
00010 *
00011 *  utilite is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 *  GNU Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public License
00017 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
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 #endif
00036 
00037 #include "rtabmap/utilite/UStl.h"
00038 #include "rtabmap/utilite/UFile.h"
00039 #include "rtabmap/utilite/UDirectory.h"
00040 #include "rtabmap/utilite/UConversion.h"
00041 
00042 #include "rtabmap/utilite/ULogger.h"
00043 
00044 #ifdef _WIN32
00045 
00046 bool sortCallback(const std::string & a, const std::string & b)
00047 {
00048         return uStrNumCmp(a,b) < 0;
00049 }
00050 #elif __APPLE__
00051 int sortCallback(const struct dirent ** a, const struct dirent ** b)
00052 {
00053         return uStrNumCmp((*a)->d_name, (*b)->d_name);
00054 }
00055 #else
00056 int sortCallback( const dirent ** a,  const dirent ** b)
00057 {
00058         return uStrNumCmp((*a)->d_name, (*b)->d_name);
00059 }
00060 #endif
00061 
00062 UDirectory::UDirectory(const std::string & path, const std::string & extensions)
00063 {
00064         extensions_ = uListToVector(uSplit(extensions, ' '));
00065         path_ = path;
00066         iFileName_ = fileNames_.begin();
00067         this->update();
00068 }
00069 
00070 UDirectory::UDirectory(const UDirectory & dir)
00071 {
00072         *this = dir;
00073 }
00074 
00075 UDirectory & UDirectory::operator=(const UDirectory & dir)
00076 {
00077         extensions_ = dir.extensions_;
00078         path_ = dir.path_;
00079         fileNames_ = dir.fileNames_;
00080         for(iFileName_=fileNames_.begin(); iFileName_!=fileNames_.end(); ++iFileName_)
00081         {
00082                 if(iFileName_->compare(*dir.iFileName_) == 0)
00083                 {
00084                         break;
00085                 }
00086         }
00087         return *this;
00088 }
00089 
00090 UDirectory::~UDirectory()
00091 {
00092 }
00093 
00094 void UDirectory::setPath(const std::string & path, const std::string & extensions)
00095 {
00096         extensions_ = uListToVector(uSplit(extensions, ' '));
00097         path_ = path;
00098         fileNames_.clear();
00099         iFileName_ = fileNames_.begin();
00100         this->update();
00101 }
00102 
00103 void UDirectory::update()
00104 {
00105         if(exists(path_))
00106         {
00107                 std::string lastName;
00108                 bool endOfDir = false;
00109                 if(iFileName_ != fileNames_.end())
00110                 {
00111                         //Record the last file name
00112                         lastName = *iFileName_;
00113                 }
00114                 else if(fileNames_.size())
00115                 {
00116                         lastName = *fileNames_.rbegin();
00117                         endOfDir = true;
00118                 }
00119                 fileNames_.clear();
00120 #ifdef _WIN32
00121                 WIN32_FIND_DATA fileInformation;
00122         #ifdef UNICODE
00123                 wchar_t * pathAll = createWCharFromChar((path_+"\\*").c_str());
00124                 HANDLE hFile  = ::FindFirstFile(pathAll, &fileInformation);
00125                 delete [] pathAll;
00126         #else
00127                 HANDLE hFile  = ::FindFirstFile((path_+"\\*").c_str(), &fileInformation);
00128         #endif
00129                 if(hFile != INVALID_HANDLE_VALUE)
00130                 {
00131                         do
00132                         {
00133         #ifdef UNICODE
00134                                 char * fileName = createCharFromWChar(fileInformation.cFileName);
00135                                 fileNames_.push_back(fileName);
00136                                 delete [] fileName;
00137         #else
00138                                 fileNames_.push_back(fileInformation.cFileName);
00139         #endif
00140                         } while(::FindNextFile(hFile, &fileInformation) == TRUE);
00141                         ::FindClose(hFile);
00142                         std::vector<std::string> vFileNames = uListToVector(fileNames_);
00143                         std::sort(vFileNames.begin(), vFileNames.end(), sortCallback);
00144                         fileNames_ = uVectorToList(vFileNames);
00145                 }
00146 #else
00147                 int nameListSize;
00148                 struct dirent ** nameList = 0;
00149                 nameListSize =  scandir(path_.c_str(), &nameList, 0, sortCallback);
00150                 if(nameList && nameListSize>0)
00151                 {
00152                         for (int i=0;i<nameListSize;++i)
00153                         {
00154                                 fileNames_.push_back(nameList[i]->d_name);
00155                                 free(nameList[i]);
00156                         }
00157                         free(nameList);
00158                 }
00159 #endif
00160 
00161                 //filter extensions...
00162                 std::list<std::string>::iterator iter = fileNames_.begin();
00163                 bool valid;
00164                 while(iter!=fileNames_.end())
00165                 {
00166                         valid = false;
00167                         if(extensions_.size() == 0 &&
00168                            iter->compare(".") != 0 &&
00169                            iter->compare("..") != 0)
00170                         {
00171                                 valid = true;
00172                         }
00173                         for(unsigned int i=0; i<extensions_.size(); ++i)
00174                         {
00175                                 if(UFile::getExtension(*iter).compare(extensions_[i]) == 0)
00176                                 {
00177                                         valid = true;
00178                                         break;
00179                                 }
00180                         }
00181                         if(!valid)
00182                         {
00183                                 iter = fileNames_.erase(iter);
00184                         }
00185                         else
00186                         {
00187                                 ++iter;
00188                         }
00189                 }
00190                 iFileName_ = fileNames_.begin();
00191                 if(!lastName.empty())
00192                 {
00193                         bool found = false;
00194                         for(std::list<std::string>::iterator iter=fileNames_.begin(); iter!=fileNames_.end(); ++iter)
00195                         {
00196                                 if(lastName.compare(*iter) == 0)
00197                                 {
00198                                         found = true;
00199                                         iFileName_ = iter;
00200                                         break;
00201                                 }
00202                         }
00203                         if(endOfDir && found)
00204                         {
00205                                 ++iFileName_;
00206                         }
00207                         else if(endOfDir && fileNames_.size())
00208                         {
00209                                 iFileName_ = --fileNames_.end();
00210                         }
00211                 }
00212         }
00213 }
00214 
00215 bool UDirectory::isValid()
00216 {
00217         return exists(path_);
00218 }
00219 
00220 std::string UDirectory::getNextFileName()
00221 {
00222         std::string fileName;
00223         if(iFileName_ != fileNames_.end())
00224         {
00225                 fileName = *iFileName_;
00226                 ++iFileName_;
00227         }
00228         return fileName;
00229 }
00230 
00231 std::string UDirectory::getNextFilePath()
00232 {
00233         std::string filePath;
00234         if(iFileName_ != fileNames_.end())
00235         {
00236                 filePath = path_+separator()+*iFileName_;
00237                 ++iFileName_;
00238         }
00239         return filePath;
00240 }
00241 
00242 void UDirectory::rewind()
00243 {
00244         iFileName_ = fileNames_.begin();
00245 }
00246 
00247 
00248 bool UDirectory::exists(const std::string & dirPath)
00249 {
00250         bool r = false;
00251 #ifdef _WIN32
00252         #ifdef UNICODE
00253         wchar_t * wDirPath = createWCharFromChar(dirPath.c_str());
00254         DWORD dwAttrib = GetFileAttributes(wDirPath);
00255         delete [] wDirPath;
00256         #else
00257         DWORD dwAttrib = GetFileAttributes(dirPath.c_str());
00258         #endif
00259         r = (dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
00260 #else
00261         DIR *dp;
00262         if((dp  = opendir(dirPath.c_str())) != NULL)
00263         {
00264                 r = true;
00265                 closedir(dp);
00266         }
00267 #endif
00268         return r;
00269 }
00270 
00271 // return the directory path of the file
00272 std::string UDirectory::getDir(const std::string & filePath)
00273 {
00274         std::string dir = filePath;
00275         int i=(int)dir.size()-1;
00276         for(; i>=0; --i)
00277         {
00278                 if(dir[i] == '/' || dir[i] == '\\')
00279                 {
00280                         //remove separators...
00281                         dir[i] = 0;
00282                         --i;
00283                         while(i>=0 && (dir[i] == '/' || dir[i] == '\\'))
00284                         {
00285                                 dir[i] = 0;
00286                                 --i;
00287                         }
00288                         break;
00289                 }
00290                 else
00291                 {
00292                         dir[i] = 0;
00293                 }
00294         }
00295 
00296         if(i<0)
00297         {
00298                 dir = ".";
00299         }
00300         else
00301         {
00302                 dir.resize(i+1);
00303         }
00304 
00305         return dir;
00306 }
00307 
00308 std::string UDirectory::currentDir(bool trailingSeparator)
00309 {
00310         std::string dir;
00311         char * buffer;
00312 
00313 #ifdef _WIN32
00314         buffer = _getcwd(NULL, 0);
00315 #else
00316         buffer = getcwd(NULL, MAXPATHLEN);
00317 #endif
00318 
00319         if( buffer != NULL )
00320         {
00321                 dir = buffer;
00322                 free(buffer);
00323                 if(trailingSeparator)
00324                 {
00325                         dir += separator();
00326                 }
00327         }
00328 
00329         return dir;
00330 }
00331 
00332 bool UDirectory::makeDir(const std::string & dirPath)
00333 {
00334         int status;
00335 #ifdef _WIN32
00336         status = _mkdir(dirPath.c_str());
00337 #else
00338         status = mkdir(dirPath.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
00339 #endif
00340         return status==0;
00341 }
00342 
00343 bool UDirectory::removeDir(const std::string & dirPath)
00344 {
00345         int status;
00346 #ifdef _WIN32
00347         status = _rmdir(dirPath.c_str());
00348 #else
00349         status = rmdir(dirPath.c_str());
00350 #endif
00351         return status==0;
00352 }
00353 
00354 std::string UDirectory::homeDir()
00355 {
00356         std::string path;
00357 #ifdef _WIN32
00358         #ifdef UNICODE
00359         wchar_t wProfilePath[250];
00360         ExpandEnvironmentStrings(L"%userprofile%",wProfilePath,250);
00361         char * profilePath = createCharFromWChar(wProfilePath);
00362         path = profilePath;
00363         delete [] profilePath;
00364         #else
00365         char profilePath[250];
00366         ExpandEnvironmentStrings("%userprofile%",profilePath,250);
00367         path = profilePath;
00368         #endif
00369 #else
00370         path = getenv("HOME");
00371 #endif
00372         return path;
00373 }
00374 
00375 std::string UDirectory::separator()
00376 {
00377 #ifdef _WIN32
00378         return "\\";
00379 #else
00380         return "/";
00381 #endif
00382 }


rtabmap
Author(s): Mathieu Labbe
autogenerated on Sat Jul 23 2016 11:44:28