// gdcmDirList.cxx //----------------------------------------------------------------------------- #include "gdcmDirList.h" #include "gdcmUtil.h" #include #include #if defined(_MSC_VER) || defined (__CYGWIN__) #include #ifdef _MSC_VER #include #endif //_MSC_VER #else #include #include #endif // Constructor / Destructor /** * \ingroup gdcmDirList * \brief Constructor * @param dirName root directory name * @param recursive whether we want to explore recursively or not */ gdcmDirList::gdcmDirList(std::string dirName,bool recursive) { name=dirName; NormalizePath(name); Explore(name,recursive); } /** * \ingroup gdcmDirList * \brief Destructor */ gdcmDirList::~gdcmDirList(void) { } //----------------------------------------------------------------------------- // Print //----------------------------------------------------------------------------- // Public /** * \ingroup gdcmDirList * \brief Get the directory name * @return the directory name */ std::string gdcmDirList::GetDirName(void) { return(name); } //----------------------------------------------------------------------------- // Protected //----------------------------------------------------------------------------- // Private /** * \ingroup gdcmDirList * \brief Explore a directory with possibility of recursion * @param dirName directory to explore * @param recursive whether we want recursion or not */ void gdcmDirList::Explore(std::string dirName,bool recursive) { std::string fileName; NormalizePath(dirName); #if defined(_MSC_VER) || (__CYGWIN__) WIN32_FIND_DATA fileData; HANDLE hFile=FindFirstFile((dirName+"*").c_str(),&fileData); int found=true; while( (hFile!=INVALID_HANDLE_VALUE) && (found) ) { fileName=fileData.cFileName; if(fileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) { if( (fileName!=".") && (fileName!="..") && (recursive) ) Explore(dirName+fileName,recursive); } else { this->push_back(dirName+fileName); } found=FindNextFile(hFile,&fileData); } #else // None of these functions is in POSIX. The functions scandir() and // alphasort() are from BSD 4.3, and have been available under Linux since // libc4. Libc4 and libc5 use the more precise prototype struct dirent **namelist; int n=scandir(dirName.c_str(), &namelist, 0, alphasort); //if n < 0 should raise error for (int i= 0;i < n; i++) { fileName=namelist[i]->d_name; if(namelist[i]->d_type==DT_DIR) { if( (fileName!=".") && (fileName!="..") && (recursive) ) Explore(dirName+fileName,recursive); } else { this->push_back(dirName+fileName); } free(namelist[i]); } free(namelist); #endif } //-----------------------------------------------------------------------------