2 //-----------------------------------------------------------------------------
3 #include "gdcmDirList.h"
8 #ifdef GDCM_NO_ANSI_STRING_STREAM
10 #define ostringstream ostrstream
23 //-----------------------------------------------------------------------------
24 const char gdcmDirList::SEPARATOR_X = '/';
25 const char gdcmDirList::SEPARATOR_WIN = '\\';
26 const std::string gdcmDirList::SEPARATOR = "/";
28 //-----------------------------------------------------------------------------
29 // Constructor / Destructor
31 * \ingroup gdcmDirList
35 gdcmDirList::gdcmDirList(std::string dirName,bool recursive)
40 Explore(name,recursive);
44 * \ingroup gdcmDirList
47 gdcmDirList::~gdcmDirList(void)
51 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
57 * \ingroup gdcmDirList
58 * \brief Get the directory name
61 std::string gdcmDirList::GetDirName(void)
66 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
72 * \ingroup gdcmDirList
73 * \brief Add a SEPARATOR to the end of the directory name is necessary
76 void gdcmDirList::NormalizePath(std::string &dirName)
78 int size=dirName.size();
79 if((dirName[size-1]!=SEPARATOR_X)&&(dirName[size-1]!=SEPARATOR_WIN))
86 * \ingroup gdcmDirList
87 * \brief Explore a directory with possibility of recursion
90 void gdcmDirList::Explore(std::string dirName,bool recursive)
94 NormalizePath(dirName);
97 WIN32_FIND_DATA fileData;
98 HANDLE hFile=FindFirstFile((dirName+"*").c_str(),&fileData);
101 while( (hFile!=INVALID_HANDLE_VALUE) && (found) )
103 fileName=fileData.cFileName;
104 if(fileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
106 if( (fileName!=".") && (fileName!="..") && (recursive) )
107 Explore(dirName+fileName,recursive);
111 this->push_back(dirName+fileName);
114 found=FindNextFile(hFile,&fileData);
118 struct dirent **namelist;
119 int n=scandir(dirName.c_str(), &namelist, 0, alphasort);
121 for (int i= 0;i < n; i++)
123 fileName=namelist[i]->d_name;
124 if(namelist[i]->d_type==DT_DIR)
126 if( (fileName!=".") && (fileName!="..") && (recursive) )
127 Explore(dirName+fileName,recursive);
131 this->push_back(dirName+fileName);
137 //-----------------------------------------------------------------------------