mirror of
https://github.com/AxioDL/nod.git
synced 2025-12-08 21:17:51 +00:00
Removed direct stdio usage
This commit is contained in:
@@ -2,11 +2,6 @@
|
||||
#include "NOD/Util.hpp"
|
||||
#include "NOD/IDiscIO.hpp"
|
||||
|
||||
#if _WIN32
|
||||
#define ftello _ftelli64
|
||||
#define fseeko _fseeki64
|
||||
#endif
|
||||
|
||||
namespace NOD
|
||||
{
|
||||
|
||||
@@ -20,60 +15,132 @@ public:
|
||||
class ReadStream : public IReadStream
|
||||
{
|
||||
friend class DiscIOISO;
|
||||
#if _WIN32
|
||||
HANDLE fp;
|
||||
ReadStream(HANDLE fpin)
|
||||
: fp(fpin) {}
|
||||
~ReadStream() {CloseHandle(fp);}
|
||||
#else
|
||||
FILE* fp;
|
||||
ReadStream(FILE* fpin)
|
||||
: fp(fpin) {}
|
||||
~ReadStream() {fclose(fp);}
|
||||
#endif
|
||||
public:
|
||||
#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
|
||||
uint64_t read(void* buf, uint64_t length)
|
||||
{return fread(buf, 1, length, fp);}
|
||||
uint64_t position() const
|
||||
{return ftello(fp);}
|
||||
void seek(int64_t offset, int whence)
|
||||
{fseeko(fp, offset, whence);}
|
||||
{FSeek(fp, offset, whence);}
|
||||
#endif
|
||||
};
|
||||
|
||||
#if _WIN32
|
||||
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const
|
||||
{
|
||||
#if NOD_UCS2
|
||||
FILE* fp = _wfopen(filepath.c_str(), L"rb");
|
||||
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));
|
||||
}
|
||||
#else
|
||||
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const
|
||||
{
|
||||
FILE* fp = fopen(filepath.c_str(), "rb");
|
||||
#endif
|
||||
if (!fp)
|
||||
{
|
||||
LogModule.report(LogVisor::Error, _S("Unable to open '%s' for reading"), filepath.c_str());
|
||||
return std::unique_ptr<IReadStream>();
|
||||
}
|
||||
fseeko(fp, offset, SEEK_SET);
|
||||
FSeek(fp, offset, SEEK_SET);
|
||||
return std::unique_ptr<IReadStream>(new ReadStream(fp));
|
||||
}
|
||||
#endif
|
||||
|
||||
class WriteStream : public IWriteStream
|
||||
{
|
||||
friend class DiscIOISO;
|
||||
#if _WIN32
|
||||
HANDLE fp;
|
||||
WriteStream(HANDLE fpin)
|
||||
: fp(fpin) {}
|
||||
~WriteStream() {CloseHandle(fp);}
|
||||
#else
|
||||
FILE* fp;
|
||||
WriteStream(FILE* fpin)
|
||||
: fp(fpin) {}
|
||||
~WriteStream() {fclose(fp);}
|
||||
#endif
|
||||
public:
|
||||
uint64_t write(void* buf, uint64_t length)
|
||||
{return fwrite(buf, 1, length, fp);}
|
||||
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
|
||||
}
|
||||
};
|
||||
|
||||
#if _WIN32
|
||||
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const
|
||||
{
|
||||
#if NOD_UCS2
|
||||
FILE* fp = _wfopen(filepath.c_str(), L"wb");
|
||||
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));
|
||||
}
|
||||
#else
|
||||
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const
|
||||
{
|
||||
FILE* fp = fopen(filepath.c_str(), "wb");
|
||||
#endif
|
||||
if (!fp)
|
||||
{
|
||||
LogModule.report(LogVisor::Error, _S("Unable to open '%s' for writing"), filepath.c_str());
|
||||
return std::unique_ptr<IWriteStream>();
|
||||
}
|
||||
fseeko(fp, offset, SEEK_SET);
|
||||
FSeek(fp, offset, SEEK_SET);
|
||||
return std::unique_ptr<IWriteStream>(new WriteStream(fp));
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
std::unique_ptr<IDiscIO> NewDiscIOISO(const SystemChar* path)
|
||||
|
||||
Reference in New Issue
Block a user