1
0
forked from 0ad/0ad
0ad/source/ps/FilePacker.cpp
janwas a859562ea7 improvements and fixes:
- properly differentiate between buffer/offset alignment and length
alignment (relevant since block size has been increased to 256k)
- use VfsPath for most game paths instead of CStr
- clean up timer interface and implementation
- self-tests no longer crash
- file_cache.cpp: fix for the case where allocation fails (prevent
deleter from seeing a null pointer)
- allocators: move all shared_ptr-related stuff to its own component;
add DummySharedPtr
- codec: disable checksums (important for performance at work)
- File: made into an interface class to avoid export problems. not
entirely sure about this..
- vfs_path.h, path.h, os_path.h: proper fix for using
fs::change_extension and similar utility functions with derivatives of
basic_path
- lib_api: automatically link against import lib if building lib/ as a
DLL
- path_util: remove unused functions (this component is deprecated)
- compiler.h: add INLINE
- Xeromyces.cpp: pass PIVFS so that GetXMBPath works in self-test
(should do this mostly everywhere rather than have one singleton g_VFS)

This was SVN commit r5537.
2008-01-07 20:03:19 +00:00

61 lines
2.0 KiB
C++

/**
* =========================================================================
* File : FilePacker.cpp
* Project : 0 A.D.
* Description : Resizable buffer, for writing binary files
* =========================================================================
*/
#include "precompiled.h"
#include "FilePacker.h"
#include "ps/Filesystem.h"
#include "lib/byte_order.h"
#include <string.h>
////////////////////////////////////////////////////////////////////////////////////////
// CFilePacker constructor
// rationale for passing in version + signature here: see header
CFilePacker::CFilePacker(u32 version, const char magicstr[4])
{
// put header in our data array.
// (size will be updated on every Pack*() call)
char header[12];
strncpy(header+0, magicstr, 4); // not 0-terminated => no _s
write_le32(header+4, version);
write_le32(header+8, 0); // datasize
m_writeBuffer.Append(header, 12);
}
////////////////////////////////////////////////////////////////////////////////////////
// Write: write out to file all packed data added so far
void CFilePacker::Write(const VfsPath& filename)
{
const u32 size_le = to_le32(u32_from_larger(m_writeBuffer.Size()));
m_writeBuffer.Overwrite(&size_le, sizeof(size_le), 8);
// write out all data (including header)
if(g_VFS->CreateFile(filename, m_writeBuffer.Data(), m_writeBuffer.Size()) < 0)
throw PSERROR_File_WriteFailed();
}
////////////////////////////////////////////////////////////////////////////////////////
// PackRaw: pack given number of bytes onto the end of the data stream
void CFilePacker::PackRaw(const void* rawData, size_t rawSize)
{
m_writeBuffer.Append(rawData, rawSize);
}
////////////////////////////////////////////////////////////////////////////////////////
// PackString: pack a string onto the end of the data stream
void CFilePacker::PackString(const CStr& str)
{
const size_t length = str.length();
const u32 length_le = to_le32(u32_from_larger(length));
PackRaw(&length_le, sizeof(length_le));
PackRaw((const char*)str, length);
}