RecursiveDirectoryIteratorImpl.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // RecursiveDirectoryIteratorImpl.h
  3. //
  4. // Library: Foundation
  5. // Package: Filesystem
  6. // Module: RecursiveDirectoryIterator
  7. //
  8. // Definition of the RecursiveDirectoryIteratorImpl class.
  9. //
  10. // Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_RecursiveDirectoryIteratorImpl_INCLUDED
  16. #define Foundation_RecursiveDirectoryIteratorImpl_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/DirectoryIteratorStrategy.h"
  19. #include <stack>
  20. #include <functional>
  21. namespace Poco {
  22. class ChildrenFirstTraverse;
  23. class SiblingsFirstTraverse;
  24. template<class TTraverseStrategy = ChildrenFirstTraverse>
  25. class RecursiveDirectoryIteratorImpl
  26. {
  27. public:
  28. enum
  29. {
  30. D_INFINITE = 0 /// Special value for infinite traverse depth.
  31. };
  32. RecursiveDirectoryIteratorImpl(const std::string& path, UInt16 maxDepth = D_INFINITE)
  33. : _maxDepth(maxDepth), _traverseStrategy(std::ptr_fun(depthFun), _maxDepth), _isFinished(false), _rc(1)
  34. {
  35. _itStack.push(DirectoryIterator(path));
  36. _current = _itStack.top()->path();
  37. }
  38. ~RecursiveDirectoryIteratorImpl()
  39. {
  40. }
  41. inline void duplicate()
  42. {
  43. ++_rc;
  44. }
  45. inline void release()
  46. {
  47. if (--_rc == 0)
  48. delete this;
  49. }
  50. inline UInt16 depth() const
  51. {
  52. return depthFun(_itStack);
  53. }
  54. inline UInt16 maxDepth() const
  55. {
  56. return _maxDepth;
  57. }
  58. inline const std::string& get() const
  59. {
  60. return _current;
  61. }
  62. const std::string& next()
  63. {
  64. if (_isFinished)
  65. return _current;
  66. _current = _traverseStrategy.next(&_itStack, &_isFinished);
  67. return _current;
  68. }
  69. private:
  70. typedef std::stack<DirectoryIterator> Stack;
  71. static UInt16 depthFun(const Stack& stack)
  72. /// Function which implements the logic of determining
  73. /// recursion depth.
  74. {
  75. return static_cast<Poco::UInt16>(stack.size());
  76. }
  77. UInt16 _maxDepth;
  78. TTraverseStrategy _traverseStrategy;
  79. bool _isFinished;
  80. Stack _itStack;
  81. std::string _current;
  82. int _rc;
  83. };
  84. } // namespace Poco
  85. #endif // Foundation_RecursiveDirectoryIteratorImpl_INCLUDED