[Scummvm-cvs-logs] CVS: scummvm/smush chunk.cpp,NONE,1.1 chunk.h,NONE,1.1 chunk_type.h,NONE,1.1

Pawe? Ko?odziejski aquadran at users.sourceforge.net
Sat Aug 24 10:19:02 CEST 2002


Update of /cvsroot/scummvm/scummvm/smush
In directory usw-pr-cvs1:/tmp/cvs-serv15189

Added Files:
	chunk.cpp chunk.h chunk_type.h 
Log Message:
added chunk files

--- NEW FILE: chunk.cpp ---
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2001/2002 The ScummVM project
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header: /cvsroot/scummvm/scummvm/smush/chunk.cpp,v 1.1 2002/08/24 17:18:14 aquadran Exp $
 *
 */

#include <stdafx.h>
#include "Chunk.h"

#include <stdio.h> // for FILE, fopen, fclose, fseek and ftell
#include <string.h> // for memcpy

/*!	@brief very small and fast wrapper for a ifstream.

	implements reference counting, so that ::file_Chunk does not leak memory !
*/
class FilePtr {
	char * _filename;
	FILE * _ifs;
	int _refcount;
	int _curPos;
public:
	FilePtr(const char * fname) : _refcount(1), _curPos(0) {
		debug(9, "FilePtr created for %s", fname);
		_filename = strdup(fname);
		_ifs  = fopen(fname, "rb");
		if(_ifs == NULL) error("FilePtr unable to read file \"%s\"", fname);
	}
	~FilePtr() {
		debug(9, "FilePtr destroyed for %s", _filename);
		free(_filename);
		fclose(_ifs);
	}
	int tell() {
		return _curPos;
	}
	bool seek(int pos) {
		if(pos != _curPos) {
			fseek(_ifs, pos, SEEK_SET);
			_curPos = pos;
		}
		return true;
	}
	bool read(void * ptr, int size) {
		fread(ptr, size, 1, _ifs);
		_curPos += size;
		return true;
	}
	void incRef() {
		_refcount++;
	}
	void decRef() {
		if(--_refcount == 0)
			delete this;
	}
};

const char * Chunk::ChunkString(Chunk::type t) {
	static char data[5];
	data[0] = (char)((t >> 24) & 0xFF);
	data[1] = (char)((t >> 16) & 0xFF);
	data[2] = (char)((t >> 8) & 0xFF);
	data[3] = (char)((t >> 0) & 0xFF);
	data[4] = 0;
	return data;
}

FileChunk::FileChunk() : _data(0), _type(0), _size(0), _curPos(0) {
}

FileChunk::~FileChunk() {
	if(_data) _data->decRef();
}

FileChunk::FileChunk(const char * fname) {
	_data = new FilePtr(fname);
	_data->read(&_type, 4);
	_type = TO_BE_32(_type);
	_data->read(&_size, 4);
	_size = TO_BE_32(_size);
	_offset = _data->tell();
	_curPos = 0;
}

Chunk::type FileChunk::getType() const { 
	return _type; 
}

unsigned int FileChunk::getSize() const { 
	return _size; 
}

Chunk * FileChunk::subBlock() {
	FileChunk * ptr = new FileChunk;
	ptr->_data = _data;
	_data->incRef();
	_data->seek(_offset + _curPos);
	unsigned int temp;
	_data->read(&temp, 4);
	ptr->_type = TO_BE_32(temp);
	_data->read(&temp, 4);
	ptr->_size = TO_BE_32(temp);
	ptr->_offset = _offset + _curPos + 8;
	ptr->_curPos = 0;
	seek(8 + ptr->getSize());
	return ptr;
}

bool FileChunk::eof() const { 
	return _curPos >= _size; 
}

unsigned int FileChunk::tell() const { 
	return _curPos; 
}

bool FileChunk::seek(int delta, seek_type dir) {
	switch(dir) {
		case seek_cur:
			_curPos += delta;
			break;
		case seek_start:
			if(delta < 0) error("invalid seek request");
			_curPos = (unsigned int)delta;
			break;
		case seek_end:
			if(delta > 0 || (_size + delta) < 0) error("invalid seek request");
			_curPos = (unsigned int)(_size + delta);
			break;
	}
	if(_curPos > _size) {
		error("invalid seek request : %d > %d (delta == %d)", _curPos, _size, delta);
	}
	return true;
}

bool FileChunk::read(void * buffer, unsigned int size) {
	if(size <= 0 || (_curPos + size) > _size) error("invalid buffer read request");
	_data->seek(_offset + _curPos);
	_data->read(buffer, size);
	_curPos += size;
	return true;
}

char FileChunk::getChar() {
	if(_curPos >= _size) error("invalid char read request");
	_data->seek(_offset + _curPos);
	char buffer;
	_data->read(&buffer, sizeof(buffer));
	_curPos+= sizeof(buffer);
	return buffer;
}

unsigned char FileChunk::getByte() {
	if(_curPos >= _size) error("invalid byte read request");
	_data->seek(_offset + _curPos);
	unsigned char buffer;
	_data->read(&buffer, sizeof(buffer));
	_curPos+= sizeof(buffer);
	return buffer;
}

