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

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: 2005/02/06 14:43:27 $
00007   Version:   $Revision: 1.45 $
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 
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 // Constructor / Destructor
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 // Public
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 // Protected
00075 
00076 //-----------------------------------------------------------------------------
00077 // Private
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          // Need to check for . and .. to avoid infinite loop
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   // Real POSIX implementation: scandir is a BSD extension only, and doesn't 
00115   // work on debian for example
00116 
00117    DIR* dir = opendir(dirName.c_str());
00118    if (!dir)
00119    {
00120       return 0;
00121    }
00122 
00123    // According to POSIX, the dirent structure contains a field char d_name[]
00124    // of  unspecified  size, with at most NAME_MAX characters preceding the
00125    // terminating null character. Use of other fields will harm the  porta-
00126    // bility of your programs.
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); //really discard output ?
00134       if( S_ISREG(buf.st_mode) )    //is it a regular file?
00135       {
00136          Filenames.push_back( fileName );
00137          numberOfFiles++;
00138       }
00139       else if( S_ISDIR(buf.st_mode) ) //directory?
00140       {
00141          if( d->d_name[0] != '.' && recursive ) //we are also skipping hidden files
00142          {
00143             numberOfFiles += Explore( fileName, recursive);
00144          }
00145       }
00146       else
00147       {
00148          // we might need to do a different treament
00149          //abort();
00150       }
00151    }
00152   closedir(dir);
00153 #endif
00154 
00155   return numberOfFiles;
00156 }
00157 
00158 //-----------------------------------------------------------------------------
00159 // Print
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 } // end namespace gdcm

Generated on Thu Feb 10 22:17:57 2005 for gdcm by doxygen 1.3.6