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

📄 directory_functions.hpp

📁 用STL的方式封装了WindowsAPI、COM调用、ACE、ATL、MFC、WTL等多种组件
💻 HPP
📖 第 1 页 / 共 2 页
字号:
/* /////////////////////////////////////////////////////////////////////////
 * File:        winstl/filesystem/directory_functions.hpp (originally MLFlMan.h, ::SynesisStd)
 *
 * Purpose:     Functions for manipulating directories.
 *
 * Created:     7th February 2002
 * Updated:     10th June 2006
 *
 * Home:        http://stlsoft.org/
 *
 * Copyright (c) 2002-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/directory_functions.hpp
 *
 * \brief [C++ only] Functions for manipulating directories.
 *  (\ref group__library__file_system "File System" Library.)
 */

#ifndef WINSTL_INCL_WINSTL_FILESYSTEM_HPP_DIRECTORY_FUNCTIONS
#define WINSTL_INCL_WINSTL_FILESYSTEM_HPP_DIRECTORY_FUNCTIONS

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define WINSTL_VER_WINSTL_FILESYSTEM_HPP_DIRECTORY_FUNCTIONS_MAJOR     5
# define WINSTL_VER_WINSTL_FILESYSTEM_HPP_DIRECTORY_FUNCTIONS_MINOR     0
# define WINSTL_VER_WINSTL_FILESYSTEM_HPP_DIRECTORY_FUNCTIONS_REVISION  1
# define WINSTL_VER_WINSTL_FILESYSTEM_HPP_DIRECTORY_FUNCTIONS_EDIT      36
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

/* /////////////////////////////////////////////////////////////////////////
 * Compatibility
 */

/*
[Incompatibilies-start]
STLSOFT_COMPILER_IS_MSVC: _MSC_VER<1200
[Incompatibilies-end]
 */

/* /////////////////////////////////////////////////////////////////////////
 * 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 */
#ifdef _ATL_MIN_CRT
# ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_SELECTOR
#  include <stlsoft/memory/allocator_selector.hpp>
# endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_SELECTOR */
#endif /* _ATL_MIN_CRT */

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

/* /////////////////////////////////////////////////////////////////////////
 * Helper functions
 */

template <ss_typename_param_k C>
inline C *find_last_path_name_separator_(C const *s)
{
    typedef filesystem_traits<C>    traits_t;

    ss_typename_type_k traits_t::char_type const    *slash  =   traits_t::str_rchr(s, '/');
    ss_typename_type_k traits_t::char_type const    *bslash =   traits_t::str_rchr(s, '\\');

    if(NULL == slash)
    {
        slash = bslash;
    }
    else if(NULL != bslash)
    {
        if(slash < bslash)
        {
            slash = bslash;
        }
    }

    return const_cast<C*>(slash);
}

