[Scummvm-cvs-logs] SF.net SVN: scummvm:[42323] tools/branches/gsoc2009-gui

Remere at users.sourceforge.net Remere at users.sourceforge.net
Fri Jul 10 02:49:27 CEST 2009


Revision: 42323
          http://scummvm.svn.sourceforge.net/scummvm/?rev=42323&view=rev
Author:   Remere
Date:     2009-07-10 00:49:27 +0000 (Fri, 10 Jul 2009)

Log Message:
-----------
*Converted a few more tools.
*Fixed a few mistakes.

Modified Paths:
--------------
    tools/branches/gsoc2009-gui/compress_gob.cpp
    tools/branches/gsoc2009-gui/compress_gob.h
    tools/branches/gsoc2009-gui/compress_scumm_bun.cpp
    tools/branches/gsoc2009-gui/compress_scumm_san.cpp
    tools/branches/gsoc2009-gui/compress_scumm_sou.cpp
    tools/branches/gsoc2009-gui/compress_sword1.cpp
    tools/branches/gsoc2009-gui/gui/tools.cpp
    tools/branches/gsoc2009-gui/util.cpp
    tools/branches/gsoc2009-gui/util.h

Modified: tools/branches/gsoc2009-gui/compress_gob.cpp
===================================================================
--- tools/branches/gsoc2009-gui/compress_gob.cpp	2009-07-10 00:49:13 UTC (rev 42322)
+++ tools/branches/gsoc2009-gui/compress_gob.cpp	2009-07-10 00:49:27 UTC (rev 42323)
@@ -101,7 +101,7 @@
  * In order to have a slightly better compression ration in some cases (Playtoons), it
  * also detects duplicate files.
  */
