1 /*=========================================================================
4 Module: $RCSfile: gdcmDirList.cxx,v $
6 Date: $Date: 2005/04/15 21:21:42 $
7 Version: $Revision: 1.47 $
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"
29 #include <sys/types.h>
35 //-----------------------------------------------------------------------------
36 // Constructor / Destructor
39 * @param dirName root directory name
40 * @param recursive whether we want to explore recursively or not
42 DirList::DirList(std::string const &dirName, bool recursive)
45 Explore(dirName, recursive);
55 //-----------------------------------------------------------------------------
58 * \brief Tells us if file name corresponds to a Directory
59 * @param dirName file name to check
60 * @return true if the file IS a Directory
62 bool DirList::IsDirectory(std::string const &dirName)
65 if(stat(dirName.c_str(), &fs) == 0)
68 return ((fs.st_mode & _S_IFDIR) != 0);
70 return S_ISDIR(fs.st_mode);
79 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
85 * \brief Explore a directory with possibility of recursion
86 * return number of files read
87 * @param dirpath directory to explore
88 * @param recursive whether we want recursion or not
90 int DirList::Explore(std::string const &dirpath, bool recursive)
92 int numberOfFiles = 0;
94 std::string dirName = Util::NormalizePath(dirpath);
96 WIN32_FIND_DATA fileData;
97 HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
99 for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
100 b = FindNextFile(hFile, &fileData))
102 fileName = fileData.cFileName;
103 if( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
105 // Need to check for . and .. to avoid infinite loop
106 if( fileName != "." && fileName != ".." && recursive )
108 numberOfFiles += Explore(dirName+fileName,recursive);
113 Filenames.push_back(dirName+fileName);
117 if (hFile != INVALID_HANDLE_VALUE) FindClose(hFile);
120 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
121 // work on debian for example
123 DIR* dir = opendir(dirName.c_str());
129 // According to POSIX, the dirent structure contains a field char d_name[]
130 // of unspecified size, with at most NAME_MAX characters preceding the
131 // terminating null character. Use of other fields will harm the porta-
132 // bility of your programs.
136 for (d = readdir(dir); d; d = readdir(dir))
138 fileName = dirName + d->d_name;
139 stat(fileName.c_str(), &buf); //really discard output ?
140 if( S_ISREG(buf.st_mode) ) //is it a regular file?
142 Filenames.push_back( fileName );
145 else if( S_ISDIR(buf.st_mode) ) //directory?
147 if( d->d_name[0] != '.' && recursive ) //we are also skipping hidden files
149 numberOfFiles += Explore( fileName, recursive);
154 // we might need to do a different treament
161 return numberOfFiles;
164 //-----------------------------------------------------------------------------
167 * \brief Print method
168 * @param os ostream to write to
170 void DirList::Print(std::ostream &os)
172 std::copy(Filenames.begin(), Filenames.end(),
173 std::ostream_iterator<std::string>(os, "\n"));
176 //-----------------------------------------------------------------------------
177 } // end namespace gdcm