1 /*=========================================================================
4 Module: $RCSfile: gdcmDirList.cxx,v $
6 Date: $Date: 2007/06/08 12:49:37 $
7 Version: $Revision: 1.62 $
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 dirName root directory name
56 * @param recursive whether we want to explore recursively or not
58 DirList::DirList(DicomDirSerie *se)
69 //-----------------------------------------------------------------------------
72 * \brief Tells us if file name corresponds to a Directory
73 * @param dirName file name to check
74 * @return true if the file IS a Directory
76 bool DirList::IsDirectory(std::string const &dirName)
79 // std::cout << "dirName[dirName.size()-1] [" << dirName[dirName.size()-1] << "]"
81 //assert( dirName[dirName.size()-1] != GDCM_FILESEPARATOR );
82 if ( stat(dirName.c_str(), &fs) == 0 )
85 return ((fs.st_mode & _S_IFDIR) != 0);
87 return S_ISDIR(fs.st_mode);
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
155 int DirList::Explore(std::string const &dirpath, bool recursive)
157 int numberOfFiles = 0;
158 std::string fileName;
159 std::string dirName = Util::NormalizePath(dirpath);
161 WIN32_FIND_DATA fileData;
162 //assert( dirName[dirName.size()-1] == '' );
163 HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
165 for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
166 b = FindNextFile(hFile, &fileData))
168 fileName = fileData.cFileName;
169 if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
171 // Need to check for . and .. to avoid infinite loop
172 if ( fileName != "." && fileName != ".." && recursive )
174 numberOfFiles += Explore(dirName+fileName,recursive);
179 Filenames.push_back(dirName+fileName);
183 DWORD dwError = GetLastError();
184 if (hFile != INVALID_HANDLE_VALUE)
186 if (dwError != ERROR_NO_MORE_FILES)
189 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
190 FORMAT_MESSAGE_FROM_SYSTEM|
191 FORMAT_MESSAGE_IGNORE_INSERTS,
193 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
194 (LPTSTR) &lpMsgBuf,0,NULL);
196 gdcmErrorMacro("FindNextFile error. Error is " << (char *)lpMsgBuf
197 <<" for the directory : "<<dirName);
202 // Real POSIX implementation: scandir is a BSD extension only, and doesn't
203 // work on debian for example
205 DIR* dir = opendir(dirName.c_str());
211 // According to POSIX, the dirent structure contains a field char d_name[]
212 // of unspecified size, with at most NAME_MAX characters preceeding the
213 // terminating null character. Use of other fields will harm the porta-
214 // bility of your programs.
218 for (d = readdir(dir); d; d = readdir(dir))
220 fileName = dirName + d->d_name;
221 if( stat(fileName.c_str(), &buf) != 0 )
223 gdcmErrorMacro( strerror(errno) );
225 if ( S_ISREG(buf.st_mode) ) //is it a regular file?
227 Filenames.push_back( fileName );
230 else if ( S_ISDIR(buf.st_mode) ) //directory?
232 if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
234 numberOfFiles += Explore( fileName, recursive);
239 gdcmErrorMacro( "Unexpected error" );
243 if( closedir(dir) != 0 )
245 gdcmErrorMacro( strerror(errno) );
249 return numberOfFiles;
252 //-----------------------------------------------------------------------------
255 * \brief Print method
256 * @param os ostream to write to
258 void DirList::Print(std::ostream &os, std::string const &)
260 std::copy(Filenames.begin(), Filenames.end(),
261 std::ostream_iterator<std::string>(os, "\n"));
264 //-----------------------------------------------------------------------------
265 } // end namespace gdcm