]> Creatis software - gdcm.git/blob - src/gdcmDirList.cxx
* Erroneous leading white fix:
[gdcm.git] / src / gdcmDirList.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDirList.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/06/20 18:08:47 $
7   Version:   $Revision: 1.16 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.htm for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmDirList.h"
20 #include "gdcmUtil.h"
21
22 #include <iostream>
23 #include <algorithm>
24
25 #if defined(_MSC_VER) || defined (__CYGWIN__)
26    #include <windows.h> 
27 #ifdef _MSC_VER
28    #include <direct.h>
29 #endif //_MSC_VER
30 #else
31    #include <dirent.h>   
32    #include <unistd.h>
33 #endif
34
35 // Constructor / Destructor
36 /**
37  * \ingroup gdcmDirList
38  * \brief Constructor  
39  * @param  dirName root directory name
40  * @param  recursive whether we want to explore recursively or not 
41  */
42 gdcmDirList::gdcmDirList(std::string dirName,bool recursive)
43 {
44    name=dirName;
45    NormalizePath(name);
46    Explore(name,recursive);
47 }
48
49 /**
50  * \ingroup gdcmDirList
51  * \brief  Destructor
52  */
53 gdcmDirList::~gdcmDirList(void)
54 {
55 }
56
57 //-----------------------------------------------------------------------------
58 // Print
59
60 //-----------------------------------------------------------------------------
61 // Public
62 /**
63  * \ingroup gdcmDirList
64  * \brief   Get the directory name
65  * @return the directory name 
66  */
67 std::string gdcmDirList::GetDirName(void)
68 {
69    return(name);
70 }
71
72 //-----------------------------------------------------------------------------
73 // Protected
74
75 //-----------------------------------------------------------------------------
76 // Private
77
78 /**
79  * \ingroup gdcmDirList
80  * \brief   Explore a directory with possibility of recursion
81  * @param  dirName directory to explore
82  * @param  recursive whether we want recursion or not
83  */
84 void gdcmDirList::Explore(std::string dirName,bool recursive)
85 {
86    std::string fileName;
87
88    NormalizePath(dirName);
89 #if defined(_MSC_VER) || (__CYGWIN__)
90    WIN32_FIND_DATA fileData; 
91    HANDLE hFile=FindFirstFile((dirName+"*").c_str(),&fileData);
92    int found=true;
93
94    while( (hFile!=INVALID_HANDLE_VALUE) && (found) )
95    {
96       fileName=fileData.cFileName;
97       if(fileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
98       {
99          if( (fileName!=".") && (fileName!="..") && (recursive) )
100             Explore(dirName+fileName,recursive);
101       }
102       else
103       {
104          this->push_back(dirName+fileName);
105       }
106
107       found=FindNextFile(hFile,&fileData);
108    }
109
110 #else
111    struct dirent **namelist;
112    int n=scandir(dirName.c_str(), &namelist, 0, alphasort);
113
114    for (int i= 0;i < n; i++) 
115    {
116       fileName=namelist[i]->d_name;     
117       if(namelist[i]->d_type==DT_DIR)
118       {
119          if( (fileName!=".") && (fileName!="..") && (recursive) )
120             Explore(dirName+fileName,recursive);
121       }
122       else
123       {
124          this->push_back(dirName+fileName);
125       }
126    }
127 #endif
128 }
129
130 //-----------------------------------------------------------------------------