1 /*=========================================================================
4 Module: $RCSfile: gdcmDirList.cxx,v $
6 Date: $Date: 2007/09/17 12:21:56 $
7 Version: $Revision: 1.63 $
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"
21 #include "gdcmDebug.h"
23 #include "gdcmDicomDirImage.h"
28 #include <sys/stat.h> //stat function
35 #include <sys/types.h>
38 namespace GDCM_NAME_SPACE
40 //-----------------------------------------------------------------------------
41 // Constructor / Destructor
44 * @param dirName root directory name
45 * @param recursive whether we want to explore recursively or not
47 DirList::DirList(std::string const &dirName, bool recursive)
50 Explore(dirName, recursive);
55 * @param se DicomDirSerie we want to explore
57 DirList::DirList(DicomDirSerie *se)
68 //-----------------------------------------------------------------------------
71 * \brief Tells us if file name corresponds to a Directory
72 * @param dirName file name to check
73 * @return true if the file IS a Directory
75 bool DirList::IsDirectory(std::string const &dirName)
78 // std::cout << "dirName[dirName.size()-1] [" << dirName[dirName.size()-1] << "]"
80 //assert( dirName[dirName.size()-1] != GDCM_FILESEPARATOR );
81 if ( stat(dirName.c_str(), &fs) == 0 )
84 return ((fs.st_mode & _S_IFDIR) != 0);
86 return S_ISDIR(fs.st_mode);
91 gdcmStaticErrorMacro( strerror(errno) );
97 * \brief Get the first entry while visiting Filenames
98 * \return The first if found, otherwhise ""
100 std::string DirList::GetFirst()
102 ItDirList = Filenames.begin();
103 if (ItDirList != Filenames.end())
109 * \brief Get the next entry while visiting Filenames
110 * \return The next if found, otherwhise ""
112 std::string DirList::GetNext()
114 gdcmAssertMacro (ItDirList != Filenames.end())
117 if (ItDirList != Filenames.end())
123 //-----------------------------------------------------------------------------
126 //-----------------------------------------------------------------------------
130 * \brief Explores a DicomDirSerie
131 * return number of files found
132 * @param se DicomDirSerie to explore
134 int DirList::Explore(DicomDirSerie *se)
136 int numberOfFiles = 0;
138 DicomDirImage *im = se->GetFirstImage();
141 Filenames.push_back( im->GetEntryString(0x0004, 0x1500) );// File name (Referenced File ID)
143 im = se->GetNextImage();
145 return numberOfFiles;
149 * \brief Explore a directory with possibility of recursion
150 * return number of files read
151 * @param dirpath directory to explore
152 * @param recursive whether we want recursion or not
154 int DirList::Explore(std::string const &dirpath, bool recursive)
156 int numberOfFiles = 0;
157 std::string fileName;
158 std::string dirName = Util::NormalizePath(dirpath);
160 WIN32_FIND_DATA fileData;
161 //assert( dirName[dirName.size()-1] == '' );
162 HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
164 for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
165 b = FindNextFile(hFile, &fileData))
167 fileName = fileData.cFileName;
168 if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
170 // Need to check for . and .. to avoid infinite loop
171 if ( fileName != "." && fileName != ".." && recursive )
173 numberOfFiles += Explore(dirName+fileName,recursive);
178 Filenames.push_back(dirName+fileName);
182 DWORD dwError = GetLastError();
183 if (hFile != INVALID_HANDLE_VALUE)
185 if (dwError != ERROR_NO_MORE_FILES)
188 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
189 FORMAT_MESSAGE_FROM_SYSTEM|
190 FORMAT_MESSAGE_IGNORE_INSERTS,
192 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
193 (LPTSTR) &lpMsgBuf,0,NULL);
195 gdcmErrorMacro("FindNextFile error. Error is " << (char *)lpMsgBuf
196 <<" for the directory : "<<dirName);
201 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
202 // work on debian for example
204 DIR* dir = opendir(dirName.c_str());
210 // According to POSIX, the dirent structure contains a field char d_name[]
211 // of unspecified size, with at most NAME_MAX characters preceeding the
212 // terminating null character. Use of other fields will harm the porta-
213 // bility of your programs.
217 for (d = readdir(dir); d; d = readdir(dir))
219 fileName = dirName + d->d_name;
220 if( stat(fileName.c_str(), &buf) != 0 )
222 gdcmErrorMacro( strerror(errno) );
224 if ( S_ISREG(buf.st_mode) ) //is it a regular file?
226 Filenames.push_back( fileName );
229 else if ( S_ISDIR(buf.st_mode) ) //directory?
231 if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
233 numberOfFiles += Explore( fileName, recursive);
238 gdcmErrorMacro( "Unexpected error" );
242 if( closedir(dir) != 0 )
244 gdcmErrorMacro( strerror(errno) );
248 return numberOfFiles;
251 //-----------------------------------------------------------------------------
254 * \brief Print method
255 * @param os ostream to write to
257 void DirList::Print(std::ostream &os, std::string const &)
259 std::copy(Filenames.begin(), Filenames.end(),
260 std::ostream_iterator<std::string>(os, "\n"));
263 //-----------------------------------------------------------------------------
264 } // end namespace gdcm