Created LoadMemBlock/SaveMemBlock in "inc/hgl/io", their code comes from MemBlock.h

This commit is contained in:
2023-07-15 00:06:30 +08:00
parent c722d40049
commit 11ef085d97
4 changed files with 76 additions and 55 deletions

25
inc/hgl/io/LoadMemBlock.h Normal file
View File

@@ -0,0 +1,25 @@
#pragma once
#include<hgl/type/MemBlock.h>
#include<hgl/io/FileInputStream.h>
namespace hgl
{
/**
* 加载一个文件到内存块类中
*/
template<typename T> MemBlock<T> *LoadFileToMemBlock(const OSString &filename)
{
io::FileInputStream fis;
if(!fis.Open(filename))return(nullptr);
const size_t file_size =fis.GetSize();
const size_t size =(file_size+sizeof(T)-1)/sizeof(T);
MemBlock<T> *mb=new MemBlock<T>(size);
fis.Read(mb->data(),file_size);
return(mb);
}
}//namespace hgl

18
inc/hgl/io/SaveMemBlock.h Normal file
View File

@@ -0,0 +1,18 @@
#pragma once
#include<hgl/type/MemBlock.h>
#include<hgl/filesystem/FileSystem.h>
namespace hgl
{
/**
* 保存一个内存块到文件
*/
template<typename T> bool SaveMemBlockToFile(const OSString &filename,const MemBlock<T> &mb)
{
const size_t size=mb.bytes();
if(size<=0)return(true);
return(hgl::filesystem::SaveMemoryToFile(filename,mb.data(),mb.bytes())==size);
}
}//namespace hgl