cmDirectory.cxx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2000 National Library of Medicine
  8. All rights reserved.
  9. See COPYRIGHT.txt for copyright details.
  10. =========================================================================*/
  11. #include "cmDirectory.h"
  12. // First microsoft compilers
  13. #ifdef _MSC_VER
  14. #include <windows.h>
  15. #include <io.h>
  16. #include <ctype.h>
  17. #include <fcntl.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <sys/stat.h>
  22. #include <sys/types.h>
  23. /**
  24. *
  25. */
  26. bool
  27. cmDirectory
  28. ::Load(const char* name)
  29. {
  30. char* buf;
  31. int n = strlen(name);
  32. if ( name[n - 1] == '/' )
  33. {
  34. buf = new char[n + 1 + 1];
  35. sprintf(buf, "%s*", name);
  36. }
  37. else
  38. {
  39. buf = new char[n + 2 + 1];
  40. sprintf(buf, "%s/*", name);
  41. }
  42. struct _finddata_t data; // data of current file
  43. // Now put them into the file array
  44. long srchHandle = _findfirst(buf, &data);
  45. delete [] buf;
  46. if ( srchHandle == -1 )
  47. {
  48. return 0;
  49. }
  50. // Loop through names
  51. do
  52. {
  53. m_Files.push_back(data.name);
  54. }
  55. while ( _findnext(srchHandle, &data) != -1 );
  56. m_Path = name;
  57. return _findclose(srchHandle) != -1;
  58. }
  59. #else
  60. // Now the POSIX style directory access
  61. #include <sys/types.h>
  62. #include <dirent.h>
  63. /**
  64. *
  65. */
  66. bool
  67. cmDirectory
  68. ::Load(const char* name)
  69. {
  70. DIR* dir = opendir(name);
  71. if ( !dir )
  72. {
  73. return 0;
  74. }
  75. dir = opendir(name);
  76. for (dirent* d = readdir(dir); d; d = readdir(dir) )
  77. {
  78. m_Files.push_back(d->d_name);
  79. }
  80. m_Path = name;
  81. closedir(dir);
  82. return 1;
  83. }
  84. #endif
  85. /**
  86. *
  87. */
  88. const char*
  89. cmDirectory
  90. ::GetFile(unsigned int index)
  91. {
  92. if ( index >= m_Files.size() )
  93. {
  94. cmSystemTools::Error("Bad index for GetFile on cmDirectory\n", 0);
  95. return 0;
  96. }
  97. return m_Files[index].c_str();
  98. }