home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / Ph 1.1.1 / Lib / wrap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-28  |  2.8 KB  |  124 lines  |  [TEXT/MPS ]

  1. /*______________________________________________________________________
  2.  
  3.     wrap.c - Tool to word wrap paragraphs.
  4.     
  5.     Author: John Norstad
  6.     
  7.     Copyright © 1988-1991 Northwestern University.
  8.     
  9.     This tool reads standard input and writes standard output.  It
  10.     word wraps each paragraph from the input file.
  11.     
  12.     In Disinfectant I use this tool to help build the online document.  I
  13.     maintain the source using Microsoft word on a text-only file, without line 
  14.     breaks.  This saves each paragraph as one big long line.  My makefile 
  15.     runs the wrap tool to word wrap each of these paragraphs to the exact 
  16.     width of my help window rectangle.  The output of wrap is then fed through 
  17.     the cvrt tool to generate the sequence of STR# resources used by the program.
  18.     
  19.     wrap -r xxxx [-p]
  20.     
  21.     xxxx = right margin in pixels.
  22.     
  23.     -p = if specified, add a special end-of-paragraph byte 31 to the
  24.         end of each line.  This option should be specified when preparing
  25.         type 1 reports for the rep.c module.  Rep.c uses the special eop
  26.         markers to identify paragraphs in the printing code.
  27.     
  28. _____________________________________________________________________*/
  29.  
  30. #pragma load "precompile"
  31.  
  32. #include "doc.h"
  33.  
  34. short main(short argc, char *argv[])
  35.  
  36. {
  37.     char        line[10000];
  38.     char        *first;
  39.     char        *last;
  40.     char        *prev;
  41.     char        *next;
  42.     char        save;
  43.     short        len;
  44.     GrafPort    myPort;
  45.     Boolean    rSpecified;
  46.     Boolean    pSpecified;
  47.     short        margin;
  48.     short        i;
  49.     
  50.     /* Crack and check parameters. */
  51.  
  52. #ifdef THINK_C
  53.     argc = ccommand(&argv);
  54. #endif
  55.  
  56.     i = 1;
  57.     rSpecified = pSpecified = false;
  58.     while (i < argc) {
  59.         if (*argv[i] == '-') {
  60.             if (tolower(*(argv[i]+1)) == 'r') {
  61.                 rSpecified = true;
  62.                 margin = atoi(argv[i+1]);
  63.                 i += 2;
  64.             } else if (tolower(*(argv[i]+1)) == 'p') {
  65.                 pSpecified = true;
  66.                 i += 2;
  67.             } else {
  68.                 fprintf(stderr, "### %s - Usage: %s -r rightmargin [-p].\n", 
  69.                     argv[0], argv[0]);
  70.                 return 1;
  71.             }
  72.         } else {
  73.             fprintf(stderr, "### %s - Usage: %s -r rightmargin [-p].\n", 
  74.                 argv[0], argv[0]);
  75.             return 1;
  76.         }
  77.     }
  78.     if (!rSpecified) {
  79.         fprintf(stderr, "### %s - right margin not specified.\n", argv[0]);
  80.         return 1;
  81.     }
  82.  
  83.     /* Process file. */
  84.     
  85.     InitGraf(&qd.thePort);
  86.     OpenPort(&myPort);
  87.     TextFont(applFont);
  88.     TextSize(9);
  89.     while (gets(line)) {
  90.         first = last = prev = line;
  91.         len = strlen(line);
  92.         *(line+len) = ' ';
  93.         *(line+len+1) = 0;
  94.         while (true) {
  95.             while (true) {
  96.                 next = strchr(prev, ' ');
  97.                 if (!next || TextWidth(first, 0, next-first) > margin ) break;
  98.                 last = next;
  99.                 prev = last + strspn(last, " ");
  100.             }
  101.             save = *last;
  102.             if (next) {
  103.                 *last = 0;
  104.                 puts(first);
  105.                 *last = save;
  106.                 first = prev;
  107.                 last = next;
  108.             } else {
  109.                 if (pSpecified) {
  110.                     *last = docEop;
  111.                     *(last+1) = 0;
  112.                     puts(first);
  113.                     break;
  114.                 } else {
  115.                     *last = 0;
  116.                     puts(first);
  117.                     break;
  118.                 }
  119.             }
  120.         }
  121.     }
  122.     
  123.     return 0;
  124. }