]> Creatis software - gdcm.git/blob - src/gdcmDirList.cxx
BUG: On recent debian system, the dirent structure is not fed properly. Furthermore...
[gdcm.git] / src / gdcmDirList.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDirList.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/07/26 19:36:56 $
7   Version:   $Revision: 1.18 $
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.htm 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 <iostream>
23 #include <algorithm>
24
25 #if defined(_MSC_VER) || defined (__CYGWIN__)
26    #include <windows.h> 
27 #ifdef _MSC_VER
28    #include <direct.h>
29 #endif //_MSC_VER
30 #else
31    #include <dirent.h>   
32    #include <unistd.h>
33    #include <sys/stat.h>
34    #include <sys/types.h>
35 #endif
36
37 // Constructor / Destructor
38 /**
39  * \ingroup gdcmDirList
40  * \brief Constructor  
41  * @param  dirName root directory name
42  * @param  recursive whether we want to explore recursively or not 
43  */
44 gdcmDirList::gdcmDirList(std::string dirName, bool recursive)
45 {
46    name = dirName;
47    NormalizePath(name);
48    Explore(name, recursive);
49 }
50
51 /**
52  * \ingroup gdcmDirList
53  * \brief  Destructor
54  */
55 gdcmDirList::~gdcmDirList()
56 {
57 }
58
59 //-----------------------------------------------------------------------------
60 // Print
61
62 //-----------------------------------------------------------------------------
63 // Public
64 /**
65  * \ingroup gdcmDirList
66  * \brief   Get the directory name
67  * @return the directory name 
68  */
69 std::string gdcmDirList::GetDirName()
70 {
71    return name;
72 }
73
74 //-----------------------------------------------------------------------------
75 // Protected
76
77 //-----------------------------------------------------------------------------
78 // Private
79
80 /**
81  * \ingroup gdcmDirList
82  * \brief   Explore a directory with possibility of recursion
83  *          return number of files read
84  * @param  dirName directory to explore
85  * @param  recursive whether we want recursion or not
86  */
87 int gdcmDirList::Explore(std::string dirName, bool recursive)
88 {
89    int numberOfFiles = 0;
90    std::string fileName;
91    NormalizePath(dirName);
92 #if defined(_MSC_VER) || (__CYGWIN__)
93    WIN32_FIND_DATA fileData; 
94    HANDLE hFile=FindFirstFile((dirName+"*").c_str(),&fileData);
95    int found = true;
96
97    while( hFile != INVALID_HANDLE_VALUE && found )
98    {
99       fileName = fileData.cFileName;
100       if( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
101       {
102          // Is the '.' and '..' usefull ?
103          if( fileName != "." && fileName != ".." && recursive )
104          {
105             numberOfFiles += Explore(dirName+fileName,recursive);
106          }
107       }
108       else
109       {
110          this->push_back(dirName+fileName);
111          numberOfFiles++;
112       }
113
114       found = FindNextFile(hFile,&fileData);
115    }
116
117 #else
118   // Real POSIX implementation: scandir is a BSD extension only, and doesn't 
119   // work on debian for example
120
121    DIR* dir = opendir(dirName.c_str());
122    if (!dir)
123    {
124       return 0;
125    }
126
127    // According to POSIX, the dirent structure contains a field char d_name[]
128    // of  unspecified  size,  with  at most NAME_MAX characters preceding the
129    // terminating null character.  Use of other fields will harm  the  porta-
130    // bility  of  your  programs.
131
132    struct stat buf;
133    dirent* d = 0;
134    for (d = readdir(dir); d; d = readdir(dir))
135    {
136       fileName = dirName + d->d_name;
137       stat(fileName.c_str(), &buf); //really discard output ?
138       if( S_ISREG(buf.st_mode) ) //is it a regular file?
139       {
140          this->push_back( fileName );
141          numberOfFiles++;
142       }
143       else if( S_ISDIR(buf.st_mode) ) //directory?
144       {
145          if( d->d_name[0] != '.' && recursive ) //we are also skipping hidden files
146          {
147             numberOfFiles += Explore( fileName, recursive);
148          }
149       }
150       else
151       {
152          // we might need to do a different treament
153          //abort();
154       }
155    }
156   closedir(dir);
157 #endif
158
159   return numberOfFiles;
160 }
161
162 //-----------------------------------------------------------------------------