nod/lib/DiscIOISO.cpp

71 lines
1.8 KiB
C++
Raw Normal View History

2015-06-26 19:30:03 +00:00
#include <stdio.h>
#include <stdexcept>
#include "IDiscIO.hpp"
namespace NOD
{
2015-06-28 05:43:53 +00:00
class DiscIOISO : public IDiscIO
2015-06-26 19:30:03 +00:00
{
std::string filepath;
public:
2015-06-28 05:43:53 +00:00
DiscIOISO(const std::string& fpin)
2015-06-26 19:30:03 +00:00
: filepath(fpin) {}
2015-06-28 05:43:53 +00:00
class ReadStream : public IReadStream
2015-06-26 19:30:03 +00:00
{
2015-06-28 05:43:53 +00:00
friend class DiscIOISO;
2015-06-26 19:30:03 +00:00
FILE* fp;
2015-06-28 05:43:53 +00:00
ReadStream(FILE* fpin)
2015-06-26 19:30:03 +00:00
: fp(fpin) {}
2015-06-28 05:43:53 +00:00
~ReadStream() {fclose(fp);}
2015-06-26 19:30:03 +00:00
public:
size_t read(void* buf, size_t length)
{return fread(buf, 1, length, fp);}
2015-06-28 05:43:53 +00:00
void seek(size_t offset, int whence)
{fseek(fp, offset, whence);}
2015-06-26 19:30:03 +00:00
};
2015-06-28 05:43:53 +00:00
std::unique_ptr<IReadStream> beginReadStream(size_t offset) const
2015-06-26 19:30:03 +00:00
{
FILE* fp = fopen(filepath.c_str(), "rb");
if (!fp)
{
throw std::runtime_error("Unable to open '" + filepath + "' for reading");
return std::unique_ptr<IReadStream>();
}
fseek(fp, offset, SEEK_SET);
2015-06-28 05:43:53 +00:00
return std::unique_ptr<IReadStream>(new ReadStream(fp));
2015-06-26 19:30:03 +00:00
}
2015-06-28 05:43:53 +00:00
class WriteStream : public IWriteStream
2015-06-26 19:30:03 +00:00
{
2015-06-28 05:43:53 +00:00
friend class DiscIOISO;
2015-06-26 19:30:03 +00:00
FILE* fp;
2015-06-28 05:43:53 +00:00
WriteStream(FILE* fpin)
2015-06-26 19:30:03 +00:00
: fp(fpin) {}
2015-06-28 05:43:53 +00:00
~WriteStream() {fclose(fp);}
2015-06-26 19:30:03 +00:00
public:
size_t write(void* buf, size_t length)
{return fwrite(buf, 1, length, fp);}
};
2015-06-28 05:43:53 +00:00
std::unique_ptr<IWriteStream> beginWriteStream(size_t offset) const
2015-06-26 19:30:03 +00:00
{
FILE* fp = fopen(filepath.c_str(), "wb");
if (!fp)
{
throw std::runtime_error("Unable to open '" + filepath + "' for writing");
return std::unique_ptr<IWriteStream>();
}
fseek(fp, offset, SEEK_SET);
2015-06-28 05:43:53 +00:00
return std::unique_ptr<IWriteStream>(new WriteStream(fp));
2015-06-26 19:30:03 +00:00
}
};
2015-06-28 05:43:53 +00:00
std::unique_ptr<IDiscIO> NewDiscIOISO(const char* path)
2015-06-26 19:30:03 +00:00
{
2015-06-28 05:43:53 +00:00
return std::unique_ptr<IDiscIO>(new DiscIOISO(path));
2015-06-26 19:30:03 +00:00
}
}