line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Config::Tiny; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# If you thought Config::Simple was small... |
4
|
|
|
|
|
|
|
|
5
|
9
|
|
|
9
|
|
4453
|
use strict; |
|
9
|
|
|
|
|
63
|
|
|
9
|
|
|
|
|
261
|
|
6
|
9
|
|
|
9
|
|
220
|
use 5.008001; # For the utf8 stuff. |
|
9
|
|
|
|
|
32
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# Warning: There is another version line, in t/02.main.t. |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '2.28'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
BEGIN { |
13
|
9
|
|
|
9
|
|
9314
|
$Config::Tiny::errstr = ''; |
14
|
|
|
|
|
|
|
} |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# Create an object. |
17
|
|
|
|
|
|
|
|
18
|
6
|
100
|
|
6
|
1
|
5830
|
sub new { return bless defined $_[1] ? $_[1] : {}, $_[0] } |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Create an object from a file. |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub read |
23
|
|
|
|
|
|
|
{ |
24
|
6
|
50
|
|
6
|
1
|
2534
|
my($class) = ref $_[0] ? ref shift : shift; |
25
|
6
|
|
|
|
|
23
|
my($file, $encoding) = @_; |
26
|
|
|
|
|
|
|
|
27
|
6
|
50
|
33
|
|
|
42
|
return $class -> _error('No file name provided') if (! defined $file || ($file eq '') ); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# Slurp in the file. |
30
|
|
|
|
|
|
|
|
31
|
6
|
100
|
|
|
|
20
|
$encoding = $encoding ? "<:$encoding" : '<'; |
32
|
6
|
|
|
|
|
27
|
local $/ = undef; |
33
|
|
|
|
|
|
|
|
34
|
6
|
50
|
|
1
|
|
247
|
open(my $CFG, $encoding, $file) or return $class -> _error( "Failed to open file '$file' for reading: $!" ); |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
6
|
|
35
|
6
|
|
|
|
|
12128
|
my $contents = <$CFG>; |
36
|
6
|
|
|
|
|
93
|
close($CFG ); |
37
|
|
|
|
|
|
|
|
38
|
6
|
50
|
|
|
|
28
|
return $class -> _error("Reading from '$file' returned undef") if (! defined $contents); |
39
|
|
|
|
|
|
|
|
40
|
6
|
|
|
|
|
39
|
return $class -> read_string( $contents ); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
} # End of read. |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Create an object from a string. |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub read_string |
47
|
|
|
|
|
|
|
{ |
48
|
14
|
50
|
|
14
|
1
|
9573
|
my($class) = ref $_[0] ? ref shift : shift; |
49
|
14
|
|
|
|
|
40
|
my($self) = bless {}, $class; |
50
|
|
|
|
|
|
|
|
51
|
14
|
50
|
|
|
|
44
|
return undef unless defined $_[0]; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# Parse the file. |
54
|
|
|
|
|
|
|
|
55
|
14
|
|
|
|
|
24
|
my $ns = '_'; |
56
|
14
|
|
|
|
|
27
|
my $counter = 0; |
57
|
|
|
|
|
|
|
|
58
|
14
|
|
|
|
|
237
|
foreach ( split /(?:\015{1,2}\012|\015|\012)/, shift ) |
59
|
|
|
|
|
|
|
{ |
60
|
61
|
|
|
|
|
80
|
$counter++; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# Skip comments and empty lines. |
63
|
|
|
|
|
|
|
|
64
|
61
|
100
|
|
|
|
167
|
next if /^\s*(?:\#|\;|$)/; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# Remove inline comments. |
67
|
|
|
|
|
|
|
|
68
|
51
|
|
|
|
|
104
|
s/\s\;\s.+$//g; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# Handle section headers. |
71
|
|
|
|
|
|
|
|
72
|
51
|
100
|
|
|
|
147
|
if ( /^\s*\[\s*(.+?)\s*\]\s*$/ ) |
73
|
|
|
|
|
|
|
{ |
74
|
|
|
|
|
|
|
# Create the sub-hash if it doesn't exist. |
75
|
|
|
|
|
|
|
# Without this sections without keys will not |
76
|
|
|
|
|
|
|
# appear at all in the completed struct. |
77
|
|
|
|
|
|
|
|
78
|
15
|
|
50
|
|
|
111
|
$self->{$ns = $1} ||= {}; |
79
|
|
|
|
|
|
|
|
80
|
15
|
|
|
|
|
30
|
next; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# Handle properties. |
84
|
|
|
|
|
|
|
|
85
|
36
|
50
|
|
|
|
175
|
if ( /^\s*([^=]+?)\s*=\s*(.*?)\s*$/ ) |
86
|
|
|
|
|
|
|
{ |
87
|
36
|
|
|
|
|
136
|
$self->{$ns}->{$1} = $2; |
88
|
|
|
|
|
|
|
|
89
|
36
|
|
|
|
|
67
|
next; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
0
|
return $self -> _error( "Syntax error at line $counter: '$_'" ); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
14
|
|
|
|
|
87
|
return $self; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# Save an object to a file. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub write |
101
|
|
|
|
|
|
|
{ |
102
|
3
|
|
|
3
|
1
|
6928
|
my($self) = shift; |
103
|
3
|
|
|
|
|
60
|
my($file, $encoding) = @_; |
104
|
|
|
|
|
|
|
|
105
|
3
|
50
|
33
|
|
|
36
|
return $self -> _error('No file name provided') if (! defined $file or ($file eq '') ); |
106
|
|
|
|
|
|
|
|
107
|
3
|
100
|
|
|
|
13
|
$encoding = $encoding ? ">:$encoding" : '>'; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# Write it to the file. |
110
|
|
|
|
|
|
|
|
111
|
3
|
|
|
|
|
11
|
my($string) = $self->write_string; |
112
|
|
|
|
|
|
|
|
113
|
3
|
50
|
|
|
|
20
|
return undef unless defined $string; |
114
|
|
|
|
|
|
|
|
115
|
3
|
50
|
|
|
|
267
|
open(my $CFG, $encoding, $file) or return $self->_error("Failed to open file '$file' for writing: $!"); |
116
|
3
|
|
|
|
|
67
|
print $CFG $string; |
117
|
3
|
|
|
|
|
153
|
close($CFG); |
118
|
|
|
|
|
|
|
|
119
|
3
|
|
|
|
|
28
|
return 1; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
} # End of write. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# Save an object to a string. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub write_string |
126
|
|
|
|
|
|
|
{ |
127
|
9
|
|
|
9
|
1
|
1459
|
my($self) = shift; |
128
|
9
|
|
|
|
|
45
|
my($contents) = ''; |
129
|
|
|
|
|
|
|
|
130
|
9
|
50
|
|
|
|
56
|
for my $section ( sort { (($b eq '_') <=> ($a eq '_')) || ($a cmp $b) } keys %$self ) |
|
9
|
|
|
|
|
42
|
|
131
|
|
|
|
|
|
|
{ |
132
|
|
|
|
|
|
|
# Check for several known-bad situations with the section |
133
|
|
|
|
|
|
|
# 1. Leading whitespace |
134
|
|
|
|
|
|
|
# 2. Trailing whitespace |
135
|
|
|
|
|
|
|
# 3. Newlines in section name. |
136
|
|
|
|
|
|
|
|
137
|
16
|
50
|
|
|
|
78
|
return $self->_error("Illegal whitespace in section name '$section'") if $section =~ /(?:^\s|\n|\s$)/s; |
138
|
|
|
|
|
|
|
|
139
|
16
|
|
|
|
|
33
|
my $block = $self->{$section}; |
140
|
16
|
100
|
|
|
|
39
|
$contents .= "\n" if length $contents; |
141
|
16
|
100
|
|
|
|
58
|
$contents .= "[$section]\n" unless $section eq '_'; |
142
|
|
|
|
|
|
|
|
143
|
16
|
|
|
|
|
47
|
for my $property ( sort keys %$block ) |
144
|
|
|
|
|
|
|
{ |
145
|
25
|
100
|
|
|
|
93
|
return $self->_error("Illegal newlines in property '$section.$property'") if $block->{$property} =~ /(?:\012|\015)/s; |
146
|
|
|
|
|
|
|
|
147
|
24
|
|
|
|
|
68
|
$contents .= "$property=$block->{$property}\n"; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
8
|
|
|
|
|
27
|
return $contents; |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
} # End of write_string. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
# Error handling. |
156
|
|
|
|
|
|
|
|
157
|
1
|
|
|
1
|
1
|
356
|
sub errstr { $Config::Tiny::errstr } |
158
|
1
|
|
|
1
|
|
3
|
sub _error { $Config::Tiny::errstr = $_[1]; undef } |
|
1
|
|
|
|
|
5
|
|
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
1; |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
__END__ |