mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-26 18:42:41 +01:00
RecursiveDirectoryIterator and ListMap
1) Recursive and SortedDirectoryIterator - added to VS projects - fixed some style/formatting - fxed windows tests 2) Added ListMap Poco::ListMap is a std::mulitimap-like container that preserves insertion order (needed to prevent Net message headers)
This commit is contained in:
@@ -43,8 +43,11 @@
|
||||
// Define to enable Windows Unicode (UTF-8) support
|
||||
#define POCO_WIN32_UTF8
|
||||
|
||||
|
||||
#if defined(__cplusplus) && (__cplusplus > 199711L)
|
||||
// Define to enable C++11 support
|
||||
//#define POCO_ENABLE_CPP11
|
||||
// #define POCO_ENABLE_CPP11
|
||||
#endif
|
||||
|
||||
|
||||
// Define to disable implicit linking
|
||||
|
||||
@@ -36,8 +36,8 @@
|
||||
//
|
||||
|
||||
|
||||
#ifndef Foundation_RecursiveDirectoryIteratorStategies_INCLUDE
|
||||
#define Foundation_RecursiveDirectoryIteratorStategies_INCLUDE
|
||||
#ifndef Foundation_RecursiveDirectoryIteratorStrategy_INCLUDE
|
||||
#define Foundation_RecursiveDirectoryIteratorStrategy_INCLUDE
|
||||
|
||||
#include "Poco/Foundation.h"
|
||||
#include "Poco/DirectoryIterator.h"
|
||||
@@ -49,7 +49,7 @@
|
||||
namespace Poco
|
||||
{
|
||||
|
||||
class TraverseBase
|
||||
class Foundation_API TraverseBase
|
||||
{
|
||||
public:
|
||||
typedef std::stack<DirectoryIterator> Stack;
|
||||
@@ -77,7 +77,7 @@ private:
|
||||
};
|
||||
|
||||
|
||||
class ChildrenFirstTraverse: public TraverseBase
|
||||
class Foundation_API ChildrenFirstTraverse: public TraverseBase
|
||||
{
|
||||
public:
|
||||
ChildrenFirstTraverse(DepthFunPtr depthDeterminer, UInt16 maxDepth = D_INFINITE);
|
||||
@@ -91,7 +91,7 @@ private:
|
||||
};
|
||||
|
||||
|
||||
class SiblingsFirstTraverse: public TraverseBase
|
||||
class Foundation_API SiblingsFirstTraverse: public TraverseBase
|
||||
{
|
||||
public:
|
||||
SiblingsFirstTraverse(DepthFunPtr depthDeterminer, UInt16 maxDepth = D_INFINITE);
|
||||
@@ -109,4 +109,4 @@ private:
|
||||
|
||||
} // namespace Poco
|
||||
|
||||
#endif // Foundation_RecursiveDirectoryIteratorStategies_INCLUDE
|
||||
#endif // Foundation_RecursiveDirectoryIteratorStrategy_INCLUDE
|
||||
279
Foundation/include/Poco/ListMap.h
Normal file
279
Foundation/include/Poco/ListMap.h
Normal file
@@ -0,0 +1,279 @@
|
||||
//
|
||||
// ListMap.h
|
||||
//
|
||||
// $Id: //poco/1.4/Foundation/include/Poco/ListMap.h#1 $
|
||||
//
|
||||
// Library: Foundation
|
||||
// Package: Hashing
|
||||
// Module: ListMap
|
||||
//
|
||||
// Definition of the ListMap class.
|
||||
//
|
||||
// Copyright (c) 2006, 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_ListMap_INCLUDED
|
||||
#define Foundation_ListMap_INCLUDED
|
||||
|
||||
|
||||
#include "Poco/Foundation.h"
|
||||
#include "Poco/String.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include <list>
|
||||
#include <utility>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
|
||||
|
||||
template <class Key, class Mapped, class Container = std::list<std::pair<Key, Mapped> >, bool CaseSensitive = false >
|
||||
class ListMap
|
||||
/// This class implements a multimap in terms of a sequential container.
|
||||
/// The use for this type of associative container is wherever automatic
|
||||
/// ordering of elements is not desirable. Naturally, this container will
|
||||
/// have inferior data retrieval performance and it is not recommended for
|
||||
/// use with large datasets. The main purpose within POCO is for Internet
|
||||
/// messages (email message, http headers etc), to prevent autmomatic
|
||||
/// header entry reordering.
|
||||
{
|
||||
public:
|
||||
typedef Key KeyType;
|
||||
typedef Mapped MappedType;
|
||||
typedef Mapped& Reference;
|
||||
typedef const Mapped& ConstReference;
|
||||
typedef Mapped* Pointer;
|
||||
typedef const Mapped* ConstPointer;
|
||||
|
||||
typedef typename Container::value_type ValueType;
|
||||
typedef typename Container::size_type SizeType;
|
||||
typedef typename Container::iterator Iterator;
|
||||
typedef typename Container::const_iterator ConstIterator;
|
||||
|
||||
ListMap()
|
||||
/// Creates an empty ListMap.
|
||||
{
|
||||
}
|
||||
|
||||
ListMap(std::size_t initialReserve):
|
||||
_list(initialReserve)
|
||||
/// Creates the ListMap with room for initialReserve entries.
|
||||
{
|
||||
}
|
||||
|
||||
ListMap& operator = (const ListMap& map)
|
||||
/// Assigns another ListMap.
|
||||
{
|
||||
ListMap tmp(map);
|
||||
swap(tmp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void swap(ListMap& map)
|
||||
/// Swaps the ListMap with another one.
|
||||
{
|
||||
_list.swap(map._list);
|
||||
}
|
||||
|
||||
ConstIterator begin() const
|
||||
/// Returns the beginning of the map.
|
||||
{
|
||||
return _list.begin();
|
||||
}
|
||||
|
||||
ConstIterator end() const
|
||||
/// Returns the end of the map.
|
||||
{
|
||||
return _list.end();
|
||||
}
|
||||
|
||||
Iterator begin()
|
||||
/// Returns the beginning of the map.
|
||||
{
|
||||
return _list.begin();
|
||||
}
|
||||
|
||||
Iterator end()
|
||||
/// Returns the end of the map.
|
||||
{
|
||||
return _list.end();
|
||||
}
|
||||
|
||||
ConstIterator find(const KeyType& key) const
|
||||
/// Finds the first occurence of the key and
|
||||
/// returns iterator pointing to the found entry
|
||||
/// or iterator pointing to the end if entry is
|
||||
/// not found.
|
||||
{
|
||||
Container::const_iterator it = _list.begin();
|
||||
Container::const_iterator end = _list.end();
|
||||
for(; it != end; ++it)
|
||||
{
|
||||
if (isEqual(it->first, key)) return it;
|
||||
}
|
||||
return end;
|
||||
}
|
||||
|
||||
Iterator find(const KeyType& key)
|
||||
/// Finds the first occurence of the key and
|
||||
/// returns iterator pointing to the found entry
|
||||
/// or iterator pointing to the end if entry is
|
||||
/// not found.
|
||||
{
|
||||
Container::iterator it = _list.begin();
|
||||
Container::iterator end = _list.end();
|
||||
for(; it != end; ++it)
|
||||
{
|
||||
if (isEqual(it->first, key)) return it;
|
||||
}
|
||||
return end;
|
||||
}
|
||||
|
||||
Iterator insert(const ValueType& val)
|
||||
/// Inserts the value into the map. If one or more values
|
||||
/// already exist, new value is inserted at the end of the
|
||||
/// block. Thus, all the equal value entries are located
|
||||
/// sequentially at all times.
|
||||
/// Returns iterator pointing to the newly inserted value
|
||||
{
|
||||
Iterator it = find(val.first);
|
||||
|
||||
if (it == _list.end())
|
||||
{
|
||||
_list.push_back(val);
|
||||
it = _list.end();
|
||||
--it;
|
||||
}
|
||||
else
|
||||
{
|
||||
_list.insert(it, 1, val);
|
||||
}
|
||||
|
||||
return it;
|
||||
}
|
||||
|
||||
void erase(Iterator it)
|
||||
{
|
||||
_list.erase(it);
|
||||
}
|
||||
|
||||
SizeType erase(const KeyType& key)
|
||||
{
|
||||
SizeType count = 0;
|
||||
Iterator it = find(key);
|
||||
bool removed = false;
|
||||
while (it != _list.end())
|
||||
{
|
||||
if (isEqual(it->first, key))
|
||||
{
|
||||
++count;
|
||||
it = _list.erase(it);
|
||||
removed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (removed) return count;
|
||||
++it;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
_list.clear();
|
||||
}
|
||||
|
||||
std::size_t size() const
|
||||
{
|
||||
return _list.size();
|
||||
}
|
||||
|
||||
bool empty() const
|
||||
{
|
||||
return _list.empty();
|
||||
}
|
||||
|
||||
ConstReference operator [] (const KeyType& key) const
|
||||
{
|
||||
ConstIterator it = find(key);
|
||||
if (it != _list.end())
|
||||
return it->second;
|
||||
else
|
||||
throw NotFoundException();
|
||||
}
|
||||
|
||||
Reference operator [] (const KeyType& key)
|
||||
{
|
||||
Iterator it = find(key);
|
||||
if (it != _list.end())
|
||||
return it->second;
|
||||
else
|
||||
{
|
||||
ValueType value(key, Mapped());
|
||||
Iterator it = insert(value);
|
||||
return it->second;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename T1, typename T2>
|
||||
bool isEqual(T1 val1, T2 val2) const
|
||||
{
|
||||
return val1 == val2;
|
||||
}
|
||||
|
||||
bool isEqual(const std::string& s1, const std::string& s2) const
|
||||
{
|
||||
if (!CaseSensitive)
|
||||
return Poco::icompare(s1, s2) == 0;
|
||||
else
|
||||
return s1 == s2;
|
||||
}
|
||||
|
||||
bool isEqual(const std::string& s1, const char* s2) const
|
||||
{
|
||||
return isEqual(s1, std::string(s2));
|
||||
}
|
||||
|
||||
bool isEqual(const char* s1, const std::string& s2) const
|
||||
{
|
||||
return isEqual(std::string(s1), s2);
|
||||
}
|
||||
|
||||
bool isEqual(const char* s1, const char* s2) const
|
||||
{
|
||||
return isEqual(std::string(s1), std::string(s2));
|
||||
}
|
||||
|
||||
Container _list;
|
||||
};
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
|
||||
|
||||
#endif // Foundation_ListMap_INCLUDED
|
||||
@@ -43,37 +43,39 @@
|
||||
#include "Poco/File.h"
|
||||
#include "Poco/Path.h"
|
||||
#include "Poco/RecursiveDirectoryIteratorImpl.h"
|
||||
#include "Poco/RecursiveDirectoryIteratorStrategies.h"
|
||||
#include "Poco/DirectoryIteratorStrategy.h"
|
||||
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace Poco {
|
||||
|
||||
|
||||
class DirectoryIterator;
|
||||
|
||||
|
||||
template<class TTravStr>
|
||||
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)
|
||||
/// * 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.
|
||||
/// * depth-first strategy;
|
||||
/// * siblings-first strategy.
|
||||
/// The stategies are set by template parameter.
|
||||
/// There are two corresponding typedefs:
|
||||
/// * SimpleRecursiveDirectoryIterator;
|
||||
/// * SiblingsFirstRecursiveDirectoryIterator.
|
||||
/// * SimpleRecursiveDirectoryIterator;
|
||||
/// * SiblingsFirstRecursiveDirectoryIterator.
|
||||
///
|
||||
/// The depth of traversal can be limited by constructor
|
||||
/// parameter maxDepth (which sets the infinite depth by default).
|
||||
@@ -260,15 +262,6 @@ inline bool operator !=(const RecursiveDirectoryIterator<T1>& a, const Recursive
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// exported instances
|
||||
//
|
||||
template class Foundation_API
|
||||
RecursiveDirectoryIterator<ChildrenFirstTraverse> ;
|
||||
template class Foundation_API
|
||||
RecursiveDirectoryIterator<SiblingsFirstTraverse> ;
|
||||
|
||||
|
||||
//
|
||||
// typedefs
|
||||
//
|
||||
|
||||
@@ -39,18 +39,20 @@
|
||||
#ifndef Foundation_RecursiveDirectoryIteratorImpl_INCLUDE
|
||||
#define Foundation_RecursiveDirectoryIteratorImpl_INCLUDE
|
||||
|
||||
|
||||
#include "Poco/Foundation.h"
|
||||
#include "Poco/RecursiveDirectoryIteratorStrategies.h"
|
||||
#include "Poco/DirectoryIteratorStrategy.h"
|
||||
#include <stack>
|
||||
#include <functional>
|
||||
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace Poco {
|
||||
|
||||
|
||||
class ChildrenFirstTraverse;
|
||||
class SiblingsFirstTraverse;
|
||||
|
||||
|
||||
template<class TTraverseStrategy = ChildrenFirstTraverse>
|
||||
class RecursiveDirectoryIteratorImpl
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user