program FixTabs; {v1.2} { actual filename - filename + dot + extension (11 chars) entire filename - drive:\dirname\filename (79 chars) or drive:\path\filename path - drive:\dirname or \dirname Good program to test for different tabs is my cw.pas program } uses crt,dos; const TAB_KEY = #9; function file_exists(filename : string) : boolean; var tmpfile : text; begin file_exists := false; assign(tmpfile,filename); {$I-} reset(tmpfile); {$I+} if IOResult = 0 then begin file_exists := true; close(tmpfile); end; end; {procedure type_filename(var textfile : text); var path : string; filename : string; ch : char; ok : boolean; begin textcolor(black); clrscr; repeat ok := false; path := 'c:\mydocu~1\pascal\'; gotoxy(36,9); textcolor(5); write('Fix Tabs'); gotoxy(33,10); textcolor(7); write('Press x to exit'); textcolor(6); gotoxy(10,13); write('Please type filename : '); textcolor(4); write('c:\mydocu~1\pascal\'); readln(filename); if filename = 'x' then begin textcolor(lightgray); clrscr; halt; end; if file_exists(path+filename) then begin assign(textfile,path+filename); reset(textfile); ok := true; end else begin textcolor(7); gotoxy(33,16); write('There''s no file'); textcolor(black); ch := readkey; gotoxy(34,13); clreol; gotoxy(33,16); clreol; end; until ok; end;} procedure convert_file(var textfile,savefile : text; tab_no : integer); var x,i : byte; tmpstr : string; tab,tmp : byte; count : byte; spaces : string; begin while not eof(textfile) do begin readln(textfile,tmpstr); x := pos(TAB_KEY,tmpstr); while x <> 0 do begin spaces := ''; tmp := x mod tab_no; tab := tab_no+1; if tmp = 0 then tab := 1 else for i := 1 to tmp do dec(tab); for i := 1 to tab do insert(' ',spaces,i); delete(tmpstr,x,1); insert(spaces,tmpstr,x); x := pos(TAB_KEY,tmpstr); end; writeln(savefile,tmpstr); end; close(textfile); close(savefile); end; {procedure enter_tabs(var tab_no : integer); var ok : boolean; tmpstr : string; error : integer; ch : char; begin ok := false; repeat clrscr; textcolor(6); gotoxy(20,13); write('Please type number of tabs : '); textcolor(4); readln(tmpstr); if tmpstr = '' then tmpstr := '3'; val(tmpstr,tab_no,error); if (tab_no < 1) or (tab_no > 255) or (error <> 0) then begin ok := false; textcolor(4); gotoxy(20,15); write('Has to be a number between 1 and 255'); ch := readkey; end else ok := true; until ok; clrscr; end;} procedure help_page; begin if paramstr(1) = '/?' then begin writeln('FixTabs v1.2'); writeln('By Fraser King'); writeln; writeln('Converts a text file''s tabs into spaces. The text file can'); writeln('then be printed in Windows.'); writeln; writeln('FixTabs name [tab number] [/?]'); writeln; writeln('name is the source file to convert tabs'); writeln('[tab distance] is the length of the tabs (default = 3)'); writeln('[/?] this help page'); halt; end; end; procedure read_source_file(var textfile : text); begin if file_exists(paramstr(1)) then {reads source file} begin assign(textfile,paramstr(1)); reset(textfile); end else begin writeln('There''s no file'); halt; end; end; procedure add_extension(var P : PathStr; D : DirStr; N : NameStr; E : ExtStr); begin P := paramstr(1); Fsplit(P,D,N,E); E := '.txt'; P := D+N+E; end; procedure read_tab_number(var tab_no : integer); var tmpnum : byte; error : integer; begin if paramstr(2) <> '' then {checks for tab number} begin val(paramstr(2),tmpnum,error); if tmpnum in [1..60] then tab_no := tmpnum else begin writeln('Tab distance should be between 1 and 60'); halt; end; end else tab_no := 3; end; procedure write_converted_file(var savefile : text; P : PathStr); begin assign(savefile,P); {$I-} rewrite(savefile); {$I+} if IOResult <> 0 then begin writeln('Error writing file'); halt; end else writeln('Converted to '+P); end; var textfile : text; savefile : text; tab_no : integer; P : PathStr; D : DirStr; N : NameStr; E : ExtStr; begin read_tab_number(tab_no); help_page; read_source_file(textfile); add_extension(P,D,N,E); write_converted_file(savefile,P); convert_file(textfile,savefile,tab_no); end.