]> Creatis software - gdcm.git/blob - src/gdcmDirList.cxx
ENH: scandir was leaking like crazy, we shouldnt be using this function anyway
[gdcm.git] / src / gdcmDirList.cxx
1 // gdcmDirList.cxx
2 //-----------------------------------------------------------------------------
3 #include "gdcmDirList.h"
4 #include "gdcmUtil.h"
5
6 #include <iostream>
7 #include <algorithm>
8
9 #if defined(_MSC_VER) || defined (__CYGWIN__)
10    #include <windows.h> 
11 #ifdef _MSC_VER
12    #include <direct.h>
13 #endif //_MSC_VER
14 #else
15    #include <dirent.h>   
16    #include <unistd.h>
17 #endif
18
19 // Constructor / Destructor
20 /**
21  * \ingroup gdcmDirList
22  * \brief Constructor  
23  * @param  dirName root directory name
24  * @param  recursive whether we want to explore recursively or not 
25  */
26 gdcmDirList::gdcmDirList(std::string dirName,bool recursive)
27 {
28    name=dirName;
29    NormalizePath(name);
30    Explore(name,recursive);
31 }
32
33 /**
34  * \ingroup gdcmDirList
35  * \brief  Destructor
36  */
37 gdcmDirList::~gdcmDirList(void)
38 {
39 }
40
41 //-----------------------------------------------------------------------------
42 // Print
43
44 //-----------------------------------------------------------------------------
45 // Public
46 /**
47  * \ingroup gdcmDirList
48  * \brief   Get the directory name
49  * @return the directory name 
50  */
51 std::string gdcmDirList::GetDirName(void)
52 {
53    return(name);
54 }
55
56 //-----------------------------------------------------------------------------
57 // Protected
58
59 //-----------------------------------------------------------------------------
60 // Private
61
62 /**
63  * \ingroup gdcmDirList
64  * \brief   Explore a directory with possibility of recursion
65  * @param  dirName directory to explore
66  * @param  recursive whether we want recursion or not
67  */
68 void gdcmDirList::Explore(std::string dirName,bool recursive)
69 {
70    std::string fileName;
71
72    NormalizePath(dirName);
73 #if defined(_MSC_VER) || (__CYGWIN__)
74    WIN32_FIND_DATA fileData; 
75    HANDLE hFile=FindFirstFile((dirName+"*").c_str(),&fileData);
76    int found=true;
77
78    while( (hFile!=INVALID_HANDLE_VALUE) && (found) )
79    {
80       fileName=fileData.cFileName;
81       if(fileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
82       {
83          if( (fileName!=".") && (fileName!="..") && (recursive) )
84             Explore(dirName+fileName,recursive);
85       }
86       else
87       {
88          this->push_back(dirName+fileName);
89       }
90
91       found=FindNextFile(hFile,&fileData);
92    }
93
94 #else
95    // None  of  these  functions  is  in  POSIX.  The functions scandir() and
96    // alphasort() are from BSD 4.3, and have been available under Linux since
97    // libc4.  Libc4 and libc5 use the more precise prototype
98
99    struct dirent **namelist;
100    int n=scandir(dirName.c_str(), &namelist, 0, alphasort);
101
102    //if n < 0 should raise error
103
104    for (int i= 0;i < n; i++) 
105    {
106       fileName=namelist[i]->d_name;     
107       if(namelist[i]->d_type==DT_DIR)
108       {
109          if( (fileName!=".") && (fileName!="..") && (recursive) )
110             Explore(dirName+fileName,recursive);
111       }
112       else
113       {
114          this->push_back(dirName+fileName);
115       }
116       free(namelist[i]);
117    }
118    free(namelist);
119 #endif
120 }
121
122 //-----------------------------------------------------------------------------