1 #include "bbstdFilesFromDirectory.h"
2 #include "bbstdPackage.h"
10 #include <sys/types.h>
13 #include <sys/stat.h> //stat function
18 BBTK_ADD_BLACK_BOX_TO_PACKAGE(std,FilesFromDirectory)
19 BBTK_BLACK_BOX_IMPLEMENTATION(FilesFromDirectory,bbtk::AtomicBlackBox);
21 void FilesFromDirectory::Process()
23 DirName = bbGetInputIn();
24 bool rec = bbGetInputRecursive();
25 /*int nbFiles = */ Explore(DirName, rec);
26 bbSetOutputOut(Filenames);
28 // for (int i=0; i<Filenames.size(); i++)
29 // std::cout << "Filenames [" << i << "] = [" << Filenames[i] << "]" << std::endl;
32 void FilesFromDirectory::bbUserSetDefaultValues()
35 bbSetInputRecursive(false);
38 void FilesFromDirectory::bbUserInitializeProcessing()
42 void FilesFromDirectory::bbUserFinalizeProcessing()
47 * \brief Add a SEPARATOR to the end of the name if necessary
48 * @param pathname file/directory name to normalize
50 std::string FilesFromDirectory::NormalizePath(std::string const &pathname)
53 const char FILESEPARATOR = '\\';
55 const char FILESEPARATOR = '/';
58 std::string name = pathname;
59 int size = name.size();
61 if ( name[size-1] != FILESEPARATOR )
63 name += FILESEPARATOR;
69 * \brief Explores a directory with possibility of recursion
70 * return number of files read
71 * @param dirpath directory to explore
72 * @param recursive whether we want recursion or not
74 int FilesFromDirectory::Explore(std::string const &dirpath, bool recursive)
76 int numberOfFiles = 0;
78 std::string dirName = NormalizePath(dirpath);
80 WIN32_FIND_DATA fileData;
81 //assert( dirName[dirName.size()-1] == '' );
82 HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
84 for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
85 b = FindNextFile(hFile, &fileData))
87 fileName = fileData.cFileName;
88 if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
90 // Need to check for . and .. to avoid infinite loop
91 if ( fileName != "." && fileName != ".." && recursive )
93 numberOfFiles += Explore(dirName+fileName, recursive);
98 std::string temp = "\"" +dirName+fileName + "\"";
99 std::string::size_type spacePosition = temp.find_first_of(' ');
100 if (spacePosition != std::string::npos)
102 std::cout << "=========================================== File name : [" <<temp <<
103 "] contains space(s); Discarted !" << std::endl;
104 temp.insert(spacePosition, "\\");
105 continue; /// \TODO : fix the trouble (vtk?)
107 Filenames.push_back(temp);
111 DWORD dwError = GetLastError();
112 if (hFile != INVALID_HANDLE_VALUE)
114 if (dwError != ERROR_NO_MORE_FILES)
117 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
118 FORMAT_MESSAGE_FROM_SYSTEM|
119 FORMAT_MESSAGE_IGNORE_INSERTS,
121 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
122 (LPTSTR) &lpMsgBuf,0,NULL);
124 //gdcmErrorMacro("FindNextFile error. Error is " << (char *)lpMsgBuf
125 // <<" for the directory : "<<dirName);
130 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
131 // work on debian for example
133 DIR* dir = opendir(dirName.c_str());
139 // According to POSIX, the dirent structure contains a field char d_name[]
140 // of unspecified size, with at most NAME_MAX characters preceeding the
141 // terminating null character. Use of other fields will harm the porta-
142 // bility of your programs.
146 for (d = readdir(dir); d; d = readdir(dir))
148 fileName = dirName + d->d_name;
149 std::string temp = fileName;
150 if( stat(fileName.c_str(), &buf) != 0 )
152 //gdcmErrorMacro( strerror(errno) );
154 if ( S_ISREG(buf.st_mode) ) //is it a regular file?
156 if ( d->d_name[0]!='.')
159 std::string::size_type /* long int */ spacePosition = temp.find_first_of(' ');
160 if (spacePosition != std::string::npos)
162 std::cout << "=========================================== File name : [" <<temp <<
163 "] contains space(s); Discarted !" << std::endl;
164 temp.insert(spacePosition, "\\");
165 continue; /// \TODO : fix the trouble (vtk?)
167 Filenames.push_back(temp);
171 else if ( S_ISDIR(buf.st_mode) ) //directory?
173 if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
175 numberOfFiles += Explore( fileName, recursive);
180 //gdcmErrorMacro( "Unexpected error" );
184 if( closedir(dir) != 0 )
186 //gdcmErrorMacro( strerror(errno) );
190 return numberOfFiles;
195 // EO namespace bbstd