_ALGORITHM ALLEY_ by Tom Swan [LISTING ONE] { instab.pas -- Algorithm #6: Insert Tabs by Tom Swan } program InsTab; const outFName = 'TXT.OUT'; tabWidth = 8; { Try 4 for source code files } blank = #32; { ASCII blank character } tab = #09; { ASCII tab character } var InFName: String; InFile, OutFile: Text; Line: String; procedure InsertTabs(var L: String); var T: String; J, K: Integer; C, Q: Char; Eol: Boolean; { End of line } function NextChar(var C: Char): Char; begin C := L[J + 1]; Eol := C = Q; { True at end of line } NextChar := C end; begin T := ''; { Set result T to null } Q := #0; { Sentinel } L := L + Q; { Append sentinal to L } K := 0; { Column count } repeat J := K; while NextChar(C) = blank do begin J := J + 1; if J mod tabWidth = 0 then begin T := T + tab; K := J end; end; while (K < J) do begin T := T + blank; K := K + 1 end; if not Eol then begin T := T + C; K := K + 1 end; until Eol; L := T { Return T via parameter L } end; begin Writeln('Insert tabs'); Write('Input file name? '); Readln(InFName); Assign(InFile, InFName); Reset(InFile); Assign(OutFile, outFName); Rewrite(OutFile); Write('Inserting tabs...'); while not Eof(InFile) do begin Readln(InFile, Line); InsertTabs(Line); Writeln(OutFile, Line) end; Writeln; Close(InFile); Close(OutFile); Writeln(InFName, ' -> ', outFName) end. [LISTING TWO] { remtab.pas -- Algorithm #7: Remove Tabs by Tom Swan } program RemTab; const outFName = 'TXT.OUT'; tabWidth = 8; { Try 4 for source code files } blank = #32; { ASCII blank character } tab = #09; { ASCII tab character } var InFName: String; InFile, OutFile: Text; Line: String; procedure RemoveTabs(var L: String); var T: String; I: Integer; C: Char; begin T := ''; for I := 1 to Length(L) do begin C := L[I]; if C = tab then repeat T := T + blank until Length(T) mod tabWidth = 0 else T := T + C end; L := T { Return T via parameter L } end; begin Writeln('Remove tabs'); Write('Input file name? '); Readln(InFName); Assign(InFile, InFName); Reset(InFile); Assign(OutFile, outFName); Rewrite(OutFile); Write('Removing tabs...'); while not Eof(InFile) do begin Readln(InFile, Line); RemoveTabs(Line); Writeln(OutFile, Line) end; Writeln; Close(InFile); Close(OutFile); Writeln(InFName, ' -> ', outFName) end. [LISTING THREE] { diff.pas -- Algorithm #8: Differential Compression by Tom Swan } program Diff; const outFName = 'TXT.OUT'; var InFName: String; InFile, OutFile: Text; AWord, Prev: String; procedure Compress(var W, P: String); var T: String; { Temporary copy of W } I: Integer; { String index } begin T := W; I := 1; while (I <= Length(W)) and (I <= Length(P)) and (W[I] = P[I] ) do I := I + 1; Delete(W, 1, I - 1); W := Chr(I - 1) + W; P := T end; begin Writeln('Differential Compression'); Write('Input file name? '); Readln(InFName); Assign(InFile, InFName); Reset(InFile); Assign(OutFile, outFName); Rewrite(OutFile); Prev := ''; Write('Compressing...'); while not Eof(InFile) do begin Readln(InFile, AWord); Compress(AWord, Prev); Writeln(OutFile, AWord) end; Writeln; Close(InFile); Close(OutFile); Writeln(InFName, ' -> ', outFName) end. [LISTING FOUR] { undiff.pas -- Algorithm #9: Differential Decompression by Tom Swan } program UnDiff; const outFName = 'TXT.OUT'; var InFName: String; InFile, OutFile: Text; AWord, Prev: String; procedure Decompress(var W, P: String); var I: Integer; begin I := Ord(W[1]); Delete(W, 1, 1); while (I >= 1) do begin W := P[I] + W; I := I - 1 end; P := W end; begin Writeln('Differential Decompression'); Write('Input file name? '); Readln(InFName); Assign(InFile, InFName); Reset(InFile); Assign(OutFile, outFName); Rewrite(OutFile); Prev := ''; Write('Decompressing...'); while not Eof(InFile) do begin Readln(InFile, AWord); Decompress(AWord, Prev); Writeln(OutFile, AWord) end; Writeln; Close(InFile); Close(OutFile); Writeln(InFName, ' -> ', outFName) end. Example 1: Pseudocode for Algorithm #6 (Insert Tabs) procedure InsertTabs(L: String); var T: String; J, K: Integer; C, Q: Char; Eol: Boolean; { End of line } function NextChar(var C: Char): Char; begin C <- L[J + 1]; Eol <- C = Q; { True at end of line } Return C; end; begin Set T to null string; Set Q to unique char; Append Q to L as sentinel; K <- 0; { Column count } repeat J <- K; while NextChar(C) = blank do begin J <- J + 1; if J mod tabWidth = 0 then begin Append tab to T; K <- J; end; end; while (K < J) do begin Append blank to T; K <- K + 1; end; if not Eol then begin Append C to T; K <- K + 1; end; until Eol; Return T; end; Example 2: Pseudocode for Algorithm #7 (Remove Tabs) procedure RemoveTabs(L: String); var T: String; I: Integer; C: Char; begin Set T to null string; for I <- 1 to Length(L) do begin C <- L[I]; if C = tab then repeat Append blank to T; until Length(T) mod tabWidth = 0; else Append char C to T; end; Return T; end; Example 3: Pseudocode for Algorithm #8 (Differential Compression) procedure Compress(var W, P: String); var T: String; { Temporary copy of W } I: Integer; { String index } begin Copy W to T; Set I equal to 1; while (I <= Length(W)) and (I <= Length(P)) and (W[I] = P[I] ) do I <- I + 1; Delete I - 1 chars from head of W; Insert Chr(I - 1) at head of W; Set P equal to T; end; Example 4: Pseudocode for Algorithm #9 (Differential Decompression) procedure Decompress(var W, P: String); var I: Integer; { String index } begin Set I to value of W[1]; Delete W[1] from W; while (I >= 1) do begin Append P[I] to head of W; I <- I - 1; end; Set P equal to W; end;