| 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 | ▶ | use version; $VERSION = qv('0.0.2'); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | # END OF LINE DECLARATIONS | ||||||||||||||||||||||||||||||||||||||||||
| 21 | 2 | my $CR = "\015"; # Apple II family, Mac OS thru version 9 | |||||||||||||||||||||||||||||||||||||||||
| 22 | 2 | my $CRLF = "\015\012"; # CP/M, MP/M, DOS, Microsoft Windows | |||||||||||||||||||||||||||||||||||||||||
| 23 | 2 | my $LF = "\012"; # Unix, Linux, Xenix, Mac OS X, BeOS, Amiga | |||||||||||||||||||||||||||||||||||||||||
| 24 | 2 | 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 | 4 | ▶ | sub eol_new_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
eol_new_file: called CC=14 SCAR=28.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 4 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 4 | my $EOL; | |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | # Check arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 4 | foreach my $arg (keys %args) { | |||||||||||||||||||||||||||||||||||||||||
| 48 | 12 | ▶ | if ( ($arg ne "in") && ($arg ne "out") && ($arg ne "eol") ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$arg ne "in" and $arg ne "out" and $arg ne "eol"
$arg ne "in" and $arg ne "out"
MC/DC0%0/3
$arg ne "in" and $arg ne "out" and $arg ne "eol"
$arg ne "in"
$arg ne "out"
$arg ne "eol"
Truth table75%3/4
A and B and C
A= $arg ne "in"B= $arg ne "out"C= $arg ne "eol"
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | croak "EOL reports: not legal arg \"$arg\"."; | |||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | # Check for text file input: | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 4 | ▶ | if (!-T $args{in}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | croak "EOL reports: $args{in} is not a text file."; | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | # Conversion steps: | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 4 | ▶ | open(IN, "$args{in}") or croak "EOL reports: cannot open $args{in}."; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if (-f $args{out}) { unlink($args{out}) } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 4 | ▶ | open(OUT, ">$args{out}") or croak "EOL reports: cannot open $args{in}."; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 4 | ▶ | if ($args{eol}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | ($args{eol} eq "CR") ? ($EOL = $CR ) | ||||||||||||||||||||||||||||||||||||||||||
| 64 | : ($args{eol} eq "CRLF") ? ($EOL = $CRLF) | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 4 | ▶ | : ($args{eol} eq "LF" ) ? ($EOL = $LF ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | : croak "EOL reports: not legal EOL tag! Use only: CR; CRLF; LF."; | ||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | $EOL = $LF; # Default is Unix line feed. | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 4 | ▶ | while(<IN>) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 49672 | s/$CRLF$|$CR|$FF$|$LF$/$EOL/g; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 49672 | print OUT $_; | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 4 | close(IN); | |||||||||||||||||||||||||||||||||||||||||
| 76 | 4 | close(OUT); | |||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 4 | 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 | 1 | ▶ | sub eol_same_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
eol_same_file: called CC=12 SCAR=27.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 1 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | my $EOL; | |||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | # Check arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | foreach my $arg (keys %args) { | |||||||||||||||||||||||||||||||||||||||||
| 106 | 3 | ▶ | if ( ($arg ne "in") && ($arg ne "backup") && ($arg ne "eol") ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$arg ne "in" and $arg ne "backup" and $arg ne "eol"
$arg ne "in" and $arg ne "backup"
MC/DC0%0/3
$arg ne "in" and $arg ne "backup" and $arg ne "eol"
$arg ne "in"
$arg ne "backup"
$arg ne "eol"
Truth table75%3/4
A and B and C
A= $arg ne "in"B= $arg ne "backup"C= $arg ne "eol"
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | croak "EOL reports: not legal arg \"$arg\"."; | |||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | # Check for text file input: | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 1 | ▶ | if (!-T $args{in}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | croak "EOL reports: $args{in} is not a text file."; | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | # Conversion steps: | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 1 | ▶ | if ($args{eol}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | ($args{eol} eq "CR") ? ($EOL = $CR ) | ||||||||||||||||||||||||||||||||||||||||||
| 119 | : ($args{eol} eq "CRLF") ? ($EOL = $CRLF) | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 1 | ▶ | : ($args{eol} eq "LF" ) ? ($EOL = $LF ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | : croak "EOL reports: Not legal EOL tag! Use only: CR; CRLF; LF."; | ||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | $EOL = $LF; # Default is Unix line feed. | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 1 | ▶ | (exists $args{backup}) ? ($^I = $args{backup}) : ($^I = ""); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | ▶ | while(<>) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 16557 | s/$CRLF$|$CR|$FF$|$LF$/$EOL/g; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 16557 | print $_; | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 1 | 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 | 1 | ▶ | sub eol_return_array { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
eol_return_array: called CC=11 SCAR=26.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 1 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 154 | 1 | my $EOL; | |||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | # Check arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 1 | foreach my $arg (keys %args) { | |||||||||||||||||||||||||||||||||||||||||
| 158 | 2 | ▶ | if ( ($arg ne "in") && ($arg ne "eol") ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$arg ne "in" and $arg ne "eol"
MC/DC0%0/2
$arg ne "in" and $arg ne "eol"
$arg ne "in"
$arg ne "eol"
Truth table66%2/3
A and B
A= $arg ne "in"B= $arg ne "eol"
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | croak "EOL reports: not legal arg \"$arg\"."; | |||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | # Check for text file input: | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 1 | ▶ | if (!-T $args{in}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | croak "EOL reports: $args{in} is not a text file."; | |||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | # Conversion steps: | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 1 | my @lines; | |||||||||||||||||||||||||||||||||||||||||
| 170 | 1 | ▶ | open(IN, "$args{in}") or croak "EOL reports: cannot open $args{in}"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 1 | ▶ | if ($args{eol}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | ($args{eol} eq "CR") ? ($EOL = $CR ) | ||||||||||||||||||||||||||||||||||||||||||
| 173 | : ($args{eol} eq "CRLF") ? ($EOL = $CRLF) | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 1 | ▶ | : ($args{eol} eq "LF" ) ? ($EOL = $LF ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | : croak "EOL reports: not legal EOL tag! Use only: CR; CRLF; LF.\n\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | $EOL = $LF; # Default is Unix line feed. | |||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | 1 | ▶ | while(<IN>) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 16557 | my $line = $_; | |||||||||||||||||||||||||||||||||||||||||
| 182 | 16557 | $line =~ s/$CRLF$|$CR|$FF$|$LF$/$EOL/g; | |||||||||||||||||||||||||||||||||||||||||
| 183 | 16557 | push (@lines, split (/($EOL)/, $line)); | |||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | 1 | close(IN); | |||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | 1 | return(\@lines); | |||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | 2 | 1; # End of module. | |||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | __END__ | ||||||||||||||||||||||||||||||||||||||||||