]> Creatis software - gdcm.git/blob - src/gdcmDirList.cxx
BUG: Attempt to fix borland IsDirectory
[gdcm.git] / src / gdcmDirList.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDirList.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/04/15 21:21:42 $
7   Version:   $Revision: 1.47 $
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
22 #include <iterator>
23
24 #ifdef _MSC_VER
25    #include <windows.h> 
26    #include <direct.h>
27 #else
28    #include <dirent.h>   
29    #include <sys/types.h>
30    #include <sys/stat.h>
31 #endif
32
33 namespace gdcm 
34 {
35 //-----------------------------------------------------------------------------
36 // Constructor / Destructor
37 /**
38  * \brief Constructor  
39  * @param  dirName root directory name
40  * @param  recursive whether we want to explore recursively or not 
41  */
42 DirList::DirList(std::string const &dirName, bool recursive)
43 {
44    DirName = dirName;
45    Explore(dirName, recursive);
46 }
47
48 /**
49  * \brief  Destructor
50  */
51 DirList::~DirList()
52 {
53 }
54
55 //-----------------------------------------------------------------------------
56 // Public
57 /**
58  * \brief Tells us if file name corresponds to a Directory   
59  * @param  dirName file name to check
60  * @return true if the file IS a Directory
61  */
62 bool DirList::IsDirectory(std::string const &dirName)
63 {
64   struct stat fs;
65   if(stat(dirName.c_str(), &fs) == 0)
66     {
67 #if _WIN32
68     return ((fs.st_mode & _S_IFDIR) != 0);
69 #else
70     return S_ISDIR(fs.st_mode);
71 #endif
72     }
73   else
74     {
75     return false;
76     }
77 }
78
79 //-----------------------------------------------------------------------------
80 // Protected
81
82 //-----------------------------------------------------------------------------
83 // Private
84 /**
85  * \brief   Explore a directory with possibility of recursion
86  *          return number of files read
87  * @param  dirpath   directory to explore
88  * @param  recursive whether we want recursion or not
89  */
90 int DirList::Explore(std::string const &dirpath, bool recursive)
91 {
92    int numberOfFiles = 0;
93    std::string fileName;
94    std::string dirName = Util::NormalizePath(dirpath);
95 #ifdef _MSC_VER
96    WIN32_FIND_DATA fileData;
97    HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
98
99    for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
100        b = FindNextFile(hFile, &fileData))
101    {
102       fileName = fileData.cFileName;
103       if( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
104       {
105          // Need to check for . and .. to avoid infinite loop
106          if( fileName != "." && fileName != ".." && recursive )
107          {
108             numberOfFiles += Explore(dirName+fileName,recursive);
109          }
110       }
111       else
112       {
113          Filenames.push_back(dirName+fileName);
114          numberOfFiles++;
115       }
116    }
117    if (hFile != INVALID_HANDLE_VALUE) FindClose(hFile);
118
119 #else
120   // Real POSIX implementation: scandir is a BSD extension only, and doesn't 
121   // work on debian for example
122
123    DIR* dir = opendir(dirName.c_str());
124    if (!dir)
125    {
126       return 0;
127    }
128
129    // According to POSIX, the dirent structure contains a field char d_name[]
130    // of  unspecified  size, with at most NAME_MAX characters preceding the
131    // terminating null character. Use of other fields will harm the  porta-
132    // bility of your programs.
133
134    struct stat buf;
135    dirent *d;
136    for (d = readdir(dir); d; d = readdir(dir))
137    {
138       fileName = dirName + d->d_name;
139       stat(fileName.c_str(), &buf); //really discard output ?
140       if( S_ISREG(buf.st_mode) )    //is it a regular file?
141       {
142          Filenames.push_back( fileName );
143          numberOfFiles++;
144       }
145       else if( S_ISDIR(buf.st_mode) ) //directory?
146       {
147          if( d->d_name[0] != '.' && recursive ) //we are also skipping hidden files
148          {
149             numberOfFiles += Explore( fileName, recursive);
150          }
151       }
152       else
153       {
154          // we might need to do a different treament
155          //abort();
156       }
157    }
158   closedir(dir);
159 #endif
160
161   return numberOfFiles;
162 }
163
164 //-----------------------------------------------------------------------------
165 // Print
166 /**
167  * \brief   Print method
168  * @param os ostream to write to 
169  */
170 void DirList::Print(std::ostream &os)
171 {
172    std::copy(Filenames.begin(), Filenames.end(), 
173              std::ostream_iterator<std::string>(os, "\n"));
174 }
175
176 //-----------------------------------------------------------------------------
177 } // end namespace gdcm