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