Directory.cxx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. namespace KWSYS_NAMESPACE {
  76. Status Directory::Load(std::string const& name, std::string* errorMessage)
  77. {
  78. this->Clear();
  79. intptr_t srchHandle;
  80. char* buf;
  81. size_t bufLength;
  82. size_t n = name.size();
  83. if (name.back() == '/' || name.back() == '\\') {
  84. bufLength = n + 1 + 1;
  85. buf = new char[bufLength];
  86. snprintf(buf, bufLength, "%s*", name.c_str());
  87. } else {
  88. // Make sure the slashes in the wildcard suffix are consistent with the
  89. // rest of the path
  90. bufLength = n + 2 + 1;
  91. buf = new char[bufLength];
  92. if (name.find('\\') != std::string::npos) {
  93. snprintf(buf, bufLength, "%s\\*", name.c_str());
  94. } else {
  95. snprintf(buf, bufLength, "%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 =
  101. _wfindfirst((wchar_t*)Encoding::ToWindowsExtendedPath(buf).c_str(), &data);
  102. delete[] buf;
  103. if (srchHandle == -1) {
  104. Status status = Status::POSIX_errno();
  105. if (errorMessage) {
  106. *errorMessage = status.GetString();
  107. }
  108. return status;
  109. }
  110. // Loop through names
  111. do {
  112. this->Internal->Files.push_back(Encoding::ToNarrow(data.name));
  113. } while (_wfindnext(srchHandle, &data) != -1);
  114. this->Internal->Path = name;
  115. if (_findclose(srchHandle) == -1) {
  116. Status status = Status::POSIX_errno();
  117. if (errorMessage) {
  118. *errorMessage = status.GetString();
  119. }
  120. return status;
  121. }
  122. return Status::Success();
  123. }
  124. unsigned long Directory::GetNumberOfFilesInDirectory(const std::string& name,
  125. std::string* errorMessage)
  126. {
  127. intptr_t srchHandle;
  128. char* buf;
  129. size_t bufLength;
  130. size_t n = name.size();
  131. if (name.back() == '/') {
  132. bufLength = n + 1 + 1;
  133. buf = new char[n + 1 + 1];
  134. snprintf(buf, bufLength, "%s*", name.c_str());
  135. } else {
  136. bufLength = n + 2 + 1;
  137. buf = new char[n + 2 + 1];
  138. snprintf(buf, bufLength, "%s/*", name.c_str());
  139. }
  140. struct _wfinddata_t data; // data of current file
  141. // Now put them into the file array
  142. srchHandle = _wfindfirst((wchar_t*)Encoding::ToWide(buf).c_str(), &data);
  143. delete[] buf;
  144. if (srchHandle == -1) {
  145. if (errorMessage) {
  146. if (unsigned int errorId = GetLastError()) {
  147. LPSTR message = nullptr;
  148. DWORD size = FormatMessageA(
  149. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
  150. FORMAT_MESSAGE_IGNORE_INSERTS,
  151. nullptr, errorId, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  152. (LPSTR)&message, 0, nullptr);
  153. *errorMessage = std::string(message, size);
  154. LocalFree(message);
  155. } else {
  156. *errorMessage = "Unknown error.";
  157. }
  158. }
  159. return 0;
  160. }
  161. // Loop through names
  162. unsigned long count = 0;
  163. do {
  164. count++;
  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. # include <errno.h>
  175. # include <string.h>
  176. // PGI with glibc has trouble with dirent and large file support:
  177. // http://www.pgroup.com/userforum/viewtopic.php?
  178. // p=1992&sid=f16167f51964f1a68fe5041b8eb213b6
  179. // Work around the problem by mapping dirent the same way as readdir.
  180. # if defined(__PGI) && defined(__GLIBC__)
  181. # define kwsys_dirent_readdir dirent
  182. # define kwsys_dirent_readdir64 dirent64
  183. # define kwsys_dirent kwsys_dirent_lookup(readdir)
  184. # define kwsys_dirent_lookup(x) kwsys_dirent_lookup_delay(x)
  185. # define kwsys_dirent_lookup_delay(x) kwsys_dirent_##x
  186. # else
  187. # define kwsys_dirent dirent
  188. # endif
  189. namespace KWSYS_NAMESPACE {
  190. Status Directory::Load(std::string const& name, std::string* errorMessage)
  191. {
  192. this->Clear();
  193. errno = 0;
  194. DIR* dir = opendir(name.c_str());
  195. if (!dir) {
  196. if (errorMessage != nullptr) {
  197. *errorMessage = std::string(strerror(errno));
  198. }
  199. return Status::POSIX_errno();
  200. }
  201. errno = 0;
  202. for (kwsys_dirent* d = readdir(dir); d; d = readdir(dir)) {
  203. this->Internal->Files.emplace_back(d->d_name);
  204. }
  205. if (errno != 0) {
  206. if (errorMessage != nullptr) {
  207. *errorMessage = std::string(strerror(errno));
  208. }
  209. return Status::POSIX_errno();
  210. }
  211. this->Internal->Path = name;
  212. closedir(dir);
  213. return Status::Success();
  214. }
  215. unsigned long Directory::GetNumberOfFilesInDirectory(const std::string& name,
  216. std::string* errorMessage)
  217. {
  218. errno = 0;
  219. DIR* dir = opendir(name.c_str());
  220. if (!dir) {
  221. if (errorMessage != nullptr) {
  222. *errorMessage = std::string(strerror(errno));
  223. }
  224. return 0;
  225. }
  226. unsigned long count = 0;
  227. for (kwsys_dirent* d = readdir(dir); d; d = readdir(dir)) {
  228. count++;
  229. }
  230. if (errno != 0) {
  231. if (errorMessage != nullptr) {
  232. *errorMessage = std::string(strerror(errno));
  233. }
  234. return false;
  235. }
  236. closedir(dir);
  237. return count;
  238. }
  239. } // namespace KWSYS_NAMESPACE
  240. #endif