CMCore/inc/hgl/filesystem/FileSystem.h

344 lines
12 KiB
C
Raw Normal View History

2019-08-19 19:19:58 +08:00
#ifndef HGL_FILE_SYSTEM_INCLUDE
#define HGL_FILE_SYSTEM_INCLUDE
2020-11-09 13:14:22 +08:00
#include<hgl/type/StringList.h>
2019-08-19 19:19:58 +08:00
namespace hgl
{
namespace io
{
class InputStream;
}//namespace io
namespace filesystem
{
2020-11-09 15:14:28 +08:00
/**
* .<Br>
*
*/
2020-11-09 13:14:22 +08:00
template<typename T>
inline const String<T> ComboFilename(const StringList<String<T>> &sl,const T spear_char=(T)HGL_DIRECTORY_SEPARATOR_RAWCHAR)
{
T *fullname=nullptr;
{
int total=0;
for(auto str:sl)
total+=str->Length();
total+=sl.GetCount();
++total;
fullname=new T[total+1];
}
T *p=fullname;
const T *tmp;
int len;
bool first=true;
for(auto str:sl)
{
len=str->Length();
tmp=trim<T>(str->c_str(),len,isslash<T>);
if(!first)
2020-11-09 13:14:22 +08:00
{
*p=spear_char;
++p;
}
else
{
2020-11-09 13:14:22 +08:00
first=false;
}
hgl_cpy<T>(p,tmp,len);
p+=len;
}
*p=0;
return String<T>::newOf(fullname,p-fullname);
}
2020-11-09 15:14:28 +08:00
/**
* <br>
*
*/
2019-08-19 19:19:58 +08:00
template<typename T>
2020-09-03 15:52:46 +08:00
inline String<T> MergeFilename(const String<T> &pathname,const String<T> &filename,const T directory_separator_char,const T *directory_separator_str)
2019-08-19 19:19:58 +08:00
{
2020-09-03 15:52:46 +08:00
String<T> fullname;
2019-08-19 19:19:58 +08:00
if(pathname.GetEndChar()==directory_separator_char) //结尾有分隔符
{
if(filename.GetBeginChar()==directory_separator_char) //开头有分隔符
{
2020-04-24 21:09:25 +08:00
fullname.SetString(pathname.c_str(),pathname.Length()-1); //少取一个字符
2019-08-19 19:19:58 +08:00
}
else
{
fullname=pathname;
}
}
else //结尾没有分隔符
{
fullname=pathname;
if(filename.GetBeginChar()!=directory_separator_char) //开头没有分隔符
{
fullname.Strcat(directory_separator_str); //添加分隔符
}
}
fullname.Strcat(filename);
return fullname;
}
/**
2020-11-09 15:14:28 +08:00
*
* @param fullname
*/
2019-08-19 19:19:58 +08:00
template<typename T>
2020-09-03 15:52:46 +08:00
inline String<T> ClipFilename(const String<T> &fullname)
2019-08-19 19:19:58 +08:00
{
if(fullname.Length()<=1)
2020-09-03 15:52:46 +08:00
return(String<T>());
2019-08-19 19:19:58 +08:00
2020-09-21 20:39:38 +08:00
const T spear_char[] = { '/','\\',0 };
2019-08-19 19:19:58 +08:00
const int pos=fullname.FindRightChars(spear_char);
2019-08-19 19:19:58 +08:00
if(pos==-1)
2020-09-03 15:52:46 +08:00
return String<T>(fullname);
2019-08-19 19:19:58 +08:00
return fullname.SubString(pos+1);
}
/**
2020-11-09 15:14:28 +08:00
*
* @param filename
* @param split_char ,'.'
*/
2019-08-19 19:19:58 +08:00
template<typename T>
2020-09-03 15:52:46 +08:00
inline String<T> ClipFileMainname(const String<T> &filename,const T split_char='.')
2019-08-19 19:19:58 +08:00
{
if(filename.Length()<=1)
2020-09-03 15:52:46 +08:00
return(String<T>());
2019-08-19 19:19:58 +08:00
2020-09-21 20:39:38 +08:00
const T spear_char[] = { '/','\\',0 };
2019-08-19 19:19:58 +08:00
2020-09-08 22:05:08 +08:00
const int dot=filename.FindRightChar(split_char);
2020-09-18 19:46:56 +08:00
const int pos=filename.FindRightChars(spear_char);
2019-08-19 19:19:58 +08:00
2020-09-08 22:05:08 +08:00
if(dot==-1)
{
if(pos==-1)
return String<T>(filename);
else
return filename.SubString(pos+1);
}
else
{
if(pos==-1)
return filename.SubString(0,dot);
else
return filename.SubString(pos+1,dot-pos-1);
}
2019-08-19 19:19:58 +08:00
}
/**
2020-11-09 15:14:28 +08:00
*
* @param fullname
* @param include_dot
*/
2019-08-19 19:19:58 +08:00
template<typename T>
2020-09-03 15:52:46 +08:00
inline String<T> ClipFileExtName(const String<T> &fullname,bool include_dot=true)
2019-08-19 19:19:58 +08:00
{
int end=fullname.FindChar(T('?')); //url的文件名以?为结束
if(end==-1)
end=fullname.Length();
int pos=fullname.FindRightChar(fullname.Length()-end,T('.'));
if(pos==-1)
2020-09-03 15:52:46 +08:00
return String<T>();
2019-08-19 19:19:58 +08:00
return include_dot? fullname.SubString(pos, end- pos ):
fullname.SubString(pos+1, end-(pos+1));
}
2020-09-14 19:51:30 +08:00
/**
2020-11-09 15:14:28 +08:00
*
* @param fullname
* @param include_dot
*/
2020-09-14 19:51:30 +08:00
template<typename T>
inline String<T> TrimFileExtName(const String<T> &fullname,bool include_dot=false)
{
int end=fullname.FindChar(T('?')); //url的文件名以?为结束
if(end==-1)
end=fullname.Length();
int pos=fullname.FindRightChar(fullname.Length()-end,T('.'));
if(pos==-1)
return String<T>();
return include_dot? fullname.SubString(0,pos):
fullname.SubString(0,pos+1);
}
2020-09-16 22:02:01 +08:00
/**
2020-11-09 15:14:28 +08:00
*
* @param filename
* @param include_spear_char
*/
2020-09-16 22:02:01 +08:00
template<typename T>
inline String<T> ClipPathname(const String<T> &filename,bool include_spear_char=true)
{
if(filename.Length()<=1)
return(String<T>());
2020-09-21 20:39:38 +08:00
const T spear_char[] = { '/','\\',':',0};
2020-09-16 22:02:01 +08:00
2020-09-18 19:46:56 +08:00
const int pos=filename.FindRightChars(spear_char);
2020-09-16 22:02:01 +08:00
if(pos==-1)
return filename;
else
if(include_spear_char)
return filename.SubString(0,pos);
else
return filename.SubString(0,pos-1);
}
2019-08-19 19:19:58 +08:00
/**
2020-11-09 15:14:28 +08:00
*
*/
2019-08-19 19:19:58 +08:00
template<typename T>
2020-09-03 15:52:46 +08:00
inline String<T> ClipLastPathname(const String<T> &fullname)
2019-08-19 19:19:58 +08:00
{
if(fullname.Length()<=1)
2020-09-03 15:52:46 +08:00
return(String<T>());
2019-08-19 19:19:58 +08:00
2020-09-21 20:39:38 +08:00
const T gap_char[]={'\\','/',0};
2019-08-19 19:19:58 +08:00
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])
{
2020-09-03 15:52:46 +08:00
return String<T>(e+1,p-e);
2019-08-19 19:19:58 +08:00
}
--e;
}
}
2020-09-03 15:52:46 +08:00
return(String<T>());
2019-08-19 19:19:58 +08:00
}
inline UTF8String MergeFilename(const UTF8String &pathname,const UTF8String &filename) ///<组合路径名与文件名
2020-07-07 19:14:42 +08:00
{return MergeFilename<u8char>(pathname,filename,HGL_DIRECTORY_SEPARATOR,HGL_DIRECTORY_SEPARATOR_U8STR);}
2019-08-19 19:19:58 +08:00
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 &); ///<文件确认是否存在
2020-05-06 17:13:16 +08:00
bool FileComp(const OSString &,const OSString &,const size_t buf_size=HGL_SIZE_1MB); ///<文件比较
2019-08-19 19:19:58 +08:00
bool FileCanRead(const OSString &); ///<检测文件是否可读
bool FileCanWrite(const OSString &); ///<检测文件是否可写
bool FileCanExec(const OSString &); ///<检测文件是否可执行
void *LoadFileToMemory(const OSString &,int64 &,bool append_zero=false); ///<加载一个文件到内存
int64 LoadFileToMemory(const OSString &,void **,bool append_zero=false); ///<加载一个文件到内存
2019-08-19 19:19:58 +08:00
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 &); ///<取得当前程序所在路径
bool GetLocalAppdataPath(OSString &); ///<取得当前用户应用程序数据存放路径
2019-08-19 19:19:58 +08:00
//使用int64而不是__int64是因为不是所有编译器都支持__int64的写法必须使用DataType.H中引入的定义
/**
2020-11-09 15:14:28 +08:00
*
*/
2019-08-19 19:19:58 +08:00
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