98 lines
3.9 KiB
C++
98 lines
3.9 KiB
C++
#ifndef CCFiles_H
|
||
#define CCFiles_H
|
||
|
||
#include "CC.h"
|
||
#include <filesystem>
|
||
|
||
// 根据不同的操作系统进行特定的设置
|
||
#ifdef _WIN32
|
||
|
||
#elif __linux__
|
||
|
||
#endif
|
||
|
||
/**
|
||
* 结构体CCFileInfo用于存储文件信息。
|
||
*/
|
||
struct CCFileInfo
|
||
{
|
||
CTL::String Name; // 文件名
|
||
size_t Size{}; // 文件大小
|
||
bool IsDirectory{}; // 是否为目录
|
||
time_t LastWriteTime;
|
||
};
|
||
|
||
// 命名空间别名,用于简化std::filesystem的使用
|
||
namespace CC_FS = std::filesystem;
|
||
// 类型别名,用于简化目录迭代器的使用
|
||
typedef CC_FS::directory_iterator CCDir;
|
||
// 类型别名,用于简化目录条目的使用
|
||
typedef CC_FS::directory_entry CCDirEntry;
|
||
|
||
/**
|
||
* CCFile类提供了对文件操作的支持,如删除、检查文件存在性等。
|
||
*/
|
||
class CCFile
|
||
{
|
||
public:
|
||
// 构造函数,通过文件路径初始化CCFile对象
|
||
CCFile(const CTL::String& path);
|
||
// 构造函数,通过目录条目初始化CCFile对象
|
||
CCFile(const CCDirEntry& entry);
|
||
|
||
//----------------------------------------------------------------------------
|
||
// 判断文件是否为目录
|
||
bool isDirectory() const;
|
||
// 删除文件或目录,如果isAll为true,则递归删除目录下的所有内容
|
||
bool Delete(bool isAll = false) const;
|
||
// 检查文件或目录是否存在
|
||
bool isExists() const;
|
||
// 判断文件是否为常规文件(非目录、符号链接等)
|
||
bool isRegular() const;
|
||
// 获取文件大小
|
||
size_t GetFileSize() const;
|
||
// 获取文件名
|
||
CTL::String GetName() const;
|
||
// 获取文件或目录的路径,如果relative为true,则返回相对路径
|
||
CTL::String GetPath(bool relative = true) const;
|
||
// 获取文件或目录的信息
|
||
CCFileInfo GetFileInfo() const;
|
||
//----------------------------------------------------------------------------
|
||
// 获取指定路径的目录列表迭代器
|
||
static CCDir DirectoryList(const CTL::String &path);
|
||
// 标准化路径,确保路径格式在不同操作系统下的一致性
|
||
static CTL::String NormalizePath(CTL::String path);
|
||
// 获取目录中的所有文件和子目录的信息
|
||
static CCVector<CCFileInfo> GetDirectoryContents(const CTL::String& directoryPath);
|
||
// 创建文件或目录,如果Dir为true,则尝试创建目录,否则创建文件
|
||
static bool Create(const CTL::String& Path,bool Dir = false);
|
||
static bool Copy(const CTL::String& src,const CTL::String& dst);
|
||
static CTL::String GetRelativePath(const CTL::String& path,const CTL::String& basePath);
|
||
static CTL::String GetParentDir(const CTL::String& path);
|
||
static bool ReviseFileName(const CTL::String& OldPath,const CTL::String& NewPath);
|
||
private:
|
||
#ifdef _WIN32
|
||
CTL::CCWString Path; // 文件或目录的路径
|
||
#elif __linux__
|
||
CTL::String Path;
|
||
#endif
|
||
// 将时间点转换为指定格式的字符串
|
||
static time_t to_string(const CC_FS::file_time_type& time) {
|
||
// #if __cplusplus >= 202002L // 检查是否支持C++20
|
||
// auto sysTime = std::chrono::clock_cast<std::chrono::system_clock>(time);
|
||
// #else // 兼容C++17及以下版本
|
||
// const auto fileTimeSinceEpoch = time.time_since_epoch();
|
||
// auto systemClockDuration = std::chrono::duration_cast<std::chrono::system_clock::duration>(fileTimeSinceEpoch);
|
||
// auto sysTime = std::chrono::time_point<std::chrono::system_clock, decltype(systemClockDuration)>(systemClockDuration);
|
||
// #endif
|
||
const auto fileTimeSinceEpoch = time.time_since_epoch();
|
||
auto systemClockDuration = std::chrono::duration_cast<std::chrono::system_clock::duration>(fileTimeSinceEpoch);
|
||
auto sysTime = std::chrono::time_point<std::chrono::system_clock, decltype(systemClockDuration)>(systemClockDuration);
|
||
const auto in_time_t = std::chrono::system_clock::to_time_t(sysTime);
|
||
return in_time_t;
|
||
}
|
||
// 删除文件,这是Delete函数的辅助函数
|
||
bool deleteFile() const;
|
||
};
|
||
#endif
|