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