| 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 | sub new { | ||||||||||||
| 19 | 1 | ▶ | my $class = shift(); | ||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||
| 20 | 1 | my($filename) = @_; | |||||||||||
| 21 | |||||||||||||
| 22 | 1 | ▶ | my $f = new IO::File("<$filename") | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 23 | or return undef; | ||||||||||||
| 24 | |||||||||||||
| 25 | 1 | 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 | sub getline { | ||||||||||||
| 36 | 1 | ▶ | my $this = shift(); | ||||||||||
|
Subroutine
getline: called CC=5 SCAR=16.7
Pod
undocumented
| |||||||||||||
| 37 | 1 | my($trim) = @_; | |||||||||||
| 38 | |||||||||||||
| 39 | 1 | my $line = pop @{ $this->{pushback} }; | |||||||||||
| 40 | 2 | ▶ | if (!defined $line) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 41 | AGAIN: | ||||||||||||
| 42 | 1 | $this->{linenumber}++; | |||||||||||
| 43 | 1 | $line = $this->{f}->getline(); | |||||||||||
| 44 | 1 | ▶ | return undef if !defined $line; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 45 | } | ||||||||||||
| 46 | |||||||||||||
| 47 | 1 | ▶ | if ($trim) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | 1 | $line =~ s/#.*//; | |||||||||||
| 49 | 1 | $line =~ s/\s+$//; | |||||||||||
| 50 | 1 | ▶ | goto AGAIN if $line =~ /^$/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | } | ||||||||||||
| 52 | |||||||||||||
| 53 | 0 | return $line; | |||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | |||||||||||||
| 57 | sub ungetline { | ||||||||||||
| 58 | 0 | ▶ | my $this = shift(); | ||||||||||
|
Subroutine
ungetline: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 59 | 0 | my($line) = @_; | |||||||||||
| 60 | |||||||||||||
| 61 | 0 | push @{ $this->{pushback} }, $line; | |||||||||||
| 62 | } | ||||||||||||
| 63 | |||||||||||||
| 64 | |||||||||||||
| 65 | # Calls to getint() and getstring() may be freely intermixed, but | ||||||||||||
| 66 | # won't play nice if mixed with getline() and ungetline() calls. | ||||||||||||
| 67 | |||||||||||||
| 68 | sub getint { | ||||||||||||
| 69 | 0 | ▶ | my $this = shift(); | ||||||||||
|
Subroutine
getint: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 70 | 0 | $this->_refresh(); | |||||||||||
| 71 | die "getint($this) on non-int buffer '" . $this->{buf} . "'" | ||||||||||||
| 72 | 0 | ▶ | if $this->{buf} !~ /^\d/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 73 | |||||||||||||
| 74 | 0 | $this->{buf} =~ s/(\d+)//; | |||||||||||
| 75 | 0 | return $1; | |||||||||||
| 76 | } | ||||||||||||
| 77 | |||||||||||||
| 78 | sub getstring { | ||||||||||||
| 79 | 0 | ▶ | my $this = shift(); | ||||||||||
|
Subroutine
getstring: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||
| 80 | 0 | $this->_refresh(); | |||||||||||
| 81 | |||||||||||||
| 82 | $this->{buf} =~ s/^[""]// | ||||||||||||
| 83 | 0 | ▶ | or die "getstring($this) on non-string buffer '" . $this->{buf} . "'"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 84 | |||||||||||||
| 85 | 0 | my $string = ""; | |||||||||||
| 86 | 0 | ▶ | while ($this->{buf} !~ /[""]/) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 87 | 0 | $string .= $this->{buf}; | |||||||||||
| 88 | 0 | $this->{buf} = $this->getline(); | |||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | 0 | ▶ | $this->{buf} =~ s/^(.*?)[""]// | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 92 | or die "can't happen"; | ||||||||||||
| 93 | |||||||||||||
| 94 | 0 | $string .= $1; | |||||||||||
| 95 | 0 | $string =~ s/[``"]/"/g; | |||||||||||
| 96 | 0 | return $string; | |||||||||||
| 97 | } | ||||||||||||
| 98 | |||||||||||||
| 99 | # PRIVATE to getint() and getstring() | ||||||||||||
| 100 | sub _refresh { | ||||||||||||
| 101 | 0 | ▶ | my $this = shift(); | ||||||||||
|
Subroutine
_refresh: not called CC=2 SCAR=17.9
| |||||||||||||
| 102 | 0 | ▶ | while ($this->{buf} =~ /^\s*$/) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 103 | 0 | $this->{buf} = $this->getline(); | |||||||||||
| 104 | } | ||||||||||||
| 105 | 0 | $this->{buf} =~ s/^\s*//; | |||||||||||
| 106 | } | ||||||||||||
| 107 | |||||||||||||
| 108 | |||||||||||||
| 109 | sub warn { | ||||||||||||
| 110 | 0 | ▶ | my $this = shift(); | ||||||||||
|
Subroutine
warn: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 111 | |||||||||||||
| 112 | 0 | print STDERR $this->{filename}, ':', $this->{linenumber}, ': ', | |||||||||||
| 113 | 'WARNING: ', @_, "\n"; | ||||||||||||
| 114 | } | ||||||||||||
| 115 | |||||||||||||
| 116 | |||||||||||||
| 117 | sub fatal { | ||||||||||||
| 118 | 0 | ▶ | my $this = shift(); | ||||||||||
|
Subroutine
fatal: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 119 | |||||||||||||
| 120 | 0 | ▶ | my $filename = $this->{filename} || '[unknown]'; | ||||||||||
|
Condition0%0/2
$this->{'filename'} || '[unknown]'
MC/DC0%0/1
$this->{'filename'} || '[unknown]'
$this->{'filename'}
Truth table0%0/2
A
A= $this->{'filename'}
| |||||||||||||
| 121 | 0 | ▶ | my $linenumber = $this->{linenumber} || '[unknown]'; | ||||||||||
|
Condition0%0/2
$this->{'linenumber'} || '[unknown]'
MC/DC0%0/1
$this->{'linenumber'} || '[unknown]'
$this->{'linenumber'}
Truth table0%0/2
A
A= $this->{'linenumber'}
| |||||||||||||
| 122 | 0 | print STDERR $filename, ':', $linenumber, ': ERROR: ', @_, "\n"; | |||||||||||
| 123 | 0 | exit 1; | |||||||||||
| 124 | } | ||||||||||||
| 125 | |||||||||||||
| 126 | |||||||||||||
| 127 | sub close { | ||||||||||||
| 128 | 0 | ▶ | my $this = shift(); | ||||||||||
|
Subroutine
close: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 129 | |||||||||||||
| 130 | 0 | $this->{f}->close(); | |||||||||||
| 131 | } | ||||||||||||
| 132 | |||||||||||||
| 133 | |||||||||||||
| 134 | 1 | 1; | |||||||||||