[opendemo-cvs] CVS: opendemo/tools/odcut odcut.pl,1.45,1.46
Status: Beta
Brought to you by:
girlich
From: Uwe G. <gi...@us...> - 2004-10-31 15:27:11
|
Update of /cvsroot/opendemo/opendemo/tools/odcut In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11437 Modified Files: odcut.pl Log Message: Added basic configuration file handling. Index: odcut.pl =================================================================== RCS file: /cvsroot/opendemo/opendemo/tools/odcut/odcut.pl,v retrieving revision 1.45 retrieving revision 1.46 diff -C2 -d -r1.45 -r1.46 *** odcut.pl 24 Oct 2004 18:47:10 -0000 1.45 --- odcut.pl 31 Oct 2004 15:27:01 -0000 1.46 *************** *** 44,47 **** --- 44,48 ---- my $opt_gui = 0; # GUI off my $opt_odfile = "parser"; # default file class: XML::Parser parser + my $opt_config = ""; # default config file: no # parse command line GetOptions( *************** *** 54,57 **** --- 55,59 ---- 'gui' => \$opt_gui, 'file=s' => \$opt_odfile, + 'config|c' => \$opt_config, ) or pod2usage(-msg=>"Syntax error", -verbose=>0); *************** *** 66,69 **** --- 68,79 ---- + # Read in configuration files. + my $config = new odconfig; + # $config->set("hallo","huhu"); + # $config->readfile("out"); + # $config->set("hallo2",$config->get("game_password") . "huhu2"); + # $config->writefile("out2"); + # exit; + # Create the output object. my $output = new odoutput; *************** *** 402,405 **** --- 412,504 ---- + # class definition odconfig ################################################### + package odconfig; + + + sub new($) + { + my $class = shift; + my $self = {}; + bless($self,$class); + $self->_init(); + return $self; + } + + + sub _init($) + { + my $self = shift; + + my $config = { + # 'game_dir' => '/home/uwe/games/q3a/game/linuxq3apoint-1.32b-2', + # 'game_executable' => 'quake3.x86', + # 'game_opendemo' => 'od', + 'game_password' => 'odcut', + }; + + $self->{"config"} = $config; + } + + + + sub get($$) + { + my ($self, $key) = @_; + + if (exists $self->{"config"}->{$key}) { + return $self->{"config"}->{$key}; + } + else { + warn "Config value $key does not exist.\n"; + return undef; + } + } + + + sub set($$$) + { + my ($self, $key, $value) = @_; + $self->{"config"}->{$key} = $value; + } + + + sub readfile($$) + { + my ($self, $filename) = @_; + + my $config = undef; + # TODO Portabiliy: Somehow 'do' does not work. + # unless (my $result = do $filename) { + unless (my $result = eval `cat $filename`) { + warn "couldn't parse $filename: $@\n" if $@; + warn "couldn't do $filename: $!\n" unless defined $result; + warn "couldn't run $filename\n" unless $result; + } + + # Merge data in. + # print $config; + foreach (keys %$config) { + # print ">>$_<<\n"; + $self->{"config"}->{$_} = $config->{$_}; + } + } + + + sub writefile($$) + { + my ($self, $filename) = @_; + + use Data::Dumper; + my $text = Data::Dumper->Dump([$self->{"config"}],["config"]); + my $fh = new IO::File ">$filename"; + if (!defined $fh) { + warn "couldn't open $filename for writing: $!\n"; + return -1; + } + print $fh $text; + $fh->close; + } + + # class definition odoutput ################################################### package odoutput; |