0ad/source/ps/CVFSFile.cpp
janwas b8c131c431 # housekeeping
* zip.h removed old test symbols
* path_util fixed bug in mixed-separating-slash handling
* renamed ERR_OK to INFO_OK; allows searching for "return ERR_" to yield
all actual errors (instead of having to filter out ERR_OK)

This was SVN commit r3945.
2006-06-04 22:27:40 +00:00

60 lines
1.2 KiB
C++

#include "precompiled.h"
#include "CVFSFile.h"
#include "lib/res/mem.h"
#include "lib/res/file/vfs.h"
#include "ps/CLogger.h"
#define LOG_CATEGORY "file"
// (for only-call-once check)
CVFSFile::CVFSFile() : m_Buffer(0) {}
CVFSFile::~CVFSFile()
{
(void)file_buf_free(m_Buffer);
}
PSRETURN CVFSFile::Load(const char* filename, uint flags /* = 0 */)
{
if (m_Buffer)
{
// Load should never be called more than once, so complain
debug_warn("Mustn't open files twice");
return PSRETURN_CVFSFile_AlreadyLoaded;
}
LibError ret = vfs_load(filename, m_Buffer, m_BufferSize, flags);
if (ret != INFO_OK)
{
LOG(ERROR, LOG_CATEGORY, "CVFSFile: file %s couldn't be opened (vfs_load: %d)", filename, ret);
return PSRETURN_CVFSFile_LoadFailed;
}
return PSRETURN_OK;
}
const void* CVFSFile::GetBuffer() const
{
// Die in a very obvious way, to avoid subtle problems caused by
// accidentally forgetting to check that the open succeeded
if (!m_Buffer)
{
debug_warn("GetBuffer() called with no file loaded");
throw PSERROR_CVFSFile_InvalidBufferAccess();
}
return m_Buffer;
}
size_t CVFSFile::GetBufferSize() const
{
return m_BufferSize;
}
CStr CVFSFile::GetAsString() const
{
return std::string((char*)GetBuffer(), GetBufferSize());
}