[fnd] Replace FileIO singleton with io namespace. Changed style of MemoryBlob, gave it exceptions.

This commit is contained in:
jakcron 2017-07-06 13:27:25 +10:00
parent 752cde4456
commit fa4bdd7143
7 changed files with 73 additions and 83 deletions

View file

@ -1,20 +0,0 @@
#pragma once
#include <string>
#include <fnd/memory_blob.h>
namespace fnd
{
class FileIO
{
public:
static void ReadFile(const std::string& path, MemoryBlob& blob);
//static void ReadFile(const char* path, MemoryBlob& blob, size_t offset, size_t size);
static void WriteFile(const std::string& path, const MemoryBlob& blob);
static void WriteFile(const std::string& path, const u8* data, size_t len);
//static void WriteFile(const char* path, const MemoryBlob& blob, size_t offset, size_t size);
private:
};
}

View file

@ -116,15 +116,15 @@
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="elf.h" /> <ClInclude Include="elf.h" />
<ClInclude Include="file_io.h" />
<ClInclude Include="exception.h" /> <ClInclude Include="exception.h" />
<ClInclude Include="io.h" />
<ClInclude Include="memory_blob.h" /> <ClInclude Include="memory_blob.h" />
<ClInclude Include="string_conv.h" /> <ClInclude Include="string_conv.h" />
<ClInclude Include="types.h" /> <ClInclude Include="types.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="file_io.cpp" />
<ClCompile Include="exception.cpp" /> <ClCompile Include="exception.cpp" />
<ClCompile Include="io.cpp" />
<ClCompile Include="memory_blob.cpp" /> <ClCompile Include="memory_blob.cpp" />
<ClCompile Include="string_conv.cpp" /> <ClCompile Include="string_conv.cpp" />
</ItemGroup> </ItemGroup>

View file

@ -18,9 +18,6 @@
<ClInclude Include="elf.h"> <ClInclude Include="elf.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="file_io.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="memory_blob.h"> <ClInclude Include="memory_blob.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
@ -33,11 +30,11 @@
<ClInclude Include="exception.h"> <ClInclude Include="exception.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="io.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="file_io.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="memory_blob.cpp"> <ClCompile Include="memory_blob.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
@ -47,6 +44,9 @@
<ClCompile Include="exception.cpp"> <ClCompile Include="exception.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="io.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="makefile" /> <None Include="makefile" />

View file

