]> Creatis software - gdcm.git/blob - src/gdcmDirList.cxx
To please python :
[gdcm.git] / src / gdcmDirList.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDirList.cxx,v $
5   Language:  C++
6   Date:      $Date: 2006/11/15 15:53:08 $
7   Version:   $Revision: 1.60 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmDirList.h"
20 #include "gdcmUtil.h"
21 #include "gdcmDebug.h"
22
23 #include <iterator>
24 #include <assert.h>
25 #include <errno.h>
26 #include <sys/stat.h>  //stat function
27
28 #ifdef _MSC_VER
29    #include <windows.h> 
30    #include <direct.h>
31 #else
32    #include <dirent.h>   
33    #include <sys/types.h>
34 #endif
35
36 namespace gdcm
37 {
38 //-----------------------------------------------------------------------------
39 // Constructor / Destructor
40 /**
41  * \brief Constructor  
42  * @param  dirName root directory name
43  * @param  recursive whether we want to explore recursively or not 
44  */
45 DirList::DirList(std::string const &dirName, bool recursive)
46 {
47    DirName = dirName;
48    Explore(dirName, recursive);
49 }
50
51 /**
52  * \brief  Destructor
53  */
54 DirList::~DirList()
55 {
56 }
57
58 //-----------------------------------------------------------------------------
59 // Public
60 /**
61  * \brief Tells us if file name corresponds to a Directory   
62  * @param  dirName file name to check
63  * @return true if the file IS a Directory
64  */
65 bool DirList::IsDirectory(std::string const &dirName)
66 {
67    struct stat fs;
68    // std::cout << "dirName[dirName.size()-1] [" << dirName[dirName.size()-1] << "]"
69    //           << std::endl;
70    //assert( dirName[dirName.size()-1] != GDCM_FILESEPARATOR );
71    if ( stat(dirName.c_str(), &fs) == 0 )
72    {
73 #if _WIN32
74       return ((fs.st_mode & _S_IFDIR) != 0);
75 #else
76       return S_ISDIR(fs.st_mode);
77 #endif
78    }
79    else
80    {
81       gdcmStaticErrorMacro( strerror(errno) );
82       return false;
83    }
84 }
85
86 /**
87  * \brief   Get the first entry while visiting Filenames
88  * \return  The first  if found, otherwhise ""
89  */ 
90 std::string DirList::GetFirst()
91 {
92    ItDirList = Filenames.begin();
93    if (ItDirList != Filenames.end())
94       return *ItDirList;
95    return "";
96
97
98 /**
99  * \brief   Get the next entry while visiting Filenames
100  * \return  The next  if found, otherwhise ""
101  */ 
102 std::string DirList::GetNext()
103 {
104    gdcmAssertMacro (ItDirList != Filenames.end())
105    {
106       ++ItDirList;
107       if (ItDirList != Filenames.end())
108          return *ItDirList;      
109    }
110    return "";
111
112
113 //-----------------------------------------------------------------------------
114 // Protected
115
116 //-----------------------------------------------------------------------------
117 // Private
118 /**
119  * \brief   Explore a directory with possibility of recursion
120  *          return number of files read
121  * @param  dirpath   directory to explore
122  * @param  recursive whether we want recursion or not
123  */
124 int DirList::Explore(std::string const &dirpath, bool recursive)
125 {
126    int numberOfFiles = 0;
127    std::string fileName;
128    std::string dirName = Util::NormalizePath(dirpath);
129 #ifdef _MSC_VER
130    WIN32_FIND_DATA fileData;
131    //assert( dirName[dirName.size()-1] == '' );
132    HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
133
134    for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
135        b = FindNextFile(hFile, &fileData))
136    {
137       fileName = fileData.cFileName;
138       if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
139       {
140          // Need to check for . and .. to avoid infinite loop
141          if ( fileName != "." && fileName != ".." && recursive )
142          {
143             numberOfFiles += Explore(dirName+fileName,recursive);
144          }
145       }
146       else
147       {
148          Filenames.push_back(dirName+fileName);
149          numberOfFiles++;
150       }
151    }
152    DWORD dwError = GetLastError();
153    if (hFile != INVALID_HANDLE_VALUE) 
154       FindClose(hFile);
155    if (dwError != ERROR_NO_MORE_FILES) 
156    {
157       LPVOID lpMsgBuf;
158       FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
159                     FORMAT_MESSAGE_FROM_SYSTEM|
160                     FORMAT_MESSAGE_IGNORE_INSERTS,
161                     NULL,GetLastError(),
162                     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
163                     (LPTSTR) &lpMsgBuf,0,NULL);
164
165       gdcmErrorMacro("FindNextFile error. Error is " << (char *)lpMsgBuf
166                    <<" for the directory : "<<dirName);
167       return -1;
168    }
169
170 #else
171   // Real POSIX implementation: scandir is a BSD extension only, and doesn't 
172   // work on debian for example
173
174    DIR* dir = opendir(dirName.c_str());
175    if (!dir)
176    {
177       return 0;
178    }
179
180    // According to POSIX, the dirent structure contains a field char d_name[]
181    // of unspecified size, with at most NAME_MAX characters preceeding the
182    // terminating null character. Use of other fields will harm the  porta-
183    // bility of your programs.
184
185    struct stat buf;
186    dirent *d;
187    for (d = readdir(dir); d; d = readdir(dir))
188    {
189       fileName = dirName + d->d_name;
190       if( stat(fileName.c_str(), &buf) != 0 )
191       {
192          gdcmErrorMacro( strerror(errno) );
193       }
194       if ( S_ISREG(buf.st_mode) )    //is it a regular file?
195       {
196          Filenames.push_back( fileName );
197          numberOfFiles++;
198       }
199       else if ( S_ISDIR(buf.st_mode) ) //directory?
200       {
201          if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
202          {
203             numberOfFiles += Explore( fileName, recursive);
204          }
205       }
206       else
207       {
208          gdcmErrorMacro( "Unexpected error" );
209          return -1;
210       }
211    }
212    if( closedir(dir) != 0 )
213    {
214       gdcmErrorMacro( strerror(errno) );
215    }
216 #endif
217
218   return numberOfFiles;
219 }
220
221 //-----------------------------------------------------------------------------
222 // Print
223 /**
224  * \brief   Print method
225  * @param os ostream to write to 
226  */
227 void DirList::Print(std::ostream &os, std::string const &)
228 {
229    std::copy(Filenames.begin(), Filenames.end(), 
230              std::ostream_iterator<std::string>(os, "\n"));
231 }
232
233 //-----------------------------------------------------------------------------
234 } // end namespace gdcm