Fread_word errors

Posted by Dace K on Sun 12 Mar 2006 01:16 AM — 8 posts, 26,586 views.

Canada #0
For some reason, fread_word refuses to read the string 'Elaen', the name of one of my players. Instead, it'll go into an infinate loop, freezing the game and filling up the log file with: fread_word: EOF encountered on read .

It doesn't happen to seem with any other players, just Elaen..
Any ideas why? Here's my fread_word:



char *fread_word( FILE *fp )
{
    static char word[MAX_INPUT_LENGTH];
    char *pword;
    char cEnd;

    do
    {
	if ( feof(fp) )
	{
	    bug("fread_word: EOF encountered on read.\n\r");
	    if ( fBootDb )
		exit(1);
	    word[0] = '\0';
	    return word;
	}
	cEnd = getc( fp );
    }
    while ( isspace( cEnd ) );

    if ( cEnd == '\'' || cEnd == '"' )
    {
	pword   = word;
    }
    else
    {
	word[0] = cEnd;
	pword   = word+1;
	cEnd    = ' ';
    }

    for ( ; pword < word + MAX_INPUT_LENGTH; pword++ )
    {
	if ( feof(fp) )
	{
	    bug("fread_word: EOF encountered on read.\n\r");
	    if ( fBootDb )
		exit(1);
	    *pword = '\0';
	    return word;
	}
	*pword = getc( fp );
	if ( cEnd == ' ' ? isspace(*pword) : *pword == cEnd )
	{
	    if ( cEnd == ' ' )
		ungetc( *pword, fp );
	    *pword = '\0';
	    return word;
	}
    }

    bug( "Fread_word: word too long" );
    exit( 1 );
    return NULL;
}


Note: Fread_string works perfectly all right with Elaen though.
Amended on Sun 12 Mar 2006 01:25 AM by Dace K
USA #1
This might be an obvious thing to ask but are you sure the pfile itself isn't corrupted somehow? I see no explanation why this code would work with one name and not another.
Canada #2
I'm sure. I've tested several times with different pfiles. Any character that is introduced to Elaen will no longer load.

The code I'm using is the AFKmud introduction code.
void fread_introductions(CHAR_DATA *ch, FILE *fp)
{
    INTRO_DATA *intro = NULL;
    char *word, *str;
    bool fMatch;

    for ( ; ; )
    {
        word   = feof( fp ) ? "End" : fread_word( fp );
        fMatch = FALSE;

        switch ( UPPER(word[0]) )
        {
        case '*':
            fMatch = TRUE;
            fread_to_eol( fp );
            break;
        case 'E':
            if ( !str_cmp( word, "End" ) )
                return;
            break;

        default:
            intro = new_intro(ch, word);
            str = fread_word( fp );
            update_intro(intro, str);
            str = fread_word( fp );
            update_recog(intro, str);
            break;
        }
    }
}


It stores the data under a #INTRO heading in each char's pfiles, with '<charname>' '<introname>' '<recognizedname>'.
USA #3
OK, well, here's your problem:
        case 'E':
            if ( !str_cmp( word, "End" ) )
                return;
            break;

There you go -- that is almost certainly your problem.

If it starts with an 'E', you return if the word is equal to End.

But, then you break in all cases. So, you never get to the code that actually reads in the introduction:
        default:
            intro = new_intro(ch, word);
            str = fread_word( fp );
            update_intro(intro, str);
            str = fread_word( fp );
            update_recog(intro, str);
            break;

You probably want to remove that 'break' in the E case.
Amended on Sun 12 Mar 2006 02:44 AM by David Haley
Canada #4
Thanks a lot! I'm kicking myself for not seeing that.

I guess it's pretty important that I restrict the player name "End" right now :P
Amended on Sun 12 Mar 2006 03:08 AM by Dace K
USA #5
That would be in interp.c somewhere, where you take a line and handle it. You'd simply loop over the line of text and report failure if there's an apostrohpe. I'm not sure why you'd want to prevent apostrophes, though?
Canada #6
Note to anyone who's using this snippet: Players putting apostrophies in their names will cause nasty crashes. Install this, and you'll be fine :)


char *stripquotes( char *text )
{
	int k = 0, j = 0;

	if (!text || text[0] == '\0')
	{
		return NULL;
	}
	else
	{
		char *buf;

		static char done[MAX_INPUT_LENGTH*2];

		done[0] = '\0';

		if ( (buf = (char *)malloc( strlen(text) * sizeof(text) )) == NULL)
			return text;

		/* Loop through until you've hit your terminating 0 */
		while (text[k] != '\0')
		{
			while (text[k] == '\'')
			{
				k ++;
			}
			if ( text[k] != '\0' )
			{
				if ( isspace(text[k]) )
				{
					buf[j] = ' ';
					k++;
					j++;
				}
				else
				{
					buf[j] = text[k];
					k++;
					j++;
				}
			}
			else
				buf[j] = '\0';
		}

		buf[j] = '\0';

		sprintf(done, "%s", buf);
		buf = realloc(buf, j*sizeof(char));
		free( buf);

		return done;
	}
}
Amended on Sun 12 Mar 2006 07:00 PM by Dace K
USA #7
Oh... apostrohpes in the names. Right, that's a bad thing. :-) I think we've disallowed that for a while.

BTW, be careful of [i] in your code posts, since it ends up italicizing the code.