From 34eef8fdabd3fe392dcde0d07eab26538e096c77 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Wed, 6 May 2020 17:13:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BFileComp=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/hgl/filesystem/FileSystem.h | 2 +- src/FileSystem/FileSystem.cpp | 34 +++++++++++---------------------- 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/inc/hgl/filesystem/FileSystem.h b/inc/hgl/filesystem/FileSystem.h index c4339ad..0048485 100644 --- a/inc/hgl/filesystem/FileSystem.h +++ b/inc/hgl/filesystem/FileSystem.h @@ -157,7 +157,7 @@ namespace hgl bool FileMove(const OSString &,const OSString &); ///<文件移动 bool FileRename(const OSString &,const OSString &); ///<文件改名 bool FileExist(const OSString &); ///<文件确认是否存在 - bool FileComp(const OSString &,const OSString &); ///<文件比较 + bool FileComp(const OSString &,const OSString &,const size_t buf_size=HGL_SIZE_1MB); ///<文件比较 bool FileCanRead(const OSString &); ///<检测文件是否可读 bool FileCanWrite(const OSString &); ///<检测文件是否可写 diff --git a/src/FileSystem/FileSystem.cpp b/src/FileSystem/FileSystem.cpp index d708543..fc6a140 100644 --- a/src/FileSystem/FileSystem.cpp +++ b/src/FileSystem/FileSystem.cpp @@ -11,15 +11,14 @@ namespace hgl { namespace filesystem { - constexpr int FILE_PROC_BUF_SIZE=HGL_SIZE_1MB; - /** * 比较两个文件是否一样 * @param filename1 第一个文件的名称 * @param filename2 第二个文件的名称 + * @param buf_size * @return 文件是否一样 */ - bool FileComp(const OSString &filename1,const OSString &filename2) + bool FileComp(const OSString &filename1,const OSString &filename2,const size_t buf_size) { io::FileInputStream fp1,fp2; int64 fs1,fs2; @@ -34,34 +33,23 @@ namespace hgl return(false); int64 pos=0,size; - char *data1,*data2; - - data1=new char[FILE_PROC_BUF_SIZE]; - data2=new char[FILE_PROC_BUF_SIZE]; + AutoDeleteArray data1=new char[buf_size]; + AutoDeleteArray data2=new char[buf_size]; while(posfs1)size=fs1-pos; fp1.Read(data1,size); fp2.Read(data2,size); - if(memcmp(data1,data2,size)==0) - { - pos+=size; - continue; - } - else - { - delete[] data1; - delete[] data2; + if(memcmp(data1,data2,size)) return(false); - } - }; - delete[] data1; - delete[] data2; + pos+=size; + } + return(true); } @@ -287,12 +275,12 @@ namespace hgl struct_stat64 file_state; - memset(&file_state,0,sizeof(struct_stat64)); + hgl_zero(file_state); if(hgl_lstat64(filename,&file_state)==-1) return(false); - memset(&fi,0,sizeof(FileInfo)); + hgl_zero(fi); if(file_state.st_mode&S_IFREG) fi.is_file=true;