PROCEDURE trim_blanks ( VAR this_line :line_string );
{Trim trailing blanks }
VAR
col :INTEGER;
flag :BOOLEAN;
BEGIN{PROCEDURE}
col := LENGTH (this_line);
flag := FALSE;
WHILE (col>0) AND (NOT flag) DO BEGIN
IF this_line[col] = ' ' THEN BEGIN
col := col - 1;
END
ELSE BEGIN
flag := TRUE;
END{IF};
END{WHILE};
SETLENGTH (this_line, col);
IF debug_flag THEN BEGIN
col := LENGTH (this_line);
WRITELN ('%exit trim_blanks: length=', col:4);
WRITELN (this_line);
END{IF};
VAR
flag :BOOLEAN;
response :CHAR;
BEGIN{FUNCTION}
flag := FALSE;
WHILE NOT flag DO BEGIN
WRITE ('(Y or N)');
READLN(response);
response := upper_case (response);
IF (response='Y') OR (response='N') THEN BEGIN
flag := TRUE;
END
ELSE BEGIN
WRITELN('Try again. ');
END{IF};
END{WHILE};
FUNCTION get_line (VAR this_line :line_string) :INTEGER;
VAR
result :INTEGER;
len :INTEGER;
BEGIN{FUNCTION}
result := 0;
IF EOF(inf_file) THEN BEGIN
result := -1;
SETLENGTH (this_line, 0);
END
ELSE BEGIN
READLN (inf_file, this_line);
IF debug_flag THEN BEGIN
len := LENGTH (this_line);
WRITELN ('Input line: status=', result:4,
' length=', len:3);
WRITELN (this_line);
END{IF};
END{IF};
BEGIN{PROGRAM}
WRITELN
('Trim File Program Version ', version);
WRITELN ('This program reads an input file, trims the ');
WRITELN ('last N columns from the lines, then trims any');
WRITELN ('trailing blanks,');
WRITELN ('and writes lines into output file.');
WRITE('Debugging on? ');
debug_flag := ask_yes_or_no;
IF debug_flag THEN WRITELN('Debug is on.');
WRITE('List the lines as they are read? ');
list_flag := ask_yes_or_no;
flag := FALSE;
WHILE NOT flag DO BEGIN
WRITE ('Enter column# to which we will truncate: ');
READLN (trunc_length);
IF (trunc_length < 1) OR (trunc_length > 255) THEN BEGIN
WRITELN ('*** Too small or too big. Try again.');
END
ELSE BEGIN
WRITELN ('Lines longer than ', trunc_length:3,
' will be truncated.');
flag := TRUE;
END{IF};
END{WHILE};
WRITE('Trim trailing blanks from output lines? ');
trim_flag := ask_yes_or_no;
status := get_open;
IF status <> 0 THEN WRITELN ('Cannot open input file.');
IF status=0 THEN BEGIN
status := put_open;
IF status <>0 THEN WRITELN ('Cannot open output file.');
END{IF};
IF status=0 THEN BEGIN
WHILE status = 0 DO BEGIN
status := get_line (this_line);
IF status = 0 THEN BEGIN
truncate_line (this_line);
IF trim_flag THEN trim_blanks (this_line);
status := put_line (this_line);
END{IF};
END{WHILE};
END{IF};