|
Linewrapping Function [by Daniel Koepke] |
|
|
|
Posted Wednesday, August 12th @ 11:30:13 PM, by George Greer in the Utils dept.
Added Mar 5, 1997. Click the link below to read it or download it.
From: Daniel Koepke <dkoepke@california.com>
Subject: Linewrapping
Here's a little function to linewrap lines at 79. It's not perfect,
but it works fairly well. There's no question about the fact that
it's ugly.
char *linewrap(char *str) {
char xbuf2[MAX_STRING_LENGTH];
char xbuf[MAX_STRING_LENGTH];
bool cap=TRUE;
char *tmp;
int i;
xbuf[0]=xbuf2[0]='\0';
i = 0;
if (strlen(str) < 79)
return (str);
for (tmp = str; *tmp; tmp++) {
if (*tmp=='\r') {
if (xbuf[i-1] != ' ') {
xbuf[[i]=' ';
i++;
}
} else if (*tmp=='\n')
;
else if (*tmp == ' ') {
if (xbuf[i-1] != ' ') {
xbuf[i] = ' ';
i++;
}
} else {
xbuf[i] = *tmp;
xbuf[i+1] = ' ';
i += 2;
}
cap = TRUE;
} else {
xbuf[i]=*tmp;
if (cap) {
cap = FALSE;
xbuf[i] = UPPER(xbuf[i]);
}
i++;
}
}
xbuf[i]='\0';
strcpy(xbuf2, xbuf);
tmp=xbuf2;
xbuf[0]='\0';
for (;;) {
for (i=0; i<79; i++)
if (!*(tmp+i))
break;
if (i<79)
break;
// locate the last space
for (i=(xbuf[0]?78:75); i; i--)
if (*(tmp+i)==' ')
break;
if (i) {
*(tmp+i)='\0';
strcat(xbuf, tmp);
strcat(xbuf, "\r\n");
tmp += i+1;
while (*tmp == ' ')
tmp++;
} else {
*(tmp+77)='\0';
strcat(xbuf,tmp);
strcat(xbuf,"-\r\n");
tmp += 78;
}
}
while (*(tmp+i) && (*(tmp+i)==' ' || IS_NEWL(*(tmp+i))))
i--;
*(tmp+i+1)='\0';
strcat(xbuf,tmp);
if (xbuf[strlen(xbuf)-2] != '\r')
strcat(xbuf, "\r\n");
return (str_dup(xbuf)); }
Note that the above wasn't written for beauty or for speed, it's
a cheap and (very) dirty hack. Furthermore, note that I retyped
this from memory, so it may not even work. Oh, and here, too, is
a function to properly capitalize a string despite ansi codes:
char *ProperString(char *str) {
char xbuf[MAX_STRING_LENGTH];
int i = 0;
*xbuf = '\0';
strcpy(xbuf, str);
if (xbuf[i] == '\x1b') {
while ((xbuf[i] >= '0' && xbuf[i] <= 9) || xbuf[i] == ';')
i++;
i++;
}
xbuf[i] = UPPER(xbuf[i]);
return (str_dup(xbuf));
}
Neither are the most efficient things in the world, but they work (or
did before I retyped them :P).
--
Daniel Koepke
dkoepke@california.com
Forgive me father, for I am sin.
<< Listen Skill [by Ted Dubroff] | Reply | View as text | Threaded | Linewrapping Function, more [by Doppleganger Software] >> |
|
Related Links |
|
|
|
CircleMUD Snippets |
|
|
Note: Not all of these snippets will work perfectly with
your version of code, so be prepared to fix one
or two bugs that may arise, and please let me know
what you needed to do to fix it. Sending a corrected
version is always welcome.
|
Finally, if you wish to use any of the snippets from this
page, you are more than welcome, just mention the
authors in your credits. If you wish to release any
of these snippets to the public on another site,
contact me FIRST.
|
|
|
|
|
|
|