Directory.cxx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "kwsysPrivate.h"
  11. #include KWSYS_HEADER(Directory.hxx)
  12. #include KWSYS_HEADER(Configure.hxx)
  13. #include KWSYS_HEADER(Encoding.hxx)
  14. #include KWSYS_HEADER(stl/string)
  15. #include KWSYS_HEADER(stl/vector)
  16. // Work-around CMake dependency scanning limitation. This must
  17. // duplicate the above list of headers.
  18. #if 0
  19. # include "Directory.hxx.in"
  20. # include "Configure.hxx.in"
  21. # include "Encoding.hxx.in"
  22. # include "kwsys_stl.hxx.in"
  23. # include "kwsys_stl_string.hxx.in"
  24. # include "kwsys_stl_vector.hxx.in"
  25. #endif
  26. namespace KWSYS_NAMESPACE
  27. {
  28. //----------------------------------------------------------------------------
  29. class DirectoryInternals
  30. {
  31. public:
  32. // Array of Files
  33. kwsys_stl::vector<kwsys_stl::string> Files;
  34. // Path to Open'ed directory
  35. kwsys_stl::string Path;
  36. };
  37. //----------------------------------------------------------------------------
  38. Directory::Directory()
  39. {
  40. this->Internal = new DirectoryInternals;
  41. }
  42. //----------------------------------------------------------------------------
  43. Directory::~Directory()
  44. {
  45. delete this->Internal;
  46. }
  47. //----------------------------------------------------------------------------
  48. unsigned long Directory::GetNumberOfFiles() const
  49. {
  50. return static_cast<unsigned long>(this->Internal->Files.size());
  51. }
  52. //----------------------------------------------------------------------------
  53. const char* Directory::GetFile(unsigned long dindex) const
  54. {
  55. if ( dindex >= this->Internal->Files.size() )
  56. {
  57. return 0;
  58. }
  59. return this->Internal->Files[dindex].c_str();
  60. }
  61. //----------------------------------------------------------------------------
  62. const char* Directory::GetPath() const
  63. {
  64. return this->Internal->Path.c_str();
  65. }
  66. //----------------------------------------------------------------------------
  67. void Directory::Clear()
  68. {
  69. this->Internal->Path.resize(0);
  70. this->Internal->Files.clear();
  71. }
  72. } // namespace KWSYS_NAMESPACE
  73. // First microsoft compilers
  74. #if defined(_MSC_VER) || defined(__WATCOMC__)
  75. #include <windows.h>
  76. #include <io.h>
  77. #include <ctype.h>
  78. #include <fcntl.h>
  79. #include <stdio.h>
  80. #include <stdlib.h>
  81. #include <string.h>
  82. #include <sys/stat.h>
  83. #include <sys/types.h>
  84. namespace KWSYS_NAMESPACE
  85. {
  86. bool Directory::Load(const kwsys_stl::string& name)
  87. {
  88. this->Clear();
  89. #if _MSC_VER < 1300
  90. long srchHandle;
  91. #else
  92. intptr_t srchHandle;
  93. #endif
  94. char* buf;
  95. size_t n = name.size();
  96. if ( *name.rbegin() == '/' || *name.rbegin() == '\\' )
  97. {
  98. buf = new char[n + 1 + 1];
  99. sprintf(buf, "%s*", name.c_str());
  100. }
  101. else
  102. {
  103. // Make sure the slashes in the wildcard suffix are consistent with the
  104. // rest of the path
  105. buf = new char[n + 2 + 1];
  106. if ( name.find('\\') != name.npos )
  107. {
  108. sprintf(buf, "%s\\*", name.c_str());
  109. }
  110. else
  111. {
  112. sprintf(buf, "%s/*", name.c_str());
  113. }
  114. }
  115. struct _wfinddata_t data; // data of current file
  116. // Now put them into the file array
  117. srchHandle = _wfindfirst((wchar_t*)Encoding::ToWide(buf).c_str(), &data);
  118. delete [] buf;
  119. if ( srchHandle == -1 )
  120. {
  121. return 0;
  122. }
  123. // Loop through names
  124. do
  125. {
  126. this->Internal->Files.push_back(Encoding::ToNarrow(data.name));
  127. }
  128. while ( _wfindnext(srchHandle, &data) != -1 );
  129. this->Internal->Path = name;
  130. return _findclose(srchHandle) != -1;
  131. }
  132. unsigned long Directory::GetNumberOfFilesInDirectory(const kwsys_stl::string& name)
  133. {
  134. #if _MSC_VER < 1300
  135. long srchHandle;
  136. #else
  137. intptr_t srchHandle;
  138. #endif
  139. char* buf;
  140. size_t n = name.size();
  141. if ( *name.rbegin() == '/' )
  142. {
  143. buf = new char[n + 1 + 1];
  144. sprintf(buf, "%s*", name.c_str());
  145. }
  146. else
  147. {
  148. buf = new char[n + 2 + 1];
  149. sprintf(buf, "%s/*", name.c_str());
  150. }
  151. struct _wfinddata_t data; // data of current file
  152. // Now put them into the file array
  153. srchHandle = _wfindfirst((wchar_t*)Encoding::ToWide(buf).c_str(), &data);
  154. delete [] buf;
  155. if ( srchHandle == -1 )
  156. {
  157. return 0;
  158. }
  159. // Loop through names
  160. unsigned long count = 0;
  161. do
  162. {
  163. count++;
  164. }
  165. while ( _wfindnext(srchHandle, &data) != -1 );
  166. _findclose(srchHandle);
  167. return count;
  168. }
  169. } // namespace KWSYS_NAMESPACE
  170. #else
  171. // Now the POSIX style directory access
  172. #include <sys/types.h>
  173. #include <dirent.h>
  174. // PGI with glibc has trouble with dirent and large file support:
  175. // http://www.pgroup.com/userforum/viewtopic.php?
  176. // p=1992&sid=f16167f51964f1a68fe5041b8eb213b6
  177. // Work around the problem by mapping dirent the same way as readdir.
  178. #if defined(__PGI) && defined(__GLIBC__)
  179. # define kwsys_dirent_readdir dirent
  180. # define kwsys_dirent_readdir64 dirent64
  181. # define kwsys_dirent kwsys_dirent_lookup(readdir)
  182. # define kwsys_dirent_lookup(x) kwsys_dirent_lookup_delay(x)
  183. # define kwsys_dirent_lookup_delay(x) kwsys_dirent_##x
  184. #else
  185. # define kwsys_dirent dirent
  186. #endif
  187. namespace KWSYS_NAMESPACE
  188. {
  189. bool Directory::Load(const kwsys_stl::string& name)
  190. {
  191. this->Clear();
  192. DIR* dir = opendir(name.c_str());
  193. if (!dir)
  194. {
  195. return 0;
  196. }
  197. for (kwsys_dirent* d = readdir(dir); d; d = readdir(dir) )
  198. {
  199. this->Internal->Files.push_back(d->d_name);
  200. }
  201. this->Internal->Path = name;
  202. closedir(dir);
  203. return 1;
  204. }
  205. unsigned long Directory::GetNumberOfFilesInDirectory(const kwsys_stl::string& name)
  206. {
  207. DIR* dir = opendir(name.c_str());
  208. unsigned long count = 0;
  209. for (kwsys_dirent* d = readdir(dir); d; d = readdir(dir) )
  210. {
  211. count++;
  212. }
  213. closedir(dir);
  214. return count;
  215. }
  216. } // namespace KWSYS_NAMESPACE
  217. #endif