首页 > 编程语言 >C++文件路径处理3 - 判断指定目录的文件类型(文件夹|普通文件|软连接)

C++文件路径处理3 - 判断指定目录的文件类型(文件夹|普通文件|软连接)

时间:2024-06-30 23:53:03浏览次数:1  
标签:std 文件 filepath filetype C++ file 文件类型 include type

1. 关键词

关键词:

C++ 文件路径处理 文件夹 普通文件 软连接 跨平台

应用场景:

根据指定的目录路径,判断该目录的文件类型(如:文件夹|普通文件|软连接等)

2. filetype.h


#include <vector>
#include <string>

#pragma once

namespace cutl
{

    /**
     * @brief Constants value: max path length.
     *
     */
    constexpr int MAX_PATH_LEN = 1024;

    /**
     * @brief The type of file.
     *
     */
    enum filetype
    {
        /** undefined */
        unknown = 0x00,
        /** directory */
        directory = 0x01,
        /** regular file */
        file = 0x02,
        /** symbolic link */
        symlink = 0x04,
        /** character device, only for unix */
        char_special = 0x08,
        /** block device, only for unix */
        block_special = 0x10,
        /** named pipe, only for unix */
        pipefifo = 0x20,
        /** socket file, only for unix */
        socket = 0x40,
        /** type mask, includes all types */
        all = 0xFF,
    };

} // namespace cutl

3. filesystem.h


#pragma once

#include <string>
#include <iostream>
#include <cstdio>
#include "filetype.h"

namespace cutl
{
    // 获取文件类型
    filetype get_file_type(const std::string &filepath);

} // namespace cutl

4. filesystem_unix.cpp

#if defined(_WIN32) || defined(__WIN32__)
// do nothing
#else

#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stack>
#include <cstring>
#include <utime.h>
#include <stdlib.h>
#include <sys/time.h>
#include "filesystem.h"
#include "inner/logger.h"

namespace cutl
{

    filetype get_file_type(int mode)
    {
        filetype type = filetype::unknown;
        if (S_ISBLK(mode))
        {
            type = filetype::block_special;
        }
        else if (S_ISCHR(mode))
        {
            type = filetype::char_special;
        }
        else if (S_ISDIR(mode))
        {
            type = filetype::directory;
        }
        else if (S_ISFIFO(mode))
        {
            type = filetype::pipefifo;
        }

        else if (S_ISLNK(mode))
        {
            type = filetype::symlink;
        }
        else if (S_ISREG(mode))
        {
            type = filetype::file;
        }
        else if (S_ISSOCK(mode))
        {
            type = filetype::socket;
        }
        return type;
    }

    filetype get_file_type(const std::string &filepath)
    {
        struct stat file_stat;
        int ret = lstat(filepath.c_str(), &file_stat);
        if (0 != ret)
        {
            CUTL_ERROR("stat error. filepath:" + filepath + ", error:" + strerror(errno));
            return filetype::unknown;
        }

        return get_file_type(file_stat.st_mode);
    }

} // namespace cutl

#endif // defined(_WIN32) || defined(__WIN32__)

5. filesystem_win.cpp

#if defined(_WIN32) || defined(__WIN32__)

#include <io.h>
#include <direct.h>
#include <Windows.h>
#include <stdlib.h>
#include "strutil.h"
#include "filesystem.h"
#include "logger.h"

namespace cutl
{
    filetype get_file_type(DWORD attributes, const std::string &extension)
    {
        filetype type = filetype::unknown;

        if (attributes == INVALID_FILE_ATTRIBUTES)
        {
            CUTL_WARN("Failed to get file attributes, error code: " + std::to_string(GetLastError()));
            if (extension == ".lnk")
            {
                // 注意:测试时发现,有些快捷方式访问会失败,用后缀名判断进行兜底
                type = filetype::symlink;
            }
            return type;
        }
        else
        {
            if (attributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                type = filetype::directory; // directory
            }
            else if (attributes & FILE_ATTRIBUTE_NORMAL || attributes & FILE_ATTRIBUTE_READONLY)
            {
                // 普通文件|只读文件
                type = filetype::file; // regular file
            }
            else if ((attributes & FILE_ATTRIBUTE_ARCHIVE) && extension == ".lnk")
            {
                // windows的快捷方式
                type = filetype::symlink; // symbolic link
            }
        }

        return type;
    }

