[Scummvm-cvs-logs] SF.net SVN: scummvm:[33301] residual/trunk/engine

aquadran at users.sourceforge.net aquadran at users.sourceforge.net
Sat Jul 26 11:18:47 CEST 2008


Revision: 33301
          http://scummvm.svn.sourceforge.net/scummvm/?rev=33301&view=rev
Author:   aquadran
Date:     2008-07-26 09:18:46 +0000 (Sat, 26 Jul 2008)

Log Message:
-----------
change code to more portable 

Modified Paths:
--------------
    residual/trunk/engine/imuse/imuse_mcmp_mgr.cpp
    residual/trunk/engine/imuse/imuse_mcmp_mgr.h
    residual/trunk/engine/lab.cpp
    residual/trunk/engine/lab.h
    residual/trunk/engine/lua/liolib.cpp
    residual/trunk/engine/resource.cpp
    residual/trunk/engine/resource.h
    residual/trunk/engine/smush/smush.cpp
    residual/trunk/engine/smush/smush.h

Modified: residual/trunk/engine/imuse/imuse_mcmp_mgr.cpp
===================================================================
--- residual/trunk/engine/imuse/imuse_mcmp_mgr.cpp	2008-07-26 08:03:20 UTC (rev 33300)
+++ residual/trunk/engine/imuse/imuse_mcmp_mgr.cpp	2008-07-26 09:18:46 UTC (rev 33301)
@@ -26,6 +26,7 @@
 #include "common/sys.h"
 #include "common/endian.h"
 #include "common/debug.h"
+#include "common/file.h"
 
 #include "engine/resource.h"
 #include "engine/engine.h"
@@ -46,8 +47,7 @@
 }
 
 McmpMgr::~McmpMgr() {
-	if (_file)
-		fclose(_file);
+	delete _file;
 	delete[] _compTable;
 	delete[] _compInput;
 }
