linux/scripts/cleanfile
<<
>>
Prefs
   1#!/usr/bin/perl -w
   2#
   3# Clean a text file -- or directory of text files -- of stealth whitespace.
   4# WARNING: this can be a highly destructive operation.  Use with caution.
   5#
   6
   7use bytes;
   8use File::Basename;
   9
  10# Default options
  11$max_width = 79;
  12
  13# Clean up space-tab sequences, either by removing spaces or
  14# replacing them with tabs.
  15sub clean_space_tabs($)
  16{
  17    no bytes;                   # Tab alignment depends on characters
  18
  19    my($li) = @_;
  20    my($lo) = '';
  21    my $pos = 0;
  22    my $nsp = 0;
  23    my($i, $c);
  24
  25    for ($i = 0; $i < length($li); $i++) {
  26        $c = substr($li, $i, 1);
  27        if ($c eq "\t") {
  28            my $npos = ($pos+$nsp+8) & ~7;
  29            my $ntab = ($npos >> 3) - ($pos >> 3);
  30            $lo .= "\t" x $ntab;
  31            $pos = $npos;
  32            $nsp = 0;
  33        } elsif ($c eq "\n" || $c eq "\r") {
  34            $lo .= " " x $nsp;
  35            $pos += $nsp;
  36            $nsp = 0;
  37            $lo .= $c;
  38            $pos = 0;
  39        } elsif ($c eq " ") {
  40            $nsp++;
  41        } else {
  42            $lo .= " " x $nsp;
  43            $pos += $nsp;
  44            $nsp = 0;
  45            $lo .= $c;
  46            $pos++;
  47        }
  48    }
  49    $lo .= " " x $nsp;
  50    return $lo;
  51s="line" name="L48">  48  52" id="L5   $lo .= $c;
5           $pos += $nsp5
5           $nsp = 0;
#
<5           $pos++;
  5  475       }
5   }
5   $lo .= " "5x $ns5pts/cleanfie#L39" id="L39" class="line" name="L39"6  506   return $lo;
6="line" name="L48">  486/a>  61id="L7" class="line" name="L7">  6pts/clea6file#L45" id="L45" clas6="lin6; x $nsp;++) {
6           $pos += $nsp6
6           $nsp = 0;
#
6
6       }
6   }
6   $lo .= " "6x $ns6quot;) {
7   return $lo;
7="line" name="L48">  487/a>  71file#L48" id="L48" class="line" name="L48"7pts/clea7file#L45" id="L45" clas7="lin72#L49" id="L49" class="line" name="L49"7  437           $pos += $nsp7
     447           $nsp = 0;
#
7
<7a href="scripts/cleanfi7ef="s76" id="L5   $lo .= $c;
     487   }
