2008-09-18 03:15:49 +00:00
|
|
|
#include "stdafx.h"
|
2016-01-12 19:09:01 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#define USE_WINDOWS_API
|
2016-01-12 19:48:07 +00:00
|
|
|
#include <Windows.h>
|
2016-01-12 19:09:01 +00:00
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-01-12 19:09:01 +00:00
|
|
|
#if defined(ANDROID)
|
|
|
|
#include <android/log.h>
|
|
|
|
|
|
|
|
#define printf(...) __android_log_print(ANDROID_LOG_VERBOSE, "UI-Console", __VA_ARGS__)
|
|
|
|
#endif
|
2015-05-03 11:05:53 +00:00
|
|
|
|
2015-11-17 18:31:36 +00:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#include <crtdbg.h>
|
|
|
|
#else
|
|
|
|
#define _ASSERTE(expr) ((void)0)
|
|
|
|
#endif
|
|
|
|
|
2008-09-18 03:15:49 +00:00
|
|
|
CFile::CFile() :
|
2016-01-12 19:09:01 +00:00
|
|
|
#ifdef USE_WINDOWS_API
|
2016-01-12 19:48:07 +00:00
|
|
|
m_hFile(INVALID_HANDLE_VALUE),
|
2016-01-12 19:09:01 +00:00
|
|
|
#else
|
2016-01-12 19:48:07 +00:00
|
|
|
m_hFile(NULL),
|
2016-01-12 19:09:01 +00:00
|
|
|
#endif
|
2016-01-12 19:48:07 +00:00
|
|
|
m_bCloseOnDelete(false)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-01-12 19:09:01 +00:00
|
|
|
CFile::CFile(void * hFile) :
|
2016-01-12 19:48:07 +00:00
|
|
|
m_hFile(hFile),
|
|
|
|
m_bCloseOnDelete(true)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2015-10-25 10:50:28 +00:00
|
|
|
if (hFile == 0)
|
|
|
|
{
|
|
|
|
_ASSERTE(hFile != 0);
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 19:09:01 +00:00
|
|
|
CFile::CFile(const char * lpszFileName, uint32_t nOpenFlags) :
|
|
|
|
#ifdef USE_WINDOWS_API
|
2016-01-12 19:48:07 +00:00
|
|
|
m_hFile(INVALID_HANDLE_VALUE),
|
2016-01-12 19:09:01 +00:00
|
|
|
#else
|
2016-01-12 19:48:07 +00:00
|
|
|
m_hFile(NULL),
|
2016-01-12 19:09:01 +00:00
|
|
|
#endif
|
2016-01-12 19:48:07 +00:00
|
|
|
m_bCloseOnDelete(true)
|
2016-01-12 19:09:01 +00:00
|
|
|
{
|
|
|
|
Open(lpszFileName, nOpenFlags);
|
|
|
|
}
|
|
|
|
|
2008-09-18 03:15:49 +00:00
|
|
|
CFile::~CFile()
|
|
|
|
{
|
2016-01-12 19:09:01 +00:00
|
|
|
#ifdef USE_WINDOWS_API
|
2015-10-25 10:50:28 +00:00
|
|
|
if (m_hFile != INVALID_HANDLE_VALUE && m_bCloseOnDelete)
|
2016-01-12 19:09:01 +00:00
|
|
|
#else
|
|
|
|
if (m_hFile != NULL && m_bCloseOnDelete)
|
|
|
|
#endif
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
Close();
|
2016-01-12 19:48:07 +00:00
|
|
|
}
|
2015-10-25 10:50:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CFile::Open(const char * lpszFileName, uint32_t nOpenFlags)
|
|
|
|
{
|
|
|
|
if (!Close())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-12 19:09:01 +00:00
|
|
|
if (lpszFileName == NULL || strlen(lpszFileName) == 0)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_bCloseOnDelete = true;
|
2016-01-12 19:09:01 +00:00
|
|
|
#ifdef USE_WINDOWS_API
|
2015-10-25 10:50:28 +00:00
|
|
|
m_hFile = INVALID_HANDLE_VALUE;
|
|
|
|
|
|
|
|
ULONG dwAccess = 0;
|
|
|
|
switch (nOpenFlags & 3)
|
|
|
|
{
|
|
|
|
case modeRead:
|
|
|
|
dwAccess = GENERIC_READ;
|
|
|
|
break;
|
|
|
|
case modeWrite:
|
|
|
|
dwAccess = GENERIC_WRITE;
|
|
|
|
break;
|
|
|
|
case modeReadWrite:
|
2016-01-12 19:48:07 +00:00
|
|
|
dwAccess = GENERIC_READ | GENERIC_WRITE;
|
2015-10-25 10:50:28 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
_ASSERTE(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// map share mode
|
|
|
|
ULONG dwShareMode = 0;
|
|
|
|
|
|
|
|
dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
|
|
|
|
if ((nOpenFlags & shareDenyWrite) == shareDenyWrite) { dwShareMode &= ~FILE_SHARE_WRITE; }
|
|
|
|
if ((nOpenFlags & shareDenyRead) == shareDenyRead) { dwShareMode &= ~FILE_SHARE_READ; }
|
|
|
|
if ((nOpenFlags & shareExclusive) == shareExclusive) { dwShareMode = 0; }
|
|
|
|
|
|
|
|
// map modeNoInherit flag
|
|
|
|
SECURITY_ATTRIBUTES sa;
|
|
|
|
sa.nLength = sizeof(sa);
|
|
|
|
sa.lpSecurityDescriptor = NULL;
|
|
|
|
sa.bInheritHandle = (nOpenFlags & modeNoInherit) == 0;
|
|
|
|
|
|
|
|
// map creation flags
|
2015-11-11 17:43:57 +00:00
|
|
|
ULONG dwCreateFlag = OPEN_EXISTING;
|
2015-10-25 10:50:28 +00:00
|
|
|
if (nOpenFlags & modeCreate)
|
|
|
|
{
|
2015-11-11 23:08:24 +00:00
|
|
|
dwCreateFlag = ((nOpenFlags & modeNoTruncate) != 0) ? OPEN_ALWAYS : CREATE_ALWAYS;
|
2015-11-11 06:47:22 +00:00
|
|
|
}
|
2015-10-25 10:50:28 +00:00
|
|
|
|
|
|
|
// attempt file creation
|
2015-11-06 11:37:21 +00:00
|
|
|
HANDLE hFile = ::CreateFile(lpszFileName, dwAccess, dwShareMode, &sa, dwCreateFlag, FILE_ATTRIBUTE_NORMAL, NULL);
|
2015-10-25 10:50:28 +00:00
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
{ //#define ERROR_PATH_NOT_FOUND 3L
|
|
|
|
//ULONG err = GetLastError();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_hFile = hFile;
|
2016-01-12 19:09:01 +00:00
|
|
|
#else
|
2015-10-25 10:50:28 +00:00
|
|
|
|
2016-01-12 19:09:01 +00:00
|
|
|
if ((nOpenFlags & CFileBase::modeCreate) != CFileBase::modeCreate)
|
|
|
|
{
|
2016-06-25 20:54:24 +00:00
|
|
|
printf("Checking if %s exists...\n",lpszFileName);
|
2016-01-12 19:48:07 +00:00
|
|
|
if (!CPath(lpszFileName).Exists())
|
2016-01-12 19:09:01 +00:00
|
|
|
{
|
2016-06-25 20:54:24 +00:00
|
|
|
printf("%s does not exist.\n",lpszFileName);
|
2016-01-12 19:09:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((nOpenFlags & CFileBase::modeCreate) == CFileBase::modeCreate)
|
|
|
|
{
|
|
|
|
CPath file(lpszFileName);
|
|
|
|
if (!file.Exists())
|
|
|
|
{
|
|
|
|
FILE * fp = fopen(lpszFileName,"wb");
|
|
|
|
if (fp)
|
|
|
|
{
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
if (!file.Exists())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((nOpenFlags & CFileBase::modeWrite) == CFileBase::modeWrite ||
|
|
|
|
(nOpenFlags & CFileBase::modeReadWrite) == CFileBase::modeReadWrite)
|
|
|
|
{
|
2016-01-12 19:48:07 +00:00
|
|
|
printf("tryinng to open %s (rb+)\n",lpszFileName);
|
2016-01-12 19:09:01 +00:00
|
|
|
m_hFile = fopen(lpszFileName, "rb+");
|
|
|
|
if (m_hFile != NULL)
|
|
|
|
{
|
|
|
|
SeekToBegin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((nOpenFlags & CFileBase::modeRead) == CFileBase::modeRead)
|
|
|
|
{
|
2016-01-12 19:48:07 +00:00
|
|
|
printf("tryinng to open %s (rb)\n",lpszFileName);
|
2016-01-12 19:09:01 +00:00
|
|
|
m_hFile = fopen(lpszFileName, "rb");
|
|
|
|
if (m_hFile != NULL)
|
|
|
|
{
|
|
|
|
SeekToBegin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
m_bCloseOnDelete = true;
|
|
|
|
return true;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CFile::Close()
|
|
|
|
{
|
2015-10-25 10:50:28 +00:00
|
|
|
bool bError = true;
|
2016-01-12 19:09:01 +00:00
|
|
|
#ifdef USE_WINDOWS_API
|
2015-10-25 10:50:28 +00:00
|
|
|
if (m_hFile != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
bError = !::CloseHandle(m_hFile);
|
|
|
|
}
|
|
|
|
m_hFile = INVALID_HANDLE_VALUE;
|
2016-01-12 19:09:01 +00:00
|
|
|
#else
|
|
|
|
if (m_hFile != NULL)
|
|
|
|
{
|
|
|
|
fclose((FILE *)m_hFile);
|
|
|
|
m_hFile = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2015-10-25 10:50:28 +00:00
|
|
|
m_bCloseOnDelete = false;
|
|
|
|
return bError;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 19:48:07 +00:00
|
|
|
uint32_t CFile::SeekToEnd(void)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
return Seek(0, CFile::end);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 19:48:07 +00:00
|
|
|
void CFile::SeekToBegin(void)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2015-10-25 10:50:28 +00:00
|
|
|
Seek(0, CFile::begin);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 19:48:07 +00:00
|
|
|
bool CFile::IsOpen(void) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 19:09:01 +00:00
|
|
|
#ifdef USE_WINDOWS_API
|
2015-10-25 10:50:28 +00:00
|
|
|
return m_hFile != INVALID_HANDLE_VALUE;
|
2016-01-12 19:09:01 +00:00
|
|
|
#else
|
|
|
|
return m_hFile != NULL;
|
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CFile::Flush()
|
|
|
|
{
|
2016-01-12 19:09:01 +00:00
|
|
|
#ifdef USE_WINDOWS_API
|
2015-10-25 10:50:28 +00:00
|
|
|
if (m_hFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-10-25 10:50:28 +00:00
|
|
|
return ::FlushFileBuffers(m_hFile) != 0;
|
2016-01-12 19:09:01 +00:00
|
|
|
#else
|
|
|
|
return fflush((FILE *)m_hFile) == 0;
|
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 06:47:22 +00:00
|
|
|
bool CFile::Write(const void* lpBuf, uint32_t nCount)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2015-10-25 10:50:28 +00:00
|
|
|
if (nCount == 0)
|
|
|
|
{
|
|
|
|
return true; // avoid Win32 "null-write" option
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-01-12 19:09:01 +00:00
|
|
|
#ifdef USE_WINDOWS_API
|
2015-11-11 06:47:22 +00:00
|
|
|
ULONG nWritten = 0;
|
|
|
|
if (!::WriteFile(m_hFile, lpBuf, nCount, &nWritten, NULL))
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-10-25 10:50:28 +00:00
|
|
|
if (nWritten != nCount)
|
|
|
|
{
|
|
|
|
// Win32s will not return an error all the time (usually DISK_FULL)
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-12 19:09:01 +00:00
|
|
|
#else
|
|
|
|
if (fwrite(lpBuf, 1, nCount, (FILE *)m_hFile) != nCount)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
2015-10-25 10:50:28 +00:00
|
|
|
return true;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 06:47:22 +00:00
|
|
|
uint32_t CFile::Read(void* lpBuf, uint32_t nCount)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2015-10-25 10:50:28 +00:00
|
|
|
if (nCount == 0)
|
|
|
|
{
|
|
|
|
return 0; // avoid Win32 "null-read"
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-01-12 19:09:01 +00:00
|
|
|
#ifdef USE_WINDOWS_API
|
2015-11-06 11:37:21 +00:00
|
|
|
DWORD dwRead = 0;
|
2015-11-11 06:47:22 +00:00
|
|
|
if (!::ReadFile(m_hFile, lpBuf, nCount, &dwRead, NULL))
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2015-11-11 06:47:22 +00:00
|
|
|
return (uint32_t)dwRead;
|
2016-01-12 19:09:01 +00:00
|
|
|
#else
|
|
|
|
uint32_t res = fread(lpBuf, sizeof(uint8_t), nCount, (FILE *)m_hFile);
|
|
|
|
return res;
|
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 19:09:01 +00:00
|
|
|
int32_t CFile::Seek(int32_t lOff, SeekPosition nFrom)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 19:09:01 +00:00
|
|
|
#ifdef USE_WINDOWS_API
|
2015-10-25 10:50:28 +00:00
|
|
|
ULONG dwNew = ::SetFilePointer(m_hFile, lOff, NULL, (ULONG)nFrom);
|
2016-01-12 19:48:07 +00:00
|
|
|
if (dwNew == (ULONG)-1)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return dwNew;
|
2016-01-12 19:09:01 +00:00
|
|
|
#else
|
|
|
|
if (m_hFile == NULL)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int origin;
|
|
|
|
|
|
|
|
switch (nFrom)
|
|
|
|
{
|
|
|
|
case begin: origin = SEEK_SET; break;
|
|
|
|
case current: origin = SEEK_CUR; break;
|
|
|
|
case end: origin = SEEK_END; break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Flush();
|
|
|
|
int res = fseek((FILE *)m_hFile, lOff, origin);
|
|
|
|
return res;
|
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-25 10:50:28 +00:00
|
|
|
uint32_t CFile::GetPosition() const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 19:09:01 +00:00
|
|
|
#ifdef USE_WINDOWS_API
|
2015-10-25 10:50:28 +00:00
|
|
|
return ::SetFilePointer(m_hFile, 0, NULL, FILE_CURRENT);
|
2016-01-12 19:09:01 +00:00
|
|
|
#else
|
|
|
|
return (uint32_t)ftell((FILE *)m_hFile);
|
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-25 10:50:28 +00:00
|
|
|
bool CFile::SetLength(uint32_t dwNewLen)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 19:09:01 +00:00
|
|
|
Seek((int32_t)dwNewLen, begin);
|
|
|
|
return SetEndOfFile();
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-25 10:50:28 +00:00
|
|
|
uint32_t CFile::GetLength() const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 19:09:01 +00:00
|
|
|
#ifdef USE_WINDOWS_API
|
|
|
|
return GetFileSize(m_hFile, 0);
|
|
|
|
#else
|
2016-01-12 19:48:07 +00:00
|
|
|
uint32_t pos = GetPosition();
|
2016-01-12 19:09:01 +00:00
|
|
|
fseek((FILE *)m_hFile, 0, SEEK_END);
|
2016-01-12 19:48:07 +00:00
|
|
|
uint32_t FileSize = GetPosition();
|
2016-01-12 19:09:01 +00:00
|
|
|
fseek((FILE *)m_hFile, (int32_t)pos, SEEK_SET);
|
|
|
|
return FileSize;
|
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CFile::SetEndOfFile()
|
|
|
|
{
|
2016-01-12 19:09:01 +00:00
|
|
|
#ifdef USE_WINDOWS_API
|
2015-10-25 10:50:28 +00:00
|
|
|
return ::SetEndOfFile(m_hFile) != 0;
|
2016-01-12 19:09:01 +00:00
|
|
|
#else
|
|
|
|
Flush();
|
|
|
|
#ifdef _WIN32
|
|
|
|
return _chsize(_fileno((FILE *)m_hFile),GetPosition()) == 0;
|
|
|
|
#else
|
|
|
|
return ftruncate(fileno((FILE *)m_hFile),GetPosition()) == 0;
|
|
|
|
#endif
|
|
|
|
#endif
|
2016-01-12 19:48:07 +00:00
|
|
|
}
|