1 /*=========================================================================
3 Program: KWSys - Kitware System Library
4 Module: $RCSfile: Directory.cxx,v $
6 Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
7 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notices for more information.
13 =========================================================================*/
14 #include "kwsysPrivate.h"
15 #include KWSYS_HEADER(Directory.hxx)
17 #include KWSYS_HEADER(Configure.hxx)
19 #include KWSYS_HEADER(stl/string)
20 #include KWSYS_HEADER(stl/vector)
22 // Work-around CMake dependency scanning limitation. This must
23 // duplicate the above list of headers.
25 # include "Directory.hxx.in"
26 # include "Configure.hxx.in"
27 # include "kwsys_stl.hxx.in"
28 # include "kwsys_stl_string.hxx.in"
29 # include "kwsys_stl_vector.hxx.in"
32 namespace KWSYS_NAMESPACE
35 //----------------------------------------------------------------------------
36 class DirectoryInternals
40 kwsys_stl::vector
<kwsys_stl::string
> Files
;
42 // Path to Open'ed directory
43 kwsys_stl::string Path
;
46 //----------------------------------------------------------------------------
47 Directory::Directory()
49 this->Internal
= new DirectoryInternals
;
52 //----------------------------------------------------------------------------
53 Directory::~Directory()
55 delete this->Internal
;
58 //----------------------------------------------------------------------------
59 unsigned long Directory::GetNumberOfFiles() const
61 return static_cast<unsigned long>(this->Internal
->Files
.size());
64 //----------------------------------------------------------------------------
65 const char* Directory::GetFile(unsigned long dindex
) const
67 if ( dindex
>= this->Internal
->Files
.size() )
71 return this->Internal
->Files
[dindex
].c_str();
74 //----------------------------------------------------------------------------
75 const char* Directory::GetPath() const
77 return this->Internal
->Path
.c_str();
80 //----------------------------------------------------------------------------
81 void Directory::Clear()
83 this->Internal
->Path
.resize(0);
84 this->Internal
->Files
.clear();
87 } // namespace KWSYS_NAMESPACE
89 // First microsoft compilers
91 #if defined(_MSC_VER) || defined(__WATCOMC__)
100 #include <sys/types.h>
102 namespace KWSYS_NAMESPACE
105 bool Directory::Load(const char* name
)
114 size_t n
= strlen(name
);
115 if ( name
[n
- 1] == '/' )
117 buf
= new char[n
+ 1 + 1];
118 sprintf(buf
, "%s*", name
);
122 buf
= new char[n
+ 2 + 1];
123 sprintf(buf
, "%s/*", name
);
125 struct _finddata_t data
; // data of current file
127 // Now put them into the file array
128 srchHandle
= _findfirst(buf
, &data
);
131 if ( srchHandle
== -1 )
136 // Loop through names
139 this->Internal
->Files
.push_back(data
.name
);
141 while ( _findnext(srchHandle
, &data
) != -1 );
142 this->Internal
->Path
= name
;
143 return _findclose(srchHandle
) != -1;
146 unsigned long Directory::GetNumberOfFilesInDirectory(const char* name
)
154 size_t n
= strlen(name
);
155 if ( name
[n
- 1] == '/' )
157 buf
= new char[n
+ 1 + 1];
158 sprintf(buf
, "%s*", name
);
162 buf
= new char[n
+ 2 + 1];
163 sprintf(buf
, "%s/*", name
);
165 struct _finddata_t data
; // data of current file
167 // Now put them into the file array
168 srchHandle
= _findfirst(buf
, &data
);
171 if ( srchHandle
== -1 )
176 // Loop through names
177 unsigned long count
= 0;
182 while ( _findnext(srchHandle
, &data
) != -1 );
183 _findclose(srchHandle
);
187 } // namespace KWSYS_NAMESPACE
191 // Now the POSIX style directory access
193 #include <sys/types.h>
196 /* There is a problem with the Portland compiler, large file
197 support and glibc/Linux system headers:
198 http://www.pgroup.com/userforum/viewtopic.php?
199 p=1992&sid=f16167f51964f1a68fe5041b8eb213b6
201 #if defined(__PGI) && defined(__USE_FILE_OFFSET64)
202 # define dirent dirent64
205 namespace KWSYS_NAMESPACE
208 bool Directory::Load(const char* name
)
216 DIR* dir
= opendir(name
);
223 for (dirent
* d
= readdir(dir
); d
; d
= readdir(dir
) )
225 this->Internal
->Files
.push_back(d
->d_name
);
227 this->Internal
->Path
= name
;
232 unsigned long Directory::GetNumberOfFilesInDirectory(const char* name
)
234 DIR* dir
= opendir(name
);
241 unsigned long count
= 0;
242 for (dirent
* d
= readdir(dir
); d
; d
= readdir(dir
) )
250 } // namespace KWSYS_NAMESPACE