home *** CD-ROM | disk | FTP | other *** search
- {REMBS.BAS, converted to Turbo Pascal
- David Kirschbaum
- Toad Hall
- kirsch@braggvax.ARPA
-
- This program removes backspaces - chr$(8) - from text files and
- restores text to its corrected state for viewing and printing.
-
- rembs.bas - D.S. Duani 3/87
- Microsoft QuickBASIC 2.0
- }
- {$K-} {no stack checking}
- {$V-} {no string parm checking}
-
- TYPE
- Str255 = STRING[255];
-
- CONST
- BS = #$08; {backspace char}
-
- VAR
- InFile,OutFile : TEXT;
- filesopen, {nr files opened}
- x,bscnt : INTEGER;
- S,
- WorkStr : Str255;
- wlen : Byte Absolute WorkStr; {sit on length byte}
-
-
- PROCEDURE Abort(Msg : Str255);
- BEGIN
- IF Msg <> '' THEN Writeln('REMBS ', Msg, '! Aborting.');
- IF filesopen <> 0 THEN BEGIN {we have file(s) open}
- {$I-}
- Close(InFile);
- IF filesopen > 1 {if we opened our output file}
- THEN Close(OutFile);
- IF IOResult <> 0 THEN; {we don't care}
- {$I+}
- END;
- Halt;
- END; {of Abort}
-
-
- PROCEDURE Open_Files;
- VAR
- err : INTEGER;
- InName,OutName : STRING[128];
- Ch : CHAR;
- BEGIN
- IF ParamCount <> 2 THEN BEGIN {we demand two filenames
- as cmdline parameters}
- Writeln('REMBS - Remove backspace characters from a text file.');
- Writeln('(Useful to edit a log of a BBS or remote editor session.)');
- Writeln('Correct syntax is: REMBS oldfile newfile');
- Halt;
- END;
-
- filesopen := 0; {no files to close}
- InName := ParamStr(1); {input filename}
- OutName := ParamStr(2); {output filename}
-
- IF InName = OutName {dummy's asking for trouble!}
- THEN Abort('Output ' + OutName + ' can''t be Input ' + InName);
-
- Assign(InFile,InName); {open input file}
- filesopen := 1; {just 1 to close}
- {$I-} Reset(InFile); {$I+}
- IF IOResult <> 0 {error, probably doesn't exist}
- THEN Abort(InName + ' Input file error');
-
- Assign(OutFile,OutName);
- {$I-}
- Reset(OutFile); {see if it exists}
- err := IOResult; {remember that test result}
- Close(OutFile); {close in any case}
- IF IOResult <> 0 THEN; {we don't care}
- {$I+}
- IF err = 0 THEN BEGIN {oh-oh, it does exist!}
- Write(OutName, ' exists! Overwrite? [Y/N] Y',BS);
- Repeat Until Keypressed; Read(Kbd,Ch); Writeln(Ch);
- IF (UpCase(Ch) = 'N') {user abort}
- THEN Abort('User Abort');
-
- END;
- filesopen := 2; {now 2 files to close}
- {$I-} Rewrite(OutFile); {$I+} {create or set file ptr to start}
- IF IOResult <> 0 {create error}
- THEN Abort(OutName + ' create error');
-
- {Ok, both input and output files are open and ready to go.}
- END; {of Open_Files}
-
- (*
- while not eof(1)
- line input #1,a$
- cnt=1
- b$=string$(len(a$),32)
- for x=1 to len(a$)
- if mid$(a$,x,1)=chr$(8) then
- cnt=cnt-1:if cnt=0 then cnt=1
- else
- mid$(b$,cnt,1)=mid$(a$,x,1)
- cnt=cnt+1
- end if
- next
- print #2,left$(b$,cnt)
- wend
- *)
-
- PROCEDURE Remove_BS;
- BEGIN
- bscnt := 0; {initialize backspace counter}
- WHILE NOT EOF(InFile) DO BEGIN
- {$I-}
- Readln(InFile,S);
- IF IOResult <> 0
- THEN Abort('Input file read error'); {close up, die}
-
- wlen := 0; {start with 0 length}
- FOR x := 1 TO LENGTH(S) DO BEGIN
- IF S[x] = BS THEN BEGIN {gobble previous real char, BS}
- bscnt := SUCC(bscnt); {bump counter}
- Write(#$0D, bscnt:5); {display}
- wlen := PRED(wlen);
- IF wlen < 0 THEN wlen := 0;
- END
- ELSE BEGIN {good char, add to work string}
- wlen := SUCC(wlen); {bump str length}
- WorkStr[wlen] := S[x]; {stuff str char in workstring}
- END;
- END;
- Writeln(OutFile,WorkStr); {write to output file}
- IF IOResult <> 0 {write failed}
- THEN Abort('Output file write error'); {close up, die}
- END; {while not EOF}
- Writeln(' backspaces removed.'); {neaten up after counter write}
- Abort(''); {close up, no error msgs}
- END; {of Remove_BS}
-
- BEGIN {main}
- Open_Files; {may die}
- Remove_BS; {do the work}
- END.