SF Feature requests #168 and #163 - part 3

This commit is contained in:
Marian Krivos 2013-02-20 09:44:25 +01:00
parent ef1c594fb1
commit 8ddba0bcd1
10 changed files with 304 additions and 400 deletions

View File

@ -52,7 +52,6 @@ set( BASE_SRCS
src/DigestEngine.cpp
src/DigestStream.cpp
src/DirectoryIterator.cpp
src/RecursiveDirectoryIterator.cpp
src/RecursiveDirectoryIteratorStrategies.cpp
src/DirectoryWatcher.cpp
src/Environment.cpp
@ -116,8 +115,6 @@ set( BASE_SRCS
src/RWLock.cpp
src/Random.cpp
src/RandomStream.cpp
src/RecursiveDirectoryIterator.cpp
src/RecursiveDirectoryIteratorStrategies.cpp
src/RefCountedObject.cpp
src/RegularExpression.cpp
src/RotateStrategy.cpp

View File

@ -21,7 +21,7 @@ objects = ArchiveStrategy Ascii ASCIIEncoding AsyncChannel \
NestedDiagnosticContext Notification NotificationCenter \
NotificationQueue PriorityNotificationQueue TimedNotificationQueue \
NullStream NumberFormatter NumberParser NumericString AbstractObserver \
Path PatternFormatter Process PurgeStrategy RWLock Random RandomStream RecursiveDirectoryIterator \
Path PatternFormatter Process PurgeStrategy RWLock Random RandomStream \
RecursiveDirectoryIteratorStrategies RegularExpression RefCountedObject Runnable RotateStrategy Condition \
SHA1Engine Semaphore SharedLibrary SimpleFileChannel \
SignalHandler SplitterChannel SortedDirectoryIterator Stopwatch StreamChannel \

View File

@ -109,7 +109,7 @@ public:
bool operator == (const DirectoryIterator& iterator) const;
bool operator != (const DirectoryIterator& iterator) const;
public:
protected:
Path _path;
File _file;

View File

@ -35,6 +35,7 @@
// DEALINGS IN THE SOFTWARE.
//
#ifndef Foundation_RecursiveDirectoryIterator_INCLUDE
#define Foundation_RecursiveDirectoryIterator_INCLUDE
@ -44,6 +45,7 @@
#include "Poco/RecursiveDirectoryIteratorImpl.h"
#include "Poco/RecursiveDirectoryIteratorStrategies.h"
namespace Poco
{
@ -54,27 +56,27 @@ class RecursiveDirectoryIteratorImpl;
template<class TTravStr = ChildrenFirstTraverse>
class RecursiveDirectoryIterator
/// The RecursiveDirectoryIterator class is used to enumerate
/// all files in a directory and its subdirectories.
///
/// RecursiveDirectoryIterator has some limitations:
/// * only forward iteration (++) is supported
/// * an iterator copied from another one will always
/// point to the same file as the original iterator,
/// even is the original iterator has been advanced
/// (all copies of an iterator share their state with
/// the original iterator)
///
/// The class can follow different traversal strategies:
/// * depth-first strategy;
/// * siblings-first strategy.
/// The stategies are set by template parameter.
/// There are two corresponding typedefs:
/// * SimpleRecursiveDirectoryIterator;
/// * SiblingsFirstRecursiveDirectoryIterator.
///
/// The depth of traversal can be limited by constructor
/// parameter maxDepth (which sets the infinite depth by default).
/// The RecursiveDirectoryIterator class is used to enumerate
/// all files in a directory and its subdirectories.
///
/// RecursiveDirectoryIterator has some limitations:
/// * only forward iteration (++) is supported
/// * an iterator copied from another one will always
/// point to the same file as the original iterator,
/// even is the original iterator has been advanced
/// (all copies of an iterator share their state with
/// the original iterator)
///
/// The class can follow different traversal strategies:
/// * depth-first strategy;
/// * siblings-first strategy.
/// The stategies are set by template parameter.
/// There are two corresponding typedefs:
/// * SimpleRecursiveDirectoryIterator;
/// * SiblingsFirstRecursiveDirectoryIterator.
///
/// The depth of traversal can be limited by constructor
/// parameter maxDepth (which sets the infinite depth by default).
{
public:
typedef RecursiveDirectoryIterator<TTravStr> MyType;
@ -85,51 +87,148 @@ public:
};
/// Constant for infinite traverse depth.
RecursiveDirectoryIterator();
RecursiveDirectoryIterator()
/// Creates the end iterator.
: _pImpl(0)
{
}
RecursiveDirectoryIterator(const std::string& path, UInt16 maxDepth = D_INFINITE);
/// Creates a recursive directory iterator for the given path.
RecursiveDirectoryIterator(const MyType& iterator);
RecursiveDirectoryIterator(const MyType& iterator)
/// Creates a copy of another recursive directory iterator.
: _pImpl(iterator._pImpl), _path(iterator._path), _file(iterator._file)
{
}
RecursiveDirectoryIterator(const DirectoryIterator& iterator, UInt16 maxDepth = D_INFINITE);
RecursiveDirectoryIterator(const DirectoryIterator& iterator, UInt16 maxDepth = D_INFINITE)
/// Creates a recursive directory iterator for the path of
/// non-recursive directory iterator.
: _pImpl(new ImplType(iterator->path(), maxDepth)), _path(Path(_pImpl->get())), _file(_path)
{
}
RecursiveDirectoryIterator(const File& file, UInt16 maxDepth = D_INFINITE);
RecursiveDirectoryIterator(const File& file, UInt16 maxDepth = D_INFINITE)
/// Creates a recursive directory iterator for the given path.
: _pImpl(new ImplType(file.path(), maxDepth)), _path(Path(_pImpl->get())), _file(_path)
{
}
RecursiveDirectoryIterator(const Path& path, UInt16 maxDepth = D_INFINITE);
RecursiveDirectoryIterator(const Path& path, UInt16 maxDepth = D_INFINITE)
/// Creates a recursive directory iterator for the given path.
: _pImpl(new ImplType(path.toString(), maxDepth)), _path(Path(_pImpl->get())), _file(_path)
{
}
~RecursiveDirectoryIterator();
~RecursiveDirectoryIterator()
/// Destroys the DirectoryIterator.
{
if (_pImpl)
_pImpl->release();
}
const std::string& name() const;
const std::string& name() const
/// Returns the current filename.
{
return _path.getFileName();
}
const Poco::Path& path() const;
const Poco::Path& path() const
/// Returns the current path.
{
return _path;
}
UInt16 depth() const;
UInt16 depth() const
/// Depth of recursion (counting from 1).
{
return _pImpl->depth();
}
UInt16 maxDepth() const;
UInt16 maxDepth() const
/// Max depth of recursion (counting from 1).
{
return _pImpl->maxDepth();
}
MyType& operator =(const MyType& it);
MyType& operator =(const File& file);
MyType& operator =(const Path& path);
MyType& operator =(const std::string& path);
MyType& operator ++();
MyType& operator =(const MyType& it)
{
if (_pImpl)
_pImpl->release();
_pImpl = it._pImpl;
if (_pImpl)
{
_pImpl->duplicate();
_path = it._path;
_file = _path;
}
return *this;
}
MyType& operator =(const File& file)
{
if (_pImpl)
_pImpl->release();
_pImpl = new ImplType(file.path());
_path = Path(_pImpl->get());
_file = _path;
return *this;
}
MyType& operator =(const Path& path)
{
if (_pImpl)
_pImpl->release();
_pImpl = new ImplType(path.toString());
_path = Path(_pImpl->get());
_file = _path;
return *this;
}
MyType& operator =(const std::string& path)
{
if (_pImpl)
_pImpl->release();
_pImpl = new ImplType(path);
_path = Path(_pImpl->get());
_file = _path;
return *this;
}
MyType& operator ++()
{
if (_pImpl)
{
_path = Path(_pImpl->next());
_file = _path;
}
return *this;
}
const File& operator *() const
{
return _file;
}
File& operator *()
{
return _file;
}
const File* operator ->() const
{
return &_file;
}
File* operator ->()
{
return &_file;
}
const File& operator *() const;
File& operator *();
const File* operator ->() const;
File* operator ->();
template<class T1, class T2>
friend inline bool operator ==(const RecursiveDirectoryIterator<T1>& a, const RecursiveDirectoryIterator<T2>& b);
@ -144,6 +243,7 @@ private:
File _file;
};
//
// friend comparsion operators
//
@ -159,172 +259,6 @@ inline bool operator !=(const RecursiveDirectoryIterator<T1>& a, const Recursive
return a.path().toString() != b.path().toString();;
}
//
// inlines
//
template<class TTravStr>
inline const std::string&
RecursiveDirectoryIterator<TTravStr>::name() const
{
return _path.getFileName();
}
template<class TTravStr>
inline const Path&
RecursiveDirectoryIterator<TTravStr>::path() const
{
return _path;
}
template<class TTravStr>
inline UInt16 RecursiveDirectoryIterator<TTravStr>::depth() const
{
return _pImpl->depth();
}
template<class TTravStr>
inline UInt16 RecursiveDirectoryIterator<TTravStr>::maxDepth() const
{
return _pImpl->maxDepth();
}
template<class TTravStr>
inline const File&
RecursiveDirectoryIterator<TTravStr>::operator *() const
{
return _file;
}
template<class TTravStr>
inline File&
RecursiveDirectoryIterator<TTravStr>::operator *()
{
return _file;
}
template<class TTravStr>
inline const File*
RecursiveDirectoryIterator<TTravStr>::operator ->() const
{
return &_file;
}
template<class TTravStr>
inline File*
RecursiveDirectoryIterator<TTravStr>::operator ->()
{
return &_file;
}
//
// not inlines
//
template<class TTravStr>
RecursiveDirectoryIterator<TTravStr>::RecursiveDirectoryIterator()
: _pImpl(0)
{
}
template<class TTravStr>
RecursiveDirectoryIterator<TTravStr>::RecursiveDirectoryIterator(const std::string& path, UInt16 maxDepth)
: _pImpl(new ImplType(path, maxDepth)), _path(Path(_pImpl->get())), _file(_path)
{
}
template<class TTravStr>
RecursiveDirectoryIterator<TTravStr>::RecursiveDirectoryIterator(const MyType& iterator)
: _pImpl(iterator._pImpl), _path(iterator._path), _file(iterator._file)
{
}
template<class TTravStr>
RecursiveDirectoryIterator<TTravStr>::RecursiveDirectoryIterator(const DirectoryIterator& iterator, UInt16 maxDepth)
: _pImpl(new ImplType(iterator->path(), maxDepth)), _path(Path(_pImpl->get())), _file(_path)
{
}
template<class TTravStr>
RecursiveDirectoryIterator<TTravStr>::RecursiveDirectoryIterator(const File& file, UInt16 maxDepth)
: _pImpl(new ImplType(file.path(), maxDepth)), _path(Path(_pImpl->get())), _file(_path)
{
}
template<class TTravStr>
RecursiveDirectoryIterator<TTravStr>::RecursiveDirectoryIterator(const Path& path, UInt16 maxDepth)
: _pImpl(new ImplType(path.toString(), maxDepth)), _path(Path(_pImpl->get())), _file(_path)
{
}
template<class TTravStr>
RecursiveDirectoryIterator<TTravStr>::~RecursiveDirectoryIterator()
{
if (_pImpl)
_pImpl->release();
}
template<class TTravStr>
RecursiveDirectoryIterator<TTravStr>&
RecursiveDirectoryIterator<TTravStr>::operator =(const MyType& it)
{
if (_pImpl)
_pImpl->release();
_pImpl = it._pImpl;
if (_pImpl)
{
_pImpl->duplicate();
_path = it._path;
_file = _path;
}
return *this;
}
template<class TTravStr>
RecursiveDirectoryIterator<TTravStr>&
RecursiveDirectoryIterator<TTravStr>::operator =(const File& file)
{
if (_pImpl)
_pImpl->release();
_pImpl = new ImplType(file.path());
_path = Path(_pImpl->get());
_file = _path;
return *this;
}
template<class TTravStr>
RecursiveDirectoryIterator<TTravStr>&
RecursiveDirectoryIterator<TTravStr>::operator =(const Path& path)
{
if (_pImpl)
_pImpl->release();
_pImpl = new ImplType(path.toString());
_path = Path(_pImpl->get());
_file = _path;
return *this;
}
template<class TTravStr>
RecursiveDirectoryIterator<TTravStr>&
RecursiveDirectoryIterator<TTravStr>::operator =(const std::string& path)
{
if (_pImpl)
_pImpl->release();
_pImpl = new ImplType(path);
_path = Path(_pImpl->get());
_file = _path;
return *this;
}
template<class TTravStr>
RecursiveDirectoryIterator<TTravStr>&
RecursiveDirectoryIterator<TTravStr>::operator ++()
{
if (_pImpl)
{
_path = Path(_pImpl->next());
_file = _path;
}
return *this;
}
//
// exported instances
@ -334,12 +268,14 @@ RecursiveDirectoryIterator<ChildrenFirstTraverse> ;
template class Foundation_API
RecursiveDirectoryIterator<SiblingsFirstTraverse> ;
//
// typedefs
//
typedef RecursiveDirectoryIterator<ChildrenFirstTraverse> SimpleRecursiveDirectoryIterator;
typedef RecursiveDirectoryIterator<SiblingsFirstTraverse> SiblingsFirstRecursiveDirectoryIterator;
} // namespace Poco
#endif // Foundation_RecursiveDirectoryIterator_INCLUDE

View File

@ -35,6 +35,7 @@
// DEALINGS IN THE SOFTWARE.
//
#ifndef Foundation_RecursiveDirectoryIteratorImpl_INCLUDE
#define Foundation_RecursiveDirectoryIteratorImpl_INCLUDE
@ -43,6 +44,7 @@
#include <stack>
#include <functional>
namespace Poco
{
@ -59,103 +61,70 @@ public:
};
/// Constant for infinite traverse depth.
RecursiveDirectoryIteratorImpl(const std::string& path, UInt16 maxDepth = D_INFINITE);
~RecursiveDirectoryIteratorImpl();
void duplicate();
void release();
UInt16 depth() const;
UInt16 maxDepth() const;
const std::string& get() const;
const std::string& next();
private:
typedef std::stack<DirectoryIterator> Stack;
static UInt16 depthFun(const Stack& stack);
/// Function which implements the logic of determining
/// recursion depth.
UInt16 _maxDepth;
TTraverseStrategy _traverseStrategy;
bool _isFinished;
Stack _itStack;
std::string _current;
int _rc;
};
//
// inlines
//
template<class TTraverseStrategy>
inline void RecursiveDirectoryIteratorImpl<TTraverseStrategy>::duplicate()
{
++_rc;
}
template<class TTraverseStrategy>
inline void RecursiveDirectoryIteratorImpl<TTraverseStrategy>::release()
{
if (--_rc == 0)
delete this;
}
template<class TTraverseStrategy>
inline UInt16 RecursiveDirectoryIteratorImpl<TTraverseStrategy>::depth() const
{
return depthFun(_itStack);
}
template<class TTraverseStrategy>
inline UInt16 RecursiveDirectoryIteratorImpl<TTraverseStrategy>::maxDepth() const
{
return _maxDepth;
}
template<class TTraverseStrategy>
inline const std::string&
RecursiveDirectoryIteratorImpl<TTraverseStrategy>::get() const
{
return _current;
}
template<class TTraverseStrategy>
inline UInt16 RecursiveDirectoryIteratorImpl<TTraverseStrategy>::depthFun(const Stack& stack)
{
return stack.size();
}
//
// not inlines
//
template<class TTraverseStrategy>
RecursiveDirectoryIteratorImpl<TTraverseStrategy>::RecursiveDirectoryIteratorImpl(const std::string& path, UInt16 maxDepth)
: _maxDepth(maxDepth), _traverseStrategy(std::ptr_fun(depthFun), _maxDepth), _isFinished(false)
{
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();
}
}
template<class TTraverseStrategy>
RecursiveDirectoryIteratorImpl<TTraverseStrategy>::~RecursiveDirectoryIteratorImpl()
{
~RecursiveDirectoryIteratorImpl()
{
}
}
inline void duplicate()
{
++_rc;
}
template<class TTraverseStrategy>
const std::string&
RecursiveDirectoryIteratorImpl<TTraverseStrategy>::next()
{
inline void release()
{
if (--_rc == 0)
delete this;
}
inline UInt16 depth() const
{
return depthFun(_itStack);
}
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;
}
}
private:
typedef std::stack<DirectoryIterator> Stack;
static UInt16 depthFun(const Stack& stack)
/// Function which implements the logic of determining
/// recursion depth.
{
return stack.size();
}
UInt16 _maxDepth;
TTraverseStrategy _traverseStrategy;
bool _isFinished;
Stack _itStack;
std::string _current;
int _rc;
};
} // namespace Poco

