1 /*=========================================================================
4 Module: $RCSfile: gdcmDirList.cxx,v $
6 Date: $Date: 2005/01/18 08:01:41 $
7 Version: $Revision: 1.39 $
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 // Constructor / Destructor
38 * @param dirName root directory name
39 * @param recursive whether we want to explore recursively or not
41 DirList::DirList(std::string const &dirName, bool recursive)
44 Explore(dirName, recursive);
54 //-----------------------------------------------------------------------------
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 //-----------------------------------------------------------------------------
74 * \brief Explore a directory with possibility of recursion
75 * return number of files read
76 * @param dirpath directory to explore
77 * @param recursive whether we want recursion or not
79 int DirList::Explore(std::string const &dirpath, bool recursive)
81 int numberOfFiles = 0;
83 std::string dirName = Util::NormalizePath(dirpath);
85 WIN32_FIND_DATA fileData;
86 HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
88 for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
89 b = FindNextFile(hFile,&fileData))
91 fileName = fileData.cFileName;
92 if( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
94 // Need to check for . and .. to avoid infinite loop
95 if( fileName != "." && fileName != ".." && recursive )
97 numberOfFiles += Explore(dirName+fileName,recursive);
102 Filenames.push_back(dirName+fileName);
106 if (hFile != INVALID_HANDLE_VALUE) FindClose(hFile);
109 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
110 // work on debian for example
112 DIR* dir = opendir(dirName.c_str());
118 // According to POSIX, the dirent structure contains a field char d_name[]
119 // of unspecified size, with at most NAME_MAX characters preceding the
120 // terminating null character. Use of other fields will harm the porta-
121 // bility of your programs.
125 for (d = readdir(dir); d; d = readdir(dir))
127 fileName = dirName + d->d_name;
128 stat(fileName.c_str(), &buf); //really discard output ?
129 if( S_ISREG(buf.st_mode) ) //is it a regular file?
131 Filenames.push_back( fileName );
134 else if( S_ISDIR(buf.st_mode) ) //directory?
136 if( d->d_name[0] != '.' && recursive ) //we are also skipping hidden files
138 numberOfFiles += Explore( fileName, recursive);
143 // we might need to do a different treament
150 return numberOfFiles;
152 } // end namespace gdcm
154 //-----------------------------------------------------------------------------