PageRenderTime 60ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/Modules/ThirdParty/KWSys/src/KWSys/Directory.cxx

https://github.com/chrismullins/ITK
C++ | 253 lines | 179 code | 39 blank | 35 comment | 23 complexity | 5f2145a6a1437712d57ccd1fcda2f848 MD5 | raw file
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "kwsysPrivate.h"
  11. #include KWSYS_HEADER(Directory.hxx)
  12. #include KWSYS_HEADER(Configure.hxx)
  13. #include KWSYS_HEADER(Encoding.hxx)
  14. #include KWSYS_HEADER(stl/string)
  15. #include KWSYS_HEADER(stl/vector)
  16. // Work-around CMake dependency scanning limitation. This must
  17. // duplicate the above list of headers.
  18. #if 0
  19. # include "Directory.hxx.in"
  20. # include "Configure.hxx.in"
  21. # include "Encoding.hxx.in"
  22. # include "kwsys_stl.hxx.in"
  23. # include "kwsys_stl_string.hxx.in"
  24. # include "kwsys_stl_vector.hxx.in"
  25. #endif
  26. namespace KWSYS_NAMESPACE
  27. {
  28. //----------------------------------------------------------------------------
  29. class DirectoryInternals
  30. {
  31. public:
  32. // Array of Files
  33. kwsys_stl::vector<kwsys_stl::string> Files;
  34. // Path to Open'ed directory
  35. kwsys_stl::string Path;
  36. };
  37. //----------------------------------------------------------------------------
  38. Directory::Directory()
  39. {
  40. this->Internal = new DirectoryInternals;
  41. }
  42. //----------------------------------------------------------------------------
  43. Directory::~Directory()
  44. {
  45. delete this->Internal;
  46. }
  47. //----------------------------------------------------------------------------
  48. unsigned long Directory::GetNumberOfFiles() const
  49. {
  50. return static_cast<unsigned long>(this->Internal->Files.size());
  51. }
  52. //----------------------------------------------------------------------------
  53. const char* Directory::GetFile(unsigned long dindex) const
  54. {
  55. if ( dindex >= this->Internal->Files.size() )
  56. {
  57. return 0;
  58. }
  59. return this->Internal->Files[dindex].c_str();
  60. }
  61. //----------------------------------------------------------------------------
  62. const char* Directory::GetPath() const
  63. {
  64. return this->Internal->Path.c_str();
  65. }
  66. //----------------------------------------------------------------------------
  67. void Directory::Clear()
  68. {
  69. this->Internal->Path.resize(0);
  70. this->Internal->Files.clear();
  71. }
  72. } // namespace KWSYS_NAMESPACE
  73. // First microsoft compilers
  74. #if defined(_MSC_VER) || defined(__WATCOMC__)
  75. #include <windows.h>
  76. #include <io.h>
  77. #include <ctype.h>
  78. #include <fcntl.h>
  79. #include <stdio.h>
  80. #include <stdlib.h>
  81. #include <string.h>
  82. #include <sys/stat.h>
  83. #include <sys/types.h>
  84. namespace KWSYS_NAMESPACE
  85. {
  86. bool Directory::Load(const kwsys_stl::string& name)
  87. {
  88. this->Clear();
  89. #if _MSC_VER < 1300
  90. long srchHandle;
  91. #else
  92. intptr_t srchHandle;
  93. #endif
  94. char* buf;
  95. size_t n = name.size();
  96. if ( *name.rbegin() == '/' || *name.rbegin() == '\\' )
  97. {
  98. buf = new char[n + 1 + 1];
  99. sprintf(buf, "%s*", name.c_str());
  100. }
  101. else
  102. {
  103. // Make sure the slashes in the wildcard suffix are consistent with the
  104. // rest of the path
  105. buf = new char[n + 2 + 1];
  106. if ( name.find('\\') != name.npos )
  107. {
  108. sprintf(buf, "%s\\*", name.c_str());
  109. }
  110. else
  111. {
  112. sprintf(buf, "%s/*", name.c_str());
  113. }
  114. }
  115. struct _wfinddata_t data; // data of current file
  116. // Now put them into the file array
  117. srchHandle = _wfindfirst((wchar_t*)Encoding::ToWide(buf).c_str(), &data);
  118. delete [] buf;
  119. if ( srchHandle == -1 )
  120. {
  121. return 0;
  122. }
  123. // Loop through names
  124. do
  125. {
  126. this->Internal->Files.push_back(Encoding::ToNarrow(data.name));
  127. }
  128. while ( _wfindnext(srchHandle, &data) != -1 );
  129. this->Internal->Path = name;
  130. return _findclose(srchHandle) != -1;
  131. }
  132. unsigned long Directory::GetNumberOfFilesInDirectory(const kwsys_stl::string& name)
  133. {
  134. #if _MSC_VER < 1300
  135. long srchHandle;
  136. #else
  137. intptr_t srchHandle;
  138. #endif
  139. char* buf;
  140. size_t n = name.size();
  141. if ( *name.rbegin() == '/' )
  142. {
  143. buf = new char[n + 1 + 1];
  144. sprintf(buf, "%s*", name.c_str());
  145. }
  146. else
  147. {
  148. buf = new char[n + 2 + 1];
  149. sprintf(buf, "%s/*", name.c_str());
  150. }
  151. struct _wfinddata_t data; // data of current file
  152. // Now put them into the file array
  153. srchHandle = _wfindfirst((wchar_t*)Encoding::ToWide(buf).c_str(), &data);
  154. delete [] buf;
  155. if ( srchHandle == -1 )
  156. {
  157. return 0;
  158. }
  159. // Loop through names
  160. unsigned long count = 0;
  161. do
  162. {
  163. count++;
  164. }
  165. while ( _wfindnext(srchHandle, &data) != -1 );
  166. _findclose(srchHandle);
  167. return count;
  168. }
  169. } // namespace KWSYS_NAMESPACE
  170. #else
  171. // Now the POSIX style directory access
  172. #include <sys/types.h>
  173. #include <dirent.h>
  174. /* There is a problem with the Portland compiler, large file
  175. support and glibc/Linux system headers:
  176. http://www.pgroup.com/userforum/viewtopic.php?
  177. p=1992&sid=f16167f51964f1a68fe5041b8eb213b6
  178. */
  179. #if defined(__PGI) && defined(__USE_FILE_OFFSET64)
  180. # define dirent dirent64
  181. #endif
  182. namespace KWSYS_NAMESPACE
  183. {
  184. bool Directory::Load(const kwsys_stl::string& name)
  185. {
  186. this->Clear();
  187. DIR* dir = opendir(name.c_str());
  188. if (!dir)
  189. {
  190. return 0;
  191. }
  192. for (dirent* d = readdir(dir); d; d = readdir(dir) )
  193. {
  194. this->Internal->Files.push_back(d->d_name);
  195. }
  196. this->Internal->Path = name;
  197. closedir(dir);
  198. return 1;
  199. }
  200. unsigned long Directory::GetNumberOfFilesInDirectory(const kwsys_stl::string& name)
  201. {
  202. DIR* dir = opendir(name.c_str());
  203. unsigned long count = 0;
  204. for (dirent* d = readdir(dir); d; d = readdir(dir) )
  205. {
  206. count++;
  207. }
  208. closedir(dir);
  209. return count;
  210. }
  211. } // namespace KWSYS_NAMESPACE
  212. #endif