]> Creatis software - gdcm.git/blob - src/gdcmDirList.cxx
doxygenation
[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 // ================= WARNING 
25 // ================= DOXYGEN DOESN'T WORK FOR THIS FILE ?!?
26 // =================
27
28 // Constructor / Destructor
29 /*
30  * \ingroup gdcmDirList
31  * \brief Constructor  
32  * @param  dirName root directory name
33  * @param  recursive whether we want to explore recursively or not 
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  * @return the directory name 
60  */
61 std::string gdcmDirList::GetDirName(void)
62 {
63    return(name);
64 }
65
66 //-----------------------------------------------------------------------------
67 // Protected
68
69 //-----------------------------------------------------------------------------
70 // Private
71
72 /*
73  * \ingroup gdcmDirList
74  * \brief   Explore a directory with possibility of recursion
75  * @param  dirName directory to explore
76  * @param  recursive whether we want recursion or not
77  */
78 void gdcmDirList::Explore(std::string dirName,bool recursive)
79 {
80    std::string fileName;
81
82    NormalizePath(dirName);
83
84 #ifdef _MSC_VER 
85    WIN32_FIND_DATA fileData; 
86    HANDLE hFile=FindFirstFile((dirName+"*").c_str(),&fileData);
87    int found=true;
88
89    while( (hFile!=INVALID_HANDLE_VALUE) && (found) )
90    {
91       fileName=fileData.cFileName;
92       if(fileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
93       {
94          if( (fileName!=".") && (fileName!="..") && (recursive) )
95             Explore(dirName+fileName,recursive);
96       }
97       else
98       {
99          this->push_back(dirName+fileName);
100       }
101
102       found=FindNextFile(hFile,&fileData);
103    }
104
105 #else
106    struct dirent **namelist;
107    int n=scandir(dirName.c_str(), &namelist, 0, alphasort);
108
109    for (int i= 0;i < n; i++) 
110    {
111       fileName=namelist[i]->d_name;
112       if(namelist[i]->d_type==DT_DIR)
113       {
114          if( (fileName!=".") && (fileName!="..") && (recursive) )
115             Explore(dirName+fileName,recursive);
116       }
117       else
118       {
119          this->push_back(dirName+fileName);
120       }
121    }
122 #endif
123 }
124
125 //-----------------------------------------------------------------------------