From 8a8fe7b0e84495df1ff8d032efaf56ace88a3601 Mon Sep 17 00:00:00 2001 From: jakcron Date: Sun, 15 Apr 2018 10:36:23 +0800 Subject: [PATCH] [fnd] Added IFile interface and SimpleFile implementation. --- lib/libfnd/include/fnd/IFile.h | 14 +++++ lib/libfnd/include/fnd/SimpleFile.h | 40 +++++++++++++ lib/libfnd/source/SimpleFile.cpp | 93 +++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 lib/libfnd/include/fnd/IFile.h create mode 100644 lib/libfnd/include/fnd/SimpleFile.h create mode 100644 lib/libfnd/source/SimpleFile.cpp diff --git a/lib/libfnd/include/fnd/IFile.h b/lib/libfnd/include/fnd/IFile.h new file mode 100644 index 0000000..6a7cc56 --- /dev/null +++ b/lib/libfnd/include/fnd/IFile.h @@ -0,0 +1,14 @@ +#pragma once +#include + +namespace fnd +{ + class IFile + { + public: + virtual size_t size() = 0; + virtual void seek(size_t offset) = 0; + virtual void read(byte_t* out, size_t offset, size_t len) = 0; + virtual void write(const byte_t* out, size_t offset, size_t len) = 0; + }; +} diff --git a/lib/libfnd/include/fnd/SimpleFile.h b/lib/libfnd/include/fnd/SimpleFile.h new file mode 100644 index 0000000..bb25f7d --- /dev/null +++ b/lib/libfnd/include/fnd/SimpleFile.h @@ -0,0 +1,40 @@ +#pragma once +#include +#include +#include + +namespace fnd +{ + class SimpleFile : public IFile + { + public: + enum OpenMode + { + Read, + Edit, + Create + }; + + SimpleFile(); + ~SimpleFile(); + + void open(const std::string& path, OpenMode mode); + bool isOpen() const; + void close(); + size_t size(); + void seek(size_t offset); + size_t pos(); + void read(byte_t* out, size_t offset, size_t len); + void write(const byte_t* out, size_t offset, size_t len); + + private: + const std::string kModuleName = "SimpleFile"; + + bool mOpen; + OpenMode mMode; + FILE* mFp; + + const char* getOpenModeStr(OpenMode mMode); + }; +} + diff --git a/lib/libfnd/source/SimpleFile.cpp b/lib/libfnd/source/SimpleFile.cpp new file mode 100644 index 0000000..bb907d3 --- /dev/null +++ b/lib/libfnd/source/SimpleFile.cpp @@ -0,0 +1,93 @@ +#include + +using namespace fnd; + +SimpleFile::SimpleFile() : + mOpen(false), + mMode(Read), + mFp(nullptr) +{ +} + +SimpleFile::~SimpleFile() +{ + close(); +} + +void SimpleFile::open(const std::string& path, OpenMode mode) +{ + //close(); + mMode = mode; + //printf("fopen(%s,%s);\n", path.c_str(), getOpenModeStr(mMode)); + mFp = fopen(path.c_str(), getOpenModeStr(mMode)); + if (mFp == nullptr) + throw fnd::Exception(kModuleName, "Failed to open file."); + mOpen = true; + seek(0); +} + +bool SimpleFile::isOpen() const +{ + return mOpen == true && mFp != nullptr; +} + +void SimpleFile::close() +{ + if (isOpen()) + { + fclose(mFp); + } + mFp = nullptr; + mOpen = false; +} + +size_t SimpleFile::size() +{ + size_t cur_pos = pos(); + fseek(mFp, 0, SEEK_END); + size_t fsize = pos(); + seek(cur_pos); + return fsize; +} + +void SimpleFile::seek(size_t offset) +{ + fseek(mFp, offset, SEEK_SET); +} + +size_t SimpleFile::pos() +{ + return ftell(mFp); +} + +void SimpleFile::read(byte_t* out, size_t offset, size_t len) +{ + seek(offset); + fread(out, len, 1, mFp); +} + +void SimpleFile::write(const byte_t* out, size_t offset, size_t len) +{ + seek(offset); + fwrite(out, len, 1, mFp); +} + +const char* SimpleFile::getOpenModeStr(OpenMode mode) +{ + const char* str = ""; + switch(mode) + { + case (Read): + str = "rb"; + break; + case (Edit): + str = "rb+"; + break; + case (Create): + str = "wb"; + break; + default: + throw fnd::Exception(kModuleName, "Unknown open mode"); + } + return str; +} \ No newline at end of file