1 /*=========================================================================
4 Module: $RCSfile: gdcmDirList.cxx,v $
6 Date: $Date: 2005/05/11 14:40:57 $
7 Version: $Revision: 1.50 $
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"
24 #include <sys/stat.h> //stat function
31 #include <sys/types.h>
36 //-----------------------------------------------------------------------------
37 // Constructor / Destructor
40 * @param dirName root directory name
41 * @param recursive whether we want to explore recursively or not
43 DirList::DirList(std::string const &dirName, bool recursive)
46 Explore(dirName, recursive);
56 //-----------------------------------------------------------------------------
59 * \brief Tells us if file name corresponds to a Directory
60 * @param dirName file name to check
61 * @return true if the file IS a Directory
63 bool DirList::IsDirectory(std::string const &dirName)
66 assert( dirName[dirName.size()-1] != '/' );
67 if(stat(dirName.c_str(), &fs) == 0)
70 return ((fs.st_mode & _S_IFDIR) != 0);
72 return S_ISDIR(fs.st_mode);
81 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
87 * \brief Explore a directory with possibility of recursion
88 * return number of files read
89 * @param dirpath directory to explore
90 * @param recursive whether we want recursion or not
92 int DirList::Explore(std::string const &dirpath, bool recursive)
94 int numberOfFiles = 0;
96 std::string dirName = Util::NormalizePath(dirpath);
98 WIN32_FIND_DATA fileData;
99 HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
101 for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
102 b = FindNextFile(hFile, &fileData))
104 fileName = fileData.cFileName;
105 if( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
107 // Need to check for . and .. to avoid infinite loop
108 if( fileName != "." && fileName != ".." && recursive )
110 numberOfFiles += Explore(dirName+fileName,recursive);
115 Filenames.push_back(dirName+fileName);
119 if (hFile != INVALID_HANDLE_VALUE) FindClose(hFile);
122 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
123 // work on debian for example
125 DIR* dir = opendir(dirName.c_str());
131 // According to POSIX, the dirent structure contains a field char d_name[]
132 // of unspecified size, with at most NAME_MAX characters preceding the
133 // terminating null character. Use of other fields will harm the porta-
134 // bility of your programs.
138 for (d = readdir(dir); d; d = readdir(dir))
140 fileName = dirName + d->d_name;
141 stat(fileName.c_str(), &buf); //really discard output ?
142 if( S_ISREG(buf.st_mode) ) //is it a regular file?
144 Filenames.push_back( fileName );
147 else if( S_ISDIR(buf.st_mode) ) //directory?
149 if( d->d_name[0] != '.' && recursive ) //we are also skipping hidden files
151 numberOfFiles += Explore( fileName, recursive);
156 // we might need to do a different treament
163 return numberOfFiles;
166 //-----------------------------------------------------------------------------
169 * \brief Print method
170 * @param os ostream to write to
172 void DirList::Print(std::ostream &os)
174 std::copy(Filenames.begin(), Filenames.end(),
175 std::ostream_iterator<std::string>(os, "\n"));
178 //-----------------------------------------------------------------------------
179 } // end namespace gdcm