1 /*=========================================================================
4 Module: $RCSfile: gdcmDirList.cxx,v $
6 Date: $Date: 2006/05/12 09:36:09 $
7 Version: $Revision: 1.59 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
19 #include "gdcmDirList.h"
21 #include "gdcmDebug.h"
26 #include <sys/stat.h> //stat function
33 #include <sys/types.h>
38 //-----------------------------------------------------------------------------
39 // Constructor / Destructor
42 * @param dirName root directory name
43 * @param recursive whether we want to explore recursively or not
45 DirList::DirList(std::string const &dirName, bool recursive)
48 Explore(dirName, recursive);
58 //-----------------------------------------------------------------------------
61 * \brief Tells us if file name corresponds to a Directory
62 * @param dirName file name to check
63 * @return true if the file IS a Directory
65 bool DirList::IsDirectory(std::string const &dirName)
68 // std::cout << "dirName[dirName.size()-1] [" << dirName[dirName.size()-1] << "]"
70 //assert( dirName[dirName.size()-1] != GDCM_FILESEPARATOR );
71 if ( stat(dirName.c_str(), &fs) == 0 )
74 return ((fs.st_mode & _S_IFDIR) != 0);
76 return S_ISDIR(fs.st_mode);
81 gdcmStaticErrorMacro( strerror(errno) );
86 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
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 = Util::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 gdcmErrorMacro("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))
162 fileName = dirName + d->d_name;
163 if( stat(fileName.c_str(), &buf) != 0 )
165 gdcmErrorMacro( strerror(errno) );
167 if ( S_ISREG(buf.st_mode) ) //is it a regular file?
169 Filenames.push_back( fileName );
172 else if ( S_ISDIR(buf.st_mode) ) //directory?
174 if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
176 numberOfFiles += Explore( fileName, recursive);
181 gdcmErrorMacro( "Unexpected error" );
185 if( closedir(dir) != 0 )
187 gdcmErrorMacro( strerror(errno) );
191 return numberOfFiles;
194 //-----------------------------------------------------------------------------
197 * \brief Print method
198 * @param os ostream to write to
200 void DirList::Print(std::ostream &os, std::string const &)
202 std::copy(Filenames.begin(), Filenames.end(),
203 std::ostream_iterator<std::string>(os, "\n"));
206 //-----------------------------------------------------------------------------
207 } // end namespace gdcm