dir_entries.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // <dirent.h> C++-wrapper
  2. //
  3. // Platform: ISO C++ 98/11 - POSIX
  4. // $Id$
  5. //
  6. // (c) __vic 2010
  7. #ifndef __VIC_POSIX_DIR_ENTRIES_H
  8. #define __VIC_POSIX_DIR_ENTRIES_H
  9. #include<__vic/defs.h>
  10. #include<dirent.h>
  11. #include<sys/types.h>
  12. #if defined(__FreeBSD__) && !defined(_DIRENT_HAVE_D_TYPE)
  13. #define _DIRENT_HAVE_D_TYPE 1
  14. #endif
  15. // Don't use readdir_r() since glibc-2.24 (Linux) and FreeBSD 12
  16. #if (defined(__GLIBC__) && (__GLIBC__ == 2 && __GLIBC_MINOR >= 24) || __GLIBC__ > 2) || \
  17. (defined(__FreeBSD__) && __FreeBSD__ >= 12)
  18. #define __VIC_USE_READDIR 1
  19. #endif
  20. namespace __vic { namespace posix {
  21. //////////////////////////////////////////////////////////////////////////////
  22. // Directory entries observer
  23. class dir_entries : private non_copyable
  24. {
  25. DIR *dir;
  26. #ifdef __VIC_USE_READDIR
  27. dirent *entry;
  28. #else
  29. dirent entry;
  30. #endif
  31. static bool is_special(const char * );
  32. public:
  33. dir_entries() : dir(nullptr) {}
  34. explicit dir_entries(const char * );
  35. ~dir_entries();
  36. #if __cpp_rvalue_references
  37. dir_entries(dir_entries &&o) noexcept :
  38. dir(o.dir), entry(o.entry) { o.dir = nullptr; }
  39. dir_entries &operator=(dir_entries &&o) noexcept
  40. {
  41. std::swap(dir, o.dir);
  42. entry = o.entry;
  43. return *this;
  44. }
  45. #endif
  46. bool reopen(const char * );
  47. void close();
  48. bool is_open() const { return dir; }
  49. const char *next(); // returns entry name without path
  50. #ifdef _DIRENT_HAVE_D_TYPE
  51. unsigned char type() const
  52. {
  53. #ifdef __VIC_USE_READDIR
  54. return entry->d_type;
  55. #else
  56. return entry.d_type;
  57. #endif
  58. }
  59. #endif
  60. void rewind();
  61. };
  62. //////////////////////////////////////////////////////////////////////////////
  63. }} // namespace
  64. #endif // header guard