GDCM_NAME_SPACE::DirList Class Reference

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

#include <gdcmDirList.h>

Inheritance diagram for GDCM_NAME_SPACE::DirList:

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

Collaboration graph
[legend]
List of all members.

Public Member Functions

 DirList (std::string const &dirName, bool recursive)
 Constructor.
 DirList (DicomDirSerie *s)
 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.
int GetSize () const
 Return the number of Files.
std::string GetFirst ()
 Get the first entry while visiting Filenames.
std::string GetNext ()
 Get the next entry while visiting Filenames.
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.
int Explore (DicomDirSerie *s)
 Explores a DicomDirSerie return number of files found.

Private Attributes

DirListType Filenames
 List of file names.
std::string DirName
 name of the root directory to explore
DirListType::iterator ItDirList
 iterator on the SQItems of the current SeqEntry

Detailed Description

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

Definition at line 45 of file gdcmDirList.h.


Constructor & Destructor Documentation

GDCM_NAME_SPACE::DirList::DirList std::string const &  dirName,
bool  recursive
 

Constructor.

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

Definition at line 47 of file gdcmDirList.cxx.

References DirName, and Explore().

00048 {
00049    DirName = dirName;
00050    Explore(dirName, recursive);
00051 }

GDCM_NAME_SPACE::DirList::DirList DicomDirSerie se  ) 
 

Constructor.

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

Definition at line 58 of file gdcmDirList.cxx.

References Explore().

00059 {
00060    Explore(se);
00061 }

GDCM_NAME_SPACE::DirList::~DirList  ) 
 

Destructor.

Definition at line 65 of file gdcmDirList.cxx.

00066 {
00067 }


Member Function Documentation

int GDCM_NAME_SPACE::DirList::Explore DicomDirSerie se  )  [private]
 

Explores a DicomDirSerie return number of files found.

Parameters:
se DicomDirSerie to explore

Definition at line 135 of file gdcmDirList.cxx.

References Filenames, GDCM_NAME_SPACE::DocEntrySet::GetEntryString(), GDCM_NAME_SPACE::DicomDirSerie::GetFirstImage(), and GDCM_NAME_SPACE::DicomDirSerie::GetNextImage().

00136 {
00137    int numberOfFiles = 0;
00138 
00139    DicomDirImage *im = se->GetFirstImage();
00140    while ( im ) 
00141    { 
00142       Filenames.push_back( im->GetEntryString(0x0004, 0x1500) );// File name (Referenced File ID)
00143       numberOfFiles++;           
00144       im = se->GetNextImage();   
00145    }
00146    return numberOfFiles;
00147 }   

int GDCM_NAME_SPACE::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 155 of file gdcmDirList.cxx.

References Filenames, and GDCM_NAME_SPACE::Util::NormalizePath().

Referenced by DirList().

00156 {
00157    int numberOfFiles = 0;
00158    std::string fileName;
00159    std::string dirName = Util::NormalizePath(dirpath);
00160 #ifdef _MSC_VER
00161    WIN32_FIND_DATA fileData;
00162    //assert( dirName[dirName.size()-1] == '' );
00163    HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
00164 
00165    for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
00166        b = FindNextFile(hFile, &fileData))
00167    {
00168       fileName = fileData.cFileName;
00169       if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
00170       {
00171          // Need to check for . and .. to avoid infinite loop
00172          if ( fileName != "." && fileName != ".." && recursive )
00173          {
00174             numberOfFiles += Explore(dirName+fileName,recursive);
00175          }
00176       }
00177       else
00178       {
00179          Filenames.push_back(dirName+fileName);
00180          numberOfFiles++;
00181       }
00182    }
00183    DWORD dwError = GetLastError();
00184    if (hFile != INVALID_HANDLE_VALUE) 
00185       FindClose(hFile);
00186    if (dwError != ERROR_NO_MORE_FILES) 
00187    {
00188       LPVOID lpMsgBuf;
00189       FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
00190                     FORMAT_MESSAGE_FROM_SYSTEM|
00191                     FORMAT_MESSAGE_IGNORE_INSERTS,
00192                     NULL,GetLastError(),
00193                     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
00194                     (LPTSTR) &lpMsgBuf,0,NULL);
00195 
00196       gdcmErrorMacro("FindNextFile error. Error is " << (char *)lpMsgBuf
00197                    <<" for the directory : "<<dirName);
00198       return -1;
00199    }
00200 
00201 #else
00202   // Real POSIX implementation: scandir is a BSD extension only, and doesn't 
00203   // work on debian for example
00204 
00205    DIR* dir = opendir(dirName.c_str());
00206    if (!dir)
00207    {
00208       return 0;
00209    }
00210 
00211    // According to POSIX, the dirent structure contains a field char d_name[]
00212    // of unspecified size, with at most NAME_MAX characters preceeding the
00213    // terminating null character. Use of other fields will harm the  porta-
00214    // bility of your programs.
00215 
00216    struct stat buf;
00217    dirent *d;
00218    for (d = readdir(dir); d; d = readdir(dir))
00219    {
00220       fileName = dirName + d->d_name;
00221       if( stat(fileName.c_str(), &buf) != 0 )
00222       {
00223          gdcmErrorMacro( strerror(errno) );
00224       }
00225       if ( S_ISREG(buf.st_mode) )    //is it a regular file?
00226       {
00227          Filenames.push_back( fileName );
00228          numberOfFiles++;
00229       }
00230       else if ( S_ISDIR(buf.st_mode) ) //directory?
00231       {
00232          if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
00233          {
00234             numberOfFiles += Explore( fileName, recursive);
00235          }
00236       }
00237       else
00238       {
00239          gdcmErrorMacro( "Unexpected error" );
00240          return -1;
00241       }
00242    }
00243    if( closedir(dir) != 0 )
00244    {
00245       gdcmErrorMacro( strerror(errno) );
00246    }
00247 #endif
00248 
00249   return numberOfFiles;
00250 }

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

