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 /*=========================================================================
31 Module: $RCSfile: creaFilesFromDirectory.cxx,v $
33 Date: $Date: 2012/11/15 10:43:26 $
34 Version: $Revision: 1.7 $
37 This software is distributed WITHOUT ANY WARRANTY; without even
38 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
39 PURPOSE. See the above copyright notices for more information.
41 =========================================================================*/
46 * \brief class Exception:generic class for throwing any exception (header)
55 #include <sys/types.h>
58 #include <sys/stat.h> //stat function
60 #include "creaFilesFromDirectory.h"
61 #include "creaMessageManager.h"
67 * \brief Add a SEPARATOR to the end of the name if necessary
68 * @param pathname file/directory name to normalize
70 std::string DirList::NormalizePath(std::string const &pathname)
75 const char FILESEPARATOR = '\\';
77 const char FILESEPARATOR = '/';
81 std::string name = pathname;
82 int size = name.size();
84 // if ( name[size-1] != SEPARATOR_X && name[size-1] != SEPARATOR_WIN )
85 if ( name[size-1] != FILESEPARATOR )
87 name += FILESEPARATOR;
92 * \brief Explore a directory with possibility of recursion
93 * return number of files read
94 * @param dirpath directory to explore
95 * @param recursive whether we want recursion or not
97 int DirList::Explore(std::string const &dirpath, bool recursive)
99 int numberOfFiles = 0;
100 std::string fileName;
101 std::string dirName = NormalizePath(dirpath);
103 WIN32_FIND_DATA fileData;
104 //assert( dirName[dirName.size()-1] == '' );
105 HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
107 for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
108 b = FindNextFile(hFile, &fileData))
110 fileName = fileData.cFileName;
111 if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
113 // Need to check for . and .. to avoid infinite loop
114 if ( fileName != "." && fileName != ".." && recursive )
116 numberOfFiles += Explore(dirName+fileName,recursive);
121 Filenames.push_back(dirName+fileName);
125 DWORD dwError = GetLastError();
126 if (hFile != INVALID_HANDLE_VALUE)
128 if (dwError != ERROR_NO_MORE_FILES)
131 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
132 FORMAT_MESSAGE_FROM_SYSTEM|
133 FORMAT_MESSAGE_IGNORE_INSERTS,
135 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
136 (LPTSTR) &lpMsgBuf,0,NULL);
138 creaError("FindNextFile error. Error is " << (char *)lpMsgBuf
139 <<" for the directory : "<<dirName);
144 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
145 // work on debian for example
147 DIR* dir = opendir(dirName.c_str());
153 // According to POSIX, the dirent structure contains a field char d_name[]
154 // of unspecified size, with at most NAME_MAX characters preceeding the
155 // terminating null character. Use of other fields will harm the porta-
156 // bility of your programs.
160 for (d = readdir(dir); d; d = readdir(dir))
163 fileName = dirName + d->d_name;
164 if( stat(fileName.c_str(), &buf) != 0 )
166 //gdcmErrorMacro( strerror(errno) );
168 if ( S_ISREG(buf.st_mode) ) //is it a regular file?
170 //printf("EED DirList::Explore [%s]\n" , d->d_name );
171 if ( d->d_name[0]!='.')
173 Filenames.push_back( fileName );
177 else if ( S_ISDIR(buf.st_mode) ) //directory?
179 if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
181 numberOfFiles += Explore( fileName, recursive);
186 //gdcmErrorMacro( "Unexpected error" );
190 if( closedir(dir) != 0 )
192 //gdcmErrorMacro( strerror(errno) );
196 return numberOfFiles;