From fa4bdd7143dbbe789302295dfdaebfb1d74de6ed Mon Sep 17 00:00:00 2001 From: jakcron Date: Thu, 6 Jul 2017 13:27:25 +1000 Subject: [PATCH] [fnd] Replace FileIO singleton with io namespace. Changed style of MemoryBlob, gave it exceptions. --- lib/fnd/file_io.h | 20 ------------- lib/fnd/fnd.vcxproj | 4 +-- lib/fnd/fnd.vcxproj.filters | 12 ++++---- lib/fnd/{file_io.cpp => io.cpp} | 23 ++++++++------- lib/fnd/io.h | 13 +++++++++ lib/fnd/memory_blob.cpp | 50 ++++++++++++++++----------------- lib/fnd/memory_blob.h | 34 ++++++++++------------ 7 files changed, 73 insertions(+), 83 deletions(-) delete mode 100644 lib/fnd/file_io.h rename lib/fnd/{file_io.cpp => io.cpp} (58%) create mode 100644 lib/fnd/io.h diff --git a/lib/fnd/file_io.h b/lib/fnd/file_io.h deleted file mode 100644 index 2479b93..0000000 --- a/lib/fnd/file_io.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include -#include - -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: - - }; -} - - diff --git a/lib/fnd/fnd.vcxproj b/lib/fnd/fnd.vcxproj index cdb225d..26ed2dd 100644 --- a/lib/fnd/fnd.vcxproj +++ b/lib/fnd/fnd.vcxproj @@ -116,15 +116,15 @@ - + - + diff --git a/lib/fnd/fnd.vcxproj.filters b/lib/fnd/fnd.vcxproj.filters index e5f3e70..66964fe 100644 --- a/lib/fnd/fnd.vcxproj.filters +++ b/lib/fnd/fnd.vcxproj.filters @@ -18,9 +18,6 @@ Header Files - - Header Files - Header Files @@ -33,11 +30,11 @@ Header Files + + Header Files + - - Source Files - Source Files @@ -47,6 +44,9 @@ Source Files + + Source Files + diff --git a/lib/fnd/file_io.cpp b/lib/fnd/io.cpp similarity index 58% rename from lib/fnd/file_io.cpp rename to lib/fnd/io.cpp index 803350d..b01b337 100644 --- a/lib/fnd/file_io.cpp +++ b/lib/fnd/io.cpp @@ -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; diff --git a/lib/fnd/io.h b/lib/fnd/io.h new file mode 100644 index 0000000..28fa7ef --- /dev/null +++ b/lib/fnd/io.h @@ -0,0 +1,13 @@ +#pragma once +#include +#include + +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); + } +} diff --git a/lib/fnd/memory_blob.cpp b/lib/fnd/memory_blob.cpp index eebcf65..8e7f4b6 100644 --- a/lib/fnd/memory_blob.cpp +++ b/lib/fnd/memory_blob.cpp @@ -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); } diff --git a/lib/fnd/memory_blob.h b/lib/fnd/memory_blob.h index 09a6155..1b9abae 100644 --- a/lib/fnd/memory_blob.h +++ b/lib/fnd/memory_blob.h @@ -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 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 mData; + size_t mSize; + size_t mVisableSize; - void ClearMemory(); + void allocateMemory(size_t size); + void clearMemory(); }; }