]> Creatis software - gdcm.git/blob - src/gdcmDirList.cxx
* FIX now file names and directory name are written properly in the
[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  dirName
34  * @param  recursive 
35  */
36 gdcmDirList::gdcmDirList(std::string dirName,bool recursive)
37 {
38    name=dirName;
39
40    NormalizePath(name);
41    Explore(name,recursive);
42 }
43
44 /*
45  * \ingroup gdcmDirList
46  * \brief  Destructor
47  */
48 gdcmDirList::~gdcmDirList(void)
49 {
50 }
51
52 //-----------------------------------------------------------------------------
53 // Print
54
55 //-----------------------------------------------------------------------------
56 // Public
57 /*
58  * \ingroup gdcmDirList
59  * \brief   Get the directory name
60  * @return the directory name 
61  */
62 std::string gdcmDirList::GetDirName(void)
63 {
64    return(name);
65 }
66
67 //-----------------------------------------------------------------------------
68 // Protected
69
70 //-----------------------------------------------------------------------------
71 // Private
72 /*
73  * \ingroup gdcmDirList
74  * \brief   Add a SEPARATOR to the end of the directory name is necessary
75  * @param   
76  */
77 void gdcmDirList::NormalizePath(std::string &dirName)
78 {
79    int size=dirName.size();
80    if((dirName[size-1]!=SEPARATOR_X)&&(dirName[size-1]!=SEPARATOR_WIN))
81    {
82       dirName+=SEPARATOR;
83    }
84 }
85
86 /*
87  * \ingroup gdcmDirList
88  * \brief   Explore a directory with possibility of recursion
89  * @param  dirName directory to explore
90  * @param  recursive whether we want recursion or not
91  */
92 void gdcmDirList::Explore(std::string dirName,bool recursive)
93 {
94    std::string fileName;
95
96    NormalizePath(dirName);
97
98 #ifdef _MSC_VER 
99    WIN32_FIND_DATA fileData; 
100    HANDLE hFile=FindFirstFile((dirName+"*").c_str(),&fileData);
101    int found=true;
102
103    while( (hFile!=INVALID_HANDLE_VALUE) && (found) )
104    {
105       fileName=fileData.cFileName;
106       if(fileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
107       {
108          if( (fileName!=".") && (fileName!="..") && (recursive) )
109             Explore(dirName+fileName,recursive);
110       }
111       else
112       {
113          this->push_back(dirName+fileName);
114       }
115
116       found=FindNextFile(hFile,&fileData);
117    }
118
119 #else
120    struct dirent **namelist;
121    int n=scandir(dirName.c_str(), &namelist, 0, alphasort);
122
123    for (int i= 0;i < n; i++) 
124    {
125       fileName=namelist[i]->d_name;
126       if(namelist[i]->d_type==DT_DIR)
127       {
128          if( (fileName!=".") && (fileName!="..") && (recursive) )
129             Explore(dirName+fileName,recursive);
130       }
131       else
132       {
133          this->push_back(dirName+fileName);
134       }
135    }
136 #endif
137 }
138
139 //-----------------------------------------------------------------------------