home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / textfile.swg / 0033_Formatting.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-01-27  |  4.7 KB  |  170 lines

  1. {
  2. | From: Scott Stone <pslvax!ucsd!u.cc.utah.edu!ss8913>
  3. |
  4. | This may sound like a simplistic request, but I need code to do the
  5. following:
  6.  
  7.  not really trivial, although its not hard
  8. |
  9. | Take a standard 80-column textfile and reformat it (w/ correct
  10. | wordwrapping) to be a new text file with lines of specified length (ie,
  11. | 40, 50, 60, etc).  Can anyone tell me how to do this (w/o truncating
  12. | lines, and w/o splitting words)?
  13.  
  14.  anyway, the following program may fill your needs as is
  15.  its for dos, of course, ..
  16.  (just change the constant max_wid to 40, 50, 60 etc), or,
  17.  at least, it will give you a head start on writing a program
  18.  for yourself.
  19. }
  20.  
  21. {*************************************************************************
  22. Program reformat
  23. by Sadunathan Nadesan
  24. 6/9/89
  25.  
  26. Formats a file into paragraphs suitable for sending via MCI
  27.  
  28. Usage: (on MS Dos)   % reformat < filename > outfilename
  29.  
  30. *************************************************************************}
  31.  
  32. program reformat(input,output);
  33.  
  34. const
  35.         max_wid =      80; {all output lines are less than this}
  36.       {change this for different sized lines}
  37. type
  38.  i_line  = string;  {input line buffer type}
  39.  o_line  = string;  {input line buffer type}
  40.  ref = ^node;
  41.  node = record
  42.     word : string;
  43.     next : ref;
  44.    end;
  45. var
  46.  root : ref;  {beginning of sized line}
  47.  tail : ref;  {pointer to last record in list}
  48.  line : i_line; {input buffer}
  49. {------------------------------------------------------------------------}
  50.  
  51. function end_of_paragraph (buffer : i_line): boolean;
  52. {detect the end of a paragraph}
  53. begin
  54. if (length(buffer) > 0) then
  55.      end_of_paragraph := FALSE
  56. else
  57.      end_of_paragraph := TRUE;
  58. end;
  59.  
  60. {------------------------------------------------------------------------}
  61. procedure store_words ( buffer : i_line );
  62. { **********************************************************
  63.   create a single linked list of all the words in a paragraph)
  64.   this is called anew for every line of the paragraph, but
  65.   uses a global linked list that it keeps working with.
  66.  
  67.   input paramters are buffer = the input line
  68.   uses global variables root and tail
  69.   ********************************************************** }
  70. var
  71.  insize          : integer; {size of input line}
  72.  b_counter : integer; {position marker in input buffer}
  73.  p  : ref;  {word record}
  74. begin
  75. insize    := length(buffer);
  76. b_counter := 1;
  77. if not (end_of_paragraph(buffer)) then  {if not an empty line}
  78.      repeat    {for each word in the input line}
  79.    begin
  80.    new (p);   {make a bucket for the word}
  81.    with p^ do
  82.         begin
  83.         next := nil;
  84.         word := '';
  85.         repeat
  86.       begin
  87.       if (buffer[b_counter] <> ' ') then
  88.     word := concat(word, buffer[b_counter]);
  89.       b_counter := b_counter + 1;
  90.       end;
  91.         until ((buffer[b_counter] = ' ') or (b_counter > insize));
  92.         end;
  93.    if (root = nil) then    {this is the first word in the par.}
  94.         begin
  95.         root := p;
  96.         tail := p;
  97.         end
  98.    else   {attach this word to the list of words}
  99.         begin
  100.         tail^.next := p;
  101.         tail := p;
  102.         end;
  103.    end; {repeat 1}
  104.      until (b_counter > insize);
  105. end; {store_words}
  106.  
  107. {------------------------------------------------------------------------}
  108. procedure format_output( p : ref );
  109. { **********************************************************
  110.   dump a single linked list of all the words in a paragraph
  111.   out into lines of <= max_wid characters
  112.  
  113.   input paramters is p = root, the starting record of the linked word list
  114.   uses global variable line
  115.  
  116.   ********************************************************** }
  117. var
  118.  pretty   : o_line; {output buffer}
  119.  one_more  : boolean;
  120. begin
  121. one_more := false;
  122. pretty := '';
  123. while (p^.next <> nil) do
  124.      begin
  125.      if (length(p^.word) + length(pretty) + 1 < max_wid)  then
  126.         begin
  127.         pretty := concat (pretty, p^.word);
  128.         pretty := concat (pretty, ' ');
  129.         p := p^.next;
  130.         end
  131.      else
  132.    begin
  133.    writeln(pretty);
  134.    pretty := '';
  135.    end;
  136.  
  137.      if (p^.next = nil) then   {for the last word!}
  138.    if (length(p^.word) + length(pretty) + 1 < max_wid)  then
  139.         pretty := concat (pretty, p^.word)
  140.      else
  141.    one_more := true;
  142.      end;
  143.  
  144. if (length(pretty) > 0) then  {get the last line}
  145.      writeln(pretty);
  146. if (one_more) then
  147.      writeln(p^.word);
  148. end;
  149. {------------------------------------------------------------------------}
  150.  
  151. begin
  152. root := nil;
  153. repeat
  154.      repeat
  155.    begin
  156.    readln(input, line);
  157.    store_words ( line);
  158.    end;
  159.      until (end_of_paragraph(line));
  160.  
  161.      if (root <> nil) then
  162.    begin
  163.    format_output(root);
  164.    writeln;
  165.    root := nil;
  166.    end;
  167.  
  168. until (eof(input));
  169. end.
  170.