]> Creatis software - gdcm.git/blob - src/gdcmDirList.cxx
* FIX : bug fix in the gdcmDirList for the recursivity in directories
[gdcm.git] / src / gdcmDirList.cxx
1 // gdcmDirList.cxx
2 //-----------------------------------------------------------------------------
3 #include "gdcmDirList.h"
4
5 #include <iostream>
6 #include <algorithm>
7
8 #ifdef GDCM_NO_ANSI_STRING_STREAM
9    #include <strstream>
10    #define  ostringstream ostrstream
11 #else
12    #include <sstream>
13 #endif
14
15 #ifdef _MSC_VER 
16    #include <windows.h> 
17    #include <direct.h>
18 #else
19    #include <dirent.h>   
20    #include <unistd.h>
21 #endif
22
23 //-----------------------------------------------------------------------------
24 const char gdcmDirList::SEPARATOR_X      = '/';
25 const char gdcmDirList::SEPARATOR_WIN    = '\\';
26 const std::string gdcmDirList::SEPARATOR = "/";
27
28 //-----------------------------------------------------------------------------
29 // Constructor / Destructor
30 /*
31  * \ingroup gdcmDirList
32  * \brief Constructor  
33  * @param   
34  */
35 gdcmDirList::gdcmDirList(std::string dirName,bool recursive)
36 {
37    name=dirName;
38
39    NormalizePath(name);
40    Explore(name,recursive);
41 }
42
43 /*
44  * \ingroup gdcmDirList
45  * \brief  Destructor
46  */
47 gdcmDirList::~gdcmDirList(void)
48 {
49 }
50
51 //-----------------------------------------------------------------------------
52 // Print
53
54 //-----------------------------------------------------------------------------
55 // Public
56 /*
57  * \ingroup gdcmDirList
58  * \brief   Get the directory name
59  * @param   
60  */
61 std::string gdcmDirList::GetDirName(void)
62 {
63    return(name);
64 }
65
66 //-----------------------------------------------------------------------------
67 // Protected
68
69 //-----------------------------------------------------------------------------
70 // Private
71 /*
72  * \ingroup gdcmDirList
73  * \brief   Add a SEPARATOR to the end of the directory name is necessary
74  * @param   
75  */
76 void gdcmDirList::NormalizePath(std::string &dirName)
77 {
78    int size=dirName.size();
79    if((dirName[size-1]!=SEPARATOR_X)&&(dirName[size-1]!=SEPARATOR_WIN))
80    {
81       dirName+=SEPARATOR;
82    }
83 }
84
85 /*
86  * \ingroup gdcmDirList
87  * \brief   Explore a directory with possibility of recursion
88  * @param   
89  */
90 void gdcmDirList::Explore(std::string dirName,bool recursive)
91 {
92    std::string fileName;
93
94    NormalizePath(dirName);
95
96 #ifdef _MSC_VER 
97    WIN32_FIND_DATA fileData; 
98    HANDLE hFile=FindFirstFile((dirName+"*").c_str(),&fileData);
99    int found=true;
100
101    while( (hFile!=INVALID_HANDLE_VALUE) && (found) )
102    {
103       fileName=fileData.cFileName;
104       std::cout<<fileName<<std::endl;
105       if(fileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
106       {
107          if( (fileName!=".") && (fileName!="..") && (recursive) )
108             Explore(dirName+fileName,recursive);
109       }
110       else
111       {
112          this->push_back(dirName+fileName);
113       }
114
115       found=FindNextFile(hFile,&fileData);
116    }
117
118 #else
119    struct dirent **namelist;
120    int n=scandir(dirName.c_str(), &namelist, 0, alphasort);
121
122    for (int i= 0;i < n; i++) 
123    {
124       fileName=namelist[i]->d_name;
125       if(namelist[i]->d_type==DT_DIR)
126       {
127          if( (fileName!=".") && (fileName!="..") && (recursive) )
128             Explore(dirName+fileName,recursive);
129       }
130       else
131       {
132          this->push_back(dirName+fileName);
133       }
134    }
135 #endif
136 }
137
138 //-----------------------------------------------------------------------------