DirectoryIterator.h 3.3 KB

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