Commit | Line | Data |
---|---|---|
9e831c8e JK |
1 | package ExtUtils::ParseXS::CountLines; |
2 | use strict; | |
3 | our $SECTION_END_MARKER; | |
4 | ||
5 | sub TIEHANDLE { | |
6 | my ($class, $cfile, $fh) = @_; | |
7 | $cfile =~ s/\\/\\\\/g; | |
8 | $SECTION_END_MARKER = qq{#line --- "$cfile"}; | |
9 | ||
10 | return bless { | |
11 | buffer => '', | |
12 | fh => $fh, | |
13 | line_no => 1, | |
14 | }, $class; | |
15 | } | |
16 | ||
17 | sub PRINT { | |
18 | my $self = shift; | |
19 | for (@_) { | |
20 | $self->{buffer} .= $_; | |
21 | while ($self->{buffer} =~ s/^([^\n]*\n)//) { | |
22 | my $line = $1; | |
23 | ++$self->{line_no}; | |
24 | $line =~ s|^\#line\s+---(?=\s)|#line $self->{line_no}|; | |
25 | print {$self->{fh}} $line; | |
26 | } | |
27 | } | |
28 | } | |
29 | ||
30 | sub PRINTF { | |
31 | my $self = shift; | |
32 | my $fmt = shift; | |
33 | $self->PRINT(sprintf($fmt, @_)); | |
34 | } | |
35 | ||
36 | sub DESTROY { | |
37 | # Not necessary if we're careful to end with a "\n" | |
38 | my $self = shift; | |
39 | print {$self->{fh}} $self->{buffer}; | |
40 | } | |
41 | ||
42 | sub UNTIE { | |
43 | # This sub does nothing, but is neccessary for references to be released. | |
44 | } | |
45 | ||
46 | sub end_marker { | |
47 | return $SECTION_END_MARKER; | |
48 | } | |
49 | ||
50 | 1; |