Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

gdcm::DirList Class Reference

List containing the file headers of all the 'gdcm readable' files found by exploring (possibely recursively) a root directory. More...

#include <gdcmDirList.h>

Inheritance diagram for gdcm::DirList:

Inheritance graph
[legend]
Collaboration diagram for gdcm::DirList:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 DirList (std::string const &dirName, bool recursive=false)
 Constructor.
 ~DirList ()
 Destructor.
void Print (std::ostream &os=std::cout, std::string const &indent="")
 Print method.
std::string const & GetDirName () const
 Return the name of the directory.
DirListType const & GetFilenames () const
 Return the file names.
void SetPrintLevel (int level)
 Sets the print level for the Dicom Header Elements.
int GetPrintLevel ()
 Gets the print level for the Dicom Entries.

Static Public Member Functions

static bool IsDirectory (std::string const &dirName)
 Tells us if file name corresponds to a Directory.

Protected Attributes

int PrintLevel
 Amount of printed details for each Dicom Entries : 0 : stands for the least detail level.

Private Member Functions

int Explore (std::string const &dirName, bool recursive=false)
 Explore a directory with possibility of recursion return number of files read.

Private Attributes

DirListType Filenames
 List of file names.
std::string DirName
 name of the root directory to explore

Detailed Description

List containing the file headers of all the 'gdcm readable' files found by exploring (possibely recursively) a root directory.

Definition at line 42 of file gdcmDirList.h.


Constructor & Destructor Documentation

gdcm::DirList::DirList std::string const &  dirName,
bool  recursive = false
 

Constructor.

Parameters:
dirName root directory name
recursive whether we want to explore recursively or not

Definition at line 45 of file gdcmDirList.cxx.

References DirName, and Explore().

00046 {
00047    DirName = dirName;
00048    Explore(dirName, recursive);
00049 }

gdcm::DirList::~DirList  ) 
 

Destructor.

Definition at line 54 of file gdcmDirList.cxx.

00055 {
00056 }


Member Function Documentation

int gdcm::DirList::Explore std::string const &  dirpath,
bool  recursive = false
[private]
 

Explore a directory with possibility of recursion return number of files read.

Parameters:
dirpath directory to explore
recursive whether we want recursion or not

Definition at line 98 of file gdcmDirList.cxx.

References Filenames, gdcmErrorMacro, and gdcm::Util::NormalizePath().

Referenced by DirList().

00099 {
00100    int numberOfFiles = 0;
00101    std::string fileName;
00102    std::string dirName = Util::NormalizePath(dirpath);
00103 #ifdef _MSC_VER
00104    WIN32_FIND_DATA fileData;
00105    //assert( dirName[dirName.size()-1] == '' );
00106    HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
00107 
00108    for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
00109        b = FindNextFile(hFile, &fileData))
00110    {
00111       fileName = fileData.cFileName;
00112       if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
00113       {
00114          // Need to check for . and .. to avoid infinite loop
00115          if ( fileName != "." && fileName != ".." && recursive )
00116          {
00117             numberOfFiles += Explore(dirName+fileName,recursive);
00118          }
00119       }
00120       else
00121       {
00122          Filenames.push_back(dirName+fileName);
00123          numberOfFiles++;
00124       }
00125    }
00126    DWORD dwError = GetLastError();
00127    if (hFile != INVALID_HANDLE_VALUE) 
00128       FindClose(hFile);
00129    if (dwError != ERROR_NO_MORE_FILES) 
00130    {
00131       LPVOID lpMsgBuf;
00132       FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
00133                     FORMAT_MESSAGE_FROM_SYSTEM|
00134                     FORMAT_MESSAGE_IGNORE_INSERTS,
00135                     NULL,GetLastError(),
00136                     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
00137                     (LPTSTR) &lpMsgBuf,0,NULL);
00138 
00139       gdcmErrorMacro("FindNextFile error. Error is " << (char *)lpMsgBuf
00140                    <<" for the directory : "<<dirName);
00141       return -1;
00142    }
00143 
00144 #else
00145   // Real POSIX implementation: scandir is a BSD extension only, and doesn't 
00146   // work on debian for example
00147 
00148    DIR* dir = opendir(dirName.c_str());
00149    if (!dir)
00150    {
00151       return 0;
00152    }
00153 
00154    // According to POSIX, the dirent structure contains a field char d_name[]
00155    // of unspecified size, with at most NAME_MAX characters preceeding the
00156    // terminating null character. Use of other fields will harm the  porta-
00157    // bility of your programs.
00158 
00159    struct stat buf;
00160    dirent *d;
00161    for (d = readdir(dir); d; d = readdir(dir))
00162    {
00163       fileName = dirName + d->d_name;
00164       if( stat(fileName.c_str(), &buf) != 0 )
00165       {
00166          const char *str = strerror(errno);
00167          gdcmErrorMacro( str );
00168       }
00169       if ( S_ISREG(buf.st_mode) )    //is it a regular file?
00170       {
00171          Filenames.push_back( fileName );
00172          numberOfFiles++;
00173       }
00174       else if ( S_ISDIR(buf.st_mode) ) //directory?
00175       {
00176          if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
00177          {
00178             numberOfFiles += Explore( fileName, recursive);
00179          }
00180       }
00181       else
00182       {
00183          gdcmErrorMacro( "Unexpected error" );
00184          return -1;
00185       }
00186    }
00187    if( closedir(dir) != 0 )
00188    {
00189       const char *str = strerror(errno);
00190       gdcmErrorMacro( str );
00191    }
00192 #endif
00193 
00194   return numberOfFiles;
00195 }

