home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c185 / 2.ddi / OWLSRC.EXE / CSCAPE / SOURCE / STRWRAP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-07  |  2.5 KB  |  119 lines

  1. /*
  2.     strwrap.c      11/18/86
  3.  
  4.     % strwrap
  5.  
  6.     OWL 1.1
  7.     Copyright (c) 1986, 1987, 1988 by Oakland Group, Inc.
  8.     ALL RIGHTS RESERVED.
  9.  
  10.     Revision History:
  11.     -----------------
  12.      4/30/88 jmd    Removed length restrictions (strright and strcenter).
  13.      7/27/88 jmd    Moved to oakland lib from C-scape.
  14. */
  15.  
  16. #include "oakhead.h"
  17. #include "strdecl.h"
  18. #include <ctype.h>
  19. /* -------------------------------------------------------------------------- */
  20.  
  21. char *strwrap(text, row, width)
  22.     char     *text;        /* the string to word wrap */
  23.     int     *row;          /* the number of rows of text in out */
  24.     int     width;        /* the width of the text rows */
  25. /*
  26.     Takes a string and copies it into a new string.
  27.     Lines are wrapped around if they are longer than width.    
  28.  
  29.     returns a pointer to the space allocated for the output.
  30.     or NULL if out of memory.
  31. */
  32. /*
  33.     Wrap algorithm: 
  34.  
  35.     Start at beginning of the string.
  36.     Check each character and increment a counter.
  37.     If char is a space remember the location as a valid break point
  38.     If the char is a '\n' end the line and start a new line
  39.     If WIDTH is reached end the line at the last valid break point
  40.         and start a new line.
  41.     If the valid break point is at the beginning of the line
  42.         hyphenate and continue.
  43. */
  44. {
  45.     char    *string;            /* the output string */
  46.     char    *line;                /* start of current line */
  47.     char       *brk;                 /* most recent break in the text */
  48.     char       *t, *s;
  49.     unsigned int len;
  50.     boolean    done = FALSE;
  51.  
  52.     *row = 0;
  53.  
  54.     /* allocate string space; assume the worst */
  55.     len = strlen(text);
  56.  
  57.     len = (len > 0x7fff) ? 0xffff : len * 2 + 1;
  58.  
  59.     if ((string = (char *) omalloc(OA_STRWRAP, len)) == NULL) {
  60.         return(NULL);
  61.     }
  62.  
  63.     if( *text == '\0' || width < 2) {
  64.         strcpy(string, text);
  65.         return(string);
  66.     }
  67.  
  68.     *string = '\0';
  69.     line = string;
  70.  
  71.     for(t = text; !done;) { 
  72.         for(brk = s = line; (s - line) < width; t++, s++) {    
  73.             *s = *t;
  74.             if (*t == '\n' || *t == '\0') {
  75.                 brk = s;
  76.                 break;
  77.             }
  78.             else if (*t == ' ') {
  79.                 brk = s;
  80.             }
  81.         }
  82.  
  83.         if (brk == line && *t != '\n' && *t != '\0') {
  84.             /* one long word... */
  85.             s--;
  86.             t--;
  87.             *s = '\n';
  88.             *(s+1) = '\0';
  89.             line = s + 1;
  90.         }
  91.         else if (*t == '\n'){
  92.             *s = '\n';
  93.             *(s+1) = '\0';
  94.             t++;
  95.             if ( *t == '\0') {
  96.                 done = TRUE;
  97.             }
  98.             else {
  99.                 line = s + 1;
  100.             }
  101.         }
  102.         else if (*t == '\0'){
  103.             *s = '\0';
  104.             done = TRUE;
  105.         }
  106.         else {
  107.             /* chop off last word */
  108.             t = t - (s - brk) + 1;  /* Causes Turbo 2.0 warning. Can't help it. */
  109.             *brk = '\n';
  110.             *(brk+1) = '\0';
  111.             line = brk + 1;
  112.         }
  113.  
  114.         (*row)++;
  115.     }
  116.  
  117.     return(string);
  118. }
  119.