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

📄 path.hpp

📁 用STL的方式封装了WindowsAPI、COM调用、ACE、ATL、MFC、WTL等多种组件
💻 HPP
📖 第 1 页 / 共 4 页
字号:
/* /////////////////////////////////////////////////////////////////////////
 * File:        winstl/filesystem/path.hpp (formerly winstl_path.h; originally MLPath.h, ::SynesisStd)
 *
 * Purpose:     Simple class that represents a path.
 *
 * Created:     1st May 1993
 * Updated:     18th June 2006
 *
 * Home:        http://stlsoft.org/
 *
 * Copyright (c) 1993-2006, 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 winstl/filesystem/path.hpp
 *
 * \brief [C++ only] Definition of the winstl::basic_path class template.
 *  (\ref group__library__file_system "File System" Library.)
 */

#ifndef WINSTL_INCL_WINSTL_FILESYSTEM_HPP_PATH
#define WINSTL_INCL_WINSTL_FILESYSTEM_HPP_PATH

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define WINSTL_VER_WINSTL_FILESYSTEM_HPP_PATH_MAJOR    6
# define WINSTL_VER_WINSTL_FILESYSTEM_HPP_PATH_MINOR    2
# define WINSTL_VER_WINSTL_FILESYSTEM_HPP_PATH_REVISION 2
# define WINSTL_VER_WINSTL_FILESYSTEM_HPP_PATH_EDIT     216
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

/* /////////////////////////////////////////////////////////////////////////
 * Includes
 */

#ifndef WINSTL_INCL_WINSTL_H_WINSTL
# include <winstl/winstl.h>
#endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
#ifndef WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FILESYSTEM_TRAITS
# include <winstl/filesystem/filesystem_traits.hpp>
#endif /* !WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FILESYSTEM_TRAITS */
#ifndef WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FILE_PATH_BUFFER
# include <winstl/filesystem/file_path_buffer.hpp>
#endif /* !WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FILE_PATH_BUFFER */
#ifndef WINSTL_INCL_WINSTL_MEMORY_HPP_PROCESSHEAP_ALLOCATOR
# include <winstl/memory/processheap_allocator.hpp>
#endif /* !WINSTL_INCL_WINSTL_MEMORY_HPP_PROCESSHEAP_ALLOCATOR */
#ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_FEATURES
# include <stlsoft/memory/allocator_features.hpp>   // for STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT
#endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_FEATURES */
#ifndef STLSOFT_INCL_STLSOFT_HPP_MEMORY_AUTO_BUFFER
# include <stlsoft/memory/auto_buffer.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_HPP_MEMORY_AUTO_BUFFER */
#ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_COPY_FUNCTIONS
# include <stlsoft/string/copy_functions.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_COPY_FUNCTIONS */
#ifndef STLSOFT_INCL_STLSOFT_HPP_STRING_ACCESS
# include <stlsoft/string_access.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_HPP_STRING_ACCESS */
#ifndef WINSTL_INCL_WINSTL_HPP_STRING_ACCESS
# include <winstl/string_access.hpp>            // for string access shims
#endif /* !WINSTL_INCL_WINSTL_HPP_STRING_ACCESS */
#include <stdexcept>                            // for std::logic_error

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

#ifndef _WINSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
     defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
