RecursiveDirectoryIteratorImpl.h 2.0 KB

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