1 /*=========================================================================
4 Module: $RCSfile: gdcmDirList.cxx,v $
6 Date: $Date: 2009/05/26 10:27:38 $
7 Version: $Revision: 1.68 $
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, bool all)
50 Explore(dirName, recursive, all);
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 gdcmStaticWarningMacro("[" << dirName << "] not found or is not a Directory");
92 //gdcmStaticErrorMacro( strerror(errno) );
98 * \brief Get the first entry while visiting Filenames
99 * \return The first if found, otherwhise ""
101 std::string DirList::GetFirst()
103 ItDirList = Filenames.begin();
104 if (ItDirList != Filenames.end())
110 * \brief Get the next entry while visiting Filenames
111 * \return The next if found, otherwhise ""
113 std::string DirList::GetNext()
115 gdcmAssertMacro (ItDirList != Filenames.end())
118 if (ItDirList != Filenames.end())
124 //-----------------------------------------------------------------------------
127 //-----------------------------------------------------------------------------
131 * \brief Explores a DicomDirSerie
132 * return number of files found
133 * @param se DicomDirSerie to explore
135 int DirList::Explore(DicomDirSerie *se)
137 int numberOfFiles = 0;
139 DicomDirImage *im = se->GetFirstImage();
142 Filenames.push_back( im->GetEntryString(0x0004, 0x1500) );// File name (Referenced File ID)
144 im = se->GetNextImage();
146 return numberOfFiles;
150 * \brief Explore a directory with possibility of recursion
151 * return number of files read
152 * @param dirpath directory to explore
153 * @param recursive whether we want recursion or not
154 * @param all whether we want all (i.e; File names + Directory names) default=false
156 int DirList::Explore(std::string const &dirpath, bool recursive, bool all)
158 int numberOfFiles = 0;
159 std::string fileName;
160 std::string dirName = Util::NormalizePath(dirpath);
162 WIN32_FIND_DATA fileData;
163 //assert( dirName[dirName.size()-1] == '' );
164 HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
166 for(BOOL b = (hFile != INVALID_HANDLE_VALUE);
168 b = FindNextFile(hFile, &fileData))
170 fileName = dirName + fileData.cFileName;
171 // avoid infinite loop!
172 if ( GDCM_NAME_SPACE::Util::GetName(fileName) == "." || GDCM_NAME_SPACE::Util::GetName(fileName) == "..")
175 if (all) // meaningfull only when recursive=false
177 Filenames.push_back( fileName );
182 if ( !( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) ) //is it a regular file?
184 Filenames.push_back( fileName );
188 else if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) //directory?
190 if ( GDCM_NAME_SPACE::Util::GetName(fileName)[0] != '.' && recursive ) //we also skip hidden files
192 numberOfFiles += Explore( fileName, recursive);
197 gdcmErrorMacro( "Unexpected error" );
203 DWORD dwError = GetLastError();
204 if (hFile != INVALID_HANDLE_VALUE)
206 if (dwError != ERROR_NO_MORE_FILES)
209 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
210 FORMAT_MESSAGE_FROM_SYSTEM|
211 FORMAT_MESSAGE_IGNORE_INSERTS,
213 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
214 (LPTSTR) &lpMsgBuf,0,NULL);
216 gdcmErrorMacro("FindNextFile error. Error is " << (char *)lpMsgBuf
217 <<" for the directory : "<<dirName);
222 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
223 // work on debian for example
225 DIR* dir = opendir(dirName.c_str());
231 // According to POSIX, the dirent structure contains a field char d_name[]
232 // of unspecified size, with at most NAME_MAX characters preceeding the
233 // terminating null character. Use of other fields will harm the porta-
234 // bility of your programs.
238 for (d = readdir(dir); d; d = readdir(dir))
240 fileName = dirName + d->d_name;
241 //std::cout << d->d_name << std::endl;
242 // avoid infinite loop!
243 if ( GDCM_NAME_SPACE::Util::GetName(d->d_name) == "." || GDCM_NAME_SPACE::Util::GetName(d->d_name) == "..")
246 if( stat(fileName.c_str(), &buf) != 0 )
248 gdcmErrorMacro( strerror(errno) );
251 if (all) // meaningfull only when recursive=false
253 Filenames.push_back( fileName );
258 if ( S_ISREG(buf.st_mode) ) //is it a regular file?
260 Filenames.push_back( fileName );
263 else if ( S_ISDIR(buf.st_mode) ) //directory?
265 if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
267 numberOfFiles += Explore( fileName, recursive);
272 gdcmErrorMacro( "Unexpected error" );
278 if( closedir(dir) != 0 )
280 gdcmErrorMacro( strerror(errno) );
284 return numberOfFiles;
287 //-----------------------------------------------------------------------------
290 * \brief Print method
291 * @param os ostream to write to
293 void DirList::Print(std::ostream &os, std::string const &)
295 std::copy(Filenames.begin(), Filenames.end(),
296 std::ostream_iterator<std::string>(os, "\n"));
299 //-----------------------------------------------------------------------------
300 } // end namespace gdcm