]> Creatis software - gdcm.git/blob - src/gdcmDirList.cxx
ENH: Remove redundancie about GDCM_DICT stuff, now we only need to modify
[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 #ifdef _MSC_VER 
10    #include <windows.h> 
11    #include <direct.h>
12 #else
13    #include <dirent.h>   
14    #include <unistd.h>
15 #endif
16
17 // Constructor / Destructor
18 /**
19  * \ingroup gdcmDirList
20  * \brief Constructor  
21  * @param  dirName root directory name
22  * @param  recursive whether we want to explore recursively or not 
23  */
24 gdcmDirList::gdcmDirList(std::string dirName,bool recursive)
25 {
26    name=dirName;
27
28    NormalizePath(name);
29    Explore(name,recursive);
30 }
31
32 /**
33  * \ingroup gdcmDirList
34  * \brief  Destructor
35  */
36 gdcmDirList::~gdcmDirList(void)
37 {
38 }
39
40 //-----------------------------------------------------------------------------
41 // Print
42
43 //-----------------------------------------------------------------------------
44 // Public
45 /**
46  * \ingroup gdcmDirList
47  * \brief   Get the directory name
48  * @return the directory name 
49  */
50 std::string gdcmDirList::GetDirName(void)
51 {
52    return(name);
53 }
54
55 //-----------------------------------------------------------------------------
56 // Protected
57
58 //-----------------------------------------------------------------------------
59 // Private
60
61 /**
62  * \ingroup gdcmDirList
63  * \brief   Explore a directory with possibility of recursion
64  * @param  dirName directory to explore
65  * @param  recursive whether we want recursion or not
66  */
67 void gdcmDirList::Explore(std::string dirName,bool recursive)
68 {
69    std::string fileName;
70
71    NormalizePath(dirName);
72
73 #ifdef _MSC_VER 
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    struct dirent **namelist;
96    int n=scandir(dirName.c_str(), &namelist, 0, alphasort);
97
98    for (int i= 0;i < n; i++) 
99    {
100       fileName=namelist[i]->d_name;
101       if(namelist[i]->d_type==DT_DIR)
102       {
103          if( (fileName!=".") && (fileName!="..") && (recursive) )
104             Explore(dirName+fileName,recursive);
105       }
106       else
107       {
108          this->push_back(dirName+fileName);
109       }
110    }
111 #endif
112 }
113
114 //-----------------------------------------------------------------------------