| 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{at_letter} = 1 if $options{at_letter}; | ||
| 27 | $$self{notes} = 1 if $options{notes}; | ||
| 28 | $self->openFile($pathname); | ||
| 29 | $self->initialize; | ||
| 30 | return $self; } | ||
| 31 | |||
| 32 | sub openFile { | ||
| 33 | my ($self, $pathname) = @_; | ||
| 34 | my $IN; | ||
| 35 | if (!-r $pathname) { | ||
| 36 | Error('I/O', 'unreadable', $self, "File $pathname is not readable. Ignoring."); } | ||
| 37 | elsif ((!-z $pathname) && (-B $pathname)) { | ||
| 38 | Error('invalid', 'binary', $self, "Input file $pathname appears to be binary. Ignoring."); } | ||
| 39 | elsif (open($IN, '<:raw', $pathname)) { } | ||
| 40 | else { | ||
| 41 | Error('I/O', 'open', $self, "Can't open $pathname for reading", $!); } | ||
| 42 | $$self{IN} = $IN; | ||
| 43 | $$self{buffer} = []; | ||
| 44 | return; } | ||
| 45 | |||
| 46 | sub finish { | ||
| 47 | my ($self) = @_; | ||
| 48 | $self->SUPER::finish; | ||
| 49 | if ($$self{IN}) { | ||
| 50 | close(\*{ $$self{IN} }); $$self{IN} = undef; } | ||
| 51 | return; } | ||
| 52 | |||
| 53 | sub hasMoreInput { | ||
| 54 | my ($self) = @_; | ||
| 55 | # ($$self{colno} < $$self{nchars}) || $$self{IN}; } | ||
| 56 | return ($$self{colno} < $$self{nchars}) || scalar(@{ $$self{buffer} }) || $$self{IN}; } | ||
| 57 | |||
| 58 | sub getNextLine { | ||
| 59 | my ($self) = @_; | ||
| 60 | if (!scalar(@{ $$self{buffer} })) { | ||
| 61 | return unless $$self{IN}; | ||
| 62 | my $fh = \*{ $$self{IN} }; | ||
| 63 | my $line = <$fh>; | ||
| 64 | if (!defined $line) { | ||
| 65 | close($fh); $$self{IN} = undef; | ||
| 66 | return; } | ||
| 67 | else { | ||
| 68 | push(@{ $$self{buffer} }, LaTeXML::Core::Mouth::splitLines($line)); } } | ||
| 69 | |||
| 70 | my $line = shift(@{ $$self{buffer} }); | ||
| 71 | if (defined $line) { | ||
| 72 | if (my $encoding = $STATE->lookupValue('PERL_INPUT_ENCODING')) { | ||
| 73 | # Note that if chars in the input cannot be decoded, they are replaced by \x{FFFD} | ||
| 74 | # I _think_ that for TeX's behaviour we actually should turn such un-decodeable chars in to space(?). | ||
| 75 | $line = decode($encoding, $line, Encode::FB_DEFAULT); | ||
| 76 | if ($line =~ s/\x{FFFD}/ /g) { # Just remove the replacement chars, and warn (or Info?) | ||
| 77 | Info('misdefined', $encoding, $self, "input isn't valid under encoding $encoding"); } } } | ||
| 78 | else { | ||
| 79 | $line = ''; } | ||
| 80 | return $line; } | ||
| 81 | |||
| 82 | sub stringify { | ||
| 83 | my ($self) = @_; | ||
| 84 | return "Mouth[$$self{source}\@$$self{lineno}x$$self{colno}]"; } | ||
| 85 | |||
| 86 | #====================================================================== | ||
| 87 | 1; | ||
| 88 | |||
| 89 | __END__ |