DirectoryIterator.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // DirectoryIterator.h
  3. //
  4. // Library: Foundation
  5. // Package: Filesystem
  6. // Module: DirectoryIterator
  7. //
  8. // Definition of the DirectoryIterator class.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_DirectoryIterator_INCLUDED
  16. #define Foundation_DirectoryIterator_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/File.h"
  19. #include "Poco/Path.h"
  20. namespace Poco {
  21. class DirectoryIteratorImpl;
  22. class Foundation_API DirectoryIterator
  23. /// The DirectoryIterator class is used to enumerate
  24. /// all files in a directory.
  25. ///
  26. /// DirectoryIterator has some limitations:
  27. /// * only forward iteration (++) is supported
  28. /// * an iterator copied from another one will always
  29. /// point to the same file as the original iterator,
  30. /// even is the original iterator has been advanced
  31. /// (all copies of an iterator share their state with
  32. /// the original iterator)
  33. /// * because of this you should only use the prefix
  34. /// increment operator
  35. {
  36. public:
  37. DirectoryIterator();
  38. /// Creates the end iterator.
  39. DirectoryIterator(const std::string& path);
  40. /// Creates a directory iterator for the given path.
  41. DirectoryIterator(const DirectoryIterator& iterator);
  42. /// Creates a directory iterator for the given path.
  43. DirectoryIterator(const File& file);
  44. /// Creates a directory iterator for the given file.
  45. DirectoryIterator(const Path& path);
  46. /// Creates a directory iterator for the given path.
  47. virtual ~DirectoryIterator();
  48. /// Destroys the DirectoryIterator.
  49. const std::string& name() const;
  50. /// Returns the current filename.
  51. const Path& path() const;
  52. /// Returns the current path.
  53. DirectoryIterator& operator = (const DirectoryIterator& it);
  54. DirectoryIterator& operator = (const File& file);
  55. DirectoryIterator& operator = (const Path& path);
  56. DirectoryIterator& operator = (const std::string& path);
  57. virtual DirectoryIterator& operator ++ (); // prefix
  58. //@ deprecated
  59. DirectoryIterator operator ++ (int); // postfix
  60. /// Please use the prefix increment operator instead.
  61. const File& operator * () const;
  62. File& operator * ();
  63. const File* operator -> () const;
  64. File* operator -> ();
  65. bool operator == (const DirectoryIterator& iterator) const;
  66. bool operator != (const DirectoryIterator& iterator) const;
  67. protected:
  68. Path _path;
  69. File _file;
  70. private:
  71. DirectoryIteratorImpl* _pImpl;
  72. };
  73. //
  74. // inlines
  75. //
  76. inline const std::string& DirectoryIterator::name() const
  77. {
  78. return _path.getFileName();
  79. }
  80. inline const Path& DirectoryIterator::path() const
  81. {
  82. return _path;
  83. }
  84. inline const File& DirectoryIterator::operator * () const
  85. {
  86. return _file;
  87. }
  88. inline File& DirectoryIterator::operator * ()
  89. {
  90. return _file;
  91. }
  92. inline const File* DirectoryIterator::operator -> () const
  93. {
  94. return &_file;
  95. }
  96. inline File* DirectoryIterator::operator -> ()
  97. {
  98. return &_file;
  99. }
  100. inline bool DirectoryIterator::operator == (const DirectoryIterator& iterator) const
  101. {
  102. return name() == iterator.name();
  103. }
  104. inline bool DirectoryIterator::operator != (const DirectoryIterator& iterator) const
  105. {
  106. return name() != iterator.name();
  107. }
  108. } // namespace Poco
  109. #endif // Foundation_DirectoryIterator_INCLUDED