]> Creatis software - gdcm.git/blob - src/gdcmDirList.cxx
To prepare use of progression bar in all types of Document
[gdcm.git] / src / gdcmDirList.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDirList.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/11/29 17:21:34 $
7   Version:   $Revision: 1.56 $
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    assert( dirName[dirName.size()-1] != '/' );
69    if ( stat(dirName.c_str(), &fs) == 0 )
70    {
71 #if _WIN32
72       return ((fs.st_mode & _S_IFDIR) != 0);
73 #else
74       return S_ISDIR(fs.st_mode);
75 #endif
76    }
77    else
78    {
79       const char *str = strerror(errno);
80       gdcmStaticErrorMacro( str );
81       return false;
82    }
83 }
84
85 //-----------------------------------------------------------------------------
86 // Protected
87
88 //-----------------------------------------------------------------------------
89 // Private
90 /**
91  * \brief   Explore a directory with possibility of recursion
92  *          return number of files read
93  * @param  dirpath   directory to explore
94  * @param  recursive whether we want recursion or not
95  */
96 int DirList::Explore(std::string const &dirpath, bool recursive)
97 {
98    int numberOfFiles = 0;
99    std::string fileName;
100    std::string dirName = Util::NormalizePath(dirpath);
101 #ifdef _MSC_VER
102    WIN32_FIND_DATA fileData;
103    assert( dirName[dirName.size()-1] == '/' );
104    HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
105
106    for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
107        b = FindNextFile(hFile, &fileData))
108    {
109       fileName = fileData.cFileName;
110       if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
111       {
112          // Need to check for . and .. to avoid infinite loop
113          if ( fileName != "." && fileName != ".." && recursive )
114          {
115             numberOfFiles += Explore(dirName+fileName,recursive);
116          }
117       }
118       else
119       {
120          Filenames.push_back(dirName+fileName);
121          numberOfFiles++;
122       }
123    }
124    DWORD dwError = GetLastError();
125    if (hFile != INVALID_HANDLE_VALUE) FindClose(hFile);
126    if (dwError != ERROR_NO_MORE_FILES) 
127    {
128       gdcmErrorMacro("FindNextFile error. Error is " << dwError);
129       return -1;
130    }
131
132 #else
133   // Real POSIX implementation: scandir is a BSD extension only, and doesn't 
134   // work on debian for example
135
136    DIR* dir = opendir(dirName.c_str());
137    if (!dir)
138    {
139       return 0;
140    }
141
142    // According to POSIX, the dirent structure contains a field char d_name[]
143    // of unspecified size, with at most NAME_MAX characters preceeding the
144    // terminating null character. Use of other fields will harm the  porta-
145    // bility of your programs.
146
147    struct stat buf;
148    dirent *d;
149    for (d = readdir(dir); d; d = readdir(dir))
150    {
151       fileName = dirName + d->d_name;
152       if( stat(fileName.c_str(), &buf) != 0 )
153       {
154          const char *str = strerror(errno);
155          gdcmErrorMacro( str );
156       }
157       if ( S_ISREG(buf.st_mode) )    //is it a regular file?
158       {
159          Filenames.push_back( fileName );
160          numberOfFiles++;
161       }
162       else if ( S_ISDIR(buf.st_mode) ) //directory?
163       {
164          if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
165          {
166             numberOfFiles += Explore( fileName, recursive);
167          }
168       }
169       else
170       {
171          gdcmErrorMacro( "Unexpected error" );
172          return -1;
173       }
174    }
175    if( closedir(dir) != 0 )
176    {
177       const char *str = strerror(errno);
178       gdcmErrorMacro( str );
179    }
180 #endif
181
182   return numberOfFiles;
183 }
184
185 //-----------------------------------------------------------------------------
186 // Print
187 /**
188  * \brief   Print method
189  * @param os ostream to write to 
190  */
191 void DirList::Print(std::ostream &os, std::string const &)
192 {
193    std::copy(Filenames.begin(), Filenames.end(), 
194              std::ostream_iterator<std::string>(os, "\n"));
195 }
196
197 //-----------------------------------------------------------------------------
198 } // end namespace gdcm