]> Creatis software - gdcm.git/blob - src/gdcmDirList.cxx
* FIX: src/* fix the doxygen warnings.
[gdcm.git] / src / gdcmDirList.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDirList.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/11/16 10:25:53 $
7   Version:   $Revision: 1.27 $
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__) || defined(__BORLANDC__)
26    #include <windows.h> 
27 #if defined _MSC_VER || defined(__BORLANDC__)
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 namespace gdcm 
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 const & 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 const & DirList::GetDirName() const
72 {
73    return name;
74 }
75
76 //-----------------------------------------------------------------------------
77 // Protected
78
79 //-----------------------------------------------------------------------------
80 // Private
81
82 /**
83  * \brief   Explore a directory with possibility of recursion
84  *          return number of files read
85  * @param  dirpath   directory to explore
86  * @param  recursive whether we want recursion or not
87  */
88 int DirList::Explore(std::string const & dirpath, bool recursive)
89 {
90    int numberOfFiles = 0;
91    std::string fileName;
92    std::string dirName = Util::NormalizePath(dirpath);
93 #if defined(_MSC_VER) || defined(__CYGWIN__) || defined(__BORLANDC__)
94    WIN32_FIND_DATA fileData; 
95    HANDLE hFile=FindFirstFile((dirName+"*").c_str(),&fileData);
96    int found = true;
97
98    while( hFile != INVALID_HANDLE_VALUE && found )
99    {
100       fileName = fileData.cFileName;
101       if( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
102       {
103          // Is the '.' and '..' usefull ?
104          if( fileName != "." && fileName != ".." && recursive )
105          {
106             numberOfFiles += Explore(dirName+fileName,recursive);
107          }
108       }
109       else
110       {
111          push_back(dirName+fileName);
112          numberOfFiles++;
113       }
114
115       found = FindNextFile(hFile,&fileData);
116    }
117
118 #else
119   // Real POSIX implementation: scandir is a BSD extension only, and doesn't 
120   // work on debian for example
121
122    DIR* dir = opendir(dirName.c_str());
123    if (!dir)
124    {
125       return 0;
126    }
127
128    // According to POSIX, the dirent structure contains a field char d_name[]
129    // of  unspecified  size,  with  at most NAME_MAX characters preceding the
130    // terminating null character.  Use of other fields will harm  the  porta-
131    // bility  of  your  programs.
132
133    struct stat buf;
134    dirent* d = 0;
135    for (d = readdir(dir); d; d = readdir(dir))
136    {
137       fileName = dirName + d->d_name;
138       stat(fileName.c_str(), &buf); //really discard output ?
139       if( S_ISREG(buf.st_mode) )    //is it a regular file?
140       {
141          push_back( fileName );
142          numberOfFiles++;
143       }
144       else if( S_ISDIR(buf.st_mode) ) //directory?
145       {
146          if( d->d_name[0] != '.' && recursive ) //we are also skipping hidden files
147          {
148             numberOfFiles += Explore( fileName, recursive);
149          }
150       }
151       else
152       {
153          // we might need to do a different treament
154          //abort();
155       }
156    }
157   closedir(dir);
158 #endif
159
160   return numberOfFiles;
161 }
162 } // end namespace gdcm
163
164 //-----------------------------------------------------------------------------