View File

@ -35,6 +35,7 @@
// DEALINGS IN THE SOFTWARE.
//
#ifndef Foundation_RecursiveDirectoryIteratorStategies_INCLUDE
#define Foundation_RecursiveDirectoryIteratorStategies_INCLUDE
@ -44,6 +45,7 @@
#include <queue>
#include <functional>
namespace Poco
{
@ -66,7 +68,6 @@ protected:
DepthFunPtr _depthDeterminer;
UInt16 _maxDepth;
DirectoryIterator _itEnd;
private:
@ -75,6 +76,7 @@ private:
TraverseBase& operator=(const TraverseBase&);
};
class ChildrenFirstTraverse: public TraverseBase
{
public:
@ -88,6 +90,7 @@ private:
ChildrenFirstTraverse& operator=(const ChildrenFirstTraverse&);
};
class SiblingsFirstTraverse: public TraverseBase
{
public:
@ -103,6 +106,7 @@ private:
std::stack<std::queue<std::string> > _dirsStack;
};
} // namespace Poco
#endif // Foundation_RecursiveDirectoryIteratorStategies_INCLUDE

View File

@ -39,7 +39,6 @@
#ifndef Foundation_SortedDirectoryIterator_INCLUDED
#define Foundation_SortedDirectoryIterator_INCLUDED
#include "Poco/Foundation.h"
#include "Poco/File.h"
#include "Poco/Path.h"
@ -47,10 +46,10 @@
#include <deque>
namespace Poco {
namespace Poco
{
class Foundation_API SortedDirectoryIterator : public DirectoryIterator
class Foundation_API SortedDirectoryIterator: public DirectoryIterator
/// The SortedDirectoryIterator class is similar to
/// DirectoryIterator class, but places directories before files
/// and sorts content alphabetically.
@ -74,7 +73,7 @@ public:
virtual ~SortedDirectoryIterator();
/// Destroys the DirsFirstDirectoryIterator.
virtual SortedDirectoryIterator& operator ++ (); // prefix
virtual SortedDirectoryIterator& operator ++(); // prefix
private:
bool _is_finished;
@ -90,6 +89,4 @@ private:
} // namespace Poco
#endif //Foundation_SortedDirectoryIterator_INCLUDED

View File

@ -1,6 +0,0 @@
#include "Poco/RecursiveDirectoryIterator.h"
namespace Poco {
}

View File

@ -33,8 +33,10 @@
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/RecursiveDirectoryIteratorStrategies.h"
namespace Poco
{
@ -48,11 +50,13 @@ TraverseBase::TraverseBase(DepthFunPtr depthDeterminer, UInt16 maxDepth)
{
}
inline bool TraverseBase::isFiniteDepth()
{
return _maxDepth != D_INFINITE;
}
//
// ChildrenFirstTraverse
//
@ -61,6 +65,7 @@ ChildrenFirstTraverse::ChildrenFirstTraverse(DepthFunPtr depthDeterminer, UInt16
{
}
const string ChildrenFirstTraverse::next(Stack* itStack, bool* isFinished)
{
// pointer mustn't point to NULL and iteration mustn't be finished
@ -108,6 +113,7 @@ const string ChildrenFirstTraverse::next(Stack* itStack, bool* isFinished)
return itStack->top()->path();
}
//
// SiblingsFirstTraverse
//
@ -117,6 +123,7 @@ SiblingsFirstTraverse::SiblingsFirstTraverse(DepthFunPtr depthDeterminer, UInt16
_dirsStack.push(queue<string>());
}
const string SiblingsFirstTraverse::next(Stack* itStack, bool* isFinished)
{
// pointer mustn't point to NULL and iteration mustn't be finished
@ -168,4 +175,5 @@ const string SiblingsFirstTraverse::next(Stack* itStack, bool* isFinished)
return itStack->top()->path();
}
} // namespace Poco

View File

@ -33,13 +33,12 @@
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/SortedDirectoryIterator.h"
#include <algorithm>
namespace Poco {
namespace Poco
{
SortedDirectoryIterator::SortedDirectoryIterator()
: DirectoryIterator(), _is_finished(true)
@ -83,8 +82,7 @@ SortedDirectoryIterator::~SortedDirectoryIterator()
{
}
SortedDirectoryIterator& SortedDirectoryIterator::operator ++ ()
SortedDirectoryIterator& SortedDirectoryIterator::operator ++()
{
if (!_is_finished)
{
@ -135,4 +133,5 @@ void SortedDirectoryIterator::next()
}
}
} // namespace Poco