    std::string get_file_extension(const std::string &filepath)
    {
        auto pos = filepath.find_last_of('.');
        std::string extension = "";
        if (pos != std::string::npos)
        {
            extension = filepath.substr(pos);
            extension = cutl::to_lower(extension);
        }
        return extension;
    }

    // https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfileattributesa
    // https://learn.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants
    filetype get_file_type(const std::string &filepath)
    {
        auto attributes = GetFileAttributesA(filepath.c_str());
        auto extension = get_file_extension(filepath);
        // CUTL_DEBUG(filepath + ", extension: " + extension + ", attributes: " + std::to_string(attributes));
        return get_file_type(attributes, extension);
    }
} // namespace cutl

#endif // defined(_WIN32) || defined(__WIN32__)

6. filepath.h


#pragma once

#include <string>
#include <iostream>
#include <cstdio>
#include "filetype.h"

namespace cutl
{

    /**
     * @brief The class for file path operations.
     *
     */
    class filepath
    {
    public:
        /**
         * @brief Construct a new filepath object
         *
         * @param path file path string
         */
        filepath(const std::string &path);

        /**
         * @brief Construct a new filepath object by copy
         *
         * @param other other filepath object
         */
        filepath(const filepath &other);

        /**
         * @brief Assign operator, assign a new filepath object by copy
         *
         * @param other other filepath object
         * @return filepath& the reference of the current filepath object
         */
        filepath &operator=(const filepath &other);

        /**
         * @brief Destroy the filepath object
         *
         */
        ~filepath() = default;

    public:
        
        /**
         * @brief Get the file type of the filepath.
         *
         * @return file type
         */
        filetype type() const;

        /**
         * @brief Check if the filepath is a regular file.
         *
         * @return true mena regular file, false means not regular file.
         */
        bool isfile() const;

        /**
         * @brief Check if the filepath is a directory.
         *
         * @return true means directory, false means not directory.
         */
        bool isdir() const;
    private:
        std::string filepath_;
    };

    /**
     * @brief Define the output stream operator for filepath object.
     *
     * @param os the std::ostream object
     * @param fp the filepath object to be output
     * @return std::ostream& the reference of the std::ostream object after outputing the filepath object.
     */
    std::ostream &operator<<(std::ostream &os, const filepath &fp);

    /**
     * @brief Create a filepath object from a string.
     *
     * @param path file path string
     * @return filepath object
     */
    filepath path(const std::string &path);

} // namespace cutl

7. filepath.cpp


#include "filepath.h"
#include "inner/logger.h"
#include "inner/filesystem.h"
#include "strutil.h"
#include "sysutil.h"

namespace cutl
{
    static constexpr char win_separator = '\\';
    static constexpr char unix_separator = '/';

    void fixpath(std::string &path)
    {
        if (win_separator == filepath::separator())
        {
            for (size_t i = 0; i < path.size(); i++)
            {
                if (path[i] == unix_separator)
                {
                    path[i] = win_separator;
                }
            }
        }
        else if (unix_separator == filepath::separator())
        {
            for (size_t i = 0; i < path.size(); i++)
            {
                if (path[i] == win_separator)
                {
                    path[i] = unix_separator;
                }
            }
        }
        else
        {
            // do nothing
        }

        while (path.empty() || path.back() == filepath::separator())
        {
            path.pop_back();
        }
    }

    filepath::filepath(const std::string &path)
    {
        filepath_ = path;
        fixpath(filepath_);
    }

    filepath::filepath(const filepath &other)
    {
        filepath_ = other.filepath_;
    }

    filepath &filepath::operator=(const filepath &other)
    {
        this->filepath_ = other.filepath_;
        return *this;
    }

    char filepath::separator()
    {
#if defined(_WIN32) || defined(__WIN32__)
        return win_separator;
#else
        return unix_separator;
#endif
    }

    std::string filepath::str() const
    {
        return filepath_;
    }

    filepath filepath::join(const std::string &filename) const
    {
        std::string path = filepath_ + separator() + filename;
        return filepath(path);
    }

    filetype filepath::type() const
    {
        return get_file_type(filepath_);
    }

    bool filepath::isfile() const
    {
        return type() == filetype::file;
    }

    bool filepath::isdir() const
    {
        return type() == filetype::directory;
    }

