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

Remere at users.sourceforge.net Remere at users.sourceforge.net
Sat Jul 4 22:03:12 CEST 2009


Revision: 42108
          http://scummvm.svn.sourceforge.net/scummvm/?rev=42108&view=rev
Author:   Remere
Date:     2009-07-04 20:03:12 +0000 (Sat, 04 Jul 2009)

Log Message:
-----------
*Converted compress_agos to more C++ oriented format.
*Minor fixes to some other files, especially a nasty bug with Filename::getFullName, and linker error with both Gob tools having a 'Chunk' class.

Modified Paths:
--------------
    tools/branches/gsoc2009-gui/compress_agos.cpp
    tools/branches/gsoc2009-gui/extract_gob_stk.cpp
    tools/branches/gsoc2009-gui/extract_gob_stk.h
    tools/branches/gsoc2009-gui/tool.cpp
    tools/branches/gsoc2009-gui/util.cpp
    tools/branches/gsoc2009-gui/util.h

Added Paths:
-----------
    tools/branches/gsoc2009-gui/compress_agos.h

Modified: tools/branches/gsoc2009-gui/compress_agos.cpp
===================================================================
--- tools/branches/gsoc2009-gui/compress_agos.cpp	2009-07-04 19:04:39 UTC (rev 42107)
+++ tools/branches/gsoc2009-gui/compress_agos.cpp	2009-07-04 20:03:12 UTC (rev 42108)
@@ -20,39 +20,37 @@
  *
  */
 
-#include "compress.h"
+#include "compress_agos.h"
 
 #define TEMP_DAT	"tempfile.dat"
 #define TEMP_IDX	"tempfile.idx"
 
-static FILE *input, *output_idx, *output_snd;
+CompressAgos::CompressAgos(const std::string &name) : CompressionTool(name) {
+	_compMode = AUDIO_MP3;
+	_convertMac = false;
 
-static AudioFormat gCompMode = AUDIO_MP3;
+	_helptext = "\nUsage: %s [mode] [mode params] [--mac] <infile>\n" kCompressionAudioHelp
+}
 
