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

📄 filesystem_traits.hpp

📁 用STL的方式封装了WindowsAPI、COM调用、ACE、ATL、MFC、WTL等多种组件
💻 HPP
📖 第 1 页 / 共 5 页
字号:
    }

    static size_type get_current_directory(size_type cchBuffer, char_type *buffer)
    {
        return ::GetCurrentDirectoryA(cchBuffer, buffer);
    }

    static bool_type file_exists(char_type const *fileName)
    {
        return 0xFFFFFFFF != ::GetFileAttributesA(fileName);
    }

    static bool_type is_file(char_type const *path)
    {
        DWORD   attr = ::GetFileAttributesA(path);

        return 0xFFFFFFFF != attr && 0 == (attr & FILE_ATTRIBUTE_DIRECTORY);
    }

    static bool_type is_directory(char_type const *path)
    {
        DWORD   attr = ::GetFileAttributesA(path);

        return 0xFFFFFFFF != attr && 0 != (attr & FILE_ATTRIBUTE_DIRECTORY);
    }

private:
    static bool_type stat_direct_(char_type const *path, stat_data_type *stat_data)
    {
        WINSTL_ASSERT(NULL != path);
        WINSTL_ASSERT(NULL != stat_data);

        if(is_root_drive_(path))
        {
            stat_data->dwFileAttributes                 =   ::GetFileAttributesA(path);
            stat_data->ftCreationTime.dwLowDateTime     =   0;
            stat_data->ftCreationTime.dwHighDateTime    =   0;
            stat_data->ftLastAccessTime.dwLowDateTime   =   0;
            stat_data->ftLastAccessTime.dwHighDateTime  =   0;
            stat_data->ftLastWriteTime.dwLowDateTime    =   0;
            stat_data->ftLastWriteTime.dwHighDateTime   =   0;
            stat_data->nFileSizeHigh                    =   0;
            stat_data->nFileSizeLow                     =   0;
            { for(ws_size_t i = 0; i < 4; ++i)
            {
                stat_data->cFileName[i]             =   path[i];
                stat_data->cAlternateFileName[i]    =   path[i];
            }}

            return 0xFFFFFFFF != stat_data->dwFileAttributes;
        }

        HANDLE  h   =   find_first_file(path, stat_data);

        return (INVALID_HANDLE_VALUE == h) ? false : (find_file_close(h), true);
    }
