1 /*=========================================================================
4 Module: $RCSfile: gdcmDirList.cxx,v $
6 Date: $Date: 2005/02/06 14:43:27 $
7 Version: $Revision: 1.45 $
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)
66 stat(dirName.c_str(), &buf);
67 return S_ISDIR(buf.st_mode);
69 return (GetFileAttributes(dirName.c_str()) & FILE_ATTRIBUTE_DIRECTORY) != 0;
73 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
79 * \brief Explore a directory with possibility of recursion
80 * return number of files read
81 * @param dirpath directory to explore
82 * @param recursive whether we want recursion or not
84 int DirList::Explore(std::string const &dirpath, bool recursive)
86 int numberOfFiles = 0;
88 std::string dirName = Util::NormalizePath(dirpath);
90 WIN32_FIND_DATA fileData;
91 HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
93 for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
94 b = FindNextFile(hFile, &fileData))
96 fileName = fileData.cFileName;
97 if( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
99 // Need to check for . and .. to avoid infinite loop
100 if( fileName != "." && fileName != ".." && recursive )
102 numberOfFiles += Explore(dirName+fileName,recursive);
107 Filenames.push_back(dirName+fileName);
111 if (hFile != INVALID_HANDLE_VALUE) FindClose(hFile);
114 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
115 // work on debian for example
117 DIR* dir = opendir(dirName.c_str());
123 // According to POSIX, the dirent structure contains a field char d_name[]
124 // of unspecified size, with at most NAME_MAX characters preceding the
125 // terminating null character. Use of other fields will harm the porta-
126 // bility of your programs.
130 for (d = readdir(dir); d; d = readdir(dir))
132 fileName = dirName + d->d_name;
133 stat(fileName.c_str(), &buf); //really discard output ?
134 if( S_ISREG(buf.st_mode) ) //is it a regular file?
136 Filenames.push_back( fileName );
139 else if( S_ISDIR(buf.st_mode) ) //directory?
141 if( d->d_name[0] != '.' && recursive ) //we are also skipping hidden files
143 numberOfFiles += Explore( fileName, recursive);
148 // we might need to do a different treament
155 return numberOfFiles;
158 //-----------------------------------------------------------------------------
161 * \brief Print method
162 * @param os ostream to write to
164 void DirList::Print(std::ostream &os)
166 std::copy(Filenames.begin(), Filenames.end(),
167 std::ostream_iterator<std::string>(os, "\n"));
170 //-----------------------------------------------------------------------------
171 } // end namespace gdcm