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

📄 filesystem_traits.hpp

📁 新版本TR1的stl
💻 HPP
📖 第 1 页 / 共 3 页
字号:
    {
        return 0 == ::chdir(dir);
    }

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

    static size_type get_current_directory(char_type *buffer, size_type cchBuffer)
    {
        char_type const* dir = ::getcwd(buffer, cchBuffer);

        return (NULL == dir) ? 0 : str_len(dir);
    }

    static bool_type file_exists(char_type const* fileName)
    {
        stat_data_type  sd;

        return class_type::stat(fileName, &sd) /* || errno != ENOENT */;
    }

    static bool_type is_file(char_type const* path)
    {
        stat_data_type  sd;

        return class_type::stat(path, &sd) && S_IFREG == (sd.st_mode & S_IFMT);
    }

    static bool_type is_directory(char_type const* path)
    {
        stat_data_type  sd;

        return class_type::stat(path, &sd) && S_IFDIR == (sd.st_mode & S_IFMT);
    }

    static bool_type stat(char_type const* path, stat_data_type *stat_data)
    {
        UNIXSTL_ASSERT(NULL != path);
        UNIXSTL_ASSERT(NULL != stat_data);

#ifdef _WIN32
        if(NULL != class_type::str_pbrk(path, "*?"))
        {
            // Sometimes the VC6 CRT libs crash with a wildcard stat
            set_last_error(EBADF);

            return false;
        }

        if(has_dir_end(path))
        {
            // Win32 impl does not like a trailing slash
            size_type   len =   str_len(path);

            if( len > 3 ||
                (   is_path_name_separator(*path) &&
                    len > 2))
            {
                buffer_type_    directory(1 + len);

                if(0 == directory.size())
                {
                    set_last_error(ENOMEM);

                    return false;
                }
                else
                {
                    str_copy(&directory[0], path);

                    class_type::remove_dir_end(&directory[0]);

                    return class_type::stat(directory.data(), stat_data);
                }
            }
        }
#endif /* _WIN32 */

        return 0 == ::stat(path, stat_data);
    }

    static bool_type lstat(char_type const* path, stat_data_type *stat_data)
    {
        UNIXSTL_ASSERT(NULL != path);
        UNIXSTL_ASSERT(NULL != stat_data);

#ifdef _WIN32
        return 0 == class_type::stat(path, stat_data);
#else /* ? _WIN32 */
        return 0 == ::lstat(path, stat_data);
#endif /* _WIN32 */
    }

    static bool_type fstat(file_handle_type fd, fstat_data_type *fstat_data)
    {
        UNIXSTL_ASSERT(-1 != fd);
        UNIXSTL_ASSERT(NULL != fstat_data);

        return 0 == ::fstat(fd, fstat_data);
    }

    static bool_type is_file(stat_data_type const* stat_data)
    {
        return S_IFREG == (stat_data->st_mode & S_IFREG);
    }
    static bool_type is_directory(stat_data_type const* stat_data)
    {
        return S_IFDIR == (stat_data->st_mode & S_IFDIR);
    }
    static bool_type is_link(stat_data_type const* stat_data)
    {
#ifdef _WIN32
        STLSOFT_SUPPRESS_UNUSED(stat_data);

        return false;
#else /* ? _WIN32 */
        return S_IFLNK == (stat_data->st_mode & S_IFLNK);
#endif /* _WIN32 */
    }
    static bool_type is_readonly(stat_data_type const* stat_data)
    {
#ifdef _WIN32
        return S_IREAD == (stat_data->st_mode & (S_IREAD | S_IWRITE));
#else /* ? _WIN32 */
        return S_IRUSR == (stat_data->st_mode & (S_IRUSR | S_IWUSR));
#endif /* _WIN32 */
    }

    static bool_type    create_directory(char_type const* dir)
    {
        mode_type   mode = 0;

#ifdef _WIN32
        mode    =   S_IREAD | S_IWRITE | S_IEXEC;
#else /* ? _WIN32 */
        mode    =   S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
#endif /* _WIN32 */

        return create_directory(dir, 0);
    }

    static bool_type create_directory(char_type const* dir, mode_type permissions)
    {
#if defined(_WIN32) && \
    (   defined(STLSOFT_COMPILER_IS_INTEL) || \
        defined(STLSOFT_COMPILER_IS_MSVC))
        return 0 == ::mkdir(dir);
#else /* ? _WIN32 */
        return 0 == ::mkdir(dir, permissions);
#endif /* _WIN32 */
    }

    static bool_type remove_directory(char_type const* dir)
    {
        return 0 == ::rmdir(dir);
    }

    static bool_type unlink_file(char_type const* file)
    {
        return 0 == ::remove(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 0 == ::rename(currentName, newName);
    }

    static file_handle_type open_file(char_type const* fileName, int oflag, int pmode)
    {
        return ::open(fileName, oflag, pmode);
    }

    static bool_type close_file(file_handle_type fd)
    {
        return 0 == ::close(fd);
    }

    static file_handle_type open(char_type const* fileName, int oflag, int pmode)
    {
        return class_type::open_file(fileName, oflag, pmode);
    }

    static bool_type close(file_handle_type fd)
    {
        return class_type::close_file(fd);
    }

#ifdef STLSOFT_CF_64BIT_INT_SUPPORT
    static us_uint64_t get_file_size(file_handle_type fd)
    {
        struct stat st;

        return class_type::fstat(fd, &st) ? st.st_size : 0;
    }
#endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
};

