Cleaned up a bit and added more comments.
This commit is contained in:
1 parent
ce6c0c5e3a
commit
f95612103f
1 file changed
+24
-2
+24
-2
@@ -5,20 +5,24 @@ package Parser::Config;
|
|||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
|
|
||||||
|
# Create a new instance.
|
||||||
sub new
|
sub new
|
||||||
{
|
{
|
||||||
my $class = shift;
|
my $class = shift;
|
||||||
my ($file) = @_;
|
my ($file) = @_;
|
||||||
my $self = bless {}, $class;
|
my $self = bless {}, $class;
|
||||||
|
|
||||||
|
# Check to see if the configuration file exists.
|
||||||
if (!-e "$Auto::Bin/../etc/$file") {
|
if (!-e "$Auto::Bin/../etc/$file") {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Open, read and close the config.
|
||||||
open CONF, "<$Auto::Bin/../etc/$file" or return 0;
|
open CONF, "<$Auto::Bin/../etc/$file" or return 0;
|
||||||
my @cosfl = <CONF> or return 0;
|
my @cosfl = <CONF> or return 0;
|
||||||
close CONF or return 0;
|
close CONF or return 0;
|
||||||
|
|
||||||
|
# Save it to self variable.
|
||||||
$self->{'config'}->{'path'} = "$Auto::Bin/../etc/$file";
|
$self->{'config'}->{'path'} = "$Auto::Bin/../etc/$file";
|
||||||
|
|
||||||
return $self;
|
return $self;
|
||||||
@@ -62,34 +66,48 @@ sub parse
|
|||||||
my $param = $ebuf[1];
|
my $param = $ebuf[1];
|
||||||
$param =~ s/"//g;
|
$param =~ s/"//g;
|
||||||
my @param = ($param);
|
my @param = ($param);
|
||||||
|
|
||||||
unless ($blk eq 0) {
|
unless (!$blk) {
|
||||||
|
# We're inside a block.
|
||||||
if ($blk =~ m/@@@/) {
|
if ($blk =~ m/@@@/) {
|
||||||
|
# We're inside a block with a parameter.
|
||||||
my @sblk = split('@@@', $blk);
|
my @sblk = split('@@@', $blk);
|
||||||
|
|
||||||
|
# Check to see if this config option already exists.
|
||||||
if (defined $rs{c}{$sblk[0]}{$sblk[1]}{$ebuf[0]}) {
|
if (defined $rs{c}{$sblk[0]}{$sblk[1]}{$ebuf[0]}) {
|
||||||
|
# It does, so merely push this second one to the existing array.
|
||||||
push(@{ $rs{c}{$sblk[0]}{$sblk[1]}{$ebuf[0]} }, $param);
|
push(@{ $rs{c}{$sblk[0]}{$sblk[1]}{$ebuf[0]} }, $param);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
# It doesn't, create it as an array.
|
||||||
@{ $rs{c}{$sblk[0]}{$sblk[1]}{$ebuf[0]} } = @param;
|
@{ $rs{c}{$sblk[0]}{$sblk[1]}{$ebuf[0]} } = @param;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
# We're inside a block with no parameter.
|
||||||
$rs{c}{$blk}{$ebuf[0]} = $ebuf[1];
|
$rs{c}{$blk}{$ebuf[0]} = $ebuf[1];
|
||||||
|
|
||||||
|
# Check to see if this config option already exists.
|
||||||
if (defined $rs{c}{$blk}{$ebuf[0]}) {
|
if (defined $rs{c}{$blk}{$ebuf[0]}) {
|
||||||
|
# It does, so merely push this second one to the existing array.
|
||||||
push(@{ $rs{c}{$blk}{$ebuf[0]} }, $param);
|
push(@{ $rs{c}{$blk}{$ebuf[0]} }, $param);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
# It doesn't, create it as an array.
|
||||||
@{ $rs{c}{$blk}{$ebuf[0]} } = @param;
|
@{ $rs{c}{$blk}{$ebuf[0]} } = @param;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
# We're not inside a block.
|
||||||
|
|
||||||
|
# Check to see if this config option already exists.
|
||||||
if (defined $rs{c}{$ebuf[0]}) {
|
if (defined $rs{c}{$ebuf[0]}) {
|
||||||
|
# It does, so merely push this second one to the existing array.
|
||||||
push(@{ $rs{c}{$ebuf[0]} }, $param);
|
push(@{ $rs{c}{$ebuf[0]} }, $param);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
# It doesn't, create it as an array.
|
||||||
@{ $rs{c}{$ebuf[0]} } = @param;
|
@{ $rs{c}{$ebuf[0]} } = @param;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -97,6 +115,7 @@ sub parse
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
# No semicolon space buffer.
|
||||||
my @ebuf = split(' ', $buff);
|
my @ebuf = split(' ', $buff);
|
||||||
|
|
||||||
if (!defined $ebuf[0]) {
|
if (!defined $ebuf[0]) {
|
||||||
@@ -119,11 +138,14 @@ sub parse
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($ebuf[0] eq '}') {
|
if ($ebuf[0] eq '}') {
|
||||||
|
# This is the end of a block.
|
||||||
$blk = 0;
|
$blk = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Return the configuration data.
|
||||||
return %rs;
|
return %rs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user