ScottAdams/File.pm

filter: Statementstmt 50.8% 30 / 59 Branchbran 40.0% 8 / 20 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 45.4% 5 / 11 Podpod 0.0% 0 / 8 totaltotal 41.3% 43 / 104 CC 13 SCAR 20.4
CC
13
Cov
46%
getstring
20.0
fatal
12.0
getint
6.0
CRAP 7.7
« ScottAdams/Action.pm ↑ Summary ScottAdams/Item.pm »
1 # $Id: File.pm,v 1.3 2006-11-03 20:59:19 mike Exp $
2  
3 # File.pm - a cleverer IO::File-alike that does pushback
4  
5 package Games::ScottAdams::File;
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # This module simply implements a slightly cleverer IO::File-alike
9 # that remembers the filename, maintains a notion of the current line
10 # number (useful for diagnostics) and can maintain an arbitrary number
11 # of pushback lines. It clearly has wider applicability outside of
12 # the Scott Adams module and should probably not be a
13 # Games::ScottAdams class.
14  
15 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 5 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
19 5 my $class = shift();
20 5 my($filename) = @_;
21  
22 5 ▶ my $f = new IO::File("<$filename")
Branch: 1 branch
BranchTrueFalseTotal
my $f = "IO::File"->new("<$filename") or return undef 5 0 5
23 or return undef;
24  
25 5 return bless {
26 f => $f,
27 filename => $filename,
28 linenumber => 0,
29 pushback => [],
30 buf => "", # for getint() and getstring() only
31 }, $class;
32 }
33  
34  
35 676 ▶sub getline {
Subroutine getline: called CC=5 SCAR=16.1
Pod undocumented
36 676 my $this = shift();
37 676 my($trim) = @_;
38  
39 676 my $line = pop @{ $this->{pushback} };
40 AGAIN:
41 847 ▶ if (!defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 666 181 847
42 666 $this->{linenumber}++;
43 666 $line = $this->{f}->getline();
44 666 ▶ return undef if !defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 6 660 666
45 }
46  
47 841 ▶ if ($trim) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trim) 841 0 841
48 841 $line =~ s/#.*//;
49 841 $line =~ s/\s+$//;
50 841 ▶ if ($line =~ /^$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^$/) 171 670 841
51 171 undef $line;
52 171 goto AGAIN;
53 }
54 }
55  
56 670 return $line;
57 }
58  
59  
60 181 ▶sub ungetline {
Subroutine ungetline: called CC=1 SCAR=0.0
Pod undocumented
61 181 my $this = shift();
62 181 my($line) = @_;
63  
64 181 push @{ $this->{pushback} }, $line;
65 }
66  
67  
68 # Calls to getint() and getstring() may be freely intermixed, but
69 # won't play nice if mixed with getline() and ungetline() calls.
70  
71 0 ▶sub getint {
Subroutine getint: not called CC=2 SCAR=17.9
Pod undocumented
72 0 my $this = shift();
73 0 $this->_refresh();
74 die "getint($this) on non-int buffer '" . $this->{buf} . "'"
75 0 ▶ if $this->{buf} !~ /^\d/;
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"buf"} =~ /^\d/ 0 0 0
76  
77 0 $this->{buf} =~ s/(\d+)//;
78 0 return $1;
79 }
80  
81 0 ▶sub getstring {
Subroutine getstring: not called CC=4 SCAR=30.0
Pod undocumented
82 0 my $this = shift();
83 0 $this->_refresh();
84  
85 $this->{buf} =~ s/^[""]//
86 0 ▶ or die "getstring($this) on non-string buffer '" . $this->{buf} . "'";
Branch: 1 branch
BranchTrueFalseTotal
$this->{"buf"} =~ s/^[""]// or die "getstring($this) on non-string buffer '" . $this->{"buf"} . q['] 0 0 0
87  
88 0 my $string = "";
89 0 ▶ while ($this->{buf} !~ /[""]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{"buf"} =~ /[""]/) 0 0 0
90 0 $string .= $this->{buf};
91 0 $this->{buf} = $this->getline();
92 }
93  
94 0 ▶ $this->{buf} =~ s/^(.*?)[""]//
Branch: 1 branch
BranchTrueFalseTotal
$this->{"buf"} =~ s/^(.*?)[""]// or die "can't happen" 0 0 0
95 or die "can't happen";
96  
97 0 $string .= $1;
98 0 $string =~ s/[``"]/"/g;
99 0 return $string;
100 }
101  
102 # PRIVATE to getint() and getstring()
103 0 ▶sub _refresh {
Subroutine _refresh: not called CC=2 SCAR=17.9
104 0 my $this = shift();
105 0 ▶ while ($this->{buf} =~ /^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"buf"} =~ /^\s*$/) 0 0 0
106 0 $this->{buf} = $this->getline();
107 }
108 0 $this->{buf} =~ s/^\s*//;
109 }
110  
111  
112 0 ▶sub warn {
Subroutine warn: not called CC=1 SCAR=6.9
Pod undocumented
113 0 my $this = shift();
114  
115 0 print STDERR $this->{filename}, ':', $this->{linenumber}, ': ',
116 'WARNING: ', @_, "\n";
117 }
118  
119  
120 0 ▶sub fatal {
Subroutine fatal: not called CC=3 SCAR=24.8
Pod undocumented
121 0 my $this = shift();
122  
123 0 ▶ my $filename = $this->{filename} || '[unknown]';
Condition0%0/2
$this->{'filename'} || '[unknown]'
l!l
00
MC/DC0%0/1
$this->{'filename'} || '[unknown]'
$this->{'filename'}
Truth table0%0/2
A
A= $this->{'filename'}
Aresult
11
00
124 0 ▶ my $linenumber = $this->{linenumber} || '[unknown]';
Condition0%0/2
$this->{'linenumber'} || '[unknown]'
l!l
00
MC/DC0%0/1
$this->{'linenumber'} || '[unknown]'
$this->{'linenumber'}
Truth table0%0/2
A
A= $this->{'linenumber'}
Aresult
11
00
125 0 print STDERR $filename, ':', $linenumber, ': ERROR: ', @_, "\n";
126 0 exit 1;
127 }
128  
129  
130 0 ▶sub close {
Subroutine close: not called CC=1 SCAR=6.9
Pod undocumented
131 0 my $this = shift();
132  
133 0 $this->{f}->close();
134 }
135  
136  
137 1 1;
« ScottAdams/Action.pm ↑ Summary ScottAdams/Item.pm »