0ad/source/ps/CVFSFile.cpp
janwas 7838627cd2 - vfs_load now returns error code and takes FileIOBuf; that must be freed via file_buf_free. if Handle is needed, use mem_wrap.
- remove ScEd hacks and CFont et al macro rename
- fix accursed bug in VFS buffer management that was causing ReadFile to
fail without error (not allocating enough padding)
- vfs_tree: bugfix in tree_lookup
- waio: temporarily disable sector size determination (pending better
approach - need to determine if using DVD drive)

This was SVN commit r3421.
2006-01-24 08:16:29 +00:00

60 lines
1.2 KiB
C++
Executable File

#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 != ERR_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());
}