lib/LaTeXML/Core/Mouth/file.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 127 CC 13 SCAR 30.7
CC
13
Cov
0%
getNextLine
56.0
openFile
20.0
new
12.0
CRAP 21.5
1 # /=====================================================================\ #
2 # | LaTeXML::Core::Mouth::file | #
3 # | Analog of TeX's Mouth: for reading from files | #
4 # |=====================================================================| #
5 # | Part of LaTeXML: | #
6 # | Public domain software, produced as part of work done by the | #
7 # | United States Government & not subject to copyright in the US. | #
8 # |---------------------------------------------------------------------| #
9 # | Bruce Miller <bruce.miller@nist.gov> #_# | #
10 # | http://dlmf.nist.gov/LaTeXML/ (o o) | #
11 # \=========================================================ooo==U==ooo=/ #
12 package LaTeXML::Core::Mouth::file;
13 use strict;
14 use warnings;
15 use LaTeXML::Global;
16 use LaTeXML::Common::Error;
17 use LaTeXML::Util::Pathname;
18 use Encode;
19 use base qw(LaTeXML::Core::Mouth);
20  
21 sub new {
22 my ($class, $pathname, %options) = @_;
23 my ($dir, $name, $ext) = pathname_split($pathname);
24 my $self = bless { source => $pathname, shortsource => "$name.$ext" }, $class;
25 $$self{fordefinitions} = 1 if $options{fordefinitions};
26 $$self{notes} = 1 if $options{notes};
27 $self->openFile($pathname);
28 $self->initialize;
29 return $self; }
30  
31 sub openFile {
32 my ($self, $pathname) = @_;
33 my $IN;
34 if (!-r $pathname) {
35 Error('I/O', 'unreadable', $self, "File $pathname is not readable. Ignoring."); }
36 elsif ((!-z $pathname) && (-B $pathname)) {
37 Error('invalid', 'binary', $self, "Input file $pathname appears to be binary. Ignoring."); }
38 elsif (open($IN, '<:raw', $pathname)) { }
39 else {
40 Error('I/O', 'open', $self, "Can't open $pathname for reading", $!); }
41 $$self{IN} = $IN;
42 $$self{buffer} = [];
43 return; }
44  
45 sub finish {
46 my ($self) = @_;
47 $self->SUPER::finish;
48 if ($$self{IN}) {
49 close(\*{ $$self{IN} }); $$self{IN} = undef; }
50 return; }
51  
52 sub hasMoreInput {
53 my ($self) = @_;
54 # ($$self{colno} < $$self{nchars}) || $$self{IN}; }
55 return ($$self{colno} < $$self{nchars}) || scalar(@{ $$self{buffer} }) || $$self{IN}; }
56  
57 sub getNextLine {
58 my ($self) = @_;
59 if (!scalar(@{ $$self{buffer} })) {
60 return unless $$self{IN};
61 my $fh = \*{ $$self{IN} };
62 my $line = <$fh>;
63 if (!defined $line) {
64 close($fh); $$self{IN} = undef;
65 return; }
66 else {
67 push(@{ $$self{buffer} }, LaTeXML::Core::Mouth::splitLines($line)); } }
68  
69 my $line = shift(@{ $$self{buffer} });
70 if (defined $line) {
71 if (my $encoding = $STATE->lookupValue('PERL_INPUT_ENCODING')) {
72 # Note that if chars in the input cannot be decoded, they are replaced by \x{FFFD}
73 # I _think_ that for TeX's behaviour we actually should turn such un-decodeable chars in to space(?).
74 $line = decode($encoding, $line, Encode::FB_DEFAULT);
75 if ($line =~ s/\x{FFFD}/ /g) { # Just remove the replacement chars, and warn (or Info?)
76 Info('misdefined', $encoding, $self, "input isn't valid under encoding $encoding"); } } }
77 else {
78 $line = ''; }
79 return $line; }
80  
81 sub stringify {
82 my ($self) = @_;
83 return "Mouth[$$self{source}\@$$self{lineno}x$$self{colno}]"; }
84  
85 #======================================================================
86 1;
87  
88 __END__