Directory.cxx 3.1 KB

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