2017-07-02 15:18:59 +00:00
|
|
|
#include "file_io.h"
|
|
|
|
|
|
|
|
using namespace fnd;
|
|
|
|
|
|
|
|
static const std::string kModuleName = "FILE_IO";
|
|
|
|
static const size_t kBlockSize = 0x100000;
|
|
|
|
|
|
|
|
void FileIO::ReadFile(const std::string& path, MemoryBlob & blob)
|
|
|
|
{
|
|
|
|
FILE* fp;
|
|
|
|
size_t filesz, filepos;
|
|
|
|
|
|
|
|
if ((fp = fopen(path.c_str(), "rb")) == NULL)
|
|
|
|
{
|
|
|
|
throw Exception(kModuleName, "Failed to open \"" + path + "\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
fseek(fp, 0, SEEK_END);
|
|
|
|
filesz = ftell(fp);
|
|
|
|
rewind(fp);
|
|
|
|
|
|
|
|
if (blob.alloc(filesz) != blob.ERR_NONE)
|
|
|
|
{
|
|
|
|
fclose(fp);
|
|
|
|
throw Exception(kModuleName, "Failed to allocate memory for file");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (filepos = 0; filesz > kBlockSize; filesz -= kBlockSize, filepos += kBlockSize)
|
|
|
|
{
|
|
|
|
fread(blob.data() + filepos, 1, kBlockSize, fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filesz)
|
|
|
|
{
|
|
|
|
fread(blob.data() + filepos, 1, filesz, fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileIO::WriteFile(const std::string& path, const MemoryBlob & blob)
|
|
|
|
{
|
2017-07-05 15:36:59 +00:00
|
|
|
WriteFile(path, blob.data(), blob.size());
|
2017-07-05 09:17:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void fnd::FileIO::WriteFile(const std::string & path, const u8 * data, size_t len)
|
|
|
|
{
|
|
|
|
FILE* fp;
|
|
|
|
size_t filesz, filepos;
|
|
|
|
|
|
|
|
if ((fp = fopen(path.c_str(), "wb")) == NULL)
|
|
|
|
{
|
|
|
|
throw Exception(kModuleName, "Failed to open \"" + path + "\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
filesz = len;
|
2017-07-02 15:18:59 +00:00
|
|
|
|
2017-07-05 09:17:04 +00:00
|
|
|
|
|
|
|
for (filepos = 0; filesz > kBlockSize; filesz -= kBlockSize, filepos += kBlockSize)
|
|
|
|
{
|
|
|
|
fwrite(data + filepos, 1, kBlockSize, fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filesz)
|
|
|
|
{
|
2017-07-05 15:36:59 +00:00
|
|
|
fwrite(data + filepos, 1, filesz, fp);
|
2017-07-05 09:17:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
2017-07-02 15:18:59 +00:00
|
|
|
}
|