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