home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / Samples / CSAPE32.ARJ / SOURCE / OWLSCR / STRWRAP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-14  |  2.6 KB  |  120 lines

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