home *** CD-ROM | disk | FTP | other *** search
- {procedures and functions for string manipulation. Adapted from
- David F. Moffat by Glenn Brooke for Turbo Pascal 11/22/85}
-
- const
- maxlen = 80; {# char in a string}
- margin = ' '; {10 column indentation}
- printwidth = 65; {width of printed line}
- type
- fixedstring = string[maxlen];
- str = record {a "variable" length string}
- st : fixedstring; {string to use}
- len : 0..maxlen; {actual length being used}
- end; {str}
-
- outputline = record {line of accumulated words}
- out : str; {actual line to be printed}
- actioncol : integer {column of last action}
- end; {outputline}
-
-
- var
- line : outputline;
- word : str;
- ch : char;
-
-
- procedure Justify(var line : outputline);
- {justify line to a length of printwidth by putting extra blanks between
- words, from right to left. The line currently has one blank between words.}
-
- var
- blanks, {# of blanks to be inserted}
- gaps, {# of gaps between words}
- n, {amount to expand 1 gap}
- dest, {new place for moved char}
- source : integer; {source column of that char}
-
- begin {justify}
- with line, out do
- if len < printwidth then
- begin
- {count # of gaps between words}
- gaps := 0;
- for source := 1 to len do
- if str[source] = ' ' then gaps := succ(gaps);
-
- {find # of blanks needed to stretch the line}
- blanks := printwidth - len;
- {shift characters to the right, distributing extra blanks}
- {between the words (in the gaps)}
- dest := printwidth;
- source := len;
- while gaps > 0 do
- begin {expand line}
- if str[source] <> ' ' then
- begin {shift char}
- str[dest] := str[source]; {move char, leave blank}
- str[source] := ' ';
- end
- else
- begin {leave blanks}
- {find # of blanks for this gap, skip that many}
- {(now blank) columns}
- n := blanks div gaps;
- dest := dest - n;
- gaps := pred(gaps);
- blanks := blanks - n;
- end;
- {step to next source and dest characters}
- source := pred(source);
- dest := pred(dest)
- end; {expand line}
- len := printwidth
- end
- end; {justify procedure}