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 SimpleFilenames.clear();
52 DirName = bbGetInputIn();
53 bool rec = bbGetInputRecursive();
54 /*int nbFiles = */ Explore(DirName, rec);
55 CleanFilenames( bbGetInputIn() );
56 bbSetOutputOut(Filenames);
57 bbSetOutputOutSimple(SimpleFilenames);
60 void FilesFromDirectory::bbUserSetDefaultValues()
63 bbSetInputRecursive(false);
67 void FilesFromDirectory::bbUserInitializeProcessing()
71 void FilesFromDirectory::bbUserFinalizeProcessing()
76 * \brief Add a SEPARATOR to the end of the name if necessary
77 * @param pathname file/directory name to normalize
79 std::string FilesFromDirectory::NormalizePath(std::string const &pathname)
82 const char FILESEPARATOR = '\\';
84 const char FILESEPARATOR = '/';
87 std::string name = pathname;
88 int size = name.size();
90 if ( name[size-1] != FILESEPARATOR )
92 name += FILESEPARATOR;
98 * \brief Explores a directory with possibility of recursion
99 * return number of files read
100 * @param dirpath directory to explore
101 * @param recursive whether we want recursion or not
106 int FilesFromDirectory::Explore(std::string const &dirpath, bool recursive)
108 int numberOfFiles = 0;
109 std::string dirName = NormalizePath(dirpath);
110 int tmpNumberOfFiles;
111 std::string fileName;
113 WIN32_FIND_DATA fileData;
114 //assert( dirName[dirName.size()-1] == '' );
115 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 std::string temp = dirName+fileName;
129 Filenames.push_back(temp);
131 } // Type ALL_directories
133 tmpNumberOfFiles = Explore(dirName+fileName, recursive);
134 if ((bbGetInputType()==2)&&tmpNumberOfFiles==0)
136 std::string temp = dirName+fileName;
137 Filenames.push_back(temp);
139 } // Type Lsast_directories
141 numberOfFiles = numberOfFiles + tmpNumberOfFiles;
144 // std::string temp = "\"" +dirName+fileName + "\"";
145 std::string temp = dirName+fileName;
148 std::string::size_type spacePosition = temp.find_first_of(' ');
149 if (spacePosition != std::string::npos)
151 std::cout << "=========================================== File name : [" <<temp <<
152 "] contains space(s); Discarted !" << std::endl;
153 temp.insert(spacePosition, "\\");
154 continue; /// \TODO : fix the trouble (vtk?)
159 if (bbGetInputType()==0)
161 Filenames.push_back(temp);
167 DWORD dwError = GetLastError();
168 if (hFile != INVALID_HANDLE_VALUE)
172 if (dwError != ERROR_NO_MORE_FILES)
175 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
176 FORMAT_MESSAGE_FROM_SYSTEM|
177 FORMAT_MESSAGE_IGNORE_INSERTS,
179 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
180 (LPTSTR) &lpMsgBuf,0,NULL);
182 //gdcmErrorMacro("FindNextFile error. Error is " << (char *)lpMsgBuf
183 // <<" for the directory : "<<dirName);
188 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
189 // work on debian for example
191 DIR* dir = opendir( dirName.c_str() );
197 // According to POSIX, the dirent structure contains a field char d_name[]
198 // of unspecified size, with at most NAME_MAX characters preceeding the
199 // terminating null character. Use of other fields will harm the porta-
200 // bility of your programs.
204 for (d = readdir(dir); d; d = readdir(dir))
206 fileName = dirName + d->d_name;
207 std::string temp = fileName;
208 if( stat(fileName.c_str(), &buf) != 0 )
210 //gdcmErrorMacro( strerror(errno) );
212 if ( S_ISREG(buf.st_mode) ) //is it a regular file?
214 if ( d->d_name[0]!='.')
218 std::string::size_type spacePosition = temp.find_first_of(' ');
219 if (spacePosition != std::string::npos)
221 std::cout << "=========================================== File name : [" <<temp <<
222 "] contains space(s); Discarted !" << std::endl;
223 temp.insert(spacePosition, "\\");
224 continue; /// \TODO : fix the trouble (vtk?)
225 } // if spacePosition
228 if (bbGetInputType()==0)
230 Filenames.push_back(temp);
234 } else if ( S_ISDIR(buf.st_mode) ) { //directory?
235 if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
237 if (bbGetInputType()==1)
239 Filenames.push_back(fileName);
241 } // Type All_directories
243 tmpNumberOfFiles = Explore( fileName, recursive);
244 if ((bbGetInputType()==2)&&tmpNumberOfFiles==0)
246 Filenames.push_back(fileName);
248 } // Type Lsast_directories
249 numberOfFiles = numberOfFiles+tmpNumberOfFiles;
252 //gdcmErrorMacro( "Unexpected error" );
256 if( closedir(dir) != 0 )
258 //gdcmErrorMacro( strerror(errno) );
263 return numberOfFiles;
267 //------------------------------------------------------------------------------
268 void FilesFromDirectory::CleanFilenames( std::string basePath )
270 std::string tmpString;
271 int i,ii,sizeFilenames = Filenames.size();
274 // Cleanning paths with spaces
275 for (i=0; i<sizeFilenames; i++)
277 tmpString=Filenames[i];
278 std::string::size_type spacePosition = tmpString.find_first_of(' ');
279 if (spacePosition != std::string::npos)
281 std::cout << "=========================================== File name : [" <<tmpString <<
282 "] contains space(s); Discarted !" << std::endl;
283 tmpString.insert(spacePosition, "\\");
284 Filenames[i]=tmpString;
285 // continue; /// \TODO : fix the trouble (vtk?)
286 } // if spacePosition
289 // Alphabetical order
290 for (i=0; i<sizeFilenames; i++)
292 for (ii=i; ii<sizeFilenames; ii++)
294 if (Filenames[i]>Filenames[ii])
296 tmpString=Filenames[i];
297 Filenames[i]=Filenames[ii];
298 Filenames[ii]=tmpString;
303 // Creating SimpleFilenames
304 unsigned int lenghtBasePath = basePath.length();
305 for (i=0; i<sizeFilenames; i++)
308 SimpleFilenames.push_back( Filenames[i].substr( lenghtBasePath ) );
318 int FilesFromDirectory::Explore(std::string const &dirpath, bool recursive)
321 int numberOfFiles = 0;
322 std::string fileName;
323 std::string dirName = NormalizePath(dirpath);
325 WIN32_FIND_DATA fileData;
326 //assert( dirName[dirName.size()-1] == '' );
327 HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
329 for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
330 b = FindNextFile(hFile, &fileData))
332 fileName = fileData.cFileName;
333 if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
335 // Need to check for . and .. to avoid infinite loop
336 if ( fileName != "." && fileName != ".." && recursive )
338 numberOfFiles += Explore(dirName+fileName, recursive);
343 // std::string temp = "\"" +dirName+fileName + "\"";
344 std::string temp = dirName+fileName;
345 std::string::size_type spacePosition = temp.find_first_of(' ');
346 if (spacePosition != std::string::npos)
348 std::cout << "=========================================== File name : [" <<temp <<
349 "] contains space(s); Discarted !" << std::endl;
350 temp.insert(spacePosition, "\\");
351 continue; /// \TODO : fix the trouble (vtk?)
353 Filenames.push_back(temp);
357 DWORD dwError = GetLastError();
358 if (hFile != INVALID_HANDLE_VALUE)
360 if (dwError != ERROR_NO_MORE_FILES)
363 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
364 FORMAT_MESSAGE_FROM_SYSTEM|
365 FORMAT_MESSAGE_IGNORE_INSERTS,
367 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
368 (LPTSTR) &lpMsgBuf,0,NULL);
370 //gdcmErrorMacro("FindNextFile error. Error is " << (char *)lpMsgBuf
371 // <<" for the directory : "<<dirName);
376 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
377 // work on debian for example
379 DIR* dir = opendir(dirName.c_str());
385 // According to POSIX, the dirent structure contains a field char d_name[]
386 // of unspecified size, with at most NAME_MAX characters preceeding the
387 // terminating null character. Use of other fields will harm the porta-
388 // bility of your programs.
392 for (d = readdir(dir); d; d = readdir(dir))
394 fileName = dirName + d->d_name;
395 std::string temp = fileName;
396 if( stat(fileName.c_str(), &buf) != 0 )
398 //gdcmErrorMacro( strerror(errno) );
400 if ( S_ISREG(buf.st_mode) ) //is it a regular file?
402 if ( d->d_name[0]!='.')
405 std::string::size_type spacePosition = temp.find_first_of(' ');
406 if (spacePosition != std::string::npos)
408 std::cout << "=========================================== File name : [" <<temp <<
409 "] contains space(s); Discarted !" << std::endl;
410 temp.insert(spacePosition, "\\");
411 continue; /// \TODO : fix the trouble (vtk?)
413 Filenames.push_back(temp);
417 else if ( S_ISDIR(buf.st_mode) ) //directory?
419 if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
421 numberOfFiles += Explore( fileName, recursive);
426 //gdcmErrorMacro( "Unexpected error" );
430 if( closedir(dir) != 0 )
432 //gdcmErrorMacro( strerror(errno) );
436 std::string tmpString;
437 int i,ii,sizeFilenames = Filenames.size();
438 for (i=0; i<sizeFilenames; i++)
440 for (ii=i; ii<sizeFilenames; ii++)
442 if (Filenames[i]>Filenames[ii])
444 tmpString=Filenames[i];
445 Filenames[i]=Filenames[ii];
446 Filenames[ii]=tmpString;
451 return numberOfFiles;
456 // EO namespace bbstd