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

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Fri Sep 25 21:51:36 CEST 2009


Revision: 44368
          http://scummvm.svn.sourceforge.net/scummvm/?rev=44368&view=rev
Author:   fingolfin
Date:     2009-09-25 19:51:35 +0000 (Fri, 25 Sep 2009)

Log Message:
-----------
Fixed buggy argument handling in GSoC tools code

Modified Paths:
--------------
    tools/branches/gsoc2009-gui/compress.cpp
    tools/branches/gsoc2009-gui/compress_agos.cpp
    tools/branches/gsoc2009-gui/compress_gob.cpp
    tools/branches/gsoc2009-gui/compress_sword1.cpp
    tools/branches/gsoc2009-gui/example_tool.cpp
    tools/branches/gsoc2009-gui/extract_kyra.cpp
    tools/branches/gsoc2009-gui/extract_parallaction.cpp
    tools/branches/gsoc2009-gui/tool.cpp
    tools/branches/gsoc2009-gui/tool.h
    tools/branches/gsoc2009-gui/tools_cli.cpp

Modified: tools/branches/gsoc2009-gui/compress.cpp
===================================================================
--- tools/branches/gsoc2009-gui/compress.cpp	2009-09-25 19:38:24 UTC (rev 44367)
+++ tools/branches/gsoc2009-gui/compress.cpp	2009-09-25 19:51:35 UTC (rev 44368)
@@ -670,33 +670,34 @@
 }
 
 bool CompressionTool::processMp3Parms() {
-	while (_arguments_parsed < _arguments.size()) {
-		std::string arg = _arguments[_arguments_parsed];
+	while (!_arguments.empty()) {
+		std::string arg = _arguments.front();
+		_arguments.pop_front();
 
 		if (arg == "--vbr") {
 			encparms.abr = 0;
 		} else if (arg == "--abr") {
 			encparms.abr = 1;
 		} else if (arg == "-b") {
-			++_arguments_parsed;
-			if (_arguments_parsed >= _arguments.size())
+			if (_arguments.empty())
 				throw ToolException("Could not parse command line options, expected value after -b");
-			encparms.minBitr = atoi(_arguments[_arguments_parsed].c_str());
+			encparms.minBitr = atoi(_arguments.front().c_str());
 
 			if (encparms.minBitr > 160)
 				throw ToolException("Minimum bitrate out of bounds (-b), must be between 8 and 160.");
 
-			if (encparms.minBitr == 0 && _arguments[_arguments_parsed] != "0")
+			if (encparms.minBitr == 0 && _arguments.front() != "0")
 				throw ToolException("Minimum bitrate (-b) must be a number.");
 
 			if (encparms.minBitr < 8)
 				throw ToolException("Minimum bitrate out of bounds (-b), must be between 8 and 160.");
 
+			_arguments.pop_front();
+
 		} else if (arg == "-B") {
-			++_arguments_parsed;
-			if (_arguments_parsed >= _arguments.size())
+			if (_arguments.empty())
 				throw ToolException("Could not parse command line options, expected value after -B");
-			encparms.maxBitr = atoi(_arguments[_arguments_parsed].c_str());
+			encparms.maxBitr = atoi(_arguments.front().c_str());
 
 			if ((encparms.maxBitr % 8) != 0) {
 				encparms.maxBitr -= encparms.maxBitr % 8;
@@ -705,17 +706,18 @@
 			if (encparms.maxBitr > 160)
 				throw ToolException("Maximum bitrate out of bounds (-B), must be between 8 and 160.");
 
-			if (encparms.maxBitr == 0 && _arguments[_arguments_parsed] != "0")
+			if (encparms.maxBitr == 0 && _arguments.front() != "0")
 				throw ToolException("Maximum bitrate (-B) must be a number.");
 
 			if (encparms.maxBitr < 8)
 				throw ToolException("Maximum bitrate out of bounds (-B), must be between 8 and 160.");
 
+			_arguments.pop_front();
+
 		} else if (arg == "-V") {
-			++_arguments_parsed;
-			if (_arguments_parsed >= _arguments.size())
+			if (_arguments.empty())
 				throw ToolException("Could not parse command line options, expected value after -V");
-			encparms.vbrqual = atoi(_arguments[_arguments_parsed].c_str());
+			encparms.vbrqual = atoi(_arguments.front().c_str());
 
 			if (encparms.vbrqual < 0)
 				throw ToolException("Quality (-q) out of bounds, must be between 0 and 9.");
@@ -723,11 +725,12 @@
 			if (encparms.vbrqual > 9)
 				throw ToolException("Quality (-q) out of bounds, must be between 0 and 9.");
 
+			_arguments.pop_front();
+
 		} else if (arg == "-q") {
-			++_arguments_parsed;
-			if (_arguments_parsed >= _arguments.size())
+			if (_arguments.empty())
 				throw ToolException("Could not parse command line options, expected value after -q");
-			encparms.algqual = atoi(_arguments[_arguments_parsed].c_str());
+			encparms.algqual = atoi(_arguments.front().c_str());
 
 			if (encparms.algqual < 0)
 				throw ToolException("Quality (-q) out of bounds, must be between 0 and 9.");
@@ -735,27 +738,27 @@
 			if (encparms.algqual > 9)
 				throw ToolException("Quality (-q) out of bounds, must be between 0 and 9.");
 
+			_arguments.pop_front();
+
 		} else if (arg == "--silent") {
 			encparms.silent = 1;
 		} else {
 			break;
 		}
-
-		++_arguments_parsed;
 	}
 
 	return true;
 }
 
 bool CompressionTool::processOggParms() {
-	while (_arguments_parsed < _arguments.size()) {
-		std::string arg = _arguments[_arguments_parsed];
+	while (!_arguments.empty()) {
+		std::string arg = _arguments.front();
+		_arguments.pop_front();
 
 		if (arg == "-b") {
-			++_arguments_parsed;
-			if (_arguments_parsed >= _arguments.size())
+			if (_arguments.empty())
 				throw ToolException("Could not parse command line options, expected value after -b");
-			oggparms.nominalBitr = atoi(_arguments[_arguments_parsed].c_str());
+			oggparms.nominalBitr = atoi(_arguments.front().c_str());
 
 			if ((oggparms.nominalBitr % 8) != 0)
 				oggparms.nominalBitr -= oggparms.nominalBitr % 8;
@@ -763,17 +766,18 @@
 			if (oggparms.nominalBitr > 160)
 				throw ToolException("Nominal bitrate out of bounds (-b), must be between 8 and 160.");
 
-			if (oggparms.nominalBitr == 0 && _arguments[_arguments_parsed] != "0")
+			if (oggparms.nominalBitr == 0 && _arguments.front() != "0")
 				throw ToolException("Nominal bitrate (-b) must be a number.");
 
 			if (oggparms.nominalBitr < 8)
 				throw ToolException("Nominal bitrate out of bounds (-b), must be between 8 and 160.");
 
+			_arguments.pop_front();
+
 		} else if (arg == "-m") {
-			++_arguments_parsed;
-			if (_arguments_parsed >= _arguments.size())
+			if (_arguments.empty())
 				throw ToolException("Could not parse command line options, expected value after -m");
-			oggparms.minBitr = atoi(_arguments[_arguments_parsed].c_str());
+			oggparms.minBitr = atoi(_arguments.front().c_str());
 
 			if ((oggparms.minBitr % 8) != 0)
 				oggparms.minBitr -= oggparms.minBitr % 8;
@@ -781,17 +785,18 @@
 			if (oggparms.minBitr > 160)
 				throw ToolException("Minimal bitrate out of bounds (-m), must be between 8 and 160.");
 
-			if (oggparms.minBitr == 0 && _arguments[_arguments_parsed] != "0")
+			if (oggparms.minBitr == 0 && _arguments.front() != "0")
 				throw ToolException("Minimal bitrate (-m) must be a number.");
 
 			if (oggparms.minBitr < 8)
 				throw ToolException("Minimal bitrate out of bounds (-m), must be between 8 and 160.");
 
+			_arguments.pop_front();
+
 		} else if (arg == "-M") {
-			++_arguments_parsed;
-			if (_arguments_parsed >= _arguments.size())
+			if (_arguments.empty())
 				throw ToolException("Could not parse command line options, expected value after -M");
-			oggparms.maxBitr = atoi(_arguments[_arguments_parsed].c_str());
+			oggparms.maxBitr = atoi(_arguments.front().c_str());
 
 			if ((oggparms.maxBitr % 8) != 0)
 				oggparms.maxBitr -= oggparms.maxBitr % 8;
@@ -799,38 +804,42 @@
 			if (oggparms.maxBitr > 160)
 				throw ToolException("Minimal bitrate out of bounds (-M), must be between 8 and 160.");
 
-			if (oggparms.maxBitr == 0 && _arguments[_arguments_parsed] != "0")
+			if (oggparms.maxBitr == 0 && _arguments.front() != "0")
 				throw ToolException("Minimal bitrate (-M) must be a number.");
 
 			if (oggparms.maxBitr < 8)
 				throw ToolException("Minimal bitrate out of bounds (-M), must be between 8 and 160.");
+
+			_arguments.pop_front();
+
 		} else if (arg == "-q") {
-			++_arguments_parsed;
-			oggparms.quality = (float)atoi(_arguments[_arguments_parsed].c_str());
+			oggparms.quality = (float)atoi(_arguments.front().c_str());
 
-			if (oggparms.quality == 0 && _arguments[_arguments_parsed] != "0")
+			if (oggparms.quality == 0 && _arguments.front() != "0")
 				throw ToolException("Quality (-q) must be a number.");
+
+			_arguments.pop_front();
+
 		} else if (arg == "--silent") {
 			oggparms.silent = 1;
 		} else {
 			break;
 		}
-
-		++_arguments_parsed;
 	}
 
 	return true;
 }
 
 bool CompressionTool::processFlacParms(){
-	while (_arguments_parsed < _arguments.size()) {
-		std::string arg = _arguments[_arguments_parsed];
+	while (!_arguments.empty()) {
+		std::string arg = _arguments.front();
+		_arguments.pop_front();
 
 		if (arg == "-b") {
-			++_arguments_parsed;
-			if (_arguments_parsed >= _arguments.size())
+			if (_arguments.empty())
 				throw ToolException("Could not parse command line options, expected value after -b");
-			flacparms.blocksize = atoi(_arguments[_arguments_parsed].c_str());
+			flacparms.blocksize = atoi(_arguments.front().c_str());
+			_arguments.pop_front();
 		} else if (arg == "--fast") {
 			flacparms.compressionLevel = 0;
 		} else if (arg == "--best") {
@@ -860,8 +869,6 @@
 		} else {
 			break;
 		}
-
-		++_arguments_parsed;
 	}
 
 	return true;
@@ -878,17 +885,17 @@
 void CompressionTool::parseAudioArguments() {
 	_format = AUDIO_MP3;
 
-	if (_arguments[_arguments_parsed] ==  "--mp3")
+	if (_arguments.front() ==  "--mp3")
 		_format = AUDIO_MP3;
-	else if (_arguments[_arguments_parsed] == "--vorbis")
+	else if (_arguments.front() == "--vorbis")
 		_format = AUDIO_VORBIS;
-	else if (_arguments[_arguments_parsed] == "--flac")
+	else if (_arguments.front() == "--flac")
 		_format = AUDIO_FLAC;
 	else
 		// No audio arguments then
 		return;
 
-	++_arguments_parsed;
+	_arguments.pop_front();
 
 	// Need workaround to be sign-correct
 	switch (_format) {

Modified: tools/branches/gsoc2009-gui/compress_agos.cpp
===================================================================
--- tools/branches/gsoc2009-gui/compress_agos.cpp	2009-09-25 19:38:24 UTC (rev 44367)
+++ tools/branches/gsoc2009-gui/compress_agos.cpp	2009-09-25 19:51:35 UTC (rev 44368)
@@ -214,9 +214,9 @@
 }
 
 void CompressAgos::parseExtraArguments() {
-	if (_arguments[_arguments_parsed] == "--mac") {
+	if (!_arguments.empty() && _arguments.front() == "--mac") {
 		_convertMac = true;
-		++_arguments_parsed;
+		_arguments.pop_front();
 	}
 }
 

Modified: tools/branches/gsoc2009-gui/compress_gob.cpp
===================================================================
--- tools/branches/gsoc2009-gui/compress_gob.cpp	2009-09-25 19:38:24 UTC (rev 44367)
+++ tools/branches/gsoc2009-gui/compress_gob.cpp	2009-09-25 19:51:35 UTC (rev 44368)
@@ -58,9 +58,9 @@
 }
 
 void CompressGob::parseExtraArguments() {
-	if (_arguments[_arguments_parsed] == "-f") {
+	if (!_arguments.empty() && _arguments.front() == "-f") {
 		_execMode |= MODE_FORCE;
-		++_arguments_parsed;
+		_arguments.pop_front();
 	}
 }
 

Modified: tools/branches/gsoc2009-gui/compress_sword1.cpp
===================================================================
--- tools/branches/gsoc2009-gui/compress_sword1.cpp	2009-09-25 19:38:24 UTC (rev 44367)
+++ tools/branches/gsoc2009-gui/compress_sword1.cpp	2009-09-25 19:51:35 UTC (rev 44368)
@@ -581,13 +581,13 @@
 }
 
 void CompressSword1::parseExtraArguments() {
-	if (_arguments[_arguments_parsed] == "--speech-only") {
+	if (!_arguments.empty() && _arguments.front() == "--speech-only") {
 		_compMusic = false;
-		++_arguments_parsed;
+		_arguments.pop_front();
 	}
-	if (_arguments[_arguments_parsed] == "--music-only") {
+	if (!_arguments.empty() && _arguments.front() == "--music-only") {
 		_compSpeech = false;
-		++_arguments_parsed;
+		_arguments.pop_front();
 	}
 }
 

Modified: tools/branches/gsoc2009-gui/example_tool.cpp
===================================================================
--- tools/branches/gsoc2009-gui/example_tool.cpp	2009-09-25 19:38:24 UTC (rev 44367)
+++ tools/branches/gsoc2009-gui/example_tool.cpp	2009-09-25 19:51:35 UTC (rev 44368)
@@ -91,15 +91,14 @@
 void CompressionExample::parseExtraArguments() {
 	// Here, we parse our own arguments
 
-	// Arguments are stored in the _arguments member
-	// and the number of arguments already parsed is in _
-	if (_arguments.size() < _arguments_parsed) {
-		if (_arguments[_arguments_parsed] == "-a") {
+	// Arguments are stored in the _arguments member.
+	// Remove any arguments that you have "used up"
+	if (!_arguments.empty() && _arguments.front() == "-a") {
 			// Set our member
 			_outputFiles = true;
 
-			// We matched one argument, make sure to advance argument counter
-			++_arguments_parsed;
+			// We matched one argument, so remove it
+			_arguments.pop_front();
 		}
 	}
 }

Modified: tools/branches/gsoc2009-gui/extract_kyra.cpp
===================================================================
--- tools/branches/gsoc2009-gui/extract_kyra.cpp	2009-09-25 19:38:24 UTC (rev 44367)
+++ tools/branches/gsoc2009-gui/extract_kyra.cpp	2009-09-25 19:51:35 UTC (rev 44368)
@@ -52,8 +52,9 @@
 
 void ExtractKyra::parseExtraArguments() {
 	// Parse our own arguments
-	while(_arguments_parsed < _arguments.size()) {
-		std::string arg = _arguments[_arguments_parsed];
+	while (!_arguments.empty()) {
+		std::string arg = _arguments.front();
+		_arguments.pop_front();
 		if (arg == "-x") {
 			extractAll = true;
 			extractOne = false;
@@ -65,17 +66,13 @@
 			extractOne = true;
 			extractAll = false;
 
-			++_arguments_parsed;
-
-			if (_arguments_parsed >= _arguments.size()) {
+			if (_arguments.empty())
 				error("No filename supplied to -n\nShould be used on the form: %s -n ALGAE.CPS -o out/ A_E.PAK");
-			} else {
-				singleFilename = _arguments[_arguments_parsed];
-			}
+			singleFilename = _arguments.front();
+			_arguments.pop_front();
 		} else {
 			break;
 		}
-		++_arguments_parsed;
 	}
 }
 

Modified: tools/branches/gsoc2009-gui/extract_parallaction.cpp
===================================================================
--- tools/branches/gsoc2009-gui/extract_parallaction.cpp	2009-09-25 19:38:24 UTC (rev 44367)
+++ tools/branches/gsoc2009-gui/extract_parallaction.cpp	2009-09-25 19:51:35 UTC (rev 44368)
@@ -305,9 +305,9 @@
 }
 
 void ExtractParallaction::parseExtraArguments() {
-	if (_arguments[_arguments_parsed] == "--small") {
+	if (!_arguments.empty() && _arguments.front() == "--small") {
 		_small = true;
-		++_arguments_parsed;
+		_arguments.pop_front();
 	}
 }
 

Modified: tools/branches/gsoc2009-gui/tool.cpp
===================================================================
--- tools/branches/gsoc2009-gui/tool.cpp	2009-09-25 19:38:24 UTC (rev 44367)
+++ tools/branches/gsoc2009-gui/tool.cpp	2009-09-25 19:51:35 UTC (rev 44368)
@@ -32,8 +32,6 @@
 	_name = name;
 	_type = type;
 
-	_arguments_parsed = 0;
-
 	_outputToDirectory = true;
 	_supportedFormats = AUDIO_ALL;
 	_supportsProgressBar = false;
@@ -56,26 +54,14 @@
 	// ...
 }
 
-int Tool::run(int argc, char *argv[]) {
-	argc -= 1;
-	argv += 1; 
-
-	std::vector<std::string> args;
-	for (int i = 0; i < argc; ++i)
-		args.push_back(argv[i]);
-
-	return run(args);
-}
-
-int Tool::run(std::vector<std::string> args) {
+int Tool::run(const std::deque<std::string> &args) {
 	_arguments = args;
-	_arguments_parsed = 0;
 
 	// Pop the first argument (name of ourselves)
-	_arguments.erase(_arguments.begin());
+	_arguments.pop_front();
 
 	// Check for help
-	if (_arguments.empty() || _arguments[0] == "-h" || _arguments[0] == "--help") {
+	if (_arguments.empty() || _arguments.front() == "-h" || _arguments.front() == "--help") {
 		print(getHelp().c_str());
 		return 2;
 	}
@@ -87,20 +73,21 @@
 	// Read tool specific arguments
 	parseExtraArguments();
 
-	if (_arguments.size() && _arguments[_arguments_parsed][0] == '-') {
-		std::string s = "Possibly ignored option " + _arguments[_arguments_parsed] + ".";
+	if (!_arguments.empty() && _arguments.front()[0] == '-') {
+		std::string s = "Possibly ignored option " + _arguments.front() + ".";
 		print(s.c_str());
 	}
 
 	// Make sure we have enough input files.
-	if (_arguments.size() - _arguments_parsed < _inputPaths.size()) {
+	if (_arguments.size() < _inputPaths.size()) {
 		print("Too few input files!");
 		return -2;
 	}
 
 	// Read input files from CLI
 	for (ToolInputs::iterator iter = _inputPaths.begin(); iter != _inputPaths.end(); ++iter) {
-		std::string &in = _arguments[_arguments_parsed++];
+		std::string &in = _arguments.front();
+		_arguments.pop_front();
 		if (!iter->file) {
 			// Append '/' to input if it's not already done
 			// TODO: We need a way to detect a proper directory here!
@@ -115,11 +102,13 @@
 	}
 
 	// We should have parsed all arguments by now
-	if (_arguments_parsed < _arguments.size() - _inputPaths.size()) {
+	if (_inputPaths.size() < _arguments.size()) {
 		std::ostringstream os;
 		os << "Too many inputs files ( ";
-		while (_arguments_parsed < _arguments.size())
-			os << "'" << _arguments[_arguments_parsed++] << "' ";
+		while (!_arguments.empty()) {
+			os << "'" << _arguments.front() << "' ";
+			_arguments.pop_front();
+		}
 		os << ")\n";
 		print(os.str().c_str());
 		return -2;
@@ -272,17 +261,16 @@
 }
 
 void Tool::parseOutputArguments() {
-	if (_arguments_parsed >= _arguments.size())
+	if (_arguments.empty())
 		return;
-	if (_arguments[_arguments_parsed] == "-o" || _arguments[_arguments_parsed] == "--output") {
+	if (_arguments.front() == "-o" || _arguments.front() == "--output") {
 		// It's an -o argument
 
-		if (_arguments_parsed + 1 < _arguments.size()) {
-			_outputPath = _arguments[_arguments_parsed + 1];
-			_arguments_parsed += 2;
-		} else {
+		_arguments.pop_front();
+		if (_arguments.empty());
 			throw ToolException("Could not parse arguments: Expected path after '-o' or '--output'.");
-		}
+
+		_outputPath = _arguments.front();
 	}
 }
 

Modified: tools/branches/gsoc2009-gui/tool.h
===================================================================
--- tools/branches/gsoc2009-gui/tool.h	2009-09-25 19:38:24 UTC (rev 44367)
+++ tools/branches/gsoc2009-gui/tool.h	2009-09-25 19:51:35 UTC (rev 44368)
@@ -24,6 +24,7 @@
 #define TOOL_H
 
 #include <vector>
+#include <deque>
 #include <string>
 
 #include "util.h"
@@ -78,15 +79,9 @@
 	virtual ~Tool();
 
 	/**
-	 * Run tool with CLI args (parses them, and then calls run())
-	 * This version also catches all errors and prints them before exiting
-	 *
-	 * @param argc Argument count
-	 * @param argv Argument values
+	 * Run tool with command line arguments.
 	 */
-	int run(int argc, char *argv[]);
-	/** Same as the above, but accepts vector of string instead */
-	int run(std::vector<std::string> args);
+	int run(const std::deque<std::string> &args);
 
 	/**
 	 * Parse with args set already (modify the public members to set them)
@@ -211,9 +206,7 @@
 
 protected:
 	/* Command line arguments we are parsing. */
-	std::vector<std::string> _arguments;
-	/* How many of the arguments we have parsed so far */
-	size_t _arguments_parsed;
+	std::deque<std::string> _arguments;
 
 	/** If this tools outputs to a directory, not a file. */
 	bool _outputToDirectory;

Modified: tools/branches/gsoc2009-gui/tools_cli.cpp
===================================================================
--- tools/branches/gsoc2009-gui/tools_cli.cpp	2009-09-25 19:38:24 UTC (rev 44367)
+++ tools/branches/gsoc2009-gui/tools_cli.cpp	2009-09-25 19:51:35 UTC (rev 44368)
@@ -57,7 +57,7 @@
 			Tool *tool = *iter;
 			if (arguments.front() == tool->getName()) {
 				// Run the tool, first argument will be name, very nice!
-				return tool->run(std::vector<std::string>(arguments.begin(), arguments.end()));
+				return tool->run(arguments);
 			}
 		}
 		std::cout << "\tUnknown tool, make sure you input one of the following:\n";
@@ -69,7 +69,7 @@
 			for (ToolList::iterator iter = _tools.begin(); iter != _tools.end(); ++iter) {
 				Tool *tool = *iter;
 				if (arguments.front() == tool->getName()) {
-					// Run the tool, first argument will be name, very nice!
+					// Obtain the help text for this tool and print it
 					std::cout << tool->getHelp() << std::endl;
 					return 2;
 				}
@@ -179,7 +179,7 @@
 		
 		// Run the tool, with the remaining arguments
 		arguments.push_front(tool->getName());
-		return tool->run(std::vector<std::string>(arguments.begin(), arguments.end()));
+		return tool->run(arguments);
 	}
 
 	return 0;


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