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

📄 filesystem_traits.hpp

📁 用STL的方式封装了WindowsAPI、COM调用、ACE、ATL、MFC、WTL等多种组件
💻 HPP
📖 第 1 页 / 共 5 页
字号:
    /// \brief Returns whether the given stat info represents a directory
    static bool_type    is_directory(stat_data_type const *stat_data);
    static bool_type    is_directory(fstat_data_type const *stat_data);
    /// \brief Returns whether the given stat info represents a link
    static bool_type    is_link(stat_data_type const *stat_data);
    static bool_type    is_link(fstat_data_type const *stat_data);
    /// \brief Returns whether the given stat info represents a read-only entry
    static bool_type    is_readonly(stat_data_type const *stat_data);
    static bool_type    is_readonly(fstat_data_type const *stat_data);

    /// \brief Indicates whether the given drive currently exists on the system
    static bool_type    drive_exists(char_type driveLetter);
    /// \brief Returns a status code denoting the type of the drive
    ///
    /// \return One of the return codes of the GetDriveType() API function
    static DWORD        get_drive_type(char_type driveLetter);
/// @}

/// \name File-system control
/// @{
public:
    /// \brief Creates a directory
    static bool_type    create_directory(char_type const *dir);
    /// \brief Creates a directory, with the given security attributes
    static bool_type    create_directory(char_type const *dir, LPSECURITY_ATTRIBUTES lpsa);
    /// \brief Deletes a directory
    static bool_type    remove_directory(char_type const *dir);

    /// \brief Delete a file
    static bool_type    unlink_file(char_type const *file);
    /// \brief Delete a file
    ///
    /// \deprecated Users should use unlink_file()
    static bool_type    delete_file(char_type const *file);
    /// \brief Rename a file
    static bool_type    rename_file(char_type const *currentName, char_type const *newName);
    /// \brief Copy a file
    static bool_type    copy_file(char_type const *sourceName, char_type const *newName, bool_type bFailIfExists = false);

    /// \brief Create / open a file
    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, HANDLE hTemplateFile);
    /// \brief Closes the given operating system handle
    static bool_type        close_handle(HANDLE h);
    /// \brief Closes the given file handle
    static bool_type        close_file(file_handle_type h);
#ifdef STLSOFT_CF_64BIT_INT_SUPPORT
    /// \brief Gets the size of the file
    static ws_uint64_t      get_file_size(file_handle_type h);
#endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
/// @}

/// \name Error
/// @{
public:
    /// \brief Gives the last error
    static error_type   get_last_error();
    /// \brief Sets the last error
    static void         set_last_error(error_type er = error_type());
/// @}

/// \name Environment
/// @{
public:
    /// \brief Gets an environment variable into the given buffer
    ///
    /// \param name The name of the variable to find
    /// \param buffer The buffer in which to write the variable. If this is NULL, then the required length is returned
    /// \param cchBuffer The size of the buffer, in characters
    static size_type    get_environment_variable(char_type const *name, char_type *buffer, size_type cchBuffer);
    /// Expands environment strings in \c src into \c buffer, up to a maximum \c cchDest characters
    static size_type    expand_environment_strings(char_type const *src, char_type *buffer, size_type cchBuffer);
/// @}
};

#else /* ? STLSOFT_DOCUMENTATION_SKIP_SECTION */

template <ss_typename_param_k C>
struct filesystem_traits;

struct filesystem_traits_
{
public:
    typedef ws_size_t                   size_type;
    typedef ws_ptrdiff_t                difference_type;
    typedef filesystem_traits_          class_type;
    typedef BY_HANDLE_FILE_INFORMATION  fstat_data_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;

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

public:
    static bool_type close_handle(HANDLE h)
    {
        return FALSE != ::CloseHandle(h);
    }

    static bool_type fstat(file_handle_type fd, fstat_data_type *fstat_data)
    {
        return FALSE != ::GetFileInformationByHandle(fd, fstat_data);
    }

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

