diff --git a/inc/hgl/filesystem/Filename.h b/inc/hgl/filesystem/Filename.h index dad06af..fd1f095 100644 --- a/inc/hgl/filesystem/Filename.h +++ b/inc/hgl/filesystem/Filename.h @@ -259,6 +259,8 @@ namespace hgl inline WideString MergeFilename(const WideString &pathname,const WideString &filename) ///<组合路径名与文件名 {return MergeFilename(pathname,filename,L'\\',L"\\");} + + OSString FixFilename(const OSString &filename); ///<修正部分文件名问题 }//namespace filesystem }//namespace hgl #endif//HGL_FILESYSTEM_FILENAME_INCLUDE diff --git a/src/FileSystem/FileSystem.cpp b/src/FileSystem/FileSystem.cpp index e3fd8d4..257656c 100644 --- a/src/FileSystem/FileSystem.cpp +++ b/src/FileSystem/FileSystem.cpp @@ -11,6 +11,75 @@ namespace hgl { namespace filesystem { + /** + * 修正部分文件名问题(目前仅处理错误斜杠和重复斜杠问题) + */ + OSString FixFilename(const OSString &filename) + { + int old_len=filename.Length(); + int new_len; + + os_char *new_string=new os_char[old_len+1]; + + os_char *sp=filename.c_str(); + os_char *tp=new_string; + os_char last_char=0; + bool fix=false; + + while(*sp) + { + if(*sp==HGL_INCORRECT_DIRECTORY_SEPARATOR) + { + if(last_char==HGL_INCORRECT_DIRECTORY_SEPARATOR) + { + fix=true; + ++sp; + continue; + } + else + { + last_char=*sp; + fix=true; + ++sp; + *tp=HGL_DIRECTORY_SEPARATOR; + } + } + else + { + if(*sp==HGL_DIRECTORY_SEPARATOR + &&last_char==HGL_DIRECTORY_SEPARATOR) + { + last_char=*sp; + fix=true; + + ++sp; + continue; + } + else + { + last_char=*sp; + + *tp=*sp; + ++sp; + } + } + + ++tp; + } + + *tp=0; + + new_len=tp-new_string; + + if(!fix) + { + delete[] new_string; + return filename; + } + + return OSString::newOf(new_string,new_len); + } + /** * 比较两个文件是否一样 * @param filename1 第一个文件的名称