X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FInifile.pm;h=43689d1c032385a034d9b9751063f6b21a78ec21;hb=fee5532a132c44dcfc1743393cba00c8e3397176;hp=7a2ffa7b2ca0fa9801a7e047e62d34a5ed1e8eb7;hpb=4dbb09950c9f5596646537c12d991c99086fe7c1;p=kivitendo-erp.git diff --git a/SL/Inifile.pm b/SL/Inifile.pm index 7a2ffa7b2..43689d1c0 100644 --- a/SL/Inifile.pm +++ b/SL/Inifile.pm @@ -18,7 +18,7 @@ # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. -# +# # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -35,62 +35,86 @@ package Inifile; +use IO::File; + +use strict; sub new { - $main::lxdebug->enter_sub(); + $main::lxdebug->enter_sub(2); + + my ($type, $file, %options) = @_; - my ($type, $file, $level) = @_; - my $id = ""; my $skip; - - $type = ref($self) || $self; - - open FH, "$file" or Form->error("$file : $!"); + + local *FH; + + my $self = { "FILE" => $file }; + + open FH, "$file" or $::form->error("$file : $!"); while () { - next if /^(#|;|\s)/; - last if /^\./; + chomp; - chop; + if (!$options{verbatim}) { + # strip comments + s/\#.*//; - # strip comments - s/\s*(#|;).*//g; - - # remove any trailing whitespace - s/^\s*(.*?)\s*$/$1/; + # remove any trailing whitespace + s/^\s*//; + s/\s*$//; + } else { + next if (m/^\s*\#/); + } + + next unless $_; - if (/^\[/) { + if (m/^\[/) { s/(\[|\])//g; $id = $_; - # if there is a level skip - if ($skip = ($id !~ /^$level/)) { - next; - } + $self->{$id} ||= { }; + + push @{ $self->{ORDER} }, $_; - push @{$self->{ORDER}}, $_; - next; - - } - if (!$skip) { - # add key=value to $id - my ($key, $value) = split /=/, $_, 2; - - $self->{$id}{$key} = $value; } + # add key=value to $id + my ($key, $value) = split m/=/, $_, 2; + + $self->{$id}->{$key} = $value; + } close FH; - - $main::lxdebug->leave_sub(); - bless $self, $type; + $main::lxdebug->leave_sub(2); + + return bless $self, $type; } +sub write { + $main::lxdebug->enter_sub(); -1; + my ($self) = @_; + my $file = $self->{FILE}; + my $fh = IO::File->new($file, "w") || $::form->error("$file : $!"); + + foreach my $section_name (sort keys %{ $self }) { + next if $section_name =~ m/^[A-Z]+$/; + + my $section = $self->{$section_name}; + print $fh "[${section_name}]\n"; + map { print $fh "${_}=$section->{$_}\n" } sort keys %{ $section }; + print $fh "\n"; + } + + $fh->close(); + + $main::lxdebug->leave_sub(); +} + +1;