forked from PASApipeline/PASApipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIniReader.pm
93 lines (56 loc) · 1.34 KB
/
IniReader.pm
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package IniReader;
use strict;
use warnings;
use Carp;
sub new {
my $packagename = shift;
my ($filename) = @_;
my $self = { section_to_att_val => {}, # section -> att = value
};
open (my $fh, $filename) or confess "Error, cannot open file $filename";
my $current_section = "";
while (<$fh>) {
if (/^[\:\#]/) { next; } ## comment line
unless (/\w/) { next; }
if (/\[([^\]]+)\]/) {
$current_section = $1;
$current_section = &_trim_flank_ws($current_section);
}
elsif (/^(.*)=(.*)$/) {
my $att = $1;
my $val = $2;
$att = &_trim_flank_ws($att);
$val = &_trim_flank_ws($val);
$self->{section_to_att_val}->{$current_section}->{$att} = $val;
}
}
close $fh;
bless ($self, $packagename);
return($self);
}
####
sub get_section_headings {
my $self = shift;
my @section_headings = keys %{$self->{section_to_att_val}};
return(@section_headings);
}
####
sub get_section_attributes {
my $self = shift;
my $section = shift;
my @attributes = keys %{$self->{section_to_att_val}->{$section}};
return(@attributes);
}
####
sub get_value {
my $self = shift;
my ($section, $attribute) = @_;
return ($self->{section_to_att_val}->{$section}->{$attribute});
}
####
sub _trim_flank_ws {
my ($string) = @_;
$string =~ s/^\s+|\s+$//g;
return($string);
}
1; #EOM