61 lines
1.5 KiB
C++
61 lines
1.5 KiB
C++
|
#include "precompiled.h"
|
||
|
|
||
|
#include "lib/res/CVFSFile.h"
|
||
|
|
||
|
#include "lib/res/mem.h"
|
||
|
#include "lib/res/vfs.h"
|
||
|
#include "ps/CLogger.h"
|
||
|
|
||
|
CVFSFile::CVFSFile() : m_Handle(0) {}
|
||
|
|
||
|
CVFSFile::~CVFSFile()
|
||
|
{
|
||
|
// I hope this is the right way to delete the handle returned by
|
||
|
// vfs_load... C++ destructors would be far less ambiguous ;-)
|
||
|
if (m_Handle)
|
||
|
mem_free_h(m_Handle);
|
||
|
}
|
||
|
|
||
|
PSRETURN CVFSFile::Load(const char* filename)
|
||
|
{
|
||
|
assert(!m_Handle && "Mustn't open files twice");
|
||
|
if (m_Handle)
|
||
|
throw PSERROR_CVFSFile_AlreadyLoaded();
|
||
|
|
||
|
m_Handle = vfs_load(filename, m_Buffer, m_BufferSize);
|
||
|
if (m_Handle <= 0)
|
||
|
{
|
||
|
LOG(ERROR, "CVFSFile: file %s couldn't be opened (vfs_load: %lld)", filename, m_Handle);
|
||
|
return PSRETURN_CVFSFile_LoadFailed;
|
||
|
}
|
||
|
|
||
|
return PSRETURN_OK;
|
||
|
}
|
||
|
|
||
|
const void* CVFSFile::GetBuffer() const
|
||
|
{
|
||
|
// Die in a very obvious way, to avoid problems caused by
|
||
|
// accidentally forgetting to check that the open succeeded
|
||
|
assert(m_Handle && "GetBuffer() called with no file loaded");
|
||
|
if (!m_Handle)
|
||
|
throw PSERROR_CVFSFile_InvalidBufferAccess();
|
||
|
|
||
|
return m_Buffer;
|
||
|
}
|
||
|
|
||
|
const size_t CVFSFile::GetBufferSize() const
|
||
|
{
|
||
|
return m_BufferSize;
|
||
|
}
|
||
|
|
||
|
CStr CVFSFile::GetAsString() const
|
||
|
{
|
||
|
// Die in a very obvious way, to avoid subtle problems caused by
|
||
|
// accidentally forgetting to check that the open succeeded
|
||
|
assert(m_Handle && "GetBuffer() called with no file loaded");
|
||
|
if (!m_Handle)
|
||
|
throw PSERROR_CVFSFile_InvalidBufferAccess();
|
||
|
|
||
|
return std::string((char*)m_Buffer, m_BufferSize);
|
||
|
}
|