/* There is no stlsoft namespace, so must define ::winstl */
namespace winstl
{
# else
/* Define stlsoft::winstl_project */

namespace stlsoft
{

namespace winstl_project
{

# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */

/* /////////////////////////////////////////////////////////////////////////
 * basic_path
 *
 * This class represents a path, and effectively acts as a C-string of its value.
 */

/** \brief Class used for composing and decomposing file-system paths.
 *
 * \ingroup group__library__file_system
 *
 * \param C The character type
 * \param T The traits type. On translators that support default template arguments, this defaults to filesystem_traits<C>
 * \param A The allocator type. On translators that support default template arguments, this defaults to processheap_allocator<C>
 *
 * \note This class derives from the Synesis Software class Path, but has been influenced
 * by other, later, ideas. The idea of using the / operator for path concatenation was
 * sparked by the Boost implementation (although the details were not investigated prior
 * to this implementation, so the two may have significant semantic differences). This
 * has been added without requiring any major fundamental changes to the original
 * <code>push/pop</code>-based interface
 */
template<   ss_typename_param_k C
#ifdef STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT
        ,   ss_typename_param_k T = filesystem_traits<C>
        ,   ss_typename_param_k A = processheap_allocator<C>
#else /* ? STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
        ,   ss_typename_param_k T /* = filesystem_traits<C> */
        ,   ss_typename_param_k A /* = processheap_allocator<C> */
#endif /* STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
        >
class basic_path
{
/// \name Types
/// @{
public:
    /// \brief The char type
    typedef C                           char_type;
    /// \brief The traits type
    typedef T                           traits_type;
    /// \brief The allocator type
    typedef A                           allocator_type;
    /// \brief The current parameterisation of the type
    typedef basic_path<C, T, A>         class_type;
    /// \brief The size type
    typedef ws_size_t                   size_type;
    /// \brief The Boolean type
    typedef ws_bool_t                   bool_type;

// TODO: Use the slice string, and provide iterators over the directory parts

/// @}

/// \name Construction
/// @{
public:
    /// \brief Constructs an empty path.
    ///
    /// \htmlonly
    /// <pre>
    /// winstl::path  p;
    ///
    /// assert(0 == p.size());
    /// assert("" == p);
    /// </pre>
    /// \endhtmlonly
    basic_path();
    /// \brief Constructs a path from a nul-terminated character string.
    ///
    /// \htmlonly
    /// <pre>
    /// winstl::path  p("C:\\Windows");
    ///
    /// assert(10 == p.size());
    /// assert("C:\\Windows" == p);
    /// assert("C:\\WINDOWS" == p);
    /// assert("c:\\windows" == p);
    /// </pre>
    /// \endhtmlonly
    ss_explicit_k basic_path(char_type const *path);
#ifdef STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
    /// \brief Constructs a path from a string object.
    ///
    /// \remarks The string object may be any type for which the 
    ///  <b>c_str_len</b> and <b>c_str_data</b> 
    ///  \ref group__concept__string_access_shims "String Access Shims" are
    ///  defined. The following example shows the use of a window handle
    ///  (HWND). Other types supported are <code>std::exception</code>,
    ///  <code>std::string</code>, <code>stlsoft::simple_string</code>,
    ///  <code>winstl::error_desc</code>, and many more
    ///
    /// \htmlonly
    /// <pre>
    /// HWND          hwndParent = . . . 
    /// HWND          hwnd       = winstl::CreateEdit(hwndParent, "C:\\Windows", ES_LEFT, 0, 0, 10, 10, 1001);
    /// winstl::path  p(hwnd);
    ///
    /// assert(10 == p.size());
    /// assert("C:\\Windows" == p);
    /// assert("C:\\WINDOWS" == p);
    /// assert("c:\\windows" == p);
    /// </pre>
    /// \endhtmlonly
    template<ss_typename_param_k S>
    ss_explicit_k basic_path(S const &s)
    {
        m_len = stlsoft_ns_qual(c_str_len)(s);

        traits_type::str_n_copy(&m_buffer[0], stlsoft_ns_qual(c_str_data)(s), m_len);
        m_buffer[m_len] = '\0';
    }
#endif /* STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
    /// \brief Constructs a path from a slice of a character string.
    ///
    /// \htmlonly
    /// <pre>
    /// winstl::path  p("C:\\Windows\\system32", 10);
    ///
    /// assert(10 == p.size());
    /// assert("C:\\Windows" == p);
    /// assert("C:\\WINDOWS" == p);
    /// assert("c:\\windows" == p);
    /// </pre>
    /// \endhtmlonly
    basic_path(char_type const *path, size_type cch);

#ifndef STLSOFT_CF_NO_COPY_CTOR_AND_COPY_CTOR_TEMPLATE_OVERLOAD
    /// \brief Copies the contents of \c rhs
    basic_path(class_type const &rhs);
#endif /* !STLSOFT_CF_NO_COPY_CTOR_AND_COPY_CTOR_TEMPLATE_OVERLOAD */

#ifndef STLSOFT_CF_NO_COPY_CTOR_AND_COPY_CTOR_TEMPLATE_OVERLOAD
    /// \brief Copies the contents of \c rhs
    class_type &operator =(class_type const &rhs);
#endif /* !STLSOFT_CF_NO_COPY_CTOR_AND_COPY_CTOR_TEMPLATE_OVERLOAD */
    /// \brief Copies the contents of \c rhs
    class_type &operator =(char_type const *rhs);
#ifdef STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
    /// \brief Copies the contents of \c s
    template<ss_typename_param_k S>
    class_type &operator =(S const &s)
    {
        return operator =(stlsoft_ns_qual(c_str_ptr)(s));
    }
#endif /* STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT */

    // Creates a root path
    static class_type root(char_type const *s);
#ifdef STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
    // Creates a root path
    template<ss_typename_param_k S>
    static class_type root(S const &s)
    {
        return root(stlsoft_ns_qual(c_str_ptr)(s));
    }
#endif /* STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
/// @}

/// \name Operations
/// @{
public:
    /// \brief Appends the contents of \c rhs to the path
    class_type  &push(class_type const &rhs, bool_type bAddPathNameSeparator = false);
    /// \brief Appends the contents of \c rhs to the path
    class_type  &push(char_type const *rhs, bool_type bAddPathNameSeparator = false);
    /// \brief Appends the contents of \c rhs to the path as an extension
    class_type  &push_ext(class_type const &rhs, bool_type bAddPathNameSeparator = false);
    /// \brief Appends the contents of \c rhs to the path as an extension
    class_type  &push_ext(char_type const *rhs, bool_type bAddPathNameSeparator = false);
    /// \brief Ensures that the path has a trailing path name separator
    class_type  &push_sep();
    /// \brief Pops the last path element from the path
    ///
    /// \note In previous versions, this operation did not remove the
    ///   left-most path component. That behaviour is no longer supported,
    ///   and the method will now leave the path instance empty in that
    ///   case.
    class_type &pop(bool_type bRemoveTrailingPathNameSeparator = true);
    /// \brief Ensures that the path does not have a trailing path name separator
    ///
    /// \note Does not trim the separator character from the root designator
    class_type  &pop_sep();
    /// \brief Removes the extension, if any, from the file component of the path
    class_type  &pop_ext();

