From d065f24770fae3132012f11312495b3dc5ff3892 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Thu, 21 Jan 2021 11:52:37 +0800 Subject: [PATCH] splited the filename functions to Filename.h from FileSystem.h --- inc/hgl/filesystem/FileSystem.h | 256 +------------------------------ inc/hgl/filesystem/Filename.h | 264 ++++++++++++++++++++++++++++++++ src/CMakeLists.txt | 1 + 3 files changed, 266 insertions(+), 255 deletions(-) create mode 100644 inc/hgl/filesystem/Filename.h diff --git a/inc/hgl/filesystem/FileSystem.h b/inc/hgl/filesystem/FileSystem.h index c4afe63..3c4d376 100644 --- a/inc/hgl/filesystem/FileSystem.h +++ b/inc/hgl/filesystem/FileSystem.h @@ -1,7 +1,7 @@ #ifndef HGL_FILE_SYSTEM_INCLUDE #define HGL_FILE_SYSTEM_INCLUDE -#include +#include namespace hgl { namespace io @@ -11,260 +11,6 @@ namespace hgl namespace filesystem { - /** - * 组合文件名.
- * 根据离散的每一级目录名称和最终名称合成完整文件名 - */ - template - inline const String ComboFilename(const StringList> &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(str->c_str(),len,isslash); - - if(!first) - { - *p=spear_char; - ++p; - } - else - { - first=false; - } - - hgl_cpy(p,tmp,len); - p+=len; - } - - *p=0; - - return String::newOf(fullname,p-fullname); - } - - /** - * 合成文件名
- * 根据路径名和文件名 - */ - template - inline String MergeFilename(const String &pathname,const String &filename,const T directory_separator_char,const T *directory_separator_str) - { - String fullname; - - if(pathname.GetEndChar()==directory_separator_char) //结尾有分隔符 - { - if(filename.GetBeginChar()==directory_separator_char) //开头有分隔符 - { - fullname.SetString(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 - inline String ClipFilename(const String &fullname) - { - if(fullname.Length()<=1) - return(String()); - - const T spear_char[] = { '/','\\',0 }; - - const int pos=fullname.FindRightChars(spear_char); - - if(pos==-1) - return String(fullname); - - return fullname.SubString(pos+1); - } - - /** - * 截取一个文件名中的主名称 - * @param filename 文件名 - * @param split_char 扩展名分隔符,一般为'.' - */ - template - inline String ClipFileMainname(const String &filename,const T split_char='.') - { - if(filename.Length()<=1) - return(String()); - - const T spear_char[] = { '/','\\',0 }; - - const int dot=filename.FindRightChar(split_char); - const int pos=filename.FindRightChars(spear_char); - - if(dot==-1) - { - if(pos==-1) - return String(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); - } - } - - /** - * 截取完整文件名中的扩展名 - * @param fullname 完整文件名 - * @param include_dot 是否包括点 - */ - template - inline String ClipFileExtName(const String &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 String(); - - return include_dot? fullname.SubString(pos, end- pos ): - fullname.SubString(pos+1, end-(pos+1)); - } - - /** - * 清除完整文件名中的扩展名 - * @param fullname 完整文件名 - * @param include_dot 是否包括点也清除 - */ - template - inline String TrimFileExtName(const String &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(); - - return include_dot? fullname.SubString(0,pos): - fullname.SubString(0,pos+1); - } - - /** - * 截取一个文件名中的路径名 - * @param filename 文件名 - * @param include_spear_char 是否包含最后的分隔符 - */ - template - inline String ClipPathname(const String &filename,bool include_spear_char=true) - { - if(filename.Length()<=1) - return(String()); - - const T spear_char[] = { '/','\\',':',0}; - - const int pos=filename.FindRightChars(spear_char); - - if(pos==-1) - return filename; - else - if(include_spear_char) - return filename.SubString(0,pos); - else - return filename.SubString(0,pos-1); - } - - /** - * 截取路径最后一个名字 - */ - template - inline String ClipLastPathname(const String &fullname) - { - if(fullname.Length()<=1) - return(String()); - - const T gap_char[]={'\\','/',0}; - - 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 String(e+1,p-e); - } - - --e; - } - } - - return(String()); - } - - inline UTF8String MergeFilename(const UTF8String &pathname,const UTF8String &filename) ///<组合路径名与文件名 - {return MergeFilename(pathname,filename,HGL_DIRECTORY_SEPARATOR,HGL_DIRECTORY_SEPARATOR_U8STR);} - - inline WideString MergeFilename(const WideString &pathname,const WideString &filename) ///<组合路径名与文件名 - {return MergeFilename(pathname,filename,L'\\',L"\\");} - bool FileCopy(const OSString &,const OSString &); ///<文件复制 bool FileDelete(const OSString &); ///<文件删除 bool FileMove(const OSString &,const OSString &); ///<文件移动 diff --git a/inc/hgl/filesystem/Filename.h b/inc/hgl/filesystem/Filename.h new file mode 100644 index 0000000..dad06af --- /dev/null +++ b/inc/hgl/filesystem/Filename.h @@ -0,0 +1,264 @@ +#ifndef HGL_FILESYSTEM_FILENAME_INCLUDE +#define HGL_FILESYSTEM_FILENAME_INCLUDE + +#include +namespace hgl +{ + namespace filesystem + { + /** + * 组合文件名.
+ * 根据离散的每一级目录名称和最终名称合成完整文件名 + */ + template + inline const String ComboFilename(const StringList> &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(str->c_str(),len,isslash); + + if(!first) + { + *p=spear_char; + ++p; + } + else + { + first=false; + } + + hgl_cpy(p,tmp,len); + p+=len; + } + + *p=0; + + return String::newOf(fullname,p-fullname); + } + + /** + * 合成文件名
+ * 根据路径名和文件名 + */ + template + inline String MergeFilename(const String &pathname,const String &filename,const T directory_separator_char,const T *directory_separator_str) + { + String fullname; + + if(pathname.GetEndChar()==directory_separator_char) //结尾有分隔符 + { + if(filename.GetBeginChar()==directory_separator_char) //开头有分隔符 + { + fullname.SetString(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 + inline String ClipFilename(const String &fullname) + { + if(fullname.Length()<=1) + return(String()); + + const T spear_char[] = { '/','\\',0 }; + + const int pos=fullname.FindRightChars(spear_char); + + if(pos==-1) + return String(fullname); + + return fullname.SubString(pos+1); + } + + /** + * 截取一个文件名中的主名称 + * @param filename 文件名 + * @param split_char 扩展名分隔符,一般为'.' + */ + template + inline String ClipFileMainname(const String &filename,const T split_char='.') + { + if(filename.Length()<=1) + return(String()); + + const T spear_char[] = { '/','\\',0 }; + + const int dot=filename.FindRightChar(split_char); + const int pos=filename.FindRightChars(spear_char); + + if(dot==-1) + { + if(pos==-1) + return String(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); + } + } + + /** + * 截取完整文件名中的扩展名 + * @param fullname 完整文件名 + * @param include_dot 是否包括点 + */ + template + inline String ClipFileExtName(const String &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 String(); + + return include_dot? fullname.SubString(pos, end- pos ): + fullname.SubString(pos+1, end-(pos+1)); + } + + /** + * 清除完整文件名中的扩展名 + * @param fullname 完整文件名 + * @param include_dot 是否包括点也清除 + */ + template + inline String TrimFileExtName(const String &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(); + + return include_dot? fullname.SubString(0,pos): + fullname.SubString(0,pos+1); + } + + /** + * 截取一个文件名中的路径名 + * @param filename 文件名 + * @param include_spear_char 是否包含最后的分隔符 + */ + template + inline String ClipPathname(const String &filename,bool include_spear_char=true) + { + if(filename.Length()<=1) + return(String()); + + const T spear_char[] = { '/','\\',':',0}; + + const int pos=filename.FindRightChars(spear_char); + + if(pos==-1) + return filename; + else + if(include_spear_char) + return filename.SubString(0,pos); + else + return filename.SubString(0,pos-1); + } + + /** + * 截取路径最后一个名字 + */ + template + inline String ClipLastPathname(const String &fullname) + { + if(fullname.Length()<=1) + return(String()); + + const T gap_char[]={'\\','/',0}; + + 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 String(e+1,p-e); + } + + --e; + } + } + + return(String()); + } + + inline UTF8String MergeFilename(const UTF8String &pathname,const UTF8String &filename) ///<组合路径名与文件名 + {return MergeFilename(pathname,filename,HGL_DIRECTORY_SEPARATOR,HGL_DIRECTORY_SEPARATOR_U8STR);} + + inline WideString MergeFilename(const WideString &pathname,const WideString &filename) ///<组合路径名与文件名 + {return MergeFilename(pathname,filename,L'\\',L"\\");} + }//namespace filesystem +}//namespace hgl +#endif//HGL_FILESYSTEM_FILENAME_INCLUDE diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d0f478e..679b419 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -110,6 +110,7 @@ SET(FILESYSTEM_INCLUDE_PATH ${CMCORE_ROOT_INCLUDE_PATH}/hgl/filesystem) SET(FILESYSTEM_HEADER_FILES ${FILESYSTEM_INCLUDE_PATH}/EnumFile.h ${FILESYSTEM_INCLUDE_PATH}/EnumVolume.h + ${FILESYSTEM_INCLUDE_PATH}/Filename.h ${FILESYSTEM_INCLUDE_PATH}/FileSystem.h) SET(FILESYSTEM_SOURCE_FILES FileSystem/FileSystem.cpp