1 /*=========================================================================
4 Module: $RCSfile: gdcmDirList.cxx,v $
6 Date: $Date: 2004/12/12 13:32:23 $
7 Version: $Revision: 1.29 $
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"
25 #if defined(_MSC_VER) || defined(__BORLANDC__)
34 #include <sys/types.h>
39 // Constructor / Destructor
43 * @param dirName root directory name
44 * @param recursive whether we want to explore recursively or not
46 DirList::DirList(std::string const & dirName, bool recursive)
49 Util::NormalizePath(name);
50 Explore(name, recursive);
61 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
68 * \brief Get the directory name
69 * @return the directory name
71 std::string const & DirList::GetDirName() const
76 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
83 * \brief Explore a directory with possibility of recursion
84 * return number of files read
85 * @param dirpath directory to explore
86 * @param recursive whether we want recursion or not
88 int DirList::Explore(std::string const & dirpath, bool recursive)
90 int numberOfFiles = 0;
92 std::string dirName = Util::NormalizePath(dirpath);
93 #if defined(_MSC_VER) || defined(__BORLANDC__)
94 WIN32_FIND_DATA fileData;
95 HANDLE hFile=FindFirstFile((dirName+"*").c_str(),&fileData);
98 while( hFile != INVALID_HANDLE_VALUE && found )
100 fileName = fileData.cFileName;
101 if( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
103 // Is the '.' and '..' usefull ?
104 if( fileName != "." && fileName != ".." && recursive )
106 numberOfFiles += Explore(dirName+fileName,recursive);
111 push_back(dirName+fileName);
115 found = FindNextFile(hFile,&fileData);
119 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
120 // work on debian for example
122 DIR* dir = opendir(dirName.c_str());
128 // According to POSIX, the dirent structure contains a field char d_name[]
129 // of unspecified size, with at most NAME_MAX characters preceding the
130 // terminating null character. Use of other fields will harm the porta-
131 // bility of your programs.
135 for (d = readdir(dir); d; d = readdir(dir))
137 fileName = dirName + d->d_name;
138 stat(fileName.c_str(), &buf); //really discard output ?
139 if( S_ISREG(buf.st_mode) ) //is it a regular file?
141 push_back( fileName );
144 else if( S_ISDIR(buf.st_mode) ) //directory?
146 if( d->d_name[0] != '.' && recursive ) //we are also skipping hidden files
148 numberOfFiles += Explore( fileName, recursive);
153 // we might need to do a different treament
160 return numberOfFiles;
162 } // end namespace gdcm
164 //-----------------------------------------------------------------------------