port from perforce
This commit is contained in:
123
aiwaz/Aiwaz/FileSystem/File.cpp
Normal file
123
aiwaz/Aiwaz/FileSystem/File.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
#include "stdafx.h"
|
||||
#include "File.h"
|
||||
|
||||
|
||||
File::File(IEngine* ar_Engine_, char* ac_Buffer_, unsigned int argBufferLength, const string16 argFileName)
|
||||
: m_Engine(ar_Engine_)
|
||||
, m_Buffer(ac_Buffer_)
|
||||
, m_BufferPosition(m_Buffer)
|
||||
, m_BufferLength(argBufferLength)
|
||||
, m_FileName(argFileName)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
File::~File()
|
||||
{
|
||||
delete [] m_Buffer;
|
||||
}
|
||||
|
||||
|
||||
char* File::get_Buffer()
|
||||
{
|
||||
return m_Buffer;
|
||||
}
|
||||
|
||||
|
||||
unsigned int File::get_BufferLength()
|
||||
{
|
||||
return m_BufferLength;
|
||||
}
|
||||
|
||||
|
||||
void File::SaveAs(string16 argFileName)
|
||||
{
|
||||
FILE* file;
|
||||
fopen_s(&file, std::to_string8(argFileName).c_str(), "wb");
|
||||
fwrite(m_Buffer, 1, m_BufferLength, file);
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
|
||||
std::string File::ReadString()
|
||||
{
|
||||
std::string result;
|
||||
char readChar = 0;
|
||||
do
|
||||
{
|
||||
this->CheckIntegrity(sizeof(char));
|
||||
|
||||
readChar = *m_BufferPosition;
|
||||
if (readChar != '\0')
|
||||
result += readChar;
|
||||
m_BufferPosition++;
|
||||
}
|
||||
while (readChar != '\0');
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
float File::ReadFloat()
|
||||
{
|
||||
this->CheckIntegrity(sizeof(float));
|
||||
|
||||
float result = *(float*)m_BufferPosition;
|
||||
m_BufferPosition += sizeof(float);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
int File::ReadInt()
|
||||
{
|
||||
this->CheckIntegrity(sizeof(int));
|
||||
|
||||
int result = *(int*)m_BufferPosition;
|
||||
m_BufferPosition += sizeof(int);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void* File::ReadDataArray(unsigned int argBytes)
|
||||
{
|
||||
this->CheckIntegrity(argBytes);
|
||||
|
||||
char* data = new char[argBytes];
|
||||
|
||||
memcpy(data, m_BufferPosition, argBytes);
|
||||
|
||||
m_BufferPosition += sizeof(char) * argBytes;
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
void File::RewindFilePointer()
|
||||
{
|
||||
m_BufferPosition = m_Buffer;
|
||||
}
|
||||
|
||||
|
||||
char* File::get_FilePointer() const
|
||||
{
|
||||
return m_BufferPosition;
|
||||
}
|
||||
|
||||
|
||||
void File::set_FilePointer(char* ac_Position_)
|
||||
{
|
||||
m_BufferPosition = ac_Position_;
|
||||
this->CheckIntegrity(0);
|
||||
}
|
||||
|
||||
|
||||
bool File::IsEof() const
|
||||
{
|
||||
return m_BufferPosition == (m_Buffer + m_BufferLength);
|
||||
}
|
||||
|
||||
|
||||
void File::CheckIntegrity(unsigned int argBytes)
|
||||
{
|
||||
if (m_BufferPosition + argBytes > m_Buffer + m_BufferLength || m_BufferPosition < m_Buffer)
|
||||
throw _T("File integrity is broken.");
|
||||
}
|
||||
Reference in New Issue
Block a user