123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- program FixTab;
- uses Dos;
- const
- {Files}
- InputExt='';
- OutputExt='*';
- var
- {General}
- InFile,
- OutFile : string[80];
- ParaFile : word;
- {Specific}
- const
- TabSize : longint=8;
- DosEol : boolean=false;
- Verbose : boolean=false;
- {****************************************************************************
- Routines
- ****************************************************************************}
- const
- {$IFDEF LINUX}
- PathCh='/';
- {$ELSE}
- PathCh='\';
- {$ENDIF}
- Function SplitPath(Const HStr:String):String;
- var
- i : byte;
- begin
- i:=Length(Hstr);
- while (i>0) and (Hstr[i]<>PathCh) do
- dec(i);
- SplitPath:=Copy(Hstr,1,i);
- end;
- Function SplitFileName(Const HStr:String):String;
- var
- i : byte;
- begin
- i:=Length(Hstr);
- while (i>0) and (Hstr[i]<>PathCh) do
- dec(i);
- SplitFileName:=Copy(Hstr,i+1,255);
- end;
- Function SplitName(Const HStr:String):String;
- var
- i,j : byte;
- begin
- i:=Length(Hstr);
- j:=i;
- while (i>0) and (Hstr[i]<>PathCh) do
- dec(i);
- while (j>0) and (Hstr[j]<>'.') do
- dec(j);
- if j<=i then
- j:=255;
- SplitName:=Copy(Hstr,i+1,j-(i+1));
- end;
- Function SplitExtension(Const HStr:String):String;
- var
- j,i : byte;
- begin
- i:=Length(Hstr);
- j:=i;
- while (i>0) and (Hstr[i]<>PathCh) do
- dec(i);
- while (j>0) and (Hstr[j]<>'.') do
- dec(j);
- if j<=i then
- j:=254;
- SplitExtension:=Copy(Hstr,j+1,255);
- end;
- Function ChangeFileExt(Const HStr,ext:String):String;
- begin
- if (Ext<>'') and (SplitExtension(HStr)='') then
- ChangeFileExt:=Hstr+'.'+Ext
- else
- ChangeFileExt:=Hstr;
- end;
- Function ForceExtension(Const HStr,ext:String):String;
- var
- j : byte;
- begin
- j:=length(Hstr);
- while (j>0) and (Hstr[j]<>'.') do
- dec(j);
- if j=0 then
- j:=255;
- ForceExtension:=Copy(Hstr,1,j-1)+'.'+Ext;
- end;
- function UCase(Const Hstr:string):string;
- var
- i : byte;
- begin
- for i:=1to Length(Hstr) do
- UCase[i]:=Upcase(Hstr[i]);
- UCase[0]:=chr(Length(Hstr));
- end;
- Function ESpace(HStr:String;len:byte):String;
- begin
- while length(Hstr)<Len do
- begin
- inc(byte(Hstr[0]));
- Hstr[Length(Hstr)]:=' ';
- end;
- ESpace:=Hstr;
- end;
- {****************************************************************************
- Main Stuff
- ****************************************************************************}
- var
- Done : array[0..1023] of string[32];
- Total : word;
- Function FileDone(const fn:string):boolean;
- var
- i : word;
- begin
- i:=0;
- while (i<Total) and (Done[i]<>fn) do
- inc(i);
- if Done[i]=fn then
- FileDone:=true
- else
- begin
- Done[Total]:=fn;
- inc(Total);
- FileDone:=false;
- end;
- end;
- procedure Convert(fn,nfn:string);
- type
- inbuftype=array[0..31999] of char;
- outbuftype=array[0..63999] of char;
- var
- f,g : file;
- inbuf : ^inbuftype;
- outbuf : ^outbuftype;
- Curr,
- TabCol,
- col,
- i,last,
- innum,
- outnum : longint;
- procedure WriteBuf;
- begin
- if i>last then
- begin
- move(InBuf^[last],OutBuf^[OutNum],i-last);
- inc(OutNum,(i-last));
- end;
- Last:=i+1;
- end;
- begin
- {Create New FileName}
- if SplitExtension(nfn)='*' then
- nfn:=ChangeFileExt(SplitPath(nfn)+SplitName(nfn),SplitExtension(fn));
- if SplitName(nfn)='*' then
- begin
- if SplitPath(nfn)='' then
- nfn:=ChangeFileExt(SplitPath(fn)+SplitName(fn),SplitExtension(nfn))
- else
- nfn:=ChangeFileExt(SplitPath(nfn)+SplitName(fn),SplitExtension(nfn));
- end;
- {Done?}
- if FileDone(nfn) then
- exit;
- {Open Files}
- Write('Converting '+ESpace(fn,30)+' ');
- if fn=nfn then
- assign(g,ForceExtension(fn,'$T$'))
- else
- begin
- Write('-> '+ESpace(nfn,30)+' ');
- assign(g,nfn);
- end;
- new(inbuf);
- new(outbuf);
- assign(f,fn);
- {$push}{$I-}
- reset(f,1);
- {$pop}
- if ioresult<>0 then
- exit;
- {$push}{$I-}
- rewrite(g,1);
- {$pop}
- if ioresult<>0 then
- begin
- close(f);
- exit;
- end;
- Curr:=0;
- col:=1;
- last:=0;
- repeat
- blockread(f,InBuf^,sizeof(InBufType),innum);
- outnum:=0;
- if innum>0 then
- begin
- i:=0;
- while (i<innum) do
- begin
- case InBuf^[i] of
- #9 : begin
- WriteBuf;
- OutBuf^[OutNum]:=' ';
- inc(OutNum);
- inc(Col);
- TabCol:=(((Col-1) div TabSize)+1)*TabSize;
- while (Col<TabCol) do
- begin
- OutBuf^[OutNum]:=' ';
- inc(OutNum);
- inc(Col);
- end;
- end;
- #13 : begin
- WriteBuf;
- while (outnum>0) and (outbuf^[outnum-1] in [' ',#9]) do
- dec(outnum);
- end;
- #10 : begin
- WriteBuf;
- while (outnum>0) and (outbuf^[outnum-1] in [' ',#9]) do
- dec(outnum);
- if DosEol then
- begin
- OutBuf^[OutNum]:=#13;
- inc(OutNum);
- end;
- OutBuf^[OutNum]:=#10;
- inc(OutNum);
- col:=0;
- inc(Curr);
- if (curr and 31)=0 then
- Write(Curr:5,#8#8#8#8#8);
- end;
- else
- inc(col);
- end;
- inc(i);
- end;
- WriteBuf;
- last:=0;
- end;
- blockwrite(g,OutBuf^,outnum);
- until innum=0;
- WriteLn(Curr,' Lines');
- close(g);
- close(f);
- if fn=nfn then
- begin
- erase(f);
- rename(g,fn);
- end;
- dispose(outbuf);
- dispose(inbuf);
- end;
- {****************************************************************************
- General Stuff
- ****************************************************************************}
- procedure getpara;
- var
- ch : char;
- para : string[128];
- i,j : word;
- procedure helpscreen;
- begin
- writeln('Usage : '+SplitName(ParamStr(0))+' [Options] <InFile(s)>'#10);
- writeln('<Options> can be : -O<OutFile> Specify OutFile Mask');
- WriteLn(' -D Use MsDos #13#10 Eols');
- writeln(' -T<size> Set Size of Tabs');
- writeln(' -V be more verbose');
- writeln(' -? or -H This HelpScreen');
- halt(1);
- end;
- begin
- for i:=1to paramcount do
- begin
- para:=ucase(paramstr(i));
- if (para[1]='-') then
- begin
- ch:=para[2];
- delete(para,1,2);
- case ch of
- 'O' : OutFile:=ChangeFileExt(Para,OutputExt);
- 'D' : DosEol:=true;
- 'T' : Val(Para,TabSize,j);
- 'V' : verbose:=true;
- '?','H' : helpscreen;
- end;
- end
- else
- begin
- if ParaFile=0 then
- ParaFile:=i;
- end;
- end;
- if (ParaFile=0) then
- HelpScreen;
- if OutFile='' then
- OutFile:=ForceExtension('*',OutPutExt);
- end;
- var
- Dir : SearchRec;
- i : word;
- begin
- GetPara;
- {Main}
- if Verbose then
- begin
- Writeln('fixtab v1.01 (C) 1999-2002 Peter Vreman');
- Writeln('TabSize ',TabSize);
- if DosEol then
- WriteLn('Using MsDos Eols');
- end;
- for i:=ParaFile to ParamCount do
- begin
- InFile:=ChangeFileExt(ParamStr(i),InputExt);
- FindFirst(InFile,$20,Dir);
- while (DosError=0) do
- begin
- Convert(SplitPath(InFile)+Dir.Name,OutFile);
- FindNext(Dir);
- end;
- end;
- end.
|