home *** CD-ROM | disk | FTP | other *** search
- /*
- strwrap.c 11/18/86
-
- % strwrap
-
- OWL 1.1
- Copyright (c) 1986, 1987, 1988 by Oakland Group, Inc.
- ALL RIGHTS RESERVED.
-
- Revision History:
- -----------------
- 4/30/88 jmd Removed length restrictions (strright and strcenter).
- 7/27/88 jmd Moved to oakland lib from C-scape.
- */
-
- #include "oakhead.h"
- #include "strdecl.h"
- #include <ctype.h>
- /* -------------------------------------------------------------------------- */
-
- char *strwrap(text, row, width)
- char *text; /* the string to word wrap */
- int *row; /* the number of rows of text in out */
- int width; /* the width of the text rows */
- /*
- Takes a string and copies it into a new string.
- Lines are wrapped around if they are longer than width.
-
- returns a pointer to the space allocated for the output.
- or NULL if out of memory.
- */
- /*
- Wrap algorithm:
-
- Start at beginning of the string.
- Check each character and increment a counter.
- If char is a space remember the location as a valid break point
- If the char is a '\n' end the line and start a new line
- If WIDTH is reached end the line at the last valid break point
- and start a new line.
- If the valid break point is at the beginning of the line
- hyphenate and continue.
- */
- {
- char *string; /* the output string */
- char *line; /* start of current line */
- char *brk; /* most recent break in the text */
- char *t, *s;
- unsigned int len;
- boolean done = FALSE;
-
- *row = 0;
-
- /* allocate string space; assume the worst */
- len = strlen(text);
-
- len = (len > 0x7fff) ? 0xffff : len * 2 + 1;
-
- if ((string = (char *) omalloc(OA_STRWRAP, len)) == NULL) {
- return(NULL);
- }
-
- if( *text == '\0' || width < 2) {
- strcpy(string, text);
- return(string);
- }
-
- *string = '\0';
- line = string;
-
- for(t = text; !done;) {
- for(brk = s = line; (s - line) < width; t++, s++) {
- *s = *t;
- if (*t == '\n' || *t == '\0') {
- brk = s;
- break;
- }
- else if (*t == ' ') {
- brk = s;
- }
- }
-
- if (brk == line && *t != '\n' && *t != '\0') {
- /* one long word... */
- s--;
- t--;
- *s = '\n';
- *(s+1) = '\0';
- line = s + 1;
- }
- else if (*t == '\n'){
- *s = '\n';
- *(s+1) = '\0';
- t++;
- if ( *t == '\0') {
- done = TRUE;
- }
- else {
- line = s + 1;
- }
- }
- else if (*t == '\0'){
- *s = '\0';
- done = TRUE;
- }
- else {
- /* chop off last word */
- t = t - (s - brk) + 1; /* Causes Turbo 2.0 warning. Can't help it. */
- *brk = '\n';
- *(brk+1) = '\0';
- line = brk + 1;
- }
-
- (*row)++;
- }
-
- return(string);
- }
-