configreader
Differences
This shows you the differences between two versions of the page.
configreader [2005/10/28 08:01] – 82.79.122.73 | configreader [2005/10/28 08:52] (current) – old revision restored 84.56.209.62 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Perl Pseudo XML Configreader ====== | ||
+ | This sub reads an configfile and returns it as a Datastructure. The format is similar to the one used by Apache. | ||
+ | |||
+ | FIXME add example config | ||
+ | |||
+ | <code perl> | ||
+ | sub readConfig($){ | ||
+ | my $cfgfile = $_[0]; | ||
+ | open (FILE, | ||
+ | my @file = < | ||
+ | close (FILE); | ||
+ | |||
+ | my ($name, | ||
+ | my %config; | ||
+ | my $section=''; | ||
+ | my $sname=''; | ||
+ | |||
+ | foreach my $line (@file){ | ||
+ | # skip comments | ||
+ | $line=~ s/#.*$//; | ||
+ | $line=~ s/^\s*//; | ||
+ | $line=~ s/\s*$//; | ||
+ | next if ($line eq '' | ||
+ | # end of section | ||
+ | if($line =~ m/< | ||
+ | $section = ''; | ||
+ | $sname = ''; | ||
+ | next; | ||
+ | } | ||
+ | # start of section | ||
+ | if($line =~ m/< | ||
+ | $section = $1; | ||
+ | $sname = $2; | ||
+ | $sname =~ s/^\s*//; | ||
+ | $sname =~ s/\s*$//; | ||
+ | if($sname eq '' | ||
+ | | ||
+ | }else{ | ||
+ | | ||
+ | } | ||
+ | next; | ||
+ | } | ||
+ | if($section && $sname eq '' | ||
+ | #inside nameless section | ||
+ | if($line =~ m/.+=.+/){ | ||
+ | ($name, | ||
+ | $val=~ s/^\s*//; | ||
+ | $name=~ s/\s*$//; | ||
+ | ${$config{lc($section)}}{lc($name)}=$val; | ||
+ | }else{ | ||
+ | push(@{$config{lc($section)}}, | ||
+ | } | ||
+ | }elsif($section && $sname ne '' | ||
+ | #inside named section | ||
+ | if($line =~ m/.+=.+/){ | ||
+ | ($name, | ||
+ | $val=~ s/^\s*//; | ||
+ | $name=~ s/\s*$//; | ||
+ | ${${$config{lc($section)}}{$sname}}{lc($name)}=$val; | ||
+ | }else{ | ||
+ | push(@{${$config{lc($section)}}{$sname}}, | ||
+ | } | ||
+ | }else{ | ||
+ | #no section | ||
+ | ($name, | ||
+ | $val=~ s/^\s*//; | ||
+ | $name=~ s/\s*$//; | ||
+ | $config{lc($name)}=$val; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | return %config; | ||
+ | } | ||
+ | </ |
configreader.txt · Last modified: 2005/10/28 08:52 by 84.56.209.62