1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2025-10-05 13:51:04 +00:00

added partial VFS support - enough to read static data from any source

This commit is contained in:
fgenesis 2011-09-15 18:33:13 +02:00
commit fa3e9e7329
56 changed files with 4021 additions and 606 deletions

View file

@ -0,0 +1,340 @@
#ifndef BYTEBUFFER_H
#define BYTEBUFFER_H
#include "LVPACommon.h"
#include "ByteConverter.h"
#include <string.h> // for memcpy
LVPA_NAMESPACE_START
#define BB_MAKE_WRITE_OP(T) inline ByteBuffer& operator<<(T val) { append<T>(val); return *this; }
#define BB_MAKE_READ_OP(T) inline ByteBuffer& operator>>(T &val) { val = read<T>(); return *this; }
class ByteBuffer
{
public:
typedef void (*delete_func)(void*);
enum Mode // for creation with existing pointers
{
COPY, //- Make a copy of the buffer (default action).
REUSE, //- Use the passed-in buffer as is. Requires the pointer
// to remain valid over the life of this object.
TAKE_OVER, //- Take over the passed-in buffer; it will be deleted on object destruction.
};
class Exception
{
public:
Exception(const ByteBuffer *bb, const char *act, uint32 sp = 0)
{
action = act;
rpos = bb->rpos();
wpos = bb->wpos();
sizeparam = sp;
cursize = bb->size();
}
uint32 rpos, wpos, sizeparam, cursize;
const char *action;
};
private:
delete_func _delfunc;
uint32 _rpos, // read position, [0 ... _size]
_wpos, // write position, [0 ... _size]
_res, // reserved buffer size, [0 ... _size ... _res]
_size; // used buffer size
uint8 *_buf; // the ptr to the buffer that holds all the bytes
bool _mybuf; // if true, destructor deletes buffer
bool _growable; // default true, if false, buffer will not re-allocate more space
public:
ByteBuffer()
: _rpos(0), _wpos(0), _buf(NULL), _size(0), _growable(true)
{
_allocate(128);
}
ByteBuffer(uint32 res)
: _rpos(0), _wpos(0), _buf(NULL), _size(0), _growable(true)
{
_allocate(res);
}
ByteBuffer(const ByteBuffer &buf, uint32 extra = 0)
: _rpos(0), _wpos(0), _buf(NULL), _size(0), _growable(true)
{
_allocate(buf.size() + extra + 64);
append(buf);
}
ByteBuffer(void *buf, uint32 size, Mode mode = COPY, delete_func del = NULL, uint32 extra = 0)
: _rpos(0), _wpos(0), _size(size), _buf(NULL), _growable(true), _delfunc(del),
_mybuf(false) // for mode == REUSE
{
switch(mode)
{
case COPY:
_allocate(size + extra);
append(buf, size);
break;
case TAKE_OVER:
_mybuf = true; // fallthrough
case REUSE:
_buf = (uint8*)buf;
_res = size;
}
}
virtual ~ByteBuffer()
{
clear();
}
void clear(void)
{
_delete();
reset();
}
inline void reset(void)
{
_rpos = _wpos = _size = 0;
}
void resize(uint32 newsize)
{
reserve(newsize);
_rpos = 0;
_wpos = newsize;
_size = newsize;
}
void reserve(uint32 newsize)
{
if(_res < newsize)
_allocate(newsize);
}
// ---------------------- Write methods -----------------------
BB_MAKE_WRITE_OP(uint8);
BB_MAKE_WRITE_OP(uint16);
BB_MAKE_WRITE_OP(uint32);
BB_MAKE_WRITE_OP(uint64);
BB_MAKE_WRITE_OP(float);
BB_MAKE_WRITE_OP(double);
BB_MAKE_WRITE_OP(int);
ByteBuffer &operator<<(bool value)
{
append<char>((char)value);
return *this;
}
ByteBuffer &operator<<(const char *str)
{
append((uint8 *)str, str ? strlen(str) : 0);
append((uint8)0);
return *this;
}
ByteBuffer &operator<<(const std::string &value)
{
append((uint8 *)value.c_str(), value.length());
append((uint8)0);
return *this;
}
// -------------------- Read methods --------------------
BB_MAKE_READ_OP(uint8);
BB_MAKE_READ_OP(uint16);
BB_MAKE_READ_OP(uint32);
BB_MAKE_READ_OP(uint64);
BB_MAKE_READ_OP(float);
BB_MAKE_READ_OP(double);
BB_MAKE_READ_OP(int);
ByteBuffer &operator>>(bool &value)
{
value = read<char>() > 0 ? true : false;
return *this;
}
uint8 operator[](uint32 pos)
{
return read<uint8>(pos);
}
ByteBuffer &operator>>(std::string& value)
{
value.clear();
char c;
while(readable() && (c = read<char>()))
value += c;
return *this;
}
// --------------------------------------------------
uint32 rpos() const { return _rpos; }
uint32 rpos(uint32 rpos)
{
_rpos = rpos < size() ? rpos : size();
return _rpos;
}
uint32 wpos() const { return _wpos; }
uint32 wpos(uint32 wpos)
{
_wpos = wpos < size() ? wpos : size();
return _wpos;
}
template <typename T> T read()
{
T r = read<T>(_rpos);
_rpos += sizeof(T);
return r;
}
template <typename T> T read(uint32 pos) const
{
if(pos + sizeof(T) > size())
throw Exception(this, "read", sizeof(T));
T val = *((T const*)(_buf + pos));
ToLittleEndian<T>(val);
return val;
}
void read(void *dest, uint32 len)
{
if (_rpos + len <= size())
memcpy(dest, &_buf[_rpos], len);
else
throw Exception(this, "read-into", len);
_rpos += len;
}
inline const uint8 *contents() const { return _buf; }
inline uint8 *contents() { return _buf; }
inline uint32 size() const { return _size; }
inline uint32 bytes() const { return size(); }
inline uint32 bits() const { return bytes() * 8; }
inline uint32 capacity() const { return _res; }
inline uint32 readable(void) const { return size() - rpos(); }
inline uint32 writable(void) const { return size() - wpos(); } // free space left before realloc will occur
template <typename T> void append(T value)
{
ToLittleEndian<T>(value);
_enlargeIfReq(_wpos + sizeof(T));
*((T*)(_buf + _wpos)) = value;
_wpos += sizeof(T);
if(_size < _wpos)
_size = _wpos;
}
void append(const void *src, uint32 bytes)
{
if (!bytes) return;
_enlargeIfReq(_wpos + bytes);
memcpy(_buf + _wpos, src, bytes);
_wpos += bytes;
if(_size < _wpos)
_size = _wpos;
}
void append(const ByteBuffer& buffer)
{
if(buffer.size())
append(buffer.contents(), buffer.size());
}
void put(uint32 pos, const void *src, uint32 bytes)
{
memcpy(_buf + pos, src, bytes);
}
template <typename T> void put(uint32 pos, T value)
{
if(pos >= size())
{
throw Exception(this, "put", sizeof(T));
}
ToLittleEndian<T>(value);
*((T*)(_buf + pos)) = value;
}
inline bool growable(void) { return _growable; }
inline void growable(bool b) { _growable = b; }
// dangerous functions
void _setPtr(void *p)
{
_buf = (uint8*)p;
}
protected:
void _delete(void)
{
if(_mybuf)
{
if(_delfunc)
_delfunc(_buf);
else
delete [] _buf;
_buf = NULL;
_res = 0;
}
}
// allocate larger buffer and copy contents. if we own the current buffer, delete old, otherwise, leave it as it is.
void _allocate(uint32 s)
{
if(!_growable && _buf) // only throw if we already have a buf
throw Exception(this, "_alloc+locked", s);
uint8 *newbuf = (uint8*)malloc(s);
if(_buf)
{
memcpy(newbuf, _buf, _size);
_delete();
}
_delfunc = free;
_buf = newbuf;
_res = s;
_mybuf = true;
}
void _enlargeIfReq(uint32 minSize)
{
if(_res < minSize)
{
uint32 a = _res * 2;
if(a < minSize) // fallback if doubling the space was not enough
a += minSize;
_allocate(a);
}
}
};
#undef BB_MAKE_WRITE_OP
#undef BB_MAKE_READ_OP
LVPA_NAMESPACE_END
#endif

