Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / kwsys / Directory.cxx
blobb408e123634891fc8e195bd04116ee9e58d8ba31
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.
24 #if 0
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"
30 #endif
32 namespace KWSYS_NAMESPACE
35 //----------------------------------------------------------------------------
36 class DirectoryInternals
38 public:
39 // Array of Files
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() )
69 return 0;
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__)
92 #include <windows.h>
93 #include <io.h>
94 #include <ctype.h>
95 #include <fcntl.h>
96 #include <stdio.h>
97 #include <stdlib.h>
98 #include <string.h>
99 #include <sys/stat.h>
100 #include <sys/types.h>
102 namespace KWSYS_NAMESPACE
105 bool Directory::Load(const char* name)
107 this->Clear();
108 #if _MSC_VER < 1300
109 long srchHandle;
110 #else
111 intptr_t srchHandle;
112 #endif
113 char* buf;
114 size_t n = strlen(name);
115 if ( name[n - 1] == '/' )
117 buf = new char[n + 1 + 1];
118 sprintf(buf, "%s*", name);
120 else
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);
129 delete [] buf;
131 if ( srchHandle == -1 )
133 return 0;
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)
148 #if _MSC_VER < 1300
149 long srchHandle;
150 #else
151 intptr_t srchHandle;
152 #endif
153 char* buf;
154 size_t n = strlen(name);
155 if ( name[n - 1] == '/' )
157 buf = new char[n + 1 + 1];
158 sprintf(buf, "%s*", name);
160 else
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);
169 delete [] buf;
171 if ( srchHandle == -1 )
173 return 0;
176 // Loop through names
177 unsigned long count = 0;
180 count++;
182 while ( _findnext(srchHandle, &data) != -1 );
183 _findclose(srchHandle);
184 return count;
187 } // namespace KWSYS_NAMESPACE
189 #else
191 // Now the POSIX style directory access
193 #include <sys/types.h>
194 #include <dirent.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
203 #endif
205 namespace KWSYS_NAMESPACE
208 bool Directory::Load(const char* name)
210 this->Clear();
212 if (!name)
214 return 0;
216 DIR* dir = opendir(name);
218 if (!dir)
220 return 0;
223 for (dirent* d = readdir(dir); d; d = readdir(dir) )
225 this->Internal->Files.push_back(d->d_name);
227 this->Internal->Path = name;
228 closedir(dir);
229 return 1;
232 unsigned long Directory::GetNumberOfFilesInDirectory(const char* name)
234 DIR* dir = opendir(name);
236 if (!dir)
238 return 0;
241 unsigned long count = 0;
242 for (dirent* d = readdir(dir); d; d = readdir(dir) )
244 count++;
246 closedir(dir);
247 return count;
250 } // namespace KWSYS_NAMESPACE
252 #endif