eae4f49e46ea62d8e26ed4600ca2ea6431274344
[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 sub new {
39   $main::lxdebug->enter_sub();
40
41   my ($type, $file, $level) = @_;
42
43   my $id = "";
44   my $skip;
45
46   local *FH;
47
48   $type = ref($self) || $self;
49
50   open FH, "$file" or Form->error("$file : $!");
51
52   while (<FH>) {
53     next if /^(#|;|\s)/;
54     last if /^\./;
55
56     chop;
57
58     # strip comments
59     s/\s*(#|;).*//g;
60
61     # remove any trailing whitespace
62     s/^\s*(.*?)\s*$/$1/;
63
64     if (/^\[/) {
65       s/(\[|\])//g;
66
67       $id = $_;
68
69       # if there is a level skip
70       if ($skip = ($id !~ /^$level/)) {
71         next;
72       }
73
74       push @{ $self->{ORDER} }, $_;
75
76       next;
77
78     }
79
80     if (!$skip) {
81
82       # add key=value to $id
83       my ($key, $value) = split /=/, $_, 2;
84
85       $self->{$id}{$key} = $value;
86     }
87
88   }
89   close FH;
90
91   $main::lxdebug->leave_sub();
92
93   bless $self, $type;
94 }
95
96 1;
97