0ad/source/ps/FileIo.cpp
janwas c0ed950657 had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).

it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.

after several hours, the code now requires fewer casts and less
guesswork.

other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.

This was SVN commit r5942.
2008-05-11 18:48:32 +00:00

163 lines
4.0 KiB
C++

/**
* =========================================================================
* File : FileIo.cpp
* Project : 0 A.D.
* Description : endian-safe binary file IO helpers.
* =========================================================================
*/
#include "precompiled.h"
#include "FileIo.h"
#include "ps/Filesystem.h"
#include "lib/byte_order.h"
#pragma pack(push, 1)
struct FileHeader
{
char magic[4];
u32 version_le;
u32 payloadSize_le; // = file size - sizeof(FileHeader)
};
cassert(sizeof(FileHeader) == 12);
#pragma pack(pop)
//-----------------------------------------------------------------------------
// CFilePacker
CFilePacker::CFilePacker(u32 version, const char magic[4])
{
// put header in our data array.
// (its payloadSize_le will be updated on every Pack*() call)
FileHeader header;
strncpy(header.magic, magic, 4); // not 0-terminated => no _s
write_le32(&header.version_le, version);
write_le32(&header.payloadSize_le, 0);
m_writeBuffer.Append(&header, sizeof(FileHeader));
}
CFilePacker::~CFilePacker()
{
}
void CFilePacker::Write(const VfsPath& filename)
{
const size_t payloadSize = m_writeBuffer.Size() - sizeof(FileHeader);
const u32 payloadSize_le = to_le32(u32_from_larger(payloadSize));
m_writeBuffer.Overwrite(&payloadSize_le, sizeof(payloadSize_le), 0+offsetof(FileHeader, payloadSize_le));
// write out all data (including header)
if(g_VFS->CreateFile(filename, m_writeBuffer.Data(), m_writeBuffer.Size()) < 0)
throw PSERROR_File_WriteFailed();
}
void CFilePacker::PackRaw(const void* rawData, size_t rawSize)
{
m_writeBuffer.Append(rawData, rawSize);
}
void CFilePacker::PackSize(size_t value)
{
const u32 value_le32 = to_le32(u32_from_larger(value));
PackRaw(&value_le32, sizeof(value_le32));
}
void CFilePacker::PackString(const CStr& str)
{
const size_t length = str.length();
PackSize(length);
PackRaw((const char*)str, length);
}
//-----------------------------------------------------------------------------
// CFileUnpacker
CFileUnpacker::CFileUnpacker()
: m_bufSize(0), m_unpackPos(0), m_version(0)
{
}
CFileUnpacker::~CFileUnpacker()
{
}
void CFileUnpacker::Read(const VfsPath& filename, const char magic[4])
{
// if the file doesn't exist, LoadFile would raise an annoying error dialog.
// since this (unfortunately) happens so often, we perform a separate
// check and "just" raise an exception for the caller to handle.
// (this is nicer than somehow squelching internal VFS error reporting)
if(!FileExists(filename))
throw PSERROR_File_OpenFailed();
// load the whole thing into memory
if(g_VFS->LoadFile(filename, m_buf, m_bufSize) < 0)
throw PSERROR_File_OpenFailed();
// make sure we read enough for the header
if(m_bufSize < sizeof(FileHeader))
{
m_buf.reset();
m_bufSize = 0;
throw PSERROR_File_ReadFailed();
}
// extract data from header
FileHeader* header = (FileHeader*)m_buf.get();
m_version = read_le32(&header->version_le);
const size_t payloadSize = (size_t)read_le32(&header->payloadSize_le);
// check we've got the right kind of file
// .. and that we read exactly headerSize+payloadSize
if(strncmp(header->magic, magic, 4) != 0 || m_bufSize != sizeof(FileHeader)+payloadSize)
{
m_buf.reset();
m_bufSize = 0;
throw PSERROR_File_InvalidType();
}
m_unpackPos = sizeof(FileHeader);
}
void CFileUnpacker::UnpackRaw(void* rawData, size_t rawDataSize)
{
// fail if reading past end of stream
if (m_unpackPos+rawDataSize > m_bufSize)
throw PSERROR_File_UnexpectedEOF();
void* src = m_buf.get() + m_unpackPos;
cpu_memcpy(rawData, src, rawDataSize);
m_unpackPos += rawDataSize;
}
size_t CFileUnpacker::UnpackSize()
{
u32 value_le32;
UnpackRaw(&value_le32, sizeof(value_le32));
return (size_t)to_le32(value_le32);
}
void CFileUnpacker::UnpackString(CStr& result)
{
const size_t length = UnpackSize();
// fail if reading past end of stream
if (m_unpackPos+length > m_bufSize)
throw PSERROR_File_UnexpectedEOF();
result = CStr((char*)m_buf.get()+m_unpackPos, length);
m_unpackPos += length;
}