-static void end(Filename *outPath) {
+void CompressAgos::end() {
 	int size;
 	char fbuf[2048];
 
-	fclose(output_snd);
-	fclose(output_idx);
-	fclose(input);
+	_output_idx.open(_outputPath, "wb");
 
-	output_idx = fopen(outPath->getFullPath().c_str(), "wb");
-
-	input = fopen(TEMP_IDX, "rb");
-	while ((size = fread(fbuf, 1, 2048, input)) > 0) {
-		fwrite(fbuf, 1, size, output_idx);
+	_input.open(TEMP_IDX, "rb");
+	while ((size = fread(fbuf, 1, 2048, _input)) > 0) {
+		fwrite(fbuf, 1, size, _output_idx);
 	}
 
-	fclose(input);
-
-	input = fopen(TEMP_DAT, "rb");
-	while ((size = fread(fbuf, 1, 2048, input)) > 0) {
-		fwrite(fbuf, 1, size, output_idx);
+	_input.open(TEMP_DAT, "rb");
+	while ((size = fread(fbuf, 1, 2048, _input)) > 0) {
+		fwrite(fbuf, 1, size, _output_idx);
 	}
 
-	fclose(input);
-	fclose(output_idx);
+	_input.close();
+	_output_idx.close();
+	_output_snd.close();
 
 	/* And some clean-up :-) */
 	unlink(TEMP_IDX);
@@ -60,94 +58,81 @@
 	unlink(TEMP_RAW);
 	unlink(tempEncoded);
 	unlink(TEMP_WAV);
-
-	exit(0);
 }
 
 
-static int get_offsets(uint32 filenums[], uint32 offsets[]) {
+int CompressAgos::get_offsets(uint32 filenums[], uint32 offsets[]) {
 	int i;
 	char buf[8];
 
 	for (i = 0;; i++) {
-		fread(buf, 1, 8, input);
+		fread(buf, 1, 8, _input);
 		if (!memcmp(buf, "Creative", 8) || !memcmp(buf, "RIFF", 4)) {
 			return i;
 		}
-		fseek(input, -8, SEEK_CUR);
+		fseek(_input, -8, SEEK_CUR);
 
-		offsets[i] = readUint32LE(input);
+		offsets[i] = _input.readU32LE();
 	}
 }
 
-static int get_offsets_mac(uint32 filenums[], uint32 offsets[]) {
+int CompressAgos::get_offsets_mac(uint32 filenums[], uint32 offsets[]) {
 	int i, size;
-	size = fileSize(input);
+	size = _input.size();
 
 	for (i = 1; i <= size / 6; i++) {
-		filenums[i] = readUint16BE(input);
-		offsets[i] = readUint32BE(input);
+		filenums[i] = _input.readU16BE();
+		offsets[i] = _input.readU32BE();
 	}
 
 	return(size/6);
 }
 
 
-static uint32 get_sound(uint32 offset) {
-	FILE *f;
+uint32 CompressAgos::get_sound(uint32 offset) {
 	uint32 tot_size;
 	char outname[256];
 	int size;
 	char fbuf[2048];
 	char buf[8];
 
-	fseek(input, offset, SEEK_SET);
+	fseek(_input, offset, SEEK_SET);
 
-	fread(buf, 1, 8, input);
+	fread(buf, 1, 8, _input);
 	if (!memcmp(buf, "Creative", 8)) {
-		printf("VOC found (pos = %d) :\n", offset);
-		fseek(input, 18, SEEK_CUR);
-		extractAndEncodeVOC(TEMP_RAW, input, gCompMode);
+		print("VOC found (pos = %d) :\n", offset);
+		fseek(_input, 18, SEEK_CUR);
+		extractAndEncodeVOC(TEMP_RAW, _input, _compMode);
 	} else if (!memcmp(buf, "RIFF", 4)) {
-		printf("WAV found (pos = %d) :\n", offset);
-		extractAndEncodeWAV(TEMP_WAV, input, gCompMode);
+		print("WAV found (pos = %d) :\n", offset);
+		extractAndEncodeWAV(TEMP_WAV, _input, _compMode);
 	} else {
 		error("Unexpected data at offset: %d", offset);
 	}
 
 	/* 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) {
 		tot_size += size;
-		fwrite(fbuf, 1, size, output_snd);
+		fwrite(fbuf, 1, size, _output_snd);
 	}
-	fclose(f);
 
 	return(tot_size);
 }
 
 
-static void convert_pc(Filename* inputPath) {
+void CompressAgos::convert_pc(Filename* inputPath) {
 	int i, size, num;
 	uint32 filenums[32768];
 	uint32 offsets[32768];
 
-	input = fopen(inputPath->getFullPath().c_str(), "rb");
-	if (!input) {
-		error("Cannot open file: %s", inputPath->getFullPath().c_str());
-	}
+	_input.open(*inputPath, "rb");
 
-	output_idx = fopen(TEMP_IDX, "wb");
-	if (!output_idx) {
-		error("Cannot open file " TEMP_IDX " for write");
-	}
+	_output_idx.open(TEMP_IDX, "wb");
 
-	output_snd = fopen(TEMP_DAT, "wb");
-	if (!output_snd) {
-		error("Cannot open file " TEMP_DAT " for write");
-	}
+	_output_snd.open(TEMP_DAT, "wb");
 
 	num = get_offsets(filenums, offsets);
 	if (!num) {
@@ -155,42 +140,33 @@
 	}
 	size = num * 4;
 
-	writeUint32LE(output_idx, 0);
-	writeUint32LE(output_idx, size);
+	_output_idx.writeU32LE(0);
+	_output_idx.writeU32LE(size);
 
 	for (i = 1; i < num; i++) {
 		if (offsets[i] == offsets[i + 1]) {
-			writeUint32LE(output_idx, size);
+			_output_idx.writeU32LE(size);
 			continue;
 		}
 
 		if (offsets[i] != 0)
 			size += get_sound(offsets[i]);
 		if (i < num - 1)
-			writeUint32LE(output_idx, size);
+			_output_idx.writeU32LE(size);
 	}
 }
 
-static void convert_mac(Filename *inputPath) {
+void CompressAgos::convert_mac(Filename *inputPath) {
 	int i, size, num;
 	uint32 filenums[32768];
 	uint32 offsets[32768];
 
 	inputPath->setFullName("voices.idx");
-	input = fopen(inputPath->getFullPath().c_str(), "rb");
-	if (!input) {
-		error("Cannot open file: %s", "voices.idx");
-	}
+	_input.open(*inputPath, "rb");
 
-	output_idx = fopen(TEMP_IDX, "wb");
-	if (!output_idx) {
-		error("Cannot open file " TEMP_IDX " for write");
-	}
+	_output_idx.open(TEMP_IDX, "wb");
 
-	output_snd = fopen(TEMP_DAT, "wb");
-	if (!output_snd) {
-		error("Cannot open file " TEMP_DAT " for write");
-	}
+	_output_snd.open(TEMP_DAT, "wb");
 
 	num = get_offsets_mac(filenums, offsets);
 	if (!num) {
@@ -198,12 +174,12 @@
 	}
 	size = num * 4;
 
-	writeUint32LE(output_idx, 0);
-	writeUint32LE(output_idx, size);
+	_output_idx.writeU32LE(0);
+	_output_idx.writeU32LE(size);
 
 	for (i = 1; i < num; i++) {
 		if (filenums[i] == filenums[i + 1] && offsets[i] == offsets[i + 1]) {
-			writeUint32LE(output_idx, size);
+			_output_idx.writeU32LE(size);
 			continue;
 		}
 
@@ -212,83 +188,49 @@
 			sprintf(filename, "voices%d.dat", filenums[i]);
 			inputPath->setFullName(filename);
 
-			if (input) {
-				fclose(input);
-			}
-
-			input = fopen(inputPath->getFullPath().c_str(), "rb");
-			if (!input) {
-				error("Cannot open file: %s", inputPath->getFullPath().c_str());
-			}
+			_input.open(*inputPath, "rb");
 		}
 
 		size += get_sound(offsets[i]);
 
 		if (i < num - 1) {
-			writeUint32LE(output_idx, size);
+			_output_idx.writeU32LE(size);
 		}
 	}
 }
 
-#define INT 2
-#define INT_STR(x) #x
-
-int export_main(compress_agos)(int argc, char *argv[]) {
-	const char *helptext = "\nUsage: %s [mode] [mode params] [--mac] <infile>\n" kCompressionAudioHelp;
-
-	bool convertMac = false;
-
-	Filename inpath, outpath;
-	int first_arg = 1;
-	int last_arg = argc - 1;
-
-	parseHelpArguments(argv, argc, helptext);
-
-	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]);
+void CompressAgos::parseExtraArguments() {
+	if (_arguments[_arguments_parsed] == "--mac") {
+		_convertMac = true;
 	}
+}
 
-	if (strcmp(argv[first_arg], "--mac") == 0) {
-		++first_arg;
-		convertMac = true;
-	}
-	
-	// 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
-		;
+void CompressAgos::execute() {
 
-	inpath.setFullPath(argv[first_arg]);
+	// We only got one input file
+	if (_inputPaths.size() > 1)
+		error("Only one input file expected!");
+	Filename inpath(_inputPaths[0]);
 
-	if (outpath.empty()) {
-		outpath = inpath;
-		outpath.setExtension(audio_extensions(gCompMode));
+	if (_outputPath.empty()) {
+		_outputPath = inpath;
+		_outputPath.setExtension(audio_extensions(_compMode));
 	}
 
-	if (convertMac) {
+	if (_convertMac) {
 		convert_mac(&inpath);
 		inpath.setFullName("simon2");
 	} else {
 		convert_pc(&inpath);
 	}
 
-	end(&outpath);
-
-	return(0);
+	end();
 }
 
-#if defined(UNIX) && defined(EXPORT_MAIN)
-int main(int argc, char *argv[]) __attribute__((weak));
+#ifdef STANDALONE_MAIN
 int main(int argc, char *argv[]) {
-	return export_main(compress_agos)(argc, argv);
+	CompressAgos agos(argv[0]);
+	return agos.run(argc, argv);
 }
 #endif
 

Added: tools/branches/gsoc2009-gui/compress_agos.h
===================================================================
--- tools/branches/gsoc2009-gui/compress_agos.h	                        (rev 0)
+++ tools/branches/gsoc2009-gui/compress_agos.h	2009-07-04 20:03:12 UTC (rev 42108)
@@ -0,0 +1,49 @@
+/* compress_agos.h - Compress Simon the Sorcerer 1/2 digital sound files into compressed audio format
+ * Copyright (C) 2009 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $URL
+ * $Id
+ *
+ */
+
+#ifndef COMPRESS_AGOS_H
+#define COMPRESS_AGOS_H
+
+#include "compress.h"
+
+class CompressAgos : public CompressionTool {
+public:
+	CompressAgos(const std::string &name = "compress_agos");
+
+	virtual void execute();
+
+protected:
+	void parseExtraArguments();
+
+	File _input, _output_idx, _output_snd;
+	AudioFormat _compMode;
+	bool _convertMac;
+
+	void end();
+	int get_offsets(uint32 filenums[], uint32 offsets[]);
+	int get_offsets_mac(uint32 filenums[], uint32 offsets[]);
+	uint32 get_sound(uint32 offset);
+	void convert_pc(Filename* inputPath);
+	void convert_mac(Filename *inputPath);
+};
+
+#endif


Property changes on: tools/branches/gsoc2009-gui/compress_agos.h
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:keywords
   + Date Rev Author URL Id
Added: svn:eol-style
   + native

Modified: tools/branches/gsoc2009-gui/extract_gob_stk.cpp
===================================================================
--- tools/branches/gsoc2009-gui/extract_gob_stk.cpp	2009-07-04 19:04:39 UTC (rev 42107)
+++ tools/branches/gsoc2009-gui/extract_gob_stk.cpp	2009-07-04 20:03:12 UTC (rev 42108)
@@ -26,7 +26,7 @@
 #define confSTK21 "STK21"
 #define confSTK10 "STK10"
 
-struct Chunk {
+struct ExtractGobStk::Chunk {
 	char name[64];
 	uint32 size, offset;
 	bool packed;
@@ -35,7 +35,7 @@
 	Chunk *next;
 
 	Chunk() : next(0) { }
-	~Chunk() { delete next; }
+	~Chunk() : { delete next; }
 };
 
 ExtractGobStk::ExtractGobStk(const std::string &name) : Tool(name) {
@@ -289,6 +289,7 @@
 				delete[] unpackedData;
 				throw;
 			}
+			delete[] data;
 		}
 
 		fflush(chunkFile);

Modified: tools/branches/gsoc2009-gui/extract_gob_stk.h
===================================================================
--- tools/branches/gsoc2009-gui/extract_gob_stk.h	2009-07-04 19:04:39 UTC (rev 42107)
+++ tools/branches/gsoc2009-gui/extract_gob_stk.h	2009-07-04 20:03:12 UTC (rev 42108)
@@ -24,9 +24,6 @@
 #define EXTRACT_GOB_STK_H
 
 #include "tool.h"
-
-struct Chunk;
-
 class ExtractGobStk : public Tool {
 public:
 	ExtractGobStk(const std::string &name = "extract_gob_stk");
@@ -35,6 +32,8 @@
 	virtual void execute();
 
 protected:
+	struct Chunk;
+
 	Chunk *_chunks;
 
 	void readChunkList(File &stk, File &gobConf);

Modified: tools/branches/gsoc2009-gui/tool.cpp
===================================================================
--- tools/branches/gsoc2009-gui/tool.cpp	2009-07-04 19:04:39 UTC (rev 42107)
+++ tools/branches/gsoc2009-gui/tool.cpp	2009-07-04 20:03:12 UTC (rev 42108)
@@ -56,24 +56,24 @@
 	_argv = 0;
 
 	// Check for help
-	if(_arguments.empty() || _arguments[0] == "-h" || _arguments[0] == "--help") {
+	if (_arguments.empty() || _arguments[0] == "-h" || _arguments[0] == "--help") {
 		print(_helptext.c_str());
 		return 2;
 	}
 
 	// Read standard arguments
-	if(_supported_formats != AUDIO_NONE)
+	if (_supported_formats != AUDIO_NONE)
 		parseAudioArguments();
 	parseOutputArguments();
 	// Read tool specific arguments
 	parseExtraArguments();
 
 	// Read input files from CLI
-	while(_arguments_parsed < _arguments.size()) {
+	while (_arguments_parsed < _arguments.size()) {
 		_inputPaths.push_back(_arguments[_arguments_parsed++]);
 	}
 
-	if(_inputPaths.empty()) {
+	if (_inputPaths.empty()) {
 		// Display help text if we got no input
 		print(_helptext.c_str());
 		return 2;
@@ -126,9 +126,9 @@
 }
 
 void Tool::parseOutputArguments() {
-	if(_arguments_parsed >= _arguments.size())
+	if (_arguments_parsed >= _arguments.size())
 		return;
-	if(_arguments[_arguments_parsed] == "-o" || _arguments[_arguments_parsed] == "--output") {
+	if (_arguments[_arguments_parsed] == "-o" || _arguments[_arguments_parsed] == "--output") {
 		// It's an -o argument
 
 		if (_arguments_parsed + 1 < _arguments.size()) {

Modified: tools/branches/gsoc2009-gui/util.cpp
===================================================================
--- tools/branches/gsoc2009-gui/util.cpp	2009-07-04 19:04:39 UTC (rev 42107)
+++ tools/branches/gsoc2009-gui/util.cpp	2009-07-04 20:03:12 UTC (rev 42108)
@@ -242,7 +242,7 @@
 	if (slash == std::string::npos)
 		return _path;
 
-	return _path.substr(slash);
+	return _path.substr(slash + 1);
 }
 
 std::string Filename::getPath() const {
@@ -280,42 +280,32 @@
 }
 
 File::~File() {
-	if (_file)
-		fclose(_file);
+	close();
 }
 
-void File::open(const std::string &filepath, FileMode mode) {
-	open(Filename(filepath), mode);
-}
-
-void File::open(const std::string &filepath, const char *mode) {
-	open(Filename(filepath), mode);
-}
-
 void File::open(const Filename &filepath, const char *mode) {
-	if(strcmp(mode, "w") == 0)
+	if (strcmp(mode, "w") == 0)
 		open(filepath, FILEMODE_WRITE);
-	else if(strcmp(mode, "r") == 0)
+	else if (strcmp(mode, "r") == 0)
 		open(filepath, FILEMODE_READ);
-	else if(strcmp(mode, "wb") == 0)
+	else if (strcmp(mode, "wb") == 0)
 		open(filepath, FileMode(FILEMODE_WRITE | FILEMODE_BINARY));
-	else if(strcmp(mode, "rb") == 0)
+	else if (strcmp(mode, "rb") == 0)
 		open(filepath, FileMode(FILEMODE_READ | FILEMODE_BINARY));
 	else
 		throw FileException(std::string("Unsupported FileMode ") + mode);
 }
 
 void File::open(const Filename &filepath, FileMode mode) {
-	// Clean up previous opened file
-	if (_file)
-		fclose(_file);
+	// Clean up previously opened file
+	close();
 
 	std::string strmode;
-	if(mode & FILEMODE_READ)
+	if (mode & FILEMODE_READ)
 		strmode += 'r';
-	if(mode & FILEMODE_WRITE)
+	if (mode & FILEMODE_WRITE)
 		strmode += 'w';
-	if(mode & FILEMODE_BINARY)
+	if (mode & FILEMODE_BINARY)
 		strmode += 'b';
 
 	_file = fopen(filepath.getFullPath().c_str(), strmode.c_str());
@@ -326,6 +316,12 @@
 		throw FileException("Could not open file " + filepath.getFullPath());
 }
 
+void File::close() {
+	if (_file)
+		fclose(_file);
+	_file = NULL;
+}
+
 uint8 File::readByte() {
 	if (!_file) 
 		throw FileException("File is not open");
@@ -430,7 +426,7 @@
 	if (!_file) 
 		throw FileException("File is not open");
 
-	if(fseek(_file, offset, origin) != 0)
+	if (fseek(_file, offset, origin) != 0)
 		throw FileException("Could not seek in file (" + _name.getFullPath() + ")");
 }
 

Modified: tools/branches/gsoc2009-gui/util.h
===================================================================
--- tools/branches/gsoc2009-gui/util.h	2009-07-04 19:04:39 UTC (rev 42107)
+++ tools/branches/gsoc2009-gui/util.h	2009-07-04 20:03:12 UTC (rev 42108)
@@ -344,10 +344,13 @@
 	 * @param filename The file to open
 	 * @param mode The mode to open the file in
 	 */
-	void open(const std::string &filename, const char *mode);
 	void open(const Filename &filename, const char *mode);
-	void open(const std::string &filename, FileMode mode);
 	void open(const Filename &filename, FileMode mode);
+
+	/**
+	 * Closes the file, if it's open
+	 */
+	void close();
 	
 	
 	/**


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