Directory.cxx 5.2 KB

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