改进FileComp函数

This commit is contained in:
hyzboy 2020-05-06 17:13:16 +08:00
parent fb3f8b1c86
commit 34eef8fdab
2 changed files with 12 additions and 24 deletions

View File

@ -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 &); ///<检测文件是否可写

View File

@ -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<char> data1=new char[buf_size];
AutoDeleteArray<char> data2=new char[buf_size];
while(pos<fs1)
{
size=FILE_PROC_BUF_SIZE;
size=buf_size;
if(pos+size>fs1)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;