Return the name of the directory.

Definition at line 55 of file gdcmDirList.h.

00055 { return DirName; }

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

Return the file names.

Definition at line 58 of file gdcmDirList.h.

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

00058 { return Filenames; }

std::string GDCM_NAME_SPACE::DirList::GetFirst  ) 
 

Get the first entry while visiting Filenames.

Returns:
The first if found, otherwhise ""

Definition at line 101 of file gdcmDirList.cxx.

References Filenames, and ItDirList.

00102 {
00103    ItDirList = Filenames.begin();
00104    if (ItDirList != Filenames.end())
00105       return *ItDirList;
00106    return "";
00107 } 

std::string GDCM_NAME_SPACE::DirList::GetNext  ) 
 

Get the next entry while visiting Filenames.

Returns:
The next if found, otherwhise ""

Definition at line 113 of file gdcmDirList.cxx.

References Filenames, gdcmAssertMacro, and ItDirList.

00114 {
00115    gdcmAssertMacro (ItDirList != Filenames.end())
00116    {
00117       ++ItDirList;
00118       if (ItDirList != Filenames.end())
00119          return *ItDirList;      
00120    }
00121    return "";
00122 } 

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

Gets the print level for the Dicom Entries.

Definition at line 50 of file gdcmBase.h.

00050 { return PrintLevel; }

int GDCM_NAME_SPACE::DirList::GetSize  )  const [inline]
 

Return the number of Files.

Definition at line 61 of file gdcmDirList.h.

00061 { return Filenames.size(); }  

bool GDCM_NAME_SPACE::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 76 of file gdcmDirList.cxx.

References gdcmStaticErrorMacro.

00077 {
00078    struct stat fs;
00079    // std::cout << "dirName[dirName.size()-1] [" << dirName[dirName.size()-1] << "]"
00080    //           << std::endl;
00081    //assert( dirName[dirName.size()-1] != GDCM_FILESEPARATOR );
00082    if ( stat(dirName.c_str(), &fs) == 0 )
00083    {
00084 #if _WIN32
00085       return ((fs.st_mode & _S_IFDIR) != 0);
00086 #else
00087       return S_ISDIR(fs.st_mode);
00088 #endif
00089    }
00090    else
00091    {
00092       gdcmStaticErrorMacro( strerror(errno) );
00093       return false;
00094    }
00095 }

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

Print method.

Parameters:
os ostream to write to

Reimplemented from GDCM_NAME_SPACE::Base.

Definition at line 258 of file gdcmDirList.cxx.

References Filenames.

00259 {
00260    std::copy(Filenames.begin(), Filenames.end(), 
00261              std::ostream_iterator<std::string>(os, "\n"));
00262 }

void GDCM_NAME_SPACE::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_NAME_SPACE::FileHelper::Print(), and GDCM_NAME_SPACE::DicomDir::Print().

00047 { PrintLevel = level; }


Member Data Documentation

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

name of the root directory to explore

Definition at line 75 of file gdcmDirList.h.

Referenced by DirList().

DirListType GDCM_NAME_SPACE::DirList::Filenames [private]
 

List of file names.

Definition at line 73 of file gdcmDirList.h.

Referenced by Explore(), GetFirst(), GetNext(), and Print().

DirListType::iterator GDCM_NAME_SPACE::DirList::ItDirList [private]
 

iterator on the SQItems of the current SeqEntry

Definition at line 78 of file gdcmDirList.h.

Referenced by GetFirst(), and GetNext().

int GDCM_NAME_SPACE::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_NAME_SPACE::SeqEntry::Print(), GDCM_NAME_SPACE::FileHelper::Print(), GDCM_NAME_SPACE::ElementSet::Print(), GDCM_NAME_SPACE::DocEntry::Print(), GDCM_NAME_SPACE::DictEntry::Print(), GDCM_NAME_SPACE::DicomDirStudy::Print(), GDCM_NAME_SPACE::DicomDirSerie::Print(), GDCM_NAME_SPACE::DicomDirPatient::Print(), GDCM_NAME_SPACE::DicomDirMeta::Print(), GDCM_NAME_SPACE::DicomDir::Print(), and GDCM_NAME_SPACE::DataEntry::Print().


The documentation for this class was generated from the following files:
Generated on Fri Aug 24 12:56:59 2007 for gdcm by  doxygen 1.4.6