]> Creatis software - gdcm.git/blob - src/gdcmDirList.cxx
Zilch.
[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 GDCM_NO_ANSI_STRING_STREAM
10    #include <strstream>
11    #define  ostringstream ostrstream
12 #else
13    #include <sstream>
14 #endif
15
16 #ifdef _MSC_VER 
17    #include <windows.h> 
18    #include <direct.h>
19 #else
20    #include <dirent.h>   
21    #include <unistd.h>
22 #endif
23
24 // Constructor / Destructor
25 /**
26  * \ingroup gdcmDirList
27  * \brief Constructor  
28  * @param  dirName root directory name
29  * @param  recursive whether we want to explore recursively or not 
30  */
31 gdcmDirList::gdcmDirList(std::string dirName,bool recursive)
32 {
33    name=dirName;
34
35    NormalizePath(name);
36    Explore(name,recursive);
37 }
38
39 /**
40  * \ingroup gdcmDirList
41  * \brief  Destructor
42  */
43 gdcmDirList::~gdcmDirList(void)
44 {
45 }
46
47 //-----------------------------------------------------------------------------
48 // Print
49
50 //-----------------------------------------------------------------------------
51 // Public
52 /**
53  * \ingroup gdcmDirList
54  * \brief   Get the directory name
55  * @return the directory name 
56  */
57 std::string gdcmDirList::GetDirName(void)
58 {
59    return(name);
60 }
61
62 //-----------------------------------------------------------------------------
63 // Protected
64
65 //-----------------------------------------------------------------------------
66 // Private
67
68 /**
69  * \ingroup gdcmDirList
70  * \brief   Explore a directory with possibility of recursion
71  * @param  dirName directory to explore
72  * @param  recursive whether we want recursion or not
73  */
74 void gdcmDirList::Explore(std::string dirName,bool recursive)
75 {
76    std::string fileName;
77
78    NormalizePath(dirName);
79
80 #ifdef _MSC_VER 
81    WIN32_FIND_DATA fileData; 
82    HANDLE hFile=FindFirstFile((dirName+"*").c_str(),&fileData);
83    int found=true;
84
85    while( (hFile!=INVALID_HANDLE_VALUE) && (found) )
86    {
87       fileName=fileData.cFileName;
88       if(fileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
89       {
90          if( (fileName!=".") && (fileName!="..") && (recursive) )
91             Explore(dirName+fileName,recursive);
92       }
93       else
94       {
95          this->push_back(dirName+fileName);
96       }
97
98       found=FindNextFile(hFile,&fileData);
99    }
100
101 #else
102    struct dirent **namelist;
103    int n=scandir(dirName.c_str(), &namelist, 0, alphasort);
104
105    for (int i= 0;i < n; i++) 
106    {
107       fileName=namelist[i]->d_name;
108       if(namelist[i]->d_type==DT_DIR)
109       {
110          if( (fileName!=".") && (fileName!="..") && (recursive) )
111             Explore(dirName+fileName,recursive);
112       }
113       else
114       {
115          this->push_back(dirName+fileName);
116       }
117    }
118 #endif
119 }
120
121 //-----------------------------------------------------------------------------