Directory.cxx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. # include "kwsys_stl_vector.hxx.in"
  23. #endif
  24. namespace KWSYS_NAMESPACE
  25. {
  26. //----------------------------------------------------------------------------
  27. class DirectoryInternals
  28. {
  29. public:
  30. // Array of Files
  31. kwsys_stl::vector<kwsys_stl::string> Files;
  32. // Path to Open'ed directory
  33. kwsys_stl::string Path;
  34. };
  35. //----------------------------------------------------------------------------
  36. Directory::Directory()
  37. {
  38. this->Internal = new DirectoryInternals;
  39. }
  40. //----------------------------------------------------------------------------
  41. Directory::~Directory()
  42. {
  43. delete this->Internal;
  44. }
  45. //----------------------------------------------------------------------------
  46. unsigned long Directory::GetNumberOfFiles() const
  47. {
  48. return static_cast<unsigned long>(this->Internal->Files.size());
  49. }
  50. //----------------------------------------------------------------------------
  51. const char* Directory::GetFile(unsigned long dindex) const
  52. {
  53. if ( dindex >= this->Internal->Files.size() )
  54. {
  55. return 0;
  56. }
  57. return this->Internal->Files[dindex].c_str();
  58. }
  59. //----------------------------------------------------------------------------
  60. const char* Directory::GetPath() const
  61. {
  62. return this->Internal->Path.c_str();
  63. }
  64. //----------------------------------------------------------------------------
  65. void Directory::Clear()
  66. {
  67. this->Internal->Path.resize(0);
  68. this->Internal->Files.clear();
  69. }
  70. } // namespace KWSYS_NAMESPACE
  71. // First microsoft compilers
  72. #if defined(_MSC_VER) || defined(__WATCOMC__)
  73. #include <windows.h>
  74. #include <io.h>
  75. #include <ctype.h>
  76. #include <fcntl.h>
  77. #include <stdio.h>
  78. #include <stdlib.h>
  79. #include <string.h>
  80. #include <sys/stat.h>
  81. #include <sys/types.h>
  82. namespace KWSYS_NAMESPACE
  83. {
  84. bool Directory::Load(const char* name)
  85. {
  86. this->Clear();
  87. #if _MSC_VER < 1300
  88. long srchHandle;
  89. #else
  90. intptr_t srchHandle;
  91. #endif
  92. char* buf;
  93. size_t n = strlen(name);
  94. if ( name[n - 1] == '/' )
  95. {
  96. buf = new char[n + 1 + 1];
  97. sprintf(buf, "%s*", name);
  98. }
  99. else
  100. {
  101. buf = new char[n + 2 + 1];
  102. sprintf(buf, "%s/*", name);
  103. }
  104. struct _finddata_t data; // data of current file
  105. // Now put them into the file array
  106. srchHandle = _findfirst(buf, &data);
  107. delete [] buf;
  108. if ( srchHandle == -1 )
  109. {
  110. return 0;
  111. }
  112. // Loop through names
  113. do
  114. {
  115. this->Internal->Files.push_back(data.name);
  116. }
  117. while ( _findnext(srchHandle, &data) != -1 );
  118. this->Internal->Path = name;
  119. return _findclose(srchHandle) != -1;
  120. }
  121. unsigned long Directory::GetNumberOfFilesInDirectory(const char* name)
  122. {
  123. #if _MSC_VER < 1300
  124. long srchHandle;
  125. #else
  126. intptr_t srchHandle;
  127. #endif
  128. char* buf;
  129. size_t n = strlen(name);
  130. if ( name[n - 1] == '/' )
  131. {
  132. buf = new char[n + 1 + 1];
  133. sprintf(buf, "%s*", name);
  134. }
  135. else
  136. {
  137. buf = new char[n + 2 + 1];
  138. sprintf(buf, "%s/*", name);
  139. }
  140. struct _finddata_t data; // data of current file
  141. // Now put them into the file array
  142. srchHandle = _findfirst(buf, &data);
  143. delete [] buf;
  144. if ( srchHandle == -1 )
  145. {
  146. return 0;
  147. }
  148. // Loop through names
  149. unsigned long count = 0;
  150. do
  151. {
  152. count++;
  153. }
  154. while ( _findnext(srchHandle, &data) != -1 );
  155. _findclose(srchHandle);
  156. return count;
  157. }
  158. } // namespace KWSYS_NAMESPACE
  159. #else
  160. // Now the POSIX style directory access
  161. #include <sys/types.h>
  162. #include <dirent.h>
  163. /* There is a problem with the Portland compiler, large file
  164. support and glibc/Linux system headers:
  165. http://www.pgroup.com/userforum/viewtopic.php?
  166. p=1992&sid=f16167f51964f1a68fe5041b8eb213b6
  167. */
  168. #if defined(__PGI) && defined(__USE_FILE_OFFSET64)
  169. # define dirent dirent64
  170. #endif
  171. namespace KWSYS_NAMESPACE
  172. {
  173. bool Directory::Load(const char* name)
  174. {
  175. this->Clear();
  176. if (!name)
  177. {
  178. return 0;
  179. }
  180. DIR* dir = opendir(name);
  181. if (!dir)
  182. {
  183. return 0;
  184. }
  185. for (dirent* d = readdir(dir); d; d = readdir(dir) )
  186. {
  187. this->Internal->Files.push_back(d->d_name);
  188. }
  189. this->Internal->Path = name;
  190. closedir(dir);
  191. return 1;
  192. }
  193. unsigned long Directory::GetNumberOfFilesInDirectory(const char* name)
  194. {
  195. DIR* dir = opendir(name);
  196. if (!dir)
  197. {
  198. return 0;
  199. }
  200. unsigned long count = 0;
  201. for (dirent* d = readdir(dir); d; d = readdir(dir) )
  202. {
  203. count++;
  204. }
  205. closedir(dir);
  206. return count;
  207. }
  208. } // namespace KWSYS_NAMESPACE
  209. #endif