Directory.cxx 6.2 KB

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