Hash.pm untested

filter: Statementstmt 0.0% 0 / 129 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 247 CC 27 SCAR 40.8
CC
27
Cov
0%
read_hash
110.0
new
90.0
make_defaults
12.0
CRAP 59.1
1 #####################################################################
2 # Hash.pm
3 # Copyright (c) 1999, 2000 by Markus Winand <mws@fatalmind.com>
4 #
5 # Class for reading config files into a hash
6 #
7 # $Id: Hash.pm,v 1.10 2000/06/25 17:08:56 mws Exp $
8 #
9  
10 package CONFIG::Hash;
11  
12 use strict;
13 use CONFIG::Plain;
14  
15 # the base class....
16 @CONFIG::Hash::ISA = qw(CONFIG::Plain);
17  
18  
19 #####################################################################
20 # new
21 #
22 # creates a new object from the class
23 #
24 # paramters: same as CONFIG::Plain->new
25 sub new {
26 my $proto = shift;
27 my $class = ref($proto) || $proto;
28 my $self;
29 my $removetrailingblanks;
30  
31 if (ref($_[0]) eq "HASH") {
32 $removetrailingblanks = $_[0]->{REMOVETRAILINGBLANKS};
33 $_[0]->{REMOVETRAILINGBLANKS} = "0";
34 } else {
35 if (! defined $_[1]) {
36 $_[1] = {};
37 }
38 $removetrailingblanks = $_[1]->{REMOVETRAILINGBLANKS};
39 $_[1]->{REMOVETRAILINGBLANKS} = "0";
40 }
41  
42 $self = $class->SUPER::new(@_);
43  
44 if (! defined $removetrailingblanks) {
45 $removetrailingblanks = "1";
46 }
47  
48 $self->{COMMON}->{CONFIG}->{REMOVETRAILINGBLANKS} = $removetrailingblanks;
49  
50 bless ($self, $class);
51  
52 if ($self->reparse) {
53 if (! defined $self->{COMMON}->{CONFIG}->{KEYREGEXP}) {
54 $self->{COMMON}->{CONFIG}->{KEYREGEXP} = "^(\\S+)";
55 }
56 if (! defined $self->{COMMON}->{CONFIG}->{HASHREGEXP}) {
57 $self->{COMMON}->{CONFIG}->{HASHREGEXP} = "\\s+(.*)\$";
58 }
59  
60 $self->read_hash();
61  
62 if (ref($self->{COMMON}->{CONFIG}->{DEFAULT}) eq "HASH") {
63 $self->make_defaults();
64 }
65
66 if (ref($self->{COMMON}->{CONFIG}->{REQUIRE}) eq "ARRAY") {
67 $self->check_require();
68 }
69 }
70  
71 $self->{COMMON}->{'_CODE_TYPE'} = "Hash";
72  
73 return $self;
74 }
75  
76 #####################################################################
77 # read_hash
78 #
79 # reads the file linewhise into a hash
80 #
81 # parameters: 1st -> object
82 sub read_hash {
83 my ($self) = @_;
84 my %HASH = ();
85 my %LINE = ();
86 my %FILE = ();
87 my ($line, $longline);
88 my ($key, $value, $hlp);
89  
90 # this variables stores the start point of a KEY.
91 # since a KEY/VALUE pair will no parsed until the next KEY or EOF
92 # is found, this variables are needed to store the point where
93 # the first KEY was found (for error reporting,...)
94 my ( $lineno, $file, $line_cursor);
95 my ($longlineno, $longfile, $longline_cursor);
96  
97 $longline = "";
98 $longlineno = 0;
99 $longfile = "unknown";
100 $longline_cursor =0;
101 while (defined ($line = $self->getline()) || (defined $longline && $longline ne "")) {
102 if ( ! defined $line || $line =~ m/$self->{COMMON}->{CONFIG}->{KEYREGEXP}/s ) {
103 $lineno = $self->getline_number;
104 $file = $self->getline_file;
105 $line_cursor = $self->getline_cursor;
106 if ($longline =~ m/$self->{COMMON}->{CONFIG}->{KEYREGEXP}$self->{COMMON}->{CONFIG}->{HASHREGEXP}/s) {
107 $key = $1;
108 if (defined $self->{COMMON}->{CONFIG}->{CASEINSENSITIVE}) {
109 $key =~ tr /A-Z/a-z/;
110 }
111 $value = $2;
112 if (defined $HASH{$key}) {
113 if ($self->{COMMON}->{CONFIG}->{ALLOWREDEFINE}){
114 $HASH{$key} = $value;
115 } else {
116 # generate error
117 $self->setline_error("Key <$key> already defined", $longline_cursor);
118 }
119 } else {
120 $HASH{$key} = $value;
121 }
122
123 # get complete include path
124 push @{$LINE{$key}}, $longlineno;
125 push @{$FILE{$key}}, $longfile;
126 while (defined ($hlp = $self->getline_number)) {
127 push @{$LINE{$key}}, $hlp;
128 push @{$FILE{$key}},$self->getline_file;
129 }
130 }
131 if ($self->{COMMON}->{CONFIG}->{REMOVETRAILINGBLANKS} &&
132 defined $line) {
133 $line =~ s/^\s*//;
134 $line =~ s/\s*\n/\n/;
135 }
136 if (defined $self->{COMMON}->{CONFIG}->{SUBSTITUTENEWLINE} &&
137 defined $line) {
138 $line =~ s/\n/$self->{COMMON}->{CONFIG}->{SUBSTITUTENEWLINE}/;
139 }
140 $longline = $line;
141 $longlineno = $lineno;
142 $longfile = $file;
143 $longline_cursor = $line_cursor;
144 } else {
145 if ($self->{COMMON}->{CONFIG}->{REMOVETRAILINGBLANKS} &&
146 defined $line) {
147 $line =~ s/^\s*//;
148 $line =~ s/\s*\n/\n/;
149 }
150 if (defined $self->{COMMON}->{CONFIG}->{SUBSTITUTENEWLINE} &&
151 defined $line) {
152 $line =~ s/\n/$self->{COMMON}->{CONFIG}->{SUBSTITUTENEWLINE}/;
153 }
154 # multi line, or error
155 $longline .= $line;
156 }
157 }
158  
159 $self->{COMMON}->{"Hash.pm"}->{HASH} = \%HASH;
160 $self->{COMMON}->{"Hash.pm"}->{LINE} = \%LINE;
161 $self->{COMMON}->{"Hash.pm"}->{FILE} = \%FILE;
162 }
163  
164 #####################################################################
165 # make_defaults
166 #
167 # inserts the DEFUALT values into the local stored data
168 #
169 # parameters: 1st -> object
170 sub make_defaults {
171 my ($self) = @_;
172 my $key;
173  
174 foreach $key (keys (%{$self->{COMMON}->{CONFIG}->{DEFAULT}})) {
175 if (defined $self->{COMMON}->{CONFIG}->{CASEINSENSITIVE}) {
176 $key =~ tr /A-Z/a-z/;
177 }
178 if (! defined $self->{COMMON}->{"Hash.pm"}->{HASH}->{$key}) {
179 $self->{COMMON}->{"Hash.pm"}->{HASH}->{$key} =
180 $self->{COMMON}->{CONFIG}->{DEFAULT}->{$key};
181 $self->{COMMON}->{"Hash.pm"}->{FILE}->{$key} =
182 ['DEFAULT'];
183 $self->{COMMON}->{"Hash.pm"}->{LINE}->{$key} = [0];
184 }
185 }
186  
187 }
188  
189 #####################################################################
190 # check_require
191 #
192 # checks for the required keys
193 #
194 # parameters: 1st -> object
195 sub check_require {
196 my ($self) = @_;
197 my $key;
198  
199 foreach $key (@{$self->{COMMON}->{CONFIG}->{REQUIRE}}) {
200 if (defined $self->{COMMON}->{CONFIG}->{CASEINSENSITIVE}) {
201 $key =~ tr /A-Z/a-z/;
202 }
203 if (! defined $self->{COMMON}->{"Hash.pm"}->{HASH}->{$key}) {
204 $self->setglobal_error("Required key <$key> not found.");
205 }
206 }
207 }
208 #####################################################################
209 # get
210 #
211 # returns the value to a given key, or a __reference__ to the hash
212 #
213 # parameters: 1st -> object
214 # 2nd -> (optional) key
215 sub get {
216 my ($self, $key) = @_;
217  
218 if (defined $key &&
219 $key ne "") {
220 return $self->{COMMON}->{"Hash.pm"}->{HASH}->{$key};
221 } else {
222 return $self->{COMMON}->{"Hash.pm"}->{HASH};
223 }
224 }
225  
226  
227 #####################################################################
228 # get_line
229 #
230 # returns the linenumber where the key was found.
231 # Call often to get include path
232 #
233 # parameters: 1st -> object
234 # 2nd -> key
235 sub get_line {
236 my ($self, $key) = @_;
237  
238 if (! defined $key) {
239 $self->{CURSORS}->{get_line_LASTKEY} = "";
240 return undef;
241 }
242  
243 if (defined $self->{CURSORS}->{get_line_LASTKEY} &&
244 $self->{CURSORS}->{get_line_LASTKEY} ne $key) {
245 $self->{CURSORS}->{get_line} = 0;
246 $self->{CURSORS}->{get_line_LASTKEY} = $key;
247 }
248  
249 return $self->{COMMON}->{"Hash.pm"}->{LINE}->{$key}->
250 [$self->{CURSORS}->{get_line}++];
251 }
252  
253 #####################################################################
254 # get_file
255 #
256 # returns the filename where the key was found.
257 # Call often to get include path
258 #
259 # parameters: 1st -> object
260 # 2nd -> key
261 sub get_file ($$) {
262 my ($self, $key) = @_;
263  
264 if (! defined $key) {
265 $self->{CURSORS}->{get_file_LASTKEY} = "";
266 return undef;
267 }
268  
269 if ($self->{CURSORS}->{get_file_LASTKEY} ne $key) {
270 $self->{CURSORS}->{get_file} = 0;
271 $self->{CURSORS}->{get_file_LASTKEY} = $key;
272 }
273  
274 return $self->{COMMON}->{"Hash.pm"}->{FILE}->{$key}->
275 [$self->{CURSORS}->{get_file}++];
276 }
277 1;
278  
279  
280 __END__