2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
8 # This software is governed by the CeCILL-B license under French law and
9 # abiding by the rules of distribution of free software. You can use,
10 # modify and/ or redistribute the software under the terms of the CeCILL-B
11 # license as circulated by CEA, CNRS and INRIA at the following URL
12 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
13 # or in the file LICENSE.txt.
15 # As a counterpart to the access to the source code and rights to copy,
16 # modify and redistribute granted by the license, users are provided only
17 # with a limited warranty and the software's author, the holder of the
18 # economic rights, and the successive licensors have only limited
21 # The fact that you are presently reading this means that you have had
22 # knowledge of the CeCILL-B license and that you accept its terms.
23 # ------------------------------------------------------------------------ */
25 /*=========================================================================
28 Module: $RCSfile: creaFilesFromDirectory.cxx,v $
30 Date: $Date: 2012/11/15 09:07:32 $
31 Version: $Revision: 1.6 $
34 This software is distributed WITHOUT ANY WARRANTY; without even
35 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
36 PURPOSE. See the above copyright notices for more information.
38 =========================================================================*/
43 * \brief class Exception:generic class for throwing any exception (header)
52 #include <sys/types.h>
55 #include <sys/stat.h> //stat function
57 #include "creaFilesFromDirectory.h"
58 #include "creaMessageManager.h"
64 * \brief Add a SEPARATOR to the end of the name if necessary
65 * @param pathname file/directory name to normalize
67 std::string DirList::NormalizePath(std::string const &pathname)
72 const char FILESEPARATOR = '\\';
74 const char FILESEPARATOR = '/';
78 std::string name = pathname;
79 int size = name.size();
81 // if ( name[size-1] != SEPARATOR_X && name[size-1] != SEPARATOR_WIN )
82 if ( name[size-1] != FILESEPARATOR )
84 name += FILESEPARATOR;
89 * \brief Explore a directory with possibility of recursion
90 * return number of files read
91 * @param dirpath directory to explore
92 * @param recursive whether we want recursion or not
94 int DirList::Explore(std::string const &dirpath, bool recursive)
96 int numberOfFiles = 0;
98 std::string dirName = NormalizePath(dirpath);
100 WIN32_FIND_DATA fileData;
101 //assert( dirName[dirName.size()-1] == '' );
102 HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
104 for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
105 b = FindNextFile(hFile, &fileData))
107 fileName = fileData.cFileName;
108 if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
110 // Need to check for . and .. to avoid infinite loop
111 if ( fileName != "." && fileName != ".." && recursive )
113 numberOfFiles += Explore(dirName+fileName,recursive);
118 Filenames.push_back(dirName+fileName);
122 DWORD dwError = GetLastError();
123 if (hFile != INVALID_HANDLE_VALUE)
125 if (dwError != ERROR_NO_MORE_FILES)
128 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
129 FORMAT_MESSAGE_FROM_SYSTEM|
130 FORMAT_MESSAGE_IGNORE_INSERTS,
132 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
133 (LPTSTR) &lpMsgBuf,0,NULL);
135 creaError("FindNextFile error. Error is " << (char *)lpMsgBuf
136 <<" for the directory : "<<dirName);
141 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
142 // work on debian for example
144 DIR* dir = opendir(dirName.c_str());
150 // According to POSIX, the dirent structure contains a field char d_name[]
151 // of unspecified size, with at most NAME_MAX characters preceeding the
152 // terminating null character. Use of other fields will harm the porta-
153 // bility of your programs.
157 for (d = readdir(dir); d; d = readdir(dir))
160 fileName = dirName + d->d_name;
161 if( stat(fileName.c_str(), &buf) != 0 )
163 //gdcmErrorMacro( strerror(errno) );
165 if ( S_ISREG(buf.st_mode) ) //is it a regular file?
167 //printf("EED DirList::Explore [%s]\n" , d->d_name );
168 if ( d->d_name[0]!='.')
170 Filenames.push_back( fileName );
174 else if ( S_ISDIR(buf.st_mode) ) //directory?
176 if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
178 numberOfFiles += Explore( fileName, recursive);
183 //gdcmErrorMacro( "Unexpected error" );
187 if( closedir(dir) != 0 )
189 //gdcmErrorMacro( strerror(errno) );
193 return numberOfFiles;