line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package EOL; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# | PACKAGE | EOL |
4
|
|
|
|
|
|
|
# | AUTHOR | Todd Wylie |
5
|
|
|
|
|
|
|
# | EMAIL | perldev@monkeybytes.org |
6
|
|
|
|
|
|
|
# | ID | $Id: EOL.pm 238 2006-10-31 19:08:15Z twylie $ |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# --------------------------------------------------------------------------- |
9
|
|
|
|
|
|
|
# PURPOSE: |
10
|
|
|
|
|
|
|
# This module aids in the conversion of text file newline characters within |
11
|
|
|
|
|
|
|
# the context of other perl code. There are several choices a user can |
12
|
|
|
|
|
|
|
# specifiy; see POD for details on usage. |
13
|
|
|
|
|
|
|
# --------------------------------------------------------------------------- |
14
|
|
|
|
|
|
|
|
15
|
2
|
|
|
2
|
|
37012
|
use version; $VERSION = qv('0.0.2'); |
|
2
|
|
|
|
|
4739
|
|
|
2
|
|
|
|
|
13
|
|
16
|
2
|
|
|
2
|
|
154
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
60
|
|
17
|
2
|
|
|
2
|
|
10
|
use strict; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
54
|
|
18
|
2
|
|
|
2
|
|
9
|
use Carp; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
2274
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# END OF LINE DECLARATIONS |
21
|
|
|
|
|
|
|
my $CR = "\015"; # Apple II family, Mac OS thru version 9 |
22
|
|
|
|
|
|
|
my $CRLF = "\015\012"; # CP/M, MP/M, DOS, Microsoft Windows |
23
|
|
|
|
|
|
|
my $LF = "\012"; # Unix, Linux, Xenix, Mac OS X, BeOS, Amiga |
24
|
|
|
|
|
|
|
my $FF = "\014"; # printer form feed |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# -------------------------------------------------------------------------- |
27
|
|
|
|
|
|
|
# E O L N E W F I L E (subroutine) |
28
|
|
|
|
|
|
|
# ========================================================================== |
29
|
|
|
|
|
|
|
# USAGE : eol_new_file( in => $in_fh, out => $out_fh, eol => 'LF' ); |
30
|
|
|
|
|
|
|
# PURPOSE : Incoming file's newlines converted & saved to new file. |
31
|
|
|
|
|
|
|
# RETURNS : none |
32
|
|
|
|
|
|
|
# PARAMETERS : in => '' # file handle |
33
|
|
|
|
|
|
|
# : out => '' # file handle |
34
|
|
|
|
|
|
|
# : eol => '' # [CR; CRLF; LF] |
35
|
|
|
|
|
|
|
# THROWS : Croaks if required arguments missing/null or unknown. |
36
|
|
|
|
|
|
|
# COMMENTS : By default, all newlines will be converted to standard |
37
|
|
|
|
|
|
|
# : Unix-style line feeds (LF or octal \012). A user may change |
38
|
|
|
|
|
|
|
# : the end-of-file tag manually by supplying the "eol" argument. |
39
|
|
|
|
|
|
|
# : Acceptable eol tags are: CR; CRLF; LF. |
40
|
|
|
|
|
|
|
# SEE ALSO : n/a |
41
|
|
|
|
|
|
|
# -------------------------------------------------------------------------- |
42
|
|
|
|
|
|
|
sub eol_new_file { |
43
|
4
|
|
|
4
|
1
|
838
|
my %args = @_; |
44
|
4
|
|
|
|
|
9
|
my $EOL; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# Check arguments: |
47
|
4
|
|
|
|
|
13
|
foreach my $arg (keys %args) { |
48
|
12
|
50
|
100
|
|
|
90
|
if ( ($arg ne "in") && ($arg ne "out") && ($arg ne "eol") ) { |
|
|
|
66
|
|
|
|
|
49
|
0
|
|
|
|
|
0
|
croak "EOL reports: not legal arg \"$arg\"."; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# Check for text file input: |
54
|
4
|
50
|
|
|
|
344
|
if (!-T $args{in}) { |
55
|
0
|
|
|
|
|
0
|
croak "EOL reports: $args{in} is not a text file."; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# Conversion steps: |
59
|
4
|
50
|
|
|
|
132
|
open(IN, "$args{in}") or croak "EOL reports: cannot open $args{in}."; |
60
|
4
|
50
|
|
|
|
96
|
if (-f $args{out}) { unlink($args{out}) } |
|
4
|
|
|
|
|
94701
|
|
61
|
4
|
50
|
|
|
|
564
|
open(OUT, ">$args{out}") or croak "EOL reports: cannot open $args{in}."; |
62
|
4
|
50
|
|
|
|
23
|
if ($args{eol}) { |
63
|
4
|
50
|
|
|
|
40
|
($args{eol} eq "CR") ? ($EOL = $CR ) |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
64
|
|
|
|
|
|
|
: ($args{eol} eq "CRLF") ? ($EOL = $CRLF) |
65
|
|
|
|
|
|
|
: ($args{eol} eq "LF" ) ? ($EOL = $LF ) |
66
|
|
|
|
|
|
|
: croak "EOL reports: not legal EOL tag! Use only: CR; CRLF; LF."; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
else { |
69
|
0
|
|
|
|
|
0
|
$EOL = $LF; # Default is Unix line feed. |
70
|
|
|
|
|
|
|
} |
71
|
4
|
|
|
|
|
6473
|
while() { |
72
|
49672
|
|
|
|
|
238931
|
s/$CRLF$|$CR|$FF$|$LF$/$EOL/g; |
73
|
49672
|
|
|
|
|
213178
|
print OUT $_; |
74
|
|
|
|
|
|
|
} |
75
|
4
|
|
|
|
|
80
|
close(IN); |
76
|
4
|
|
|
|
|
200
|
close(OUT); |
77
|
|
|
|
|
|
|
|
78
|
4
|
|
|
|
|
89
|
return(1); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# -------------------------------------------------------------------------- |
82
|
|
|
|
|
|
|
# E O L S A M E F I L E (subroutine) |
83
|
|
|
|
|
|
|
# ========================================================================== |
84
|
|
|
|
|
|
|
# USAGE : eol_same_file( in => $in_fh, eol => 'LF', backup => '.bak' ); |
85
|
|
|
|
|
|
|
# PURPOSE : Incoming file's newlines converted & overwrites self. |
86
|
|
|
|
|
|
|
# RETURNS : none |
87
|
|
|
|
|
|
|
# PARAMETERS : in => '' # file handle |
88
|
|
|
|
|
|
|
# : eol => '' # [CR; CRLF; LF] |
89
|
|
|
|
|
|
|
# : backup => '' # scalar |
90
|
|
|
|
|
|
|
# THROWS : Croaks if required arguments missing/null or unknown. |
91
|
|
|
|
|
|
|
# COMMENTS : By default, all newlines will be converted to standard |
92
|
|
|
|
|
|
|
# : Unix-style line feeds (LF or octal \012). A user may change |
93
|
|
|
|
|
|
|
# : the end-of-file tag manually by supplying the "eol" argument. |
94
|
|
|
|
|
|
|
# : Acceptable eol tags are: CR; CRLF; LF. As already mentioned, |
95
|
|
|
|
|
|
|
# : this routine will clobber the in-file. Sending the "backup" |
96
|
|
|
|
|
|
|
# : argument with a file suffix will produce a one-time backup of |
97
|
|
|
|
|
|
|
# : the original file. !!! USE AT OWN RISK !!! |
98
|
|
|
|
|
|
|
# SEE ALSO : n/a |
99
|
|
|
|
|
|
|
# -------------------------------------------------------------------------- |
100
|
|
|
|
|
|
|
sub eol_same_file { |
101
|
1
|
|
|
1
|
1
|
22
|
my %args = @_; |
102
|
1
|
|
|
|
|
3
|
my $EOL; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# Check arguments: |
105
|
1
|
|
|
|
|
10
|
foreach my $arg (keys %args) { |
106
|
3
|
50
|
100
|
|
|
33
|
if ( ($arg ne "in") && ($arg ne "backup") && ($arg ne "eol") ) { |
|
|
|
66
|
|
|
|
|
107
|
0
|
|
|
|
|
0
|
croak "EOL reports: not legal arg \"$arg\"."; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
# Check for text file input: |
112
|
1
|
50
|
|
|
|
163
|
if (!-T $args{in}) { |
113
|
0
|
|
|
|
|
0
|
croak "EOL reports: $args{in} is not a text file."; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# Conversion steps: |
117
|
1
|
50
|
|
|
|
7
|
if ($args{eol}) { |
118
|
1
|
50
|
|
|
|
17
|
($args{eol} eq "CR") ? ($EOL = $CR ) |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
119
|
|
|
|
|
|
|
: ($args{eol} eq "CRLF") ? ($EOL = $CRLF) |
120
|
|
|
|
|
|
|
: ($args{eol} eq "LF" ) ? ($EOL = $LF ) |
121
|
|
|
|
|
|
|
: croak "EOL reports: Not legal EOL tag! Use only: CR; CRLF; LF."; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
else { |
124
|
0
|
|
|
|
|
0
|
$EOL = $LF; # Default is Unix line feed. |
125
|
|
|
|
|
|
|
} |
126
|
1
|
50
|
|
|
|
9
|
(exists $args{backup}) ? ($^I = $args{backup}) : ($^I = ""); |
127
|
1
|
|
|
|
|
983
|
while(<>) { |
128
|
16557
|
|
|
|
|
92693
|
s/$CRLF$|$CR|$FF$|$LF$/$EOL/g; |
129
|
16557
|
|
|
|
|
69709
|
print $_; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
1
|
|
|
|
|
19
|
return(1); |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
# -------------------------------------------------------------------------- |
136
|
|
|
|
|
|
|
# E O L R E T U R N A R R A Y (subroutine) |
137
|
|
|
|
|
|
|
# ========================================================================== |
138
|
|
|
|
|
|
|
# USAGE : eol_return_array( in => $in_fh, eol => 'LF' ); |
139
|
|
|
|
|
|
|
# PURPOSE : Incoming file's newlines converted & array of lines returned. |
140
|
|
|
|
|
|
|
# RETURNS : array-reference |
141
|
|
|
|
|
|
|
# PARAMETERS : in => '' # file handle |
142
|
|
|
|
|
|
|
# : eol => '' # [CR; CRLF; LF] |
143
|
|
|
|
|
|
|
# THROWS : Croaks if required arguments missing/null or unknown. |
144
|
|
|
|
|
|
|
# COMMENTS : By default, all newlines will be converted to standard |
145
|
|
|
|
|
|
|
# : Unix-style line feeds (LF or octal \012). A user may change |
146
|
|
|
|
|
|
|
# : the end-of-file tag manually by supplying the "eol" argument. |
147
|
|
|
|
|
|
|
# : Acceptable eol tags are: CR; CRLF; LF. An array reference to |
148
|
|
|
|
|
|
|
# : the converted file lines will be returned. Obviously, for |
149
|
|
|
|
|
|
|
# : larger files, this may be a considerable drain on memory. |
150
|
|
|
|
|
|
|
# SEE ALSO : n/a |
151
|
|
|
|
|
|
|
# -------------------------------------------------------------------------- |
152
|
|
|
|
|
|
|
sub eol_return_array { |
153
|
1
|
|
|
1
|
1
|
7
|
my %args = @_; |
154
|
1
|
|
|
|
|
3
|
my $EOL; |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
# Check arguments: |
157
|
1
|
|
|
|
|
5
|
foreach my $arg (keys %args) { |
158
|
2
|
50
|
66
|
|
|
16
|
if ( ($arg ne "in") && ($arg ne "eol") ) { |
159
|
0
|
|
|
|
|
0
|
croak "EOL reports: not legal arg \"$arg\"."; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
# Check for text file input: |
164
|
1
|
50
|
|
|
|
94
|
if (!-T $args{in}) { |
165
|
0
|
|
|
|
|
0
|
croak "EOL reports: $args{in} is not a text file."; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
# Conversion steps: |
169
|
1
|
|
|
|
|
4
|
my @lines; |
170
|
1
|
50
|
|
|
|
41
|
open(IN, "$args{in}") or croak "EOL reports: cannot open $args{in}"; |
171
|
1
|
50
|
|
|
|
5
|
if ($args{eol}) { |
172
|
1
|
50
|
|
|
|
9
|
($args{eol} eq "CR") ? ($EOL = $CR ) |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
173
|
|
|
|
|
|
|
: ($args{eol} eq "CRLF") ? ($EOL = $CRLF) |
174
|
|
|
|
|
|
|
: ($args{eol} eq "LF" ) ? ($EOL = $LF ) |
175
|
|
|
|
|
|
|
: croak "EOL reports: not legal EOL tag! Use only: CR; CRLF; LF.\n\n"; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
else { |
178
|
0
|
|
|
|
|
0
|
$EOL = $LF; # Default is Unix line feed. |
179
|
|
|
|
|
|
|
} |
180
|
1
|
|
|
|
|
14
|
while() { |
181
|
16557
|
|
|
|
|
21902
|
my $line = $_; |
182
|
16557
|
|
|
|
|
72122
|
$line =~ s/$CRLF$|$CR|$FF$|$LF$/$EOL/g; |
183
|
16557
|
|
|
|
|
87914
|
push (@lines, split (/($EOL)/, $line)); |
184
|
|
|
|
|
|
|
} |
185
|
1
|
|
|
|
|
46
|
close(IN); |
186
|
|
|
|
|
|
|
|
187
|
1
|
|
|
|
|
22
|
return(\@lines); |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
1; # End of module. |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
__END__ |