File: scanf.5c

package info (click to toggle)
nickle 2.68-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,336 kB
  • ctags: 3,288
  • sloc: ansic: 31,198; yacc: 1,860; lex: 858; sh: 830; makefile: 229
file content (214 lines) | stat: -rw-r--r-- 3,698 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * Scanf code.  Extend File namespace with reading code
 */
extend namespace File {
    public int fscanf (file f, string format, *poly args ...)
	/*
	 * According to 'format', read from 'f' to 'args'
	 */
    {
	/* Skip whitespace */
	void whitespace ()
	{
	    int c;

	    while (!end (f))
		if (!Ctype::isspace (c = getc (f))) {
		    ungetc (c, f);
		    break;
		}
	}

	bool isbinary (int c)
	{
	    if ('0' <= c && c <= '1')
		return true;
	    if (c == '-')
		return true;
	    return false;
	}
	
	bool isoctal (int c)
	{
	    if ('0' <= c && c <= '7')
		return true;
	    if (c == '-')
		return true;
	    return false;
	}
	
	bool isdecimal (int c)
	{
	    if ('0' <= c && c <= '9')
		return true;
	    if (c == '-')
		return true;
	    return false;
	}

	bool ishex (int c)
	{
	    if ('0' <= c && c <= '9')
		return true;
	    if ('a' <= c && c <= 'f')
		return true;
	    if ('A' <= c && c <= 'F')
		return true;
	    if (c == '-')
		return true;
	    return false;
	}

	bool isfloat (int c)
	{
	    if ('0' <= c && c <= '9')
		return true;
	    if (c == 'e')
		return true;
	    if (c == '-')
		return true;
	    if (c == '.')
		return true;
	    if (c == '{' || c == '}')
		return true;
	    return false;
	}

	/* return next integer in input */
	int integer (bool(int c) test, int base)
	{
	    int	    c;
	    string  s = "";

	    whitespace();
	    while (!end (f)) {
		if (!test (c = getc (f))) {
		    ungetc (c, f);
		    break;
		}
		s = s + String::new(c);
	    }
	    return string_to_integer (s, base);
	}

	/* return next number in input */
	real number (bool(int c) test)
	{
	    int	    c;
	    string  s = "";

	    whitespace();
	    while (!end (f)) {
		if (!test (c = getc (f))) {
		    ungetc (c, f);
		    break;
		}
		s = s + String::new(c);
	    }
	    return string_to_real (s);
	}

	string word ()
	{
	    whitespace();
	    string  s = "";
	    while (!end (f)) {
		int c = getc(f);
		if (!Ctype::isgraph (c)) {
		    ungetc (c, f);
		    break;
		}
		s = s + String::new(c);
	    }
	    return s;
	}

	int	i = 0;
	int	argc = 0;
	int	c;

	while (i < String::length (format) && !end(f) && !error(f))
	{
	    switch (format[i]) {
	    case ' ':
	    case '\t':
		whitespace ();
		break;
	    case '%':
		i++;
		switch (format[i]) {
		case 'b': case 'B':
		    *args[argc++] = integer (isbinary, 2);
		    break;
		case 'o': case 'O':
		    *args[argc++] = integer (isoctal, 8);
		    break;
		case 'd': case 'D':
		    *args[argc++] = integer (isdecimal, 10);
		    break;
		case 'x': case 'X':
		    *args[argc++] = integer (ishex, 16);
		    break;
		case 'e': case 'E':
		case 'f': case 'F':
		case 'g': case 'G':
		    *args[argc++] = number(isfloat);
		    break;
		case 'c':
		    *args[argc++] = getc(f);
		    break;
		case 's':
		    *args[argc++] = word();
		    break;
		default:
		    if (end (f))
			return argc;
		    c = getc(f);
		    if (c != format[i]) {
			ungetc (c, f);
			return argc;
		    }
		    break;
		}
		break;
	    default:
		if (end (f))
		    return argc;
		c = getc(f);
		if (c != format[i]) {
		    ungetc (c, f);
		    return argc;
		}
		break;
	    }
	    i++;
	}
	return argc;
    }

    public int sscanf (string s, string format, *poly args...)
	/*
	 * According to 'format', read from 's' to 'args'
	 */
    {
        file sf = string_read(s);
        int n = fscanf (sf, format, args ...);
        close(sf);
        return n;
    }

    public namespace ScanfGlobals {
	
	public int scanf (string format, *poly args...)
	    /*
	     * According to 'format', read from stdin to 'args'
	     */
	{
	    return fscanf (stdin, format, args ...);
	}

    }

    public import ScanfGlobals;
}
import File::ScanfGlobals;