1 #include "bbstdFilesFromDirectory.h"
2 #include "bbstdPackage.h"
12 #include <sys/stat.h> //stat function
17 BBTK_ADD_BLACK_BOX_TO_PACKAGE(std,FilesFromDirectory)
18 BBTK_BLACK_BOX_IMPLEMENTATION(FilesFromDirectory,bbtk::AtomicBlackBox);
20 void FilesFromDirectory::Process()
23 DirName = bbGetInputIn();
24 bool rec = bbGetInputRecursive();
25 /*int nbFiles = */ Explore(DirName, rec);
26 bbSetOutputOut(Filenames);
30 void FilesFromDirectory::bbUserSetDefaultValues()
34 bbSetInputRecursive(false);
37 void FilesFromDirectory::bbUserInitializeProcessing()
40 void FilesFromDirectory::bbUserFinalizeProcessing()
45 * \brief Add a SEPARATOR to the end of the name if necessary
46 * @param pathname file/directory name to normalize
48 std::string FilesFromDirectory::NormalizePath(std::string const &pathname)
51 const char FILESEPARATOR = '\\';
53 const char FILESEPARATOR = '/';
56 std::string name = pathname;
57 int size = name.size();
59 if ( name[size-1] != FILESEPARATOR )
61 name += FILESEPARATOR;
67 * \brief Explores a directory with possibility of recursion
68 * return number of files read
69 * @param dirpath directory to explore
70 * @param recursive whether we want recursion or not
72 int FilesFromDirectory::Explore(std::string const &dirpath, bool recursive)
74 int numberOfFiles = 0;
76 std::string dirName = NormalizePath(dirpath);
78 WIN32_FIND_DATA fileData;
79 //assert( dirName[dirName.size()-1] == '' );
80 HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
82 for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
83 b = FindNextFile(hFile, &fileData))
85 fileName = fileData.cFileName;
86 if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
88 // Need to check for . and .. to avoid infinite loop
89 if ( fileName != "." && fileName != ".." && recursive )
91 numberOfFiles += Explore(dirName+fileName,recursive);
96 Filenames.push_back(dirName+fileName);
100 DWORD dwError = GetLastError();
101 if (hFile != INVALID_HANDLE_VALUE)
103 if (dwError != ERROR_NO_MORE_FILES)
106 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
107 FORMAT_MESSAGE_FROM_SYSTEM|
108 FORMAT_MESSAGE_IGNORE_INSERTS,
110 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
111 (LPTSTR) &lpMsgBuf,0,NULL);
113 //gdcmErrorMacro("FindNextFile error. Error is " << (char *)lpMsgBuf
114 // <<" for the directory : "<<dirName);
119 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
120 // work on debian for example
122 DIR* dir = opendir(dirName.c_str());
128 // According to POSIX, the dirent structure contains a field char d_name[]
129 // of unspecified size, with at most NAME_MAX characters preceeding the
130 // terminating null character. Use of other fields will harm the porta-
131 // bility of your programs.
135 for (d = readdir(dir); d; d = readdir(dir))
137 fileName = dirName + d->d_name;
138 if( stat(fileName.c_str(), &buf) != 0 )
140 //gdcmErrorMacro( strerror(errno) );
142 if ( S_ISREG(buf.st_mode) ) //is it a regular file?
144 if ( d->d_name[0]!='.')
146 Filenames.push_back( fileName );
150 else if ( S_ISDIR(buf.st_mode) ) //directory?
152 if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
154 numberOfFiles += Explore( fileName, recursive);
159 //gdcmErrorMacro( "Unexpected error" );
163 if( closedir(dir) != 0 )
165 //gdcmErrorMacro( strerror(errno) );
169 return numberOfFiles;
174 // EO namespace bbstd