7   $lo .= " "7x $ns7" id="L10" class="line" name="L10"8  508   return $lo;
8="line" name="L48">  488/a>  81id="L7" class="line" name="L7">  8pts/clea8file#L45" id="L45" clas8="lin82#L49wh of (defd="d($apts/hift(@ARGV))id="L28" class="line" name="L28"8  438           $pos += $nsp8
8           $nsp = 0;
#
8
<8a href="scripts/cleanfi8ef="s86uot;) {
us8e bytes;
usee File::Basename;
8   $lo .= " "8x $ns89file#L48" id="L48" class="line" name="L48"9  509   return $lo;
9="line" name="L48">  489/a>  91file#L48" idpush(@ref="#L2ad="L27" class="line" name="L27"9pts/clea9file#L45" id="L45" clas9="lin92#L49" id="L49" class="line" name="L49"9  439           $pos += $nsp9
9           $nsp = 0;
#
9
<9a href="scripts/cleanfi9ef="s96uot;) {
pn/ct STDERRleanfil$ript: $fus9e bytes;
  9 8us9e File::Basename;

#  Default options
$0max_width = 79;

00  13#0 Clean up space-tab sequ03crif=ef="scripscri!open(FILE,le" id+cripe" id, $fiid="L28" class="line" name="L28"10  14#0 replacing them with tab04crif=4file#L48" idpn/ct STDERRleanfil$ript: Cannot openhref=: $f: $!s0ub clean_space_tabs($)
<05crif=5file#L48" idnext="L39" class="line" name="L39"10  16{0
 0   no bytes;            07crif=7id="L7" class="line" name="L7">  10  18
0 0   my($li) = @_;
>  10## Default options
>  11$mmax_width = 79;
>  12

>  13## Clean up space-tab sequuence11ef="scrip$is_binaryh id="L39" class="line" name="L39"1>  14## replacing them with tabbs.
<11" id="L25" class="line" name="L25"1>  15ssub clean_space_tabs($)
<  16{{
  17     no bytes;                 11"scripts/cleanfileis_binaryh i1="L39" class="line" name="L39"1>  18

  19     my($li) = @_;
  201    my($lo) = ''1;
  11$    my $pos = 0;
  12  12
<  231    my($i, $c);
  241
  251    for ($i = 0; $i <1 leng1h($li); $i="L48" class="line" name="L48"1>  16{        $c = substr($li,1 $i, 12 id="L7" class="line" name="L7">  1>  271        if ($c eq "1\t&qu1t;) {
  281            my $npos = (1$pos+12" id="L19" class="line" name="L19">>  291            my $ntab = (1$npos1>> 3$in_ment h id="L39" class="line" name="L39"1>  301            $lo .= "1;\t&q1ot; x $nta$out_ment h id="L39" class="line" name="L39"1>  11$            $pos = $npos1;
  12
            $nsp = 0;
   13#  Clean up space-tab sequescri1n" ||@blank"scrid="L27" class="line" name="L27"1>  341            $lo .= "1; &qu1t; x $nsp;@ $los scrid="L27" class="line" name="L27"1>  251            $pos += $nsp1;
  16{            $nsp = 0;
  1>  371            $lo .= $c;
<1a hre1="scripts/wh of ( defd="d($ $lonfilripFILEile#)sid="L28" class="line" name="L28"1>  381            $pos = 0;
  391        } elsif ($c eq &1quot;1") {
  401            $nsp++;
  11$        } else {
  12
            $lo .= "1; &qu14" id="L13" class="line" name="L13">>  431            $pos += $nsp1;
#  replacing them with tabs &qu1"scripts/cleanfilepush(@blank", dilned="L27" class="line" name="L27"1>  251            $lo .= $c;
<1a hre1="scripts/cleanfileblank_ment h+=ts/cleanfilned="L27" class="line" name="L27"1>  16{            $pos++;
  471        }
  381    }
  391    $lo .= " "1 x $n14gt;> 3) - ($popush(@ $los,|filned="L27" class="line" name="L27"1>  501    return $lo;
  511s="line" name="L48">  481 15ef="scripts/clean@blank"scrid="L27" class="line" name="L27"1ipts/clea1nfile#L45" id="L45" clas1s="li15; x $nsp;
15           $pos += $nsp15
#5           $nsp = 0;
#
1<  16{5           $pos++;
15       }
15   }
15   $lo .= " "15x $n159file#L48" id="L48" class="line" name="L48"16  5016   return $lo;
16="line" name="L48">  4816/a> 161id="L7" class="line" name="L7">  16pts/clea16file#L45" id="L45" clas16="li162file#L48# Anyhblank"sateanf endace.
nf L7"> are discardedL7" class="line" name="L7">  16  4316           $pos += $nsp16
  16  4416           $nsp = 0;
#
16 "sce#LngedL7" class="line" name="L7">  16  16{

16       }
16   }
6  4916   $lo .= " "16x $n16quot;) {
7  5017   return $lo;
17="line" name="L48">  4817/a> 17ef="scripts/cleandieleanfil$ript: Failed to 
runcate modified ref=: $f: $!17           $pos += $nsp17
17           $nsp = 0;
#
17
<17a href="scripts/cleanfi17ef="176" id="L5   $lo .= $c;

Tnf original LXR software by 
nf L5   $lo http://sourceforge.net/projec;
LXR communityid=",eratioexperiscrial vid=optiby L5   $lo mailto:lxd@ $lux.no">lxd@ $lux.noid=".
"file_cntentno bytesubfoo id">
lxd. $lux.no kindly hosted by L5   $lo http://www.redpill- $lpro.no">Redpill L$lpro ASid=",eprovider ce.L$lux consultfileandac="scriptshservi4" isince 1995.
"file_con/body_cn/htmlh_