@ -1,11 +1,11 @@
#include "file_io.h" #include "io.h"
using namespace fnd; using namespace fnd;
static const std::string kModuleName = "FILE_IO"; static const std::string kModuleName = "IO";
static const size_t kBlockSize = 0x100000; static const size_t kBlockSize = 0x100000;
void FileIO::ReadFile(const std::string& path, MemoryBlob & blob) void io::readFile(const std::string& path, MemoryBlob & blob)
{ {
FILE* fp; FILE* fp;
size_t filesz, filepos; size_t filesz, filepos;
@ -19,31 +19,34 @@ void FileIO::ReadFile(const std::string& path, MemoryBlob & blob)
filesz = ftell(fp); filesz = ftell(fp);
rewind(fp); rewind(fp);
if (blob.alloc(filesz) != blob.ERR_NONE) try {
blob.alloc(filesz);
}
catch (const fnd::Exception& e)
{ {
fclose(fp); fclose(fp);
throw Exception(kModuleName, "Failed to allocate memory for file"); throw fnd::Exception(kModuleName, "Failed to allocate memory for file: " + std::string(e.what()));
} }
for (filepos = 0; filesz > kBlockSize; filesz -= kBlockSize, filepos += kBlockSize) for (filepos = 0; filesz > kBlockSize; filesz -= kBlockSize, filepos += kBlockSize)
{ {
fread(blob.data() + filepos, 1, kBlockSize, fp); fread(blob.getBytes() + filepos, 1, kBlockSize, fp);
} }
if (filesz) if (filesz)
{ {
fread(blob.data() + filepos, 1, filesz, fp); fread(blob.getBytes() + filepos, 1, filesz, fp);
} }
fclose(fp); fclose(fp);
} }
void FileIO::WriteFile(const std::string& path, const MemoryBlob & blob) void io::writeFile(const std::string& path, const MemoryBlob & blob)
{ {
WriteFile(path, blob.data(), blob.size()); writeFile(path, blob.getBytes(), blob.getSize());
} }
void fnd::FileIO::WriteFile(const std::string & path, const u8 * data, size_t len) void io::writeFile(const std::string & path, const u8 * data, size_t len)
{ {
FILE* fp; FILE* fp;
size_t filesz, filepos; size_t filesz, filepos;

13
lib/fnd/io.h Normal file
View file

@ -0,0 +1,13 @@
#pragma once
#include <string>
#include <fnd/memory_blob.h>
namespace fnd
{
namespace io
{
void readFile(const std::string& path, MemoryBlob& blob);
void writeFile(const std::string& path, const MemoryBlob& blob);
void writeFile(const std::string& path, const u8* data, size_t len);
}
}

View file

@ -3,56 +3,54 @@
using namespace fnd; using namespace fnd;
MemoryBlob::MemoryBlob() : MemoryBlob::MemoryBlob() :
data_(), mData(),
size_(0), mSize(0),
apparent_size_(0) mVisableSize(0)
{ {
} }
MemoryBlob::~MemoryBlob() fnd::MemoryBlob::MemoryBlob(const byte_t * bytes, size_t len) :
mData(),
mSize(0),
mVisableSize(0)
{ {
alloc(len);
memcpy(getBytes(), bytes, getSize());
} }
int MemoryBlob::alloc(size_t size) void MemoryBlob::alloc(size_t size)
{ {
int ret = ERR_NONE; if (size > mSize)
if (size > size_)
{ {
ret = AllocateMemory(size); allocateMemory(size);
} }
else else
{ {
apparent_size_ = size; mVisableSize = size;
ClearMemory(); clearMemory();
} }
return ret;
} }
int MemoryBlob::extend(size_t new_size) void MemoryBlob::extend(size_t new_size)
{ {
try { try {
data_.resize(new_size); mData.resize(new_size);
} }
catch (...) { catch (...) {
return ERR_FAILMALLOC; throw fnd::Exception(kModuleName, "extend() failed to allocate memory");
} }
return ERR_NONE;
return 0;
} }
int MemoryBlob::AllocateMemory(size_t size) void MemoryBlob::allocateMemory(size_t size)
{ {
size_ = (size_t)align(size, 0x1000); mSize = (size_t)align(size, kAllocBlockSize);
apparent_size_ = size; mVisableSize = size;
data_.resize(size_); extend(mSize);
ClearMemory(); clearMemory();
return ERR_NONE;
} }
void MemoryBlob::ClearMemory() void MemoryBlob::clearMemory()
{ {
memset(data_.data(), 0, size_); memset(mData.data(), 0, mSize);
} }

View file

@ -11,31 +11,27 @@ namespace fnd
class MemoryBlob class MemoryBlob
{ {
public: public:
enum ErrorCode
{
ERR_NONE,
ERR_FAILOPEN,
ERR_FAILMALLOC,
ERR_FAILREAD,
};
MemoryBlob(); MemoryBlob();
MemoryBlob(const byte_t* bytes, size_t len);
~MemoryBlob(); void alloc(size_t size);
void extend(size_t new_size);
int alloc(size_t size); inline byte_t& operator[](size_t index) { return mData[index]; }
int extend(size_t new_size);; inline const byte_t& operator[](size_t index) const { return mData[index]; }
inline byte_t* data() { return data_.data(); } inline byte_t* getBytes() { return mData.data(); }
inline const byte_t* data() const { return data_.data(); } inline const byte_t* getBytes() const { return mData.data(); }
inline size_t size() const { return apparent_size_; } inline size_t getSize() const { return mVisableSize; }
private: private:
std::vector<byte_t> data_; const std::string kModuleName = "MEMORY_BLOB";
size_t size_; static const size_t kAllocBlockSize = 0x1000;
size_t apparent_size_;
int AllocateMemory(size_t size); std::vector<byte_t> mData;
size_t mSize;
size_t mVisableSize;
void ClearMemory(); void allocateMemory(size_t size);
void clearMemory();
}; };
} }