1. 关键词
关键词:
C++ 文件路径处理 文件夹 普通文件 软连接 跨平台
应用场景:
根据指定的目录路径,判断该目录的文件类型(如:文件夹|普通文件|软连接等)
2. filetype.h
#include <vector>
#include <string>
#pragma once
namespace cutl
{
constexpr int MAX_PATH_LEN = 1024 ;
enum filetype
{
unknown = 0x00 ,
directory = 0x01 ,
file = 0x02 ,
symlink = 0x04 ,
char_special = 0x08 ,
block_special = 0x10 ,
pipefifo = 0x20 ,
socket = 0x40 ,
all = 0xFF ,
};
}
3. filesystem.h
#pragma once
#include <string>
#include <iostream>
#include <cstdio>
#include "filetype.h"
namespace cutl
{
filetype get_file_type (const std ::string &filepath) ;
}
4. filesystem_unix.cpp
#if defined(_WIN32) || defined(__WIN32__)
#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);
}
}
#endif
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;
}
else if (attributes & FILE_ATTRIBUTE_NORMAL || attributes & FILE_ATTRIBUTE_READONLY)
{
type = filetype::file;
}
else if ((attributes & FILE_ATTRIBUTE_ARCHIVE) && extension == ".lnk" )
{
type = filetype::symlink;
}
}
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;
}
filetype get_file_type (const std ::string &filepath)
{
auto attributes = GetFileAttributesA(filepath.c_str());
auto extension = get_file_extension(filepath);
return get_file_type(attributes, extension);
}
}
#endif
6. filepath.h
#pragma once
#include <string>
#include <iostream>
#include <cstdio>
#include "filetype.h"
namespace cutl
{
class filepath
{
public:
filepath(const std ::string &path);
filepath(const filepath &other);
filepath &operator=(const filepath &other);
~filepath() = default ;
public:
filetype type () const ;
bool isfile () const ;
bool isdir () const ;
private:
std ::string filepath_;
};
std ::ostream &operator<<(std ::ostream &os, const filepath &fp);
filepath path (const std ::string &path) ;
}
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
{
}
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);
}
}
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。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战