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()
50 DirName = bbGetInputIn();
51 bool rec = bbGetInputRecursive();
52 /*int nbFiles = */ Explore(DirName, rec);
53 bbSetOutputOut(Filenames);
55 // for (int i=0; i<Filenames.size(); i++)
56 // std::cout << "Filenames [" << i << "] = [" << Filenames[i] << "]" << std::endl;
59 void FilesFromDirectory::bbUserSetDefaultValues()
62 bbSetInputRecursive(false);
65 void FilesFromDirectory::bbUserInitializeProcessing()
69 void FilesFromDirectory::bbUserFinalizeProcessing()
74 * \brief Add a SEPARATOR to the end of the name if necessary
75 * @param pathname file/directory name to normalize
77 std::string FilesFromDirectory::NormalizePath(std::string const &pathname)
80 const char FILESEPARATOR = '\\';
82 const char FILESEPARATOR = '/';
85 std::string name = pathname;
86 int size = name.size();
88 if ( name[size-1] != FILESEPARATOR )
90 name += FILESEPARATOR;
96 * \brief Explores a directory with possibility of recursion
97 * return number of files read
98 * @param dirpath directory to explore
99 * @param recursive whether we want recursion or not
101 int FilesFromDirectory::Explore(std::string const &dirpath, bool recursive)
103 int numberOfFiles = 0;
104 std::string fileName;
105 std::string dirName = NormalizePath(dirpath);
107 WIN32_FIND_DATA fileData;
108 //assert( dirName[dirName.size()-1] == '' );
109 HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
111 for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
112 b = FindNextFile(hFile, &fileData))
114 fileName = fileData.cFileName;
115 if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
117 // Need to check for . and .. to avoid infinite loop
118 if ( fileName != "." && fileName != ".." && recursive )
120 numberOfFiles += Explore(dirName+fileName, recursive);
125 std::string temp = "\"" +dirName+fileName + "\"";
126 std::string::size_type spacePosition = temp.find_first_of(' ');
127 if (spacePosition != std::string::npos)
129 std::cout << "=========================================== File name : [" <<temp <<
130 "] contains space(s); Discarted !" << std::endl;
131 temp.insert(spacePosition, "\\");
132 continue; /// \TODO : fix the trouble (vtk?)
134 Filenames.push_back(temp);
138 DWORD dwError = GetLastError();
139 if (hFile != INVALID_HANDLE_VALUE)
141 if (dwError != ERROR_NO_MORE_FILES)
144 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
145 FORMAT_MESSAGE_FROM_SYSTEM|
146 FORMAT_MESSAGE_IGNORE_INSERTS,
148 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
149 (LPTSTR) &lpMsgBuf,0,NULL);
151 //gdcmErrorMacro("FindNextFile error. Error is " << (char *)lpMsgBuf
152 // <<" for the directory : "<<dirName);
157 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
158 // work on debian for example
160 DIR* dir = opendir(dirName.c_str());
166 // According to POSIX, the dirent structure contains a field char d_name[]
167 // of unspecified size, with at most NAME_MAX characters preceeding the
168 // terminating null character. Use of other fields will harm the porta-
169 // bility of your programs.
173 for (d = readdir(dir); d; d = readdir(dir))
175 fileName = dirName + d->d_name;
176 std::string temp = fileName;
177 if( stat(fileName.c_str(), &buf) != 0 )
179 //gdcmErrorMacro( strerror(errno) );
181 if ( S_ISREG(buf.st_mode) ) //is it a regular file?
183 if ( d->d_name[0]!='.')
186 std::string::size_type /* long int */ spacePosition = temp.find_first_of(' ');
187 if (spacePosition != std::string::npos)
189 std::cout << "=========================================== File name : [" <<temp <<
190 "] contains space(s); Discarted !" << std::endl;
191 temp.insert(spacePosition, "\\");
192 continue; /// \TODO : fix the trouble (vtk?)
194 Filenames.push_back(temp);
198 else if ( S_ISDIR(buf.st_mode) ) //directory?
200 if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
202 numberOfFiles += Explore( fileName, recursive);
207 //gdcmErrorMacro( "Unexpected error" );
211 if( closedir(dir) != 0 )
213 //gdcmErrorMacro( strerror(errno) );
217 std::string tmpString;
218 int i,ii,sizeFilenames = Filenames.size();
219 for (i=0; i<sizeFilenames; i++)
221 for (ii=i; ii<sizeFilenames; ii++)
223 if (Filenames[i]>Filenames[ii])
225 tmpString=Filenames[i];
226 Filenames[i]=Filenames[ii];
227 Filenames[ii]=tmpString;
232 return numberOfFiles;
237 // EO namespace bbstd