Directory.cxx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. // First microsoft compilers
  15. #ifdef _MSC_VER
  16. #include <windows.h>
  17. #include <io.h>
  18. #include <ctype.h>
  19. #include <fcntl.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/stat.h>
  24. #include <sys/types.h>
  25. namespace KWSYS_NAMESPACE
  26. {
  27. bool Directory::Load(const char* name)
  28. {
  29. char* buf;
  30. size_t n = strlen(name);
  31. if ( name[n - 1] == '/' )
  32. {
  33. buf = new char[n + 1 + 1];
  34. sprintf(buf, "%s*", name);
  35. }
  36. else
  37. {
  38. buf = new char[n + 2 + 1];
  39. sprintf(buf, "%s/*", name);
  40. }
  41. struct _finddata_t data; // data of current file
  42. // Now put them into the file array
  43. size_t srchHandle = _findfirst(buf, &data);
  44. delete [] buf;
  45. if ( srchHandle == -1 )
  46. {
  47. return 0;
  48. }
  49. // Loop through names
  50. do
  51. {
  52. m_Files.push_back(data.name);
  53. }
  54. while ( _findnext(srchHandle, &data) != -1 );
  55. m_Path = name;
  56. return _findclose(srchHandle) != -1;
  57. }
  58. } // namespace KWSYS_NAMESPACE
  59. #else
  60. // Now the POSIX style directory access
  61. #include <sys/types.h>
  62. #include <dirent.h>
  63. namespace KWSYS_NAMESPACE
  64. {
  65. bool Directory::Load(const char* name)
  66. {
  67. DIR* dir = opendir(name);
  68. if (!dir)
  69. {
  70. return 0;
  71. }
  72. for (dirent* d = readdir(dir); d; d = readdir(dir) )
  73. {
  74. m_Files.push_back(d->d_name);
  75. }
  76. m_Path = name;
  77. closedir(dir);
  78. return 1;
  79. }
  80. } // namespace KWSYS_NAMESPACE
  81. #endif
  82. namespace KWSYS_NAMESPACE
  83. {
  84. const char* Directory::GetFile(size_t dindex)
  85. {
  86. if ( dindex >= m_Files.size() )
  87. {
  88. return 0;
  89. }
  90. return m_Files[dindex].c_str();
  91. }
  92. } // namespace KWSYS_NAMESPACE