mirror of
https://github.com/jakcron/nstool
synced 2024-12-25 06:01:12 +00:00
[fnd] Replace FileIO singleton with io namespace. Changed style of MemoryBlob, gave it exceptions.
This commit is contained in:
parent
752cde4456
commit
fa4bdd7143
7 changed files with 73 additions and 83 deletions
|
@ -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:
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -116,15 +116,15 @@
|
|||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="elf.h" />
|
||||
<ClInclude Include="file_io.h" />
|
||||
<ClInclude Include="exception.h" />
|
||||
<ClInclude Include="io.h" />
|
||||
<ClInclude Include="memory_blob.h" />
|
||||
<ClInclude Include="string_conv.h" />
|
||||
<ClInclude Include="types.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="file_io.cpp" />
|
||||
<ClCompile Include="exception.cpp" />
|
||||
<ClCompile Include="io.cpp" />
|
||||
<ClCompile Include="memory_blob.cpp" />
|
||||
<ClCompile Include="string_conv.cpp" />
|
||||
</ItemGroup>
|
||||
|
|
|
@ -18,9 +18,6 @@
|
|||
<ClInclude Include="elf.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="file_io.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="memory_blob.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
|
@ -33,11 +30,11 @@
|
|||
<ClInclude Include="exception.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="io.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="file_io.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="memory_blob.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
|
@ -47,6 +44,9 @@
|
|||
<ClCompile Include="exception.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="io.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="makefile" />
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
#include "file_io.h"
|
||||
#include "io.h"
|
||||
|
||||
using namespace fnd;
|
||||
|
||||
static const std::string kModuleName = "FILE_IO";
|
||||
static const std::string kModuleName = "IO";
|
||||
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;
|
||||
size_t filesz, filepos;
|
||||
|
@ -19,31 +19,34 @@ void FileIO::ReadFile(const std::string& path, MemoryBlob & blob)
|
|||
filesz = ftell(fp);
|
||||
rewind(fp);
|
||||
|
||||
if (blob.alloc(filesz) != blob.ERR_NONE)
|
||||
try {
|
||||
blob.alloc(filesz);
|
||||
}
|
||||
catch (const fnd::Exception& e)
|
||||
{
|
||||
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)
|
||||
{
|
||||
fread(blob.data() + filepos, 1, kBlockSize, fp);
|
||||
fread(blob.getBytes() + filepos, 1, kBlockSize, fp);
|
||||
}
|
||||
|
||||
if (filesz)
|
||||
{
|
||||
fread(blob.data() + filepos, 1, filesz, fp);
|
||||
fread(blob.getBytes() + filepos, 1, filesz, 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;
|
||||
size_t filesz, filepos;
|
13
lib/fnd/io.h
Normal file
13
lib/fnd/io.h
Normal 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);
|
||||
}
|
||||
}
|
|
@ -3,56 +3,54 @@
|
|||
using namespace fnd;
|
||||
|
||||
MemoryBlob::MemoryBlob() :
|
||||
data_(),
|
||||
size_(0),
|
||||
apparent_size_(0)
|
||||
mData(),
|
||||
mSize(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 > size_)
|
||||
if (size > mSize)
|
||||
{
|
||||
ret = AllocateMemory(size);
|
||||
allocateMemory(size);
|
||||
}
|
||||
else
|
||||
{
|
||||
apparent_size_ = size;
|
||||
ClearMemory();
|
||||
mVisableSize = size;
|
||||
clearMemory();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int MemoryBlob::extend(size_t new_size)
|
||||
void MemoryBlob::extend(size_t new_size)
|
||||
{
|
||||
try {
|
||||
data_.resize(new_size);
|
||||
mData.resize(new_size);
|
||||
}
|
||||
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);
|
||||
apparent_size_ = size;
|
||||
data_.resize(size_);
|
||||
ClearMemory();
|
||||
return ERR_NONE;
|
||||
mSize = (size_t)align(size, kAllocBlockSize);
|
||||
mVisableSize = size;
|
||||
extend(mSize);
|
||||
clearMemory();
|
||||
}
|
||||
|
||||
void MemoryBlob::ClearMemory()
|
||||
void MemoryBlob::clearMemory()
|
||||
{
|
||||
memset(data_.data(), 0, size_);
|
||||
memset(mData.data(), 0, mSize);
|
||||
}
|
||||
|
|
|
@ -11,31 +11,27 @@ namespace fnd
|
|||
class MemoryBlob
|
||||
{
|
||||
public:
|
||||
enum ErrorCode
|
||||
{
|
||||
ERR_NONE,
|
||||
ERR_FAILOPEN,
|
||||
ERR_FAILMALLOC,
|
||||
ERR_FAILREAD,
|
||||
};
|
||||
|
||||
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);
|
||||
int extend(size_t new_size);;
|
||||
inline byte_t& operator[](size_t index) { return mData[index]; }
|
||||
inline const byte_t& operator[](size_t index) const { return mData[index]; }
|
||||
|
||||
inline byte_t* data() { return data_.data(); }
|
||||
inline const byte_t* data() const { return data_.data(); }
|
||||
inline size_t size() const { return apparent_size_; }
|
||||
inline byte_t* getBytes() { return mData.data(); }
|
||||
inline const byte_t* getBytes() const { return mData.data(); }
|
||||
inline size_t getSize() const { return mVisableSize; }
|
||||
private:
|
||||
std::vector<byte_t> data_;
|
||||
size_t size_;
|
||||
size_t apparent_size_;
|
||||
const std::string kModuleName = "MEMORY_BLOB";
|
||||
static const size_t kAllocBlockSize = 0x1000;
|
||||
|
||||
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();
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue