948c632088e8 — Duncan Ross Palmer 1 year, 2 months ago
Start work on options parser
2 files changed, 125 insertions(+), 61 deletions(-)

M bin/raw2c
M t/raw2c.pl
M bin/raw2c +111 -61
@@ 31,6 31,117 @@ 
 # POSSIBILITY OF SUCH DAMAGE.
 #
 
+package Options;
+use strict;
+use warnings;
+
+sub new {
+	my ($class) = @_;
+
+	return bless({
+		quiet => 0,
+		nullTerminator => 0,
+		symbol => '',
+		header => '',
+		inputFileName => '',
+		outputFileName => '',
+	}, $class);
+}
+
+sub quiet {
+	my ($self, @args) = @_;
+	($self->{quiet}) = @args if (scalar(@args));
+	return $self->{quiet};
+}
+
+sub inputFileName {
+	my ($self, @args) = @_;
+	($self->{inputFileName}) = @args if (scalar(@args));
+	return $self->{inputFileName};
+}
+
+sub outputFileName {
+	my ($self, @args) = @_;
+	($self->{outputFileName}) = @args if (scalar(@args));
+	return $self->{outputFileName};
+}
+
+sub symbolName {
+	my ($self, @args) = @_;
+	($self->{symbolName}) = @args if (scalar(@args));
+	return $self->{symbolName};
+}
+
+package OptionsParser;
+use strict;
+use warnings;
+
+sub new {
+	my ($class) = @_;
+	return bless({}, $class);
+}
+
+sub randChar {
+	my $validChars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_';
+	my $randOffset = int(rand(length($validChars)));
+	return substr($validChars, $randOffset, 1);
+}
+
+sub randomSymbol {
+	my $buffer = '';
+	$buffer .= randChar() foreach (0..19);
+	return '__rawimage_' . $buffer;
+}
+
+sub parse {
+	my ($self, $argumentVector) = @_;
+
+	my $options = Options->new();
+
+	for (my $i = 0; $i < scalar(@$argumentVector); $i++) {
+		my $arg = $argumentVector->[$i];
+		if ($arg eq '-q') {
+			$options->quiet(1);
+#		} else if (compare(argumentVector[i], "-n")) {
+#			options.null_terminator = 1;
+#		} else if (compare(argumentVector[i], "-r")) {
+#			options.recurse = 1;
+#		} else if (compareN(argumentVector[i], "--symbol", 8)) {
+#			char *pointer = argumentVector[i];
+#			pointer += 8; /* Move along to symbol name */
+#			if ('\0' != pointer[0]) pointer++;
+#			options.symbolName = pointer;
+#		} else if (compareN(argumentVector[i], "--header", 8)) {
+#			char *pointer = argumentVector[i];
+#			pointer += 8; /* Move along to symbol name */
+#			if ('\0' != pointer[0]) pointer++;
+#			options.headerName = pointer;
+		} else { # Must be a filename
+			if (!$options->inputFileName) {
+				$options->inputFileName($arg);
+			} elsif (!$options->outputFileName) {
+				$options->outputFileName($arg);
+			} else {
+				die('Too many filenames specified');
+			}
+		}
+	}
+
+	if (!$options->inputFileName || !$options->outputFileName) {
+		die('Must specify input & output filenames');
+	}
+
+	if (!$options->symbolName) {
+		if (!$options->quiet) {
+			printf("No symbol name was specified, a random symbol will be generated.\n");
+		}
+
+		$options->symbolName($self->randomSymbol());
+	}
+
+	return $options;
+}
+
 package Main;
 use strict;
 use warnings;

          
@@ 64,18 175,6 @@ sub title {
 	return;
 }
 
-sub randChar {
-	my $validChars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_';
-	my $randOffset = int(rand(length($validChars)));
-	return substr($validChars, $randOffset, 1);
-}
-
-sub randomSymbol {
-	my $buffer = '';
-	$buffer .= randChar() foreach (0..19);
-	return '__rawimage_' . $buffer;
-}
-
 sub main {
 	srand($PID + (time() % 100_000));
 	return EXIT_SUCCESS;

          
@@ 121,7 220,6 @@ exit(Main->new->main()) unless (caller()
 #	struct FileOrDir *pOutput,
 #	struct FileOrDir *pHeader
 #);
-#static char *randomSymbol(void);
 #static char randChar(void);
 #static void makeSource(struct FileOrDir *pIn, struct FileOrDir *pOut);
 #static void makeHeader(struct FileOrDir *pHeader);

          
@@ 167,54 265,6 @@ exit(Main->new->main()) unless (caller()
 #	return;
 #}
 
-#static int parse(int argumentCount, char *argumentVector[]) {
-
-#	int i;
-
-#	for (i = 1; i < argumentCount; i++) {
-#		if (compare(argumentVector[i], "-q")) {
-#			options.quiet = 1;
-#		} else if (compare(argumentVector[i], "-n")) {
-#			options.null_terminator = 1;
-#		} else if (compare(argumentVector[i], "-r")) {
-#			options.recurse = 1;
-#		} else if (compareN(argumentVector[i], "--symbol", 8)) {
-#			char *pointer = argumentVector[i];
-#			pointer += 8; /* Move along to symbol name */
-#			if ('\0' != pointer[0]) pointer++;
-#			options.symbolName = pointer;
-#		} else if (compareN(argumentVector[i], "--header", 8)) {
-#			char *pointer = argumentVector[i];
-#			pointer += 8; /* Move along to symbol name */
-#			if ('\0' != pointer[0]) pointer++;
-#			options.headerName = pointer;
-#		} else { /* Must be a filename */
-#			if (!options.inputFileName) {
-#				options.inputFileName = argumentVector[i];
-#			} else if (!options.outputFileName) {
-#				options.outputFileName = argumentVector[i];
-#			} else {
-#				printf("Too many filenames specified\n");
-#				return EXIT_FAILURE;
-#			}
-#		}
-#	}
-
-#	if (!options.inputFileName || !options.outputFileName) {
-#		printf("Must specify input & output filenames\n");
-#		return EXIT_FAILURE;
-#	}
-#
-#	if (!options.symbolName) {
-#		if (!options.quiet)
-#			printf("No symbol name was specified, a random symbol will be generated.\n");
-
-#		options.symbolName = randomSymbol();
-#	}
-
-#	return EXIT_SUCCESS;
-#}
-
 #static int openFiles(
 #	struct FileOrDir *pInput,
 #	struct FileOrDir *pOutput,

          
M t/raw2c.pl +14 -0
@@ 48,6 48,8 @@ sub testRandom {
 	my ($self) = @_;
 	plan tests => 2;
 
+	$self->sut(OptionsParser->new());
+
 	my %chars = ( );
 	do {
 		my $char = $self->sut->randChar();

          
@@ 64,6 66,18 @@ sub testRandom {
 	return EXIT_SUCCESS;
 }
 
+sub testParse {
+	my ($self) = @_;
+	plan tests => 1;
+
+	$self->sut(OptionsParser->new());
+
+	my $options = $self->sut->parse(['-q', 'input.bin', 'output.c']);
+	ok($options->quiet, 'quiet flag is set with -q');
+
+	return EXIT_SUCCESS;
+}
+
 package main;
 use strict;
 use warnings;