first commit
This commit is contained in:
9
inc/hgl/filesystem/AssetManage.h
Normal file
9
inc/hgl/filesystem/AssetManage.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef HGL_FILESYSTEM_ASSET_MANAGE_INCLUDE
|
||||
#define HGL_FILESYSTEM_ASSET_MANAGE_INCLUDE
|
||||
|
||||
#include<hgl/type/BaseString.h>
|
||||
namespace hgl
|
||||
{
|
||||
|
||||
}//namespace hgl
|
||||
#endif//HGL_FILESYSTEM_ASSET_MANAGE_INCLUDE
|
75
inc/hgl/filesystem/EnumFile.h
Normal file
75
inc/hgl/filesystem/EnumFile.h
Normal file
@@ -0,0 +1,75 @@
|
||||
#include<hgl/filesystem/FileSystem.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
namespace filesystem
|
||||
{
|
||||
struct EnumFileConfig
|
||||
{
|
||||
OSString folder_name; ///<要枚举的目录名称
|
||||
|
||||
#if HGL_OS == HGL_OS_Windows ///<通配符过滤是Windows平台独有
|
||||
OSString find_name; ///<要枚举的文件名称
|
||||
#endif//HGL_OS == HGL_OS_Windows
|
||||
|
||||
bool proc_folder; ///<是否处理目录
|
||||
bool proc_file; ///<是否处理文件
|
||||
bool sub_folder; ///<是否查找子目录
|
||||
|
||||
public:
|
||||
|
||||
EnumFileConfig(const OSString &fn)
|
||||
{
|
||||
folder_name=fn;
|
||||
|
||||
#if HGL_OS == HGL_OS_Windows
|
||||
find_name=OS_TEXT("*.*");
|
||||
#endif//HGL_OS == HGL_OS_Windows
|
||||
|
||||
proc_folder=true;
|
||||
proc_file=true;
|
||||
sub_folder=false;
|
||||
}
|
||||
|
||||
EnumFileConfig(const EnumFileConfig *efc,const OSString &sub_folder_name)
|
||||
{
|
||||
folder_name =sub_folder_name;
|
||||
|
||||
#if HGL_OS == HGL_OS_Windows
|
||||
find_name =efc->find_name;
|
||||
#endif//HGL_OS == HGL_OS_Windows
|
||||
|
||||
proc_folder =efc->proc_folder;
|
||||
proc_file =efc->proc_file;
|
||||
sub_folder =efc->sub_folder;
|
||||
}
|
||||
|
||||
virtual ~EnumFileConfig()=default;
|
||||
};//struct EnumFileConfig
|
||||
|
||||
/**
|
||||
* 枚举文件系统内的文件
|
||||
*/
|
||||
class EnumFile
|
||||
{
|
||||
protected:
|
||||
|
||||
virtual void ProcFolder(struct EnumFileConfig *parent_efc,struct EnumFileConfig *cur_efc,FileInfo &fi){}
|
||||
virtual void ProcFile(struct EnumFileConfig *,FileInfo &fi){}
|
||||
|
||||
virtual EnumFileConfig *CreateSubConfig(struct EnumFileConfig *up_efc,const FileInfo &fi)
|
||||
{
|
||||
const OSString full_sub_folder_name=MergeFilename(up_efc->folder_name,fi.name);
|
||||
|
||||
return(new EnumFileConfig(up_efc,full_sub_folder_name));
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
EnumFile()=default;
|
||||
virtual ~EnumFile()=default;
|
||||
|
||||
virtual int Enum(EnumFileConfig *);
|
||||
};//class EnumFile
|
||||
}//namespace filesystem
|
||||
}//namespace hgl
|
99
inc/hgl/filesystem/EnumVolume.h
Normal file
99
inc/hgl/filesystem/EnumVolume.h
Normal file
@@ -0,0 +1,99 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/type/List.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
namespace filesystem
|
||||
{
|
||||
/**
|
||||
* 卷信息数据结构
|
||||
*/
|
||||
struct VolumeInfo
|
||||
{
|
||||
enum DriverType
|
||||
{
|
||||
dtNone=0, ///<未知类型
|
||||
|
||||
dtRemovable, ///<可移动设备
|
||||
dtFixed, ///<固定设备
|
||||
dtRemote, ///<远程设备
|
||||
dtCDROM, ///<光盘驱动器
|
||||
dtRamDisk, ///<内存虚拟设备
|
||||
|
||||
dtEnd ///<结束定义
|
||||
};
|
||||
|
||||
u16char name[HGL_MAX_PATH]; ///<卷名称
|
||||
|
||||
u16char path[HGL_MAX_PATH]; ///<卷所对应的路径名(注意:不是所有卷都有对应路径)
|
||||
|
||||
DriverType driver_type; ///<驱动器类型(注意:不是所有的卷都对应驱动器)
|
||||
|
||||
uint32 serial; ///<卷序列号
|
||||
|
||||
u16char volume_label[HGL_MAX_PATH]; ///<卷标名称
|
||||
|
||||
u16char file_system[HGL_MAX_PATH]; ///<文件系统名称
|
||||
|
||||
uint32 filename_max_length; ///<文件名最大长度
|
||||
|
||||
bool unicode; ///<文件名支持UNICODE
|
||||
|
||||
uint64 available_space; ///<有效容量
|
||||
uint64 total_space; ///<总空量
|
||||
uint64 free_space; ///<自由容量
|
||||
};//struct VolumeInfo
|
||||
|
||||
#if HGL_OS == HGL_OS_Windows
|
||||
|
||||
/**
|
||||
* 卷检测配置
|
||||
*/
|
||||
struct VolumeCheckConfig
|
||||
{
|
||||
bool removable =false;
|
||||
bool fixed =false;
|
||||
bool remote =false;
|
||||
bool cdrom =false;
|
||||
bool ram_disk =false;
|
||||
bool unknow =false;
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* 设置为全部检测
|
||||
*/
|
||||
void SetFullCheck()
|
||||
{
|
||||
memset(this,0xff,sizeof(VolumeCheckConfig));
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否无效配置
|
||||
*/
|
||||
bool isErrorConfig()const
|
||||
{
|
||||
if(removable)return(false);
|
||||
if(fixed)return(false);
|
||||
if(remote)return(false);
|
||||
if(cdrom)return(false);
|
||||
if(ram_disk)return(false);
|
||||
if(unknow)return(false);
|
||||
|
||||
return(true);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 枚举当前计算机所有卷
|
||||
* @param vi_list 储存卷信息的列表
|
||||
* @param check_removable 检测可移除设备
|
||||
* @param check_remote 检测远程驱动器
|
||||
* @param check_cd 检测光盘
|
||||
* @return 查找到的卷数量,-1表示失败
|
||||
*/
|
||||
int EnumVolume(List<VolumeInfo> &vi_list,const VolumeCheckConfig &);
|
||||
#endif//HGL_OS == HGL_OS_Windows
|
||||
}//namespace filesystem
|
||||
}//namespace hgl
|
229
inc/hgl/filesystem/FileSystem.h
Normal file
229
inc/hgl/filesystem/FileSystem.h
Normal file
@@ -0,0 +1,229 @@
|
||||
#ifndef HGL_FILE_SYSTEM_INCLUDE
|
||||
#define HGL_FILE_SYSTEM_INCLUDE
|
||||
|
||||
#include<hgl/type/BaseString.h>
|
||||
#include<hgl/type/List.h>
|
||||
namespace hgl
|
||||
{
|
||||
namespace io
|
||||
{
|
||||
class InputStream;
|
||||
}//namespace io
|
||||
|
||||
namespace filesystem
|
||||
{
|
||||
template<typename T>
|
||||
inline BaseString<T> MergeFilename(const BaseString<T> &pathname,const BaseString<T> &filename,const T directory_separator_char,const T *directory_separator_str)
|
||||
{
|
||||
BaseString<T> fullname;
|
||||
|
||||
if(pathname.GetEndChar()==directory_separator_char) //结尾有分隔符
|
||||
{
|
||||
if(filename.GetBeginChar()==directory_separator_char) //开头有分隔符
|
||||
{
|
||||
fullname.Set(pathname.c_str(),pathname.Length()-1); //少取一个字符
|
||||
}
|
||||
else
|
||||
{
|
||||
fullname=pathname;
|
||||
}
|
||||
}
|
||||
else //结尾没有分隔符
|
||||
{
|
||||
fullname=pathname;
|
||||
|
||||
if(filename.GetBeginChar()!=directory_separator_char) //开头没有分隔符
|
||||
{
|
||||
fullname.Strcat(directory_separator_str); //添加分隔符
|
||||
}
|
||||
}
|
||||
|
||||
fullname.Strcat(filename);
|
||||
return fullname;
|
||||
}
|
||||
|
||||
/**
|
||||
* 截取完整路径中的文件名
|
||||
* @param fullname 完整路径文件名
|
||||
*/
|
||||
template<typename T>
|
||||
inline BaseString<T> ClipFilename(const BaseString<T> &fullname)
|
||||
{
|
||||
if(fullname.Length()<=1)
|
||||
return(BaseString<T>());
|
||||
|
||||
const T spear_char[] = { '/','\\' };
|
||||
|
||||
const int pos=fullname.FindRightChar(spear_char);
|
||||
|
||||
if(pos==-1)
|
||||
return BaseString<T>(fullname);
|
||||
|
||||
return fullname.SubString(pos+1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 截取一个文件名中的主名称(不能带路径)
|
||||
* @param filename 文件名
|
||||
* @param split_char 扩展名分隔符,一般为'.'
|
||||
*/
|
||||
template<typename T>
|
||||
inline BaseString<T> ClipFileMainname(const BaseString<T> &filename,const T split_char='.')
|
||||
{
|
||||
if(filename.Length()<=1)
|
||||
return(BaseString<T>());
|
||||
|
||||
const int pos=filename.FindRightChar(split_char);
|
||||
|
||||
if(pos==-1)
|
||||
return BaseString<T>(filename);
|
||||
|
||||
return filename.SubString(0,pos);
|
||||
}
|
||||
|
||||
/**
|
||||
* 截取完整文件名中的扩展名
|
||||
* @param fullname 完整文件名
|
||||
* @param include_dot 是否包括点
|
||||
*/
|
||||
template<typename T>
|
||||
inline BaseString<T> ClipFileExtName(const BaseString<T> &fullname,bool include_dot=true)
|
||||
{
|
||||
int end=fullname.FindChar(T('?')); //url的文件名,以?为结束
|
||||
|
||||
if(end==-1)
|
||||
end=fullname.Length();
|
||||
|
||||
int pos=fullname.FindRightChar(fullname.Length()-end,T('.'));
|
||||
|
||||
if(pos==-1)
|
||||
return BaseString<T>();
|
||||
|
||||
return include_dot? fullname.SubString(pos, end- pos ):
|
||||
fullname.SubString(pos+1, end-(pos+1));
|
||||
}
|
||||
|
||||
/**
|
||||
* 截取路径最后一个名字
|
||||
*/
|
||||
template<typename T>
|
||||
inline BaseString<T> ClipLastPathname(const BaseString<T> &fullname)
|
||||
{
|
||||
if(fullname.Length()<=1)
|
||||
return(BaseString<T>());
|
||||
|
||||
const T gap_char[2]={'\\','/'};
|
||||
|
||||
T *p=nullptr;
|
||||
|
||||
T *s=fullname.c_str();
|
||||
T *e=fullname.c_str()+fullname.Length()-1;
|
||||
|
||||
while(e>s)
|
||||
{
|
||||
if(!p)
|
||||
{
|
||||
if(*e==gap_char[0]||*e==gap_char[1])
|
||||
{
|
||||
--e;
|
||||
continue;
|
||||
}
|
||||
|
||||
p=e;
|
||||
--e;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(*e==gap_char[0]||*e==gap_char[1])
|
||||
{
|
||||
return BaseString<T>(e+1,p-e);
|
||||
}
|
||||
|
||||
--e;
|
||||
}
|
||||
}
|
||||
|
||||
return(BaseString<T>());
|
||||
}
|
||||
|
||||
inline UTF8String MergeFilename(const UTF8String &pathname,const UTF8String &filename) ///<组合路径名与文件名
|
||||
{return MergeFilename<char>(pathname,filename,HGL_DIRECTORY_SEPARATOR,HGL_DIRECTORY_SEPARATOR_U8STR);}
|
||||
|
||||
inline WideString MergeFilename(const WideString &pathname,const WideString &filename) ///<组合路径名与文件名
|
||||
{return MergeFilename<wchar_t>(pathname,filename,L'\\',L"\\");}
|
||||
|
||||
bool FileCopy(const OSString &,const OSString &); ///<文件复制
|
||||
bool FileDelete(const OSString &); ///<文件删除
|
||||
bool FileMove(const OSString &,const OSString &); ///<文件移动
|
||||
bool FileRename(const OSString &,const OSString &); ///<文件改名
|
||||
bool FileExist(const OSString &); ///<文件确认是否存在
|
||||
bool FileComp(const OSString &,const OSString &); ///<文件比较
|
||||
|
||||
bool FileCanRead(const OSString &); ///<检测文件是否可读
|
||||
bool FileCanWrite(const OSString &); ///<检测文件是否可写
|
||||
bool FileCanExec(const OSString &); ///<检测文件是否可执行
|
||||
|
||||
int64 LoadFileToMemory(const OSString &,void **); ///<加载一个文件到内存
|
||||
int64 SaveMemoryToFile(const OSString &,const void *,const int64 &); ///<保存一块内存成文件
|
||||
int64 SaveMemoryToFile(const OSString &,void **,const int64 *,const int &); ///<保存多块内存成一个文件
|
||||
|
||||
void *LoadFileToMemory(const OSString &,int64,void *buf,int64); ///<加载一个文件的一部分到内存
|
||||
bool SaveMemoryToFile(const OSString &,int64,const void *,int64); ///<保存一块内存到一个文件
|
||||
|
||||
bool IsDirectory(const os_char *);
|
||||
inline bool IsDirectory(const OSString &str){return IsDirectory(str.c_str());} ///<判断这个名称是否是目录
|
||||
|
||||
#if HGL_OS != HGL_OS_Windows
|
||||
bool IsLink(const os_char *); ///<判断这个名称是否是链接
|
||||
#endif//
|
||||
|
||||
bool MakePath(const OSString &); ///<创建一个路径
|
||||
bool DeletePath(const OSString &); ///<删除一个路径
|
||||
void DeleteTree(const OSString &); ///<删除一个路径(包含所有文件)
|
||||
|
||||
bool GetCurrentPath(OSString &); ///<取得当前路径
|
||||
bool GetCurrentProgram(OSString &); ///<取得当前程序全路径名称
|
||||
bool GetCurrentProgramPath(OSString &); ///<取得当前程序所在路径
|
||||
void GetLocalAppdataPath(os_char fn[HGL_MAX_PATH]); ///<取得当前用户应用程序数据存放路径
|
||||
|
||||
//使用int64而不是__int64是因为不是所有编译器都支持__int64的写法,必须使用DataType.H中引入的定义
|
||||
|
||||
/**
|
||||
* 文件信息数据结构
|
||||
*/
|
||||
struct FileInfo ///文件信息
|
||||
{
|
||||
os_char name[HGL_MAX_PATH]; ///<文件名(不包含路径)
|
||||
os_char fullname[HGL_MAX_PATH]; ///<完整名称(包含路径)
|
||||
|
||||
uint64 size; ///<文件长度
|
||||
|
||||
union
|
||||
{
|
||||
uint32 attrib; ///<文件属性
|
||||
|
||||
struct
|
||||
{
|
||||
bool is_file:1; ///<是文件
|
||||
bool is_directory:1; ///<是目录
|
||||
|
||||
bool is_hiddle:1; ///<是否隐藏文件
|
||||
|
||||
#if HGL_OS != HGL_OS_Windows
|
||||
bool is_link:1; ///<是否是链接
|
||||
#endif//HGL_OS != HGL_OS_Windows
|
||||
|
||||
bool can_read:1; ///<可以读
|
||||
bool can_write:1; ///<可以写
|
||||
};
|
||||
};
|
||||
|
||||
uint64 mtime; ///<最后修改日期(这个值在win/unix下不通用)
|
||||
};//struct FileInfo
|
||||
|
||||
bool GetFileInfo(const os_char *filename,struct FileInfo &); ///<取得文件信息
|
||||
|
||||
int GetFileInfoList(List<FileInfo> &, const OSString &folder_name, bool proc_folder, bool proc_file, bool sub_folder);
|
||||
}//namespace filesystem
|
||||
}//namespace hgl
|
||||
#endif//HGL_FILE_SYSTEM_INCLUDE
|
Reference in New Issue
Block a user