1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#!/usr/bin/perl
sub getinifile
{
my ($filename) = @_;
my ($section, %entry);
open(FILE, "< $filename");
while(<FILE>) {
if(/^\[(.+?)\].*/) {
$section = $1;
next;
}
if (!/^\s*;/ and /^\s*(\S+)\s*=\s*(\S*?)\s*(;.+)*$/) {
push(@{$entry{"$section;$1"}}, $2);
}
}
close(FILE);
return %entry;
}
sub setinifile
{
my ($filename, $newfilename, %values) = @_;
my ($seciton, $newsection, %newvalues, @list);
open(INFILE, "< $filename");
open(OUTFILE, "> $newfilename");
while(<INFILE>) {
if (/^\[(.+?)\].*/) {
$newsection = $1;
if ($section ne '') {
foreach $key (keys %values) {
@list = @{$values{$key}};
$key =~ /^(\S+?);(\S+)/;
if ($1 eq $section) {
foreach $value (@list) {
print OUTFILE "$2 = $value\n";
}
}
}
}
$section = $newsection;
} elsif (!/^\s*;/ and /^\s*(\S+)\s*=\s*(\S*?)\s*(;.+)*$/) {
@list = @{$values{"$section;$1"}};
if ($#list < 0) {
print OUTFILE "; $_";
} else {
foreach $item (@list) {
if ($item eq $2) {
print OUTFILE "$1 = $2 $3\n";
@list = grep { $_ ne $2 } @list;
@{$values{"$section;$1"}} = @list;
last;
} else {
print OUTFILE "; $1 = $2 $3\n";
}
}
}
next;
}
print OUTFILE $_;
}
close(OUTFILE);
close(INFILE);
}
%inifile = getinifile('php3.ini');
#foreach $i (keys %inifile) {
# foreach $j (@{$inifile{$i}}) {
# print "$i = '$j'\n" if ($i =~ /;;/i);
# }
#}
push(@{$inifile{'PHP_3;extension'}}, 'hallo.hallo');
setinifile('php3.ini', 'php3.ini.new', %inifile);
|