    std::ostream &operator<<(std::ostream &os, const filepath &fp)
    {
        os << fp.str();
        return os;
    }

    filepath path(const std::string &path)
    {
        return filepath(path);
    }
} // namespace cutl

8. 测试代码

#include "common.hpp"
#include "fileutil.h"

void TestFileType()
{
    PrintSubTitle("TestFileType");

    auto path1 = cutl::path("./fileutil_test/dir1");
    std::cout << "path1: " << path1 << ", type: " << path1.type() << ", " << filetype_flag(path1.type()) << std::endl;
    auto path2 = cutl::path("./fileutil_test/file3.txt");
    std::cout << "path2: " << path2 << ", type: " << path2.type() << ", " << filetype_flag(path2.type()) << std::endl;
    auto path3 = cutl::path("./fileutil_test/file4.data");
    std::cout << "path3: " << path3 << ", type: " << path3.type() << ", " << filetype_flag(path3.type()) << std::endl;
    auto path4 = cutl::path(R"(C:\Users\Public\Desktop\CMake-gui.lnk)");
    std::cout << "path4: " << path4 << ", type: " << path4.type() << ", " << filetype_flag(path4.type()) << std::endl;
    auto path5 = cutl::path(R"(C:\Users\vboxuser\Desktop\VisualStudio2015)");
    std::cout << "path5: " << path5 << ", type: " << path5.type() << ", " << filetype_flag(path5.type()) << std::endl;
    auto path6 = cutl::path(R"(C:\Users\Public\Desktop\VisualStudio2015.lnk)");
    std::cout << "path6: " << path6 << ", type: " << path6.type() << ", " << filetype_flag(path6.type()) << std::endl;
    auto path7 = cutl::path(R"(C:\Users\vboxuser\Desktop\VisualStudio2015.lnk)");
    std::cout << "path7: " << path7 << ", type: " << path6.type() << ", " << filetype_flag(path7.type()) << std::endl;
}

9. 运行结果

--------------------------------------------TestFileType--------------------------------------------
path1: ./fileutil_test/dir1, type: 1, d
path2: ./fileutil_test/file3.txt, type: 2, -
path3: ./fileutil_test/file4.data, type: 2, -
path4: C:/Users/Public/Desktop/CMake-gui.lnk, type: [2024-06-26 12:25:45.877][E]]0x7ff85b8d5fc0](cutl) [filesystem_unix.cpp:298:get_file_type] stat error. filepath:C:/Users/Public/Desktop/CMake-gui.lnk, error:No such file or directory
0, [2024-06-26 12:25:45.877][E]]0x7ff85b8d5fc0](cutl) [filesystem_unix.cpp:298:get_file_type] stat error. filepath:C:/Users/Public/Desktop/CMake-gui.lnk, error:No such file or directory
u
path5: C:/Users/vboxuser/Desktop/VisualStudio2015, type: [2024-06-26 12:25:45.877][E]]0x7ff85b8d5fc0](cutl) [filesystem_unix.cpp:298:get_file_type] stat error. filepath:C:/Users/vboxuser/Desktop/VisualStudio2015, error:No such file or directory
0, [2024-06-26 12:25:45.877][E]]0x7ff85b8d5fc0](cutl) [filesystem_unix.cpp:298:get_file_type] stat error. filepath:C:/Users/vboxuser/Desktop/VisualStudio2015, error:No such file or directory
u
path6: C:/Users/Public/Desktop/VisualStudio2015.lnk, type: [2024-06-26 12:25:45.877][E]]0x7ff85b8d5fc0](cutl) [filesystem_unix.cpp:298:get_file_type] stat error. filepath:C:/Users/Public/Desktop/VisualStudio2015.lnk, error:No such file or directory
0, [2024-06-26 12:25:45.877][E]]0x7ff85b8d5fc0](cutl) [filesystem_unix.cpp:298:get_file_type] stat error. filepath:C:/Users/Public/Desktop/VisualStudio2015.lnk, error:No such file or directory
u
path7: C:/Users/vboxuser/Desktop/VisualStudio2015.lnk, type: [2024-06-26 12:25:45.877][E]]0x7ff85b8d5fc0](cutl) [filesystem_unix.cpp:298:get_file_type] stat error. filepath:C:/Users/Public/Desktop/VisualStudio2015.lnk, error:No such file or directory
0, [2024-06-26 12:25:45.877][E]]0x7ff85b8d5fc0](cutl) [filesystem_unix.cpp:298:get_file_type] stat error. filepath:C:/Users/vboxuser/Desktop/VisualStudio2015.lnk, error:No such file or directory
u

