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