35 |
35 |
|
36 |
36 |
package Inifile;
|
37 |
37 |
|
|
38 |
use IO::File;
|
|
39 |
|
38 |
40 |
sub new {
|
39 |
41 |
$main::lxdebug->enter_sub();
|
40 |
42 |
|
41 |
|
my ($type, $file, $level) = @_;
|
|
43 |
my ($type, $file) = @_;
|
42 |
44 |
|
43 |
45 |
my $id = "";
|
44 |
46 |
my $skip;
|
45 |
47 |
|
46 |
48 |
local *FH;
|
47 |
49 |
|
48 |
|
$type = ref($self) || $self;
|
|
50 |
my $self = { "FILE" => $file };
|
49 |
51 |
|
50 |
52 |
open FH, "$file" or Form->error("$file : $!");
|
51 |
53 |
|
52 |
54 |
while (<FH>) {
|
53 |
|
next if /^(#|;|\s)/;
|
54 |
|
last if /^\./;
|
|
55 |
next if m/^(#|;|\s)/;
|
55 |
56 |
|
56 |
|
chop;
|
|
57 |
chomp;
|
57 |
58 |
|
58 |
59 |
# strip comments
|
59 |
|
s/\s*(#|;).*//g;
|
|
60 |
s/(#|;).*//g;
|
60 |
61 |
|
61 |
62 |
# remove any trailing whitespace
|
62 |
|
s/^\s*(.*?)\s*$/$1/;
|
|
63 |
s/^\s*//;
|
|
64 |
s/\s*$//;
|
|
65 |
|
|
66 |
next unless $_;
|
63 |
67 |
|
64 |
|
if (/^\[/) {
|
|
68 |
if (m/^\[/) {
|
65 |
69 |
s/(\[|\])//g;
|
66 |
70 |
|
67 |
71 |
$id = $_;
|
68 |
72 |
|
69 |
|
# if there is a level skip
|
70 |
|
if ($skip = ($id !~ /^$level/)) {
|
71 |
|
next;
|
72 |
|
}
|
|
73 |
$self->{$id} ||= { };
|
73 |
74 |
|
74 |
75 |
push @{ $self->{ORDER} }, $_;
|
75 |
76 |
|
... | ... | |
77 |
78 |
|
78 |
79 |
}
|
79 |
80 |
|
80 |
|
if (!$skip) {
|
|
81 |
# add key=value to $id
|
|
82 |
my ($key, $value) = split m/=/, $_, 2;
|
81 |
83 |
|
82 |
|
# add key=value to $id
|
83 |
|
my ($key, $value) = split /=/, $_, 2;
|
84 |
|
|
85 |
|
$self->{$id}{$key} = $value;
|
86 |
|
}
|
|
84 |
$self->{$id}->{$key} = $value;
|
87 |
85 |
|
88 |
86 |
}
|
89 |
87 |
close FH;
|
90 |
88 |
|
91 |
89 |
$main::lxdebug->leave_sub();
|
92 |
90 |
|
93 |
|
bless $self, $type;
|
|
91 |
return bless $self, $type;
|
|
92 |
}
|
|
93 |
|
|
94 |
sub write {
|
|
95 |
$main::lxdebug->enter_sub();
|
|
96 |
|
|
97 |
my ($self) = @_;
|
|
98 |
|
|
99 |
my $file = $self->{FILE};
|
|
100 |
my $fh = IO::File->new($file, "w") || Form->error("$file : $!");
|
|
101 |
|
|
102 |
foreach my $section_name (sort keys %{ $self }) {
|
|
103 |
next if $section_name =~ m/^[A-Z]+$/;
|
|
104 |
|
|
105 |
my $section = $self->{$section_name};
|
|
106 |
print $fh "[${section_name}]\n";
|
|
107 |
map { print $fh "${_}=$section->{$_}\n" } sort keys %{ $section };
|
|
108 |
print $fh "\n";
|
|
109 |
}
|
|
110 |
|
|
111 |
$fh->close();
|
|
112 |
|
|
113 |
$main::lxdebug->leave_sub();
|
94 |
114 |
}
|
95 |
115 |
|
96 |
116 |
1;
|
Inifile.pm auch dazu benutzen, um die memers/users einzulesen und neu zu schreiben.