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

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。

posted @ 2024-06-30 23:53  陌尘(MoChen)  阅读(1)  评论(0编辑  收藏  举报