librw/src/rwbase.cpp

221 lines
3.0 KiB
C++
Raw Normal View History

2014-12-18 17:26:57 +01:00
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <new>
2014-12-18 17:26:57 +01:00
#include "rwbase.h"
#include "rwplugin.h"
using namespace std;
namespace Rw {
int Version = 0x36003;
int Build = 0xFFFF;
int32
Stream::writeI8(int8 val)
2014-12-18 17:26:57 +01:00
{
return write(&val, sizeof(int8));
2014-12-18 17:26:57 +01:00
}
int32
Stream::writeU8(uint8 val)
2014-12-18 17:26:57 +01:00
{
return write(&val, sizeof(uint8));
2014-12-18 17:26:57 +01:00
}
int32
Stream::writeI16(int16 val)
2014-12-18 17:26:57 +01:00
{
return write(&val, sizeof(int16));
2014-12-18 17:26:57 +01:00
}
int32
Stream::writeU16(uint16 val)
2014-12-18 17:26:57 +01:00
{
return write(&val, sizeof(uint16));
2014-12-18 17:26:57 +01:00
}
int32
Stream::writeI32(int32 val)
2014-12-18 17:26:57 +01:00
{
return write(&val, sizeof(int32));
2014-12-18 17:26:57 +01:00
}
int32
Stream::writeU32(uint32 val)
2014-12-18 17:26:57 +01:00
{
return write(&val, sizeof(uint32));
2014-12-18 17:26:57 +01:00
}
int32
Stream::writeF32(float32 val)
2014-12-18 17:26:57 +01:00
{
return write(&val, sizeof(float32));
}
int8
Stream::readI8(void)
{
int8 tmp;
read(&tmp, sizeof(int8));
return tmp;
2014-12-18 17:26:57 +01:00
}
uint8
Stream::readU8(void)
2014-12-18 17:26:57 +01:00
{
uint8 tmp;
read(&tmp, sizeof(uint8));
2014-12-18 17:26:57 +01:00
return tmp;
}
int16
Stream::readI16(void)
2014-12-18 17:26:57 +01:00
{
int16 tmp;
read(&tmp, sizeof(int16));
2014-12-18 17:26:57 +01:00
return tmp;
}
uint16
Stream::readU16(void)
2014-12-18 17:26:57 +01:00
{
uint16 tmp;
read(&tmp, sizeof(uint16));
2014-12-18 17:26:57 +01:00
return tmp;
}
int32
Stream::readI32(void)
2014-12-18 17:26:57 +01:00
{
int32 tmp;
read(&tmp, sizeof(int32));
2014-12-18 17:26:57 +01:00
return tmp;
}
uint32
Stream::readU32(void)
2014-12-18 17:26:57 +01:00
{
uint32 tmp;
read(&tmp, sizeof(uint32));
2014-12-18 17:26:57 +01:00
return tmp;
}
float32
Stream::readF32(void)
2014-12-18 17:26:57 +01:00
{
float32 tmp;
read(&tmp, sizeof(float32));
2014-12-18 17:26:57 +01:00
return tmp;
}
StreamFile*
StreamFile::open(const char *path, const char *mode)
{
this->file = fopen(path, mode);
return this->file ? this : NULL;
}
void
StreamFile::close(void)
{
fclose(this->file);
}
uint32
StreamFile::write(const void *data, uint32 length)
{
return fwrite(data, length, 1, this->file);
}
uint32
StreamFile::read(void *data, uint32 length)
{
printf("read %d bytes @ %x\n", length, this->tell());
return fread(data, length, 1, this->file);
}
void
StreamFile::seek(int32 offset, int32 whence)
{
fseek(this->file, offset, whence);
}
uint32
StreamFile::tell(void)
{
return ftell(this->file);
}
bool
StreamFile::eof(void)
{
return feof(this->file);
}
2014-12-18 17:26:57 +01:00
bool
WriteChunkHeader(Stream *s, int32 type, int32 size)
2014-12-18 17:26:57 +01:00
{
struct {
int32 type, size;
uint32 id;
} buf = { type, size, LibraryIDPack(Version, Build) };
s->write(&buf, 12);
2014-12-18 17:26:57 +01:00
return true;
}
bool
ReadChunkHeaderInfo(Stream *s, ChunkHeaderInfo *header)
2014-12-18 17:26:57 +01:00
{
struct {
int32 type, size;
uint32 id;
} buf;
s->read(&buf, 12);
if(s->eof())
2014-12-18 17:26:57 +01:00
return false;
assert(header != NULL);
header->type = buf.type;
header->length = buf.size;
header->version = LibraryIDUnpackVersion(buf.id);
header->build = LibraryIDUnpackBuild(buf.id);
return true;
}
bool
FindChunk(Stream *s, uint32 type, uint32 *length, uint32 *version)
2014-12-18 17:26:57 +01:00
{
ChunkHeaderInfo header;
while(ReadChunkHeaderInfo(s, &header)){
if(header.type == ID_NAOBJECT)
return false;
if(header.type == type){
if(length)
*length = header.length;
if(version)
*version = header.version;
return true;
}
s->seek(header.length);
2014-12-18 17:26:57 +01:00
}
return false;
}
int32
findPointer(void *p, void **list, int32 num)
{
int i;
for(i = 0; i < num; i++)
if(list[i] == p)
goto found;
return -1;
found:
return i;
}
2014-12-18 17:26:57 +01:00
}