0ad/source/ps/CVFSFile.cpp
janwas f4adce44bf cleanup:
- callbacks now have a uintptr_t "cbData" parameter (instead of
haphazard void*/uintptr_t, cb/ctx/data)
- resource loading code now more uniformly deals with u8* pointers
instead of void*

allocators: add support for page_aligned_alloc via boost::shared_ptr.
add evil hack to avoid the need for default ctor and ensure alignment in
SingleAllocator
archive: improve Decompressor
compression:
. near complete rewrite (previous code was a poorly factored mess)
. fix bug related to buffer allocation
. no longer provide get_output API (prone to abuse)
. add call to get max. size of output buffer (for preallocation)

This was SVN commit r5370.
2007-09-25 09:39:20 +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 u8* 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());
}