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