std::string const& gdcm::DirList::GetDirName  )  const [inline]
 

Return the name of the directory.

Definition at line 51 of file gdcmDirList.h.

Referenced by gdcm::DicomDir::CreateDicomDirChainedList().

00051 { return DirName; }

DirListType const& gdcm::DirList::GetFilenames  )  const [inline]
 

Return the file names.

Definition at line 54 of file gdcmDirList.h.

Referenced by gdcm::DicomDir::CreateDicomDirChainedList(), and gdcm::SerieHelper::SetDirectory().

00054 { return Filenames; }

int gdcm::Base::GetPrintLevel  )  [inline, inherited]
 

Gets the print level for the Dicom Entries.

Definition at line 50 of file gdcmBase.h.

00050 { return PrintLevel; }

bool gdcm::DirList::IsDirectory std::string const &  dirName  )  [static]
 

Tells us if file name corresponds to a Directory.

Parameters:
dirName file name to check
Returns:
true if the file IS a Directory

Definition at line 65 of file gdcmDirList.cxx.

References gdcmStaticErrorMacro.

00066 {
00067    struct stat fs;
00068    // std::cout << "dirName[dirName.size()-1] [" << dirName[dirName.size()-1] << "]"
00069    //           << std::endl;
00070    //assert( dirName[dirName.size()-1] != GDCM_FILESEPARATOR );
00071    if ( stat(dirName.c_str(), &fs) == 0 )
00072    {
00073 #if _WIN32
00074       return ((fs.st_mode & _S_IFDIR) != 0);
00075 #else
00076       return S_ISDIR(fs.st_mode);
00077 #endif
00078    }
00079    else
00080    {
00081       const char *str = strerror(errno);
00082       gdcmStaticErrorMacro( str );
00083       return false;
00084    }
00085 }

void gdcm::DirList::Print std::ostream &  os = std::cout,
std::string const &  indent = ""
[virtual]
 

Print method.

Parameters:
os ostream to write to

Reimplemented from gdcm::Base.

Definition at line 203 of file gdcmDirList.cxx.

References Filenames.

00204 {
00205    std::copy(Filenames.begin(), Filenames.end(), 
00206              std::ostream_iterator<std::string>(os, "\n"));
00207 }

void gdcm::Base::SetPrintLevel int  level  )  [inline, inherited]
 

Sets the print level for the Dicom Header Elements.

Note:
0 for Light Print; 1 for 'medium' Print, 2 for Heavy Print

Definition at line 47 of file gdcmBase.h.

Referenced by gdcm::SQItem::Print(), gdcm::SeqEntry::Print(), gdcm::FileHelper::Print(), gdcm::ElementSet::Print(), and gdcm::DicomDir::Print().

00047 { PrintLevel = level; }


Member Data Documentation

std::string gdcm::DirList::DirName [private]
 

name of the root directory to explore

Definition at line 64 of file gdcmDirList.h.

Referenced by DirList().

DirListType gdcm::DirList::Filenames [private]
 

List of file names.

Definition at line 62 of file gdcmDirList.h.

Referenced by Explore(), and Print().

int gdcm::Base::PrintLevel [protected, inherited]
 

Amount of printed details for each Dicom Entries : 0 : stands for the least detail level.

Definition at line 55 of file gdcmBase.h.

Referenced by gdcm::SQItem::Print(), gdcm::SeqEntry::Print(), gdcm::FileHelper::Print(), gdcm::ElementSet::Print(), gdcm::DocEntry::Print(), gdcm::DictEntry::Print(), gdcm::DicomDirStudy::Print(), gdcm::DicomDirSerie::Print(), gdcm::DicomDirPatient::Print(), gdcm::DicomDirMeta::Print(), gdcm::DicomDir::Print(), and gdcm::DataEntry::Print().


The documentation for this class was generated from the following files:
Generated on Fri Jan 20 10:14:35 2006 for gdcm by  doxygen 1.4.4