View file

@ -0,0 +1,46 @@
#ifndef BYTECONVERTER_H
#define BYTECONVERTER_H
#include <algorithm>
#include "LVPAInternal.h" // this is important to fix up any possible ***_ENDIAN misconfigurations
LVPA_NAMESPACE_START
namespace ByteConverter
{
template<size_t T>
inline void convert(char *val)
{
std::swap(*val, *(val + T - 1));
convert<T - 2>(val + 1);
}
template<> inline void convert<0>(char *) {}
template<> inline void convert<1>(char *) {}
template<typename T>
inline void apply(T *val)
{
convert<sizeof(T)>((char *)(val));
}
}
#if IS_BIG_ENDIAN
template<typename T> inline void ToLittleEndian(T& val) { ByteConverter::apply<T>(&val); }
template<typename T> inline void ToBigEndian(T&) { }
#else
template<typename T> inline void ToLittleEndian(T&) { }
template<typename T> inline void ToBigEndian(T& val) { ByteConverter::apply<T>(&val); }
#endif
template<typename T> void ToLittleEndian(T*); // will generate link error
template<typename T> void ToBigEndian(T*); // will generate link error
inline void ToLittleEndian(uint8&) { }
inline void ToLittleEndian(int8&) { }
inline void ToBigEndian(uint8&) { }
inline void ToBigEndian( int8&) { }
LVPA_NAMESPACE_END
#endif

