view mc09/scanf.txt @ 180:c3145f913f3d default tip

possible features
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 04 May 2019 11:17:42 +0900
parents 1a30cd6e5973
children
line wrap: on
line source


_fskipspc(fp)
FILE *fp;
{char c;
        while (_isspace(c = getc(fp)));
        return c;
}

_isspace(c)
char c;
{       return ((c == ' ') || (c == '\t'));
}

scanf(s)
char *s;
{int i;
        i = _fscanf(stdin,s,(int *)&s+1);
        if (_fch && ((_ch == EOF) || (_ch == '\n'))) _fch = 0;
        return i;
}

fscanf(fp,s)
FILE *fp;
char *s;
{       return _fscanf(fp,s,(int *)&s+1);
}

_fscanf(fp,s,p)
FILE *fp;
char *s;
int *p;
{char c,ch;
 int m,n,r,x;
        n = 0;
        while(c = *s++)
                if (!_isspace(c))
                if (c != '%')
                {       if ((ch = _fskipspc(fp)) == EOF) return EOF;
                        if (ch != c) return n;
                }
                else
                {       if (x = (*s == '*')) ++s;
                        if (isdigit(*s)) s += _getint(&m,s);
                        else m = 32767;
                        switch(c = *s++)
                        {case 'd' :
                                r = _atoin(fp,*p++,m,x);
                                break;
                        case 'o' :
                                r = _otoin(fp,*p++,m,x);
                                break;
                        case 'x' :
                                r = _xtoin(fp,*p++,m,x);
                                break;
                        case 'c' :
                                r = _fgetc(fp,*p++,x);
                                break;
                        case 's' :
                                r = _getstrn(fp,*p++,m,x);
                                break;
                        case '\0':
                                return n;
                        default:
                                if ((ch = _fskipspc(fp)) == EOF) return EOF;
                                if (ch != c) return n;
                                continue;
                        }
                        if (!x) ++n;
                        if (r == EOF) return EOF;
                        if (r) return n;
                }
        return n;
}

_atoin(fp,p,m,x)
FILE *fp;
int *p,m,x;
{int i,s;
 char c;
        if (isdigit(c = _fskipspc(fp)) || (c == '-'))
        {       if (s = (c == '-')) c = getc(fp);
                for (i = 0; isdigit(c) && --m >= 0; c = getc(fp))
                                                i = i * 10 + c - '0';
                ungetc(c,fp);
                if (!x) *p = (s ? -i : i);
                return 0;
        }
        return ((c == EOF) ? EOF : 1);
}

_otoin(fp,p,m,x)
FILE *fp;
int *p,m,x;
{int i;
 char c;
        if (isoct(c = _fskipspc(fp)))
        {       for (i = 0; isoct(c) && --m >= 0; c = getc(fp))
                                                i = i * 8 + c -'0';
                ungetc(c,fp);
                if (!x) *p = i;
                return 0;
        }
        return ((c == EOF) ? EOF : 1);
}

isoct(c)
char c;
{       return ('0' <= c && c <= '7');
}

_xtoin(fp,p,m,x)
FILE *fp;
int *p,m,x;
{int i;
 char c;
        if (ishex(c = _fskipspc(fp)))
        {       for (i = 0; ishex(c) && --m >= 0; c = getc(fp))
                        if (isdigit(c)) i = i * 16 + c - '0';
                        else if ('A' <= (c = toupper(c)) && c <= 'F')
                                        i = i * 16 + c - ('A'-10);
                ungetc(c,fp);
                if (!x) *p = i;
                return 0;
        }
        return ((c == EOF) ? EOF : 1);
}

ishex(c)
char c;
{       return (isdigit(c) || ('A' <= (c = toupper(c)) && c <= 'F'));
}

_fgetc(fp,p,x)
FILE *fp;
char *p;
int x;
{char c;
        if ((c = getc(fp)) == EOF) return EOF;
        if (!x) *p = c;
        return 0;
}

_getstrn(fp,p,m,x)
FILE *fp;
char *p;
int m,x;
{char c,*q;
        if (((c = _fskipspc(fp)) == EOF) || (c == '\n')) return EOF;
        for (q = p; !_isspace(c) && --m >= 0; c = getc(fp))
        {       if ((c == EOF) || (c == '\n')) break;
                if ((c == '\b') && (q != p)) --q;
                else if (!x) *q++ = c;
        }
        ungetc(c,fp);
        if (!x) *q = '\0';
        return 0;
}