25 Apr, 2010, Oliver wrote in the 1st comment:
Votes: 0
So. I'd appreciate if anyone could give me a hand with this. I've done some of my own tinkering, asked a few of my friends, and searched all over the 'nets for a pre-existing solution, but I haven't found anything yet.

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. :)
25 Apr, 2010, David Haley wrote in the 2nd comment:
Votes: 0
Is this something that works in general but not for your MUD? In any case, if you post the function you'll be most likely to get eyes looking at it. :smile:
25 Apr, 2010, Oliver wrote in the 3rd comment:
Votes: 0
David Haley said:
Is this something that works in general but not for your MUD? In any case, if you post the function you'll be most likely to get eyes looking at it. :smile:


It's the stock OLC format_string() function. I have never seen another mud with the same problem, but that is ostensibly because every MUD I've played with OLC has already fixed it. It is definitely a problem with stock OLC.

Here's the ol' function; you'll notice that I have commented out a line or three, but that isn't the root of the problem. Before that, it was doing even weirder things on repeat formats.

/*
* 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));
}


If anyone else is curious about anything, let me know. I can provide details.
25 Apr, 2010, Sharmair wrote in the 4th comment:
Votes: 0
Though this code has a lot of corner cases like the possibility of reading out of bounds
or making a mess of non normal text like maybe scripts, off hand one thing what could
cause unintended line breaks might be the commenting of the code that removes the
\n from the original text. It seems it would just be passed on to the thing intended
to be one long string, then copied onto the final string. You might try uncommenting
that block and see how it goes.
Also, your input conditions are a bit unclear. If possible, make sure to give us the
EXACT input you are giving, and exactly what you are getting as output and what
you mean by repeat formats.
Also as a note, the line ends should be \r\n not \n\r, but fixing that here will have to
be done carefully.
0.0/4