2012-10-03 23:58:33 +00:00
|
|
|
#include "FileStream.h"
|
|
|
|
|
2012-11-07 21:38:34 +00:00
|
|
|
#include <Windows.h>
|
2012-10-03 23:58:33 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
class FileStreamImpl {
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Track the number of references to this filestream so
|
|
|
|
// that we know whether or not we need to close it when
|
|
|
|
// the object gets destroyed.
|
|
|
|
uint32 m_ReferenceCount;
|
|
|
|
|
2012-11-07 21:38:34 +00:00
|
|
|
HANDLE m_Handle;
|
2012-10-03 23:58:33 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
FileStreamImpl(const CHAR *filename, EFileMode mode)
|
|
|
|
: m_ReferenceCount(1)
|
|
|
|
{
|
|
|
|
|
2012-11-07 21:38:34 +00:00
|
|
|
DWORD dwDesiredAccess = GENERIC_READ;
|
|
|
|
DWORD dwShareMode = 0;
|
2012-10-03 23:58:33 +00:00
|
|
|
switch(mode) {
|
|
|
|
default:
|
2012-11-07 21:38:34 +00:00
|
|
|
case eFileMode_ReadBinary:
|
2012-10-03 23:58:33 +00:00
|
|
|
case eFileMode_Read:
|
|
|
|
// Do nothing.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case eFileMode_Write:
|
|
|
|
case eFileMode_WriteBinary:
|
2012-11-07 21:38:34 +00:00
|
|
|
dwDesiredAccess = GENERIC_WRITE;
|
2012-10-03 23:58:33 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case eFileMode_WriteAppend:
|
|
|
|
case eFileMode_WriteBinaryAppend:
|
2012-11-07 21:38:34 +00:00
|
|
|
dwDesiredAccess = FILE_APPEND_DATA;
|
2012-10-03 23:58:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-11-07 21:38:34 +00:00
|
|
|
m_Handle = CreateFile(filename, dwDesiredAccess, dwShareMode, NULL, 0, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
assert(m_Handle != INVALID_HANDLE_VALUE);
|
2012-10-03 23:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~FileStreamImpl() {
|
2012-11-07 21:38:34 +00:00
|
|
|
CloseHandle(m_Handle);
|
2012-10-03 23:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IncreaseReferenceCount() { m_ReferenceCount++; }
|
|
|
|
void DecreaseReferenceCount() { m_ReferenceCount--; }
|
|
|
|
|
|
|
|
uint32 GetReferenceCount() { return m_ReferenceCount; }
|
|
|
|
|
2012-11-07 21:38:34 +00:00
|
|
|
HANDLE GetFileHandle() const { return m_Handle; }
|
2012-10-03 23:58:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
FileStream::FileStream(const CHAR *filename, EFileMode mode)
|
|
|
|
: m_Impl(new FileStreamImpl(filename, mode))
|
|
|
|
, m_Mode(mode)
|
|
|
|
{
|
2012-11-07 21:38:34 +00:00
|
|
|
strncpy_s(m_Filename, filename, kMaxFilenameSz);
|
|
|
|
m_Filename[kMaxFilenameSz - 1] = CHAR('\0');
|
2012-10-03 23:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FileStream::FileStream(const FileStream &other)
|
|
|
|
: m_Impl(other.m_Impl)
|
|
|
|
, m_Mode(other.m_Mode)
|
|
|
|
{
|
|
|
|
m_Impl->IncreaseReferenceCount();
|
2012-11-07 21:38:34 +00:00
|
|
|
strncpy_s(m_Filename, other.m_Filename, kMaxFilenameSz);
|
2012-10-03 23:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FileStream &FileStream::operator=(const FileStream &other) {
|
|
|
|
|
|
|
|
// We no longer reference this implementation.
|
|
|
|
m_Impl->DecreaseReferenceCount();
|
|
|
|
|
|
|
|
// If we're the last ones to reference it, then it should be destroyed.
|
|
|
|
if(m_Impl->GetReferenceCount() <= 0) {
|
|
|
|
assert(m_Impl->GetReferenceCount() == 0);
|
|
|
|
delete m_Impl;
|
|
|
|
m_Impl = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Impl = other.m_Impl;
|
|
|
|
m_Impl->IncreaseReferenceCount();
|
|
|
|
|
|
|
|
m_Mode = other.m_Mode;
|
2012-11-07 21:38:34 +00:00
|
|
|
strncpy_s(m_Filename, other.m_Filename, kMaxFilenameSz);
|
2012-11-07 20:26:33 +00:00
|
|
|
|
|
|
|
return *this;
|
2012-10-03 23:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FileStream::~FileStream() {
|
|
|
|
// We no longer reference this implementation.
|
|
|
|
m_Impl->DecreaseReferenceCount();
|
|
|
|
|
|
|
|
// If we're the last ones to reference it, then it should be destroyed.
|
|
|
|
if(m_Impl->GetReferenceCount() <= 0) {
|
|
|
|
assert(m_Impl->GetReferenceCount() == 0);
|
|
|
|
delete m_Impl;
|
|
|
|
m_Impl = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 FileStream::Read(uint8 *buf, uint32 bufSz) {
|
|
|
|
|
|
|
|
if(
|
|
|
|
m_Mode == eFileMode_Write ||
|
|
|
|
m_Mode == eFileMode_WriteBinary ||
|
|
|
|
m_Mode == eFileMode_WriteAppend ||
|
|
|
|
m_Mode == eFileMode_WriteBinaryAppend
|
|
|
|
) {
|
2012-11-07 21:38:34 +00:00
|
|
|
CHAR errStr[256];
|
|
|
|
_sntprintf_s(errStr, 256, "Cannot read from file '%s': File opened for reading.", m_Filename);
|
|
|
|
OutputDebugString(errStr);
|
2012-10-03 23:58:33 +00:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2012-11-07 21:38:34 +00:00
|
|
|
HANDLE fp = m_Impl->GetFileHandle();
|
|
|
|
if(INVALID_HANDLE_VALUE == fp)
|
2012-10-03 23:58:33 +00:00
|
|
|
return -1;
|
|
|
|
|
2012-11-07 21:38:34 +00:00
|
|
|
DWORD oldPosition = SetFilePointer(fp, 0, NULL, FILE_CURRENT);
|
|
|
|
if(INVALID_SET_FILE_POINTER == oldPosition) {
|
|
|
|
CHAR errStr[256];
|
|
|
|
_sntprintf_s(errStr, 256, "Error querying the file position before reading from file '%s'(0x%x).", m_Filename, GetLastError());
|
|
|
|
OutputDebugString(errStr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD amtRead;
|
|
|
|
BOOL success = ReadFile(fp, buf, bufSz, &amtRead, NULL);
|
|
|
|
if(!success) {
|
|
|
|
CHAR errStr[256];
|
|
|
|
_sntprintf_s(errStr, 256, "Error reading from file '%s'.", m_Filename);
|
|
|
|
OutputDebugString(errStr);
|
2012-10-03 23:58:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-11-07 21:38:34 +00:00
|
|
|
DWORD newPosition = SetFilePointer(fp, 0, NULL, FILE_CURRENT);
|
|
|
|
if(INVALID_SET_FILE_POINTER == newPosition) {
|
|
|
|
CHAR errStr[256];
|
|
|
|
_sntprintf_s(errStr, 256, "Error querying the file position after reading from file '%s'(0x%x).", m_Filename, GetLastError());
|
|
|
|
OutputDebugString(errStr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return newPosition - oldPosition;
|
2012-10-03 23:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32 FileStream::Write(const uint8 *buf, uint32 bufSz) {
|
|
|
|
if(
|
|
|
|
m_Mode == eFileMode_Read ||
|
|
|
|
m_Mode == eFileMode_ReadBinary
|
|
|
|
) {
|
2012-11-07 21:38:34 +00:00
|
|
|
CHAR errStr[256];
|
|
|
|
_sntprintf_s(errStr, 256, "Cannot write to file '%s': File opened for writing.", m_Filename);
|
|
|
|
OutputDebugString(errStr);
|
2012-10-03 23:58:33 +00:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2012-11-07 21:38:34 +00:00
|
|
|
HANDLE fp = m_Impl->GetFileHandle();
|
2012-10-03 23:58:33 +00:00
|
|
|
if(NULL == fp)
|
|
|
|
return -1;
|
|
|
|
|
2012-11-07 21:38:34 +00:00
|
|
|
DWORD dwPos;
|
|
|
|
if(m_Mode == eFileMode_WriteBinaryAppend || m_Mode == eFileMode_WriteAppend) {
|
|
|
|
dwPos = SetFilePointer(fp, 0, NULL, FILE_END);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dwPos = SetFilePointer(fp, 0, NULL, FILE_CURRENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(INVALID_SET_FILE_POINTER == dwPos) {
|
|
|
|
CHAR errStr[256];
|
|
|
|
_sntprintf_s(errStr, 256, "Error querying the file position before reading to file '%s'(0x%x).", m_Filename, GetLastError());
|
|
|
|
OutputDebugString(errStr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(!LockFile(fp, dwPos, 0, bufSz, 0)) Sleep(1);
|
|
|
|
|
|
|
|
DWORD amtWritten;
|
|
|
|
BOOL success = WriteFile(fp, buf, bufSz, &amtWritten, NULL);
|
|
|
|
|
|
|
|
UnlockFile(fp, dwPos, 0, bufSz, 0);
|
|
|
|
|
|
|
|
if(!success) {
|
|
|
|
CHAR errStr[256];
|
|
|
|
_sntprintf_s(errStr, 256, "Error writing to file '%s'.", m_Filename);
|
|
|
|
OutputDebugString(errStr);
|
2012-10-03 23:58:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return amtWritten;
|
|
|
|
}
|
|
|
|
|
2013-01-29 18:39:45 +00:00
|
|
|
int32 FileStream::Tell() {
|
2012-11-07 21:38:34 +00:00
|
|
|
HANDLE fp = m_Impl->GetFileHandle();
|
2012-10-03 23:58:33 +00:00
|
|
|
if(NULL == fp) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-11-07 21:38:34 +00:00
|
|
|
DWORD pos = SetFilePointer(fp, 0, NULL, FILE_CURRENT);
|
|
|
|
if(INVALID_SET_FILE_POINTER == pos) {
|
|
|
|
CHAR errStr[256];
|
|
|
|
_sntprintf_s(errStr, 256, "Error querying the file position before reading to file '%s'(0x%x).", m_Filename, GetLastError());
|
|
|
|
OutputDebugString(errStr);
|
|
|
|
return -1;
|
2012-10-03 23:58:33 +00:00
|
|
|
}
|
|
|
|
|
2012-11-07 21:38:34 +00:00
|
|
|
return pos;
|
2012-10-03 23:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileStream::Seek(uint32 offset, ESeekPosition pos) {
|
|
|
|
|
|
|
|
// We cannot seek in append mode.
|
|
|
|
if(m_Mode == eFileMode_WriteAppend || m_Mode == eFileMode_WriteBinaryAppend)
|
|
|
|
return false;
|
|
|
|
|
2012-11-07 21:38:34 +00:00
|
|
|
HANDLE fp = m_Impl->GetFileHandle();
|
2012-10-03 23:58:33 +00:00
|
|
|
if(NULL == fp) return false;
|
|
|
|
|
2012-11-07 21:38:34 +00:00
|
|
|
DWORD origin = FILE_BEGIN;
|
2012-10-03 23:58:33 +00:00
|
|
|
switch(pos) {
|
|
|
|
default:
|
|
|
|
case eSeekPosition_Beginning:
|
|
|
|
// Do nothing
|
|
|
|
break;
|
|
|
|
|
|
|
|
case eSeekPosition_Current:
|
2012-11-07 21:38:34 +00:00
|
|
|
origin = FILE_CURRENT;
|
2012-10-03 23:58:33 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case eSeekPosition_End:
|
2012-11-07 21:38:34 +00:00
|
|
|
origin = FILE_END;
|
2012-10-03 23:58:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-11-07 21:38:34 +00:00
|
|
|
if(SetFilePointer(fp, offset, NULL, origin) == INVALID_SET_FILE_POINTER)
|
2012-10-03 23:58:33 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileStream::Flush() {
|
2012-11-07 21:38:34 +00:00
|
|
|
HANDLE fp = m_Impl->GetFileHandle();
|
|
|
|
if(NULL == fp) return;
|
|
|
|
|
|
|
|
FlushFileBuffers(fp);
|
2012-10-03 23:58:33 +00:00
|
|
|
}
|