Form::error nur als Instanz-, nicht als Klassenmethode aufrufen
[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 use strict;
41
42 sub new {
43   $main::lxdebug->enter_sub(2);
44
45   my ($type, $file, %options) = @_;
46
47   my $id = "";
48   my $skip;
49
50   local *FH;
51
52   my $self = { "FILE" => $file };
53
54   open FH, "$file" or $::form->error("$file : $!");
55
56   while (<FH>) {
57     chomp;
58
59     if (!$options{verbatim}) {
60       # strip comments
61       s/\#.*//;
62
63       # remove any trailing whitespace
64       s/^\s*//;
65       s/\s*$//;
66     } else {
67       next if (m/^\s*\#/);
68     }
69
70     next unless $_;
71
72     if (m/^\[/) {
73       s/(\[|\])//g;
74
75       $id = $_;
76
77       $self->{$id} ||= { };
78
79       push @{ $self->{ORDER} }, $_;
80
81       next;
82
83     }
84
85     # add key=value to $id
86     my ($key, $value) = split m/=/, $_, 2;
87
88     $self->{$id}->{$key} = $value;
89
90   }
91   close FH;
92
93   $main::lxdebug->leave_sub(2);
94
95   return bless $self, $type;
96 }
97
98 sub write {
99   $main::lxdebug->enter_sub();
100
101   my ($self) = @_;
102
103   my $file = $self->{FILE};
104   my $fh   = IO::File->new($file, "w") || $::form->error("$file : $!");
105
106   foreach my $section_name (sort keys %{ $self }) {
107     next if $section_name =~ m/^[A-Z]+$/;
108
109     my $section = $self->{$section_name};
110     print $fh "[${section_name}]\n";
111     map { print $fh "${_}=$section->{$_}\n" } sort keys %{ $section };
112     print $fh "\n";
113   }
114
115   $fh->close();
116
117   $main::lxdebug->leave_sub();
118 }
119
120 1;