Files
dl_old/dl/io/dl_file.h
T
2023-11-19 17:34:06 +08:00

251 lines
6.6 KiB
C++

/**
* @file dl_file.h
* @brief 文件系统
*
*
* @version 1.0
* @author lveyou
* @date 21-08-09
*
* @note 统一操作系统的文件操作
*
* @note 原则0:使用/表示路径分隔符
* @note 原则1:路径的结尾规定带有/,因为cd到盘根路径必须加/,还因为相对路径不以/开头
* @note 原则2:优先使用相对路径,相对路径开头不带有/
* @note 原则3:/表示根路径、./表示当前目录、../表示上一目录
*
* @note 优先使用C++文件流,为了兼容再以C文件操作实现。
* @note 由于std::filesystem的字符编码,为了便于使用,所以封装一层
*
* @note 命名规则:path表示路径、path_name表示路径的文件
* file表示文件,folder表示文件夹、content表示所有内容
*/
#pragma once
//#include <filesystem>
#include <string>
#include <vector>
#include <fstream>
#include "base/dl_tree.h"
#include "memory/dl_buffer.h"
namespace dl::File
{
/**
* @brief 获取文件夹所有文件
* @param[in] path 路径
* @param[out] ret 返回的文件名(相对于path)
* @param[in] regex 正则表达式,例如 .(png|jpg)$
* @param[in] recursion 递归文件夹
*/
void GetPathFileAll(std::string_view path, std::vector<std::string>& ret,
std::string_view regex = "", bool recursion = true);
/**
* @brief 获取文件夹当前内容
* @param[in] path 路径
* @param[out] ret 返回的目录
* @param[in] regex 正则表达式,例如 .(png|jpg)$
* @param[in] folder 是否包含目录
* @param[in] file 是否包含文件
*/
void GetPathContent(std::string_view path, std::vector<std::string>& ret,
std::string_view regex = "", bool folder = true, bool file = true);
/**
* @brief 获取文件夹所有内容
* @param[in] path 路径
* @param[out] ret 返回的目录树
*/
void GetPathContentAll(std::string_view path, NTree<std::string>& ret);
/**
* @brief 打开一个文件(只读),可能从压缩包内读取
* @param[in] path_name 路径名称
* @param[in] binary 是否以二进制模式打开
* @retval 文件句柄
*
* @note 如果成功读取,则用户需要手动释放文件
*/
FILE* GetFile(std::string_view path_name, bool is_binary = true);
std::ifstream Get(std::string_view path_name, bool is_binary = true);
/**
* @brief 将文本文件从mb转到utf-8
* @param[in] path_name 路径名称
* @param[in] out 输出文件的路径名称
* @retval 如果失败,会输出日志
*/
bool ConvertMb2U8(std::string_view path_name, std::string_view out);
/**
* @brief 返回文件内存
* @param[in] path_name 路径名称
* @retval 需要手动释放内存
*/
Buffer GetBuffer(std::string_view path_name);
/**
* @brief 返回文件内存
* @param[in] ifs 文件句柄
* @retval 需要手动释放内存
*/
Buffer GetBuffer(std::ifstream& ifs);
/**
* @brief 将内存保存为文件
* @param[in] buf 内存
* @param[in] path_name 保存路径(文件夹必须存在)
*/
bool SaveBuffer(BufferView buf, std::string_view path_name);
///**
//* @brief 返回文件Blake2b值
//* @param[in] path_name 路径名称
//* @param[out] Blake2b 返回值(512位)
//*/
//bool HashBlake2b(std::string_view path_name, Crypto::Blake2b& blake2b);
//
///**
//* @brief 效验文件Blake2b值
//* @param[in] path_name 路径名称
//* @param[in] digest 消息摘要(512位)
//*/
//bool HashBlake2bCheck(std::string_view path_name, const Crypto::Blake2b& digest);
/**
* @brief 返回文件crc32值
* @param[in] path_name 路径名称
* @retval 0代表失败
*/
uint32_t GetCrc32(std::string_view path_name);
/**
* @brief 返回文件是否存在
* @param[in] path_name 路径名称
* @note 不包含压缩包内文件
*/
bool IsExist(std::string_view path_name);
/**
* @brief 删除文件或路径
* @param[in] path_name 路径名称
* @param[in] all 为真时可以删除非空文件夹
* @retval 删除失败返回假
*/
bool Delete(std::string_view path_name, bool all = false);
/**
* @brief 删除文件夹内容,但不删除文件夹
* @param[in] path_name 路径名称
* @note 文件夹不存在则什么都不做
*/
void DeleteContent(std::string_view path_name);
/**
* @brief 复制文件
* @param[in] source 源文件
* @param[in] target 目标文件
* @note 文件夹不存在会失败
*/
bool Copy(std::string_view source, std::string_view target);
/**
* @brief 返回文件后缀
* @param[in] path_name
* @param[in] has_dot 是否含有“.”,比如.txt而不是txt
*/
std::string GetExtension(std::string_view path_name, bool has_dot = false);
/**
* @brief 返回文件名
* @param[in] path_name
*/
std::string GetFilename(std::string_view path_name);
/**
* @brief 返回文件名不含后缀
* @param[in] path_name
*/
std::string GetStem(std::string_view path_name);
/**
* @brief 创建目录
* @param[in] path 路径
*/
bool CreateFolder(std::string_view path);
/**
* @brief 返回文件最后修改时间
* @param[in] path_name 路径名称
* @note 复制相同文件也会更新,建议使用GetCrc32
*/
time_t GetLastWriteTime(std::string_view path_name);
/**
* @brief 判断路径内的文件是否有修改(包括增加、删除文件)
* @param[in] path_name 路径名称
* @param[in] cache 用于生成和保存的文件信息的文件
* @param[in] update cache文件如果存在且不匹配,是否更新cache
*
* @note 第一次必然返回真,然后根据文件信息生成cache文件。下次调用会依据cache文件判断路径内文件是否有修改
* 发生错误返回真
* @note 根据文件修改时间判断
*/
bool CheckPathFileModified(std::string_view path_name, std::string_view cache, bool update);
//----------------------------常见路径----------------------------
/**
* @brief 返回资源路径
* @note windows rw exe
* linux rw
* android r assets
*/
std::string PathCurrent();
/**
* @brief 返回临时路径 rw
* @note 这个位置进行文件写入,容易引起误杀,请改用g_factory的接口
*/
std::string PathTemp();
/**
* @brief 返回存储路径 rw
* @note windows AppData/Local/
* linux
* android /storage/emulated/0/Android/data/org.dl.test/files
*/
std::string PathStorage();
/**
* @brief 返回私有路径 rw
* @note windows AppData/LocalLow/
* linux
* android /data/data/org.dl.test/files/
* 安卓容量有限
*/
std::string PathPrivate();
/**
* @brief 返回公共路径 rw
* @note windows ProgramData/
* linux
* android /storage/emulated/0/
*/
std::string PathPublic();
////! C++20才有标准化的转换方式s
//string LastWriteTimeTostring(const std::file_time_type& ftime)
//{
// string str;
// std::time_t cftime = file_time_type::clock::time_point(ftime).time_since_epoch();
// std::time_t cftime = std::chrono::system_clock::to_time_t();
// return std::asctime(std::localtime(&cftime));
//}
void InitFileSystem();
}