################################################################################ # File: tabify.pl # # <> # # Purpose: Tabify the contents of source file(s). # # Created: 2nd January 2001 # Updated: 3rd July 2003 # 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_TABIFY_MAJOR 1 #_SYNSOFT_VER_PL_TABIFY_MINOR 3 #_SYNSOFT_VER_PL_TABIFY_REVISION 1 #_SYNSOFT_VER_PL_TABIFY_EDIT 12 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; $verbose = 0; if(scalar(@ARGV) > 0) { } else { print "Recognised flags: -v (verbose output); -tN (tabstop = N, defaults to 4)\n\n"; print "Enter the path (no wildcards):"; $_ = ; (@ARGV) = ($_); } my $length = 15; my $spaces = 0; my $tabs = 0; my $total; my $modified = 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(substr($arg, 0, 1) == "v") { $verbose = 1; } } elsif(!$file) { print qq(Invalid file specified " . $file . "\n); } elsif(! -T $file) { printf "Tabify: %-*s is not a text file\n", $length, $file; } else { my $file_spaces = 0; my $file_tabs = 0; # open the file, read all the lines in, and close open FILE, $file; @lines = ; close FILE; # tabify all the lines @tabified_lines = unexpand(@lines); if(list_equal(@tabified_lines, @lines)) { if($verbose) { printf "Tabify: %-*s nothing to do\n", $length, $file, $!; } } else { # open the file for writing, read all the lines in, and close if(! -W $file) { printf "Tabify: %-*s cannot be modified\n", $length, $file; } elsif(!open FILE, ">" . $file) { printf "Tabify: %-*s not processed: %s\n", $length, $file, $!; } else { foreach $line (@tabified_lines) { # Replace all '%' with %%, so that the printf works correctly $line =~ s/\%/\%\%/gs; printf FILE $line; } printf "Tabify: %-*s processed\n", $length, $file; ++$modified; } } } ++$total; } printf " Modified: %d of %d files\n", $modified, $total;