]> Creatis software - gdcm.git/blob - src/gdcmDirList.cxx
FIX: Building on cygwin is fine now
[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
30    NormalizePath(name);
31    Explore(name,recursive);
32 }
33
34 /**
35  * \ingroup gdcmDirList
36  * \brief  Destructor
37  */
38 gdcmDirList::~gdcmDirList(void)
39 {
40 }
41
42 //-----------------------------------------------------------------------------
43 // Print
44
45 //-----------------------------------------------------------------------------
46 // Public
47 /**
48  * \ingroup gdcmDirList
49  * \brief   Get the directory name
50  * @return the directory name 
51  */
52 std::string gdcmDirList::GetDirName(void)
53 {
54    return(name);
55 }
56
57 //-----------------------------------------------------------------------------
58 // Protected
59
60 //-----------------------------------------------------------------------------
61 // Private
62
63 /**
64  * \ingroup gdcmDirList
65  * \brief   Explore a directory with possibility of recursion
66  * @param  dirName directory to explore
67  * @param  recursive whether we want recursion or not
68  */
69 void gdcmDirList::Explore(std::string dirName,bool recursive)
70 {
71    std::string fileName;
72
73    NormalizePath(dirName);
74
75 #if defined(_MSC_VER) || (__CYGWIN__)
76    WIN32_FIND_DATA fileData; 
77    HANDLE hFile=FindFirstFile((dirName+"*").c_str(),&fileData);
78    int found=true;
79
80    while( (hFile!=INVALID_HANDLE_VALUE) && (found) )
81    {
82       fileName=fileData.cFileName;
83       if(fileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
84       {
85          if( (fileName!=".") && (fileName!="..") && (recursive) )
86             Explore(dirName+fileName,recursive);
87       }
88       else
89       {
90          this->push_back(dirName+fileName);
91       }
92
93       found=FindNextFile(hFile,&fileData);
94    }
95
96 #else
97    struct dirent **namelist;
98    int n=scandir(dirName.c_str(), &namelist, 0, alphasort);
99
100    for (int i= 0;i < n; i++) 
101    {
102       fileName=namelist[i]->d_name;
103       if(namelist[i]->d_type==DT_DIR)
104       {
105          if( (fileName!=".") && (fileName!="..") && (recursive) )
106             Explore(dirName+fileName,recursive);
107       }
108       else
109       {
110          this->push_back(dirName+fileName);
111       }
112    }
113 #endif
114 }
115
116 //-----------------------------------------------------------------------------