Main Page | File List | Related Pages

gdcmDirList.cxx

00001 // gdcmDirList.cxx
00002 //-----------------------------------------------------------------------------
00003 #include "gdcmDirList.h"
00004 
00005 #include <iostream>
00006 #include <algorithm>
00007 
00008 #ifdef GDCM_NO_ANSI_STRING_STREAM
00009    #include <strstream>
00010    #define  ostringstream ostrstream
00011 #else
00012    #include <sstream>
00013 #endif
00014 
00015 #ifdef _MSC_VER 
00016    #include <windows.h> 
00017    #include <direct.h>
00018 #else
00019    #include <dirent.h>   
00020    #include <unistd.h>
00021 #endif
00022 
00023 //-----------------------------------------------------------------------------
00024 const char gdcmDirList::SEPARATOR_X      = '/';
00025 const char gdcmDirList::SEPARATOR_WIN    = '\\';
00026 const std::string gdcmDirList::SEPARATOR = "/";
00027 
00028 //-----------------------------------------------------------------------------
00029 // Constructor / Destructor
00030 /*
00031  * \ingroup gdcmDirList
00032  * \brief Constructor  
00033  * @param   
00034  */
00035 gdcmDirList::gdcmDirList(std::string dirName,bool recursive)
00036 {
00037    name=dirName;
00038 
00039    NormalizePath(name);
00040    Explore(name,recursive);
00041 }
00042 
00043 /*
00044  * \ingroup gdcmDirList
00045  * \brief  Destructor
00046  * @param   
00047  */
00048 gdcmDirList::~gdcmDirList(void)
00049 {
00050 }
00051 
00052 //-----------------------------------------------------------------------------
00053 // Print
00054 
00055 //-----------------------------------------------------------------------------
00056 // Public
00057 /*
00058  * \ingroup gdcmDirList
00059  * \brief   Get the directory name
00060  * @param   
00061  */
00062 std::string gdcmDirList::GetDirName(void)
00063 {
00064    return(name);
00065 }
00066 
00067 //-----------------------------------------------------------------------------
00068 // Protected
00069 
00070 //-----------------------------------------------------------------------------
00071 // Private
00072 /*
00073  * \ingroup gdcmDirList
00074  * \brief   Add a SEPARATOR to the end of the directory name is necessary
00075  * @param   
00076  */
00077 void gdcmDirList::NormalizePath(std::string &dirName)
00078 {
00079    int size=dirName.size();
00080    if((dirName[size-1]!=SEPARATOR_X)&&(dirName[size-1]!=SEPARATOR_WIN))
00081    {
00082       dirName+=SEPARATOR;
00083    }
00084 }
00085 
00086 /*
00087  * \ingroup gdcmDirList
00088  * \brief   Explore a directory with possibility of recursion
00089  * @param   
00090  */
00091 void gdcmDirList::Explore(std::string dirName,bool recursive)
00092 {
00093    std::string fileName;
00094 
00095    NormalizePath(dirName);
00096 
00097 #ifdef _MSC_VER 
00098    WIN32_FIND_DATA fileData; 
00099    HANDLE hFile=FindFirstFile((dirName+"*").c_str(),&fileData);
00100    int found=true;
00101 
00102    while( (hFile!=INVALID_HANDLE_VALUE) && (found) )
00103    {
00104       fileName=fileData.cFileName;
00105       if(fileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
00106       {
00107          if( (fileName!=".") && (fileName!="..") && (recursive) )
00108             Explore(dirName+fileName);
00109       }
00110       else
00111       {
00112          this->push_back(dirName+fileName);
00113       }
00114 
00115       found=FindNextFile(hFile,&fileData);
00116    }
00117 
00118 #else
00119    struct dirent **namelist;
00120    int n=scandir(dirName.c_str(), &namelist, 0, alphasort);
00121 
00122    for (int i= 0;i < n; i++) 
00123    {
00124       fileName=namelist[i]->d_name;
00125       if(namelist[i]->d_type==DT_DIR)
00126       {
00127          if( (fileName!=".") && (fileName!="..") && (recursive) )
00128             Explore(dirName+fileName);
00129       }
00130       else
00131       {
00132          this->push_back(dirName+fileName);
00133       }
00134    }
00135 #endif
00136 }
00137 
00138 //-----------------------------------------------------------------------------

Generated on Mon Feb 14 16:13:43 2005 for gdcm by doxygen 1.3.6