[1] | 1 | |
---|
| 2 | _fskipspc(fp) |
---|
| 3 | FILE *fp; |
---|
| 4 | {char c; |
---|
| 5 | while (_isspace(c = getc(fp))); |
---|
| 6 | return c; |
---|
| 7 | } |
---|
| 8 | |
---|
| 9 | _isspace(c) |
---|
| 10 | char c; |
---|
| 11 | { return ((c == ' ') || (c == '\t')); |
---|
| 12 | } |
---|
| 13 | |
---|
| 14 | scanf(s) |
---|
| 15 | char *s; |
---|
| 16 | {int i; |
---|
| 17 | i = _fscanf(stdin,s,(int *)&s+1); |
---|
| 18 | if (_fch && ((_ch == EOF) || (_ch == '\n'))) _fch = 0; |
---|
| 19 | return i; |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | fscanf(fp,s) |
---|
| 23 | FILE *fp; |
---|
| 24 | char *s; |
---|
| 25 | { return _fscanf(fp,s,(int *)&s+1); |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | _fscanf(fp,s,p) |
---|
| 29 | FILE *fp; |
---|
| 30 | char *s; |
---|
| 31 | int *p; |
---|
| 32 | {char c,ch; |
---|
| 33 | int m,n,r,x; |
---|
| 34 | n = 0; |
---|
| 35 | while(c = *s++) |
---|
| 36 | if (!_isspace(c)) |
---|
| 37 | if (c != '%') |
---|
| 38 | { if ((ch = _fskipspc(fp)) == EOF) return EOF; |
---|
| 39 | if (ch != c) return n; |
---|
| 40 | } |
---|
| 41 | else |
---|
| 42 | { if (x = (*s == '*')) ++s; |
---|
| 43 | if (isdigit(*s)) s += _getint(&m,s); |
---|
| 44 | else m = 32767; |
---|
| 45 | switch(c = *s++) |
---|
| 46 | {case 'd' : |
---|
| 47 | r = _atoin(fp,*p++,m,x); |
---|
| 48 | break; |
---|
| 49 | case 'o' : |
---|
| 50 | r = _otoin(fp,*p++,m,x); |
---|
| 51 | break; |
---|
| 52 | case 'x' : |
---|
| 53 | r = _xtoin(fp,*p++,m,x); |
---|
| 54 | break; |
---|
| 55 | case 'c' : |
---|
| 56 | r = _fgetc(fp,*p++,x); |
---|
| 57 | break; |
---|
| 58 | case 's' : |
---|
| 59 | r = _getstrn(fp,*p++,m,x); |
---|
| 60 | break; |
---|
| 61 | case '\0': |
---|
| 62 | return n; |
---|
| 63 | default: |
---|
| 64 | if ((ch = _fskipspc(fp)) == EOF) return EOF; |
---|
| 65 | if (ch != c) return n; |
---|
| 66 | continue; |
---|
| 67 | } |
---|
| 68 | if (!x) ++n; |
---|
| 69 | if (r == EOF) return EOF; |
---|
| 70 | if (r) return n; |
---|
| 71 | } |
---|
| 72 | return n; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | _atoin(fp,p,m,x) |
---|
| 76 | FILE *fp; |
---|
| 77 | int *p,m,x; |
---|
| 78 | {int i,s; |
---|
| 79 | char c; |
---|
| 80 | if (isdigit(c = _fskipspc(fp)) || (c == '-')) |
---|
| 81 | { if (s = (c == '-')) c = getc(fp); |
---|
| 82 | for (i = 0; isdigit(c) && --m >= 0; c = getc(fp)) |
---|
| 83 | i = i * 10 + c - '0'; |
---|
| 84 | ungetc(c,fp); |
---|
| 85 | if (!x) *p = (s ? -i : i); |
---|
| 86 | return 0; |
---|
| 87 | } |
---|
| 88 | return ((c == EOF) ? EOF : 1); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | _otoin(fp,p,m,x) |
---|
| 92 | FILE *fp; |
---|
| 93 | int *p,m,x; |
---|
| 94 | {int i; |
---|
| 95 | char c; |
---|
| 96 | if (isoct(c = _fskipspc(fp))) |
---|
| 97 | { for (i = 0; isoct(c) && --m >= 0; c = getc(fp)) |
---|
| 98 | i = i * 8 + c -'0'; |
---|
| 99 | ungetc(c,fp); |
---|
| 100 | if (!x) *p = i; |
---|
| 101 | return 0; |
---|
| 102 | } |
---|
| 103 | return ((c == EOF) ? EOF : 1); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | isoct(c) |
---|
| 107 | char c; |
---|
| 108 | { return ('0' <= c && c <= '7'); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | _xtoin(fp,p,m,x) |
---|
| 112 | FILE *fp; |
---|
| 113 | int *p,m,x; |
---|
| 114 | {int i; |
---|
| 115 | char c; |
---|
| 116 | if (ishex(c = _fskipspc(fp))) |
---|
| 117 | { for (i = 0; ishex(c) && --m >= 0; c = getc(fp)) |
---|
| 118 | if (isdigit(c)) i = i * 16 + c - '0'; |
---|
| 119 | else if ('A' <= (c = toupper(c)) && c <= 'F') |
---|
| 120 | i = i * 16 + c - ('A'-10); |
---|
| 121 | ungetc(c,fp); |
---|
| 122 | if (!x) *p = i; |
---|
| 123 | return 0; |
---|
| 124 | } |
---|
| 125 | return ((c == EOF) ? EOF : 1); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | ishex(c) |
---|
| 129 | char c; |
---|
| 130 | { return (isdigit(c) || ('A' <= (c = toupper(c)) && c <= 'F')); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | _fgetc(fp,p,x) |
---|
| 134 | FILE *fp; |
---|
| 135 | char *p; |
---|
| 136 | int x; |
---|
| 137 | {char c; |
---|
| 138 | if ((c = getc(fp)) == EOF) return EOF; |
---|
| 139 | if (!x) *p = c; |
---|
| 140 | return 0; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | _getstrn(fp,p,m,x) |
---|
| 144 | FILE *fp; |
---|
| 145 | char *p; |
---|
| 146 | int m,x; |
---|
| 147 | {char c,*q; |
---|
| 148 | if (((c = _fskipspc(fp)) == EOF) || (c == '\n')) return EOF; |
---|
| 149 | for (q = p; !_isspace(c) && --m >= 0; c = getc(fp)) |
---|
| 150 | { if ((c == EOF) || (c == '\n')) break; |
---|
| 151 | if ((c == '\b') && (q != p)) --q; |
---|
| 152 | else if (!x) *q++ = c; |
---|
| 153 | } |
---|
| 154 | ungetc(c,fp); |
---|
| 155 | if (!x) *q = '\0'; |
---|
| 156 | return 0; |
---|
| 157 | } |
---|