kivitendo/SL/Inifile.pm @ dd9732d8
d319704a | Moritz Bunkus | #=====================================================================
|
||
# LX-Office ERP
|
||||
# Copyright (C) 2004
|
||||
# Based on SQL-Ledger Version 2.1.9
|
||||
# Web http://www.lx-office.org
|
||||
#
|
||||
#=====================================================================
|
||||
# SQL-Ledger Accounting
|
||||
# Copyright (C) 2001
|
||||
#
|
||||
# Author: Dieter Simader
|
||||
# Email: dsimader@sql-ledger.org
|
||||
# Web: http://www.sql-ledger.org
|
||||
#
|
||||
# Contributors:
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# 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
|
||||
# GNU General Public License for more details.
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
f7b15d43 | Christian Wittmer | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
# MA 02110-1335, USA.
|
||||
d319704a | Moritz Bunkus | #=====================================================================
|
||
#
|
||||
# routines to retrieve / manipulate win ini style files
|
||||
# ORDER is used to keep the elements in the order they appear in .ini
|
||||
#
|
||||
#=====================================================================
|
||||
package Inifile;
|
||||
db53dc8a | Moritz Bunkus | use IO::File;
|
||
76c486e3 | Sven Schöling | use strict;
|
||
d319704a | Moritz Bunkus | sub new {
|
||
705b8473 | Sven Schöling | $main::lxdebug->enter_sub(2);
|
||
d319704a | Moritz Bunkus | |||
dc3cd296 | Moritz Bunkus | my ($type, $file, %options) = @_;
|
||
d319704a | Moritz Bunkus | |||
my $id = "";
|
||||
189d03d2 | Sven Schöling | my $cur;
|
||
d319704a | Moritz Bunkus | |||
189d03d2 | Sven Schöling | my $self = { FILE => $file, ORDER => [] };
|
||
ba863fd6 | Moritz Bunkus | |||
189d03d2 | Sven Schöling | open my $fh, "$file" or $::form->error("$file : $!");
|
||
d319704a | Moritz Bunkus | |||
189d03d2 | Sven Schöling | for (<$fh>) {
|
||
db53dc8a | Moritz Bunkus | chomp;
|
||
d319704a | Moritz Bunkus | |||
dc3cd296 | Moritz Bunkus | if (!$options{verbatim}) {
|
||
# strip comments
|
||||
# remove any trailing whitespace
|
||||
189d03d2 | Sven Schöling | s/\s*#.*$//;
|
||
dc3cd296 | Moritz Bunkus | s/^\s*//;
|
||
} else {
|
||||
8a27e4ff | Sven Schöling | next if m/^\s*#/;
|
||
dc3cd296 | Moritz Bunkus | }
|
||
db53dc8a | Moritz Bunkus | |||
next unless $_;
|
||||
d319704a | Moritz Bunkus | |||
189d03d2 | Sven Schöling | if (m/^\[(.*)\]$/) {
|
||
$id = $1;
|
||||
$cur = $self->{$1} ||= { };
|
||||
d319704a | Moritz Bunkus | |||
189d03d2 | Sven Schöling | push @{ $self->{ORDER} }, $1;
|
||
} else {
|
||||
# add key=value to $id
|
||||
my ($key, $value) = split m/=/, $_, 2;
|
||||
d319704a | Moritz Bunkus | |||
189d03d2 | Sven Schöling | $cur->{$key} = $value;
|
||
d319704a | Moritz Bunkus | }
|
||
}
|
||||
189d03d2 | Sven Schöling | close $fh;
|
||
d319704a | Moritz Bunkus | |||
705b8473 | Sven Schöling | $main::lxdebug->leave_sub(2);
|
||
d319704a | Moritz Bunkus | |||
db53dc8a | Moritz Bunkus | return bless $self, $type;
|
||
}
|
||||
sub write {
|
||||
$main::lxdebug->enter_sub();
|
||||
my ($self) = @_;
|
||||
my $file = $self->{FILE};
|
||||
0503e56b | Moritz Bunkus | my $fh = IO::File->new($file, "w") || $::form->error("$file : $!");
|
||
db53dc8a | Moritz Bunkus | |||
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();
|
||||
d319704a | Moritz Bunkus | }
|
||
1;
|