cmDirectory.cxx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  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 Copyright.txt or 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 "cmDirectory.h"
  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. /**
  26. *
  27. */
  28. bool
  29. cmDirectory
  30. ::Load(const char* name)
  31. {
  32. char* buf;
  33. size_t n = strlen(name);
  34. if ( name[n - 1] == '/' )
  35. {
  36. buf = new char[n + 1 + 1];
  37. sprintf(buf, "%s*", name);
  38. }
  39. else
  40. {
  41. buf = new char[n + 2 + 1];
  42. sprintf(buf, "%s/*", name);
  43. }
  44. struct _finddata_t data; // data of current file
  45. // Now put them into the file array
  46. size_t srchHandle = _findfirst(buf, &data);
  47. delete [] buf;
  48. if ( srchHandle == -1 )
  49. {
  50. return 0;
  51. }
  52. // Loop through names
  53. do
  54. {
  55. m_Files.push_back(data.name);
  56. }
  57. while ( _findnext(srchHandle, &data) != -1 );
  58. m_Path = name;
  59. return _findclose(srchHandle) != -1;
  60. }
  61. #else
  62. // Now the POSIX style directory access
  63. #include <sys/types.h>
  64. #include <dirent.h>
  65. /**
  66. *
  67. */
  68. bool
  69. cmDirectory
  70. ::Load(const char* name)
  71. {
  72. DIR* dir = opendir(name);
  73. if (!dir)
  74. {
  75. return 0;
  76. }
  77. for (dirent* d = readdir(dir); d; d = readdir(dir) )
  78. {
  79. m_Files.push_back(d->d_name);
  80. }
  81. m_Path = name;
  82. closedir(dir);
  83. return 1;
  84. }
  85. #endif
  86. /**
  87. *
  88. */
  89. const char*
  90. cmDirectory
  91. ::GetFile(size_t dindex)
  92. {
  93. if ( dindex >= m_Files.size() )
  94. {
  95. cmSystemTools::Error("Bad index for GetFile on cmDirectory\n", 0);
  96. return 0;
  97. }
  98. return m_Files[dindex].c_str();
  99. }