Directory.cxx 3.1 KB

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