blib/lib/Games/LMSolve/Input.pm

filter: Statementstmt 52.1% 85 / 163 Branchbran 44.1% 30 / 68 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 43.7% 7 / 16 Podpod 100% 3 / 3 totaltotal 50.0% 125 / 250 SCAR 53.0
CC
36
Cov
50%
input_board
71.1
__ANON__
20.0
__ANON__
12.0
CRAP 200.1
« blib/lib/Games/LMSolve/Minotaur.pm ↑ Summary blib/lib/Games/LMSolve/Base.pm »
1 package Games::LMSolve::Input;
2 10 $Games::LMSolve::Input::VERSION = '0.14.0';
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Games::LMSolve::Input::Scalar::FH;
7 10 $Games::LMSolve::Input::Scalar::FH::VERSION = '0.14.0';
8 sub TIEHANDLE
9 {
10 0 ▶ my $class = shift;
Subroutine TIEHANDLE: not called CC=1 SCAR=6.9
11 0 my $self = {};
12 0 my $buffer = shift;
13 0 $self->{'lines'} =
14 [ reverse( my @a = ( $buffer =~ /([^\n]*(?:\n|$))/sg ) ) ];
15 0 bless $self, $class;
16 0 return $self;
17 }
18  
19 sub READLINE
20 {
21 0 ▶ my $self = shift;
Subroutine READLINE: not called CC=1 SCAR=6.9
22 0 return pop( @{ $self->{'lines'} } );
23 }
24  
25 sub EOF
26 {
27 0 ▶ my $self = shift;
Subroutine EOF: not called CC=1 SCAR=6.9
28 0 return ( scalar( @{ $self->{'lines'} } ) == 0 );
29 }
30  
31 package Games::LMSolve::Input;
32  
33 10 ▶use English qw/ -no_match_vars /;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35  
36 sub new
37 {
38 1 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
39  
40 1 my $self = {};
41  
42 1 bless $self, $class;
43  
44 1 $self->_initialize(@_);
45  
46 1 return $self;
47 }
48  
49 sub _initialize
50 {
51 1 ▶ my $self = shift;
Subroutine _initialize: called CC=1 SCAR=0.0
52  
53 1 return 0;
54 }
55  
56  
57 sub input_board
58 {
59 1 ▶ my $self = shift;
Subroutine input_board: called CC=29 SCAR=42.6
Pod documented
60  
61 1 my $file_spec = shift;
62  
63 1 my $spec = shift;
64  
65 1 my $ret = {};
66  
67 1 my $file_ref;
68  
69 1 local (*I);
70  
71 1 my $filename_str;
72  
73 1 ▶ if ( ref($file_spec) eq "" )
Branch: 3 branches
BranchTrueFalseTotal
if (ref $file_spec eq '') { } 1 0 1
elsif (ref $file_spec eq 'GLOB') { } 0 0 0
elsif (ref $file_spec eq 'SCALAR') { } 0 0 0
74 {
75 1 my $filename = $file_spec;
76 1 ▶ open( my $in_fh, "<", $filename )
Branch: 1 branch
BranchTrueFalseTotal
open my $in_fh, "<", $filename or die qq[Failed to read "$filename" : $Games::LMSolve::Input::OS_ERROR] 0 1 1
77 || die "Failed to read \"$filename\" : $OS_ERROR";
78  
79 1 $file_ref = $in_fh;
80 1 ▶ $filename_str =
Branch: 1 branch
BranchTrueFalseTotal
$filename eq '-' ? : 0 1 1
81 ( $filename eq "-" )
82 ? "standard input"
83 : "\"$filename\"";
84 }
85 elsif ( ref($file_spec) eq "GLOB" )
86 {
87 0 $file_ref = $file_spec;
88 0 $filename_str = "FILEHANDLE";
89 }
90 elsif ( ref($file_spec) eq "SCALAR" )
91 {
92 0 tie( *I, "Games::LMSolve::Input::Scalar::FH", $$file_spec );
93 0 $file_ref = \*I;
94 0 $filename_str = "BUFFER";
95 }
96 else
97 {
98 0 die "Unknown file specification passed to input_board!";
99 }
100  
101 # Now we have the filehandle *$file_ref opened.
102  
103 1 my $line;
104 1 my $line_num = 0;
105  
106 my $read_line = sub {
107 11 ▶ if ( eof( *{$file_ref} ) )
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (eof do { $file_ref }) 1 10 11
108 {
109 1 return 0;
110 }
111 10 $line = readline( *{$file_ref} );
112 10 ++$line_num;
113 10 chomp($line);
114 10 return 1;
115 1 };
116  
117 my $gen_exception = sub {
118 0 ▶ my $text = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
119 0 close( *{$file_ref} );
120 0 die "$text on $filename_str at line $line_num!\n";
121 1 };
122  
123 1 my $xy_pair = "\\(\\s*(\\d+)\\s*\\,\\s*(\\d+)\\s*\\)";
124  
125 1 ▶ while ( $read_line->() )
Branch: 1 branch
BranchTrueFalseTotal
if (&$read_line()) 4 1 5
126 {
127 # Skip if this is an empty line
128 4 ▶ if ( $line =~ /^\s*$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*$/) 1 3 4
129 {
130 1 next;
131 }
132  
133 # Check if we have a "key =" construct
134 3 ▶ if ( $line =~ /^\s*(\w+)\s*=/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*(\w+)\s*=/) 3 0 3
135 {
136 3 my $key = lc($1);
137  
138 # Save the line number for safekeeping because a layout or
139 # other multi-line value can increase it.
140 3 my $key_line_num = $line_num;
141  
142 3 ▶ if ( !exists( $spec->{$key} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $spec->{$key}) 0 3 3
143 {
144 0 $gen_exception->("Unknown key \"$key\"");
145 }
146 3 ▶ if ( exists( $ret->{$key} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ret->{$key}) 0 3 3
147 {
148 0 $gen_exception->("Key \"$key\" was already inputted!\n");
149 }
150  
151 # Strip anything up to and including the equal sign
152 3 $line =~ s/^.*?=\s*//;
153 3 my $type = $spec->{$key}->{'type'};
154 3 my $value;
155 3 ▶ if ( $type eq "integer" )
Branch: 5 branches
BranchTrueFalseTotal
if ($type eq 'integer') { } 0 3 3
elsif ($type eq 'xy(integer)') { } 1 2 3
elsif ($type eq 'array(xy(integer))') { } 0 2 2
elsif ($type eq 'array(start_end(xy(integer)))') { } 1 1 2
elsif ($type eq 'layout') { } 1 0 1
156 {
157 0 ▶ if ( $line =~ /^(\d+)\s*$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(\d+)\s*$/) { } 0 0 0
158 {
159 0 $value = $1;
160 }
161 else
162 {
163 0 $gen_exception->(
164 "Key \"$key\" expects an integer as a value");
165 }
166 }
167 elsif ( $type eq "xy(integer)" )
168 {
169 1 ▶ if ( $line =~ /^\(\s*(\d+)\s*,\s*(\d+)\s*\)\s*$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\(\s*(\d+)\s*,\s*(\d+)\s*\)\s*$/) { } 1 0 1
170 {
171 1 $value = { 'x' => $1, 'y' => $2 };
172 }
173 else
174 {
175 0 $gen_exception->(
176 "Key \"$key\" expects an (x,y) integral pair as a value"
177 );
178 }
179 }
180 elsif ( $type eq "array(xy(integer))" )
181 {
182  
183 0 ▶ if ( $line =~ /^\[\s*$xy_pair(\s*\,\s*$xy_pair)*\s*\]\s*$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\[\s*$xy_pair(\s*\,\s*$xy_pair)*\s*\]\s*$/) { } 0 0 0
184 {
185 0 my @elements = ( $line =~ m/$xy_pair/g );
186 0 my @pairs;
187 0 ▶ while ( scalar(@elements) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @elements) 0 0 0
188 {
189 0 my $x = shift(@elements);
190 0 my $y = shift(@elements);
191 0 push @pairs, { 'x' => $x, 'y' => $y };
192 }
193 0 $value = \@pairs;
194 }
195 else
196 {
197 0 $gen_exception->(
198 "Key \"$key\" expects an array of integral (x,y) pairs as a value"
199 );
200 }
201 }
202 elsif ( $type eq "array(start_end(xy(integer)))" )
203 {
204 1 my $se_xy_pair = "\\(\\s*$xy_pair\\s*->\\s*$xy_pair\\s*\\)";
205 1 ▶ if ( $line =~
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\[\s*$se_xy_pair(\s*\,\s*$se_xy_pair)*\s*\]\s*$/) { } 1 0 1
206 /^\[\s*$se_xy_pair(\s*\,\s*$se_xy_pair)*\s*\]\s*$/ )
207 {
208 1 my @elements = ( $line =~ m/$se_xy_pair/g );
209 1 my @pairs;
210 1 ▶ while ( scalar(@elements) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @elements) 4 1 5
211 {
212 4 my ( $sx, $sy, $ex, $ey ) = @elements[ 0 .. 3 ];
213 4 @elements = @elements[ 4 .. $#elements ];
214 4 push @pairs,
215 {
216 'start' => { 'x' => $sx, 'y' => $sy },
217 'end' => { 'x' => $ex, 'y' => $ey }
218 };
219 }
220 1 $value = \@pairs;
221 }
222 else
223 {
224 0 $gen_exception->(
225 "Key \"$key\" expects an array of integral (sx,sy) -> (ex,ey) start/end x,y pairs as a value"
226 );
227 }
228 }
229 elsif ( $type eq "layout" )
230 {
231 1 ▶ if ( $line =~ /^<<\s*(\w+)\s*$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^<<\s*(\w+)\s*$/) { } 1 0 1
232 {
233 1 my $terminator = $1;
234 1 my @lines = ();
235 1 my $eof = 1;
236 1 ▶ while ( $read_line->() )
Branch: 1 branch
BranchTrueFalseTotal
if (&$read_line()) 6 0 6
237 {
238 6 ▶ if ( $line =~ /^\s*$terminator\s*$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*$terminator\s*$/) 1 5 6
239 {
240 1 $eof = 0;
241 1 last;
242 }
243 5 push @lines, $line;
244 }
245 1 ▶ if ($eof)
Branch: 1 branch
BranchTrueFalseTotal
if ($eof) 0 1 1
246 {
247 0 $gen_exception->(
248 "End of file reached before the terminator (\"$terminator\") for key \"$key\" was found"
249 );
250 }
251 1 $value = \@lines;
252 }
253 else
254 {
255 0 $gen_exception->(
256 "Key \"$key\" expects a layout specification (<<TERMINATOR_STRING)"
257 );
258 }
259 }
260 else
261 {
262 0 $gen_exception->("Unknown type \"$type\"!");
263 }
264  
265 3 $ret->{$key} = { 'value' => $value, 'line_num' => $key_line_num };
266 }
267 }
268  
269 1 close( *{$file_ref} );
270  
271 1 foreach my $key ( keys(%$spec) )
272 {
273 3 ▶ if ( $spec->{$key}->{'required'} )
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{$key}{"required"}) 3 0 3
274 {
275 3 ▶ if ( !exists( $ret->{$key} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ret->{$key}) 0 3 3
276 {
277 0 die
278 "The required key \"$key\" was not specified on $filename_str!\n";
279 }
280 }
281 }
282  
283 1 return $ret;
284 }
285  
286  
287 sub input_horiz_vert_walls_layout
288 {
289 0 ▶ my $self = shift;
Subroutine input_horiz_vert_walls_layout: not called CC=2 SCAR=17.9
Pod documented
290  
291 0 my $width = shift;
292 0 my $height = shift;
293 0 my $lines_ptr = shift;
294  
295 0 my ( @vert_walls, @horiz_walls );
296  
297 0 my $line;
298 0 my $line_num = 0;
299 0 my $y;
300  
301 my $get_next_line = sub {
302 0 ▶ my $ret = $lines_ptr->{'value'}->[$line_num];
Subroutine __ANON__: not called CC=1 SCAR=6.9
303 0 ++$line_num;
304  
305 0 return $ret;
306 0 };
307  
308 my $gen_exception = sub {
309 0 ▶ my $msg = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
310 die(
311 0 $msg . " at line " . ( $line_num + $lines_ptr->{'line_num'} + 1 ) );
312 0 };
313  
314 my $input_horiz_wall = sub {
315 0 ▶ $line = $get_next_line->();
Subroutine __ANON__: not called CC=4 SCAR=30.0
316 0 ▶ if ( length($line) != $width )
Branch: 1 branch
BranchTrueFalseTotal
if (length $line != $width) 0 0 0
317 {
318 0 $gen_exception->("Incorrect number of blocks");
319 }
320 0 ▶ if ( $line =~ /([^ _\-])/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /([^ _\-])/) 0 0 0
321 {
322 0 $gen_exception->("Incorrect character \'$1\'");
323 }
324 push @horiz_walls,
325 0 ▶ [ ( map { ( $_ eq "_" ) || ( $_ eq "-" ) } split( //, $line ) ) ];
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "_" or $_ eq "-" 0 0 0
326 0 };
327  
328 my $input_vert_wall = sub {
329 0 ▶ $line = $get_next_line->();
Subroutine __ANON__: not called CC=3 SCAR=24.8
330 0 ▶ if ( length($line) != $width + 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (length $line != $width + 1) 0 0 0
331 {
332 0 $gen_exception->("Incorrect number of blocks");
333 }
334 0 ▶ if ( $line =~ /([^ |])/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /([^ |])/) 0 0 0
335 {
336 0 $gen_exception->("Incorrect character \'$1\'");
337 }
338 0 push @vert_walls, [ ( map { $_ eq "|" } split( //, $line ) ) ];
339 0 };
340  
341 0 ▶ for ( $y = 0 ; $y < $height ; $y++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $height) 0 0 0
342 {
343 0 $input_horiz_wall->();
344 0 $input_vert_wall->();
345 }
346 0 $input_horiz_wall->();
347  
348 0 return ( \@horiz_walls, \@vert_walls );
349 }
350  
351  
352 10 1;
353  
354 __END__
« blib/lib/Games/LMSolve/Minotaur.pm ↑ Summary blib/lib/Games/LMSolve/Base.pm »