1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#=====================================================================
# SQL-Ledger ERP
# Copyright (C) 2006
#
# Author: DWS Systems Inc.
# Web: http://www.sql-ledger.com
#
#=====================================================================
#
# routines to retrieve / manipulate win ini style files
# ORDER is used to keep the elements in the order they appear in .ini
#
#=====================================================================
package Inifile;
sub new {
my ($type, $file) = @_;
$type = ref($type) || $type;
my $self = bless {}, $type;
$self->add_file($file) if defined $file;
return $self;
}
sub add_file {
my ($self, $file) = @_;
my $id = "";
my %menuorder = ();
for (@{$self->{ORDER}}) { $menuorder{$_} = 1 }
open FH, "$file" or Form->error("$file : $!");
while (<FH>) {
next if /^(#|;|\s)/;
last if /^\./;
chop;
# strip comments
s/\s*(#|;).*//g;
# remove any trailing whitespace
s/^\s*(.*?)\s*$/$1/;
if (/^\[/) {
s/(\[|\])//g;
$id = $_;
push @{$self->{ORDER}}, $_ if ! $menuorder{$_};
$menuorder{$_} = 1;
next;
}
# add key=value to $id
my ($key, $value) = split /=/, $_, 2;
$self->{$id}{$key} = $value;
}
close FH;
}
1;
|