template <ss_typename_param_k C>
inline ws_bool_t create_directory_recurse_impl(C const *dir, LPSECURITY_ATTRIBUTES lpsa)
{
    typedef C                                   char_type;
    typedef filesystem_traits<C>                traits_t;
#ifdef _ATL_MIN_CRT
    typedef ss_typename_type_k stlsoft_ns_qual(allocator_selector)<C>::allocator_type   allocator_t;
    typedef basic_file_path_buffer< char_type
                                ,   allocator_t
                                >               file_path_buffer_t;
#else /* ? _ATL_MIN_CRT */
    typedef basic_file_path_buffer<char_type>   file_path_buffer_t;
#endif /* _ATL_MIN_CRT */

    ws_bool_t    bRet;

    if( NULL == dir ||
        '\0' == *dir)
    {
        traits_t::set_last_error(ERROR_DIRECTORY);

        bRet = false;
    }
    else
    {
        if(traits_t::file_exists(dir))
        {
            if(traits_t::is_directory(dir))
            {
                traits_t::set_last_error(ERROR_ALREADY_EXISTS);

                bRet = true;
            }
            else
            {
                traits_t::set_last_error(ERROR_FILE_EXISTS);

                bRet = false;
            }
        }
        else
        {
            file_path_buffer_t  sz;
            file_path_buffer_t  szParent;

            // May be being compiled absent exception support, so need to check the
            // file path buffers. (This _could_ be done with a compile-time #ifdef,
            // but it's best not, since some translators support exceptions but yet
            // don't throw on mem exhaustion, and in any case a user could change
            // ::new)
            if( 0 == sz.size() ||
                0 == szParent.size())
            {
                bRet = false;
            }
            else
            {
                traits_t::str_copy(&sz[0], dir);
                traits_t::remove_dir_end(&sz[0]);

                if( traits_t::create_directory(sz.c_str(), lpsa) ||
                    ERROR_ALREADY_EXISTS == traits_t::get_last_error())
                {
                    traits_t::set_last_error(ERROR_SUCCESS);

                    bRet = true;
                }
                else
                {
                    // Trim previous directory
                    traits_t::str_copy(&szParent[0], sz.c_str());

                    char_type   *pszSlash = find_last_path_name_separator_<C>(szParent.c_str());
                    if(pszSlash == NULL)
                    {
                        traits_t::set_last_error(ERROR_DIRECTORY);

                        bRet = false;
                    }
                    else
                    {
                        *pszSlash = '\0';                   // Will always have enough room for two bytes

                        // If second character is ':', and total lengths is less than four,
                        // or the recurse create fails, then return false;
                        if( (   szParent[1] == ':' &&
                                (traits_t::set_last_error(ERROR_CANNOT_MAKE), traits_t::str_len(szParent.c_str()) < 4)) ||
                            !create_directory_recurse(szParent.c_str(), lpsa))
                        {
                            bRet = false;
                        }
                        else
                        {
                            bRet = traits_t::create_directory(sz.c_str(), lpsa) || ERROR_ALREADY_EXISTS == traits_t::get_last_error();
                        }
                    }
                }
            }
        }
    }

    return bRet;
}

template<   ss_typename_param_k C
        ,   ss_typename_param_k FD  // This is need because VC++6 cannot deduce filesystem_traits<C>::find_data_type
        >
inline ws_dword_t remove_directory_recurse_impl(C const *dir, ws_int_t (*pfn)(void *param, C const *subDir, FD const *st, DWORD err), void *param)
{
    typedef C                                   char_type;
    typedef filesystem_traits<C>                traits_t;
#ifdef _ATL_MIN_CRT
    typedef ss_typename_type_k stlsoft_ns_qual(allocator_selector)<C>::allocator_type   allocator_t;
    typedef basic_file_path_buffer< char_type
                                ,   allocator_t
                                >   file_path_buffer_t;
#else /* ? _ATL_MIN_CRT */
    typedef basic_file_path_buffer<char_type>   file_path_buffer_t;
#endif /* _ATL_MIN_CRT */
    ws_dword_t                                  dwRet = static_cast<ws_dword_t>(E_FAIL);

    if(NULL != pfn)
    {
        (void)(*pfn)(param, dir, NULL, ~static_cast<ws_dword_t>(0)); // Entering
    }

    if( NULL == dir ||
        '\0' == *dir)
    {
        dwRet = ERROR_DIRECTORY;

        if(NULL != pfn)
        {
            (void)(*pfn)(param, dir, NULL, dwRet);
        }
    }
    else
    {
        if(!traits_t::file_exists(dir))
        {
            // The given path does not exist, so this is treated as success, but
            // reporting ERROR_PATH_NOT_FOUND

            dwRet = ERROR_PATH_NOT_FOUND;

            if(NULL != pfn)
            {
                (void)(*pfn)(param, dir, NULL, dwRet);
            }
        }
        else
        {
            if(traits_t::is_file(dir))
            {
                // The given path exists as a file. This is failure
                dwRet = ERROR_FILE_EXISTS;

                if(NULL != pfn)
                {
                    (void)(*pfn)(param, dir, NULL, dwRet);
                }
            }
            else
            {
                // Otherwise, we attempt to remove it
                if(traits_t::remove_directory(dir))
                {
                    dwRet = ERROR_SUCCESS;

                    if(NULL != pfn)
                    {

⌨️ 快捷键说明

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