]> Creatis software - crea.git/blob - src/creaFilesFromDirectory.cxx
Feature #1763
[crea.git] / src / creaFilesFromDirectory.cxx
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 #
8 #  This software is governed by the CeCILL-B license under French law and 
9 #  abiding by the rules of distribution of free software. You can  use, 
10 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
11 #  license as circulated by CEA, CNRS and INRIA at the following URL 
12 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
13 #  or in the file LICENSE.txt.
14 #
15 #  As a counterpart to the access to the source code and  rights to copy,
16 #  modify and redistribute granted by the license, users are provided only
17 #  with a limited warranty  and the software's author,  the holder of the
18 #  economic rights,  and the successive licensors  have only  limited
19 #  liability. 
20 #
21 #  The fact that you are presently reading this means that you have had
22 #  knowledge of the CeCILL-B license and that you accept its terms.
23 # ------------------------------------------------------------------------ */ 
24
25 /*=========================================================================
26                                                                                 
27   Program:   crea
28   Module:    $RCSfile: creaFilesFromDirectory.cxx,v $
29   Language:  C++
30   Date:      $Date: 2012/11/15 09:07:32 $
31   Version:   $Revision: 1.6 $
32                                                                                 
33    
34      This software is distributed WITHOUT ANY WARRANTY; without even
35      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
36      PURPOSE.  See the above copyright notices for more information.
37                                                                                 
38 =========================================================================*/
39
40
41 /**
42  *  \file 
43  *  \brief  class Exception:generic class for throwing any exception (header) 
44  *
45  *    Long description:
46  */
47 #ifdef _MSC_VER
48    #include <windows.h>
49    #include <direct.h>
50 #else
51    #include <dirent.h>   
52    #include <sys/types.h>
53 #endif
54
55 #include <sys/stat.h>  //stat function
56
57  #include "creaFilesFromDirectory.h"
58  #include "creaMessageManager.h"
59
60 namespace crea
61
62
63 /**
64  * \brief   Add a SEPARATOR to the end of the name if necessary
65  * @param   pathname file/directory name to normalize 
66  */
67 std::string DirList::NormalizePath(std::string const &pathname)
68 {
69
70
71 #ifdef _WIN32
72    const char FILESEPARATOR = '\\';
73 #else
74    const char FILESEPARATOR = '/';
75 #endif
76
77         
78    std::string name = pathname;
79    int size = name.size();
80
81 //   if ( name[size-1] != SEPARATOR_X && name[size-1] != SEPARATOR_WIN )
82    if ( name[size-1] != FILESEPARATOR )
83    {
84       name += FILESEPARATOR;
85    }
86    return name;
87 }  
88 /**
89  * \brief   Explore a directory with possibility of recursion
90  *          return number of files read
91  * @param  dirpath   directory to explore
92  * @param  recursive whether we want recursion or not
93  */
94 int DirList::Explore(std::string const &dirpath, bool recursive)
95 {
96    int numberOfFiles = 0;
97    std::string fileName;
98    std::string dirName = NormalizePath(dirpath);
99 #ifdef _MSC_VER
100    WIN32_FIND_DATA fileData;
101    //assert( dirName[dirName.size()-1] == '' );
102    HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
103
104    for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
105        b = FindNextFile(hFile, &fileData))
106    {
107       fileName = fileData.cFileName;
108       if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
109       {
110          // Need to check for . and .. to avoid infinite loop
111          if ( fileName != "." && fileName != ".." && recursive )
112          {
113             numberOfFiles += Explore(dirName+fileName,recursive);
114          }
115       }
116       else
117       {
118          Filenames.push_back(dirName+fileName);
119          numberOfFiles++;
120       }
121    }
122    DWORD dwError = GetLastError();
123    if (hFile != INVALID_HANDLE_VALUE) 
124       FindClose(hFile);
125    if (dwError != ERROR_NO_MORE_FILES) 
126    {
127       LPVOID lpMsgBuf;
128       FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
129                     FORMAT_MESSAGE_FROM_SYSTEM|
130                     FORMAT_MESSAGE_IGNORE_INSERTS,
131                     NULL,GetLastError(),
132                     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
133                     (LPTSTR) &lpMsgBuf,0,NULL);
134
135       creaError("FindNextFile error. Error is " << (char *)lpMsgBuf
136                    <<" for the directory : "<<dirName);
137       return -1;
138    }
139
140 #else
141   // Real POSIX implementation: scandir is a BSD extension only, and doesn't 
142   // work on debian for example
143
144    DIR* dir = opendir(dirName.c_str());
145    if (!dir)
146    {
147       return 0;
148    }
149
150    // According to POSIX, the dirent structure contains a field char d_name[]
151    // of unspecified size, with at most NAME_MAX characters preceeding the
152    // terminating null character. Use of other fields will harm the  porta-
153    // bility of your programs.
154
155    struct stat buf;
156    dirent *d;
157    for (d = readdir(dir); d; d = readdir(dir))
158    {
159            
160       fileName = dirName + d->d_name;
161       if( stat(fileName.c_str(), &buf) != 0 )
162       {
163          //gdcmErrorMacro( strerror(errno) );
164       }
165       if ( S_ISREG(buf.st_mode) )    //is it a regular file?
166       {
167                   //printf("EED DirList::Explore [%s]\n" , d->d_name );
168                   if ( d->d_name[0]!='.')
169                   {     
170                  Filenames.push_back( fileName );
171                  numberOfFiles++;
172                   }
173       }
174       else if ( S_ISDIR(buf.st_mode) ) //directory?
175       {
176          if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
177          {
178             numberOfFiles += Explore( fileName, recursive);
179          }
180       }
181       else
182       {
183          //gdcmErrorMacro( "Unexpected error" );
184          return -1;
185       }
186    }
187    if( closedir(dir) != 0 )
188    {
189       //gdcmErrorMacro( strerror(errno) );
190    }
191 #endif
192
193   return numberOfFiles;
194 }
195
196 }