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

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 133 CC 13 SCAR 33.0
CC
13
Cov
0%
getNextLine
72.0
new
12.0
openFile
12.0
CRAP 27.1
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 Fatal('I/O', 'unreadable', $self, "File $pathname is not readable."); }
36 elsif ((!-z $pathname) && (-B $pathname)) {
37 Fatal('invalid', 'binary', $self, "Input file $pathname appears to be binary."); }
38 open($IN, '<', $pathname)
39 || Fatal('I/O', 'open', $self, "Can't open $pathname for reading", $!);
40 $$self{IN} = $IN;
41 $$self{buffer} = [];
42 return; }
43  
44 sub finish {
45 my ($self) = @_;
46 $self->SUPER::finish;
47 if ($$self{IN}) {
48 close(\*{ $$self{IN} }); $$self{IN} = undef; }
49 return; }
50  
51 sub hasMoreInput {
52 my ($self) = @_;
53 # ($$self{colno} < $$self{nchars}) || $$self{IN}; }
54 return ($$self{colno} < $$self{nchars}) || scalar(@{ $$self{buffer} }) || $$self{IN}; }
55  
56 sub getNextLine {
57 my ($self) = @_;
58 if (!scalar(@{ $$self{buffer} })) {
59 return unless $$self{IN};
60 my $fh = \*{ $$self{IN} };
61 my $line = <$fh>;
62 if (!defined $line) {
63 close($fh); $$self{IN} = undef;
64 return; }
65 else {
66 push(@{ $$self{buffer} }, LaTeXML::Core::Mouth::splitLines($line)); } }
67  
68 my $line = shift(@{ $$self{buffer} });
69 if (defined $line) {
70 if (my $encoding = $STATE->lookupValue('PERL_INPUT_ENCODING')) {
71 # Note that if chars in the input cannot be decoded, they are replaced by \x{FFFD}
72 # I _think_ that for TeX's behaviour we actually should turn such un-decodeable chars in to space(?).
73 $line = decode($encoding, $line, Encode::FB_DEFAULT);
74 if ($line =~ s/\x{FFFD}/ /g) { # Just remove the replacement chars, and warn (or Info?)
75 Info('misdefined', $encoding, $self, "input isn't valid under encoding $encoding"); } } }
76 else {
77 $line = ''; }
78 $line .= "\r"; # put line ending back!
79  
80 if (!($$self{lineno} % 25)) {
81 NoteProgressDetailed("[#$$self{lineno}]"); }
82 return $line; }
83  
84 sub stringify {
85 my ($self) = @_;
86 return "Mouth[$$self{source}\@$$self{lineno}x$$self{colno}]"; }
87  
88 #======================================================================
89 1;
90  
91 __END__