-CompressGob::Chunk *CompressGob::readChunkConf(File &gobConf, const Filename &stkName, uint16 &chunkCount) {
+CompressGob::Chunk *CompressGob::readChunkConf(File &gobConf, Filename stkName, uint16 &chunkCount) {
 	Chunk *chunks = new Chunk;
 	Chunk *curChunk = chunks;
 	Chunk *parseChunk;
@@ -111,11 +111,8 @@
 	chunkCount = 1;
 
 // First read: Output filename
-FIXME: The following is an error, you cannot read into getFullPath this way, since
-(a) it is a *const* object, and 
-(b) you are not allowed to read into a string like that -- c_str may point to a tiny storage, for example
-I have no idea what this code is meant to do, so I'll leave it to Remere to clean it up ;)
-	fscanf(gobConf, "%s", stkName.getFullPath().c_str());
+	fscanf(gobConf, "%s", buffer);
+	stkName.setFullName(buffer);
 
 // Second read: signature
 	fscanf(gobConf, "%s", buffer);

Modified: tools/branches/gsoc2009-gui/compress_gob.h
===================================================================
--- tools/branches/gsoc2009-gui/compress_gob.h	2009-07-10 00:49:13 UTC (rev 42322)
+++ tools/branches/gsoc2009-gui/compress_gob.h	2009-07-10 00:49:27 UTC (rev 42323)
@@ -49,7 +49,7 @@
 
 	void parseExtraArguments();
 
-	Chunk *readChunkConf(File &gobconf, const Filename &stkName, uint16 &chunkCount);
+	Chunk *readChunkConf(File &gobconf, Filename stkName, uint16 &chunkCount);
 	void writeEmptyHeader(File &stk, uint16 chunkCount);
 	void writeBody(Filename *inpath, File &stk, Chunk *chunks);
 	uint32 writeBodyStoreFile(File &stk, File &src);

Modified: tools/branches/gsoc2009-gui/compress_scumm_bun.cpp
===================================================================
--- tools/branches/gsoc2009-gui/compress_scumm_bun.cpp	2009-07-10 00:49:13 UTC (rev 42322)
+++ tools/branches/gsoc2009-gui/compress_scumm_bun.cpp	2009-07-10 00:49:27 UTC (rev 42323)
@@ -20,7 +20,7 @@
  *
  */
 
-#include "compress.h"
+#include "compress_scumm_bun.h"
 
 /*
  * The "IMC" codec below (see cases 13 & 15 in decompressCodec) is actually a
@@ -120,7 +120,7 @@
 		}                                  \
 	} while (0)
 
-static int32 compDecode(byte *src, byte *dst) {
+int32 CompressScummBun::compDecode(byte *src, byte *dst) {
 	byte *result, *srcptr = src, *dstptr = dst;
 	int data, size, bit, bitsleft = 16, mask = READ_LE_UINT16(srcptr);
 	srcptr += 2;
@@ -156,7 +156,7 @@
 }
 #undef NextBit
 
-int32 decompressCodec(int32 codec, byte *comp_input, byte *comp_output, int32 input_size) {
+int32 CompressScummBun::decompressCodec(int32 codec, byte *comp_input, byte *comp_output, int32 input_size) {
 	int32 output_size, channels;
 	int32 offset1, offset2, offset3, length, k, c, s, j, r, t, z;
 	byte *src, *t_table, *p, *ptr;
@@ -605,7 +605,7 @@
 		break;
 
 	default:
-		printf("decompressCodec() Unknown codec %d!", (int)codec);
+		print("decompressCodec() Unknown codec %d!", (int)codec);
 		output_size = 0;
 		break;
 	}
@@ -613,19 +613,7 @@
 	return output_size;
 }
 
-struct BundleAudioTable {
-	char filename[24];
-	int size;
-	int offset;
-};
-
-static FILE *_waveTmpFile;
-static int32 _waveDataSize;
-static BundleAudioTable *bundleTable;
-static BundleAudioTable cbundleTable[10000]; // difficult to calculate
-static int32 cbundleCurIndex = 0;
-
-void encodeWaveWithFlac(char *filename) {
+void CompressScummBun::encodeWaveWithFlac(char *filename) {
 	char fbuf[2048];
 	char fbuf2[2048];
 	sprintf(fbuf, "%s.wav", filename);
@@ -633,7 +621,7 @@
 	encodeAudio(fbuf, false, -1, fbuf2, AUDIO_FLAC);
 }
 
-void encodeWaveWithOgg(char *filename) {
+void CompressScummBun::encodeWaveWithOgg(char *filename) {
 	char fbuf[2048];
 	char fbuf2[2048];
 	sprintf(fbuf, "%s.wav", filename);
@@ -641,7 +629,7 @@
 	encodeAudio(fbuf, false, -1, fbuf2, AUDIO_VORBIS);
 }
 
-void encodeWaveWithLame(char *filename) {
+void CompressScummBun::encodeWaveWithLame(char *filename) {
 	char fbuf[2048];
 	char fbuf2[2048];
 
@@ -650,7 +638,7 @@
 	encodeAudio(fbuf, false, -1, fbuf2, AUDIO_MP3);
 }
 
-void writeWaveHeader(int s_size, int rate, int chan) {
+void CompressScummBun::writeWaveHeader(int s_size, int rate, int chan) {
 	int bits = 16;
 	byte wav[44];
 	memset(wav, 0, 44);
@@ -693,25 +681,19 @@
 	wav[42]	= (s_size >> 16) & 0xff;
 	wav[43]	= (s_size >> 24) & 0xff;
 
-	fseek(_waveTmpFile, 0, SEEK_SET);
-	if (fwrite(wav, 1, 44, _waveTmpFile) != 44) {
+	_waveTmpFile.seek(0, SEEK_SET);
+	if (_waveTmpFile.write(wav, 1, 44) != 44) {
 		error("error writing temp wave file");
 	}
-	fclose(_waveTmpFile);
-	_waveTmpFile = NULL;
+	_waveTmpFile.close();
 }
 
-void writeToTempWave(char *fileName, byte *output_data, unsigned int size) {
+void CompressScummBun::writeToTempWave(char *fileName, byte *output_data, unsigned int size) {
 	if (!_waveTmpFile) {
-		_waveTmpFile = fopen(fileName, "wb");
-		if (!_waveTmpFile) {
-			error("error writing temp wave file");
-		}
+		_waveTmpFile.open(fileName, "wb");
 		byte wav[44];
 		memset(wav, 0, 44);
-		if (fwrite(output_data, 1, 44, _waveTmpFile) != 44) {
-			error("error writing temp wave file");
-		}
+		_waveTmpFile.write(output_data, 1, 44);
 		_waveDataSize = 0;
 	}
 	for (unsigned int j = 0; j < size - 1; j += 2) {
@@ -719,9 +701,7 @@
 		output_data[j + 0] = output_data[j + 1];
 		output_data[j + 1] = tmp;
 	}
-	if (fwrite(output_data, 1, size, _waveTmpFile) != size) {
-		error("error writing temp wave file");
-	}
+	_waveTmpFile.write(output_data, 1, size);
 	_waveDataSize += size;
 }
 
@@ -729,24 +709,24 @@
 
 typedef struct { int offset, size, codec; } CompTable;
 
-byte *decompressBundleSound(int index, FILE *input, int32 &finalSize) {
+byte *CompressScummBun::decompressBundleSound(int index, File  &input, int32 &finalSize) {
 	byte compOutput[0x2000];
 	int i;
 
-	fseek(input, bundleTable[index].offset, SEEK_SET);
+	input.seek(_bundleTable[index].offset, SEEK_SET);
 
-	uint32 tag = readUint32BE(input);
+	uint32 tag = input.readUint32BE();
 	assert(tag == 'COMP');
-	int numCompItems = readUint32BE(input);
-	fseek(input, 8, SEEK_CUR);
+	int numCompItems = input.readUint32BE();
+	input.seek(8, SEEK_CUR);
 
 	CompTable *compTable = (CompTable *)malloc(sizeof(CompTable) * numCompItems);
 	int32 maxSize = 0;
 	for (i = 0; i < numCompItems; i++) {
-		compTable[i].offset = readUint32BE(input);
-		compTable[i].size = readUint32BE(input);
-		compTable[i].codec = readUint32BE(input);
-		fseek(input, 4, SEEK_CUR);
+		compTable[i].offset = input.readUint32BE();
+		compTable[i].size = input.readUint32BE();
+		compTable[i].codec = input.readUint32BE();
+		input.seek(4, SEEK_CUR);
 		if (compTable[i].size > maxSize)
 			maxSize = compTable[i].size;
 	}
@@ -758,8 +738,8 @@
 
 	for (i = 0; i < numCompItems; i++) {
 		compInput[compTable[i].size] = 0;
-		fseek(input, bundleTable[index].offset + compTable[i].offset, SEEK_SET);
-		fread(compInput, 1, compTable[i].size, input);
+		input.seek(_bundleTable[index].offset + compTable[i].offset, SEEK_SET);
+		input.read(compInput, 1, compTable[i].size);
 		int outputSize = decompressCodec(compTable[i].codec, compInput, compOutput, compTable[i].size);
 		assert(outputSize <= 0x2000);
 		memcpy(compFinal + finalSize, compOutput, outputSize);
@@ -772,7 +752,7 @@
 	return compFinal;
 }
 
-byte *convertTo16bit(byte *ptr, int inputSize, int &outputSize, int bits, int freq, int channels) {
+byte *CompressScummBun::convertTo16bit(byte *ptr, int inputSize, int &outputSize, int bits, int freq, int channels) {
 	outputSize = inputSize;
 	if (bits == 8)
 		outputSize *= 2;
@@ -820,7 +800,7 @@
 	return outputBuf;
 }
 
-void countMapElements(byte *ptr, int &numRegions, int &numJumps, int &numSyncs, int &numMarkers) {
+void CompressScummBun::countMapElements(byte *ptr, int &numRegions, int &numJumps, int &numSyncs, int &numMarkers) {
 	uint32 tag;
 	int32 size = 0;
 
@@ -881,7 +861,7 @@
 static Region *_region;
 static int _numRegions;
 
-void writeRegions(byte *ptr, int bits, int freq, int channels, const char *dir, char *filename, FILE *output) {
+void CompressScummBun::writeRegions(byte *ptr, int bits, int freq, int channels, const char *dir, char *filename, File &output) {
 	char tmpPath[200];
 
 	for (int l = 0; l < _numRegions; l++) {
@@ -912,42 +892,43 @@
 		sprintf(tmpPath, "%s/%s_reg%03d.wav", dir, filename, l);
 		unlink(tmpPath);
 
-		int32 startPos = ftell(output);
+		int32 startPos = output.pos();
 		switch (gCompMode) {
 		case AUDIO_MP3:
-			sprintf(cbundleTable[cbundleCurIndex].filename, "%s_reg%03d.mp3", filename, l);
+			sprintf(_cbundleTable[_cbundleCurIndex].filename, "%s_reg%03d.mp3", filename, l);
 			sprintf(tmpPath, "%s/%s_reg%03d.mp3", dir, filename, l);
 			break;
 		case AUDIO_VORBIS:
-			sprintf(cbundleTable[cbundleCurIndex].filename, "%s_reg%03d.ogg", filename, l);
+			sprintf(_cbundleTable[_cbundleCurIndex].filename, "%s_reg%03d.ogg", filename, l);
 			sprintf(tmpPath, "%s/%s_reg%03d.ogg", dir, filename, l);
 			break;
 		case AUDIO_FLAC:
-			sprintf(cbundleTable[cbundleCurIndex].filename, "%s_reg%03d.fla", filename, l);
+			sprintf(_cbundleTable[_cbundleCurIndex].filename, "%s_reg%03d.fla", filename, l);
 			sprintf(tmpPath, "%s/%s_reg%03d.fla", dir, filename, l);
 			break;
 		default:
 			error("Unknown encoding method");
 		}
-		cbundleTable[cbundleCurIndex].offset = startPos;
+		_cbundleTable[_cbundleCurIndex].offset = startPos;
 
-		FILE *cmpFile = fopen(tmpPath, "rb");
-		fseek(cmpFile, 0, SEEK_END);
-		size = ftell(cmpFile);
-		fseek(cmpFile, 0, SEEK_SET);
+		File cmpFile(tmpPath, "rb");
+		cmpFile.seek(0, SEEK_END);
+		size = cmpFile.pos();
+		cmpFile.seek(0, SEEK_SET);
 		byte *tmpBuf = (byte *)malloc(size);
-		fread(tmpBuf, size, 1, cmpFile);
-		fclose(cmpFile);
+		cmpFile.read(tmpBuf, size, 1);
+		cmpFile.close();
 		unlink(tmpPath);
-		fwrite(tmpBuf, size, 1, output);
+
+		output.write(tmpBuf, size, 1);
 		free(tmpBuf);
-		cbundleTable[cbundleCurIndex].size = ftell(output) - startPos;
-		cbundleCurIndex++;
+		_cbundleTable[_cbundleCurIndex].size = output.pos() - startPos;
+		_cbundleCurIndex++;
 	}
 	free(_region);
 }
 
-void recalcRegions(int32 &value, int bits, int freq, int channels) {
+void CompressScummBun::recalcRegions(int32 &value, int bits, int freq, int channels) {
 	int size = value;
 	if (bits == 8)
 		size *= 2;
@@ -956,7 +937,7 @@
 	value = size;
 }
 
-void writeToRMAPFile(byte *ptr, FILE *output, char *filename, int &offsetData, int &bits, int &freq, int &channels) {
+void CompressScummBun::writeToRMAPFile(byte *ptr, File &output, char *filename, int &offsetData, int &bits, int &freq, int &channels) {
 	byte *s_ptr = ptr;
 	int32 size = 0;
 	int l;
@@ -1032,47 +1013,47 @@
 	} while (tag != 'DATA');
 	offsetData = (int32)(ptr - s_ptr);
 
-	int32 startPos = ftell(output);
-	sprintf(cbundleTable[cbundleCurIndex].filename, "%s.map", filename);
-	cbundleTable[cbundleCurIndex].offset = startPos;
+	int32 startPos = output.pos();
+	sprintf(_cbundleTable[_cbundleCurIndex].filename, "%s.map", filename);
+	_cbundleTable[_cbundleCurIndex].offset = startPos;
 
-	writeUint32BE(output, 'RMAP');
-	writeUint32BE(output, 3); // version
-	writeUint32BE(output, 16); // bits
-	writeUint32BE(output, freq);
-	writeUint32BE(output, channels);
-	writeUint32BE(output, numRegions);
-	writeUint32BE(output, numJumps);
-	writeUint32BE(output, numSyncs);
-	writeUint32BE(output, numMarkers);
+	output.writeUint32BE('RMAP');
+	output.writeUint32BE(3); // version
+	output.writeUint32BE(16); // bits
+	output.writeUint32BE(freq);
+	output.writeUint32BE(channels);
+	output.writeUint32BE(numRegions);
+	output.writeUint32BE(numJumps);
+	output.writeUint32BE(numSyncs);
+	output.writeUint32BE(numMarkers);
 	memcpy(_region, region, sizeof(Region) * numRegions);
 	for (l = 0; l < numRegions; l++) {
 		_region[l].offset -= offsetData;
 		region[l].offset -= offsetData;
 		recalcRegions(region[l].offset, bits, freq, channels);
 		recalcRegions(region[l].length, bits, freq, channels);
-		writeUint32BE(output, region[l].offset);
-		writeUint32BE(output, region[l].length);
+		output.writeUint32BE(region[l].offset);
+		output.writeUint32BE(region[l].length);
 	}
 	for (l = 0; l < numJumps; l++) {
 		jump[l].offset -= offsetData;
 		jump[l].dest -= offsetData;
 		recalcRegions(jump[l].offset, bits, freq, channels);
 		recalcRegions(jump[l].dest, bits, freq, channels);
-		writeUint32BE(output, jump[l].offset);
-		writeUint32BE(output, jump[l].dest);
-		writeUint32BE(output, jump[l].hookId);
-		writeUint32BE(output, jump[l].fadeDelay);
+		output.writeUint32BE(jump[l].offset);
+		output.writeUint32BE(jump[l].dest);
+		output.writeUint32BE(jump[l].hookId);
+		output.writeUint32BE(jump[l].fadeDelay);
 	}
 	for (l = 0; l < numSyncs; l++) {
-		writeUint32BE(output, sync[l].size);
-		fwrite(sync[l].ptr, sync[l].size, 1, output);
+		output.writeUint32BE(sync[l].size);
+		output.write(sync[l].ptr, sync[l].size, 1);
 		free(sync[l].ptr);
 	}
 	for (l = 0; l < numMarkers; l++) {
-		writeUint32BE(output, marker[l].pos);
-		writeUint32BE(output, marker[l].length);
-		fwrite(marker[l].ptr, marker[l].length, 1, output);
+		output.writeUint32BE(marker[l].pos);
+		output.writeUint32BE(marker[l].length);
+		output.write(marker[l].ptr, marker[l].length, 1);
 		delete[] marker[l].ptr;
 	}
 	free(region);
@@ -1080,72 +1061,49 @@
 	free(sync);
 	free(marker);
 
-	cbundleTable[cbundleCurIndex].size = ftell(output) - startPos;
-	cbundleCurIndex++;
+	_cbundleTable[_cbundleCurIndex].size = output.pos() - startPos;
+	_cbundleCurIndex++;
 }
 
+CompressScummBun::CompressScummBun(const std::string &name) : CompressionTool(name) {
+	_cbundleCurIndex = 0;
 
-int export_main(compress_scumm_bun)(int argc, char *argv[]) {
-	const char *helptext = "\nUsage: %s [mode] [mode-params] [-o outputfile = inputfile.bun] <inputfile>\n";
+	_helptext = "\nUsage: " + _name + " [mode] [mode-params] [-o outputfile = inputfile.bun] <inputfile>\n";
+}
 
-	Filename inpath, outpath;
-	int first_arg = 1;
-	int last_arg = argc - 1;
+void CompressScummBun::execute() {
+	// Check input
+	if (_inputPaths.size() == 1)
+		error("One input file expected!");
+	Filename inpath(_inputPaths[0]);
+	Filename &outpath = _outputPath;
 
-	parseHelpArguments(argv, argc, helptext);
-
-	// compression mode
-	gCompMode = process_audio_params(argc, argv, &first_arg);
-
-	if (gCompMode == AUDIO_NONE) {
-		// Unknown mode (failed to parse arguments), display help and exit
-		displayHelp(helptext, argv[0]);
-	}
-
 	uint32 tag;
 	int32 numFiles, offset;
 
-	// Now we try to find the proper output file
-	// also make sure we skip those arguments
-	if (parseOutputFileArguments(&outpath, argv, argc, first_arg))
-		first_arg += 2;
-	else if (parseOutputFileArguments(&outpath, argv, argc, last_arg - 2))
-		last_arg -= 2;
-	else 
-		// Just leave it empty, we just change extension of input file
-		;
+	File input(inpath, "rb");
 
-	inpath.setFullPath(argv[first_arg]);
-
-	FILE *input = fopen(inpath.getFullPath().c_str(), "rb");
-	if (!input) {
-		error("Cannot open file: %s", inpath.getFullPath().c_str());
-	}
-
 	if (outpath.empty()) {
 		// Change extension for output
 		outpath = inpath;
 		outpath.setExtension(".bun");
 	}
 
-	FILE *output = fopen(outpath.getFullPath().c_str(), "wb");
-	if (!output) {
-		error("Cannot open file: %s", outpath.getFullPath().c_str());
-	}
+	File output(outpath, "wb");
 
-	writeUint32BE(output, 'LB23');
-	writeUint32BE(output, 0); // will be later
-	writeUint32BE(output, 0); // will be later
+	output.writeUint32BE('LB23');
+	output.writeUint32BE(0); // will be later
+	output.writeUint32BE(0); // will be later
 
 	initializeImcTables();
 
-	tag = readUint32BE(input);
+	tag = input.readUint32BE();
 	assert(tag == 'LB83');
-	offset = readUint32BE(input);
-	numFiles = readUint32BE(input);
+	offset = input.readUint32BE();
+	numFiles = input.readUint32BE();
 
-	bundleTable = (BundleAudioTable *)malloc(numFiles * sizeof(BundleAudioTable));
-	fseek(input, offset, SEEK_SET);
+	_bundleTable = (BundleAudioTable *)malloc(numFiles * sizeof(BundleAudioTable));
+	input.seek(offset, SEEK_SET);
 
 	for (int i = 0; i < numFiles; i++) {
 		char filename[13], c;
@@ -1160,45 +1118,42 @@
 			if ((c = readByte(input)) != 0)
 				filename[z++] = c;
 		filename[z] = '\0';
-		strcpy(bundleTable[i].filename, filename);
-		bundleTable[i].offset = readUint32BE(input);
-		bundleTable[i].size = readUint32BE(input);
+		strcpy(_bundleTable[i].filename, filename);
+		_bundleTable[i].offset = input.readUint32BE();
+		_bundleTable[i].size = input.readUint32BE();
 	}
 
 	for (int i = 0; i < numFiles; i++) {
-		if (strcmp(bundleTable[i].filename, "PRELOAD.") == 0)
+		if (strcmp(_bundleTable[i].filename, "PRELOAD.") == 0)
 			continue;
 		int offsetData = 0, bits = 0, freq = 0, channels = 0;
 		int32 size = 0;
 		byte *compFinal = decompressBundleSound(i, input, size);
-		writeToRMAPFile(compFinal, output, bundleTable[i].filename, offsetData, bits, freq, channels);
-		writeRegions(compFinal + offsetData, bits, freq, channels, outpath.getPath().c_str(), bundleTable[i].filename, output);
+		writeToRMAPFile(compFinal, output, _bundleTable[i].filename, offsetData, bits, freq, channels);
+		writeRegions(compFinal + offsetData, bits, freq, channels, outpath.getPath().c_str(), _bundleTable[i].filename, output);
 		free(compFinal);
 	}
 
-	int32 curPos = ftell(output);
-	for (int i = 0; i < cbundleCurIndex; i++) {
-		fwrite(cbundleTable[i].filename, 24, 1, output);
-		writeUint32BE(output, cbundleTable[i].offset);
-		writeUint32BE(output, cbundleTable[i].size);
+	int32 curPos = output.pos();
+	for (int i = 0; i < _cbundleCurIndex; i++) {
+		output.write(_cbundleTable[i].filename, 24, 1);
+		output.writeUint32BE(_cbundleTable[i].offset);
+		output.writeUint32BE(_cbundleTable[i].size);
 	}
 
-	fseek(output, 4, SEEK_SET);
-	writeUint32BE(output, curPos);
-	writeUint32BE(output, cbundleCurIndex);
+	output.seek(4, SEEK_SET);
+	output.writeUint32BE(curPos);
+	output.writeUint32BE(_cbundleCurIndex);
 
-	free(bundleTable);
+	free(_bundleTable);
 
-	fclose(input);
-
-	printf("compression done.\n");
-
-	return 0;
+	print("compression done.\n");
 }
 
 #ifdef STANDALONE_MAIN
 int main(int argc, char *argv[]) {
-	return export_main(compress_scumm_bun)(argc, argv);
+	CompressScummBun scummbun(argv[0]);
+	return scummbun.run(argc, argv);
 }
 #endif
 

Modified: tools/branches/gsoc2009-gui/compress_scumm_san.cpp
===================================================================
--- tools/branches/gsoc2009-gui/compress_scumm_san.cpp	2009-07-10 00:49:13 UTC (rev 42322)
+++ tools/branches/gsoc2009-gui/compress_scumm_san.cpp	2009-07-10 00:49:27 UTC (rev 42323)
@@ -20,46 +20,10 @@
  *
  */
 
-#include "compress.h"
+#include "compress_scumm_san.h"
 #include "zlib.h"
 
-struct FrameInfo {
-	int32 frameSize;
-	int32 offsetOutput;
-	int32 fobjDecompressedSize;
-	int32 fobjCompressedSize;
-	int32 lessIACTSize;
-	int32 lessPSADSize;
-};
-
-struct AudioTrackInfo {
-	int animFrame;
-	int trackId;
-	int bits;
-	bool stereo;
-	int freq;
-	bool used;
-	FILE *file;
-	int waveDataSize;
-	int *volumes;
-	int *pans;
-	int *sizes;
-	int nbframes;
-	int countFrames;
-	int lastFrame;
-	int32 sdatSize;
-};
-
-#define MAX_TRACKS 150
-
-static byte _IACToutput[0x1000];
-static int _IACTpos = 0;
-static FILE *_waveTmpFile;
-static int32 _waveDataSize;
-static AudioTrackInfo _audioTracks[MAX_TRACKS];
-static bool _oggMode = false; // mp3 default
-
-void encodeSanWaveWithOgg(char *filename) {
+void CompressScummSan::encodeSanWaveWithOgg(char *filename) {
 	char fbuf[2048];
 	char fbuf2[2048];
 	sprintf(fbuf, "%s.wav", filename);
@@ -67,7 +31,7 @@
 	encodeAudio(fbuf, false, -1, fbuf2, AUDIO_VORBIS);
 }
 
-void encodeSanWaveWithLame(char *filename) {
+void CompressScummSan::encodeSanWaveWithLame(char *filename) {
 	char fbuf[2048];
 	char fbuf2[2048];
 
@@ -76,7 +40,7 @@
 	encodeAudio(fbuf, false, -1, fbuf2, AUDIO_MP3);
 }
 
-void writeWaveHeader(int s_size) {
+void CompressScummSan::writeWaveHeader(int s_size) {
 	int rate = 22050;
 	int bits = 16;
 	int chan = 2;
@@ -121,24 +85,18 @@
 	wav[42]	= (s_size >> 16) & 0xff;
 	wav[43]	= (s_size >> 24) & 0xff;
 
-	fseek(_waveTmpFile, 0, SEEK_SET);
-	if (fwrite(wav, 1, 44, _waveTmpFile) != 44) {
-		error("error writing temp wave file");
-	}
-	fclose(_waveTmpFile);
-	_waveTmpFile = NULL;
+	_waveTmpFile.seek(0, SEEK_SET);
+	_waveTmpFile.write(wav, 1, 44);
 }
-void writeToTempWaveFile(char *fileName, byte *output_data, unsigned int size) {
+void CompressScummSan::writeToTempWaveFile(char *fileName, byte *output_data, unsigned int size) {
 	if (!_waveTmpFile) {
-		_waveTmpFile = fopen(fileName, "wb");
+		_waveTmpFile.open(fileName, "wb");
 		if (!_waveTmpFile) {
 			error("error writing temp wave file");
 		}
 		byte wav[44];
 		memset(wav, 0, 44);
-		if (fwrite(output_data, 1, 44, _waveTmpFile) != 44) {
-			error("error writing temp wave file");
-		}
+		_waveTmpFile.write(output_data, 1, 44);
 		_waveDataSize = 0;
 	}
 	for (unsigned int j = 0; j < size - 1; j += 2) {
@@ -147,13 +105,11 @@
 		output_data[j + 1] = tmp;
 	}
 
-	if (fwrite(output_data, 1, size, _waveTmpFile) != size) {
-		error("error writing temp wave file");
-	}
+	_waveTmpFile.write(output_data, 1, size);
 	_waveDataSize += size;
 }
 
-void decompressComiIACT(char *fileName, byte *output_data, byte *d_src, int bsize) {
+void CompressScummSan::decompressComiIACT(char *fileName, byte *output_data, byte *d_src, int bsize) {
 	byte value;
 
 	while (bsize > 0) {
@@ -211,13 +167,13 @@
 	}
 }
 
-void handleComiIACT(FILE *input, int size, const char *outputDir, const char *inputFilename) {
+void CompressScummSan::handleComiIACT(File &input, int size, const char *outputDir, const char *inputFilename) {
 	char tmpPath[1024];
-	fseek(input, 10, SEEK_CUR);
+	input.seek(10, SEEK_CUR);
 	int bsize = size - 18;
 	byte output_data[0x1000];
 	byte *src = (byte *)malloc(bsize);
-	(void)fread(src, bsize, 1, input);
+	input.read(src, bsize, 1);
 
 	sprintf(tmpPath, "%s/%s.wav", outputDir, inputFilename);
 	decompressComiIACT(tmpPath, output_data, src, bsize);
@@ -225,49 +181,46 @@
 	free(src);
 }
 
-AudioTrackInfo *allocAudioTrack(int trackId, int frame) {
-	for (int l = 0; l < MAX_TRACKS; l++) {
+CompressScummSan::AudioTrackInfo *CompressScummSan::allocAudioTrack(int trackId, int frame) {
+	for (int l = 0; l < COMPRESS_SCUMM_SAN_MAX_TRACKS; l++) {
 		if ((_audioTracks[l].animFrame != frame) && (_audioTracks[l].trackId != trackId) && (!_audioTracks[l].used))
 			return &_audioTracks[l];
 	}
 	return NULL;
 }
 
-AudioTrackInfo *findAudioTrack(int trackId) {
-	for (int l = 0; l < MAX_TRACKS; l++) {
+CompressScummSan::AudioTrackInfo *CompressScummSan::findAudioTrack(int trackId) {
+	for (int l = 0; l < COMPRESS_SCUMM_SAN_MAX_TRACKS; l++) {
 		if (_audioTracks[l].trackId == trackId && _audioTracks[l].used && _audioTracks[l].file)
 			return &_audioTracks[l];
 	}
 	return NULL;
 }
 
-void flushTracks(int frame) {
-	for (int l = 0; l < MAX_TRACKS; l++) {
+void CompressScummSan::flushTracks(int frame) {
+	for (int l = 0; l < COMPRESS_SCUMM_SAN_MAX_TRACKS; l++) {
 		if (_audioTracks[l].used && _audioTracks[l].file && (frame - _audioTracks[l].lastFrame) > 1) {
-			fclose(_audioTracks[l].file);
-			_audioTracks[l].file = NULL;
+			_audioTracks[l].file.close();
 		}
 	}
 }
 
-void prepareForMixing(const char *outputDir, const char *inputFilename) {
+void CompressScummSan::prepareForMixing(const char *outputDir, const char *inputFilename) {
 	char filename[200];
 
-	printf("Decompresing tracks files...\n");
-	for (int l = 0; l < MAX_TRACKS; l++) {
+	print("Decompresing tracks files...\n");
+	for (int l = 0; l < COMPRESS_SCUMM_SAN_MAX_TRACKS; l++) {
 		if (_audioTracks[l].used) {
-			if (_audioTracks[l].file)
-				fclose(_audioTracks[l].file);
+			_audioTracks[l].file.close();
+			
 			sprintf(filename, "%s/%s_%04d_%03d.tmp", outputDir, inputFilename, _audioTracks[l].animFrame, _audioTracks[l].trackId);
-			_audioTracks[l].file = fopen(filename, "rb");
-			assert(_audioTracks[l].file);
-			fseek(_audioTracks[l].file, 0, SEEK_END);
-			int fileSize = ftell(_audioTracks[l].file);
-			fseek(_audioTracks[l].file, 0, SEEK_SET);
+			_audioTracks[l].file.open(filename, "rb");
+			_audioTracks[l].file.seek(0, SEEK_END);
+			int fileSize = _audioTracks[l].file.pos();
+			_audioTracks[l].file.seek(0, SEEK_SET);
 			byte *audioBuf = (byte *)malloc(fileSize);
-			(void)fread(audioBuf, fileSize, 1, _audioTracks[l].file);
-			fclose(_audioTracks[l].file);
-			_audioTracks[l].file = NULL;
+			_audioTracks[l].file.read(audioBuf, fileSize, 1);
+			_audioTracks[l].file.close();
 
 			int outputSize = fileSize;
 			if (_audioTracks[l].bits == 8)
@@ -345,11 +298,8 @@
 			}
 
 			free(audioBuf);
-			_audioTracks[l].file = fopen(filename, "wb");
-			assert(_audioTracks[l].file);
-			fwrite(outputBuf, outputSize, 1, _audioTracks[l].file);
-			fclose(_audioTracks[l].file);
-			_audioTracks[l].file = NULL;
+			_audioTracks[l].file.open(filename, "wb");
+			_audioTracks[l].file.write(outputBuf, outputSize, 1);
 			free(outputBuf);
 		}
 	}
@@ -369,14 +319,13 @@
 
 	a = val;
 }
-void mixing(const char *outputDir, const char *inputFilename, int frames, int fps) {
+void CompressScummSan::mixing(const char *outputDir, const char *inputFilename, int frames, int fps) {
 	char wavPath[200];
 	char filename[200];
 	int l, r, z;
 
 	sprintf(wavPath, "%s/%s.wav", outputDir, inputFilename);
-	FILE *wavFile = fopen(wavPath, "wb+");
-	assert(wavFile);
+	File wavFile(wavPath, "wb+");
 
 	int frameAudioSize;
 	if (fps == 12) {
@@ -387,28 +336,28 @@
 		error("Unsupported fps value %d", fps);
 	}
 
-	printf("Creating silent wav file...\n");
+	print("Creating silent wav file...\n");
 	for (l = 0; l < 44 + (frameAudioSize * frames); l++) {
 		fputc(0, wavFile);
 	}
 
-	printf("Mixing tracks into wav file...\n");
-	for (l = 0; l < MAX_TRACKS; l++) {
+	print("Mixing tracks into wav file...\n");
+	for (l = 0; l < COMPRESS_SCUMM_SAN_MAX_TRACKS; l++) {
 		if (_audioTracks[l].used) {
 			sprintf(filename, "%s/%s_%04d_%03d.tmp", outputDir, inputFilename, _audioTracks[l].animFrame, _audioTracks[l].trackId);
-			_audioTracks[l].file = fopen(filename, "rb");
+			_audioTracks[l].file.open(filename, "rb");
 			assert(_audioTracks[l].file);
-			fseek(_audioTracks[l].file, 0, SEEK_END);
-			int fileSize = ftell(_audioTracks[l].file);
-			fseek(_audioTracks[l].file, 0, SEEK_SET);
+			_audioTracks[l].file.seek(0, SEEK_END);
+			int fileSize = _audioTracks[l].file.pos();
+			_audioTracks[l].file.seek(0, SEEK_SET);
 			byte *tmpBuf = (byte *)malloc(fileSize);
-			(void)fread(tmpBuf, fileSize, 1, _audioTracks[l].file);
-			fclose(_audioTracks[l].file);
+			_audioTracks[l].file.read(tmpBuf, fileSize, 1);
+			_audioTracks[l].file.close();
 			unlink(filename);
 
 			byte *wavBuf = (byte *)malloc(fileSize);
-			fseek(wavFile, 44 + (frameAudioSize * _audioTracks[l].animFrame), SEEK_SET);
-			(void)fread(wavBuf, fileSize, 1, wavFile);
+			wavFile.seek(44 + (frameAudioSize * _audioTracks[l].animFrame), SEEK_SET);
+			wavFile.read(wavBuf, fileSize, 1);
 
 			int offset = 0;
 			for (z = 0; z < _audioTracks[l].countFrames; z++) {
@@ -435,8 +384,8 @@
 				}
 				offset += length;
 			}
-			fseek(wavFile, 44 + (frameAudioSize * _audioTracks[l].animFrame), SEEK_SET);
-			fwrite(wavBuf, fileSize, 1, wavFile);
+			wavFile.seek(44 + (frameAudioSize * _audioTracks[l].animFrame), SEEK_SET);
+			wavFile.write(wavBuf, fileSize, 1);
 
 			free(wavBuf);
 			free(tmpBuf);
@@ -447,75 +396,75 @@
 	_waveDataSize = frames * frameAudioSize;
 }
 
-void handleMapChunk(AudioTrackInfo *audioTrack, FILE *input) {
+void CompressScummSan::handleMapChunk(AudioTrackInfo *audioTrack, File &input) {
 	uint32 tag;
 	int32 size;
-	tag = readUint32BE(input);
-	size = readUint32BE(input);
+	tag = input.readUint32BE();
+	size = input.readUint32BE();
 	assert(tag == 'iMUS');
-	tag = readUint32BE(input);
-	size = readUint32BE(input);
+	tag = input.readUint32BE();
+	size = input.readUint32BE();
 	assert(tag == 'MAP ');
-	tag = readUint32BE(input);
-	size = readUint32BE(input);
+	tag = input.readUint32BE();
+	size = input.readUint32BE();
 	assert(tag == 'FRMT');
-	fseek(input, 8, SEEK_CUR);
-	audioTrack->bits = readUint32BE(input);
-	audioTrack->freq = readUint32BE(input);
-	int chan = readUint32BE(input);
+	input.seek(8, SEEK_CUR);
+	audioTrack->bits = input.readUint32BE();
+	audioTrack->freq = input.readUint32BE();
+	int chan = input.readUint32BE();
 	if (chan == 2)
 		audioTrack->stereo = true;
-	tag = readUint32BE(input);
-	size = readUint32BE(input);
+	tag = input.readUint32BE();
+	size = input.readUint32BE();
 	if (tag == 'TEXT') {
-		fseek(input, size, SEEK_CUR);
-		tag = readUint32BE(input);
-		size = readUint32BE(input);
+		input.seek(size, SEEK_CUR);
+		tag = input.readUint32BE();
+		size = input.readUint32BE();
 		if (tag == 'TEXT') {
-			fseek(input, size, SEEK_CUR);
-			tag = readUint32BE(input);
-			size = readUint32BE(input);
+			input.seek(size, SEEK_CUR);
+			tag = input.readUint32BE();
+			size = input.readUint32BE();
 		}
 	}
 	assert(tag == 'REGN');
-	fseek(input, 8, SEEK_CUR);
-	tag = readUint32BE(input);
-	size = readUint32BE(input);
+	input.seek(8, SEEK_CUR);
+	tag = input.readUint32BE();
+	size = input.readUint32BE();
 	if (tag == 'TEXT') {
-		fseek(input, size, SEEK_CUR);
-		tag = readUint32BE(input);
-		size = readUint32BE(input);
+		input.seek(size, SEEK_CUR);
+		tag = input.readUint32BE();
+		size = input.readUint32BE();
 		if (tag == 'REGN') {
-			fseek(input, 8, SEEK_CUR);
-			tag = readUint32BE(input);
-			size = readUint32BE(input);
+			input.seek(8, SEEK_CUR);
+			tag = input.readUint32BE();
+			size = input.readUint32BE();
 		}
 	}
 	if (tag == 'STOP') {
-		fseek(input, 4, SEEK_CUR);
-		tag = readUint32BE(input);
-		size = readUint32BE(input);
+		input.seek(4, SEEK_CUR);
+		tag = input.readUint32BE();
+		size = input.readUint32BE();
 	}
 	assert(tag == 'DATA');
 }
 
-int32 handleSaudChunk(AudioTrackInfo *audioTrack, FILE *input) {
+int32 CompressScummSan::handleSaudChunk(AudioTrackInfo *audioTrack, File &input) {
 	uint32 tag;
 	int32 size;
-	tag = readUint32BE(input);
-	size = readUint32BE(input);
+	tag = input.readUint32BE();
+	size = input.readUint32BE();
 	assert(tag == 'SAUD');
-	tag = readUint32BE(input);
-	size = readUint32BE(input);
+	tag = input.readUint32BE();
+	size = input.readUint32BE();
 	assert(tag == 'STRK');
-	fseek(input, size, SEEK_CUR);
-	tag = readUint32BE(input);
-	size = readUint32BE(input);
+	input.seek(size, SEEK_CUR);
+	tag = input.readUint32BE();
+	size = input.readUint32BE();
 	assert(tag == 'SDAT');
 	return size;
 }
 
-void handleAudioTrack(int index, int trackId, int frame, int nbframes, FILE *input, const char *outputDir,
+void CompressScummSan::handleAudioTrack(int index, int trackId, int frame, int nbframes, File &input, const char *outputDir,
 					  const char *inputFilename, int &size, int volume, int pan, bool iact) {
 	char tmpPath[1024];
 	AudioTrackInfo *audioTrack = NULL;
@@ -531,21 +480,21 @@
 		audioTrack->sizes = (int *)malloc(nbframes * sizeof(int));
 		memset(audioTrack->sizes, 0, nbframes * sizeof(int));
 		if (iact) {
-			int pos = ftell(input);
+			int pos = input.pos();
 			handleMapChunk(audioTrack, input);
-			size -= (ftell(input) - pos) + 18;
+			size -= (input.pos() - pos) + 18;
 		} else {
 			audioTrack->bits = 8;
 			audioTrack->stereo = false;
 			audioTrack->freq = 22050;
-			int pos = ftell(input);
+			int pos = input.pos();
 			audioTrack->sdatSize = handleSaudChunk(audioTrack, input);
 			audioTrack->sdatSize *= 4;
-			size -= (ftell(input) - pos) + 10;
+			size -= (input.pos() - pos) + 10;
 			audioTrack->lastFrame = frame;
 		}
 		sprintf(tmpPath, "%s/%s_%04d_%03d.tmp", outputDir, inputFilename, frame, trackId);
-		audioTrack->file = fopen(tmpPath, "wb");
+		audioTrack->file.open(tmpPath, "wb");
 		if (!audioTrack->file) {
 			error("error writing temp file");
 		}
@@ -562,8 +511,8 @@
 		}
 	}
 	byte *buffer = (byte *)malloc(size);
-	(void)fread(buffer, size, 1, input);
-	fwrite(buffer, size, 1, audioTrack->file);
+	input.read(buffer, size, 1);
+	audioTrack->file.write(buffer, size, 1);
 	free(buffer);
 	audioTrack->volumes[index] = volume;
 	audioTrack->pans[index] = pan;
@@ -578,13 +527,11 @@
 		audioTrack->sizes[index] *= 2;
 	audioTrack->countFrames++;
 	if ((index + 1) == nbframes) {
-		if (audioTrack->file)
-			fclose(audioTrack->file);
-		audioTrack->file = NULL;
+		audioTrack->file.close();
 	}
 }
 
-void handleDigIACT(FILE *input, int size, const char *outputDir, const char *inputFilename,int flags, int track_flags, int frame) {
+void CompressScummSan::handleDigIACT(File &input, int size, const char *outputDir, const char *inputFilename,int flags, int track_flags, int frame) {
 	int track = readUint16LE(input);
 	int index = readUint16LE(input);
 	int nbframes = readUint16LE(input);
@@ -615,7 +562,7 @@
 	handleAudioTrack(index, trackId, frame, nbframes, input, outputDir, inputFilename, size, volume, pan, true);
 }
 
-void handlePSAD(FILE *input, int size, const char *outputDir, const char *inputFilename, int frame) {
+void CompressScummSan::handlePSAD(File &input, int size, const char *outputDir, const char *inputFilename, int frame) {
 	int trackId = readUint16LE(input);
 	int index = readUint16LE(input);
 	int nbframes = readUint16LE(input);
@@ -626,86 +573,59 @@
 	handleAudioTrack(index, trackId, frame, nbframes, input, outputDir, inputFilename, size, volume, pan, false);
 }
 
-int export_main(compress_scumm_san)(int argc, char *argv[]) {
-	// TODO
-	// Feature set seems more limited than what kCompressionAudioHelp contains
-	const char *helptext = "\nUsage: %s [mode] [mode-params] [-o outpufile = inputfile.san] <inputfile>\n" kCompressionAudioHelp;
+CompressScummSan::CompressScummSan(const std::string &name) : CompressionTool(name) {
+	_IACTpos = 0;
 
-	Filename inpath, outpath;
-	int first_arg = 1;
-	int last_arg = argc - 1;
+	// TODO: Feature set seems more limited than what kCompressionAudioHelp contains
+	_helptext = "\nUsage: " + _name + " [mode] [mode-params] [-o outpufile = inputfile.san] <inputfile>\n" kCompressionAudioHelp;
+}
 
-	parseHelpArguments(argv, argc, helptext);
+void CompressScummSan::execute() {
+	if (_format == AUDIO_FLAC)
+		error("Only ogg vorbis and MP3 is supported for this tool.");
 
-	// compression mode
-	AudioFormat compMode = process_audio_params(argc, argv, &first_arg);
+	// Check input
+	if (_inputPaths.size() == 1)
+		error("One input file expected!");
+	Filename inpath(_inputPaths[0]);
+	Filename &outpath = _outputPath;
 
-	// TODO
-	// Support flac too?
-	if (compMode == AUDIO_VORBIS)
-		_oggMode = true;
-	else if (compMode != AUDIO_MP3)
-		notice("Only ogg vorbis and MP3 is supported for this tool.");
-	if (compMode == AUDIO_NONE) {
-		// Unknown mode (failed to parse arguments), display help and exit
-		displayHelp(helptext, argv[0]);
-	}
-
-	uint32 tag;
-
-	// Now we try to find the proper output file
-	// also make sure we skip those arguments
-	if (parseOutputFileArguments(&outpath, argv, argc, first_arg))
-		first_arg += 2;
-	else if (parseOutputFileArguments(&outpath, argv, argc, last_arg - 2))
-		last_arg -= 2;
-	else 
-		// Just leave it empty, we just change extension of input file
-		;
-
-	inpath.setFullPath(argv[first_arg]);
-
 	if (outpath.empty()) {
 		// Change extension for output
 		outpath = inpath;
 		outpath.setExtension(".san");
 	}
 
-	FILE *input = fopen(inpath.getFullPath().c_str(), "rb");
-	if (!input) {
-		error("Cannot open file: %s", inpath.getFullPath().c_str());
-	}
+	File input(inpath, "rb");
+	File output(outpath, "wb");
 
-	FILE *output = fopen(outpath.getFullPath().c_str(), "wb");
-	if (!output) {
-		error("Cannot open file: %s", outpath.getFullPath().c_str());
-	}
-
 	Filename flupath(inpath);
 	flupath.setExtension(".flu");
 
-	FILE *flu_in = NULL;
-	FILE *flu_out = NULL;
-	flu_in = fopen(flupath.getFullPath().c_str(), "rb");
+	File flu_in;
+	
+	try {
+		flu_in.open(flupath, "rb");
+	} catch(...) {
+		// pass
+	}
 
-	if (flu_in) {
+	File flu_out;
+	if (flu_in.isOpen()) {
 		flupath = outpath;
 		flupath.setExtension(".flu");
-		flu_out = fopen(flupath.getFullPath().c_str(), "wb");
-		if (!flu_out) {
-			error("Cannot open ancillary file: %s", flupath.getFullPath().c_str());
-		}
+		flu_out.open(flupath, "wb");
 	}
 
 	int32 l, size;
 
-	writeUint32BE(output, readUint32BE(input)); // ANIM
-	int32 animChunkSize = readUint32BE(input); // ANIM size
-	writeUint32BE(output, animChunkSize);
+	output.writeUint32BE(input.readUint32BE()); // ANIM
+	int32 animChunkSize = input.readUint32BE(); // ANIM size
+	output.writeUint32BE(animChunkSize);
 
-	writeUint32BE(output, readUint32BE(input)); // AHDR
-	size = readUint32BE(input);
-	writeUint32BE(output, size); // AHDR size
+	output.writeUint32BE(input.readUint32BE()); // AHDR
+	size = input.readUint32BE();
+	output.writeUint32BE(size); // AHDR size
 	writeUint16BE(output, readUint16BE(input)); // version
 	int32 nbframes = readUint16LE(input); // number frames
 	writeUint16LE(output, nbframes);
@@ -716,8 +636,8 @@
 
 	FrameInfo *frameInfo = (FrameInfo *)malloc(sizeof(FrameInfo) * nbframes);
 
-	memset(_audioTracks, 0, sizeof(AudioTrackInfo) * MAX_TRACKS);
-	for (l = 0; l < MAX_TRACKS; l++) {
+	memset(_audioTracks, 0, sizeof(AudioTrackInfo) * COMPRESS_SCUMM_SAN_MAX_TRACKS);
+	for (l = 0; l < COMPRESS_SCUMM_SAN_MAX_TRACKS; l++) {
 		_audioTracks[l].animFrame = -1;
 	}
 
@@ -725,28 +645,28 @@
 	int fps = 0;
 
 	for (l = 0; l < nbframes; l++) {
-		printf("frame: %d\n", l);
+		print("frame: %d\n", l);
 		bool first_fobj = true;
-		tag = readUint32BE(input); // chunk tag
+		uint32 tag = input.readUint32BE(); // chunk tag
 		assert(tag == 'FRME');
-		writeUint32BE(output, tag); // FRME
-		int32 frameSize = readUint32BE(input); // FRME size
+		output.writeUint32BE(tag); // FRME
+		int32 frameSize = input.readUint32BE(); // FRME size
 		frameInfo[l].frameSize = frameSize;
-		frameInfo[l].offsetOutput = ftell(output);
+		frameInfo[l].offsetOutput = output.pos();
 		frameInfo[l].fobjDecompressedSize = 0;
 		frameInfo[l].fobjCompressedSize = 0;
 		frameInfo[l].lessIACTSize = 0;
 		frameInfo[l].lessPSADSize = 0;
-		writeUint32BE(output, frameSize);
+		output.writeUint32BE(frameSize);
 		for (;;) {
-			tag = readUint32BE(input); // chunk tag
+			tag = input.readUint32BE(); // chunk tag
 			if (feof(input))
 				break;
 			if (tag == 'FRME') {
-				fseek(input, -4, SEEK_CUR);
+				input.seek(-4, SEEK_CUR);
 				break;
 			} else if ((tag == 'FOBJ') && (first_fobj)) {
-				size = readUint32BE(input); // FOBJ size
+				size = input.readUint32BE(); // FOBJ size
 				if ((size & 1) != 0)
 					size++;
 				first_fobj = false;
@@ -764,9 +684,9 @@
 					outputSize++;
 				frameInfo[l].fobjDecompressedSize = size;
 				frameInfo[l].fobjCompressedSize = outputSize;
-				writeUint32BE(output, 'ZFOB');
-				writeUint32BE(output, outputSize + 4);
-				writeUint32BE(output, size);
+				output.writeUint32BE('ZFOB');
+				output.writeUint32BE(outputSize + 4);
+				output.writeUint32BE(size);
 				for (unsigned int k = 0; k < outputSize; k++) {
 					writeByte(output, *(zlibOutputBuffer + k)); // compressed FOBJ datas
 				}
@@ -774,7 +694,7 @@
 				free(zlibOutputBuffer);
 				continue;
 			} else if ((tag == 'IACT') && (!flu_in)) {
-				size = readUint32BE(input); // chunk size
+				size = input.readUint32BE(); // chunk size
 				int code = readUint16LE(input);
 				int flags = readUint16LE(input);
 				int unk = readUint16LE(input);
@@ -786,21 +706,21 @@
 					tracksCompress = true;
 					fps = 12;
 				} else {
-					fseek(input, -12, SEEK_CUR);
+					input.seek(-12, SEEK_CUR);
 					goto skip;
 				}
 
 				if ((size & 1) != 0) {
-					fseek(input, 1, SEEK_CUR);
+					input.seek(1, SEEK_CUR);
 					size++;
 				}
 				frameInfo[l].lessIACTSize += size + 8;
 				continue;
 			} else if ((tag == 'PSAD') && (!flu_in)) {
-				size = readUint32BE(input); // chunk size
+				size = input.readUint32BE(); // chunk size
 				handlePSAD(input, size, outpath.getPath().c_str(), inpath.getFullName().c_str(), l);
 				if ((size & 1) != 0) {
-					fseek(input, 1, SEEK_CUR);
+					input.seek(1, SEEK_CUR);
 					size++;
 				}
 				frameInfo[l].lessPSADSize += size + 8;
@@ -808,9 +728,9 @@
 				fps = 10;
 			} else {
 skip:
-				size = readUint32BE(input); // chunk size
-				writeUint32BE(output, tag);
-				writeUint32BE(output, size);
+				size = input.readUint32BE(); // chunk size
+				output.writeUint32BE(tag);
+				output.writeUint32BE(size);
 				if ((size & 1) != 0)
 					size++;
 				for (int k = 0; k < size; k++) {
@@ -830,7 +750,7 @@
 		char tmpPath[1024];
 		writeWaveHeader(_waveDataSize);
 		sprintf(tmpPath, "%s/%s", outpath.getPath().c_str(), inpath.getFullName().c_str());
-		if (_oggMode)
+		if (_format == AUDIO_VORBIS)
 			encodeSanWaveWithOgg(tmpPath);
 		else
 			encodeSanWaveWithLame(tmpPath);
@@ -838,9 +758,9 @@
 		unlink(tmpPath);
 	}
 
-	fclose(input);
+	input.close();
 
-	printf("Fixing frames header...\n");
+	print("Fixing frames header...\n");
 	int32 sumDiff = 0;
 	for (l = 0; l < nbframes; l++) {
 		int32 diff = 0;
@@ -853,47 +773,40 @@
 		if (frameInfo[l].lessPSADSize != 0) {
 			diff += frameInfo[l].lessPSADSize;
 		}
-		fseek(output, frameInfo[l].offsetOutput, SEEK_SET);
+		output.seek(frameInfo[l].offsetOutput, SEEK_SET);
 		sumDiff += diff;
 		if (diff != 0)
-			writeUint32BE(output, frameInfo[l].frameSize - diff);
+			output.writeUint32BE(frameInfo[l].frameSize - diff);
 	}
-	printf("done.\n");
+	print("done.\n");
 
-	printf("Fixing anim header...\n");
-	fseek(output, 4, SEEK_SET);
-	writeUint32BE(output, animChunkSize - sumDiff);
-	printf("done.\n");
+	print("Fixing anim header...\n");
+	output.seek(4, SEEK_SET);
+	output.writeUint32BE(animChunkSize - sumDiff);
+	print("done.\n");
 
-	if (flu_in) {
-		printf("Fixing flu offsets...\n");
-		fseek(flu_in, 0, SEEK_END);
-		int fsize = ftell(flu_in);
-		fseek(flu_in, 0, SEEK_SET);
+	if (flu_in.isOpen()) {
+		print("Fixing flu offsets...\n");
+		int fsize = flu_in.size();
 		for (int k = 0; k < fsize; k++) {
-			writeByte(flu_out, readByte(flu_in));
+			flu_out.writeByte(readByte(flu_in));
 		}
-		fseek(flu_out, 0x324, SEEK_SET);
+		flu_out.seek(0x324, SEEK_SET);
 		for (l = 0; l < nbframes; l++) {
-			writeUint32LE(flu_out, frameInfo[l].offsetOutput - 4);
+			flu_out.writeUint32LE(frameInfo[l].offsetOutput - 4);
 		}
-		fclose(flu_in);
-		fclose(flu_out);
-		printf("done.\n");
+		print("done.\n");
 	}
 
 	free(frameInfo);
 
-	fclose(output);
-
-	printf("compression done.\n");
-
-	return 0;
+	print("compression done.\n");
 }
 
 #ifdef STANDALONE_MAIN
 int main(int argc, char *argv[]) {
-        return export_main(compress_scumm_san)(argc, argv);
+	CompressScummSan scummsan(argv[0]);
+	return scummsan.run(argc, argv);
 }
 #endif
 

Modified: tools/branches/gsoc2009-gui/compress_scumm_sou.cpp
===================================================================
--- tools/branches/gsoc2009-gui/compress_scumm_sou.cpp	2009-07-10 00:49:13 UTC (rev 42322)
+++ tools/branches/gsoc2009-gui/compress_scumm_sou.cpp	2009-07-10 00:49:27 UTC (rev 42323)
@@ -21,7 +21,7 @@
  *
  */
 
-#include "compress.h"
+#include "compress_scumm_sou.h"
 
 
 static const char f_hdr[] = {
@@ -32,189 +32,153 @@
 #define OUTPUT_OGG	"monster.sog"
 #define OUTPUT_FLAC	"monster.sof"
 
-static const char *g_output_filename = OUTPUT_MP3;
-
 #define TEMP_DAT	"tempfile.dat"
 #define TEMP_IDX	"tempfile.idx"
 
-static FILE *input, *output_idx, *output_snd;
-
-static AudioFormat gCompMode = AUDIO_MP3;
-
-
-void end_of_file(const char *inputPath) {
-	FILE *in;
-	int idx_size = ftell(output_idx);
+void CompressScummSou::end_of_file(const char *inputPath) {
+	int idx_size = _output_idx.pos();
 	size_t size;
 	char buf[2048];
 
-	fclose(output_snd);
-	fclose(output_idx);
+	_output_snd.close();
+	_output_idx.close();
 
-	output_idx = fopen(g_output_filename, "wb");
-	writeUint32BE(output_idx, (uint32)idx_size);
+	_output_idx.open(_audioOuputFilename, "wb");
+	_output_idx.writeUint32BE((uint32)idx_size);
 
-	in = fopen(TEMP_IDX, "rb");
-	while ((size = fread(buf, 1, 2048, in)) > 0) {
-		fwrite(buf, 1, size, output_idx);
+	File in(TEMP_IDX, "rb");
+	while ((size = in.readN(buf, 1, 2048)) > 0) {
+		_output_idx.write(buf, 1, size);
 	}
-	fclose(in);
-	in = fopen(TEMP_DAT, "rb");
-	while ((size = fread(buf, 1, 2048, in)) > 0) {
-		fwrite(buf, 1, size, output_idx);
+
+	in.open(TEMP_DAT, "rb");
+	while ((size = in.readN(buf, 1, 2048)) > 0) {
+		_output_idx.write(buf, 1, size);
 	}
-	fclose(in);
-	fclose(output_idx);
-	fclose(input);
+	in.close();
+	_output_idx.close();
+	_input.close();
 
 	/* And some clean-up :-) */
 	unlink(TEMP_IDX);
 	unlink(TEMP_DAT);
 	unlink(TEMP_RAW);
 	unlink(tempEncoded);
-
-	exit(-1);
 }
 
-void append_byte(int size, char buf[]) {
+void CompressScummSou::append_byte(int size, char buf[]) {
 	int i;
 	for (i = 0; i < (size - 1); i++)
 		buf[i] = buf[i + 1];
-	buf[i] = fgetc(input);
+	buf[i] = _input.readByte();
 }
 
-void get_part(const char *inputPath) {
-	FILE *f;
+void CompressScummSou::get_part(const char *inputPath) {
 	uint32 tot_size;
 	char outname[256];
 	int size;
 	char fbuf[2048];
 
 	char buf[2048];
-	int pos = ftell(input);
+	int pos = _input.pos();
 	uint32 tags;
 
 	/* Scan for the VCTL header */
-	fread(buf, 1, 4, input);
+	_input.read(buf, 1, 4);
 	/* The demo (snmdemo) and floppy version of Sam & Max use VTTL */
 	while (memcmp(buf, "VCTL", 4)&&memcmp(buf, "VTTL", 4)) {
 		pos++;
 		append_byte(4, buf);
-		if (feof(input))
+		if (feof(_input)) {
 			end_of_file(inputPath);
+			return;
+		}
 	}
 
-	tags = readUint32BE(input);
+	tags = _input.readUint32BE();
 	assert(tags >= 8);
 	tags -= 8;
 
-	writeUint32BE(output_idx, (uint32)pos);
-	writeUint32BE(output_idx, (uint32)ftell(output_snd));
-	writeUint32BE(output_idx, tags);
+	_output_idx.writeUint32BE((uint32)pos);
+	_output_idx.writeUint32BE((uint32)_output_snd.pos());
+	_output_idx.writeUint32BE(tags);
 	while (tags > 0) {
-		fputc(fgetc(input), output_snd);
+		fputc(fgetc(_input), _output_snd);
 		tags--;
 	}
 
-	fread(buf, 1, 8, input);
+	_input.read(buf, 1, 8);
 	if (!memcmp(buf, "Creative", 8))
-		fseek(input, 18, SEEK_CUR);
+		_input.seek(18, SEEK_CUR);
 	else if (!memcmp(buf, "VTLK", 4))
-		fseek(input, 26, SEEK_CUR);
+		_input.seek(26, SEEK_CUR);
 	else
 		error("Unexpected data encountered");
-	printf("Voice file found (pos = %d) :", pos);
+	print("Voice file found (pos = %d) :", pos);
 
 	/* Convert the VOC data */
-	extractAndEncodeVOC(TEMP_RAW, input, gCompMode);
+	extractAndEncodeVOC(TEMP_RAW, _input, _format);
 
 	/* Append the converted data to the master output file */
 	sprintf(outname, tempEncoded);
-	f = fopen(outname, "rb");
+	File f(outname, "rb");
 	tot_size = 0;
-	while ((size = fread(fbuf, 1, 2048, f)) > 0) {
+	while ((size = f.read(fbuf, 1, 2048)) > 0) {
 		tot_size += size;
-		fwrite(fbuf, 1, size, output_snd);
+		_output_snd.write(fbuf, 1, size);
 	}
-	fclose(f);
 
-	writeUint32BE(output_idx, tot_size);
+	_output_idx.writeUint32BE(tot_size);
 }
 
+CompressScummSou::CompressScummSou(const std::string &name) : CompressionTool(name) {
+	_audioOuputFilename = OUTPUT_MP3;
 
-int export_main(compress_scumm_sou)(int argc, char *argv[]) {
-	const char *helptext = "\nUsage: %s [mode] [mode params] monster.sou\n" kCompressionAudioHelp;
+	_helptext = "\nUsage: " + _name + " [mode] [mode params] monster.sou\n" kCompressionAudioHelp;
+}
 
+void CompressScummSou::execute() {
 	char buf[2048];
-	Filename inpath, outpath;
-	int first_arg = 1;
-	int last_arg = argc - 1;
 
-	// Should we help the user?
-	parseHelpArguments(argv, argc, helptext);
+	// Check input
+	if (_inputPaths.size() == 1)
+		error("One input file expected!");
+	Filename inpath(_inputPaths[0]);
+	Filename &outpath = _outputPath;
 
-	// compression mode
-	gCompMode = process_audio_params(argc, argv, &first_arg);
-
-	if (gCompMode == AUDIO_NONE) {
-		// Unknown mode (failed to parse arguments), display help and exit
-		displayHelp(helptext, argv[0]);
+	switch (_format) {
+	case AUDIO_MP3:
+		_audioOuputFilename = OUTPUT_MP3;
+		break;
+	case AUDIO_VORBIS:
+		_audioOuputFilename = OUTPUT_OGG;
+		break;
+	case AUDIO_FLAC:
+		_audioOuputFilename = OUTPUT_FLAC;
+		break;
+	default:
+		throw ToolException("Unknown audio format");
+		break;
 	}
 
-	// Now we try to find the proper output file
-	// also make sure we skip those arguments
-	if (parseOutputFileArguments(&outpath, argv, argc, first_arg))
-		first_arg += 2;
-	else if (parseOutputFileArguments(&outpath, argv, argc, last_arg - 2))
-		last_arg -= 2;
-	else {
-		switch(gCompMode) {
-		case AUDIO_MP3:
-			g_output_filename = OUTPUT_MP3;
-			break;
-		case AUDIO_VORBIS:
-			g_output_filename = OUTPUT_OGG;
-			break;
-		case AUDIO_FLAC:
-			g_output_filename = OUTPUT_FLAC;
-			break;
-		default:
-			displayHelp(helptext, argv[0]);
-			break;
-		}
-	}
+	_input.open(inpath.getFullPath().c_str(), "rb");
+	_output_idx.open(TEMP_IDX, "wb");
+	_output_snd.open(TEMP_DAT, "wb");
 
-	inpath.setFullPath(argv[first_arg]);
-
-	input = fopen(inpath.getFullPath().c_str(), "rb");
-	if (!input) {
-		error("Cannot open file: %s", inpath.getFullPath().c_str());
-	}
-
-	output_idx = fopen(TEMP_IDX, "wb");
-	if (!output_idx) {
-		error("Cannot open file " TEMP_IDX " for write" );
-	}
-	output_snd = fopen(TEMP_DAT, "wb");
-	if (!output_snd) {
-		error("Cannot open file " TEMP_DAT " for write");
-	}
-
 	/* Get the 'SOU ....' header */
-	fread(buf, 1, 8, input);
+	_input.read(buf, 1, 8);
 	if (strncmp(buf, f_hdr, 8)) {
 		error("Bad SOU");
 	}
 
-	while (1)
+	while (true)
 		get_part(inpath.getFullPath().c_str());
-
-	return 0;
 }
 
 #ifdef STANDALONE_MAIN
 int main(int argc, char *argv[]) {
-	return export_main(compress_scumm_sou)(argc, argv);
+	CompressScummSou scummsou(argv[0]);
+	return scummsou.run(argc, argv);
 }
 #endif
 

Modified: tools/branches/gsoc2009-gui/compress_sword1.cpp
===================================================================
--- tools/branches/gsoc2009-gui/compress_sword1.cpp	2009-07-10 00:49:13 UTC (rev 42322)
+++ tools/branches/gsoc2009-gui/compress_sword1.cpp	2009-07-10 00:49:27 UTC (rev 42323)
@@ -20,15 +20,13 @@
  *
  */
 
-#include "compress.h"
+#include "compress_sword1.h"
 
 #define READ_BE_UINT32(x) \
 	((((uint8*)(x))[0] << 24) | (((uint8*)(x))[1] << 16) | (((uint8*)(x))[2] << 8) | (((uint8*)(x))[3] << 0))
 
 #define TOTAL_TUNES 269
 
-const char *tempOutName;
-
 typedef struct {
 	char fileName[8];
 	bool missing; /* Some of the music files seem to have been removed from the game
@@ -307,13 +305,13 @@
 	{ "RM3D", false }
 };
 
-int16 *uncompressSpeech(FILE *clu, uint32 idx, uint32 cSize, uint32 *returnSize) {
+int16 *CompressSword1::uncompressSpeech(File &clu, uint32 idx, uint32 cSize, uint32 *returnSize) {
 	uint32 resSize, srcPos;
 	int16 *srcData, *dstData, *dstPos;
 	uint32 headerPos = 0;
 	int16 length, cnt;
 	uint8 *fBuf = (uint8 *)malloc(cSize);
-	fseek(clu, idx, SEEK_SET);
+	clu.seek(idx, SEEK_SET);
 	assert(fread(fBuf, 1, cSize, clu) == cSize);
 	while ((READ_BE_UINT32(fBuf + headerPos) != 'data') && (headerPos < 100))
 		headerPos++;
@@ -349,26 +347,23 @@
 	}
 }
 
-uint8 *convertData(uint8 *rawData, uint32 rawSize, AudioFormat compMode, uint32 *resSize) {
-	FILE *temp;
+uint8 *CompressSword1::convertData(uint8 *rawData, uint32 rawSize, uint32 *resSize) {
 	uint8 *resBuf;
 
 	uint32 size;
-	temp = fopen(TEMP_RAW, "wb");
-	assert(fwrite(rawData, 1, rawSize, temp) == rawSize);
-	fclose(temp);
-	encodeAudio(TEMP_RAW, true, 11025, tempOutName, compMode);
-	temp = fopen(tempOutName, "rb");
-	fseek(temp, 0, SEEK_END);
-	*resSize = size = ftell(temp);
+	File temp(TEMP_RAW, "wb");
+	assert(temp.write(rawData, 1, rawSize) == rawSize);
+	encodeAudio(TEMP_RAW, true, 11025, _audioOuputFilename.c_str(), _format);
+	temp.open(_audioOuputFilename, "rb");
+	temp.seek(0, SEEK_END);
+	*resSize = size = temp.pos();
 	resBuf = (uint8*)malloc(size);
-	fseek(temp, 0, SEEK_SET);
-	fread(resBuf, 1, size, temp);
-	fclose(temp);
+	temp.seek(0, SEEK_SET);
+	temp.read(resBuf, 1, size);
 	return resBuf;
 }
 
-void convertClu(FILE *clu, FILE *cl3, AudioFormat compMode) {
+void CompressSword1::convertClu(File &clu, File &cl3) {
 	uint32 *cowHeader;
 	uint32 numRooms;
 	uint32 numSamples;
@@ -377,7 +372,7 @@
 	uint32 smpSize, mp3Size;
 	uint8 *smpData, *mp3Data;
 
-	uint32 headerSize = readUint32LE(clu);
+	uint32 headerSize = clu.readUint32LE();
 
 	assert(!(headerSize & 3));
 	cowHeader = (uint32*)malloc(headerSize);
@@ -390,15 +385,15 @@
 
 	/* The samples are divided into rooms and samples. We don't care about the room indexes at all. */
 	/* We simply copy them and go to the sample-index data. */
-	writeUint32LE(cl3, headerSize);
+	cl3.writeUint32LE(headerSize);
 	for (cnt = 0; cnt < numRooms; cnt++)
-		writeUint32LE(cl3, cowHeader[cnt]);
-	writeUint32LE(cl3, cowHeader[numRooms]);
+		cl3.writeUint32LE(cowHeader[cnt]);
+	cl3.writeUint32LE(cowHeader[numRooms]);
 
 	numSamples = (((headerSize / 4) - numRooms) / 2) - 1;
 	for (cnt = 0; cnt < numSamples * 2; cnt++) {
 		/* This is where we'll put the sample index data later. */
-		writeUint32BE(cl3, 0xdeadbeefL);
+		cl3.writeUint32BE(0xdeadbeefL);
 	}
 	cl3Index = (uint32*)malloc(numSamples * 8);
 	memset(cl3Index, 0, numSamples * 8);
@@ -406,17 +401,17 @@
 	sampleIndex = cowHeader + numRooms + 1;
 	/* This points to the sample index table. 8 bytes each (4 bytes size and then 4 bytes file index) */
 
-	printf("converting %d samples\n", numSamples);
+	print("converting %d samples\n", numSamples);
 
 	for (cnt = 0; cnt < numSamples; cnt++) {
 		if (sampleIndex[cnt << 1] | sampleIndex[(cnt << 1) | 1]) {
-			printf("sample %5d: \n", cnt);
+			print("sample %5d: \n", cnt);
 			smpData = (uint8*)uncompressSpeech(clu, sampleIndex[cnt << 1] + headerSize, sampleIndex[(cnt << 1) | 1], &smpSize);
 			if ((!smpData) || (!smpSize))
 				error("unable to handle speech sample %d!\n", cnt);
 
-			mp3Data = convertData(smpData, smpSize, compMode, &mp3Size);
-			cl3Index[cnt << 1] = ftell(cl3);
+			mp3Data = convertData(smpData, smpSize, &mp3Size);
+			cl3Index[cnt << 1] = cl3.pos();
 			cl3Index[(cnt << 1) | 1] = mp3Size;
 			assert(fwrite(mp3Data, 1, mp3Size, cl3) == mp3Size);
 
@@ -424,18 +419,18 @@
 			free(mp3Data);
 		} else {
 			cl3Index[cnt << 1] = cl3Index[(cnt << 1) | 1] = 0;
-			printf("sample %5d: skipped\n", cnt);
+			print("sample %5d: skipped\n", cnt);
 		}
 	}
-	fseek(cl3, (numRooms + 2) * 4, SEEK_SET);	/* Now write the sample index into the CL3 file */
+	cl3.seek((numRooms + 2) * 4, SEEK_SET);	/* Now write the sample index into the CL3 file */
 	for (cnt = 0; cnt < numSamples * 2; cnt++)
-		writeUint32LE(cl3, cl3Index[cnt]);
+		cl3.writeUint32LE(cl3Index[cnt]);
 	free(cl3Index);
 	free(cowHeader);
 }
 
-void compressSpeech(AudioFormat compMode, const Filename *inpath, const Filename *outpath) {
-	FILE *clu, *cl3 = NULL;
+void CompressSword1::compressSpeech(const Filename *inpath, const Filename *outpath) {
+	File clu, cl3;
 	int i;
 	char cluName[256], outName[256];
 
@@ -443,60 +438,51 @@
 
 	for (i = 1; i <= 2; i++) {
 		sprintf(cluName, "%s/SPEECH/SPEECH%d.CLU", inpath->getPath().c_str(), i);
-		clu = fopen(cluName, "rb");
-		if (!clu) {
-			printf("Unable to open \"SPEECH%d.CLU\".\n", i);
-			printf("Please copy the \"SPEECH.CLU\" from CD %d\nand rename it to \"SPEECH%d.CLU\".\n", i, i);
-		} else {
-			switch (compMode) {
-			case AUDIO_MP3:
-				sprintf(outName, "%s/SPEECH/SPEECH%d.%s", outpath->getPath().c_str(), i, "CL3");
-				break;
-			case AUDIO_VORBIS:
-				sprintf(outName, "%s/SPEECH/SPEECH%d.%s", outpath->getPath().c_str(), i, "CLV");
-				break;
-			case AUDIO_FLAC:
-				sprintf(outName, "%s/SPEECH/SPEECH%d.%s", outpath->getPath().c_str(), i, "CLF");
-				break;
-			default:
-				error("Unknown encoding method");
-			}
+		try {
+			clu.open(cluName, "rb");
+		} catch(FileException &) {
+			print("Unable to open \"SPEECH%d.CLU\".\n", i);
+			print("Please copy the \"SPEECH.CLU\" from CD %d\nand rename it to \"SPEECH%d.CLU\".\n", i, i);
+			continue;
+		}
 
-			cl3 = fopen(outName, "wb");
-			if (!cl3) {
-				printf("Unable to create file \"%s\".\n", outName);
-				printf("Please make sure you've got write permission in this directory.\n");
-			} else {
-				printf("Converting CD %d...\n", i);
-				convertClu(clu, cl3, compMode);
-			}
+		switch (_format) {
+		case AUDIO_MP3:
+			sprintf(outName, "%s/SPEECH/SPEECH%d.%s", outpath->getPath().c_str(), i, "CL3");
+			break;
+		case AUDIO_VORBIS:
+			sprintf(outName, "%s/SPEECH/SPEECH%d.%s", outpath->getPath().c_str(), i, "CLV");
+			break;
+		case AUDIO_FLAC:
+			sprintf(outName, "%s/SPEECH/SPEECH%d.%s", outpath->getPath().c_str(), i, "CLF");
+			break;
+		default:
+			error("Unknown encoding method");
 		}
-		if (clu)
-			fclose(clu);
-		if (cl3)
-			fclose(cl3);
+
+		cl3.open(outName, "wb");
+		if (!cl3) {
+			print("Unable to create file \"%s\".\n", outName);
+			print("Please make sure you've got write permission in this directory.\n");
+		} else {
+			print("Converting CD %d...\n", i);
+			convertClu(clu, cl3);
+		}
 	}
 	unlink(TEMP_RAW);
-	unlink(tempOutName);
+	unlink(_audioOuputFilename.c_str());
 }
 
-void compressMusic(AudioFormat compMode, const Filename *inpath, const Filename *outpath) {
+void CompressSword1::compressMusic(const Filename *inpath, const Filename *outpath) {
 	int i;
-	FILE *inf;
 	char fNameIn[256], fNameOut[256];
 
 	for (i = 0; i < TOTAL_TUNES; i++) {
 		sprintf(fNameIn, "%s/MUSIC/%s.WAV", inpath->getPath().c_str(), musicNames[i].fileName);
-		inf = fopen(fNameIn, "rb");
+		try {
+			File inf(fNameIn, "rb");
 
-		if (!inf) {
-			if (!musicNames[i].missing) {
-				printf("unable to open file \"%s\"\n", fNameIn);
-			}
-		} else {
-			fclose(inf);
-
-			switch (compMode) {
+			switch (_format) {
 			case AUDIO_MP3:
 				sprintf(fNameOut, "%s/MUSIC/%s.%s", outpath->getPath().c_str(), musicNames[i].fileName, "MP3");
 				break;
@@ -510,13 +496,15 @@
 				error("Unknown encoding method");
 			}
 
-			printf("encoding file (%3d/%d) %s -> %s\n", i + 1, TOTAL_TUNES, musicNames[i].fileName, fNameOut);
-			encodeAudio(fNameIn, false, -1, fNameOut, compMode);
+			print("encoding file (%3d/%d) %s -> %s\n", i + 1, TOTAL_TUNES, musicNames[i].fileName, fNameOut);
+			encodeAudio(fNameIn, false, -1, fNameOut, _format);
+		} catch(FileException& err) {
+			print(err.what());
 		}
 	}
 }
 
-void checkFilesExist(bool checkSpeech, bool checkMusic, const Filename *inpath) {
+void CompressSword1::checkFilesExist(bool checkSpeech, bool checkMusic, const Filename *inpath) {
 	int i;
 	FILE *testFile;
 	char fileName[256];
@@ -534,12 +522,12 @@
 		}
 
 		if (!speechFound) {
-			printf("Unable to find speech files.\n");
-			printf("Please copy the SPEECH.CLU files from Broken Sword CD1 and CD2\n");
-			printf("into the \"SPEECH\" subdirectory and rename them to\n");
-			printf("SPEECH1.CLU and SPEECH2.CLU\n\n");
-			printf("If your OS is case-sensitive, make sure the filenames\n");
-			printf("and directorynames are all upper-case.\n\n");
+			print("Unable to find speech files.\n");
+			print("Please copy the SPEECH.CLU files from Broken Sword CD1 and CD2\n");
+			print("into the \"SPEECH\" subdirectory and rename them to\n");
+			print("SPEECH1.CLU and SPEECH2.CLU\n\n");
+			print("If your OS is case-sensitive, make sure the filenames\n");
+			print("and directorynames are all upper-case.\n\n");
 		}
 	}
 
@@ -555,99 +543,80 @@
 		}
 
 		if (!musicFound) {
-			printf("Unable to find music files.\n");
-			printf("Please copy the music files from Broken Sword CD1 and CD2\n");
-			printf("into the \"MUSIC\" subdirectory.\n");
-			printf("If your OS is case-sensitive, make sure the filenames\n");
-			printf("and directorynames are all upper-case.\n");
+			print("Unable to find music files.\n");
+			print("Please copy the music files from Broken Sword CD1 and CD2\n");
+			print("into the \"MUSIC\" subdirectory.\n");
+			print("If your OS is case-sensitive, make sure the filenames\n");
+			print("and directorynames are all upper-case.\n");
 		}
 	}
 
 	if ((checkSpeech && (!speechFound)) || (checkMusic && (!musicFound))) {
-		printf("\nUse --help for more information\n");
-		exit(2);
+		throw ToolException("Use --help for more information");
 	}
 }
 
-int export_main(compress_sword1)(int argc, char *argv[]) {
+CompressSword1::CompressSword1(const std::string &name) : CompressionTool(name) {
+	_compSpeech = true;
+	_compMusic = true;
 
-	const char *helptext = "\nUsage: %s [only] [mode] [mode params] [-o outputdir] <inputdir>\n"
+	_helptext = "\nUsage: %s [only] [mode] [mode params] [-o outputdir] <inputdir>\n"
 		"only can be either:\n"
 		" --speech-only  only encode speech clusters\n"
 		" --music-only   only encode music files\n"
 		kCompressionAudioHelp;
+}
 
-	AudioFormat compMode = AUDIO_MP3;
-	Filename inpath, outpath;
-	int first_arg = 1;
-	int last_arg = argc - 1;
-	bool compMusic = true, compSpeech = true;
-
-	// Should we display some help perhaps?
-	parseHelpArguments(argv, argc, helptext);
-
-	// Check extra arguments
-	if (strcmp(argv[first_arg], "--speech-only") == 0) {
-		compMusic = false;
-		++first_arg;
-	} else if (strcmp(argv[first_arg], "--music-only") == 0) {
-		compSpeech = false;
-		++first_arg;
+void CompressSword1::parseExtraArguments() {
+	if(_arguments[_arguments_parsed] == "--speech-only") {
+		_compMusic = false;
+		++_arguments_parsed;
 	}
-
-	// compression mode
-	compMode = process_audio_params(argc, argv, &first_arg);
-
-	if (compMode == AUDIO_NONE) {
-		// Unknown mode (failed to parse arguments), display help and exit
-		displayHelp(helptext, argv[0]);
+	if(_arguments[_arguments_parsed] == "--music-only") {
+		_compSpeech = false;
+		++_arguments_parsed;
 	}
+}
 
-	// Now we try to find the proper output dir
-	// also make sure we skip those arguments
-	if (parseOutputDirectoryArguments(&outpath, argv, argc, first_arg))
-		first_arg += 2;
-	else if (parseOutputDirectoryArguments(&outpath, argv, argc, last_arg - 2))
-		last_arg -= 2;
-	
-	switch(compMode) {
+void CompressSword1::execute() {
+	// Check input
+	if (_inputPaths.size() == 1)
+		error("One input file expected!");
+	Filename inpath(_inputPaths[0]);
+	Filename &outpath = _outputPath;
+
+	switch (_format) {
 	case AUDIO_MP3:
-		tempOutName = TEMP_MP3;
+		_audioOuputFilename = TEMP_MP3;
 		break;
 	case AUDIO_VORBIS:
-		tempOutName = TEMP_OGG;
+		_audioOuputFilename = TEMP_OGG;
 		break;
 	case AUDIO_FLAC:
-		tempOutName = TEMP_FLAC;
+		_audioOuputFilename = TEMP_FLAC;
 		break;
 	default:
-		// Never happends, avoid warnings
+		throw ToolException("Unknown audio format");
 		break;
 	}
 
-	inpath.setFullPath(argv[first_arg]);
-
 	if (outpath.empty())
 		// Extensions change between the in/out files, so we can use the same directory
 		outpath = inpath;
 
 	/* Do a quick check to see if we can open any files at all */
-	checkFilesExist(compSpeech, compMusic, &inpath);
+	checkFilesExist(_compSpeech, _compMusic, &inpath);
 
-	if (compSpeech) {
-		compressSpeech(compMode, &inpath, &outpath);
-	}
-
-	if (compMusic) {
-		compressMusic(compMode, &inpath, &outpath);
-	}
-
-	return EXIT_SUCCESS;
+	if (_compSpeech)
+		compressSpeech(&inpath, &outpath);
+	if (_compMusic)
+		compressMusic(&inpath, &outpath);
 }
 
 #ifdef STANDALONE_MAIN
 int main(int argc, char *argv[]) {
-	return export_main(compress_sword1)(argc, argv);
+	CompressSword1 sword1(argv[0]);
+	return sword1.run(argc, argv);
 }
 #endif
 

Modified: tools/branches/gsoc2009-gui/gui/tools.cpp
===================================================================
--- tools/branches/gsoc2009-gui/gui/tools.cpp	2009-07-10 00:49:13 UTC (rev 42322)
+++ tools/branches/gsoc2009-gui/gui/tools.cpp	2009-07-10 00:49:27 UTC (rev 42323)
@@ -40,6 +40,11 @@
 #include "../compress_kyra.h"
 #include "../compress_queen.h"
 #include "../compress_saga.h"
+#include "../compress_scumm_bun.h"
+#include "../compress_scumm_san.h"
+#include "../compress_scumm_sou.h"
+#include "../compress_sword1.h"
+#include "../compress_touche.h"
 #include "../compress_tinsel.h"
 #include "../compress_touche.h"
 #include "../compress_tucker.h"
@@ -68,6 +73,10 @@
 	addTool(new ToolGUI(new CompressKyra()));
 	addTool(new ToolGUI(new CompressQueen()));
 	addTool(new ToolGUI(new CompressSaga()));
+	addTool(new ToolGUI(new CompressScummBun()));
+	addTool(new ToolGUI(new CompressScummSan()));
+	addTool(new ToolGUI(new CompressScummSou()));
+	addTool(new ToolGUI(new CompressSword1()));
 	addTool(new ToolGUI(new CompressTinsel()));
 	addTool(new ToolGUI(new CompressTouche(), wxT("/")));
 	addTool(new ToolGUI(new CompressTucker(), wxT("/")));

Modified: tools/branches/gsoc2009-gui/util.cpp
===================================================================
--- tools/branches/gsoc2009-gui/util.cpp	2009-07-10 00:49:13 UTC (rev 42322)
+++ tools/branches/gsoc2009-gui/util.cpp	2009-07-10 00:49:27 UTC (rev 42323)
@@ -313,16 +313,18 @@
 }
 
 void File::open(const Filename &filepath, const char *mode) {
-	if (strcmp(mode, "w") == 0)
-		open(filepath, FILEMODE_WRITE);
-	else if (strcmp(mode, "r") == 0)
-		open(filepath, FILEMODE_READ);
-	else if (strcmp(mode, "wb") == 0)
-		open(filepath, FileMode(FILEMODE_WRITE | FILEMODE_BINARY));
-	else if (strcmp(mode, "rb") == 0)
-		open(filepath, FileMode(FILEMODE_READ | FILEMODE_BINARY));
-	else
-		throw FileException(std::string("Unsupported FileMode ") + mode);
+	FileMode m = FILEMODE_WRITE;
+	do {
+		switch(*mode) {
+		case 'w': m = FILEMODE_WRITE; break;
+		case 'r': m = FILEMODE_READ; break;
+		case 'b': m = FileMode(m | FILEMODE_BINARY); break;
+		case '+': m = FileMode(m | FILEMODE_APPEND); break;
+		default: throw FileException(std::string("Unsupported FileMode ") + mode);
+		}
+	} while(*++mode);
+	
+	open(filepath, m);
 }
 
 void File::open(const Filename &filepath, FileMode mode) {
@@ -336,6 +338,8 @@
 		strmode += 'w';
 	if (mode & FILEMODE_BINARY)
 		strmode += 'b';
+	if (mode & FILEMODE_APPEND)
+		strmode += '+';
 
 	_file = fopen(filepath.getFullPath().c_str(), strmode.c_str());
 	_mode = mode;

Modified: tools/branches/gsoc2009-gui/util.h
===================================================================
--- tools/branches/gsoc2009-gui/util.h	2009-07-10 00:49:13 UTC (rev 42322)
+++ tools/branches/gsoc2009-gui/util.h	2009-07-10 00:49:27 UTC (rev 42323)
@@ -325,6 +325,7 @@
 	FILEMODE_READ = 1,
 	FILEMODE_WRITE = 2,
 	FILEMODE_BINARY = 4,
+	FILEMODE_APPEND = 8,
 };
 
 /**
@@ -411,6 +412,10 @@
 	 * @param elementCount the number of elements to read
 	 */
 	size_t read(void *data, size_t elementSize, size_t elementCount);
+	// Just an alias, for now... 
+	// (if we change the other version to throw it will be 
+	// easier if the concerned places already use the no-throw version)
+	size_t readN(void *data, size_t elementSize, size_t elementCount) {return read(data, elementSize, elementCount);}
 
 	
 	/**
@@ -461,7 +466,7 @@
 	void rewind();
 
 	/**
-	 * Returns the position in the stream.
+	 * Returns current position of the file cursor
 	 */
 	int pos();
 
@@ -475,6 +480,10 @@
 	 * @todo get rid of this ASAP
 	 */
 	operator FILE *() { return _file; }
+	/**
+	 * Explicit conversion to a FILE *, all code should use this in the future
+	 */
+	FILE *handle() {return _file;}
 
 	// FIXME: Remove this method ASAP
 	FILE *getFileHandle() { return _file; }


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