⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readdir_sequence.hpp

📁 新版本TR1的stl
💻 HPP
📖 第 1 页 / 共 2 页
字号:
/* /////////////////////////////////////////////////////////////////////////
 * File:        unixstl/filesystem/readdir_sequence.hpp
 *
 * Purpose:     readdir_sequence class.
 *
 * Created:     15th January 2002
 * Updated:     22nd March 2007
 *
 * 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/readdir_sequence.hpp
 *
 * \brief [C++ only] Definition of the unixstl::readdir_sequence class
 *   (\ref group__library__filesystem "File System" Library).
 */

#ifndef UNIXSTL_INCL_UNIXSTL_FILESYSTEM_HPP_READDIR_SEQUENCE
#define UNIXSTL_INCL_UNIXSTL_FILESYSTEM_HPP_READDIR_SEQUENCE

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define UNIXSTL_VER_UNIXSTL_FILESYSTEM_HPP_READDIR_SEQUENCE_MAJOR      5
# define UNIXSTL_VER_UNIXSTL_FILESYSTEM_HPP_READDIR_SEQUENCE_MINOR      1
# define UNIXSTL_VER_UNIXSTL_FILESYSTEM_HPP_READDIR_SEQUENCE_REVISION   5
# define UNIXSTL_VER_UNIXSTL_FILESYSTEM_HPP_READDIR_SEQUENCE_EDIT       119
#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 UNIXSTL_INCL_UNIXSTL_HPP_ERROR_UNIX_EXCEPTIONS
# include <unixstl/error/exceptions.hpp>
#endif /* !UNIXSTL_INCL_UNIXSTL_ERROR_HPP_UNIX_EXCEPTIONS */
#include <unistd.h>
#if defined(PATH_MAX)
# ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_STATIC_STRING
#  include <stlsoft/string/static_string.hpp>
# endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_STATIC_STRING */
#else /* ? PATH_MAX */
# ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_SIMPLE_STRING
#  include <stlsoft/string/simple_string.hpp>
# endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_SIMPLE_STRING */
#endif /* !PATH_MAX */
#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_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>
#include <dirent.h>

/* /////////////////////////////////////////////////////////////////////////
 * Namespace
 */

/* No STLSoft namespaces means no UNIXSTL namespaces */
#ifdef _STLSOFT_NO_NAMESPACES
# define _UNIXSTL_NO_NAMESPACES
#endif /* _STLSOFT_NO_NAMESPACES */

/* No UNIXSTL namespaces means no unixstl namespace */
#ifdef _UNIXSTL_NO_NAMESPACES
# define _UNIXSTL_NO_NAMESPACE
#endif /* _UNIXSTL_NO_NAMESPACES */

#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
 */

/** \brief Exception class thrown by unixstl::readdir_sequence.
 *
 * \ingroup group__library__filesystem
 */
class readdir_sequence_exception
    : public unix_exception
{
/// \name Types
/// @{
public:
    typedef unix_exception              parent_class_type;
    typedef readdir_sequence_exception  class_type;
/// @}

/// \name Construction
/// @{
public:
    readdir_sequence_exception(us_char_a_t const* message, us_int_t erno)
        : parent_class_type(message, erno)
    {}
/// @}
};


/** \brief STL-like readonly sequence based on directory contents
 *
 * \ingroup group__library__filesystem
 *
 * This class presents and STL-like readonly sequence interface to allow the
 * iteration over the contents of a directory.
 */
