epic-s6ts
[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., 51 Franklin Street, Fifth Floor, Boston,
29 # MA 02110-1335, USA.
30 #=====================================================================
31 #
32 # routines to retrieve / manipulate win ini style files
33 # ORDER is used to keep the elements in the order they appear in .ini
34 #
35 #=====================================================================
36
37 package Inifile;
38
39 use IO::File;
40
41 use strict;
42
43 sub new {
44   $main::lxdebug->enter_sub(2);
45
46   my ($type, $file, %options) = @_;
47
48   my $id = "";
49   my $cur;
50
51   my $self = { FILE => $file, ORDER => [] };
52
53   open my $fh, "$file" or $::form->error("$file : $!");
54
55   for (<$fh>) {
56     chomp;
57
58     if (!$options{verbatim}) {
59       # strip comments
60       # remove any trailing whitespace
61       s/\s*#.*$//;
62       s/^\s*//;
63     } else {
64       next if m/^\s*#/;
65     }
66
67     next unless $_;
68
69     if (m/^\[(.*)\]$/) {
70       $id = $1;
71       $cur = $self->{$1} ||= { };
72
73       push @{ $self->{ORDER} }, $1;
74     } else {
75       # add key=value to $id
76       my ($key, $value) = split m/=/, $_, 2;
77
78       $cur->{$key} = $value;
79     }
80
81   }
82   close $fh;
83
84   $main::lxdebug->leave_sub(2);
85
86   return bless $self, $type;
87 }
88
89 sub write {
90   $main::lxdebug->enter_sub();
91
92   my ($self) = @_;
93
94   my $file = $self->{FILE};
95   my $fh   = IO::File->new($file, "w") || $::form->error("$file : $!");
96
97   foreach my $section_name (sort keys %{ $self }) {
98     next if $section_name =~ m/^[A-Z]+$/;
99
100     my $section = $self->{$section_name};
101     print $fh "[${section_name}]\n";
102     map { print $fh "${_}=$section->{$_}\n" } sort keys %{ $section };
103     print $fh "\n";
104   }
105
106   $fh->close();
107
108   $main::lxdebug->leave_sub();
109 }
110
111 1;