short FileChunk::getShort() {
	unsigned short buffer = getWord();
	return *((short*)&buffer);
}

unsigned short FileChunk::getWord() {
	if(_curPos >= _size - 1) error("invalid word read request");
	_data->seek(_offset + _curPos);
	unsigned short buffer;
	_data->read(&buffer, sizeof(buffer));
	_curPos+= sizeof(buffer);
	return TO_LE_16(buffer);
}

unsigned int FileChunk::getDword() {
	if(_curPos >= _size - 3) error("invalid dword read request");
	_data->seek(_offset + _curPos);
	unsigned int buffer;
	_data->read(&buffer, sizeof(buffer));
	_curPos+= sizeof(buffer);
	return TO_LE_32(buffer);
}

ContChunk::ContChunk(char * data) {
	if(data == 0) error("Chunk() called with NULL pointer");
	_type = (Chunk::type)READ_BE_UINT32(data);
	_size = READ_BE_UINT32(data+4);
	_data = data + sizeof(Chunk::type) + sizeof(unsigned int);
	_curPos = 0;
}

Chunk::type ContChunk::getType() const { 
	return _type; 
}

unsigned int ContChunk::getSize() const { 
	return _size; 
}

Chunk * ContChunk::subBlock() {
	ContChunk * ptr = new ContChunk(_data + _curPos);
	seek(sizeof(Chunk::type) + sizeof(unsigned int) + ptr->getSize());
	return ptr;
}

bool ContChunk::eof() const { 
	return _curPos >= _size; 
}

unsigned int ContChunk::tell() const { 
	return _curPos; 
}

bool ContChunk::seek(int delta, seek_type dir) {
	switch(dir) {
		case seek_cur:
			_curPos += delta;
			break;
		case seek_start:
			if(delta < 0) error("invalid seek request");
			_curPos = (unsigned int)delta;
			break;
		case seek_end:
			if(delta > 0 || (_size + delta) < 0) error("invalid seek request");
			_curPos = (unsigned int)(_size + delta);
			break;
	}
	if(_curPos > _size) {
		error("invalid seek request : %d > %d (delta == %d)", _curPos, _size, delta);
	}
	return true;
}

bool ContChunk::read(void * buffer, unsigned int size) {
	if(size <= 0 || (_curPos + size) > _size) error("invalid buffer read request");
	memcpy(buffer, _data + _curPos, size);
	_curPos += size;
	return true;
}

char ContChunk::getChar() {
	if(_curPos >= _size) error("invalid char read request");
	return _data[_curPos++];
}

unsigned char ContChunk::getByte() {
	if(_curPos >= _size) error("invalid byte read request");
	unsigned char * ptr = (unsigned char *)(_data + _curPos);
	_curPos += 1;
	return *ptr;
}

short ContChunk::getShort() {
	if(_curPos >= _size - 1) error("invalid short read request");
	unsigned short buffer = getWord();
	return *((short*)&buffer);
}

unsigned short ContChunk::getWord() {
	if(_curPos >= _size - 1) error("invalid word read request");
	unsigned short * ptr = (unsigned short *)(_data + _curPos);
	_curPos += 2;
	return READ_LE_UINT16(ptr);
}

unsigned int ContChunk::getDword() {
	if(_curPos >= _size - 3) error("invalid dword read request");
	unsigned int * ptr = (unsigned int *)(_data + _curPos);
	_curPos += 4;
	return READ_LE_UINT32(ptr);
}

--- NEW FILE: chunk.h ---
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2001/2002 The ScummVM project
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header: /cvsroot/scummvm/scummvm/smush/chunk.h,v 1.1 2002/08/24 17:18:14 aquadran Exp $
 *
 */

#ifndef __CHUNK_H_
#define __CHUNK_H_

#include "config.h"

/*! 	@brief Interface for Chunk handling

	This class is an interface for reading from a Chunk.

	\todo handle big endian system.
*/
class Chunk {
public:
	enum seek_type { seek_start, seek_end, seek_cur };
	virtual ~Chunk() {};
	typedef unsigned int type;			//!< type of a Chunk (i.e. The first 4byte field of the Chunk structure).
	/*!	@brief convert a type to a string
		
		Utility function that convert a type to a string.
		
		@param t the type to convert to a string
		
		@return the converted string
	*/
	static const char * ChunkString(type t);

	virtual type getType() const = 0;	//!< return the type of the Chunk
	virtual unsigned int getSize() const = 0;	//!< return the size of the Chunk
	virtual Chunk * subBlock() = 0; //!< extract a subChunk from the current read position
	virtual bool eof() const = 0;	//!< is the Chunk completely read ?
	virtual unsigned int tell() const = 0;	//!< get the Chunk current read position
	virtual bool seek(int delta, seek_type dir = seek_cur) = 0;	//!< move the current read position inside the Chunk
	virtual bool read(void * buffer, unsigned int size) = 0;		//!< read some data for the current read position
	virtual char getChar() = 0;							//!< extract the character at the current read position
	virtual unsigned char getByte() = 0;					//!< extract the byte at the current read position
	virtual short getShort() = 0;						//!< extract the short at the current read position
	virtual unsigned short getWord() = 0;					//!< extract the word at the current read position
	virtual unsigned int getDword()= 0;					//!< extract the dword at the current read position
};

