[fnd] Added IFile interface and SimpleFile implementation.

This commit is contained in:
jakcron 2018-04-15 10:36:23 +08:00
parent cca4a29fd6
commit 8a8fe7b0e8
3 changed files with 147 additions and 0 deletions

View file

@ -0,0 +1,14 @@
#pragma once
#include <fnd/types.h>
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;
};
}

View file

@ -0,0 +1,40 @@
#pragma once
#include <fnd/IFile.h>
#include <string>
#include <cstdio>
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);
};
}

View file

@ -0,0 +1,93 @@
#include <fnd/SimpleFile.h>
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;
}