delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2004/08/12/14:00:35

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
X-Trace-PostClient-IP: 68.147.131.211
From: Brian Inglis <Brian DOT Inglis AT SystematicSw DOT Invalid>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: program hanging
Organization: Systematic Software
Message-ID: <sv9nh0tndcdjgritie7gqkjth48290ftoe@4ax.com>
References: <1092258885 DOT 100659 AT proxy2 DOT srv DOT ualberta DOT ca> <7105-Thu12Aug2004064431+0300-eliz AT gnu DOT org> <1092324277 DOT 615039 AT proxy2 DOT srv DOT ualberta DOT ca>
X-Newsreader: Forte Agent 1.93/32.576 English (American)
MIME-Version: 1.0
Lines: 122
Date: Thu, 12 Aug 2004 17:59:10 GMT
NNTP-Posting-Host: 24.71.223.147
X-Complaints-To: abuse AT shaw DOT ca
X-Trace: pd7tw3no 1092333550 24.71.223.147 (Thu, 12 Aug 2004 11:59:10 MDT)
NNTP-Posting-Date: Thu, 12 Aug 2004 11:59:10 MDT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On Thu, 12 Aug 2004 09:25:49 -0600 in comp.os.msdos.djgpp, "John
Hanley" <jdhanley AT telusplanet DOT net> wrote:

You do realize that this routine will eat not just spacing characters,
but any and all special characters, including [+-.]?
If you just want to skip spacing characters (which includes \n), test
for newline before spacing, and use !isspace(c) instead of isalnum(c),
and then you can legitimately use the name given. 

>f_eat_spaces() will return
>either 1 if
>it hits an alphanum char or EOF if it hits EOF:
>
>int f_eat_spaces(FILE * f)
>{
> int rtn=2;
>     char c;
>
>     /* loop until either an EOF is encountered
>      * or an alphanumeric character or \n is found.
>      *
>      * Return:
>      * 1 - if alphanumeric char is encountered
>      * 0 - if \n is encountered
>      * EOF - if EOF is encountered
>      */
>     while(rtn!=EOF)
>     {
>      rtn=fscanf(f,"%c",&c);

What happens if you get an EOF? 
You just go ahead and process whatever is in c anyway! 
And if it happens to look like a \n or an alnum, you return that code.
This could be called and return the same result indefinitely! 
See below for C approach: and note the name change to reflect reality.

>          /* if we come across an alphanumeric
>           * character, we put it back and break
>           * out of the loop.*/
>          if(isalnum(c))
>          {
>           rtn=1;
>           ungetc(c,f);
>               break;
>          }
>          else if(c=='\n')
>          {
>            rtn=0;
>               break;
>          }
>
>     }
>     return rtn;
>}

/* read until an alphanumeric character, \n or EOF is found.
 * Return:
 * 0 - if \n is encountered
 * 1 - if alphanumeric char is encountered
 * EOF - if EOF is encountered
 */
int f_eat_non_alnum(FILE * f)
{
    int c; /* EOF does not fit in a char, you need an int */

/* normal idiomatic technique for C I/O avoids problems:
 * get data, save the result, and test for the end condition */
    while ((c = getc(f)) != EOF)
    {
        /* if newline, just return flag value */
        if (c == '\n')
        {
            return 0;
        }
     /* else redundant because of return */
        /* if alphanumeric, put it back, and return flag value */
        if(isalnum(c))
        {
            ungetc(c,f);
            return 1;
        }
    }

    return c;
}

And for your eat newlines loop setup and loop:

>rtn=f_eat_spaces(param);
>          /* eat up white spaces and multiple \n characters
>           * until an alphanumeric or EOF encountered */
>          while(rtn==0)
>          {
>          rtn=f_eat_spaces(param);
>          }
>
>          /* if it's EOF, we're done with parameter file */
>          if(rtn==EOF)
>           break;

just use the same kind of C I/O idiom:

    /* eat up non-alnum and \n characters
     * until an alnum or EOF encountered */
    while ((rtn = f_eat_non_alnum( param )) == 0)
    {
        continue; /* does nothing, just obvious placeholder */
    }

    /* if it's EOF, we're done with parameter file */
    if (rtn == EOF)
        break;

If you put in a few more spaces, and keep the indentation consistent,
it makes the code easier to read, and makes it easier to see where you
may have gone wrong. 

-- 
Thanks. Take care, Brian Inglis 	Calgary, Alberta, Canada

Brian DOT Inglis AT CSi DOT com 	(Brian dot Inglis at SystematicSw dot ab dot ca)
    fake address		use address above to reply

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019