OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [branches/] [newlib/] [newlib/] [newlib/] [libc/] [stdio/] [sscanf.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 39 lampret
/*
2
 * Copyright (c) 1990 The Regents of the University of California.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms are permitted
6
 * provided that the above copyright notice and this paragraph are
7
 * duplicated in all such forms and that any documentation,
8
 * advertising materials, and other materials related to such
9
 * distribution and use acknowledge that the software was developed
10
 * by the University of California, Berkeley.  The name of the
11
 * University may not be used to endorse or promote products derived
12
 * from this software without specific prior written permission.
13
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16
 */
17
 
18
/*
19
 
20
FUNCTION
21
        <<scanf>>, <<fscanf>>, <<sscanf>>---scan and format input
22
 
23
INDEX
24
        scanf
25
INDEX
26
        fscanf
27
INDEX
28
        sscanf
29
 
30
ANSI_SYNOPSIS
31
        #include <stdio.h>
32
 
33
        int scanf(const char *<[format]> [, <[arg]>, ...]);
34
        int fscanf(FILE *<[fd]>, const char *<[format]> [, <[arg]>, ...]);
35
        int sscanf(const char *<[str]>, const char *<[format]>
36
                   [, <[arg]>, ...]);
37
 
38
 
39
TRAD_SYNOPSIS
40
        #include <stdio.h>
41
 
42
        int scanf(<[format]> [, <[arg]>, ...])
43
        char *<[format]>;
44
 
45
        int fscanf(<[fd]>, <[format]> [, <[arg]>, ...]);
46
        FILE *<[fd]>;
47
        char *<[format]>;
48
 
49
        int sscanf(<[str]>, <[format]> [, <[arg]>, ...]);
50
        char *<[str]>;
51
        char *<[format]>;
52
 
53
 
54
DESCRIPTION
55
        <<scanf>> scans a series of input fields from standard input,
56
                one character at a time.  Each field is interpreted according to
57
                a format specifier passed to <<scanf>> in the format string at
58
        <<*<[format]>>>.  <<scanf>> stores the interpreted input from
59
                each field at the address passed to it as the corresponding argument
60
                following <[format]>.  You must supply the same number of
61
                format specifiers and address arguments as there are input fields.
62
 
63
        There must be sufficient address arguments for the given format
64
        specifiers; if not the results are unpredictable and likely
65
        disasterous.  Excess address arguments are merely ignored.
66
 
67
        <<scanf>> often produces unexpected results if the input diverges from
68
        an expected pattern. Since the combination of <<gets>> or <<fgets>>
69
        followed by <<sscanf>> is safe and easy, that is the preferred way
70
        to be certain that a program is synchronized with input at the end
71
                of a line.
72
 
73
        <<fscanf>> and <<sscanf>> are identical to <<scanf>>, other than the
74
        source of input: <<fscanf>> reads from a file, and <<sscanf>>
75
                from a string.
76
 
77
        The string at <<*<[format]>>> is a character sequence composed
78
        of zero or more directives. Directives are composed of
79
        one or more whitespace characters, non-whitespace characters,
80
        and format specifications.
81
 
82
        Whitespace characters are blank (<< >>), tab (<<\t>>), or
83
                newline (<<\n>>).
84
        When <<scanf>> encounters a whitespace character in the format string
85
        it will read (but not store) all consecutive whitespace characters
86
        up to the next non-whitespace character in the input.
87
 
88
        Non-whitespace characters are all other ASCII characters except the
89
        percent sign (<<%>>).  When <<scanf>> encounters a non-whitespace
90
        character in the format string it will read, but not store
91
        a matching non-whitespace character.
92
 
93
        Format specifications tell <<scanf>> to read and convert characters
94
        from the input field into specific types of values, and store then
95
        in the locations specified by the address arguments.
96
 
97
        Trailing whitespace is left unread unless explicitly
98
        matched in the format string.
99
 
100
        The format specifiers must begin with a percent sign (<<%>>)
101
        and have the following form:
102
 
103
.       %[*][<[width]>][<[size]>]<[type]>
104
 
105
        Each format specification begins with the percent character (<<%>>).
106
        The other fields are:
107
        o+
108
                o *
109
                an optional marker; if present, it suppresses interpretation and
110
        assignment of this input field.
111
 
112
        o <[width]>
113
                an optional maximum field width: a decimal integer,
114
                which controls the maximum number of characters that
115
                will be read before converting the current input field.  If the
116
                input field has fewer than <[width]> characters, <<scanf>>
117
                reads all the characters in the field, and then
118
                proceeds with the next field and its format specification.
119
 
120
                If a whitespace or a non-convertable character occurs
121
                before <[width]> character are read, the characters up
122
                to that character are read, converted, and stored.
123
                Then <<scanf>> proceeds to the next format specification.
124
 
125
        o size
126
                <<h>>, <<l>>, and <<L>> are optional size characters which
127
                override the default way that <<scanf>> interprets the
128
                data type of the corresponding argument.
129
 
130
 
131
.Modifier   Type(s)
132
.   h       d, i, o, u, x     convert input to short,
133
.                             store in short object
134
.
135
.   h       D, I, O, U, X     no effect
136
.           e, f, c, s, n, p
137
.
138
.   l       d, i, o, u, x     convert input to long,
139
.                             store in long object
140
.
141
.   l       e, f, g           convert input to double
142
.                             store in a double object
143
.
144
.   l       D, I, O, U, X     no effect
145
.           c, s, n, p
146
.
147
.   L       d, i, o, u, x     convert to long double,
148
.                             store in long double
149
.
150
.   L      all others         no effect
151
 
152
 
153
        o <[type]>
154
 
155
                A character to specify what kind of conversion
156
                <<scanf>> performs.  Here is a table of the conversion
157
                characters:
158
 
159
                o+
160
                o  %
161
                No conversion is done; the percent character (<<%>>) is stored.
162
 
163
                o c
164
                Scans one character.  Corresponding <[arg]>: <<(char *arg)>>.
165
 
166
                o s
167
                Reads a character string into the array supplied.
168
                Corresponding <[arg]>: <<(char arg[])>>.
169
 
170
                o  [<[pattern]>]
171
                Reads a non-empty character string into memory
172
                starting at <[arg]>.  This area must be large
173
                enough to accept the sequence and a
174
                terminating null character which will be added
175
                automatically.  (<[pattern]> is discussed in the paragraph following
176
                this table). Corresponding <[arg]>: <<(char *arg)>>.
177
 
178
                o d
179
                Reads a decimal integer into the corresponding <[arg]>: <<(int *arg)>>.
180
 
181
                o D
182
                Reads a decimal integer into the corresponding
183
                <[arg]>: <<(long *arg)>>.
184
 
185
                o o
186
                Reads an octal integer into the corresponding <[arg]>: <<(int *arg)>>.
187
 
188
                o O
189
                Reads an octal integer into the corresponding <[arg]>: <<(long *arg)>>.
190
 
191
                o u
192
                Reads an unsigned decimal integer into the corresponding
193
                <[arg]>: <<(unsigned int *arg)>>.
194
 
195
 
196
                o U
197
                Reads an unsigned decimal integer into the corresponding <[arg]>:
198
                <<(unsigned long *arg)>>.
199
 
200
                o x,X
201
                Read a hexadecimal integer into the corresponding <[arg]>:
202
                <<(int *arg)>>.
203
 
204
                o e, f, g
205
                Read a floating point number into the corresponding <[arg]>:
206
                <<(float *arg)>>.
207
 
208
                o E, F, G
209
                Read a floating point number into the corresponding <[arg]>:
210
                <<(double *arg)>>.
211
 
212
                o  i
213
                Reads a decimal, octal or hexadecimal integer into the
214
                corresponding <[arg]>: <<(int *arg)>>.
215
 
216
                o  I
217
                Reads a decimal, octal or hexadecimal integer into the
218
                corresponding <[arg]>: <<(long *arg)>>.
219
 
220
                o  n
221
                Stores the number of characters read in the corresponding
222
                <[arg]>: <<(int *arg)>>.
223
 
224
                o  p
225
                Stores a scanned pointer.  ANSI C leaves the details
226
                to each implementation; this implementation treats
227
                <<%p>> exactly the same as <<%U>>.  Corresponding
228
                <[arg]>: <<(void **arg)>>.
229
                o-
230
 
231
        A <[pattern]> of characters surrounded by square brackets can be used
232
        instead of the <<s>> type character.  <[pattern]> is a set of
233
        characters which define a search set of possible characters making up
234
        the <<scanf>> input field.  If the first character in the brackets is a
235
        caret (<<^>>), the search set is inverted to include all ASCII characters
236
        except those between the brackets.  There is also a range facility
237
        which you can use as a shortcut. <<%[0-9] >> matches all decimal digits.
238
        The hyphen must not be the first or last character in the set.
239
        The character prior to the hyphen must be lexically less than the
240
        character after it.
241
 
242
        Here are some <[pattern]> examples:
243
                o+
244
                o %[abcd]
245
                matches strings containing only <<a>>, <<b>>, <<c>>, and <<d>>.
246
 
247
                o %[^abcd]
248
                matches strings containing any characters except <<a>>, <<b>>,
249
                <<c>>, or <<d>>
250
 
251
                o %[A-DW-Z]
252
                matches strings containing <<A>>, <<B>>, <<C>>, <<D>>, <<W>>,
253
                <<X>>, <<Y>>, <<Z>>
254
 
255
                o %[z-a]
256
                matches the characters  <<z>>, <<->>, and <<a>>
257
                o-
258
 
259
        Floating point numbers (for field types <<e>>, <<f>>, <<g>>, <<E>>,
260
        <<F>>, <<G>>) must correspond to the following general form:
261
 
262
.               [+/-] ddddd[.]ddd [E|e[+|-]ddd]
263
 
264
        where objects inclosed in square brackets are optional, and <<ddd>>
265
        represents decimal, octal, or hexadecimal digits.
266
        o-
267
 
268
RETURNS
269
        <<scanf>> returns the number of input fields successfully
270
        scanned, converted and stored; the return value does
271
        not include scanned fields which were not stored.
272
 
273
        If <<scanf>> attempts to read at end-of-file, the return
274
        value is <<EOF>>.
275
 
276
        If no fields were stored, the return value is <<0>>.
277
 
278
        <<scanf>> might stop scanning a particular field before
279
        reaching the normal field end character, or may
280
        terminate entirely.
281
 
282
        <<scanf>> stops scanning and storing the current field
283
        and moves to the next input field (if any)
284
        in any of the following situations:
285
 
286
        O+
287
        o       The assignment suppressing character (<<*>>) appears
288
        after the <<%>> in the format specification; the current
289
        input field is scanned but not stored.
290
 
291
        o       <[width]> characters have been read (<[width]> is a
292
        width specification, a positive decimal integer).
293
 
294
        o       The next character read cannot be converted
295
        under the the current format (for example,
296
        if a <<Z>> is read when the format is decimal).
297
 
298
        o       The next character in the input field does not appear
299
        in the search set (or does appear in the inverted search set).
300
        O-
301
 
302
        When <<scanf>> stops scanning the current input field for one of
303
        these reasons, the next character is considered unread and
304
        used as the first character of the following input field, or the
305
        first character in a subsequent read operation on the input.
306
 
307
        <<scanf>> will terminate under the following circumstances:
308
 
309
        O+
310
        o       The next character in the input field conflicts
311
        with a corresponding non-whitespace character in the
312
        format string.
313
 
314
        o       The next character in the input field is <<EOF>>.
315
 
316
        o       The format string has been exhausted.
317
        O-
318
 
319
        When the format string contains a character sequence that is
320
        not part of a format specification, the same character
321
        sequence must appear in the input; <<scanf>> will
322
        scan but not store the matched characters.  If a
323
        conflict occurs, the first conflicting character remains in the input
324
        as if it had never been read.
325
 
326
PORTABILITY
327
<<scanf>> is ANSI C.
328
 
329
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
330
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
331
*/
332
 
333
#include <_ansi.h>
334
#include <reent.h>
335
#include <stdio.h>
336
#include <string.h>
337
#ifdef _HAVE_STDC
338
#include <stdarg.h>
339
#else
340
#include <varargs.h>
341
#endif
342
#include "local.h"
343
 
344
/* | ARGSUSED */
345
/*SUPPRESS 590*/
346
static
347
int
348
eofread (cookie, buf, len)
349
     _PTR cookie;
350
     char *buf;
351
     int len;
352
{
353
  return 0;
354
}
355
 
356
#ifdef _HAVE_STDC
357
int
358
_DEFUN (sscanf, (str, fmt), _CONST char *str _AND _CONST char *fmt _DOTS)
359
#else
360
int
361
sscanf (str, fmt, va_alist)
362
     _CONST char *str;
363
     _CONST char *fmt;
364
     va_dcl
365
#endif
366
{
367
  int ret;
368
  va_list ap;
369
  FILE f;
370
 
371
  f._flags = __SRD;
372
  f._bf._base = f._p = (unsigned char *) str;
373
  f._bf._size = f._r = strlen (str);
374
  f._read = eofread;
375
  f._ub._base = NULL;
376
  f._lb._base = NULL;
377
  f._data = _REENT;
378
#ifdef _HAVE_STDC
379
  va_start (ap, fmt);
380
#else
381
  va_start (ap);
382
#endif
383
  ret = __svfscanf (&f, fmt, ap);
384
  va_end (ap);
385
  return ret;
386
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.