1 /*=========================================================================
4 Module: $RCSfile: gdcmDirList.cxx,v $
6 Date: $Date: 2005/01/14 23:51:40 $
7 Version: $Revision: 1.38 $
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
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);
56 //-----------------------------------------------------------------------------
63 void DirList::Print(std::ostream &os)
65 std::copy(Filenames.begin(), Filenames.end(),
66 std::ostream_iterator<std::string>(os, "\n"));
69 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
76 * \brief Explore a directory with possibility of recursion
77 * return number of files read
78 * @param dirpath directory to explore
79 * @param recursive whether we want recursion or not
81 int DirList::Explore(std::string const &dirpath, bool recursive)
83 int numberOfFiles = 0;
85 std::string dirName = Util::NormalizePath(dirpath);
87 WIN32_FIND_DATA fileData;
88 HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
90 for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
91 b = FindNextFile(hFile,&fileData))
93 fileName = fileData.cFileName;
94 if( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
96 // Need to check for . and .. to avoid infinite loop
97 if( fileName != "." && fileName != ".." && recursive )
99 numberOfFiles += Explore(dirName+fileName,recursive);
104 Filenames.push_back(dirName+fileName);
108 if (hFile != INVALID_HANDLE_VALUE) FindClose(hFile);
111 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
112 // work on debian for example
114 DIR* dir = opendir(dirName.c_str());
120 // According to POSIX, the dirent structure contains a field char d_name[]
121 // of unspecified size, with at most NAME_MAX characters preceding the
122 // terminating null character. Use of other fields will harm the porta-
123 // bility of your programs.
127 for (d = readdir(dir); d; d = readdir(dir))
129 fileName = dirName + d->d_name;
130 stat(fileName.c_str(), &buf); //really discard output ?
131 if( S_ISREG(buf.st_mode) ) //is it a regular file?
133 Filenames.push_back( fileName );
136 else if( S_ISDIR(buf.st_mode) ) //directory?
138 if( d->d_name[0] != '.' && recursive ) //we are also skipping hidden files
140 numberOfFiles += Explore( fileName, recursive);
145 // we might need to do a different treament
152 return numberOfFiles;
154 } // end namespace gdcm
156 //-----------------------------------------------------------------------------