STLSOFT_TEMPLATE_SPECIALISATION
struct filesystem_traits<us_char_w_t>
    : public system_traits<us_char_w_t>
{
public:
    typedef us_char_w_t char_type;
    typedef us_size_t   size_type;

#ifdef PATH_MAX
    enum
    {
        maxPathLength   =   1 + PATH_MAX    //!< The maximum length of a path for the current file system
    };
#endif /* PATH_MAX */

public:
    static char_type *ensure_dir_end(char_type *dir)
    {
        UNIXSTL_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)
    {
        UNIXSTL_ASSERT(NULL != dir);

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

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

        char_type   *end    =   str_end(dir);

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

        return dir;
    }

    static bool_type has_dir_end(char_type const* dir)
    {
        UNIXSTL_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)
    {
        UNIXSTL_ASSERT(NULL != dir);

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

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

#ifdef _WIN32
        // If it's really on Windows, then we need to skip the drive, if present
        if( isalpha(path[0]) &&
            ':' == path[1])
        {
            path += 2;
        }

        // If it's really on Windows, then we need to account for the fact that
        // the slash might be backwards
        if('\\' == *path)
        {
            return true;
        }
#endif /* _WIN32 */

        return '/' == *path;
    }

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

#ifdef _WIN32
        // If it's really on Windows, then it can only be absolute if ...
        //
        // ... it's a UNC path, or ...
        if(is_path_UNC(path))
        {
            return true;
        }
        // ... it's got drive + root slash, or
        if( isalpha(path[0]) &&
            ':' == path[1] &&
            is_path_name_separator(path[2]))
        {
            return true;
        }
        // ... it's got root forward slash
        if('/' == path[0])
        {
            return true;
        }

        return false;
#else /* ? _WIN32 */
        return is_path_rooted(path);
#endif /* _WIN32 */
    }

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

#ifdef _WIN32
        return (L'\\' == path[0] && L'\\' == path[1]);
#else /* ? _WIN32 */
        STLSOFT_SUPPRESS_UNUSED(path);

        return false;
#endif /* _WIN32 */
    }

    static bool_type is_path_name_separator(char_type ch)
    {
#ifdef _WIN32
        return L'\\' == ch || L'/' == ch;
#else /* ? _WIN32 */
        return L'/' == ch;
#endif /* _WIN32 */
    }

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

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

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

    static size_type path_max()
    {
#if defined(PATH_MAX)
        return 1 + PATH_MAX;
#else /* ? PATH_MAX */
        return 1 + pathconf("/", _PC_PATH_MAX);
#endif /* PATH_MAX */
    }

    static size_type get_full_path_name(char_type const* fileName, size_type cchBuffer, char_type* buffer, char_type **ppFile);

    static size_type get_full_path_name(char_type const* fileName, char_type* buffer, size_type cchBuffer)
    {
        char_type *pFile;

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

    static size_type get_full_path_name(char_type const* fileName, size_type cchBuffer, char_type* buffer)
    {
        return get_full_path_name(fileName, buffer, cchBuffer);
    }

    // File-system state
    static bool_type set_current_directory(char_type const* dir);

    static size_type get_current_directory(size_type cchBuffer, char_type* buffer);

    static size_type get_current_directory(char_type *buffer, size_type cchBuffer);
};

#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

/* /////////////////////////////////////////////////////////////////////////
 * Unit-testing
 */

#ifdef STLSOFT_UNITTEST
# include "./unittest/filesystem_traits_unittest_.h"
#endif /* STLSOFT_UNITTEST */

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

#ifndef _UNIXSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
     defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace unixstl
# else
} // namespace unixstl_project
} // namespace stlsoft
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_UNIXSTL_NO_NAMESPACE */

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

#endif /* UNIXSTL_INCL_UNIXSTL_FILESYSTEM_HPP_FILESYSTEM_TRAITS */

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

⌨️ 快捷键说明

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