1
0

readdir.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "precomp_wasabi_bfc.h"
  2. #include "readdir.h"
  3. #ifdef _WIN32
  4. #include <shlwapi.h>
  5. #endif
  6. #if !defined(WIN32) && !defined(LINUX)
  7. #error port me
  8. #endif
  9. //PORT
  10. ReadDir::ReadDir( const wchar_t *_path, const wchar_t *_match, bool _skipdots ) : skipdots( _skipdots ), first( 1 ), path( _path ), match( _match )
  11. {
  12. files = INVALID_HANDLE_VALUE;
  13. if ( match.isempty() )
  14. match = MATCHALLFILES;
  15. ZERO( data );
  16. }
  17. ReadDir::~ReadDir()
  18. {
  19. //PORT
  20. #ifdef WIN32
  21. if ( files != INVALID_HANDLE_VALUE ) FindClose( files );
  22. #endif
  23. #ifdef LINUX
  24. if ( d != NULL ) closedir( d );
  25. #endif
  26. }
  27. int ReadDir::next()
  28. {
  29. //PORT
  30. #ifdef WIN32
  31. for ( ;;)
  32. {
  33. if ( first )
  34. {
  35. wchar_t fullpath[ MAX_PATH ];
  36. PathCombineW( fullpath, path.getValue(), match.getValue() );
  37. files = FindFirstFileW( fullpath, &data );
  38. }
  39. if ( files == INVALID_HANDLE_VALUE ) return 0;
  40. if ( first )
  41. {
  42. first = 0;
  43. if ( skipdots && ( isDotDir() || isDotDotDir() ) ) continue;
  44. return 1;
  45. }
  46. if ( !FindNextFileW( files, &data ) ) return 0;
  47. if ( skipdots && ( isDotDir() || isDotDotDir() ) ) continue;
  48. return 1;
  49. }
  50. #endif//WIN32
  51. #ifdef LINUX
  52. path.AddBackslash();
  53. if ( first || d == NULL )
  54. {
  55. if ( !( d = opendir( path ) ) ) return 0;
  56. first = 0;
  57. }
  58. while ( 1 )
  59. {
  60. de = readdir( d );
  61. if ( !de )
  62. {
  63. closedir( d );
  64. d = NULL;
  65. return 0;
  66. }
  67. StringW full;
  68. full.printf( L"%s%s", path.v(), de->d_name );
  69. if ( stat( full, &st ) == -1 )
  70. continue;
  71. if ( skipdots && ( isDotDir() || isDotDotDir() ) ) continue;
  72. if ( !Std::match( match, de->d_name ) ) continue;
  73. return 1;
  74. }
  75. #endif
  76. }
  77. const wchar_t *ReadDir::getFilename()
  78. {
  79. if ( first ) if ( !next() ) return NULL;
  80. //PORT
  81. return data.cFileName;
  82. }
  83. int ReadDir::isDir()
  84. {
  85. //PORT
  86. if ( files == INVALID_HANDLE_VALUE ) return 0;
  87. return !!( data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY );
  88. }
  89. int ReadDir::isReadonly()
  90. {
  91. //PORT
  92. if ( files == INVALID_HANDLE_VALUE ) return 0;
  93. return !!( data.dwFileAttributes & FILE_ATTRIBUTE_READONLY );
  94. }
  95. int ReadDir::isDotDir()
  96. {
  97. //PORT
  98. if ( files == INVALID_HANDLE_VALUE ) return 0;
  99. return ( data.cFileName[ 0 ] == '.' && data.cFileName[ 1 ] == 0 );
  100. }
  101. int ReadDir::isDotDotDir()
  102. {
  103. //PORT
  104. if ( files == INVALID_HANDLE_VALUE ) return 0;
  105. return ( data.cFileName[ 0 ] == '.' && data.cFileName[ 1 ] == '.' && data.cFileName[ 2 ] == 0 );
  106. }