################################################################################ # File: trim_src.pl # # <> # # Purpose: Trim trailing spaces from lines, and trailing blank lines. # # Created: 2nd January 2001 # Updated: 28th March 2002 # Author: Synesis Software (Pty) Ltd (C) 2000. All rights reserved. # (www.synesis-group.com/software). # # Status: Public-domain # # Copyright: Synesis Software (Pty) Ltd provides the source code contained in # this file/document free-of-charge according to the following # conditions: # # (i) No warranty, expressed or implied, is given as to the # correctness or efficiency of the code. # (ii) No part of this code may be modified or copied, by whatever # means without including a copy of this notice in its entirety # (this means the all content between the <> and <<:END>> # tokens inclusive, including, and not limited to, purpose, date # of creation, date of last update, authorship, address, copyright # and status). # (iii) This code, in whole or part, remains the property of # Synesis Software (Pty) Ltd, whether incorporated as-is, or in # modified form. # # <<:END>> # # ############################################################################## #_SYNSOFT_VER_PL_TRIM_SRC_MAJOR 1 #_SYNSOFT_VER_PL_TRIM_SRC_MINOR 0 #_SYNSOFT_VER_PL_TRIM_SRC_REVISION 4 #_SYNSOFT_VER_PL_TRIM_SRC_EDIT 5 use integer; use Text::Tabs; sub trim_line { $_ = @_[0]; s/[\r\n]+$//; # strip cr-lf from end of line s/^\s*(.*?)\s*$/$1/; # trim spaces return $_; } sub list_equal { my $equal = 1; my $dim = scalar(@_) / 2; my $i; for($i = 0; $i < $dim; ++$i) { if($_[$i] ne $_[$i + $dim]) { $equal = 0; last; } } return $equal; } $tabstop = 4; if(scalar(@ARGV) > 0) { } else { print "Enter the path (no wildcards):"; $_ = ; (@ARGV) = ($_); } my $length = 15; my $trailing_spaces = 0; my $trailing_tabs = 0; $_ = trim_line($_); foreach (@ARGV) { if(length($_) > $length) { $length = length($_); } } foreach $file (@ARGV) { if(substr($file, 0, 1) eq "-") { $arg = substr($file, 1); # if(substr($arg, 0, 1) == "t") # { # $tabstop = scalar(substr($arg, 1)); # } } elsif(!$file) { print qq(Invalid file specified " . $file . "\n); } elsif(! -T $file) { printf "Trim Src: %-*s is not a text file\n", $length, $file; } elsif(! -W $file) { printf "Trim Src: %-*s cannot be modified\n", $length, $file; } else { my $file_trailing_spaces = 0; my $file_trailing_lines = 0; # open the file, read all the lines in, and close open FILE, $file; @lines = ; close FILE; # Remove lines my $num_lines = scalar(@lines); for(; $num_lines > 0; --$num_lines) { # my $trimmed = trim_line($lines[$num_lines - 1]); if(length($trimmed) == 0) { --$#lines; ++$file_trailing_lines; } else { last; } } # Trim the lines foreach $line (@lines) { if($line =~ s/\s+([\r\n]+)$/$1/) { ++$file_trailing_spaces; } } if( $file_trailing_spaces == 0 && $file_trailing_lines == 0) { printf "Trim Src: %-*s nothing to do\n", $length, $file, $!; } else { # open the file for writing, read all the lines in, and close if(!open FILE, ">" . $file) { printf "Trim Src: %-*s not processed: %s\n", $length, $file, $!; } else { foreach $line (@lines) { # Replace all '%' with %%, so that the printf works correctly $line =~ s/\%/\%\%/gs; printf FILE $line; } printf "Trim Src: %-*s processed: %8d trimmed lines, %8d removed lines\n", $length, $file, $file_trailing_spaces, $file_trailing_lines; } } } }