    static bool_type free_library(module_type hModule)
    {
        return FALSE != ::FreeLibrary(hModule);
    }

    static FARPROC find_symbol(module_type hModule, char const *symbolName)
    {
        return ::GetProcAddress(hModule, symbolName);
    }


#ifdef STLSOFT_CF_64BIT_INT_SUPPORT
    static ws_uint64_t  get_file_size(file_handle_type h)
    {
        DWORD   dwHigh;
        DWORD   dwLow   =   ::GetFileSize(h, &dwHigh);

        if( 0xFFFFFFFF == dwLow &&
            ERROR_SUCCESS != ::GetLastError())
        {
            dwHigh = 0xFFFFFFFF;
        }

        return (static_cast<ws_uint64_t>(dwHigh) << 32) | dwLow;
    }
#endif /* STLSOFT_CF_64BIT_INT_SUPPORT */

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

    static void set_last_error(error_type er)
    {
        ::SetLastError(er);
    }
};

STLSOFT_TEMPLATE_SPECIALISATION
struct filesystem_traits<ws_char_a_t>
{
public:
    typedef ws_char_a_t                             char_type;
    typedef ws_size_t                               size_type;
    typedef ws_ptrdiff_t                            difference_type;
    typedef WIN32_FIND_DATAA                        find_data_type;
    typedef WIN32_FIND_DATAA                        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 ::lstrcpyA(dest, src);
    }

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

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

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

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

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

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

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

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

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

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

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

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

        for(; *s != '\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 GetLocaleInfoA(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)  =   '\0';
        }

        return dir;
    }

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

        char_type   *end    =   str_end(dir);

        // Don't trim drive roots ...
        if( isalpha(dir[0]) &&
            ':' == dir[1] &&
            is_path_name_separator(dir[2]) &&
            '\0' == dir[3])
        {
            return dir;
        }

        // ... or UNC roots
        if( '\\' == dir[0] &&
            '\\' == dir[1] &&
            '\0' == dir[3])
        {
            return dir;
        }

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

        return dir;
    }

    static bool_type has_dir_end(char_type const *dir)
    {
        WINSTL_ASSERT(NULL != dir);

        size_type len = str_len(dir);

        return (0 < len) && is_path_name_separator(dir[len - 1]);
    }

    static bool_type is_dots(char_type const *dir)
    {
        WINSTL_ASSERT(NULL != dir);

        return  dir[0] == '.' &&
                (   dir[1] == '\0' ||
                    (    dir[1] == '.' &&
                        dir[2] == '\0'));
    }

    static bool_type is_path_rooted(char_type const *path)
    {
        WINSTL_ASSERT(NULL != path);

        return is_path_name_separator(*path) || is_path_absolute(path);
    }

    static bool_type is_path_absolute(char_type const *path)
    {
        WINSTL_ASSERT(NULL != path);

        size_type len = str_len(path);

        return  is_path_UNC(path) ||
                (   (2 < len) &&
                    (':' == path[1]) &&
                    is_path_name_separator(path[2]));
    }

    static bool_type is_path_UNC(char_type const *path)
    {
        WINSTL_ASSERT(NULL != path);

        return ('\\' == path[0] && '\\' == path[1]);
    }

private:
    static bool_type is_root_drive_(char_type const *path)
    {
        if( isalpha(path[0]) &&
            ':' == path[1] &&
            is_path_name_separator(path[2]) &&
            '\0' == path[3])
        {
            return true;
        }

        return false;
    }
    static bool_type is_root_UNC_(char_type const *path)
    {
        if(is_path_UNC(path))
        {
            char_type const *sep    =   str_pbrk(path + 2, "\\/");

            if( NULL == sep ||
                '\0' == sep[1])
            {
                return true;
            }
        }

        return false;
    }
    static bool_type is_root_directory_(char_type const *path)

⌨️ 快捷键说明

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