]> git.donarmstrong.com Git - perltidy.git/blob - lib/Perl/Tidy/LineSource.pm
New upstream version 20181120
[perltidy.git] / lib / Perl / Tidy / LineSource.pm
1 #####################################################################
2 #
3 # the Perl::Tidy::LineSource class supplies an object with a 'get_line()' method
4 # which returns the next line to be parsed
5 #
6 #####################################################################
7
8 package Perl::Tidy::LineSource;
9 use strict;
10 use warnings;
11 our $VERSION = '20181120';
12
13 sub new {
14
15     my ( $class, $input_file, $rOpts, $rpending_logfile_message ) = @_;
16
17     my $input_line_ending;
18     if ( $rOpts->{'preserve-line-endings'} ) {
19         $input_line_ending = Perl::Tidy::find_input_line_ending($input_file);
20     }
21
22     ( my $fh, $input_file ) = Perl::Tidy::streamhandle( $input_file, 'r' );
23     return unless $fh;
24
25     # in order to check output syntax when standard output is used,
26     # or when it is an object, we have to make a copy of the file
27     if ( ( $input_file eq '-' || ref $input_file ) && $rOpts->{'check-syntax'} )
28     {
29
30         # Turning off syntax check when input output is used.
31         # The reason is that temporary files cause problems on
32         # on many systems.
33         $rOpts->{'check-syntax'} = 0;
34
35         ${$rpending_logfile_message} .= <<EOM;
36 Note: --syntax check will be skipped because standard input is used
37 EOM
38
39     }
40
41     return bless {
42         _fh                => $fh,
43         _filename          => $input_file,
44         _input_line_ending => $input_line_ending,
45         _rinput_buffer     => [],
46         _started           => 0,
47     }, $class;
48 }
49
50 sub close_input_file {
51     my $self = shift;
52
53     # Only close physical files, not STDIN and other objects
54     my $filename = $self->{_filename};
55     if ( $filename ne '-' && !ref $filename ) {
56         eval { $self->{_fh}->close() };
57     }
58     return;
59 }
60
61 sub get_line {
62     my $self          = shift;
63     my $line          = undef;
64     my $fh            = $self->{_fh};
65     my $rinput_buffer = $self->{_rinput_buffer};
66
67     if ( scalar( @{$rinput_buffer} ) ) {
68         $line = shift @{$rinput_buffer};
69     }
70     else {
71         $line = $fh->getline();
72
73         # patch to read raw mac files under unix, dos
74         # see if the first line has embedded \r's
75         if ( $line && !$self->{_started} ) {
76             if ( $line =~ /[\015][^\015\012]/ ) {
77
78                 # found one -- break the line up and store in a buffer
79                 @{$rinput_buffer} = map { $_ . "\n" } split /\015/, $line;
80                 my $count = @{$rinput_buffer};
81                 $line = shift @{$rinput_buffer};
82             }
83             $self->{_started}++;
84         }
85     }
86     return $line;
87 }
88 1;
89