]> Creatis software - gdcm.git/blob - src/gdcmDirList.cxx
- now Write drops LUT Descriptors and LUTs (if any) when SamplesPerPixel =3
[gdcm.git] / src / gdcmDirList.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDirList.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/06/28 09:30:58 $
7   Version:   $Revision: 1.17 $
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    NormalizePath(dirName);
88 #if defined(_MSC_VER) || (__CYGWIN__)
89    WIN32_FIND_DATA fileData; 
90    HANDLE hFile=FindFirstFile((dirName+"*").c_str(),&fileData);
91    int found=true;
92
93    while( (hFile!=INVALID_HANDLE_VALUE) && (found) )
94    {
95       fileName=fileData.cFileName;
96       if(fileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
97       {
98          if( (fileName!=".") && (fileName!="..") && (recursive) )
99             Explore(dirName+fileName,recursive);
100       }
101       else
102       {
103          this->push_back(dirName+fileName);
104       }
105
106       found=FindNextFile(hFile,&fileData);
107    }
108
109 #else
110    struct dirent **namelist;
111    int n=scandir(dirName.c_str(), &namelist, 0, alphasort);
112
113    for (int i= 0;i < n; i++) 
114    {
115       fileName=namelist[i]->d_name;     
116       if(namelist[i]->d_type==DT_DIR)
117       {
118          if( (fileName!=".") && (fileName!="..") && (recursive) )
119             Explore(dirName+fileName,recursive);
120       }
121       else
122       {
123          this->push_back(dirName+fileName);
124       }
125    }
126 #endif
127 }
128
129 //-----------------------------------------------------------------------------