View file

@ -0,0 +1,44 @@
#ifndef LVPA_COMMON_H
#define LVPA_COMMON_H
#include "LVPACompileConfig.h"
#include <stdlib.h>
#include <cstring>
LVPA_NAMESPACE_START
#ifdef _MSC_VER
typedef __int64 int64;
typedef long int32;
typedef short int16;
typedef char int8;
typedef unsigned __int64 uint64;
typedef unsigned long uint32;
typedef unsigned short uint16;
typedef unsigned char uint8;
#else
typedef long long int64;
typedef int int32;
typedef short int16;
typedef char int8;
typedef unsigned long long uint64;
typedef unsigned int uint32;
typedef unsigned short uint16;
typedef unsigned char uint8;
#endif
struct memblock
{
memblock() : ptr(NULL), size(0) {}
memblock(uint8 *p, uint32 s) : size(s), ptr(p) {}
uint8 *ptr;
uint32 size;
};
LVPA_NAMESPACE_END
#endif

View file

@ -0,0 +1,28 @@
#ifndef LVPA_COMPILE_CONFIG
#define LVPA_COMPILE_CONFIG
// TODO ADD TEXT
#define LVPA_NAMESPACE lvpa
//#define LVPA_SUPPORT_ZLIB
#define LVPA_SUPPORT_LZMA
//#define LVPA_SUPPORT_LZO
#define LVPA_SUPPORT_LZF
// ------ End of config ------
#ifdef LVPA_NAMESPACE
# define LVPA_NAMESPACE_START namespace LVPA_NAMESPACE {
# define LVPA_NAMESPACE_END }
# define LVPA_NAMESPACE_IMPL LVPA_NAMESPACE::
namespace LVPA_NAMESPACE {} // predeclare namespace to make compilers happy
#else
# define LVPA_NAMESPACE_START
# define LVPA_NAMESPACE_END
# define LVPA_NAMESPACE_IMPL
#endif
#endif

View file

