www.pudn.com > sphinx_recipe.zip > CreateConfig.pl


#!/usr/bin/perl

# Create a CMU sphinx confirugation file from a template
# by replacing name/value pairs in the template file.
#
# Can either accept the name/value pairs on the command
# line, or if an input template filename, this file is 
# assumed to contain name/value pairs, one on each line.
#
# Also can take a config file plus additional command
# line name/value pairs.  Command line options override
# things in the config file.
#
# Copyright 2006 by Keith Vertanen
#

use strict;

if ( @ARGV < 2 )
{
    print "$0    [name2] [value2] ...\n"; 
    print " or\n";
    print "$0   [name1] [value1] ...\n";
    exit(1);
}

my $templateFile;
my $line;
my $i;
#my $pos;
my $find;
#my @names;
#my @values;
my %pairs;
my $configFile;
my @chunks;
my $argNum;
my $name;

($templateFile) = $ARGV[0];

$argNum = 1;

if ( (@ARGV % 2) == 0)
{
    # Read in the name/value pairs from another config file

    ($configFile) = @ARGV[1];

    open(IN, $configFile);
    while ($line = )
    {
	$line =~ s/[\n\r]//g;

	# Skip any comment lines
	if (index($line, "\#") != 0)
	{
	    @chunks = split(/\s{1,}/, $line);
	    if (@chunks == 2)
	    {
#		$names[$pos]   = $chunks[0];
#		$values[$pos]  = $chunks[1];

		# Replace any shell variables
		if (index($chunks[1], "\$") == 0)
		{
		    $chunks[1] = $ENV{substr($chunks[1], 1)};
		}
			
		$pairs{$chunks[0]} = $chunks[1];
	
#		print "name = " . $chunks[0] . ", value = " . $pairs{$chunks[0]} . "\n";
		
#		$pos++;
	    }
	}
    }
    close(IN);

    $argNum = 2;
}

# Get the name/value pairs from the command line
for ($i = $argNum; $i < scalar @ARGV; $i = $i + 2)
{
    $pairs{$ARGV[$i]} = $ARGV[$i + 1];

#    print "command line, name = " . $ARGV[$i] . ", value = " . $pairs{$ARGV[$i]} . "\n";

}



open(IN, $templateFile);

while ($line = ) 
{
    foreach $name (%pairs)
    {
	$find = "__" . $name . "__";
	$line =~ s/$find/$pairs{$name}/g;	
    }
   
    print $line;
}
close IN;