Added CVFSFile, a simple wrapper around vfs_load to allow slightly cleaner code elsewhere
This was SVN commit r841.
This commit is contained in:
parent
aeb2433acd
commit
7820919689
60
source/lib/res/CVFSFile.cpp
Executable file
60
source/lib/res/CVFSFile.cpp
Executable file
@ -0,0 +1,60 @@
|
||||
#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);
|
||||
}
|
31
source/lib/res/CVFSFile.h
Executable file
31
source/lib/res/CVFSFile.h
Executable file
@ -0,0 +1,31 @@
|
||||
// OO wrapper around VFS file Handles, to simplify common usages
|
||||
|
||||
#include "lib/res/h_mgr.h"
|
||||
#include "ps/CStr.h"
|
||||
|
||||
ERROR_GROUP(CVFSFile);
|
||||
ERROR_TYPE(CVFSFile, LoadFailed);
|
||||
ERROR_TYPE(CVFSFile, AlreadyLoaded);
|
||||
ERROR_TYPE(CVFSFile, InvalidBufferAccess);
|
||||
|
||||
// Reads a file, then gives read-only access to the contents
|
||||
class CVFSFile
|
||||
{
|
||||
public:
|
||||
CVFSFile();
|
||||
~CVFSFile();
|
||||
|
||||
// Returns either PSRETURN_OK or PSRETURN_CVFSFile_LoadFailed.
|
||||
// Dies if a file has already been successfully loaded.
|
||||
PSRETURN Load(const char* filename);
|
||||
|
||||
// These die if called when no file has been successfully loaded.
|
||||
const void* GetBuffer() const;
|
||||
const size_t GetBufferSize() const;
|
||||
CStr GetAsString() const;
|
||||
|
||||
private:
|
||||
Handle m_Handle;
|
||||
void* m_Buffer;
|
||||
size_t m_BufferSize;
|
||||
};
|
Loading…
Reference in New Issue
Block a user