Source file: /~heha/hs/dos/dosmisc.zip/SRC/SPC2TAB.PAS

{$A-,G-,I-,S-,V-,M $800,0,0}
uses WinDos,Strings;
{Dieses Programm wandelt Spaces zu Tabulatoren}
{Neue Version mit variabler TabSize und nullterminierten Strings}
{h#s 08/02: Behalte Datei-Zeit im Überschreibmodus}
const
 OutTabSize: Integer=8;
 InTabSize: Integer=8;
 NoSubst: Integer=0;
 LiteralChar: Char='''';
 CommentChar: Char='{';

Procedure Usage;
 begin
  writeln('Spc2Tab'#9#9'[08/02] (haftmann#software ++FREEWARE++)');
  writeln('Substitute tabs for spaces (and vice versa) where appropriate'#13#10);
  writeln('Usage:');
  writeln(' spc2tab [switches] [file ...]');
  writeln(' replace <file> with - or pipe to stdout - a new-tabbed file.');
  writeln('  /Txx tabsize in+out, /TIxx tabsize in, /TOxx tabsize out (1=spaces only)');
  writeln('  /Lc set literal string delimiter, /P pipe to stdout (don''t overwrite)');
  writeln('  /Nxx no TAB subst for *single* spaces beyond column xx, defaults to 1');
  writeln('  /Cc set comment char to prevent *single* space interpretation before that');
  writeln(' Standards are both tab sizes =8, string delimiter="''", comment char="{"');
  writeln('Examples:');
  writeln(' spc2tab -C; x.asm'#9#9'insert tabs into spaced (hard to edit) ASM file');
  writeln(' spc2tab -L" -C/ -TI3 *.c* *.h'#9'Make most BC & VC sources readable');
  writeln(' spc2tab -TO1 x.txt'#9#9'remove all tabs, assuming tabsize = 8');
  writeln(' spc2tab -P x.pas >y.pas'#9'don''t overwrite source');
  halt(0);
 end;

procedure error(const msg:PChar);
 begin
  WriteLn('spc2tab: error: ',msg);
  halt(1);
 end;

function DivMul(a,b:Integer):Integer;
 begin
  DivMul:=a-(a mod b);	{auf durch 8 teilbare Spalten}
 end;

procedure AdvanceColumn(var col:Integer; c:Char; TabSize:Integer);
{col je nach Zeichen um 1 oder zum nächsten Tabulatorschritt vorrücken,
 die Spaltenzählung beginnt mit Spalte 0}
 begin
  if c=#9
  then col:=DivMul(col,TabSize)+TabSize
  else inc(col); {Nächste Spalte}
 end;

Procedure spc2tab(var input,output:Text);
var
 iin,iout: Integer;	{Indizes}
 inpos,outpos: Integer;	{Spalten}
 ins,outs: array[0..1023] of Char;	{Arbeitspuffer}

 c:char;
 literal:boolean;

 Procedure AppendChar(c:Char);
  begin
   outs[iout]:=c;
   Inc(iout);
   AdvanceColumn(outpos,c,OutTabSize);
  end;

 begin
  while not EOF(input) do begin
   readln(input,ins);
   literal:=false;
   iin:=0; inpos:=0;
   iout:=0; outpos:=0;

   while ins[iin]<>#0 do begin
    c:=ins[iin];
    if c=LiteralChar then literal:=not literal; {Texte werden nie getabt}
    if literal or (c<>' ') and (c<>#9) then begin
{Leerzeichen und eingehende Tabs zurückhalten, aber bei allen anderen
 Zeichen die Ausgabe von Tabs und Leerzeichen nachholen}
     if OutTabSize>1 then while outpos<divmul(inpos,OutTabSize) do
      if (outpos=inpos-1) and (outpos>=NoSubst) and (c<>CommentChar)
      then AppendChar(' ')
      else AppendChar(#9);
     while outpos<inpos do AppendChar(' ');
     AppendChar(c);
    end;
    Inc(iin);
    AdvanceColumn(inpos,c,InTabSize);
   end;
   outs[iout]:=#0;
   writeln(output,outs);
  end;
 end; {spc2tab}

var
 s,fname: array[0..127] of Char;
 sp: PChar;
 infile,outfile:Text;
 i,ts,ec: Integer;
 SR: TSearchRec;
 ftime: LongInt;
const
 overwrite:Boolean=true;

begin
 if GetArgCount=0 then begin
  spc2tab(input,output);
  exit;
 end;
 for i:=1 to GetArgCount do begin
  GetArgStr(s,i,sizeof(s));
  case s[0] of
   '/','-': case upcase(s[1]) of
    'H','?': Usage;
    'P': begin
     if overwrite
     then begin
      Assign(outfile,'');
      Rewrite(outfile);
      if ioresult<>0 then error('can''t create temp file');
      overwrite:=false;
     end else error('superfluous option P');
    end;
    'T': begin
     case upcase(s[2]) of
      'I': begin Val(s+3,ts,ec); InTabSize:=ts; end;
      'O': begin Val(s+3,ts,ec); OutTabSize:=ts; end;
      else begin Val(s+2,ts,ec); InTabSize:=ts; OutTabSize:=ts; end;
     end;
     if ts<1 then error('invalid tabsize');
    end;
    'N': begin
     Val(s+2,NoSubst,ec); Dec(NoSubst);
     if NoSubst<0 then error('invalid margin');
    end;
    'L': begin
     LiteralChar:=s[2];
    end;
    'C': begin
     CommentChar:=s[2];
    end;
    else error('invalid switch');
   end;
   else begin
    FileSplit(s,fname,nil,nil);
    sp:=StrEnd(fname);
    FindFirst(s,0,SR);
    if DosError<>0 then error('no match');
    repeat
     StrCopy(sp,SR.name);
     Assign(infile,fname);
     Reset(infile);
     if ioresult<>0 then error('can''t open input file');
     if overwrite then begin
      StrCopy(sp,'SPC2TAB$.$$$');	{Gleicher Pfad}
      Assign(outfile,fname);
      Rewrite(outfile);
      if ioresult<>0 then error('can''t create temp file');
     end;
{Dateien geöffnet zur Verarbeitung}
     spc2tab(infile,outfile);
     if overwrite then begin
      GetFTime(infile,ftime);
      Reset(outfile);
      SetFTime(outfile,ftime);
     end;
     close(infile);
     close(outfile);
{Nun ggf. überschreiben}
     if overwrite then begin
      Erase(infile);
      Rename(outfile,TTextRec(infile).name);
      if ioresult<>0 then error('can''t replace input file');
     end;
     FindNext(SR);
    until DosError<>0;
   end;
  end;
 end;
end.
Detected encoding: OEM (CP437)1
Wrong umlauts? - Assume file is ANSI (CP1252) encoded