    /// \brief Equivalent to push()
    class_type  &operator /=(char_type const *rhs);

#if !defined(STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT) || \
    defined(STLSOFT_CF_MEMBER_TEMPLATE_OVERLOAD_DISCRIMINATED)
    /// \brief Equivalent to push()
    class_type  &operator /=(class_type const &rhs);
#endif /* !STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT || STLSOFT_CF_MEMBER_TEMPLATE_OVERLOAD_DISCRIMINATED */

#if defined(STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT)
    /// \brief Equivalent to push()
    template <ss_typename_param_k S>
    class_type  &operator /=(S const &rhs)
    {
        return operator /=(stlsoft_ns_qual(c_str_ptr)(rhs));
    }
#endif /* STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT */

    /// \brief Removes all content
    void        clear();

    /// \brief Converts the path to absolute form
    class_type  &make_absolute(bool_type bRemoveTrailingPathNameSeparator = true);
    /// \brief Canonicalises the path, removing all "./" parts and evaluating all "../" parts
    class_type  &canonicalise(bool_type bRemoveTrailingPathNameSeparator = true);
/// @}

/// \name Attributes
/// @{
public:
    /// \brief Returns a pointer to the part of the path after the last path name separator
    ///
    /// \note If the path represents a directory, the name of the directory will be returned, except
    /// if the path is terminated by the path name separator
    ///
    /// \note If the path contains no path name separator, the full path will be returned
    char_type const *get_file() const;
    /// \brief Returns a pointer to the extension, or to the empty string if there is no extension
    char_type const *get_ext() const;
    /// \brief Returns the length of the converted path
    size_type       length() const;
    /// \brief Returns the length of the converted path
    ///
    /// \remarks Equivalent to length()
    size_type       size() const;
    /// \brief Conversion to a non-mutable (const) pointer to the path
    char_type const *c_str() const;
    /// \brief Returns a non-mutable (const) reference to the character at
    ///  the given index
    ///
    /// \note The behaviour is undefined if <code>index >= size()</code>.
    char_type const &operator [](size_type index) const;
    /// \brief Indicates whether the path represents an existing file system entry
    bool_type       exists() const;
    /// \brief Indicates whether the path is rooted
    bool_type       is_rooted() const;
    /// \brief Indicates whether the path is absolute
    bool_type       is_absolute() const;

    /// \brief Copies the contents into a caller supplied buffer
    ///
    /// \param buffer Pointer to character buffer to receive the contents.
    ///  May be NULL, in which case the method returns size().
    /// \param cchBuffer Number of characters of available space in \c buffer.
    size_type       copy(char_type *buffer, size_type cchBuffer) const;
/// @}

/// \name Comparison
/// @{
public:
    /// \brief Determines whether the instance holds a string that refers to
    ///  the same file-system entity as the given string.
    ///
    /// \htmlonly
    /// <pre>
    /// winstl::path  p("C:\\Windows\\system32\\..");
    /// 
    /// assert(p != "C:\\WINDOWS\\");
    /// assert(!p.equal("C:\\WINDOWS\\"));
    /// assert(p.equivalent("C:\\WINDOWS\\"));
    /// </pre>
    /// \endhtmlonly
    bool_type equivalent(char_type const *rhs) const;

    /// \brief Evaluates whether the two instances hold strings that refer
    ///  to the same file-system entity.
    ///
    /// \remarks See \link winstl::basic_path::equivalent(char_type const *) equivalent()\endlink for an example.
    ///
    /// \note The string comparison is case-insensitive.
    bool_type equivalent(class_type const &rhs) const;

    /// \brief Evaluates whether the instance holds an identical string.
    ///
    /// \note The string comparison is case-insensitive.
    bool_type equal(char_type const *rhs) const;
    /// \brief Evaluates whether the two instances hold identical strings.
    ///
    /// \note The string comparison is case-insensitive.
    bool_type equal(class_type const &rhs) const;
/// @}

/// \name Iteration
/// @{
public:
#if 0
    directory_iterator  dir_begin() const;
    directory_iterator  dir_end() const;
#endif /* 0 */
/// @}

// Implementation
private:
    class_type              &push_sep_(char_type sep);
    void                    swap(class_type &rhs);
    class_type              &concat_(char_type const *rhs);

⌨️ 快捷键说明

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