@ -0,0 +1,201 @@
#ifndef LVPA_INTERNAL_H
#define LVPA_INTERNAL_H
#ifdef _DEBUG
# define DBG if(1)
# define DEBUG(x) x;
# define logdebug(...) { printf(__VA_ARGS__); putchar('\n'); }
# define logerror(...) { fputs("ERROR: ",stdout); printf(__VA_ARGS__); putchar('\n'); }
#else
# define DBG if(0)
# define DEBUG(x)
# define logdebug(...)
# define logerror(...)
#endif
//////////////////////////////////////
// Platform defines
//////////////////////////////////////
#define PLATFORM_WIN32 0
#define PLATFORM_UNIX 1
#define PLATFORM_APPLE 2
#define PLATFORM_INTEL 3
#if defined( __WIN32__ ) || defined( WIN32 ) || defined( _WIN32 )
# define PLATFORM PLATFORM_WIN32
#elif defined( __APPLE_CC__ )
# define PLATFORM PLATFORM_APPLE
#elif defined( __INTEL_COMPILER )
# define PLATFORM PLATFORM_INTEL
#else
# define PLATFORM PLATFORM_UNIX
#endif
#define COMPILER_MICROSOFT 0
#define COMPILER_GNU 1
#define COMPILER_BORLAND 2
#define COMPILER_INTEL 3
#ifdef _MSC_VER
# define COMPILER COMPILER_MICROSOFT
#elif defined( __BORLANDC__ )
# define COMPILER COMPILER_BORLAND
#elif defined( __INTEL_COMPILER )
# define COMPILER COMPILER_INTEL
#elif defined( __GNUC__ )
# define COMPILER COMPILER_GNU
#else
# pragma error "FATAL ERROR: Unknown compiler."
#endif
// stupid warnings
#if COMPILER == COMPILER_MICROSOFT
# define _CRT_SECURE_NO_WARNINGS
# define _CRT_SECURE_NO_DEPRECATE
# pragma warning(disable: 4996)
#endif
////////////////////////////////////
// Compiler defines
////////////////////////////////////
#if COMPILER == COMPILER_MICROSOFT
#define I64FMT "%016I64X"
#define I64FMTD "%I64u"
#define I64LIT(x) (x ## i64)
#define UI64LIT(x) (x ## ui64)
#define snprintf _snprintf
#else
#define stricmp strcasecmp
#define strnicmp strncasecmp
#define I64FMT "%016llX"
#define I64FMTD "%llu"
#define I64LIT(x) (x ## LL)
#define UI64LIT(x) (x ## ULL)
#endif
#ifndef _LP64
# if defined (_M_IA64) || defined (__ia64__) || defined (_M_AMD64) || defined (__amd64) || defined(_M_X64)
# define _LP64 1
# endif
#endif
#ifdef _LP64 // to be set for 64 bit compile
# define PTRFMT "0x"I64FMT
# define SYSTEM_BITS 64
#else
# define PTRFMT "0x%X"
# define SYSTEM_BITS 32
#endif
#ifndef SIGQUIT
#define SIGQUIT 3
#endif
#if COMPILER == COMPILER_MICROSOFT
# if _MSC_VER >= 1600
# define COMPILER_NAME "VC100+"
# elif _MSC_VER >= 1500
# define COMPILER_NAME "VC90"
# elif _MSC_VER >= 1400
# define COMPILER_NAME "VC80"
# elif _MSC_VER >= 1310
# define COMPILER_NAME "VC71"
# endif
# define COMPILER_VERSION _MSC_VER
# define COMPILER_VERSION_OUT "%u"
#elif COMPILER == COMPILER_GNU
# define COMPILER_NAME "GCC"
# ifndef __GNUC_PATCHLEVEL__
# define __GNUC_PATCHLEVEL__ 0
# endif
# define COMPILER_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
# define COMPILER_VERSION_OUT "%u"
// TODO: add more compilers here when necessary
#else
# define COMPILER_NAME "unknown"
# define COMPILER_VERSION "unk"
# define COMPILER_VERSION_OUT "%s"
#endif
#if PLATFORM == PLATFORM_UNIX
# define PLATFORM_NAME "Unix"
#elif PLATFORM == PLATFORM_WIN32
# define PLATFORM_NAME "Win32"
#elif PLATFORM == PLATFORM_APPLE
# define PLATFORM_NAME "Apple"
// TODO: add more platforms here when necessary
#else
# define PLATFORM_NAME "unknown"
#endif
#if COMPILER == COMPILER_GNU
# define ATTR_NORETURN __attribute__((noreturn))
# define ATTR_PRINTF(F,V) __attribute__ ((format (printf, F, V)))
#else //COMPILER != COMPILER_GNU
# define ATTR_NORETURN
# define ATTR_PRINTF(F,V)
#endif //COMPILER == COMPILER_GNU
// taken from ACE
// have seen on some systems that both defines exist, so if that is is the case, rely on this detection here
#if (!defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN)) || (defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN))
# if defined (i386) || defined (__i386__) || defined (_M_IX86) || \
defined (vax) || defined (__alpha) || defined (__LITTLE_ENDIAN__) || \
defined (ARM) || defined (_M_IA64) || defined (__ia64__) || \
defined (_M_AMD64) || defined (__amd64)
// We know these are little endian.
# undef LITTLE_ENDIAN
# undef BIG_ENDIAN
# define LITTLE_ENDIAN 1
# define IS_LITTLE_ENDIAN 1
# define IS_BIG_ENDIAN 0
# else
// Otherwise, we assume big endian.
# undef LITTLE_ENDIAN
# undef BIG_ENDIAN
# define BIG_ENDIAN 1
# define IS_LITTLE_ENDIAN 0
# define IS_BIG_ENDIAN 1
# endif
#endif
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define ASSERT(what) { if (!(what)) { fprintf( stderr, "\n%s:%i in %s ASSERTION FAILED:\n %s\n", __FILE__, __LINE__,__FUNCTION__, #what); assert( #what &&0 ); } }
#include "LVPACommon.h"
LVPA_NAMESPACE_START
template <typename T> class AutoPtrVector
{
public:
inline AutoPtrVector(uint32 prealloc) :v(prealloc)
{
for(uint32 i = 0; i < prealloc; ++i)
v[i] = NULL;
}
inline ~AutoPtrVector()
{
for(uint32 i = 0; i < v.size(); ++i)
if(v[i])
delete v[i];
}
std::vector<T*> v;
};
LVPA_NAMESPACE_END
#endif