nod/lib/DiscIOISO.cpp

82 lines
2.1 KiB
C++
Raw Normal View History

2015-06-26 19:30:03 +00:00
#include <stdio.h>
#include <stdexcept>
2015-07-02 18:33:55 +00:00
#include "NOD/Util.hpp"
#include "NOD/IDiscIO.hpp"
2015-06-26 19:30:03 +00:00
namespace NOD
{
2015-06-28 05:43:53 +00:00
class DiscIOISO : public IDiscIO
2015-06-26 19:30:03 +00:00
{
2015-07-02 18:33:55 +00:00
SystemString filepath;
2015-06-26 19:30:03 +00:00
public:
2015-07-02 18:33:55 +00:00
DiscIOISO(const SystemString& 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:
uint64_t read(void* buf, uint64_t length)
2015-06-26 19:30:03 +00:00
{return fread(buf, 1, length, fp);}
2015-07-07 03:22:19 +00:00
uint64_t position() const
{return ftello(fp);}
void seek(int64_t offset, int whence)
{fseeko(fp, offset, whence);}
2015-06-26 19:30:03 +00:00
};
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const
2015-06-26 19:30:03 +00:00
{
2015-07-02 18:33:55 +00:00
#if NOD_UCS2
2015-07-02 20:37:07 +00:00
FILE* fp = _wfopen(filepath.c_str(), L"rb");
2015-07-02 18:33:55 +00:00
#else
2015-06-26 19:30:03 +00:00
FILE* fp = fopen(filepath.c_str(), "rb");
2015-07-02 18:33:55 +00:00
#endif
2015-06-26 19:30:03 +00:00
if (!fp)
{
2015-07-04 22:02:37 +00:00
LogModule.report(LogVisor::Error, _S("Unable to open '%s' for reading"), filepath.c_str());
2015-06-26 19:30:03 +00:00
return std::unique_ptr<IReadStream>();
}
fseeko(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:
uint64_t write(void* buf, uint64_t length)
2015-06-26 19:30:03 +00:00
{return fwrite(buf, 1, length, fp);}
};
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const
2015-06-26 19:30:03 +00:00
{
2015-07-02 18:33:55 +00:00
#if NOD_UCS2
2015-07-02 20:37:07 +00:00
FILE* fp = _wfopen(filepath.c_str(), L"wb");
2015-07-02 18:33:55 +00:00
#else
2015-06-26 19:30:03 +00:00
FILE* fp = fopen(filepath.c_str(), "wb");
2015-07-02 18:33:55 +00:00
#endif
2015-06-26 19:30:03 +00:00
if (!fp)
{
2015-07-04 22:02:37 +00:00
LogModule.report(LogVisor::Error, _S("Unable to open '%s' for writing"), filepath.c_str());
2015-06-26 19:30:03 +00:00
return std::unique_ptr<IWriteStream>();
}
fseeko(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-07-02 18:33:55 +00:00
std::unique_ptr<IDiscIO> NewDiscIOISO(const SystemChar* 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
}
}