public:
    static bool_type stat(char_type const *path, stat_data_type *stat_data)
    {
        WINSTL_ASSERT(NULL != path);
        WINSTL_ASSERT(NULL != stat_data);

#if !defined(STLSOFT_COMPILER_IS_MSVC) || \
    _MSC_VER >= 1200
        size_type   len         =   class_type::str_len(path);
        bool_type   bTryEndTest =   false;

        if(is_path_absolute(path))
        {
            if(len > 4)
            {
                bTryEndTest = true;
            }
        }
        else if(is_path_rooted(path))
        {
            if(len > 3)
            {
                bTryEndTest = true;
            }
        }
        else if(len > 2)
        {
            bTryEndTest = true;
        }

        if( bTryEndTest &&
            class_type::has_dir_end(path + len - 2))
        {
            typedef stlsoft_ns_qual(auto_buffer)<char_type> buffer_t;

            WINSTL_ASSERT(len > 2);

            buffer_t    buffer(1 + len);

            if(0 == buffer.size())
            {
                return false;
            }
            else
            {
                WINSTL_ASSERT(len > 0);

                class_type::str_copy(&buffer[0], path);

                buffer[len - 1] = '\0';

                return class_type::stat_direct_(buffer.data(), stat_data);
            }
        }
        else
#endif /* compiler */
        {
            return stat_direct_(path, stat_data);
        }
    }

    static bool_type fstat(file_handle_type fd, fstat_data_type *fstat_data)
    {
        return filesystem_traits_::fstat(fd, fstat_data);
    }

    static bool_type is_file(stat_data_type const *stat_data)
    {
        return FILE_ATTRIBUTE_DIRECTORY != (stat_data->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
    }
    static bool_type is_directory(stat_data_type const *stat_data)
    {
        return FILE_ATTRIBUTE_DIRECTORY == (stat_data->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
    }
    static bool_type is_link(stat_data_type const * /* stat_data */)
    {
        return false;
    }
    static bool_type is_readonly(stat_data_type const *stat_data)
    {
        return FILE_ATTRIBUTE_READONLY == (stat_data->dwFileAttributes & FILE_ATTRIBUTE_READONLY);
    }

    static bool_type is_file(fstat_data_type const *stat_data)
    {
        return filesystem_traits_::is_file(stat_data);
    }
    static bool_type is_directory(fstat_data_type const *stat_data)
    {
        return filesystem_traits_::is_directory(stat_data);
    }
    static bool_type is_link(fstat_data_type const *stat_data)
    {
        return filesystem_traits_::is_link(stat_data);
    }
    static bool_type is_readonly(fstat_data_type const *stat_data)
    {
        return filesystem_traits_::is_readonly(stat_data);
    }

    static bool_type drive_exists(char_type driveLetter)
    {
        WINSTL_ASSERT(::IsCharAlphaA(driveLetter));

        const DWORD drivesBitmap    =   ::GetLogicalDrives();
        const int   driveOrdinal    =   (driveLetter - (::IsCharUpperA(driveLetter) ? 'A' : 'a'));

        return 0 != ((0x01 << driveOrdinal) & drivesBitmap);
    }

    static DWORD get_drive_type(char_type driveLetter)
    {
        WINSTL_ASSERT(::IsCharAlphaA(driveLetter));

        char_type   drive[] = { '?', ':', '\\', '\0' };

        return (drive[0] = driveLetter, ::GetDriveTypeA(drive));
    }

    // File-system control

    static bool_type    create_directory(char_type const *dir)
    {
        return FALSE != ::CreateDirectoryA(dir, NULL);
    }

    static bool_type    create_directory(char_type const *dir, LPSECURITY_ATTRIBUTES lpsa)
    {
        return FALSE != ::CreateDirectoryA(dir, lpsa);
    }

    static bool_type    remove_directory(char_type const *dir)
    {
        return FALSE != ::RemoveDirectoryA(dir);
    }

    static bool_type    unlink_file(char_type const *file)
    {
        return FALSE != ::DeleteFileA(file);
    }

    static bool_type    delete_file(char_type const *file)
    {
        return unlink_file(file);
    }

    static bool_type    rename_file(char_type const *currentName, char_type const *newName)
    {
        return FALSE != ::MoveFileA(currentName, newName);
    }

    static bool_type    copy_file(char_type const *sourceName, char_type const *newName, bool_type bFailIfExists = false)
    {
        return FALSE != ::CopyFileA(sourceName, newName, bFailIfExists);
    }

    // File functions

    static file_handle_type create_file(char_type const *fileName, size_type desiredAccess, size_type shareMode, LPSECURITY_ATTRIBUTES sa, size_type creationDisposition, size_type flagAndAttributes, file_handle_type hTemplateFile)
    {
        return ::CreateFileA(fileName, desiredAccess, shareMode, sa, creationDisposition, flagAndAttributes, hTemplateFile);
    }

    static bool_type close_handle(HANDLE h)
    {
        return filesystem_traits_::close_handle(h);
    }

    static bool_type close_file(file_handle_type h)
    {
        return filesystem_traits_::close_handle(h);
    }

#ifdef STLSOFT_CF_64BIT_INT_SUPPORT
    static ws_uint64_t  get_file_size(file_handle_type h)
    {
        return filesystem_traits_::get_file_size(h);
    }
#endif /* STLSOFT_CF_64BIT_INT_SUPPORT */

    // Error

    static error_type get_last_error()
    {
        return filesystem_traits_::get_last_error();
    }

    static void set_last_error(error_type er = error_type())
    {
        filesystem_traits_::set_last_error(er);
    }

    // Environment

    static size_type get_environment_variable(char_type const *name, char_type *buffer, size_type cchBuffer)
    {
        return ::GetEnvironmentVariableA(name, buffer, cchBuffer);
    }

    static size_type expand_environment_strings(char_type const *src, char_type *dest, size_type cch_dest)
    {
        return static_cast<size_type>(::ExpandEnvironmentStringsA(src, dest, cch_dest));
    }
};

STLSOFT_TEMPLATE_SPECIALISATION
struct filesystem_traits<ws_char_w_t>
{
public:
    typedef ws_char_w_t                             char_type;
    typedef ws_size_t                               size_type;
    typedef ws_ptrdiff_t                            difference_type;
    typedef WIN32_FIND_DATAW                        find_data_type;
    typedef WIN32_FIND_DATAW                        stat_data_type;
    typedef BY_HANDLE_FILE_INFORMATION              fstat_data_type;
    typedef filesystem_traits<char_type>            class_type;
    typedef ws_int_t                                int_type;
    typedef ws_bool_t                               bool_type;
    typedef HANDLE                                  file_handle_type;
    typedef HINSTANCE                               module_type;
    typedef DWORD                                   error_type;
private:
#if !defined(STLSOFT_COMPILER_IS_MSVC) || \
    _MSC_VER >= 1200
    typedef stlsoft_ns_qual(auto_buffer)<char_type> buffer_type_;
#endif /* compiler */
public:

    enum
    {
        maxPathLength   =   1 + _MAX_PATH       //!< The maximum length of a path for the current file system
    };

public:
    // General string handling
    static char_type *str_copy(char_type *dest, char_type const *src)
    {
        return ::lstrcpyW(dest, src);
    }

    static char_type *str_n_copy(char_type *dest, char_type const *src, size_type cch)
    {
        return ::wcsncpy(dest, src, cch);
    }

    static char_type *str_cat(char_type *dest, char_type const *src)
    {
        return ::lstrcatW(dest, src);
    }

    static char_type *str_n_cat(char_type *dest, char_type const *src, size_type cch)
    {
        return ::wcsncat(dest, src, cch);
    }

    static int_type str_compare(char_type const *s1, char_type const *s2)
    {
        return ::lstrcmpW(s1, s2);
    }

    static int_type str_compare_no_case(char_type const *s1, char_type const *s2)
    {
        return ::lstrcmpiW(s1, s2);
    }

    static int_type str_n_compare(char_type const *s1, char_type const *s2, size_type cch)
    {
        return ::wcsncmp(s1, s2, cch);
    }

    static size_type str_len(char_type const *src)
    {
        return static_cast<size_type>(::lstrlenW(src));
    }

    static char_type *str_chr(char_type const *s, char_type ch)
    {
        return const_cast<char_type*>(::wcschr(s, ch));
    }

    static char_type *str_rchr(char_type const *s, char_type ch)
    {
        return const_cast<char_type*>(::wcsrchr(s, ch));
    }

    static char_type *str_str(char_type const *s, char_type const *sub)
    {
        return const_cast<char_type*>(::wcsstr(s, sub));
    }

    static char_type *str_pbrk(char_type const *s, char_type const *charSet)
    {
        return const_cast<char_type*>(::wcspbrk(s, charSet));
    }

    static char_type *str_end(char_type const *s)
    {
        WINSTL_ASSERT(NULL != s);

        for(; *s != L'\0'; ++s)
        {}

        return const_cast<char_type*>(s);
    }

    // Locale management
    static int_type get_locale_info(LCID locale, LCTYPE type, char_type *data, int_type cchData)
    {
        return ::GetLocaleInfoW(locale, type, data, cchData);
    }

    // File-system entry names
    static char_type *ensure_dir_end(char_type *dir)
    {
        WINSTL_ASSERT(NULL != dir);

        char_type   *end    =   str_end(dir);

        if( dir < end &&
            !is_path_name_separator(*(end - 1)))
        {
            *end        =   path_name_separator();
            *(end + 1)  =   L'\0';
        }

        return dir;
    }

    static char_type *remove_dir_end(char_type *dir)
    {
        WINSTL_ASSERT(NULL != dir);

        char_type   *end    =   str_end(dir);

        if( isalpha(dir[0]) &&
            L':' == dir[1] &&
            is_path_name_separator(dir[2]) &&

⌨️ 快捷键说明

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