poco/Foundation/include/Poco/RecursiveDirectoryIteratorImpl.h

134 lines
3.3 KiB
C
Raw Normal View History

2013-02-19 21:29:58 +01:00
//
// RecursiveDirectoryIteratorImpl.h
//
// $Id$
//
// Library: Foundation
// Package: Filesystem
// Module: RecursiveDirectoryIterator
//
// Definition of the RecursiveDirectoryIteratorImpl class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#ifndef Foundation_RecursiveDirectoryIteratorImpl_INCLUDED
#define Foundation_RecursiveDirectoryIteratorImpl_INCLUDED
2013-02-19 21:29:58 +01:00
2013-02-19 21:29:58 +01:00
#include "Poco/Foundation.h"
#include "Poco/DirectoryIteratorStrategy.h"
2013-02-19 21:29:58 +01:00
#include <stack>
#include <functional>
namespace Poco {
2013-02-19 21:29:58 +01:00
class ChildrenFirstTraverse;
class SiblingsFirstTraverse;
2013-02-19 21:29:58 +01:00
template<class TTraverseStrategy = ChildrenFirstTraverse>
class RecursiveDirectoryIteratorImpl
{
public:
enum
{
D_INFINITE = 0 /// Special value for infinite traverse depth.
2013-02-19 21:29:58 +01:00
};
RecursiveDirectoryIteratorImpl(const std::string& path, UInt16 maxDepth = D_INFINITE)
: _maxDepth(maxDepth), _traverseStrategy(std::ptr_fun(depthFun), _maxDepth), _isFinished(false), _rc(1)
{
_itStack.push(DirectoryIterator(path));
_current = _itStack.top()->path();
}
~RecursiveDirectoryIteratorImpl()
{
}
2013-02-19 21:29:58 +01:00
inline void duplicate()
{
++_rc;
}
inline void release()
{
if (--_rc == 0)
delete this;
}
2013-02-19 21:29:58 +01:00
inline UInt16 depth() const
{
return depthFun(_itStack);
}
2013-02-19 21:29:58 +01:00
inline UInt16 maxDepth() const
{
return _maxDepth;
}
inline const std::string& get() const
{
return _current;
}
const std::string& next()
{
if (_isFinished)
return _current;
_current = _traverseStrategy.next(&_itStack, &_isFinished);
return _current;
}
2013-02-19 21:29:58 +01:00
private:
typedef std::stack<DirectoryIterator> Stack;
static UInt16 depthFun(const Stack& stack)
/// Function which implements the logic of determining
/// recursion depth.
{
return static_cast<Poco::UInt16>(stack.size());
}
2013-02-19 21:29:58 +01:00
UInt16 _maxDepth;
TTraverseStrategy _traverseStrategy;
bool _isFinished;
Stack _itStack;
std::string _current;
int _rc;
};
} // namespace Poco
#endif // Foundation_RecursiveDirectoryIteratorImpl_INCLUDED