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

📄 filesystem_traits.hpp

📁 新版本TR1的stl
💻 HPP
📖 第 1 页 / 共 3 页
字号:
    }

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

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

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

    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));
    }

    // File-system entry names
    static char_type *ensure_dir_end(char_type *dir)
    {
        char_type       *end;
        char_type const separator = (NULL == wcschr(dir, L'/') && NULL != wcschr(dir, L'\\')) ? L'\\' : L'/';

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

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

        return dir;
    }

    static char_type *remove_dir_end(char_type *dir)
    {
        char_type   *end;

        for(end = dir; *end != '\0'; ++end)
        {}

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

        return dir;
    }

    static bool_type has_dir_end(char_type const* dir)
    {
        is_size_t len = str_len(dir);

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

    static bool_type is_dots(char_type const* dir)
    {
        return  dir != 0 &&
                dir[0] == '.' &&
                (   dir[1] == L'\0' ||
                    (    dir[1] == L'.' &&
                        dir[2] == L'\0'));
    }

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

        return L'/' == *path;
    }

    static bool_type is_path_absolute(char_type const* path)
    {
        return is_path_rooted(path);
    }

    static char_type path_separator()
    {
        return L';';
    }

    static char_type path_name_separator()
    {
        return L'/';
    }

    static char_type const* pattern_all()
    {
        return L"*";
    }

    static is_dword_t get_full_path_name(HINTERNET hconn, char_type const* fileName, is_dword_t cchBuffer, char_type* buffer, char_type **ppFile)
    {
        INETSTL_ASSERT(0 == cchBuffer || NULL != buffer);
        INETSTL_ASSERT(NULL == buffer || 0 != cchBuffer);

        // Deduce the separator
        char_type const separator = (NULL == wcschr(fileName, L'/') && NULL != wcschr(fileName, L'\\')) ? L'\\' : L'/';
        char_type       fullPath[1 + _MAX_PATH];

        // If we're not rooted, then get the current directory and concatenate
        if(separator != *fileName)
        {
            is_size_t   cchBuffer   =   STLSOFT_NUM_ELEMENTS(fullPath);
            int         isDot       =   0 == str_compare(fileName, L".");

#ifdef __SYNSOFT_DBS_COMPILER_SUPPORTS_PRAGMA_MESSAGE
# pragma message(_sscomp_fileline_message("This looks a bit dodgy. Better to use an auto_buffer, and cycle the size, testing the return value from get_current_directory"))
#endif /* __SYNSOFT_DBS_COMPILER_SUPPORTS_PRAGMA_MESSAGE */

            get_current_directory(hconn, cchBuffer, fullPath);
            if(!isDot)
            {
                ensure_dir_end(fullPath);
                str_cat(fullPath, fileName);
            }

            fileName = fullPath;
        }

        size_type       len =   str_len(fileName);

        if(NULL != buffer)
        {
            if(cchBuffer < len)
            {
                len = cchBuffer;
            }

            ::wcsncpy(buffer, fileName, cchBuffer);

            if(NULL != ppFile)
            {
                char_type const* pRSlash        =   wcsrchr(buffer, L'/');
                char_type const* pRBackSlash    =   wcsrchr(buffer, L'\\');

                if(pRSlash < pRBackSlash)
                {
                    pRSlash = pRBackSlash;
                }

                if(NULL == pRSlash)
                {
                    *ppFile = NULL;
                }
                else
                {
                    *ppFile = const_cast<char_type*>(pRSlash) + 1;
                }
            }
        }

        return len;
    }

    static is_dword_t get_full_path_name(HINTERNET hconn, char_type const* fileName, is_dword_t cchBuffer, char_type* buffer)
    {
        char_type *pFile;

        return get_full_path_name(hconn, fileName, cchBuffer, buffer, &pFile);
    }

    // Internet connectivity
    static HINTERNET internet_open(char_type const* agent, is_dword_t accessType, char_type const* proxy, char_type const* proxyBypass, is_dword_t flags)
    {
        return ::InternetOpenW(agent, accessType, proxy, proxyBypass, flags);
    }

    static HINTERNET internet_connect(HINTERNET hsess, char_type const* server, INTERNET_PORT port, char_type const* userName, char_type const* password, is_dword_t service, is_dword_t flags, is_dword_t context)
    {
        return ::InternetConnectW(hsess, server, port, userName, password, service, flags, context);
    }

    static void close_connection(HINTERNET hconn)
    {
        INETSTL_ASSERT(NULL != hconn);

        ::InternetCloseHandle(hconn);
    }

    // FindFile() API
    static HINTERNET find_first_file(HINTERNET hconn, char_type const* spec, find_data_type *findData, is_dword_t flags = 0, is_dword_t context = 0)
    {
        return ::FtpFindFirstFileW(hconn, spec, stlsoft_ns_qual(any_caster)<find_data_type*, LPWIN32_FIND_DATAA, LPWIN32_FIND_DATAW>(findData), flags, context);
    }

    static bool_type find_next_file(HANDLE h, find_data_type *findData)
    {
        return FALSE != ::InternetFindNextFileW(h, findData);
    }

    static void find_close(HINTERNET hfind)
    {
        INETSTL_ASSERT(NULL != hfind);

        ::InternetCloseHandle(hfind);
    }

    // File system state
    static bool_type set_current_directory(HINTERNET hconn, char_type const* dir)
    {
        return ::FtpSetCurrentDirectoryW(hconn, dir) != FALSE;
    }

    static bool_type get_current_directory(HINTERNET hconn, is_size_t &cchBuffer, char_type* buffer)
    {
        return FALSE != ::FtpGetCurrentDirectoryW(hconn, buffer, sap_cast<unsigned long*>(&cchBuffer));
    }

    /// Returns whether a file exists or not
    static bool_type file_exists(HINTERNET hconn, char_type const* fileName)
    {
        find_data_type  data;
        HINTERNET       hfind = find_first_file(hconn, fileName, &data);

        return (NULL == hfind) ? false : (find_close(hfind), true);
    }

#if 0
    static bool_type    is_file(HINTERNET hconn, char_type const* path);
    static bool_type    is_directory(HINTERNET hconn, char_type const* path);
    static bool_type    stat(HINTERNET hconn, char_type const* path, stat_data_type *stat_data);
#endif /* 0 */

    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);
    static bool_type    is_readonly(stat_data_type const* stat_data)
    {
        return FILE_ATTRIBUTE_READONLY == (stat_data->dwFileAttributes & FILE_ATTRIBUTE_READONLY);
    }

    // File system control
    static bool_type create_directory(HINTERNET hconn, char_type const* dir)
    {
        return FALSE != ::FtpCreateDirectoryW(hconn, dir);
    }

    static bool_type remove_directory(HINTERNET hconn, char_type const* dir)
    {
        return FALSE != ::FtpRemoveDirectoryW(hconn, dir);
    }

    static bool_type delete_file(HINTERNET hconn, char_type const* file)
    {
        return FALSE != ::FtpDeleteFileW(hconn, file);
    }

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

#endif /* STLSOFT_DOCUMENTATION_SKIP_SECTION */

/* ////////////////////////////////////////////////////////////////////// */

#ifndef _INETSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
     defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace inetstl
# else
} // namespace inetstl_project
} // namespace stlsoft
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_INETSTL_NO_NAMESPACE */

/* ////////////////////////////////////////////////////////////////////// */

#endif /* INETSTL_INCL_INETSTL_FILESYSTEM_HPP_FILESYSTEM_TRAITS */

/* ////////////////////////////////////////////////////////////////////// */

⌨️ 快捷键说明

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