@@ -55,53 +55,46 @@
 bool McmpMgr::openSound(const char *filename, byte **resPtr, int &offsetData) {
 	_file = g_resourceloader->openNewStream(filename);
 
-	if (!_file) {
+	if (!_file || !_file->isOpen()) {
 		warning("McmpMgr::openSound() Can't open sound MCMP file: %s", filename);
 		return false;
 	}
 
-	uint32 tag;
-	fread(&tag, 1, 4, _file);
-	if (READ_BE_UINT32(&tag) != MKID_BE('MCMP')) {
+	uint32 tag = _file->readUint32BE();
+	if (tag != 'MCMP') {
 		error("McmpMgr::openSound() Expected MCMP tag");
 		return false;
 	}
 
-	fread(&_numCompItems, 1, 2, _file);
-	_numCompItems = READ_BE_UINT16(&_numCompItems);
+	_numCompItems = _file->readSint16BE();
 	assert(_numCompItems > 0);
 
-	int offset = ftell(_file) + (_numCompItems * 9) + 2;
+	int32 offset = _file->pos() + (_numCompItems * 9) + 2;
 	_numCompItems--;
 	_compTable = new CompTable[_numCompItems];
-	fseek(_file, 5, SEEK_CUR);
-	fread(&_compTable[0].decompSize, 1, 4, _file);
-	int headerSize = _compTable[0].decompSize = READ_BE_UINT32(&_compTable[0].decompSize);
-	int maxSize = headerSize;
+	_file->seek(5, SEEK_CUR);
+	int32 headerSize = _compTable[0].decompSize = _file->readSint32BE();
+	int32 maxSize = headerSize;
 	offset += headerSize;
 
 	int i;
 	for (i = 0; i < _numCompItems; i++) {
-		fread(&_compTable[i].codec, 1, 1, _file);
-		fread(&_compTable[i].decompSize, 1, 4, _file);
-		_compTable[i].decompSize = READ_BE_UINT32(&_compTable[i].decompSize);
-		fread(&_compTable[i].compSize, 1, 4, _file);
-		_compTable[i].compSize = READ_BE_UINT32(&_compTable[i].compSize);
+		_compTable[i].codec = _file->readByte();
+		_compTable[i].decompSize = _file->readSint32BE();
+		_compTable[i].compSize = _file->readSint32BE();
 		_compTable[i].offset = offset;
 		offset += _compTable[i].compSize;
 		if (_compTable[i].compSize > maxSize)
 			maxSize = _compTable[i].compSize;
 	}
-	int16 sizeCodecs;
-	fread(&sizeCodecs, 1, 2, _file);
-	sizeCodecs = READ_BE_UINT16(&sizeCodecs);
+	int16 sizeCodecs = _file->readSint16BE();
 	for (i = 0; i < _numCompItems; i++) {
 		_compTable[i].offset += sizeCodecs;
 	}
-	fseek(_file, sizeCodecs, SEEK_CUR);
+	_file->seek(sizeCodecs, SEEK_CUR);
 	// hack: one more byte at the end of input buffer
 	_compInput = new byte[maxSize + 1];
-	fread(_compInput, 1, headerSize, _file);
+	_file->read(_compInput, headerSize);
 	*resPtr = _compInput;
 	offsetData = headerSize;
 	return true;
@@ -111,7 +104,7 @@
 	int32 i, final_size, output_size;
 	int skip, first_block, last_block;
 
-	if (!_file) {
+	if (!_file || !_file->isOpen()) {
 		error("McmpMgr::decompressSampleByName() File is not open!");
 		return 0;
 	}
@@ -132,8 +125,8 @@
 		if (_lastBlock != i) {
 			// hack: one more zero byte at the end of input buffer
 			_compInput[_compTable[i].compSize] = 0;
-			fseek(_file, _compTable[i].offset, SEEK_SET);
-			fread(_compInput, 1, _compTable[i].compSize, _file);
+			_file->seek(_compTable[i].offset, SEEK_SET);
+			_file->read(_compInput, _compTable[i].compSize);
 			decompressVima(_compInput, (int16 *)_compOutput, _compTable[i].decompSize, imuseDestTable);
 			_outputSize = _compTable[i].decompSize;
 			if (_outputSize > 0x2000) {

Modified: residual/trunk/engine/imuse/imuse_mcmp_mgr.h
===================================================================
--- residual/trunk/engine/imuse/imuse_mcmp_mgr.h	2008-07-26 08:03:20 UTC (rev 33300)
+++ residual/trunk/engine/imuse/imuse_mcmp_mgr.h	2008-07-26 09:18:46 UTC (rev 33301)
@@ -35,6 +35,10 @@
 
 #include "engine/imuse/imuse_sndmgr.h"
 
+namespace Common {
+	class File;
+}
+
 class McmpMgr {
 private:
 
@@ -48,7 +52,7 @@
 	CompTable *_compTable;
 	int16 _numCompItems;
 	int _curSample;
-	FILE *_file;
+	Common::File *_file;
 	byte _compOutput[0x2000];
 	byte *_compInput;
 	int _outputSize;

Modified: residual/trunk/engine/lab.cpp
===================================================================
--- residual/trunk/engine/lab.cpp	2008-07-26 08:03:20 UTC (rev 33300)
+++ residual/trunk/engine/lab.cpp	2008-07-26 09:18:46 UTC (rev 33301)
@@ -25,6 +25,8 @@
 
 #include "common/sys.h"
 #include "common/endian.h"
+#include "common/file.h"
+
 #include "engine/lab.h"
 
 #include <algorithm>
@@ -96,14 +98,15 @@
 	return new Block(data, i->second.len);
 }
 
-std::FILE *Lab::openNewStream(const char *filename) const {
+Common::File *Lab::openNewStream(const char *filename) const {
+	Common::File *file;
 	FileMapType::const_iterator i = findFilename(filename);
 	if (i == _fileMap.end())
 		return NULL;
 
-	FILE *file = std::fopen(_labFileName.c_str(), "rb");
-	assert(file);
-	std::fseek(file, i->second.offset, SEEK_SET);
+	file = new Common::File();
+	file->open(_labFileName.c_str());
+	file->seek(i->second.offset, SEEK_SET);
 
 	return file;
 }

Modified: residual/trunk/engine/lab.h
===================================================================
--- residual/trunk/engine/lab.h	2008-07-26 08:03:20 UTC (rev 33300)
+++ residual/trunk/engine/lab.h	2008-07-26 09:18:46 UTC (rev 33301)
@@ -31,6 +31,10 @@
 #include <cstdio>
 #include <map>
 
+namespace Common {
+	class File;
+}
+
 class Block {
 public:
 	Block(const char *data, int len) : _data(data), _len(len) {}
@@ -55,7 +59,7 @@
 	void close();
 	bool fileExists(const char *filename) const;
 	Block *getFileBlock(const char *filename) const;
-	std::FILE *openNewStream(const char *filename) const;
+	Common::File *openNewStream(const char *filename) const;
 	int fileLength(const char *filename) const;
 
 	~Lab() { close(); }

Modified: residual/trunk/engine/lua/liolib.cpp
===================================================================
--- residual/trunk/engine/lua/liolib.cpp	2008-07-26 08:03:20 UTC (rev 33300)
+++ residual/trunk/engine/lua/liolib.cpp	2008-07-26 09:18:46 UTC (rev 33301)
@@ -120,8 +120,8 @@
 	else {
       current = fopen(s, "r");
 #ifdef LUA_ADD_CUSTOM_FOPEN
-      if (current == NULL)
-	    current = g_resourceloader->openNewStream(s);
+//      if (current == NULL)
+//	    current = g_resourceloader->openNewStream(s);
 #endif
 	}
     if (current == NULL) {

Modified: residual/trunk/engine/resource.cpp
===================================================================
--- residual/trunk/engine/resource.cpp	2008-07-26 08:03:20 UTC (rev 33300)
+++ residual/trunk/engine/resource.cpp	2008-07-26 09:18:46 UTC (rev 33301)
@@ -147,7 +147,7 @@
 		return l->getFileBlock(filename);
 }
 
-std::FILE *ResourceLoader::openNewStream(const char *filename) const {
+Common::File *ResourceLoader::openNewStream(const char *filename) const {
 	const Lab *l = findFile(filename);
 
 	if (l == NULL)
@@ -158,10 +158,10 @@
 
 int ResourceLoader::fileLength(const char *filename) const {
 	const Lab *l = findFile(filename);
-	if (l == NULL)
+	if (l)
+		return l->fileLength(filename);
+	else
 		return 0;
-	else
-		return l->fileLength(filename);
 }
 
 Bitmap *ResourceLoader::loadBitmap(const char *filename) {

Modified: residual/trunk/engine/resource.h
===================================================================
--- residual/trunk/engine/resource.h	2008-07-26 08:03:20 UTC (rev 33300)
+++ residual/trunk/engine/resource.h	2008-07-26 09:18:46 UTC (rev 33301)
@@ -96,7 +96,7 @@
 public:
 	bool fileExists(const char *filename) const;
 	Block *getFileBlock(const char *filename) const;
-	std::FILE *openNewStream(const char *filename) const;
+	Common::File *openNewStream(const char *filename) const;
 	int fileLength(const char *filename) const;
 
 	bool exportResource(const char *filename);

Modified: residual/trunk/engine/smush/smush.cpp
===================================================================
--- residual/trunk/engine/smush/smush.cpp	2008-07-26 08:03:20 UTC (rev 33300)
+++ residual/trunk/engine/smush/smush.cpp	2008-07-26 09:18:46 UTC (rev 33301)
@@ -27,6 +27,7 @@
 #include "common/endian.h"
 #include "common/debug.h"
 #include "common/timer.h"
+#include "common/file.h"
 
 #include "engine/smush/smush.h"
 #include "engine/resource.h"
@@ -170,7 +171,7 @@
 		data = new byte[size];
 		_file.read(data, size);
 		anno = (char *)data;
-		if (strncmp(anno, ANNO_HEADER, sizeof(ANNO_HEADER)-1) == 0) {
+		if (strncmp(anno, ANNO_HEADER, sizeof(ANNO_HEADER) - 1) == 0) {
 			//char *annoData = anno + sizeof(ANNO_HEADER);
 
 			// Examples:
@@ -277,7 +278,7 @@
 	_nbframes = READ_LE_UINT32(s_header + 2);
 	int width = READ_LE_UINT16(s_header + 8);
 	int height = READ_LE_UINT16(s_header + 10);
-	if ((_width != width) || (_height != height)) {
+	if (_width != width || _height != height) {
 		_blocky16.init(width, height);
 	}
 
@@ -291,7 +292,7 @@
 	// Output information for checking out the flags
 	if (debugLevel == DEBUG_SMUSH || debugLevel == DEBUG_NORMAL || debugLevel == DEBUG_ALL) {
 		printf("SMUSH Flags:");
-		for(int i = 0; i < 16; i++)
+		for (int i = 0; i < 16; i++)
 			printf(" %d", (flags & (1 << i)) != 0);
 		printf("\n");
 	}
@@ -345,8 +346,8 @@
 
 struct SavePos *zlibFile::getPos() { 
 	struct SavePos *pos;
-	long position = std::ftell(_handle);
-	
+	uint32 position = _handle->pos();
+
 	if (position == -1) {
 		if (debugLevel == DEBUG_SMUSH || debugLevel == DEBUG_WARN || debugLevel == DEBUG_ALL)
 			warning("zlibFile::open() unable to find start position! %m");
@@ -355,24 +356,22 @@
 	pos = new SavePos;
 	pos->filePos = position;
 	inflateCopy(&pos->streamBuf, &_stream);
-	pos->tmpBuf = new char[BUFFER_SIZE];
+	pos->tmpBuf = new byte[BUFFER_SIZE];
 	memcpy(pos->tmpBuf, _inBuf, BUFFER_SIZE);
 	return pos;
 }
 
 bool zlibFile::setPos(struct SavePos *pos) { 
-	int ret;
-	
 	if (pos == NULL) {
 		warning("Unable to rewind SMUSH movie (no position passed)!");
 		return false;
 	}
-	if (_handle == NULL) {
+	if (!_handle || !_handle->isOpen()) {
 		warning("Unable to rewind SMUSH movie (invalid handle)!");
 		return false;
 	}
-	ret = std::fseek(_handle, pos->filePos, SEEK_SET);
-	if (ret == -1) {
+	_handle->seek(pos->filePos, SEEK_SET);
+	if (_handle->ioFailed()) {
 		warning("Unable to rewind SMUSH movie (seek failed)! %m");
 		return false;
 	}
@@ -387,7 +386,7 @@
 
 bool zlibFile::open(const char *filename) {
 	char flags = 0;
-	_inBuf = (char *)calloc(1, BUFFER_SIZE);
+	_inBuf = (byte *)calloc(1, BUFFER_SIZE);
 
 	if (_handle) {
 		if (debugLevel == DEBUG_SMUSH || debugLevel == DEBUG_WARN || debugLevel == DEBUG_ALL)
@@ -395,38 +394,39 @@
 		return false;
 	}
 
-	if (filename == NULL || *filename == 0)
+	if (!filename || *filename == 0)
 		return false;
 
 	_handle = g_resourceloader->openNewStream(filename);
-	if (!_handle) {
+	if (!_handle->isOpen()) {
 		if (debugLevel == DEBUG_SMUSH || debugLevel == DEBUG_WARN || debugLevel == DEBUG_ALL)
 			warning("zlibFile::open() zlibFile %s not found", filename);
 		return false;
 	}
 
 	// Read in the GZ header
-	fread(_inBuf, 2, sizeof(char), _handle);				// Header
-	fread(_inBuf, 1, sizeof(char), _handle);				// Method
-	fread(_inBuf, 1, sizeof(char), _handle); flags = _inBuf[0];		// Flags
-	fread(_inBuf, 6, sizeof(char), _handle);				// XFlags
+	_handle->read(_inBuf, 2); // Header
+	_handle->read(_inBuf, 1); // Method
+	_handle->read(_inBuf, 1); // Flags
+	flags = _inBuf[0];
+	_handle->read(_inBuf, 6); // XFlags
 
 	// Xtra & Comment
-	if (((flags & 0x04) != 0) || ((flags & 0x10) != 0)) {
+	if (flags & 0x04 || flags & 0x10) {
 		if (debugLevel == DEBUG_SMUSH || debugLevel == DEBUG_ERROR || debugLevel == DEBUG_ALL) {
 			error("zlibFile::open() Unsupported header flag");
 		}
 		return false;
 	}
 
-	if ((flags & 0x08) != 0) {					// Orig. Name
+	if (flags & 0x08) { // Orig. Name
 		do {
-			fread(_inBuf, 1, sizeof(char), _handle);
-		} while(_inBuf[0] != 0);
+			_handle->read(_inBuf, 1);
+		} while(_inBuf[0]);
 	}
 
-	if ((flags & 0x02) != 0) // CRC
-		fread(_inBuf, 2, sizeof(char), _handle);
+	if (flags & 0x02) // CRC
+		_handle->read(_inBuf, 2);
 
 	memset(_inBuf, 0, BUFFER_SIZE - 1); // Zero buffer (debug)
 	_stream.zalloc = NULL;
@@ -439,14 +439,15 @@
 	_stream.next_in = NULL;
 	_stream.next_out = NULL;
 	_stream.avail_in = 0;
-	_stream.avail_out = BUFFER_SIZE-1;
+	_stream.avail_out = BUFFER_SIZE - 1;
 
 	return true;
 }
 
 void zlibFile::close() {
 	if (_handle) {
-		fclose(_handle);
+		_handle->close();
+		delete _handle;
 		_handle = NULL;
 	}
 
@@ -457,14 +458,14 @@
 }
 
 bool zlibFile::isOpen() {
-	return _handle != NULL;
+	return _handle->isOpen();
 }
 
 uint32 zlibFile::read(void *ptr, uint32 len) {
 	int result = Z_OK;
 	bool fileEOF = false;
 
-	if (_handle == NULL) {
+	if (!_handle->isOpen()) {
 		if (debugLevel == DEBUG_SMUSH || debugLevel == DEBUG_ERROR || debugLevel == DEBUG_ALL)
 			error("zlibFile::read() File is not open!");
 		return 0;
@@ -477,9 +478,9 @@
 	_stream.avail_out = len;
 
 	_fileDone = false;
-	while (_stream.avail_out != 0) {
-		if (_stream.avail_in == 0) {	// !eof
-			_stream.avail_in = fread(_inBuf, 1, BUFFER_SIZE-1, _handle);
+	while (_stream.avail_out) {
+		if (_stream.avail_in == 0) { // !EOF
+			_stream.avail_in = _handle->read(_inBuf, BUFFER_SIZE - 1);
 			if (_stream.avail_in == 0) {
 				fileEOF = true;
 				break;
@@ -488,7 +489,7 @@
 		}
 
 		result = inflate(&_stream, Z_NO_FLUSH);
-		if (result == Z_STREAM_END) {	// EOF
+		if (result == Z_STREAM_END) { // EOF
 			// "Stream end" is zlib's way of saying that it's done after the current call,
 			// so as long as no calls are made after we've received this message we're OK
 			if (debugLevel == DEBUG_SMUSH || debugLevel == DEBUG_NORMAL || debugLevel == DEBUG_ALL)

Modified: residual/trunk/engine/smush/smush.h
===================================================================
--- residual/trunk/engine/smush/smush.h	2008-07-26 08:03:20 UTC (rev 33300)
+++ residual/trunk/engine/smush/smush.h	2008-07-26 09:18:46 UTC (rev 33301)
@@ -42,17 +42,21 @@
 
 #include <cstring>
 
+namespace Common {
+	class File;
+}
+
 struct SavePos {
-	long filePos;
+	uint32 filePos;
 	z_stream streamBuf;
-	char *tmpBuf;
+	byte *tmpBuf;
 };
 
 class zlibFile {
 private:
-	FILE *_handle;
+	Common::File *_handle;
 	z_stream _stream;	// Zlib stream
-	char *_inBuf;		// Buffer for decompression
+	byte *_inBuf;		// Buffer for decompression
 	bool _fileDone;
 
 public:


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list