Hi guys !
A little problem. I have a parser (not mine) for a configuration file (smb.conf if someone knows). this file as from the format:
[name1]
key1 = value1
key2 = value2
...
[name2]
key1 = value1
key2 = value2
...
# and so on
The problem is when I have something like this:
[name1]
key1 = value1
key2 = value2
...
[name2]
[name3]
key1 = value1
key2 = value2
...
as you can see
name2 has no values, this thing is ok (default values are taken), but the parser I have doesn't return it, here is it's code:
sub smbconf_parse {
my $smbconf = shift;
my %smbconf;
my $share = '';
if (! open(SMB, $smbconf)) {
warn "Couldn't read smbconf file $smbconf\n";
warn "$!\n";
return 0;
}
while (<SMB>) {
s/^\s+//g;
s/\s+$//g;
next if (/^$/);
next if (/^\#/);
next if (/^\;/);
if (/^\[(.*)\]/) {
$share = $1; #######
} else {
my ($key, $value) = (/^(.*) ?\= (\S.*)/);
$key =~ s/\s+$//;
if ($value =~ /^\"(.*)\"$/) {
$smbconf{$share}{$key} = $1;
} elsif ($value =~ /\,/) {
my @value = split(/\,/, $value);
$smbconf{$share}{$key} = [ @value ];
} else {
$smbconf{$share}{$key} = $value;
}
}
}
close SMB;
return \%smbconf;
}
I can see the problem, no hash entry is opened if it didn't find any key=value pairs, so I tried adding something like
$smbconf{$share} = ''; # just to open the entry
right after the line with
$share = $1;, but that seemed to mess the whole parsing.
anyone has an idea ?
Hotshot