Directory.cxx 3.2 KB

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