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