class readdir_sequence
    : public stlsoft_ns_qual(stl_collection_tag)
{
/// \name Member Types
/// @{
public:
    /// \brief This class
    typedef readdir_sequence                                    class_type;
private:
    // These make it easy to move to a template, if ever needed
    typedef us_char_a_t                                         char_type;
    typedef filesystem_traits<char_type>                        traits_type;
public:
    /// \brief The size type
    typedef us_size_t                                           size_type;
    /// \brief The non-mutating (const) iterator type
    class                                                       const_iterator;
    /// \brief The value type
#if defined(UNIXSTL_READDIR_SEQUENCE_OLD_VALUE_TYPE)
    typedef struct dirent const                                 *value_type;
#else /* ? UNIXSTL_READDIR_SEQUENCE_OLD_VALUE_TYPE */
    typedef char_type const                                     *value_type;
#endif /* UNIXSTL_READDIR_SEQUENCE_OLD_VALUE_TYPE */
    /// \brief The flags type
    typedef us_int_t                                            flags_type;

public:
#if defined(PATH_MAX)
    typedef stlsoft_ns_qual(basic_static_string)<   char_type
                                                ,   PATH_MAX
                                                >               string_type;
#else /* ? PATH_MAX */
    typedef stlsoft_ns_qual(basic_simple_string)<   char_type
                                                >               string_type;
#endif /* !PATH_MAX */
/// @}

/// \name Member Constants
/// @{
public:
    enum
    {
            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 */
        ,   fullPath        =   0x0100  /*!< \brief Each file entry is presented as a full path relative to the search directory. */
        ,   absolutePath    =   0x0200  /*!< \brief The search directory is converted to an absolute path. */
    };
/// @}

/// \name Construction
/// @{
public:
    /// \brief Constructs a sequence according to the given criteria
    ///
    /// The constructor initialises a readdir_sequence instance on the given
    /// directory with the given flags.
    ///
    /// \param directory The directory whose contents are to be searched
    /// \param flags Flags to alter the behaviour of the search
    ///
    /// \note The \c flags parameter defaults to <code>directories | files</code> because
    /// this reflects the default behaviour of \c readdir(), and also because it is the
    /// most efficient.
    template <ss_typename_param_k S>
    readdir_sequence(S const& directory, flags_type flags = directories | files)
        : m_flags(validate_flags_(flags))
        , m_directory(prepare_directory_(stlsoft_ns_qual(c_str_ptr)(directory), flags))
    {}
/// @}

/// \name Iteration
/// @{
public:
    /// \brief Begins the iteration
    ///
    /// \return An iterator representing the start of the sequence
    const_iterator  begin() const;
    /// \brief Ends the iteration
    ///
    /// \return An iterator representing the end of the sequence
    const_iterator  end() const;
/// @}

/// \name Attributes
/// @{
public:
    /// \brief Indicates whether the search sequence is empty
    us_bool_t empty() const;

    /// \brief The search directory
    ///
    /// \note The value returned by this method always has a trailing path name separator, so
    /// you can safely concatenate this with the value returned by the iterator's operator *()
    /// with minimal fuss.
    string_type const   &get_directory() const;

    /// \brief The flags used by the sequence
    ///
    /// \note This value is the value used by the sequence, which may, as a result of the
    /// determination of defaults, be different from those specified in its constructor. In
    /// other words, if <code>includeDots</code> is specified, this function
    /// will return <code>includeDots | directories | files</code>
    flags_type          get_flags() const;
/// @}

/// \name Implementation
/// @{
private:
    /// \brief Ensures that the flags are correct
    static flags_type   validate_flags_(flags_type flags);

    /// \brief Prepares the directory, according to the given flags
    static string_type  prepare_directory_(char_type const* directory, flags_type flags);
/// @}

/// \name Members
/// @{
private:
    const flags_type    m_flags;
    const string_type   m_directory;
/// @}

/// \name Not to be implemented
/// @{
private:
    readdir_sequence(class_type const&);
    class_type& operator =(class_type const&);
/// @}
};

/** \brief Iterator for the \link unixstl::readdir_sequence readdir_sequence\endlink class.
 *
 * \ingroup group__library__filesystem
 *
 * This class performs as a non-mutating iterator (aka const iterator) for the
 * readdir_sequence class.
 */
class readdir_sequence::const_iterator
    : public stlsoft_ns_qual(iterator_base)<unixstl_ns_qual_std(input_iterator_tag)
                                        ,   readdir_sequence::value_type
                                        ,   us_ptrdiff_t
                                        ,   void                            // By-Value Temporary reference
                                        ,   readdir_sequence::value_type    // By-Value Temporary reference
                                        >
{
/// \name Members
/// @{
private:
    typedef readdir_sequence::string_type           string_type;
public:
    /// \brief The class type
    typedef const_iterator                          class_type;
    /// \brief The value type
    typedef readdir_sequence::value_type            value_type;
    /// \brief The flags type
    typedef readdir_sequence::flags_type            flags_type;
//    typedef value_type                            *pointer;
//    typedef value_type                            &reference;
/// @}

/// \name Construction
/// @{
private:
    friend class readdir_sequence;

    /// \brief Construct an instance and begin a sequence iteration on the given dir.
    const_iterator(DIR *dir, string_type const& directory, flags_type flags);
public:
    /// \brief Default constructor
    const_iterator();
    /// \brief Copy constructor
    const_iterator(class_type const& rhs);
    /// \brief Release the search handle
    ~const_iterator() stlsoft_throw_0();

    /// \brief Copy assignment operator
    class_type const& operator =(class_type const& rhs);
/// @}

/// \name Input Iterator Methods
/// @{
public:
    /// \brief Returns the value representative
    value_type operator *() const;

    /// \brief Moves the iteration on to the next point in the sequence, or end() if
    /// the sequence is exhausted
    class_type& operator ++();

    /// \brief Post-increment form of operator ++().
    ///
    /// \note Because this version uses a temporary on which to call the
    /// pre-increment form it is thereby less efficient, and should not be used
    /// except where post-increment semantics are required.
    class_type operator ++(int);

    /// \brief Compares \c this for equality with \c rhs
    ///
    /// \param rhs The instance against which to test
    /// \retval true if the iterators are equivalent
    /// \retval false if the iterators are not equivalent
    bool equal(class_type const& rhs) const;
/// @}

/// \name Members
/// @{
private:
    struct shared_handle;

    shared_handle   *m_handle;  // The DIR handle, shared with other iterator instances
    struct dirent   *m_entry;   // The current entry
    flags_type      m_flags;    // flags. (Only non-const, to allow copy assignment)
    string_type     m_scratch;  // Holds the directory, and is a scratch area
    size_type       m_dirLen;   // The length of the directory
/// @}
};

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
struct readdir_sequence::const_iterator::shared_handle
{
/// \name Member Types
/// @{
public:
    typedef shared_handle   class_type;
    typedef DIR             *handle_type;
/// @}

/// \name Members
/// @{
public:
    handle_type m_dir;
private:
    ss_sint32_t m_refCount;
/// @}

/// \name Construction
/// @{
public:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -