1 /*=========================================================================
4 Module: $RCSfile: gdcmDirList.cxx,v $
6 Date: $Date: 2004/10/12 04:35:45 $
7 Version: $Revision: 1.23 $
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 (__CYGWIN__)
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 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 DirList::GetDirName()
76 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
84 * \brief Explore a directory with possibility of recursion
85 * return number of files read
86 * @param dirName directory to explore
87 * @param recursive whether we want recursion or not
89 int DirList::Explore(std::string dirName, bool recursive)
91 int numberOfFiles = 0;
93 Util::NormalizePath(dirName);
94 #if defined(_MSC_VER) || (__CYGWIN__)
95 WIN32_FIND_DATA fileData;
96 HANDLE hFile=FindFirstFile((dirName+"*").c_str(),&fileData);
99 while( hFile != INVALID_HANDLE_VALUE && found )
101 fileName = fileData.cFileName;
102 if( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
104 // Is the '.' and '..' usefull ?
105 if( fileName != "." && fileName != ".." && recursive )
107 numberOfFiles += Explore(dirName+fileName,recursive);
112 push_back(dirName+fileName);
116 found = FindNextFile(hFile,&fileData);
120 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
121 // work on debian for example
123 DIR* dir = opendir(dirName.c_str());
129 // According to POSIX, the dirent structure contains a field char d_name[]
130 // of unspecified size, with at most NAME_MAX characters preceding the
131 // terminating null character. Use of other fields will harm the porta-
132 // bility of your programs.
136 for (d = readdir(dir); d; d = readdir(dir))
138 fileName = dirName + d->d_name;
139 stat(fileName.c_str(), &buf); //really discard output ?
140 if( S_ISREG(buf.st_mode) ) //is it a regular file?
142 push_back( fileName );
145 else if( S_ISDIR(buf.st_mode) ) //directory?
147 if( d->d_name[0] != '.' && recursive ) //we are also skipping hidden files
149 numberOfFiles += Explore( fileName, recursive);
154 // we might need to do a different treament
161 return numberOfFiles;
163 } // end namespace gdcm
165 //-----------------------------------------------------------------------------