class FilePtr;

/*! 	@brief file based ::Chunk

	This class is an implementation of ::Chunk that handles file.

*/
class FileChunk : public Chunk {
private:
	FilePtr * _data;
	type _type;
	unsigned int _size;
	unsigned int _offset;
	unsigned int _curPos;
protected:
	FileChunk();
public:
	FileChunk(const char * fname);
	virtual ~FileChunk();
	type getType() const;
	unsigned int getSize() const;
	Chunk * subBlock();
	bool eof() const;
	unsigned int tell() const;
	bool seek(int delta, seek_type dir = seek_cur);
	bool read(void * buffer, unsigned int size);
	char getChar();
	unsigned char getByte();
	short getShort();
	unsigned short getWord();
	unsigned int getDword();
};

/*! 	@brief memory based ::Chunk

	This class is an implementation of ::Chunk that handles a memory buffer.
*/
class ContChunk : public Chunk {
private:
	char * _data;
	Chunk::type _type;
	unsigned int _size;
	unsigned int _curPos;
public:
	ContChunk(char * data);
	Chunk::type getType() const;
	unsigned int getSize() const;
	Chunk * subBlock();
	bool eof() const;
	unsigned int tell() const;
	bool seek(int delta, seek_type dir = seek_cur);
	bool read(void * buffer, unsigned int size);
	char getChar();
	unsigned char getByte();
	short getShort();
	unsigned short getWord();
	unsigned int getDword();
};

#endif

--- NEW FILE: chunk_type.h ---
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2001/2002 The ScummVM project
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header: /cvsroot/scummvm/scummvm/smush/chunk_type.h,v 1.1 2002/08/24 17:18:14 aquadran Exp $
 *
 */

#ifndef __CHUNK_TYPE_H
#define __CHUNK_TYPE_H

#include "Chunk.h"

#define MAKE_TYPE(a,b,c,d) (Chunk::type)( ((a) << 24) | ((b) << 16) | ((c) << 8) | (d) )

static const Chunk::type TYPE_ANIM = MAKE_TYPE('A', 'N', 'I', 'M');
static const Chunk::type TYPE_AHDR = MAKE_TYPE('A', 'H', 'D', 'R');
static const Chunk::type TYPE_FRME = MAKE_TYPE('F', 'R', 'M', 'E');
static const Chunk::type TYPE_NPAL = MAKE_TYPE('N', 'P', 'A', 'L');
static const Chunk::type TYPE_FOBJ = MAKE_TYPE('F', 'O', 'B', 'J');
static const Chunk::type TYPE_PSAD = MAKE_TYPE('P', 'S', 'A', 'D');
static const Chunk::type TYPE_TRES = MAKE_TYPE('T', 'R', 'E', 'S');
static const Chunk::type TYPE_XPAL = MAKE_TYPE('X', 'P', 'A', 'L');
static const Chunk::type TYPE_IACT = MAKE_TYPE('I', 'A', 'C', 'T');
static const Chunk::type TYPE_STOR = MAKE_TYPE('S', 'T', 'O', 'R');
static const Chunk::type TYPE_FTCH = MAKE_TYPE('F', 'T', 'C', 'H');
static const Chunk::type TYPE_SKIP = MAKE_TYPE('S', 'K', 'I', 'P');
static const Chunk::type TYPE_STRK = MAKE_TYPE('S', 'T', 'R', 'K');
static const Chunk::type TYPE_SMRK = MAKE_TYPE('S', 'M', 'R', 'K');
static const Chunk::type TYPE_SHDR = MAKE_TYPE('S', 'H', 'D', 'R');
static const Chunk::type TYPE_SDAT = MAKE_TYPE('S', 'D', 'A', 'T');
static const Chunk::type TYPE_SAUD = MAKE_TYPE('S', 'A', 'U', 'D');
static const Chunk::type TYPE_iMUS = MAKE_TYPE('i', 'M', 'U', 'S');
static const Chunk::type TYPE_FRMT = MAKE_TYPE('F', 'R', 'M', 'T');
static const Chunk::type TYPE_TEXT = MAKE_TYPE('T', 'E', 'X', 'T');
static const Chunk::type TYPE_REGN = MAKE_TYPE('R', 'E', 'G', 'N');
static const Chunk::type TYPE_STOP = MAKE_TYPE('S', 'T', 'O', 'P');
static const Chunk::type TYPE_MAP_ = MAKE_TYPE('M', 'A', 'P', ' ');
static const Chunk::type TYPE_DATA = MAKE_TYPE('D', 'A', 'T', 'A');
static const Chunk::type TYPE_ETRS = MAKE_TYPE('E', 'T', 'R', 'S');

#undef MAKE_TYPE

#endif





More information about the Scummvm-git-logs mailing list