gdcmDirList.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002                                                                                 
00003   Program:   gdcm
00004   Module:    $RCSfile: gdcmDirList.cxx,v $
00005   Language:  C++
00006   Date:      $Date: 2007/06/08 12:49:37 $
00007   Version:   $Revision: 1.62 $
00008                                                                                 
00009   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
00010   l'Image). All rights reserved. See Doc/License.txt or
00011   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
00012                                                                                 
00013      This software is distributed WITHOUT ANY WARRANTY; without even
00014      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00015      PURPOSE.  See the above copyright notices for more information.
00016                                                                                 
00017 =========================================================================*/
00018 
00019 #include "gdcmDirList.h"
00020 #include "gdcmUtil.h"
00021 #include "gdcmDebug.h"
00022 
00023 #include "gdcmDicomDirImage.h"
00024 
00025 #include <iterator>
00026 #include <assert.h>
00027 #include <errno.h>
00028 #include <sys/stat.h>  //stat function
00029 
00030 #ifdef _MSC_VER
00031    #include <windows.h> 
00032    #include <direct.h>
00033 #else
00034    #include <dirent.h>   
00035    #include <sys/types.h>
00036 #endif
00037 
00038 namespace GDCM_NAME_SPACE
00039 {
00040 //-----------------------------------------------------------------------------
00041 // Constructor / Destructor
00047 DirList::DirList(std::string const &dirName, bool recursive)
00048 {
00049    DirName = dirName;
00050    Explore(dirName, recursive);
00051 }
00052 
00058 DirList::DirList(DicomDirSerie *se)
00059 {
00060    Explore(se);
00061 }
00065 DirList::~DirList()
00066 {
00067 }
00068 
00069 //-----------------------------------------------------------------------------
00070 // Public
00076 bool DirList::IsDirectory(std::string const &dirName)
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 }
00096 
00101 std::string DirList::GetFirst()
00102 {
00103    ItDirList = Filenames.begin();
00104    if (ItDirList != Filenames.end())
00105       return *ItDirList;
00106    return "";
00107 } 
00108 
00113 std::string DirList::GetNext()
00114 {
00115    gdcmAssertMacro (ItDirList != Filenames.end())
00116    {
00117       ++ItDirList;
00118       if (ItDirList != Filenames.end())
00119          return *ItDirList;      
00120    }
00121    return "";
00122 } 
00123 
00124 //-----------------------------------------------------------------------------
00125 // Protected
00126 
00127 //-----------------------------------------------------------------------------
00128 // Private
00129 
00135 int DirList::Explore(DicomDirSerie *se)
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 }   
00148    
00155 int DirList::Explore(std::string const &dirpath, bool recursive)
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 }
00251 
00252 //-----------------------------------------------------------------------------
00253 // Print
00258 void DirList::Print(std::ostream &os, std::string const &)
00259 {
00260    std::copy(Filenames.begin(), Filenames.end(), 
00261              std::ostream_iterator<std::string>(os, "\n"));
00262 }
00263 
00264 //-----------------------------------------------------------------------------
00265 } // end namespace gdcm

Generated on Fri Aug 24 12:53:14 2007 for gdcm by  doxygen 1.4.6