#!/Perl
print "Hello, World...\n";
# test12.pl - just a test file ...
# index
my $str = 'abcdabcd';
my $ind1 = index( $str, 'a' );
my $ind2 = index( $str, 'b' );
my $ind3 = index( $str, 'x' );
my $ind4 = index( $str, 'b', 4);
print "Found 'a' at $ind1, 'b' at $ind2, and not 'x' $ind3 ... and second 'b' is $ind4 ...\n";
$str = 'abcdabcd.';
$ind1 = rindex( $str, 'a' );
$ind2 = rindex( $str, 'b' );
$ind3 = rindex( $str, 'x' );
$ind4 = rindex( $str, 'b', 4);
print "Found 'a' at $ind1, 'b' at $ind2, and not 'x' $ind3 ... and second 'b' is $ind4 ...\n";
my $ind5 = rindex( $str, '.' );
my $ext = substr( $str, $ind5 + 1 );
print "Found '.' at $ind5, thus the extension is [$ext] ...\n";
$str = 'abcdabcd.cat';
$ind5 = rindex( $str, '.' );
$ext = substr( $str, $ind5 + 1 );
print "Found '.' at $ind5, thus the extension is [$ext] ...\n";

my $file = 'openal\test.cxx';
print "File $file has ext = [".file_extension($file)."], and a title = [".file_title($file)."] ...\n";
$file = 'openal/test.cxx';
print "File $file has ext = [".file_extension($file)."], and a title = [".file_title($file)."] ...\n";
$file = 'openal\../test';
print "File $file has ext = [".file_extension($file)."], and a title = [".file_title($file)."] ...\n";
$file = 'test.h';
print "File $file has ext = [".file_extension($file)."], and a title = [".file_title($file)."] ...\n";
$file = 'openal/test';
print "File $file has ext = [".file_extension($file)."], and a title = [".file_title($file)."] ...\n";
$file = 'test';
print "File $file has ext = [".file_extension($file)."], and a title = [".file_title($file)."] ...\n";
$file = 'openal\../test.c';
print "File $file has ext = [".file_extension($file)."], and a title = [".file_title($file)."] ...\n";
$file = 'openal\../';
print "File $file has ext = [".file_extension($file)."], and a title = [".file_title($file)."] ...\n";

sub pos_of_last_slash {
	my $fil = shift;
	my $in1 = rindex( $fil, '/' );
	my $in2 = rindex( $fil, '\\' );
	my $pos = -1;
	# if BOTH exist
	if (($in1 >= 0) && ($in2 >= 0)) {
		# get the LAST
		if ($in1 > $in2) {
			$pos = $in1;
		} else {
			$pos = $in2;
		}
	} elsif ($in1 >= 0 ) {
		$pos = $in1;
	} elsif ($in2 >= 0 ) {
		$pos = $in2;
	}
	return $pos;
}

sub file_extension {
	my $fil = shift;
	my $pos = pos_of_last_slash($fil);
	my $last = rindex( $fil, '.' );
	my $ext = '';
	if ( $last >= 0 ) {
		if ($pos >= 0) {
			if ($last > $pos) {
				$ext = substr($fil, $last + 1);
			}
		} else {
			$ext = substr($fil, $last + 1);
		}
	}
	return $ext;
}

sub file_title {
	my $fil = shift;
	my $pos = pos_of_last_slash($fil);
	my $last = rindex( $fil, '.' );
	my $tit = '';
	if ($last >= 0) {
		if ($pos >= 0) {
			if ($last > $pos) {
				print "Using 1 substr( $fil, $pos+1, $last - $pos - 1 ) ...\n"; 
				$tit = substr( $fil, $pos+1, $last - $pos - 1 ); 
			} else {
				print "Using 2 substr( $fil, $pos+1 ) ...\n"; 
				$tit = substr( $fil, $pos+1 ); 
			}
		} else {
			print "Using 3 substr( $fil, 0, $last ) ...\n"; 
			$tit = substr( $fil, 0, $last ); 
		}
	} elsif ($pos >= 0) {
		print "Using 4 substr( $fil, $pos+1 ) ...\n"; 
		$tit = substr( $fil, $pos+1 ); 
	} else {
		print "Using 5 no slash, no dot ...\n"; 
		$tit = $fil;
	}
	return $tit;
}

