2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ */
28 #include "bbstdFilesFromDirectory.h"
29 #include "bbstdPackage.h"
37 #include <sys/types.h>
40 #include <sys/stat.h> //stat function
45 BBTK_ADD_BLACK_BOX_TO_PACKAGE(std,FilesFromDirectory)
46 BBTK_BLACK_BOX_IMPLEMENTATION(FilesFromDirectory,bbtk::AtomicBlackBox);
48 void FilesFromDirectory::Process()
51 DirName = bbGetInputIn();
52 bool rec = bbGetInputRecursive();
53 /*int nbFiles = */ Explore(DirName, rec);
54 CleanFilenames( bbGetInputIn() );
55 bbSetOutputOut(Filenames);
56 bbSetOutputOutSimple(SimpleFilenames);
59 void FilesFromDirectory::bbUserSetDefaultValues()
62 bbSetInputRecursive(false);
66 void FilesFromDirectory::bbUserInitializeProcessing()
70 void FilesFromDirectory::bbUserFinalizeProcessing()
75 * \brief Add a SEPARATOR to the end of the name if necessary
76 * @param pathname file/directory name to normalize
78 std::string FilesFromDirectory::NormalizePath(std::string const &pathname)
81 const char FILESEPARATOR = '\\';
83 const char FILESEPARATOR = '/';
86 std::string name = pathname;
87 int size = name.size();
89 if ( name[size-1] != FILESEPARATOR )
91 name += FILESEPARATOR;
97 * \brief Explores a directory with possibility of recursion
98 * return number of files read
99 * @param dirpath directory to explore
100 * @param recursive whether we want recursion or not
105 int FilesFromDirectory::Explore(std::string const &dirpath, bool recursive)
107 int numberOfFiles = 0;
108 std::string dirName = NormalizePath(dirpath);
109 int tmpNumberOfFiles;
110 std::string fileName;
112 WIN32_FIND_DATA fileData;
113 //assert( dirName[dirName.size()-1] == '' );
114 HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
117 for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
118 b = FindNextFile(hFile, &fileData))
120 fileName = fileData.cFileName;
121 if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
123 // Need to check for . and .. to avoid infinite loop
124 if ( fileName != "." && fileName != ".." && recursive )
126 if (bbGetInputType()==1)
128 Filenames.push_back(fileName);
130 } // Type ALL_directories
132 tmpNumberOfFiles = Explore(dirName+fileName, recursive);
133 if ((bbGetInputType()==2)&&tmpNumberOfFiles==0)
135 Filenames.push_back(fileName);
137 } // Type Lsast_directories
139 numberOfFiles = numberOfFiles + tmpNumberOfFiles;
142 // std::string temp = "\"" +dirName+fileName + "\"";
143 std::string temp = dirName+fileName;
146 std::string::size_type spacePosition = temp.find_first_of(' ');
147 if (spacePosition != std::string::npos)
149 std::cout << "=========================================== File name : [" <<temp <<
150 "] contains space(s); Discarted !" << std::endl;
151 temp.insert(spacePosition, "\\");
152 continue; /// \TODO : fix the trouble (vtk?)
157 if (bbGetInputType()==0)
159 Filenames.push_back(temp);
165 DWORD dwError = GetLastError();
166 if (hFile != INVALID_HANDLE_VALUE)
170 if (dwError != ERROR_NO_MORE_FILES)
173 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
174 FORMAT_MESSAGE_FROM_SYSTEM|
175 FORMAT_MESSAGE_IGNORE_INSERTS,
177 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
178 (LPTSTR) &lpMsgBuf,0,NULL);
180 //gdcmErrorMacro("FindNextFile error. Error is " << (char *)lpMsgBuf
181 // <<" for the directory : "<<dirName);
186 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
187 // work on debian for example
189 DIR* dir = opendir(dirName.c_str());
195 // According to POSIX, the dirent structure contains a field char d_name[]
196 // of unspecified size, with at most NAME_MAX characters preceeding the
197 // terminating null character. Use of other fields will harm the porta-
198 // bility of your programs.
202 for (d = readdir(dir); d; d = readdir(dir))
204 fileName = dirName + d->d_name;
205 std::string temp = fileName;
206 if( stat(fileName.c_str(), &buf) != 0 )
208 //gdcmErrorMacro( strerror(errno) );
210 if ( S_ISREG(buf.st_mode) ) //is it a regular file?
212 if ( d->d_name[0]!='.')
216 std::string::size_type spacePosition = temp.find_first_of(' ');
217 if (spacePosition != std::string::npos)
219 std::cout << "=========================================== File name : [" <<temp <<
220 "] contains space(s); Discarted !" << std::endl;
221 temp.insert(spacePosition, "\\");
222 continue; /// \TODO : fix the trouble (vtk?)
223 } // if spacePosition
226 if (bbGetInputType()==0)
228 Filenames.push_back(temp);
232 } else if ( S_ISDIR(buf.st_mode) ) { //directory?
233 if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
235 if (bbGetInputType()==1)
237 Filenames.push_back(fileName);
239 } // Type All_directories
241 tmpNumberOfFiles = Explore( fileName, recursive);
242 if ((bbGetInputType()==2)&&tmpNumberOfFiles==0)
244 Filenames.push_back(fileName);
246 } // Type Lsast_directories
247 numberOfFiles = numberOfFiles+tmpNumberOfFiles;
250 //gdcmErrorMacro( "Unexpected error" );
254 if( closedir(dir) != 0 )
256 //gdcmErrorMacro( strerror(errno) );
261 return numberOfFiles;
265 //------------------------------------------------------------------------------
266 void FilesFromDirectory::CleanFilenames( std::string basePath )
268 std::string tmpString;
269 int i,ii,sizeFilenames = Filenames.size();
271 // Cleanning paths with spaces
272 for (i=0; i<sizeFilenames; i++)
274 tmpString=Filenames[i];
275 std::string::size_type spacePosition = tmpString.find_first_of(' ');
276 if (spacePosition != std::string::npos)
278 std::cout << "=========================================== File name : [" <<tmpString <<
279 "] contains space(s); Discarted !" << std::endl;
280 tmpString.insert(spacePosition, "\\");
281 Filenames[i]=tmpString;
282 // continue; /// \TODO : fix the trouble (vtk?)
283 } // if spacePosition
286 // Alphabetical order
287 for (i=0; i<sizeFilenames; i++)
289 for (ii=i; ii<sizeFilenames; ii++)
291 if (Filenames[i]>Filenames[ii])
293 tmpString=Filenames[i];
294 Filenames[i]=Filenames[ii];
295 Filenames[ii]=tmpString;
300 // Creating SimpleFilenames
301 unsigned int lenghtBasePath = basePath.length();
302 for (i=0; i<sizeFilenames; i++)
304 SimpleFilenames.push_back( Filenames[i].substr( lenghtBasePath ) );
314 int FilesFromDirectory::Explore(std::string const &dirpath, bool recursive)
317 int numberOfFiles = 0;
318 std::string fileName;
319 std::string dirName = NormalizePath(dirpath);
321 WIN32_FIND_DATA fileData;
322 //assert( dirName[dirName.size()-1] == '' );
323 HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
325 for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
326 b = FindNextFile(hFile, &fileData))
328 fileName = fileData.cFileName;
329 if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
331 // Need to check for . and .. to avoid infinite loop
332 if ( fileName != "." && fileName != ".." && recursive )
334 numberOfFiles += Explore(dirName+fileName, recursive);
339 // std::string temp = "\"" +dirName+fileName + "\"";
340 std::string temp = dirName+fileName;
341 std::string::size_type spacePosition = temp.find_first_of(' ');
342 if (spacePosition != std::string::npos)
344 std::cout << "=========================================== File name : [" <<temp <<
345 "] contains space(s); Discarted !" << std::endl;
346 temp.insert(spacePosition, "\\");
347 continue; /// \TODO : fix the trouble (vtk?)
349 Filenames.push_back(temp);
353 DWORD dwError = GetLastError();
354 if (hFile != INVALID_HANDLE_VALUE)
356 if (dwError != ERROR_NO_MORE_FILES)
359 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
360 FORMAT_MESSAGE_FROM_SYSTEM|
361 FORMAT_MESSAGE_IGNORE_INSERTS,
363 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
364 (LPTSTR) &lpMsgBuf,0,NULL);
366 //gdcmErrorMacro("FindNextFile error. Error is " << (char *)lpMsgBuf
367 // <<" for the directory : "<<dirName);
372 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
373 // work on debian for example
375 DIR* dir = opendir(dirName.c_str());
381 // According to POSIX, the dirent structure contains a field char d_name[]
382 // of unspecified size, with at most NAME_MAX characters preceeding the
383 // terminating null character. Use of other fields will harm the porta-
384 // bility of your programs.
388 for (d = readdir(dir); d; d = readdir(dir))
390 fileName = dirName + d->d_name;
391 std::string temp = fileName;
392 if( stat(fileName.c_str(), &buf) != 0 )
394 //gdcmErrorMacro( strerror(errno) );
396 if ( S_ISREG(buf.st_mode) ) //is it a regular file?
398 if ( d->d_name[0]!='.')
401 std::string::size_type spacePosition = temp.find_first_of(' ');
402 if (spacePosition != std::string::npos)
404 std::cout << "=========================================== File name : [" <<temp <<
405 "] contains space(s); Discarted !" << std::endl;
406 temp.insert(spacePosition, "\\");
407 continue; /// \TODO : fix the trouble (vtk?)
409 Filenames.push_back(temp);
413 else if ( S_ISDIR(buf.st_mode) ) //directory?
415 if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
417 numberOfFiles += Explore( fileName, recursive);
422 //gdcmErrorMacro( "Unexpected error" );
426 if( closedir(dir) != 0 )
428 //gdcmErrorMacro( strerror(errno) );
432 std::string tmpString;
433 int i,ii,sizeFilenames = Filenames.size();
434 for (i=0; i<sizeFilenames; i++)
436 for (ii=i; ii<sizeFilenames; ii++)
438 if (Filenames[i]>Filenames[ii])
440 tmpString=Filenames[i];
441 Filenames[i]=Filenames[ii];
442 Filenames[ii]=tmpString;
447 return numberOfFiles;
452 // EO namespace bbstd