📄 glob_sequence.hpp
字号:
/* /////////////////////////////////////////////////////////////////////////
* File: unixstl/filesystem/glob_sequence.hpp
*
* Purpose: glob_sequence class.
*
* Created: 15th January 2002
* Updated: 25th September 2007
*
* Thanks: To Carlos Santander Bernal for helping with Mac compatibility.
* To Nevin Liber for pressing upon me the need to lead by
* example when writing books about good design/implementation.
*
* Home: http://stlsoft.org/
*
* Copyright (c) 2002-2007, Matthew Wilson and Synesis Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
* any contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* ////////////////////////////////////////////////////////////////////// */
/** \file unixstl/filesystem/glob_sequence.hpp
*
* \brief [C++ only] Definition of the unixstl::glob_sequence class
* (\ref group__library__filesystem "File System" Library).
*/
#ifndef UNIXSTL_INCL_UNIXSTL_FILESYSTEM_HPP_GLOB_SEQUENCE
#define UNIXSTL_INCL_UNIXSTL_FILESYSTEM_HPP_GLOB_SEQUENCE
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define UNIXSTL_VER_UNIXSTL_FILESYSTEM_HPP_GLOB_SEQUENCE_MAJOR 5
# define UNIXSTL_VER_UNIXSTL_FILESYSTEM_HPP_GLOB_SEQUENCE_MINOR 0
# define UNIXSTL_VER_UNIXSTL_FILESYSTEM_HPP_GLOB_SEQUENCE_REVISION 9
# define UNIXSTL_VER_UNIXSTL_FILESYSTEM_HPP_GLOB_SEQUENCE_EDIT 137
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* Includes
*/
#ifndef UNIXSTL_INCL_UNIXSTL_H_UNIXSTL
# include <unixstl/unixstl.h>
#endif /* !UNIXSTL_INCL_UNIXSTL_H_UNIXSTL */
#ifndef UNIXSTL_INCL_UNIXSTL_FILESYSTEM_HPP_FILESYSTEM_TRAITS
# include <unixstl/filesystem/filesystem_traits.hpp>
#endif /* !UNIXSTL_INCL_UNIXSTL_FILESYSTEM_HPP_FILESYSTEM_TRAITS */
#ifndef UNIXSTL_INCL_UNIXSTL_FILESYSTEM_HPP_FILE_PATH_BUFFER
# include <unixstl/filesystem/file_path_buffer.hpp>
#endif /* !UNIXSTL_INCL_UNIXSTL_FILESYSTEM_HPP_FILE_PATH_BUFFER */
#ifndef STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_HELPER
# include <stlsoft/util/std/iterator_helper.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_HELPER */
#ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_AUTO_BUFFER
# include <stlsoft/memory/auto_buffer.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_AUTO_BUFFER */
#ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_SELECTOR
# include <stlsoft/memory/allocator_selector.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_SELECTOR */
#ifndef STLSOFT_INCL_STLSOFT_SMARTPTR_HPP_SCOPED_HANDLE
# include <stlsoft/smartptr/scoped_handle.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_SMARTPTR_HPP_SCOPED_HANDLE */
#ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_TOKENISER_FUNCTIONS
//# include <stlsoft/string/tokeniser_functions.hpp> // for find_next_token
#endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_TOKENISER_FUNCTIONS */
#ifndef STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS
# include <stlsoft/collections/util/collections.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS */
#include <sys/types.h>
#include <sys/stat.h> // for stat()
#include <errno.h>
#include <glob.h> // for glob(), globfree()
#include <algorithm> // for std::sort
#include <exception> // for std::exception
#include <stdexcept> // for std::runtime_error
#include <utility> // for std::swap
#ifdef STLSOFT_UNITTEST
# include <stlsoft/string/simple_string.hpp>
#endif /* STLSOFT_UNITTEST */
/* /////////////////////////////////////////////////////////////////////////
* Library compatibility
*/
#ifndef GLOB_ONLYDIR
# undef UNIXSTL_GLOB_SEQUENCE_TRUST_ONLYDIR
#endif /* GLOB_ONLYDIR */
// Define UNIXSTL_GLOB_SEQUENCE_DONT_TRUST_MARK to not trust GLOB_MARK
//#define UNIXSTL_GLOB_SEQUENCE_DONT_TRUST_MARK
/* /////////////////////////////////////////////////////////////////////////
* Compiler compatibility
*/
#ifdef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define GLOB_SEQUENCE_CTOR_PRIMARY_FORM
#elif defined(STLSOFT_COMPILER_IS_CUSTOM)
# define GLOB_SEQUENCE_CTOR_PRIMARY_FORM
#elif defined(STLSOFT_COMPILER_IS_UNKNOWN)
# define GLOB_SEQUENCE_CTOR_PRIMARY_FORM
#elif defined(STLSOFT_COMPILER_IS_BORLAND)
# define GLOB_SEQUENCE_CTOR_OLD_FORM
#elif defined(STLSOFT_COMPILER_IS_COMO)
# define GLOB_SEQUENCE_CTOR_PRIMARY_FORM
#elif defined(STLSOFT_COMPILER_IS_DMC)
# define GLOB_SEQUENCE_CTOR_PRIMARY_FORM
#elif defined(STLSOFT_COMPILER_IS_GCC)
# define GLOB_SEQUENCE_CTOR_PRIMARY_FORM
#elif defined(STLSOFT_COMPILER_IS_INTEL)
# define GLOB_SEQUENCE_CTOR_PRIMARY_FORM
#elif defined(STLSOFT_COMPILER_IS_MSVC)
# if _MSC_VER >= 1310
# define GLOB_SEQUENCE_CTOR_PRIMARY_FORM
# elif _MSC_VER >= 1020
# define GLOB_SEQUENCE_CTOR_ALT_FORM
# else /* ? compiler */
# define GLOB_SEQUENCE_CTOR_OLD_FORM
# endif /* compiler */
#elif defined(STLSOFT_COMPILER_IS_MWERKS)
# define GLOB_SEQUENCE_CTOR_ALT_FORM
#elif defined(STLSOFT_COMPILER_IS_VECTORC)
# define GLOB_SEQUENCE_CTOR_ALT_FORM
#elif defined(STLSOFT_COMPILER_IS_WATCOM)
# define GLOB_SEQUENCE_CTOR_OLD_FORM
#else /* ? compiler */
# error Unrecognised compiler
#endif /* compiler */
/* /////////////////////////////////////////////////////////////////////////
* Namespace
*/
#ifndef _UNIXSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
/* There is no stlsoft namespace, so must define ::unixstl */
namespace unixstl
{
# else
/* Define stlsoft::unixstl_project */
namespace stlsoft
{
namespace unixstl_project
{
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_UNIXSTL_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* Classes
*/
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
/** \brief The exception-type thrown by unixstl::glob_sequence
*
* \ingroup group__library__filesystem
*/
class glob_sequence_exception
#if defined(STLSOFT_COMPILER_IS_DMC)
: public std::exception
#else /* ? compiler */
: public unixstl_ns_qual_std(exception)
#endif /* compiler */
{
/// \name Types
/// @{
public:
#if defined(STLSOFT_COMPILER_IS_DMC)
typedef std::exception parent_class_type;
#else /* ? compiler */
typedef unixstl_ns_qual_std(exception) parent_class_type;
#endif /* compiler */
typedef glob_sequence_exception class_type;
/// @}
/// \name Construction
/// @{
public:
ss_explicit_k glob_sequence_exception(us_int_t globStatus, us_int_t errno_) stlsoft_throw_0()
: m_globStatus(globStatus)
, m_errno(errno_)
{}
/// @}
/// \name Accessors
/// @{
public:
#if defined(STLSOFT_COMPILER_IS_DMC)
char const *what() const throw()
#else /* ? compiler */
char const *what() const stlsoft_throw_0()
#endif /* compiler */
{
return "glob_sequence failure";
}
us_int_t get_globstatus() const stlsoft_throw_0()
{
return m_globStatus;
}
us_int_t get_errno() const stlsoft_throw_0()
{
return m_errno;
}
/// @}
/// \name Members
/// @{
private:
us_int_t const m_globStatus;
us_int_t const m_errno;
/// @}
/// \name Not to be implemented
/// @{
private:
class_type& operator =(class_type const&);
/// @}
};
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
/** \brief STL-like readonly sequence based on the results of file-system wildcard matches
*
* \ingroup group__library__filesystem
*
* This class presents and STL-like readonly sequence interface to allow the
* iteration over the results of file-system wildcard matches.
*/
class glob_sequence
: public stlsoft_ns_qual(stl_collection_tag)
{
/// \name Types
/// @{
public:
/// \brief This class
typedef glob_sequence class_type;
/// \brief The char type
typedef us_char_a_t char_type;
// The traits type
typedef filesystem_traits<char_type> traits_type;
/// \brief The value type
typedef char_type const *value_type;
/// \brief The non-mutating (const) reference type
typedef value_type const &const_reference;
/// \brief The non-mutating (const) pointer type
typedef value_type const *const_pointer;
/// \brief The size type
typedef us_size_t size_type;
/// \brief The difference type
typedef us_ptrdiff_t difference_type;
/// \brief The allocator type
typedef stlsoft_ns_qual(allocator_selector)<value_type>::allocator_type allocator_type;
/// \brief The non-mutating (const) iterator type
typedef stlsoft_ns_qual(pointer_iterator)< value_type const
, const_pointer
, const_reference
>::type const_iterator;
#ifdef STLSOFT_CF_BIDIRECTIONAL_ITERATOR_SUPPORT
/// \brief The type of the const (non-mutating) reverse iterator
typedef stlsoft_ns_qual(reverse_iterator_base) < const_iterator
, value_type
, const_reference
, const_pointer
, difference_type
> const_reverse_iterator;
#endif /* STLSOFT_CF_BIDIRECTIONAL_ITERATOR_SUPPORT */
/// @}
/// \name Member Constants
/// @{
public:
enum search_flags
{
includeDots = 0x0008 /*!< \brief Requests that dots directories be included in the returned sequence */
, directories = 0x0010 /*!< \brief Causes the search to include directories */
, files = 0x0020 /*!< \brief Causes the search to include files */
, noSort = 0x0100 /*!< \brief Does not sort entries. Corresponds to GLOB_NOSORT. */
, markDirs = 0x0200 /*!< \brief Mark directories with a trailing path name separator. Corresponds to GLOB_MARK. */
, absolutePath = 0x0400 /*!< \brief Return all entries in absolute format */
, breakOnError = 0x0800 /*!< \brief Causes processing to stop on the first filesystem error. Corresponds to GLOB_ERR. */
, noEscape = 0x1000 /*!< \brief Treats backslashes literally. Corresponds to GLOB_NOESCAPE. */
#ifdef GLOB_PERIOD
, matchPeriod = 0x2000 /*!< \brief Leading '.' can be matched by metacharacters. Corresponds to GLOB_PERIOD. */
#endif /* GLOB_PERIOD */
#ifdef GLOB_BRACE
, bracePatterns = 0x4000 /*!< \brief Allow "{*.cpp;makefile*}" style multi-part patterns. Corresponds to GLOB_BRACE. */
#endif /* GLOB_BRACE */
#ifdef GLOB_TILDE
, expandTilde = 0x8000 /*!< \brief Expand ~ and ~<user> directories. Corresponds to GLOB_TILDE. */
#endif /* GLOB_TILDE */
};
/// @}
/// \name Construction
/// @{
public:
#if defined(GLOB_SEQUENCE_CTOR_PRIMARY_FORM)
/// \brief Constructs a sequence according to the given criteria
///
/// The constructor initialises a glob_sequence instance on the given
/// pattern with the given flags.
///
/// \param pattern The pattern against which to match the file-system contents
/// \param flags Flags to alter the behaviour of the search
///
/// \note If exceptions are supported, then this will throw a glob_sequence_exception
/// on failure of any underlying functions
template<ss_typename_param_k S>
ss_explicit_k glob_sequence(S const& pattern, us_int_t flags = noSort)
: m_flags(validate_flags_(flags))
, m_buffer(1)
{
m_cItems = init_glob_(NULL, stlsoft_ns_qual(c_str_ptr)(pattern));
UNIXSTL_ASSERT(is_valid());
}
# ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
template<ss_typename_param_k S>
glob_sequence(S const& pattern, search_flags flag)
: m_flags(validate_flags_(flag))
, m_buffer(1)
{
m_cItems = init_glob_(NULL, stlsoft_ns_qual(c_str_ptr)(pattern));
UNIXSTL_ASSERT(is_valid());
}
# endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/// \brief Constructs a sequence according to the given criteria
///
/// The constructor initialises a glob_sequence instance on the given
/// pattern with the given flags.
///
/// \param directory The directory in which the pattern is located
/// \param pattern The pattern against which to match the file-system contents
/// \param flags Flags to alter the behaviour of the search
///
/// \note If exceptions are supported, then this will throw a glob_sequence_exception
/// on failure of any underlying functions
template< ss_typename_param_k S1
, ss_typename_param_k S2
>
glob_sequence(S1 const& directory, S2 const& pattern, us_int_t flags = noSort)
: m_flags(validate_flags_(flags))
, m_buffer(1)
{
m_cItems = init_glob_(stlsoft_ns_qual(c_str_ptr)(directory), stlsoft_ns_qual(c_str_ptr)(pattern));
UNIXSTL_ASSERT(is_valid());
}
# ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
template< ss_typename_param_k S1
, ss_typename_param_k S2
>
glob_sequence(S1 const& directory, S2 const& pattern, search_flags flag)
: m_flags(validate_flags_(flag))
, m_buffer(1)
{
m_cItems = init_glob_(stlsoft_ns_qual(c_str_ptr)(directory), stlsoft_ns_qual(c_str_ptr)(pattern));
UNIXSTL_ASSERT(is_valid());
}
# endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -