Directory.cxx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*=========================================================================
  2. Program: KWSys - Kitware System Library
  3. Module: $RCSfile$
  4. Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
  5. See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  8. PURPOSE. See the above copyright notices for more information.
  9. =========================================================================*/
  10. #include "kwsysPrivate.h"
  11. #include KWSYS_HEADER(Directory.hxx)
  12. #include KWSYS_HEADER(Configure.hxx)
  13. #include KWSYS_HEADER(stl/string)
  14. #include KWSYS_HEADER(stl/vector)
  15. // Work-around CMake dependency scanning limitation. This must
  16. // duplicate the above list of headers.
  17. #if 0
  18. # include "Directory.hxx.in"
  19. # include "Configure.hxx.in"
  20. # include "kwsys_stl.hxx.in"
  21. # include "kwsys_stl_string.hxx.in"
  22. # include "kwsys_stl_vector.hxx.in"
  23. #endif
  24. namespace KWSYS_NAMESPACE
  25. {
  26. //----------------------------------------------------------------------------
  27. class DirectoryInternals
  28. {
  29. public:
  30. // Array of Files
  31. kwsys_stl::vector<kwsys_stl::string> Files;
  32. // Path to Open'ed directory
  33. kwsys_stl::string Path;
  34. };
  35. //----------------------------------------------------------------------------
  36. Directory::Directory()
  37. {
  38. this->Internal = new DirectoryInternals;
  39. }
  40. //----------------------------------------------------------------------------
  41. Directory::~Directory()
  42. {
  43. delete this->Internal;
  44. }
  45. //----------------------------------------------------------------------------
  46. unsigned long Directory::GetNumberOfFiles() const
  47. {
  48. return static_cast<unsigned long>(this->Internal->Files.size());
  49. }
  50. //----------------------------------------------------------------------------
  51. const char* Directory::GetFile(unsigned long dindex) const
  52. {
  53. if ( dindex >= this->Internal->Files.size() )
  54. {
  55. return 0;
  56. }
  57. return this->Internal->Files[dindex].c_str();
  58. }
  59. //----------------------------------------------------------------------------
  60. const char* Directory::GetPath() const
  61. {
  62. return this->Internal->Path.c_str();
  63. }
  64. //----------------------------------------------------------------------------
  65. void Directory::Clear()
  66. {
  67. this->Internal->Path.resize(0);
  68. this->Internal->Files.clear();
  69. }
  70. } // namespace KWSYS_NAMESPACE
  71. // First microsoft compilers
  72. #if defined(_MSC_VER) || defined(__WATCOMC__)
  73. #include <windows.h>
  74. #include <io.h>
  75. #include <ctype.h>
  76. #include <fcntl.h>
  77. #include <stdio.h>
  78. #include <stdlib.h>
  79. #include <string.h>
  80. #include <sys/stat.h>
  81. #include <sys/types.h>
  82. namespace KWSYS_NAMESPACE
  83. {
  84. bool Directory::Load(const char* name)
  85. {
  86. this->Clear();
  87. #if _MSC_VER < 1300
  88. long srchHandle;
  89. #else
  90. intptr_t srchHandle;
  91. #endif
  92. char* buf;
  93. size_t n = strlen(name);
  94. if ( name[n - 1] == '/' )
  95. {
  96. buf = new char[n + 1 + 1];
  97. sprintf(buf, "%s*", name);
  98. }
  99. else
  100. {
  101. buf = new char[n + 2 + 1];
  102. sprintf(buf, "%s/*", name);
  103. }
  104. struct _finddata_t data; // data of current file
  105. // Now put them into the file array
  106. srchHandle = _findfirst(buf, &data);
  107. delete [] buf;
  108. if ( srchHandle == -1 )
  109. {
  110. return 0;
  111. }
  112. // Loop through names
  113. do
  114. {
  115. this->Internal->Files.push_back(data.name);
  116. }
  117. while ( _findnext(srchHandle, &data) != -1 );
  118. this->Internal->Path = name;
  119. return _findclose(srchHandle) != -1;
  120. }
  121. } // namespace KWSYS_NAMESPACE
  122. #else
  123. // Now the POSIX style directory access
  124. #include <sys/types.h>
  125. #include <dirent.h>
  126. namespace KWSYS_NAMESPACE
  127. {
  128. bool Directory::Load(const char* name)
  129. {
  130. this->Clear();
  131. DIR* dir = opendir(name);
  132. if (!dir)
  133. {
  134. return 0;
  135. }
  136. for (dirent* d = readdir(dir); d; d = readdir(dir) )
  137. {
  138. this->Internal->Files.push_back(d->d_name);
  139. }
  140. this->Internal->Path = name;
  141. closedir(dir);
  142. return 1;
  143. }
  144. } // namespace KWSYS_NAMESPACE
  145. #endif