2015-06-26 19:30:03 +00:00
|
|
|
#include <stdio.h>
|
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;
|
2016-01-25 05:55:31 +00:00
|
|
|
#if _WIN32
|
|
|
|
HANDLE fp;
|
|
|
|
ReadStream(HANDLE fpin)
|
|
|
|
: fp(fpin) {}
|
|
|
|
~ReadStream() {CloseHandle(fp);}
|
|
|
|
#else
|
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);}
|
2016-01-25 05:55:31 +00:00
|
|
|
#endif
|
2015-06-26 19:30:03 +00:00
|
|
|
public:
|
2016-01-25 05:55:31 +00:00
|
|
|
#if _WIN32
|
|
|
|
uint64_t read(void* buf, uint64_t length)
|
|
|
|
{
|
|
|
|
DWORD ret = 0;
|
|
|
|
ReadFile(fp, buf, length, &ret, nullptr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
uint64_t position() const
|
|
|
|
{
|
|
|
|
LARGE_INTEGER liOfs={0};
|
|
|
|
LARGE_INTEGER liNew={0};
|
|
|
|
SetFilePointerEx(fp, liOfs, &liNew, FILE_CURRENT);
|
|
|
|
return liNew.QuadPart;
|
|
|
|
}
|
|
|
|
void seek(int64_t offset, int whence)
|
|
|
|
{
|
|
|
|
LARGE_INTEGER li;
|
|
|
|
li.QuadPart = offset;
|
|
|
|
SetFilePointerEx(fp, li, nullptr, whence);
|
|
|
|
}
|
|
|
|
#else
|
2015-06-30 19:38:51 +00:00
|
|
|
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);}
|
2015-06-30 19:38:51 +00:00
|
|
|
void seek(int64_t offset, int whence)
|
2016-01-25 05:55:31 +00:00
|
|
|
{FSeek(fp, offset, whence);}
|
|
|
|
#endif
|
2015-06-26 19:30:03 +00:00
|
|
|
};
|
2016-01-25 05:55:31 +00:00
|
|
|
|
|
|
|
#if _WIN32
|
2015-06-30 19:38:51 +00:00
|
|
|
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const
|
2015-06-26 19:30:03 +00:00
|
|
|
{
|
2016-01-25 05:55:31 +00:00
|
|
|
HANDLE fp = CreateFileW(filepath.c_str(), GENERIC_READ, FILE_SHARE_READ,
|
|
|
|
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
|
|
|
if (fp == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
LogModule.report(LogVisor::Error, _S("Unable to open '%s' for reading"), filepath.c_str());
|
|
|
|
return std::unique_ptr<IReadStream>();
|
|
|
|
}
|
|
|
|
LARGE_INTEGER li;
|
|
|
|
li.QuadPart = offset;
|
|
|
|
SetFilePointerEx(fp, li, nullptr, FILE_BEGIN);
|
|
|
|
return std::unique_ptr<IReadStream>(new ReadStream(fp));
|
|
|
|
}
|
2015-07-02 18:33:55 +00:00
|
|
|
#else
|
2016-01-25 05:55:31 +00:00
|
|
|
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const
|
|
|
|
{
|
2015-06-26 19:30:03 +00:00
|
|
|
FILE* fp = fopen(filepath.c_str(), "rb");
|
|
|
|
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>();
|
|
|
|
}
|
2016-01-25 05:55:31 +00:00
|
|
|
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
|
|
|
}
|
2016-01-25 05:55:31 +00:00
|
|
|
#endif
|
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;
|
2016-01-25 05:55:31 +00:00
|
|
|
#if _WIN32
|
|
|
|
HANDLE fp;
|
|
|
|
WriteStream(HANDLE fpin)
|
|
|
|
: fp(fpin) {}
|
|
|
|
~WriteStream() {CloseHandle(fp);}
|
|
|
|
#else
|
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);}
|
2016-01-25 05:55:31 +00:00
|
|
|
#endif
|
2015-06-26 19:30:03 +00:00
|
|
|
public:
|
2016-01-25 05:55:31 +00:00
|
|
|
uint64_t write(const void* buf, uint64_t length)
|
|
|
|
{
|
|
|
|
#if _WIN32
|
|
|
|
DWORD ret = 0;
|
|
|
|
WriteFile(fp, buf, length, &ret, nullptr);
|
|
|
|
return ret;
|
|
|
|
#else
|
|
|
|
return fwrite(buf, 1, length, fp);
|
|
|
|
#endif
|
|
|
|
}
|
2015-06-26 19:30:03 +00:00
|
|
|
};
|
2016-01-25 05:55:31 +00:00
|
|
|
|
|
|
|
#if _WIN32
|
2015-06-30 19:38:51 +00:00
|
|
|
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const
|
2015-06-26 19:30:03 +00:00
|
|
|
{
|
2016-01-25 05:55:31 +00:00
|
|
|
HANDLE fp = CreateFileW(filepath.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE,
|
|
|
|
nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
|
|
|
|
if (fp == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
LogModule.report(LogVisor::Error, _S("Unable to open '%s' for writing"), filepath.c_str());
|
|
|
|
return std::unique_ptr<IWriteStream>();
|
|
|
|
}
|
|
|
|
LARGE_INTEGER li;
|
|
|
|
li.QuadPart = offset;
|
|
|
|
SetFilePointerEx(fp, li, nullptr, FILE_BEGIN);
|
|
|
|
return std::unique_ptr<IWriteStream>(new WriteStream(fp));
|
|
|
|
}
|
2015-07-02 18:33:55 +00:00
|
|
|
#else
|
2016-01-25 05:55:31 +00:00
|
|
|
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const
|
|
|
|
{
|
2015-06-26 19:30:03 +00:00
|
|
|
FILE* fp = fopen(filepath.c_str(), "wb");
|
|
|
|
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>();
|
|
|
|
}
|
2016-01-25 05:55:31 +00:00
|
|
|
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
|
|
|
}
|
2016-01-25 05:55:31 +00:00
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|