2018-04-15 02:36:23 +00:00
|
|
|
#include <fnd/SimpleFile.h>
|
2018-05-25 14:54:30 +00:00
|
|
|
#include <fnd/StringConv.h>
|
2018-04-15 02:36:23 +00:00
|
|
|
|
|
|
|
using namespace fnd;
|
|
|
|
|
|
|
|
SimpleFile::SimpleFile() :
|
|
|
|
mOpen(false),
|
|
|
|
mMode(Read),
|
2018-05-25 14:54:30 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
mFileHandle()
|
|
|
|
#else
|
2018-04-15 02:36:23 +00:00
|
|
|
mFp(nullptr)
|
2018-05-25 14:54:30 +00:00
|
|
|
#endif
|
2018-04-15 02:36:23 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleFile::~SimpleFile()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleFile::open(const std::string& path, OpenMode mode)
|
|
|
|
{
|
2018-05-25 14:54:30 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
// convert string to unicode
|
|
|
|
std::u16string unicodePath = fnd::StringConv::ConvertChar8ToChar16(path);
|
|
|
|
|
|
|
|
// save mode
|
|
|
|
mMode = mode;
|
|
|
|
|
|
|
|
// open file
|
|
|
|
mFileHandle = CreateFileW((LPCWSTR)unicodePath.c_str(),
|
|
|
|
getOpenModeFlag(mMode),
|
|
|
|
getShareModeFlag(mMode),
|
|
|
|
0,
|
|
|
|
getCreationModeFlag(mMode),
|
|
|
|
FILE_ATTRIBUTE_NORMAL,
|
|
|
|
NULL);
|
|
|
|
// check file handle
|
|
|
|
if (mFileHandle == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "Failed to open file.");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#else
|
2018-04-15 02:36:23 +00:00
|
|
|
//close();
|
|
|
|
mMode = mode;
|
|
|
|
//printf("fopen(%s,%s);\n", path.c_str(), getOpenModeStr(mMode));
|
|
|
|
mFp = fopen(path.c_str(), getOpenModeStr(mMode));
|
|
|
|
if (mFp == nullptr)
|
|
|
|
throw fnd::Exception(kModuleName, "Failed to open file.");
|
|
|
|
mOpen = true;
|
2018-05-25 14:54:30 +00:00
|
|
|
#endif
|
|
|
|
|
2018-04-15 02:36:23 +00:00
|
|
|
seek(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SimpleFile::isOpen() const
|
|
|
|
{
|
2018-05-25 14:54:30 +00:00
|
|
|
return mOpen == true;
|
2018-04-15 02:36:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleFile::close()
|
|
|
|
{
|
|
|
|
if (isOpen())
|
|
|
|
{
|
2018-05-25 14:54:30 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
CloseHandle(mFileHandle);
|
|
|
|
#else
|
2018-04-15 02:36:23 +00:00
|
|
|
fclose(mFp);
|
2018-05-25 14:54:30 +00:00
|
|
|
mFp = nullptr;
|
|
|
|
#endif
|
2018-04-15 02:36:23 +00:00
|
|
|
}
|
|
|
|
mOpen = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t SimpleFile::size()
|
|
|
|
{
|
2018-05-25 14:54:30 +00:00
|
|
|
size_t fsize = 0;
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (mMode != Create)
|
|
|
|
{
|
|
|
|
LARGE_INTEGER win_fsize;
|
|
|
|
if (GetFileSizeEx(mFileHandle, &win_fsize) == false)
|
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "Failed to check filesize");
|
|
|
|
}
|
|
|
|
|
|
|
|
fsize = win_fsize.QuadPart;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fsize = 0;
|
|
|
|
}
|
|
|
|
#else
|
2018-04-15 02:36:23 +00:00
|
|
|
size_t cur_pos = pos();
|
|
|
|
fseek(mFp, 0, SEEK_END);
|
2018-05-25 14:54:30 +00:00
|
|
|
fsize = pos();
|
2018-04-15 02:36:23 +00:00
|
|
|
seek(cur_pos);
|
2018-05-25 14:54:30 +00:00
|
|
|
#endif
|
2018-04-15 02:36:23 +00:00
|
|
|
return fsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleFile::seek(size_t offset)
|
|
|
|
{
|
2018-05-25 14:54:30 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
LARGE_INTEGER win_pos, out;
|
|
|
|
win_pos.QuadPart = offset;
|
|
|
|
if (SetFilePointerEx(
|
|
|
|
mFileHandle,
|
|
|
|
win_pos,
|
|
|
|
&out,
|
|
|
|
FILE_BEGIN
|
|
|
|
) == false || out.QuadPart != win_pos.QuadPart)
|
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "Failed to change file offset");
|
|
|
|
}
|
|
|
|
#else
|
2018-04-15 02:36:23 +00:00
|
|
|
fseek(mFp, offset, SEEK_SET);
|
2018-05-25 14:54:30 +00:00
|
|
|
#endif
|
2018-04-15 02:36:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t SimpleFile::pos()
|
|
|
|
{
|
2018-05-25 14:54:30 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
LARGE_INTEGER win_pos, out;
|
|
|
|
win_pos.QuadPart = 0;
|
|
|
|
if (SetFilePointerEx(
|
|
|
|
mFileHandle,
|
|
|
|
win_pos,
|
|
|
|
&out,
|
|
|
|
FILE_CURRENT
|
|
|
|
) == false)
|
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "Failed to check file offset");
|
|
|
|
}
|
|
|
|
|
|
|
|
return out.QuadPart;
|
|
|
|
#else
|
2018-04-15 02:36:23 +00:00
|
|
|
return ftell(mFp);
|
2018-05-25 14:54:30 +00:00
|
|
|
#endif
|
2018-04-15 02:36:23 +00:00
|
|
|
}
|
|
|
|
|
2018-04-15 06:01:24 +00:00
|
|
|
void SimpleFile::read(byte_t* out, size_t len)
|
|
|
|
{
|
2018-05-25 14:54:30 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
LARGE_INTEGER win_len;
|
|
|
|
win_len.QuadPart = len;
|
|
|
|
|
|
|
|
static const DWORD kDwordHalf = (MAXDWORD / (DWORD)2) + 1; // 0x80000000
|
|
|
|
static const size_t kDwordFull = (size_t)kDwordHalf * (size_t)2; // 0x100000000
|
|
|
|
|
|
|
|
// if the size is greater than a DWORD, read it in parts,
|
|
|
|
for (LONG i = 0; i < win_len.HighPart; i++)
|
|
|
|
{
|
|
|
|
// since kDwordFull isn't a valid DWORD value, read in two parts
|
|
|
|
ReadFile(
|
|
|
|
mFileHandle,
|
|
|
|
out + i * kDwordFull,
|
|
|
|
kDwordHalf,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
ReadFile(
|
|
|
|
mFileHandle,
|
|
|
|
out + i * kDwordFull + kDwordHalf,
|
|
|
|
kDwordHalf,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// read remainding low part
|
|
|
|
if (win_len.LowPart > 0)
|
|
|
|
{
|
|
|
|
ReadFile(
|
|
|
|
mFileHandle,
|
|
|
|
out + win_len.HighPart * kDwordFull,
|
|
|
|
win_len.LowPart,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
}
|
|
|
|
#else
|
2018-04-15 06:01:24 +00:00
|
|
|
fread(out, len, 1, mFp);
|
2018-05-25 14:54:30 +00:00
|
|
|
#endif
|
2018-04-15 06:01:24 +00:00
|
|
|
}
|
|
|
|
|
2018-04-15 02:36:23 +00:00
|
|
|
void SimpleFile::read(byte_t* out, size_t offset, size_t len)
|
|
|
|
{
|
|
|
|
seek(offset);
|
2018-04-15 06:01:24 +00:00
|
|
|
read(out, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleFile::write(const byte_t* out, size_t len)
|
|
|
|
{
|
2018-05-25 14:54:30 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
LARGE_INTEGER win_len;
|
|
|
|
win_len.QuadPart = len;
|
|
|
|
|
|
|
|
static const DWORD kDwordHalf = ((DWORD)MAXDWORD / (DWORD)2) + 1; // 0x80000000
|
|
|
|
static const size_t kDwordFull = (size_t)kDwordHalf * (size_t)2; // 0x100000000
|
|
|
|
|
|
|
|
// if the size is greater than a DWORD, read it in parts,
|
|
|
|
for (LONG i = 0; i < win_len.HighPart; i++)
|
|
|
|
{
|
|
|
|
// since kDwordFull isn't a valid DWORD value, read in two parts
|
|
|
|
WriteFile(
|
|
|
|
mFileHandle,
|
|
|
|
out + i * kDwordFull,
|
|
|
|
kDwordHalf,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
WriteFile(
|
|
|
|
mFileHandle,
|
|
|
|
out + i * kDwordFull + kDwordHalf,
|
|
|
|
kDwordHalf,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// read remainding low part
|
|
|
|
if (win_len.LowPart > 0)
|
|
|
|
{
|
|
|
|
WriteFile(
|
|
|
|
mFileHandle,
|
|
|
|
out + win_len.HighPart * kDwordFull,
|
|
|
|
win_len.LowPart,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
}
|
|
|
|
#else
|
2018-04-15 06:01:24 +00:00
|
|
|
fwrite(out, len, 1, mFp);
|
2018-05-25 14:54:30 +00:00
|
|
|
#endif
|
2018-04-15 02:36:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleFile::write(const byte_t* out, size_t offset, size_t len)
|
|
|
|
{
|
|
|
|
seek(offset);
|
2018-04-15 06:01:24 +00:00
|
|
|
write(out, len);
|
2018-04-15 02:36:23 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 14:54:30 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD SimpleFile::getOpenModeFlag(OpenMode mode) const
|
|
|
|
{
|
|
|
|
DWORD flag = 0;
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case (Read):
|
|
|
|
flag = GENERIC_READ;
|
|
|
|
break;
|
|
|
|
case (Edit):
|
|
|
|
flag = GENERIC_READ | GENERIC_WRITE;
|
|
|
|
break;
|
|
|
|
case (Create):
|
|
|
|
flag = GENERIC_WRITE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw fnd::Exception(kModuleName, "Unknown open mode");
|
|
|
|
}
|
|
|
|
return flag;
|
|
|
|
}
|
|
|
|
DWORD fnd::SimpleFile::getShareModeFlag(OpenMode mode) const
|
|
|
|
{
|
|
|
|
DWORD flag = 0;
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case (Read):
|
|
|
|
flag = FILE_SHARE_READ;
|
|
|
|
break;
|
|
|
|
case (Edit):
|
|
|
|
flag = FILE_SHARE_READ;
|
|
|
|
break;
|
|
|
|
case (Create):
|
|
|
|
flag = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw fnd::Exception(kModuleName, "Unknown open mode");
|
|
|
|
}
|
|
|
|
return flag;
|
|
|
|
}
|
|
|
|
DWORD fnd::SimpleFile::getCreationModeFlag(OpenMode mode) const
|
|
|
|
{
|
|
|
|
DWORD flag = 0;
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case (Read):
|
|
|
|
flag = OPEN_EXISTING;
|
|
|
|
break;
|
|
|
|
case (Edit):
|
|
|
|
flag = OPEN_EXISTING;
|
|
|
|
break;
|
|
|
|
case (Create):
|
|
|
|
flag = CREATE_ALWAYS;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw fnd::Exception(kModuleName, "Unknown open mode");
|
|
|
|
}
|
|
|
|
return flag;
|
|
|
|
}
|
|
|
|
#else
|
2018-04-15 02:36:23 +00:00
|
|
|
const char* SimpleFile::getOpenModeStr(OpenMode mode)
|
|
|
|
{
|
|
|
|
const char* str = "";
|
|
|
|
switch(mode)
|
|
|
|
{
|
|
|
|
case (Read):
|
|
|
|
str = "rb";
|
|
|
|
break;
|
|
|
|
case (Edit):
|
|
|
|
str = "rb+";
|
|
|
|
break;
|
|
|
|
case (Create):
|
|
|
|
str = "wb";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw fnd::Exception(kModuleName, "Unknown open mode");
|
|
|
|
}
|
|
|
|
return str;
|
2018-05-25 14:54:30 +00:00
|
|
|
}
|
|
|
|
#endif
|