Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added IBufferedFileStream - a faster variant of IFileStream designed … #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmake/headerlist.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set(headers
common/IArchive.h
common/IBufferedFileStream.h
common/IBufferStream.h
common/IConsole.h
common/ICriticalSection.h
Expand Down
1 change: 1 addition & 0 deletions cmake/sourcelist.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set(sources
common/IArchive.cpp
common/IBufferedFileStream.cpp
common/IBufferStream.cpp
common/IConsole.cpp
common/IDataStream.cpp
Expand Down
84 changes: 84 additions & 0 deletions common/IBufferedFileStream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "IBufferedFileStream.h"

IBufferedFileStream::IBufferedFileStream() { }

IBufferedFileStream::~IBufferedFileStream()
{
Close();
}

bool IBufferedFileStream::Open(const char* name)
{
bool success = streamFile.Open(name);

if (success)
{
SetLength(streamFile.GetLength());
SetOffset(0);

streamFile.ReadBuf(streamBuffer.data(), streamLength);

streamFile.SetOffset(0);
}

return success;
}

bool IBufferedFileStream::Create(const char* name)
{
bool success = streamFile.Create(name);

if (success)
{
SetLength(0);
SetOffset(0);
}

return success;
}

void IBufferedFileStream::Close(void)
{
if (streamBufferHasWrite)
{
streamFile.SetLength(streamLength);

streamFile.SetOffset(0);

streamFile.WriteBuf(streamBuffer.data(), streamLength);

streamBufferHasWrite = false;
}

SetLength(0);
SetOffset(0);

streamFile.Close();
}

void IBufferedFileStream::ReadBuf(void* buf, UInt32 inLength)
{
ASSERT_STR(inLength <= GetRemain(), "IBufferedFileStream::ReadBuf: hit eof");
ASSERT_STR(streamBuffer.size() >= streamOffset + inLength, "IBufferedFileStream::ReadBuf: hit buffer eof");

memcpy(buf, &streamBuffer[streamOffset], inLength);
streamOffset += inLength;
}

void IBufferedFileStream::WriteBuf(const void* buf, UInt32 inLength)
{
streamBufferHasWrite = true;

if (streamBuffer.size() < streamOffset + inLength)
SetLength(streamOffset + inLength);

memcpy(&streamBuffer[streamOffset], buf, inLength);
streamOffset += inLength;
}

void IBufferedFileStream::SetLength(UInt64 length)
{
streamBuffer.resize(length);

streamLength = length;
}
31 changes: 31 additions & 0 deletions common/IBufferedFileStream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include "common/IDataStream.h"
#include "common/IFileStream.h"
#include <vector>

/**
* An input/output file stream with buffering, write file on close
*/
class IBufferedFileStream : public IDataStream
{
public:
IBufferedFileStream();
virtual ~IBufferedFileStream();

bool Open(const char* name);
bool Create(const char* name);

void Close(void);

virtual void ReadBuf(void* buf, UInt32 inLength);
virtual void WriteBuf(const void* buf, UInt32 inLength);

void SetLength(UInt64 length);

private:
std::vector<UInt8> streamBuffer;
IFileStream streamFile;

bool streamBufferHasWrite = false;
};
2 changes: 1 addition & 1 deletion common/IFileStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "common/IDataStream.h"

/**
* An input file stream
* An input/output file stream
*/
class IFileStream : public IDataStream
{
Expand Down