Directory.cxx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.clear();
  68. this->Internal->Path = "";
  69. this->Internal->Files.clear();
  70. }
  71. } // namespace KWSYS_NAMESPACE
  72. // First microsoft compilers
  73. #if defined(_MSC_VER) || defined(__WATCOMC__)
  74. #include <windows.h>
  75. #include <io.h>
  76. #include <ctype.h>
  77. #include <fcntl.h>
  78. #include <stdio.h>
  79. #include <stdlib.h>
  80. #include <string.h>
  81. #include <sys/stat.h>
  82. #include <sys/types.h>
  83. namespace KWSYS_NAMESPACE
  84. {
  85. bool Directory::Load(const char* name)
  86. {
  87. this->Clear();
  88. #if _MSC_VER < 1300
  89. long srchHandle;
  90. #else
  91. intptr_t srchHandle;
  92. #endif
  93. char* buf;
  94. size_t n = strlen(name);
  95. if ( name[n - 1] == '/' )
  96. {
  97. buf = new char[n + 1 + 1];
  98. sprintf(buf, "%s*", name);
  99. }
  100. else
  101. {
  102. buf = new char[n + 2 + 1];
  103. sprintf(buf, "%s/*", name);
  104. }
  105. struct _finddata_t data; // data of current file
  106. // Now put them into the file array
  107. srchHandle = _findfirst(buf, &data);
  108. delete [] buf;
  109. if ( srchHandle == -1 )
  110. {
  111. return 0;
  112. }
  113. // Loop through names
  114. do
  115. {
  116. this->Internal->Files.push_back(data.name);
  117. }
  118. while ( _findnext(srchHandle, &data) != -1 );
  119. this->Internal->Path = name;
  120. return _findclose(srchHandle) != -1;
  121. }
  122. } // namespace KWSYS_NAMESPACE
  123. #else
  124. // Now the POSIX style directory access
  125. #include <sys/types.h>
  126. #include <dirent.h>
  127. namespace KWSYS_NAMESPACE
  128. {
  129. bool Directory::Load(const char* name)
  130. {
  131. this->Clear();
  132. DIR* dir = opendir(name);
  133. if (!dir)
  134. {
  135. return 0;
  136. }
  137. for (dirent* d = readdir(dir); d; d = readdir(dir) )
  138. {
  139. this->Internal->Files.push_back(d->d_name);
  140. }
  141. this->Internal->Path = name;
  142. closedir(dir);
  143. return 1;
  144. }
  145. } // namespace KWSYS_NAMESPACE
  146. #endif