home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TURBOPAS / JUSTIFY.ZIP / JUSTIFY.PAS
Encoding:
Pascal/Delphi Source File  |  1987-11-08  |  2.9 KB  |  76 lines

  1. {procedures and functions for string manipulation.  Adapted from
  2.  David F. Moffat by Glenn Brooke for Turbo Pascal 11/22/85}
  3.  
  4. const
  5.      maxlen = 80;      {# char in a string}
  6.      margin = '          ';     {10 column indentation}
  7.      printwidth = 65;            {width of printed line}
  8. type
  9.     fixedstring = string[maxlen];
  10.     str = record   {a "variable" length string}
  11.              st : fixedstring;     {string to use}
  12.              len : 0..maxlen;      {actual length being used}
  13.           end; {str}
  14.  
  15.     outputline = record       {line of accumulated words}
  16.                    out : str;       {actual line to be printed}
  17.                    actioncol : integer  {column of last action}
  18.                  end; {outputline}
  19.  
  20.  
  21. var
  22.    line : outputline;
  23.    word : str;
  24.    ch : char;
  25.  
  26.  
  27. procedure Justify(var line : outputline);
  28. {justify line to a length of printwidth by putting extra blanks between
  29.  words, from right to left.  The line currently has one blank between words.}
  30.  
  31. var
  32.    blanks,               {# of blanks to be inserted}
  33.    gaps,                 {# of gaps between words}
  34.    n,                    {amount to expand 1 gap}
  35.    dest,                 {new place for moved char}
  36.    source : integer;     {source column of that char}
  37.  
  38. begin {justify}
  39.       with line, out do
  40.            if len < printwidth then
  41.                   begin
  42.                   {count # of gaps between words}
  43.                   gaps := 0;
  44.                   for source := 1 to len do
  45.                       if str[source] = ' ' then gaps := succ(gaps);
  46.  
  47.                   {find # of blanks needed to stretch the line}
  48.                   blanks := printwidth - len;
  49.                   {shift characters to the right, distributing extra blanks}
  50.                   {between the words (in the gaps)}
  51.                   dest := printwidth;
  52.                   source := len;
  53.                   while gaps > 0 do
  54.                         begin {expand line}
  55.                         if str[source] <> ' ' then
  56.                            begin {shift char}
  57.                            str[dest] := str[source];   {move char, leave blank}
  58.                            str[source] := ' ';
  59.                            end
  60.                         else
  61.                            begin  {leave blanks}
  62.                            {find # of blanks for this gap, skip that many}
  63.                            {(now blank) columns}
  64.                            n := blanks div gaps;
  65.                            dest := dest - n;
  66.                            gaps := pred(gaps);
  67.                            blanks := blanks - n;
  68.                            end;
  69.                         {step to next source and dest characters}
  70.                         source := pred(source);
  71.                         dest := pred(dest)
  72.                         end; {expand line}
  73.                  len := printwidth
  74.                  end
  75.         end; {justify procedure}
  76.