line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Copyright (C) 2004 by Dominic Mitchell. All rights reserved. |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Redistribution and use in source and binary forms, with or without |
4
|
|
|
|
|
|
|
# modification, are permitted provided that the following conditions |
5
|
|
|
|
|
|
|
# are met: |
6
|
|
|
|
|
|
|
# 1. Redistributions of source code must retain the above copyright |
7
|
|
|
|
|
|
|
# notice, this list of conditions and the following disclaimer. |
8
|
|
|
|
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright |
9
|
|
|
|
|
|
|
# notice, this list of conditions and the following disclaimer in the |
10
|
|
|
|
|
|
|
# documentation and/or other materials provided with the distribution. |
11
|
|
|
|
|
|
|
# |
12
|
|
|
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
13
|
|
|
|
|
|
|
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
14
|
|
|
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
15
|
|
|
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
16
|
|
|
|
|
|
|
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
17
|
|
|
|
|
|
|
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
18
|
|
|
|
|
|
|
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
19
|
|
|
|
|
|
|
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
20
|
|
|
|
|
|
|
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
21
|
|
|
|
|
|
|
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
22
|
|
|
|
|
|
|
# SUCH DAMAGE. |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=pod |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 NAME |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
Config::Setting::IniParser - parse windows .ini style files. |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 SYNOPSIS |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
use Config::Setting::IniParser; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
my $ini = Config::Setting::IniParser->new(Filename => $inifile); |
35
|
|
|
|
|
|
|
foreach my $s ($ini->sections()) { |
36
|
|
|
|
|
|
|
print "[$s]\n"; |
37
|
|
|
|
|
|
|
foreach my $k ($ini->keylist($s)) { |
38
|
|
|
|
|
|
|
print $k, "=", $ini->get($s, $k), "\n"; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
print "\n"; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head1 DESCRIPTION |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
This class provides OO access to windows .ini style files. At present, |
47
|
|
|
|
|
|
|
it only provides read access, not writing. |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head1 METHODS |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=over 4 |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=item new ( ARGS ) |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Instantiate a new object. ARGS is a set of keyword / value pairs. |
56
|
|
|
|
|
|
|
Recognised options are: |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=over 4 |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=item CommentChar |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
Pass in a character that is used as a comment inside the data. This |
63
|
|
|
|
|
|
|
defaults to "#", but is also commonly ";". |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=back |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=item parse_file ( FILENAME ) |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Parse FILENAME into the object. |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=item parse_string ( STRING ) |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Parse STRING into the object. |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=item sections ( ) |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
Return a list of all sections that occurred in the data. They are |
78
|
|
|
|
|
|
|
returned in the order in which they originally occurred. |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=item keylist ( SECTION ) |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Return a list of all keys in SECTION. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=item get ( SECTION, KEY ) |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Return the value of KEY in SECTION. |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=back |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 SEE ALSO |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
perl(1). |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 AUTHOR |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Dominic Mitchell, Ecpan (at) happygiraffe.netE. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 BUGS |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Does not cater for quoted keys and values. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
It is a bit eager about comment stripping. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=cut |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
package Config::Setting::IniParser; |
107
|
|
|
|
|
|
|
|
108
|
2
|
|
|
2
|
|
10
|
use strict; |
|
2
|
|
|
|
|
90
|
|
|
2
|
|
|
|
|
78
|
|
109
|
2
|
|
|
2
|
|
11
|
use vars qw($rcsid $VERSION); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
103
|
|
110
|
|
|
|
|
|
|
|
111
|
2
|
|
|
2
|
|
10
|
use Carp; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
197
|
|
112
|
2
|
|
|
2
|
|
1172
|
use Config::Setting::Chunk; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
1162
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
$rcsid = '@(#) $Id: IniParser.pm 765 2005-08-31 20:05:59Z dom $ '; |
115
|
|
|
|
|
|
|
$VERSION = (qw( $Revision: 765 $ ))[1]; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
# Pass in either a Filename parameter or a String parameter. |
118
|
|
|
|
|
|
|
sub new { |
119
|
1
|
|
|
1
|
1
|
3
|
my $class = shift; |
120
|
1
|
|
|
|
|
2
|
my (%args) = @_; |
121
|
|
|
|
|
|
|
|
122
|
1
|
|
|
|
|
7
|
my $self = { |
123
|
|
|
|
|
|
|
Contents => {}, |
124
|
|
|
|
|
|
|
Sections => [], |
125
|
|
|
|
|
|
|
CommentChar => "#", |
126
|
|
|
|
|
|
|
%args, |
127
|
|
|
|
|
|
|
}; |
128
|
1
|
|
|
|
|
3
|
bless($self, $class); |
129
|
1
|
|
|
|
|
4
|
return $self; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub parse_file { |
133
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
134
|
0
|
0
|
|
|
|
0
|
open my $fh, $self->{Filename} |
135
|
|
|
|
|
|
|
or croak "open($self->{Filename}): $!"; |
136
|
0
|
|
|
|
|
0
|
my $string = do { local $/ ; <$fh> }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
137
|
0
|
|
|
|
|
0
|
close $fh; |
138
|
0
|
|
|
|
|
0
|
return $self->_parse( $string ); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub parse_string { |
142
|
1
|
|
|
1
|
1
|
3
|
my $self = shift; |
143
|
1
|
|
|
|
|
2
|
my ( $string ) = @_; |
144
|
1
|
|
|
|
|
4
|
return $self->_parse( $string ); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
# Parse the stuff we hold. |
148
|
|
|
|
|
|
|
sub _parse { |
149
|
1
|
|
|
1
|
|
2
|
my $self = shift; |
150
|
1
|
|
|
|
|
2
|
my ( $string ) = @_; |
151
|
1
|
|
|
|
|
2
|
my $section = ""; |
152
|
1
|
|
|
|
|
6
|
my $cc = $self->{CommentChar}; |
153
|
1
|
|
|
|
|
1
|
my $lineno = 1; |
154
|
1
|
|
|
|
|
9
|
my $chunk = Config::Setting::Chunk->new; |
155
|
|
|
|
|
|
|
|
156
|
1
|
|
|
|
|
25
|
foreach my $line (split /\r?\n/, $string) { |
157
|
16
|
|
|
|
|
62
|
$line =~ s/$cc.*//; |
158
|
16
|
|
|
|
|
28
|
$line =~ s/^\s+//; |
159
|
16
|
100
|
|
|
|
39
|
next unless $line; |
160
|
|
|
|
|
|
|
|
161
|
8
|
100
|
|
|
|
48
|
if ($line =~ m/^\[(.*?)\]/) { |
|
|
50
|
|
|
|
|
|
162
|
2
|
|
|
|
|
6
|
$section = $1; |
163
|
2
|
|
|
|
|
7
|
$chunk->add_section( $section ); |
164
|
|
|
|
|
|
|
} elsif ($line =~ m/^(.+?)\s*=\s*(.*)/) { |
165
|
6
|
50
|
|
|
|
13
|
croak "line $lineno occurs outside a section" |
166
|
|
|
|
|
|
|
unless $section; |
167
|
6
|
|
|
|
|
22
|
$chunk->set_item( $section, $1, $2 ); |
168
|
|
|
|
|
|
|
} else { |
169
|
0
|
|
|
|
|
0
|
carp "line $lineno is invalid: '$line'"; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
} |
172
|
1
|
|
|
|
|
15
|
return $chunk; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
1; |
176
|
|
|
|
|
|
|
__END__ |