/*
* Revised by Selquest to wrap colored lines!
* Thanks to Kalgen for the new procedure (no more bug!)
* Original wordwrap() written by Surreality.
*/
/*****************************************************************************
Name: format_string
Purpose: Special string formating and word-wrapping.
Called by: string_add(string.c) (many)olc_act.c
****************************************************************************/
char *format_string (char *oldstring /*, bool fSpace */ )
{
char xbuf[MAX_STRING_LENGTH];
char xbuf2[MAX_STRING_LENGTH];
char *rdesc;
char tmp;
int i = 0;
int j;
bool cap = TRUE;
xbuf[0] = xbuf2[0] = 0;
i = 0;
for (rdesc = oldstring; *rdesc; rdesc++)
{
//replacing newlines with spaces, makes one long line.
/* if (*rdesc == '\n')
{
if (xbuf[i - 1] != ' ')
{
xbuf[i] = ' ';
i++;
}
}
else*/if (*rdesc == '\r');
//smashing repeated spaces
else if (*rdesc == ' ')
{
if (xbuf[i - 1] != ' ')
{
xbuf[i] = ' ';
i++;
}
}
//kill spaces before a ), if it follows punctuation
else if (*rdesc == ')')
{
if (xbuf[i - 1] == ' ' && xbuf[i - 2] == ' ' &&
(xbuf[i - 3] == '.' || xbuf[i - 3] == '?'
|| xbuf[i - 3] == '!'))
{
xbuf[i - 2] = *rdesc;
xbuf[i - 1] = ' ';
xbuf[i] = ' ';
i++;
}
else
{
xbuf[i] = *rdesc;
i++;
}
}
//spaces after punctuation
else if (*rdesc == '.' || *rdesc == '?' || *rdesc == '!')
{
if (xbuf[i - 1] == ' ' && xbuf[i - 2] == ' ' &&
(xbuf[i - 3] == '.' || xbuf[i - 3] == '?'
|| xbuf[i - 3] == '!'))
{
xbuf[i - 2] = *rdesc;
if (*(rdesc + 1) != '\"')
{
xbuf[i - 1] = ' ';
xbuf[i] = ' ';
i++;
}
else
{
xbuf[i - 1] = '\"';
xbuf[i] = ' ';
xbuf[i + 1] = ' ';
i += 2;
rdesc++;
}
}
else
{
xbuf[i] = *rdesc;
if (*(rdesc + 1) != '\"')
{
xbuf[i + 1] = ' ';
xbuf[i + 2] = ' ';
i += 3;
}
else
{
xbuf[i + 1] = '\"';
xbuf[i + 2] = ' ';
xbuf[i + 3] = ' ';
i += 4;
rdesc++;
}
}
cap = TRUE;
}
else
{
xbuf[i] = *rdesc;
//capitalize first letter after punc
if (cap)
{
cap = FALSE;
xbuf[i] = UPPER (xbuf[i]);
}
i++;
}
}
xbuf[i] = 0;
strcpy (xbuf2, xbuf);
rdesc = xbuf2;
xbuf[0] = 0;
for (;;)
{
i = 0;
for (j = 0; j < 77; i++)
{
if (!*(rdesc + i)){
break;
} else if (*(rdesc + i) == '{'){
//ignore color codes when counting for format
i++;
if (*(rdesc + i)){
//if it was a double {, the character actually counts
//otherwise, skip the second part too!
if (*(rdesc + i) == '{'){
j++;
}
}
} else {
j++;
}
}
if (j < 77)
{
break;
}
for (j = i; j; j–)
{
if (*(rdesc + j) == ' ')
break;
}
if (j)
{
*(rdesc + j) = 0;
strcat (xbuf, rdesc);
strcat (xbuf, "\n\r");
rdesc += j + 1;
while (*rdesc == ' ')
rdesc++;
}
else
{
bug ("No spaces", 0);
tmp = *(rdesc + i );
*(rdesc + i) = 0;
strcat (xbuf, rdesc);
strcat (xbuf, "-\n\r");
*(rdesc + i) = tmp;
rdesc += i;
}
}
while (*(rdesc + i) && (*(rdesc + i) == ' ' ||
*(rdesc + i) == '\n' || *(rdesc + i) == '\r'))
i–;
*(rdesc + i + 1) = 0;
strcat (xbuf, rdesc);
if ((xbuf[strlen (xbuf) - 2] != '\n'))
strcat (xbuf, "\n\r");
free_string (oldstring);
return (str_dup (xbuf));
}
Basically, the string_format() function for OLC on my RoM codebase isn't functioning properly. I'll show what I mean:
"This is a string that needs
to be formatted."
formatted will turn into:
"This is a string that needs to be formatted."
Everything is going as planned. Let's say, for some reason, I add a new part to my string.
"This is a string that needs to be formatted.
This, too, is a string that needs to be formatted."
Should turn into:
"This is a string that needs to be formatted. This, too, is a string that [here is where it should line break]
needs to be formatted."
Instead, it turns into this:
"This is a string that needs to be
formatted.
This, too, is a string that needs to be
formatted."
Essentially, it just keeps lopping off the last word of a string and adding it on a newline, which it is not supposed to do. If anyone has dealt with this problem in the past, I'd appreciate the help. Otherwise, if anyone is interested, I can paste my format_string() function to the forum here, if you'd like to take a look. Just let me know. Thanks in advance. :)