Directory.cxx 5.5 KB

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