1 /*=========================================================================
4 Module: $RCSfile: gdcmDirList.cxx,v $
6 Date: $Date: 2005/02/01 10:29:55 $
7 Version: $Revision: 1.42 $
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 //-----------------------------------------------------------------------------
59 * @param os ostream to write to
61 void DirList::Print(std::ostream &os)
63 std::copy(Filenames.begin(), Filenames.end(),
64 std::ostream_iterator<std::string>(os, "\n"));
67 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
73 * \brief Explore a directory with possibility of recursion
74 * return number of files read
75 * @param dirpath directory to explore
76 * @param recursive whether we want recursion or not
78 int DirList::Explore(std::string const &dirpath, bool recursive)
80 int numberOfFiles = 0;
82 std::string dirName = Util::NormalizePath(dirpath);
84 WIN32_FIND_DATA fileData;
85 HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
87 for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
88 b = FindNextFile(hFile,&fileData))
90 fileName = fileData.cFileName;
91 if( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
93 // Need to check for . and .. to avoid infinite loop
94 if( fileName != "." && fileName != ".." && recursive )
96 numberOfFiles += Explore(dirName+fileName,recursive);
101 Filenames.push_back(dirName+fileName);
105 if (hFile != INVALID_HANDLE_VALUE) FindClose(hFile);
108 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
109 // work on debian for example
111 DIR* dir = opendir(dirName.c_str());
117 // According to POSIX, the dirent structure contains a field char d_name[]
118 // of unspecified size, with at most NAME_MAX characters preceding the
119 // terminating null character. Use of other fields will harm the porta-
120 // bility of your programs.
124 for (d = readdir(dir); d; d = readdir(dir))
126 fileName = dirName + d->d_name;
127 stat(fileName.c_str(), &buf); //really discard output ?
128 if( S_ISREG(buf.st_mode) ) //is it a regular file?
130 Filenames.push_back( fileName );
133 else if( S_ISDIR(buf.st_mode) ) //directory?
135 if( d->d_name[0] != '.' && recursive ) //we are also skipping hidden files
137 numberOfFiles += Explore( fileName, recursive);
142 // we might need to do a different treament
149 return numberOfFiles;
152 bool DirList::IsDirectory(std::string const &dirName)
156 stat(dirName.c_str(), &buf);
157 return S_ISDIR(buf.st_mode);
159 return (GetFileAttributes(dirName.c_str()) & FILE_ATTRIBUTE_DIRECTORY) != 0;
163 //-----------------------------------------------------------------------------
166 //-----------------------------------------------------------------------------
167 } // end namespace gdcm