1 /*=========================================================================
4 Module: $RCSfile: gdcmDirList.cxx,v $
6 Date: $Date: 2004/07/26 19:36:56 $
7 Version: $Revision: 1.18 $
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.htm 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>
37 // Constructor / Destructor
39 * \ingroup gdcmDirList
41 * @param dirName root directory name
42 * @param recursive whether we want to explore recursively or not
44 gdcmDirList::gdcmDirList(std::string dirName, bool recursive)
48 Explore(name, recursive);
52 * \ingroup gdcmDirList
55 gdcmDirList::~gdcmDirList()
59 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
65 * \ingroup gdcmDirList
66 * \brief Get the directory name
67 * @return the directory name
69 std::string gdcmDirList::GetDirName()
74 //-----------------------------------------------------------------------------
77 //-----------------------------------------------------------------------------
81 * \ingroup gdcmDirList
82 * \brief Explore a directory with possibility of recursion
83 * return number of files read
84 * @param dirName directory to explore
85 * @param recursive whether we want recursion or not
87 int gdcmDirList::Explore(std::string dirName, bool recursive)
89 int numberOfFiles = 0;
91 NormalizePath(dirName);
92 #if defined(_MSC_VER) || (__CYGWIN__)
93 WIN32_FIND_DATA fileData;
94 HANDLE hFile=FindFirstFile((dirName+"*").c_str(),&fileData);
97 while( hFile != INVALID_HANDLE_VALUE && found )
99 fileName = fileData.cFileName;
100 if( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
102 // Is the '.' and '..' usefull ?
103 if( fileName != "." && fileName != ".." && recursive )
105 numberOfFiles += Explore(dirName+fileName,recursive);
110 this->push_back(dirName+fileName);
114 found = FindNextFile(hFile,&fileData);
118 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
119 // work on debian for example
121 DIR* dir = opendir(dirName.c_str());
127 // According to POSIX, the dirent structure contains a field char d_name[]
128 // of unspecified size, with at most NAME_MAX characters preceding the
129 // terminating null character. Use of other fields will harm the porta-
130 // bility of your programs.
134 for (d = readdir(dir); d; d = readdir(dir))
136 fileName = dirName + d->d_name;
137 stat(fileName.c_str(), &buf); //really discard output ?
138 if( S_ISREG(buf.st_mode) ) //is it a regular file?
140 this->push_back( fileName );
143 else if( S_ISDIR(buf.st_mode) ) //directory?
145 if( d->d_name[0] != '.' && recursive ) //we are also skipping hidden files
147 numberOfFiles += Explore( fileName, recursive);
152 // we might need to do a different treament
159 return numberOfFiles;
162 //-----------------------------------------------------------------------------