Make the windows based filestream actually use the MSDN file IO functions

This commit is contained in:
Pavel Krajcevski 2012-11-07 16:38:34 -05:00
parent 96f223c509
commit 113749c82f

View file

@ -1,8 +1,7 @@
#include "FileStream.h" #include "FileStream.h"
#include <Windows.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h> #include <assert.h>
class FileStreamImpl { class FileStreamImpl {
@ -13,46 +12,39 @@ private:
// the object gets destroyed. // the object gets destroyed.
uint32 m_ReferenceCount; uint32 m_ReferenceCount;
FILE *m_FilePtr; HANDLE m_Handle;
public: public:
FileStreamImpl(const CHAR *filename, EFileMode mode) FileStreamImpl(const CHAR *filename, EFileMode mode)
: m_ReferenceCount(1) : m_ReferenceCount(1)
{ {
const char *modeStr = "r"; DWORD dwDesiredAccess = GENERIC_READ;
DWORD dwShareMode = 0;
switch(mode) { switch(mode) {
default: default:
case eFileMode_ReadBinary:
case eFileMode_Read: case eFileMode_Read:
// Do nothing. // Do nothing.
break; break;
case eFileMode_ReadBinary:
modeStr = "rb";
break;
case eFileMode_Write: case eFileMode_Write:
modeStr = "w";
break;
case eFileMode_WriteBinary: case eFileMode_WriteBinary:
modeStr = "wb"; dwDesiredAccess = GENERIC_WRITE;
break; break;
case eFileMode_WriteAppend: case eFileMode_WriteAppend:
modeStr = "a";
break;
case eFileMode_WriteBinaryAppend: case eFileMode_WriteBinaryAppend:
modeStr = "ab"; dwDesiredAccess = FILE_APPEND_DATA;
break; break;
} }
m_FilePtr = fopen(filename, modeStr); m_Handle = CreateFile(filename, dwDesiredAccess, dwShareMode, NULL, 0, FILE_ATTRIBUTE_NORMAL, NULL);
assert(m_Handle != INVALID_HANDLE_VALUE);
} }
~FileStreamImpl() { ~FileStreamImpl() {
fclose(m_FilePtr); CloseHandle(m_Handle);
} }
void IncreaseReferenceCount() { m_ReferenceCount++; } void IncreaseReferenceCount() { m_ReferenceCount++; }
@ -60,15 +52,15 @@ public:
uint32 GetReferenceCount() { return m_ReferenceCount; } uint32 GetReferenceCount() { return m_ReferenceCount; }
FILE *GetFilePtr() const { return m_FilePtr; } HANDLE GetFileHandle() const { return m_Handle; }
}; };
FileStream::FileStream(const CHAR *filename, EFileMode mode) FileStream::FileStream(const CHAR *filename, EFileMode mode)
: m_Impl(new FileStreamImpl(filename, mode)) : m_Impl(new FileStreamImpl(filename, mode))
, m_Mode(mode) , m_Mode(mode)
{ {
strncpy(m_Filename, filename, kMaxFilenameSz); strncpy_s(m_Filename, filename, kMaxFilenameSz);
m_Filename[kMaxFilenameSz - 1] = '\0'; m_Filename[kMaxFilenameSz - 1] = CHAR('\0');
} }
FileStream::FileStream(const FileStream &other) FileStream::FileStream(const FileStream &other)
@ -76,7 +68,7 @@ FileStream::FileStream(const FileStream &other)
, m_Mode(other.m_Mode) , m_Mode(other.m_Mode)
{ {
m_Impl->IncreaseReferenceCount(); m_Impl->IncreaseReferenceCount();
strncpy(m_Filename, other.m_Filename, kMaxFilenameSz); strncpy_s(m_Filename, other.m_Filename, kMaxFilenameSz);
} }
FileStream &FileStream::operator=(const FileStream &other) { FileStream &FileStream::operator=(const FileStream &other) {
@ -95,7 +87,7 @@ FileStream &FileStream::operator=(const FileStream &other) {
m_Impl->IncreaseReferenceCount(); m_Impl->IncreaseReferenceCount();
m_Mode = other.m_Mode; m_Mode = other.m_Mode;
strncpy(m_Filename, other.m_Filename, kMaxFilenameSz); strncpy_s(m_Filename, other.m_Filename, kMaxFilenameSz);
return *this; return *this;
} }
@ -120,21 +112,42 @@ int32 FileStream::Read(uint8 *buf, uint32 bufSz) {
m_Mode == eFileMode_WriteAppend || m_Mode == eFileMode_WriteAppend ||
m_Mode == eFileMode_WriteBinaryAppend m_Mode == eFileMode_WriteBinaryAppend
) { ) {
fprintf(stderr, "Cannot read from file '%s': File opened for reading.", m_Filename); CHAR errStr[256];
_sntprintf_s(errStr, 256, "Cannot read from file '%s': File opened for reading.", m_Filename);
OutputDebugString(errStr);
return -2; return -2;
} }
FILE *fp = m_Impl->GetFilePtr(); HANDLE fp = m_Impl->GetFileHandle();
if(NULL == fp) if(INVALID_HANDLE_VALUE == fp)
return -1; return -1;
uint32 amtRead = fread(buf, 1, bufSz, fp); DWORD oldPosition = SetFilePointer(fp, 0, NULL, FILE_CURRENT);
if(amtRead != bufSz && !feof(fp)) { if(INVALID_SET_FILE_POINTER == oldPosition) {
fprintf(stderr, "Error reading from file '%s'.", m_Filename); 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);
return -1; return -1;
} }
return amtRead; 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;
} }
int32 FileStream::Write(const uint8 *buf, uint32 bufSz) { int32 FileStream::Write(const uint8 *buf, uint32 bufSz) {
@ -142,17 +155,42 @@ int32 FileStream::Write(const uint8 *buf, uint32 bufSz) {
m_Mode == eFileMode_Read || m_Mode == eFileMode_Read ||
m_Mode == eFileMode_ReadBinary m_Mode == eFileMode_ReadBinary
) { ) {
fprintf(stderr, "Cannot write to file '%s': File opened for writing.", m_Filename); CHAR errStr[256];
_sntprintf_s(errStr, 256, "Cannot write to file '%s': File opened for writing.", m_Filename);
OutputDebugString(errStr);
return -2; return -2;
} }
FILE *fp = m_Impl->GetFilePtr(); HANDLE fp = m_Impl->GetFileHandle();
if(NULL == fp) if(NULL == fp)
return -1; return -1;
uint32 amtWritten = fwrite(buf, 1, bufSz, fp); DWORD dwPos;
if(amtWritten != bufSz) { if(m_Mode == eFileMode_WriteBinaryAppend || m_Mode == eFileMode_WriteAppend) {
fprintf(stderr, "Error writing to file '%s'.", m_Filename); 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);
return -1; return -1;
} }
@ -160,18 +198,20 @@ int32 FileStream::Write(const uint8 *buf, uint32 bufSz) {
} }
int64 FileStream::Tell() { int64 FileStream::Tell() {
FILE *fp = m_Impl->GetFilePtr(); HANDLE fp = m_Impl->GetFileHandle();
if(NULL == fp) { if(NULL == fp) {
return -1; return -1;
} }
long int ret = ftell(fp); DWORD pos = SetFilePointer(fp, 0, NULL, FILE_CURRENT);
if(-1L == ret) { if(INVALID_SET_FILE_POINTER == pos) {
perror("Error opening file"); CHAR errStr[256];
return -1; _sntprintf_s(errStr, 256, "Error querying the file position before reading to file '%s'(0x%x).", m_Filename, GetLastError());
OutputDebugString(errStr);
return -1;
} }
return ret; return pos;
} }
bool FileStream::Seek(uint32 offset, ESeekPosition pos) { bool FileStream::Seek(uint32 offset, ESeekPosition pos) {
@ -180,10 +220,10 @@ bool FileStream::Seek(uint32 offset, ESeekPosition pos) {
if(m_Mode == eFileMode_WriteAppend || m_Mode == eFileMode_WriteBinaryAppend) if(m_Mode == eFileMode_WriteAppend || m_Mode == eFileMode_WriteBinaryAppend)
return false; return false;
FILE *fp = m_Impl->GetFilePtr(); HANDLE fp = m_Impl->GetFileHandle();
if(NULL == fp) return false; if(NULL == fp) return false;
int origin = SEEK_SET; DWORD origin = FILE_BEGIN;
switch(pos) { switch(pos) {
default: default:
case eSeekPosition_Beginning: case eSeekPosition_Beginning:
@ -191,23 +231,23 @@ bool FileStream::Seek(uint32 offset, ESeekPosition pos) {
break; break;
case eSeekPosition_Current: case eSeekPosition_Current:
origin = SEEK_CUR; origin = FILE_CURRENT;
break; break;
case eSeekPosition_End: case eSeekPosition_End:
origin = SEEK_END; origin = FILE_END;
break; break;
} }
if(fseek(fp, offset, origin)) if(SetFilePointer(fp, offset, NULL, origin) == INVALID_SET_FILE_POINTER)
return false; return false;
return true; return true;
} }
void FileStream::Flush() { void FileStream::Flush() {
FILE *fp = m_Impl->GetFilePtr(); HANDLE fp = m_Impl->GetFileHandle();
if(NULL != fp) { if(NULL == fp) return;
fflush(fp);
} FlushFileBuffers(fp);
} }