]> Creatis software - gdcm.git/blob - src/gdcmDirList.cxx
* Reorder file content
[gdcm.git] / src / gdcmDirList.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDirList.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/02/02 15:07:41 $
7   Version:   $Revision: 1.43 $
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 bool DirList::IsDirectory(std::string const &dirName)
58 {
59 #ifndef _MSC_VER
60    struct stat buf;
61    stat(dirName.c_str(), &buf);
62    return S_ISDIR(buf.st_mode);
63 #else
64    return (GetFileAttributes(dirName.c_str()) & FILE_ATTRIBUTE_DIRECTORY) != 0;
65 #endif
66 }
67
68 //-----------------------------------------------------------------------------
69 // Protected
70
71 //-----------------------------------------------------------------------------
72 // Private
73 /**
74  * \brief   Explore a directory with possibility of recursion
75  *          return number of files read
76  * @param  dirpath   directory to explore
77  * @param  recursive whether we want recursion or not
78  */
79 int DirList::Explore(std::string const &dirpath, bool recursive)
80 {
81    int numberOfFiles = 0;
82    std::string fileName;
83    std::string dirName = Util::NormalizePath(dirpath);
84 #ifdef _MSC_VER
85    WIN32_FIND_DATA fileData;
86    HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
87
88    for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
89        b = FindNextFile(hFile,&fileData))
90    {
91       fileName = fileData.cFileName;
92       if( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
93       {
94          // Need to check for . and .. to avoid infinite loop
95          if( fileName != "." && fileName != ".." && recursive )
96          {
97             numberOfFiles += Explore(dirName+fileName,recursive);
98          }
99       }
100       else
101       {
102          Filenames.push_back(dirName+fileName);
103          numberOfFiles++;
104       }
105    }
106    if (hFile != INVALID_HANDLE_VALUE) FindClose(hFile);
107
108 #else
109   // Real POSIX implementation: scandir is a BSD extension only, and doesn't 
110   // work on debian for example
111
112    DIR* dir = opendir(dirName.c_str());
113    if (!dir)
114    {
115       return 0;
116    }
117
118    // According to POSIX, the dirent structure contains a field char d_name[]
119    // of  unspecified  size,  with  at most NAME_MAX characters preceding the
120    // terminating null character.  Use of other fields will harm the  porta-
121    // bility  of  your  programs.
122
123    struct stat buf;
124    dirent *d = 0;
125    for (d = readdir(dir); d; d = readdir(dir))
126    {
127       fileName = dirName + d->d_name;
128       stat(fileName.c_str(), &buf); //really discard output ?
129       if( S_ISREG(buf.st_mode) )    //is it a regular file?
130       {
131          Filenames.push_back( fileName );
132          numberOfFiles++;
133       }
134       else if( S_ISDIR(buf.st_mode) ) //directory?
135       {
136          if( d->d_name[0] != '.' && recursive ) //we are also skipping hidden files
137          {
138             numberOfFiles += Explore( fileName, recursive);
139          }
140       }
141       else
142       {
143          // we might need to do a different treament
144          //abort();
145       }
146    }
147   closedir(dir);
148 #endif
149
150   return numberOfFiles;
151 }
152
153 //-----------------------------------------------------------------------------
154 // Print
155 /**
156  * \brief   Print method
157  * @param os ostream to write to 
158  */
159 void DirList::Print(std::ostream &os)
160 {
161    std::copy(Filenames.begin(), Filenames.end(), 
162              std::ostream_iterator<std::string>(os, "\n"));
163 }
164
165 //-----------------------------------------------------------------------------
166 } // end namespace gdcm