X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FInifile.pm;h=6895d6030b5c2b15502be359505d7aca93ea9036;hb=3d7898cf2bab46c72face3011460d093d8778b32;hp=8ec3e80bde517b50c52f9a4c392355543b71ff01;hpb=ee072e4f077213bf6f8792ca8f0a1afebbb6282f;p=kivitendo-erp.git diff --git a/SL/Inifile.pm b/SL/Inifile.pm index 8ec3e80bd..6895d6030 100644 --- a/SL/Inifile.pm +++ b/SL/Inifile.pm @@ -35,39 +35,46 @@ package Inifile; +use IO::File; + +use strict; + sub new { - $main::lxdebug->enter_sub(); + $main::lxdebug->enter_sub(2); - my ($type, $file, $level) = @_; + my ($type, $file, %options) = @_; my $id = ""; my $skip; - $type = ref($self) || $self; + 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/\s*$//; + } else { + next if (m/^\s*\#/); + } - # remove any trailing whitespace - s/^\s*(.*?)\s*$/$1/; + next unless $_; - if (/^\[/) { + if (m/^\[/) { s/(\[|\])//g; $id = $_; - # if there is a level skip - if ($skip = ($id !~ /^$level/)) { - next; - } + $self->{$id} ||= { }; push @{ $self->{ORDER} }, $_; @@ -75,20 +82,39 @@ sub new { } - if (!$skip) { + # add key=value to $id + my ($key, $value) = split m/=/, $_, 2; - # add key=value to $id - my ($key, $value) = split /=/, $_, 2; - - $self->{$id}{$key} = $value; - } + $self->{$id}->{$key} = $value; } close FH; - $main::lxdebug->leave_sub(); + $main::lxdebug->leave_sub(2); + + return bless $self, $type; +} - bless $self, $type; +sub write { + $main::lxdebug->enter_sub(); + + 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;