1
0

Directory.cxx 3.2 KB

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