can't set $p without $cgi
[freeside.git] / sql-ledger / old / sql-ledger / SL / Inifile.pm
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (C) 2001
4 #
5 #  Author: Dieter Simader
6 #   Email: dsimader@sql-ledger.org
7 #     Web: http://www.sql-ledger.org
8 #
9 #  Contributors:
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #=====================================================================
24 #
25 # routines to retrieve / manipulate win ini style files
26 # ORDER is used to keep the elements in the order they appear in .ini
27 #
28 #=====================================================================
29
30 package Inifile;
31
32
33 sub new {
34   my ($type, $file, $level) = @_;
35   
36   my $id = "";
37   my $skip;
38   
39   $type = ref($self) || $self;
40   
41   open FH, "$file" or Form->error("$file : $!");
42
43   while (<FH>) {
44     next if /^(#|;|\s)/;
45     last if /^\./;
46
47     chop;
48
49     # strip comments
50     s/\s*(#|;).*//g;
51     
52     # remove any trailing whitespace
53     s/^\s*(.*?)\s*$/$1/;
54
55     if (/^\[/) {
56       s/(\[|\])//g;
57
58       $id = $_;
59
60       # if there is a level skip
61       if ($skip = ($id !~ /^$level/)) {
62         next;
63       }
64
65       push @{$self->{ORDER}}, $_;
66       
67       next;
68       
69     }
70
71     if (!$skip) {
72       # add key=value to $id
73       my ($key, $value) = split /=/, $_, 2;
74       
75       $self->{$id}{$key} = $value;
76     }
77
78   }
79   close FH;
80   
81   bless $self, $type;
82   
83 }
84
85
86 1;
87