2015-06-30 06:46:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-07-02 18:33:55 +00:00
|
|
|
#include "NOD/Util.hpp"
|
|
|
|
#include "NOD/IFileIO.hpp"
|
2015-06-28 05:43:53 +00:00
|
|
|
|
2015-07-22 19:01:28 +00:00
|
|
|
#if _WIN32
|
|
|
|
#define ftello _ftelli64
|
|
|
|
#define fseeko _fseeki64
|
|
|
|
#endif
|
|
|
|
|
2015-06-30 06:46:19 +00:00
|
|
|
/* Macros for min/max */
|
|
|
|
#define MIN(a,b) (((a)<(b))?(a):(b))
|
|
|
|
#define MAX(a,b) (((a)>(b))?(a):(b))
|
|
|
|
|
2015-06-28 05:43:53 +00:00
|
|
|
namespace NOD
|
|
|
|
{
|
|
|
|
|
|
|
|
class FileIOFILE : public IFileIO
|
|
|
|
{
|
2015-07-02 18:33:55 +00:00
|
|
|
SystemString m_path;
|
2015-06-28 05:43:53 +00:00
|
|
|
public:
|
2015-07-02 18:33:55 +00:00
|
|
|
FileIOFILE(const SystemString& path)
|
2015-06-30 06:46:19 +00:00
|
|
|
: m_path(path) {}
|
2016-01-21 06:30:37 +00:00
|
|
|
FileIOFILE(const SystemChar* path)
|
|
|
|
: m_path(path) {}
|
2015-06-30 06:46:19 +00:00
|
|
|
|
2015-06-30 19:38:51 +00:00
|
|
|
uint64_t size()
|
2015-06-28 05:43:53 +00:00
|
|
|
{
|
2015-07-02 18:33:55 +00:00
|
|
|
#if NOD_UCS2
|
2015-07-02 20:37:07 +00:00
|
|
|
FILE* fp = _wfopen(m_path.c_str(), L"rb");
|
2015-07-02 18:33:55 +00:00
|
|
|
#else
|
2015-06-30 06:46:19 +00:00
|
|
|
FILE* fp = fopen(m_path.c_str(), "rb");
|
2015-07-02 18:33:55 +00:00
|
|
|
#endif
|
2015-06-30 06:46:19 +00:00
|
|
|
if (!fp)
|
|
|
|
return 0;
|
2015-06-30 19:38:51 +00:00
|
|
|
fseeko(fp, 0, SEEK_END);
|
|
|
|
uint64_t result = ftello(fp);
|
2015-06-30 06:46:19 +00:00
|
|
|
fclose(fp);
|
|
|
|
return result;
|
2015-06-28 05:43:53 +00:00
|
|
|
}
|
2015-06-30 06:46:19 +00:00
|
|
|
|
|
|
|
struct WriteStream : public IFileIO::IWriteStream
|
|
|
|
{
|
|
|
|
uint8_t buf[0x7c00];
|
2016-01-21 06:30:37 +00:00
|
|
|
FILE* fp;
|
2015-07-02 18:33:55 +00:00
|
|
|
WriteStream(const SystemString& path)
|
2015-06-30 06:46:19 +00:00
|
|
|
{
|
2015-07-02 18:33:55 +00:00
|
|
|
#if NOD_UCS2
|
2015-07-02 20:37:07 +00:00
|
|
|
fp = _wfopen(path.c_str(), L"wb");
|
2015-07-02 18:33:55 +00:00
|
|
|
#else
|
2015-06-30 06:46:19 +00:00
|
|
|
fp = fopen(path.c_str(), "wb");
|
2015-07-02 18:33:55 +00:00
|
|
|
#endif
|
2015-06-30 06:46:19 +00:00
|
|
|
if (!fp)
|
2015-07-26 02:44:44 +00:00
|
|
|
LogModule.report(LogVisor::Error, _S("unable to open '%s' for writing"), path.c_str());
|
2015-06-30 06:46:19 +00:00
|
|
|
}
|
2016-01-22 02:30:17 +00:00
|
|
|
WriteStream(const SystemString& path, uint64_t offset)
|
2016-01-21 06:30:37 +00:00
|
|
|
{
|
|
|
|
#if NOD_UCS2
|
2016-01-22 23:45:58 +00:00
|
|
|
fp = _wfopen(path.c_str(), L"ab");
|
|
|
|
fclose(fp);
|
2016-01-21 06:30:37 +00:00
|
|
|
fp = _wfopen(path.c_str(), L"r+b");
|
|
|
|
#else
|
2016-01-22 23:45:58 +00:00
|
|
|
fp = fopen(path.c_str(), "ab");
|
|
|
|
fclose(fp);
|
2016-01-21 06:30:37 +00:00
|
|
|
fp = fopen(path.c_str(), "r+b");
|
|
|
|
#endif
|
|
|
|
if (!fp)
|
|
|
|
LogModule.report(LogVisor::Error, _S("unable to open '%s' for writing"), path.c_str());
|
2016-01-23 23:36:58 +00:00
|
|
|
FSeek(fp, offset, SEEK_SET);
|
2016-01-21 06:30:37 +00:00
|
|
|
}
|
|
|
|
~WriteStream()
|
|
|
|
{
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
uint64_t write(const void* buf, uint64_t length)
|
|
|
|
{
|
|
|
|
return fwrite(buf, 1, length, fp);
|
|
|
|
}
|
2015-06-30 19:38:51 +00:00
|
|
|
uint64_t copyFromDisc(IPartReadStream& discio, uint64_t length)
|
2015-06-30 06:46:19 +00:00
|
|
|
{
|
2015-06-30 19:38:51 +00:00
|
|
|
uint64_t read = 0;
|
2015-06-30 06:46:19 +00:00
|
|
|
while (length)
|
|
|
|
{
|
2015-06-30 19:38:51 +00:00
|
|
|
uint64_t thisSz = MIN(0x7c00, length);
|
|
|
|
uint64_t readSz = discio.read(buf, thisSz);
|
2015-06-30 06:46:19 +00:00
|
|
|
if (thisSz != readSz)
|
2015-07-26 02:44:44 +00:00
|
|
|
{
|
|
|
|
LogModule.report(LogVisor::Error, "unable to read enough from disc");
|
|
|
|
return read;
|
|
|
|
}
|
2015-06-30 06:46:19 +00:00
|
|
|
if (fwrite(buf, 1, readSz, fp) != readSz)
|
2015-07-26 02:44:44 +00:00
|
|
|
{
|
|
|
|
LogModule.report(LogVisor::Error, "unable to write in file");
|
|
|
|
return read;
|
|
|
|
}
|
2015-06-30 06:46:19 +00:00
|
|
|
length -= thisSz;
|
|
|
|
read += thisSz;
|
|
|
|
}
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
std::unique_ptr<IWriteStream> beginWriteStream() const
|
2016-01-21 06:30:37 +00:00
|
|
|
{
|
|
|
|
return std::unique_ptr<IWriteStream>(new WriteStream(m_path));
|
|
|
|
}
|
2016-01-22 02:30:17 +00:00
|
|
|
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const
|
2016-01-21 06:30:37 +00:00
|
|
|
{
|
|
|
|
return std::unique_ptr<IWriteStream>(new WriteStream(m_path, offset));
|
|
|
|
}
|
2015-06-30 06:46:19 +00:00
|
|
|
|
|
|
|
struct ReadStream : public IFileIO::IReadStream
|
|
|
|
{
|
|
|
|
FILE* fp;
|
|
|
|
uint8_t buf[0x7c00];
|
2015-07-02 18:33:55 +00:00
|
|
|
ReadStream(const SystemString& path)
|
2015-06-30 06:46:19 +00:00
|
|
|
{
|
2015-07-02 18:33:55 +00:00
|
|
|
#if NOD_UCS2
|
2015-07-02 20:37:07 +00:00
|
|
|
fp = _wfopen(path.c_str(), L"rb");
|
2015-07-02 18:33:55 +00:00
|
|
|
#else
|
2015-06-30 06:46:19 +00:00
|
|
|
fp = fopen(path.c_str(), "rb");
|
2015-07-02 18:33:55 +00:00
|
|
|
#endif
|
2015-06-30 06:46:19 +00:00
|
|
|
if (!fp)
|
2015-07-26 02:44:44 +00:00
|
|
|
LogModule.report(LogVisor::Error, _S("unable to open '%s' for reading"), path.c_str());
|
2015-06-30 06:46:19 +00:00
|
|
|
}
|
2016-01-22 02:30:17 +00:00
|
|
|
ReadStream(const SystemString& path, uint64_t offset)
|
2016-01-21 06:30:37 +00:00
|
|
|
: ReadStream(path)
|
|
|
|
{
|
2016-01-23 23:36:58 +00:00
|
|
|
FSeek(fp, offset, SEEK_SET);
|
2016-01-21 06:30:37 +00:00
|
|
|
}
|
2015-06-30 06:46:19 +00:00
|
|
|
~ReadStream() {fclose(fp);}
|
2015-06-30 19:38:51 +00:00
|
|
|
uint64_t read(void* buf, uint64_t length)
|
|
|
|
{return fread(buf, 1, length, fp);}
|
|
|
|
uint64_t copyToDisc(IPartWriteStream& discio, uint64_t length)
|
2015-06-30 06:46:19 +00:00
|
|
|
{
|
2015-06-30 19:38:51 +00:00
|
|
|
uint64_t written = 0;
|
2015-06-30 06:46:19 +00:00
|
|
|
while (length)
|
|
|
|
{
|
2015-06-30 19:38:51 +00:00
|
|
|
uint64_t thisSz = MIN(0x7c00, length);
|
2015-06-30 06:46:19 +00:00
|
|
|
if (fread(buf, 1, thisSz, fp) != thisSz)
|
2015-07-26 02:44:44 +00:00
|
|
|
{
|
|
|
|
LogModule.report(LogVisor::Error, "unable to read enough from file");
|
|
|
|
return written;
|
|
|
|
}
|
2015-06-30 06:46:19 +00:00
|
|
|
if (discio.write(buf, thisSz) != thisSz)
|
2015-07-26 02:44:44 +00:00
|
|
|
{
|
|
|
|
LogModule.report(LogVisor::Error, "unable to write enough to disc");
|
|
|
|
return written;
|
|
|
|
}
|
2015-06-30 06:46:19 +00:00
|
|
|
length -= thisSz;
|
|
|
|
written += thisSz;
|
|
|
|
}
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
std::unique_ptr<IReadStream> beginReadStream() const
|
2016-01-21 06:30:37 +00:00
|
|
|
{
|
|
|
|
return std::unique_ptr<IReadStream>(new ReadStream(m_path));
|
|
|
|
}
|
2016-01-22 02:30:17 +00:00
|
|
|
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const
|
2016-01-21 06:30:37 +00:00
|
|
|
{
|
|
|
|
return std::unique_ptr<IReadStream>(new ReadStream(m_path, offset));
|
|
|
|
}
|
2015-06-28 05:43:53 +00:00
|
|
|
};
|
|
|
|
|
2015-07-02 18:33:55 +00:00
|
|
|
std::unique_ptr<IFileIO> NewFileIO(const SystemString& path)
|
2015-06-30 06:46:19 +00:00
|
|
|
{
|
|
|
|
return std::unique_ptr<IFileIO>(new FileIOFILE(path));
|
|
|
|
}
|
|
|
|
|
2016-01-21 06:30:37 +00:00
|
|
|
std::unique_ptr<IFileIO> NewFileIO(const SystemChar* path)
|
|
|
|
{
|
|
|
|
return std::unique_ptr<IFileIO>(new FileIOFILE(path));
|
|
|
|
}
|
|
|
|
|
2015-06-28 05:43:53 +00:00
|
|
|
}
|