Directory.cxx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #endif
  23. namespace KWSYS_NAMESPACE
  24. {
  25. //----------------------------------------------------------------------------
  26. class DirectoryInternals
  27. {
  28. public:
  29. // Array of Files
  30. kwsys_stl::vector<kwsys_stl::string> Files;
  31. // Path to Open'ed directory
  32. kwsys_stl::string Path;
  33. };
  34. //----------------------------------------------------------------------------
  35. Directory::Directory()
  36. {
  37. this->Internal = new DirectoryInternals;
  38. }
  39. //----------------------------------------------------------------------------
  40. Directory::~Directory()
  41. {
  42. delete this->Internal;
  43. }
  44. //----------------------------------------------------------------------------
  45. unsigned long Directory::GetNumberOfFiles()
  46. {
  47. return static_cast<unsigned long>(this->Internal->Files.size());
  48. }
  49. //----------------------------------------------------------------------------
  50. const char* Directory::GetFile(unsigned long dindex)
  51. {
  52. if ( dindex >= this->Internal->Files.size() )
  53. {
  54. return 0;
  55. }
  56. return this->Internal->Files[dindex].c_str();
  57. }
  58. } // namespace KWSYS_NAMESPACE
  59. // First microsoft compilers
  60. #ifdef _MSC_VER
  61. #include <windows.h>
  62. #include <io.h>
  63. #include <ctype.h>
  64. #include <fcntl.h>
  65. #include <stdio.h>
  66. #include <stdlib.h>
  67. #include <string.h>
  68. #include <sys/stat.h>
  69. #include <sys/types.h>
  70. namespace KWSYS_NAMESPACE
  71. {
  72. bool Directory::Load(const char* name)
  73. {
  74. #if _MSC_VER < 1300
  75. long srchHandle;
  76. #else
  77. intptr_t srchHandle;
  78. #endif
  79. char* buf;
  80. size_t n = strlen(name);
  81. if ( name[n - 1] == '/' )
  82. {
  83. buf = new char[n + 1 + 1];
  84. sprintf(buf, "%s*", name);
  85. }
  86. else
  87. {
  88. buf = new char[n + 2 + 1];
  89. sprintf(buf, "%s/*", name);
  90. }
  91. struct _finddata_t data; // data of current file
  92. // Now put them into the file array
  93. srchHandle = _findfirst(buf, &data);
  94. delete [] buf;
  95. if ( srchHandle == -1 )
  96. {
  97. return 0;
  98. }
  99. // Loop through names
  100. do
  101. {
  102. this->Internal->Files.push_back(data.name);
  103. }
  104. while ( _findnext(srchHandle, &data) != -1 );
  105. this->Internal->Path = name;
  106. return _findclose(srchHandle) != -1;
  107. }
  108. } // namespace KWSYS_NAMESPACE
  109. #else
  110. // Now the POSIX style directory access
  111. #include <sys/types.h>
  112. #include <dirent.h>
  113. namespace KWSYS_NAMESPACE
  114. {
  115. bool Directory::Load(const char* name)
  116. {
  117. DIR* dir = opendir(name);
  118. if (!dir)
  119. {
  120. return 0;
  121. }
  122. for (dirent* d = readdir(dir); d; d = readdir(dir) )
  123. {
  124. this->Internal->Files.push_back(d->d_name);
  125. }
  126. this->Internal->Path = name;
  127. closedir(dir);
  128. return 1;
  129. }
  130. } // namespace KWSYS_NAMESPACE
  131. #endif