From 12a766d021ee4d7d3d2201c99cc8e6a55f917941 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Fri, 5 Jun 2020 11:00:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=B0=E5=BD=A2=E5=BC=8F?= =?UTF-8?q?=E7=9A=84LoadFileToMemory=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 | 1 + src/FileSystem/FileSystem.cpp | 45 +++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/inc/hgl/filesystem/FileSystem.h b/inc/hgl/filesystem/FileSystem.h index 0048485..a0b3417 100644 --- a/inc/hgl/filesystem/FileSystem.h +++ b/inc/hgl/filesystem/FileSystem.h @@ -163,6 +163,7 @@ namespace hgl 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); ///<加载一个文件到内存 int64 SaveMemoryToFile(const OSString &,const void *,const int64 &); ///<保存一块内存成文件 int64 SaveMemoryToFile(const OSString &,void **,const int64 *,const int &); ///<保存多块内存成一个文件 diff --git a/src/FileSystem/FileSystem.cpp b/src/FileSystem/FileSystem.cpp index fc6a140..23b871e 100644 --- a/src/FileSystem/FileSystem.cpp +++ b/src/FileSystem/FileSystem.cpp @@ -52,6 +52,29 @@ namespace hgl return(true); } + + void *LoadFileToMemory(const OSString &filename,int64 &size,bool append_zero) + { + io::FileInputStream fs; + + if(!fs.Open(filename)) + return(nullptr); + + size=fs.GetSize(); + + char *fb=new char[append_zero?size+1:size]; + + if(fs.Read(fb,size)==size) + { + if(append_zero) + fb[size]=0; + + return fb; + } + + delete[] fb; + return(nullptr); + } /** * 加载一个文件到内存,文件数据请自己delete[]掉 @@ -61,27 +84,11 @@ namespace hgl */ int64 LoadFileToMemory(const OSString &filename,void **buf,bool append_zero) { - io::FileInputStream fs; + int64 size; - if(!fs.Open(filename)) - return(-1); + *buf=LoadFileToMemory(filename,size,append_zero); - const int64 size=fs.GetSize(); - - char *fb=new char[append_zero?size+1:size]; - - if(fs.Read(fb,size)==size) - { - *buf=fb; - - if(append_zero) - fb[size]=0; - - return(size); - } - - delete[] fb; - return(-1); + return size; } /**