]> Creatis software - gdcm.git/blob - src/gdcmDirList.cxx
ENH: Adding 'gdcm' namespace. Be nice with me this was a ~13000 lines patch. Also...
[gdcm.git] / src / gdcmDirList.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDirList.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/10/12 04:35:45 $
7   Version:   $Revision: 1.23 $
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 <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 namespace gdcm 
37 {
38
39 // Constructor / Destructor
40 /**
41  * \ingroup DirList
42  * \brief Constructor  
43  * @param  dirName root directory name
44  * @param  recursive whether we want to explore recursively or not 
45  */
46 DirList::DirList(std::string dirName, bool recursive)
47 {
48    name = dirName;
49    Util::NormalizePath(name);
50    Explore(name, recursive);
51 }
52
53 /**
54  * \ingroup DirList
55  * \brief  Destructor
56  */
57 DirList::~DirList()
58 {
59 }
60
61 //-----------------------------------------------------------------------------
62 // Print
63
64 //-----------------------------------------------------------------------------
65 // Public
66 /**
67  * \ingroup DirList
68  * \brief   Get the directory name
69  * @return the directory name 
70  */
71 std::string DirList::GetDirName()
72 {
73    return name;
74 }
75
76 //-----------------------------------------------------------------------------
77 // Protected
78
79 //-----------------------------------------------------------------------------
80 // Private
81
82 /**
83  * \ingroup DirList
84  * \brief   Explore a directory with possibility of recursion
85  *          return number of files read
86  * @param  dirName directory to explore
87  * @param  recursive whether we want recursion or not
88  */
89 int DirList::Explore(std::string dirName, bool recursive)
90 {
91    int numberOfFiles = 0;
92    std::string fileName;
93    Util::NormalizePath(dirName);
94 #if defined(_MSC_VER) || (__CYGWIN__)
95    WIN32_FIND_DATA fileData; 
96    HANDLE hFile=FindFirstFile((dirName+"*").c_str(),&fileData);
97    int found = true;
98
99    while( hFile != INVALID_HANDLE_VALUE && found )
100    {
101       fileName = fileData.cFileName;
102       if( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
103       {
104          // Is the '.' and '..' usefull ?
105          if( fileName != "." && fileName != ".." && recursive )
106          {
107             numberOfFiles += Explore(dirName+fileName,recursive);
108          }
109       }
110       else
111       {
112          push_back(dirName+fileName);
113          numberOfFiles++;
114       }
115
116       found = FindNextFile(hFile,&fileData);
117    }
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 = 0;
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          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 } // end namespace gdcm
164
165 //-----------------------------------------------------------------------------