";" nicht als Kommentarzeichen ansehen.
[kivitendo-erp.git] / SL / Inifile.pm
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 2001
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors:
16 #
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
21 #
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #=====================================================================
30 #
31 # routines to retrieve / manipulate win ini style files
32 # ORDER is used to keep the elements in the order they appear in .ini
33 #
34 #=====================================================================
35
36 package Inifile;
37
38 use IO::File;
39
40 sub new {
41   $main::lxdebug->enter_sub();
42
43   my ($type, $file) = @_;
44
45   my $id = "";
46   my $skip;
47
48   local *FH;
49
50   my $self = { "FILE" => $file };
51
52   open FH, "$file" or Form->error("$file : $!");
53
54   while (<FH>) {
55     chomp;
56
57     # strip comments
58     s/#.*//g;
59
60     # remove any trailing whitespace
61     s/^\s*//;
62     s/\s*$//;
63
64     next unless $_;
65
66     if (m/^\[/) {
67       s/(\[|\])//g;
68
69       $id = $_;
70
71       $self->{$id} ||= { };
72
73       push @{ $self->{ORDER} }, $_;
74
75       next;
76
77     }
78
79     # add key=value to $id
80     my ($key, $value) = split m/=/, $_, 2;
81
82     $self->{$id}->{$key} = $value;
83
84   }
85   close FH;
86
87   $main::lxdebug->leave_sub();
88
89   return bless $self, $type;
90 }
91
92 sub write {
93   $main::lxdebug->enter_sub();
94
95   my ($self) = @_;
96
97   my $file = $self->{FILE};
98   my $fh   = IO::File->new($file, "w") || Form->error("$file : $!");
99
100   foreach my $section_name (sort keys %{ $self }) {
101     next if $section_name =~ m/^[A-Z]+$/;
102
103     my $section = $self->{$section_name};
104     print $fh "[${section_name}]\n";
105     map { print $fh "${_}=$section->{$_}\n" } sort keys %{ $section };
106     print $fh "\n";
107   }
108
109   $fh->close();
110
111   $main::lxdebug->leave_sub();
112 }
113
114 1;
115