00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "gdcmDirList.h"
00020 #include "gdcmUtil.h"
00021
00022 #include <iterator>
00023
00024 #ifdef _MSC_VER
00025 #include <windows.h>
00026 #include <direct.h>
00027 #else
00028 #include <dirent.h>
00029 #include <sys/types.h>
00030 #include <sys/stat.h>
00031 #endif
00032
00033 namespace gdcm
00034 {
00035
00036
00042 DirList::DirList(std::string const &dirName, bool recursive)
00043 {
00044 DirName = dirName;
00045 Explore(dirName, recursive);
00046 }
00047
00051 DirList::~DirList()
00052 {
00053 }
00054
00055
00056
00062 bool DirList::IsDirectory(std::string const &dirName)
00063 {
00064 #ifndef _MSC_VER
00065 struct stat buf;
00066 stat(dirName.c_str(), &buf);
00067 return S_ISDIR(buf.st_mode);
00068 #else
00069 return (GetFileAttributes(dirName.c_str()) & FILE_ATTRIBUTE_DIRECTORY) != 0;
00070 #endif
00071 }
00072
00073
00074
00075
00076
00077
00084 int DirList::Explore(std::string const &dirpath, bool recursive)
00085 {
00086 int numberOfFiles = 0;
00087 std::string fileName;
00088 std::string dirName = Util::NormalizePath(dirpath);
00089 #ifdef _MSC_VER
00090 WIN32_FIND_DATA fileData;
00091 HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
00092
00093 for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
00094 b = FindNextFile(hFile, &fileData))
00095 {
00096 fileName = fileData.cFileName;
00097 if( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
00098 {
00099
00100 if( fileName != "." && fileName != ".." && recursive )
00101 {
00102 numberOfFiles += Explore(dirName+fileName,recursive);
00103 }
00104 }
00105 else
00106 {
00107 Filenames.push_back(dirName+fileName);
00108 numberOfFiles++;
00109 }
00110 }
00111 if (hFile != INVALID_HANDLE_VALUE) FindClose(hFile);
00112
00113 #else
00114
00115
00116
00117 DIR* dir = opendir(dirName.c_str());
00118 if (!dir)
00119 {
00120 return 0;
00121 }
00122
00123
00124
00125
00126
00127
00128 struct stat buf;
00129 dirent *d = 0;
00130 for (d = readdir(dir); d; d = readdir(dir))
00131 {
00132 fileName = dirName + d->d_name;
00133 stat(fileName.c_str(), &buf);
00134 if( S_ISREG(buf.st_mode) )
00135 {
00136 Filenames.push_back( fileName );
00137 numberOfFiles++;
00138 }
00139 else if( S_ISDIR(buf.st_mode) )
00140 {
00141 if( d->d_name[0] != '.' && recursive )
00142 {
00143 numberOfFiles += Explore( fileName, recursive);
00144 }
00145 }
00146 else
00147 {
00148
00149
00150 }
00151 }
00152 closedir(dir);
00153 #endif
00154
00155 return numberOfFiles;
00156 }
00157
00158
00159
00164 void DirList::Print(std::ostream &os)
00165 {
00166 std::copy(Filenames.begin(), Filenames.end(),
00167 std::ostream_iterator<std::string>(os, "\n"));
00168 }
00169
00170
00171 }