10. 源码地址

更多详细代码,请查看本人写的C++ 通用工具库: common_util, 本项目已开源,代码简洁,且有详细的文档和Demo。

标签:std,文件,filepath,filetype,C++,file,文件类型,include,type
From: https://www.cnblogs.com/luoweifu/p/18277205

相关文章

  • SpringMVC配置文件的位置和命名
    配置web.xml文件时我们需要配置SpringMVC框架的配置文件,该文件有两种配置方式。第一种是默认配置,第二种是带配置参数的配置。1.1默认配置方式SpringMVC框架有它自己的配置文件,该配置文件的名字默认为:-servlet.xml,默认存放的位置是WEB-INF目录下。如果采用这种方式意味着文件的......
  • 文件时间属性
    文件的时间为什么要学习关于文件属性,因为我们的文件,不要认为内容没有发生改变,你的文件就没有被人动过1、有人偷看了你的密码文件2、有人偷偷修改了你的重要文件,肉眼无法观察出来3、有人偷偷修改了你的文件属性,你却还不知道关于文件的属性,有如下三个时间,可以更加清晰的了解你......
  • C++Primer Plus 第十四章代码重用:模板类和友元14.4.9 ----002
    C++PrimerPlus第十四章代码重用:模板类和友元14.4.9提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加例如:模板类和友元14.4.9提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录C++PrimerPlus第十四章代码重用:模板类和友元14.4......
  • C++11新特性
    1.字符串原始字面量        在C++11中添加了定义原始字符串的字面量,定义方式为:R“xxx(原始字符串)xxx”其中()两边的字符串可以省略。原始字面量R可以直接表示字符串的实际含义,而不需要额外对字符串做转义或连接等操作。        比如:编程过程中,使用的字符串中......
  • Java方法递归:File文件搜索
        在Java中,方法递归是一种特殊的情况,其中方法直接或间接地调用自身。为了使用方法递归,方法需要有基本情况,即不再调用自身的条件,以防止进入无限循环。    我们来做一个搜索文件并打开的案例。以打开QQ为例,因为我的电脑只有C盘,我搜索文件的地方,就写C盘。publ......
  • C++11 mem_fn成员指针包装器
    C++11mem_fn成员指针包装器介绍函数模板std::mem_fn生成成员指针的包装器对象,用于存储、复制及调用成员指针。指向对象的引用和指针(包括智能指针)都可以在调用std::mem_fn时使用。注意:std::mem_fn只能包装public的成员指针,不能包装全局函数这里的成员指针指的是成员......
  • C++基础语法——《循环结构》题解
    循环结构参考资料:https://blog.csdn.net/m0_56945138/article/details/118929416需要掌握:1.for循环用法2.while循环用法3.continue跳过和break终止题号题目名称题解链接3067输出范围内的整数https://www.cnblogs.com/jyssh/p/182740551206简单的累加https://www......
  • Linux---文件的权限
    在Linux中的权限采用的是三位十进制的数表示权限,如:0755,0644权限的使用是由四个部分组成,ABCD(为了方便说明,采用这四个字母代表四个部分)A~0:表示十进制B:表示用户的权限C:表示组用户的权限D:表示其他用户的权限—:0,表示不能读,不能写,不可以执行–x:1,表示不能读,不能写,可以执......
  • 文件的基础
    1、定义文件:一组相关数据的有序集合。文件名:这个数据集合的名称。2、文件类型Linux上一切皆文件常规文件:-}ASCII码文件}二进制的文件目录:d字符设备:c块设备:b有名管道:p套接口:s符号链接:l3、系统调用和库函数系统调用用户空间进程访问内核的接口把......
  • 【Redis —— 04 Redis配置文件】
    Redis配置文件(通常为redis.conf)包含多种配置选项,可以调整Redis服务器的行为和性能。以下是Redis配置文件中的常见配置项及其详解:官网:Redisconfiguration|Docs常用配置项1.基本配置bind绑定的IP地址。如果你想让Redis监听多个IP地址,可以用空格分隔多个IP。示例:b......