1 |
207 |
jeremybenn |
/*
|
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 |
|
|
FUNCTION
|
20 |
|
|
<<swscanf>>, <<fwscanf>>, <<wscanf>>---scan and format wide character input
|
21 |
|
|
|
22 |
|
|
INDEX
|
23 |
|
|
wscanf
|
24 |
|
|
INDEX
|
25 |
|
|
_wscanf_r
|
26 |
|
|
INDEX
|
27 |
|
|
fwscanf
|
28 |
|
|
INDEX
|
29 |
|
|
_fwscanf_r
|
30 |
|
|
INDEX
|
31 |
|
|
swscanf
|
32 |
|
|
INDEX
|
33 |
|
|
_swscanf_r
|
34 |
|
|
|
35 |
|
|
ANSI_SYNOPSIS
|
36 |
|
|
#include <stdio.h>
|
37 |
|
|
|
38 |
|
|
int wscanf(const wchar_t *<[format]>, ...);
|
39 |
|
|
int fwscanf(FILE *<[fd]>, const wchar_t *<[format]>, ...);
|
40 |
|
|
int swscanf(const wchar_t *<[str]>, const wchar_t *<[format]>, ...);
|
41 |
|
|
|
42 |
|
|
int _wscanf_r(struct _reent *<[ptr]>, const wchar_t *<[format]>, ...);
|
43 |
|
|
int _fwscanf_r(struct _reent *<[ptr]>, FILE *<[fd]>,
|
44 |
|
|
const wchar_t *<[format]>, ...);
|
45 |
|
|
int _swscanf_r(struct _reent *<[ptr]>, const wchar_t *<[str]>,
|
46 |
|
|
const wchar_t *<[format]>, ...);
|
47 |
|
|
|
48 |
|
|
|
49 |
|
|
TRAD_SYNOPSIS
|
50 |
|
|
#include <stdio.h>
|
51 |
|
|
|
52 |
|
|
int wscanf(<[format]> [, <[arg]>, ...])
|
53 |
|
|
wchar_t *<[format]>;
|
54 |
|
|
|
55 |
|
|
int fwscanf(<[fd]>, <[format]> [, <[arg]>, ...]);
|
56 |
|
|
FILE *<[fd]>;
|
57 |
|
|
wchar_t *<[format]>;
|
58 |
|
|
|
59 |
|
|
int swscanf(<[str]>, <[format]> [, <[arg]>, ...]);
|
60 |
|
|
wchar_t *<[str]>;
|
61 |
|
|
wchar_t *<[format]>;
|
62 |
|
|
|
63 |
|
|
int _wscanf_r(<[ptr]>, <[format]> [, <[arg]>, ...])
|
64 |
|
|
struct _reent *<[ptr]>;
|
65 |
|
|
wchar_t *<[format]>;
|
66 |
|
|
|
67 |
|
|
int _fwscanf_r(<[ptr]>, <[fd]>, <[format]> [, <[arg]>, ...]);
|
68 |
|
|
struct _reent *<[ptr]>;
|
69 |
|
|
FILE *<[fd]>;
|
70 |
|
|
wchar_t *<[format]>;
|
71 |
|
|
|
72 |
|
|
int _swscanf_r(<[ptr]>, <[str]>, <[format]> [, <[arg]>, ...]);
|
73 |
|
|
struct _reent *<[ptr]>;
|
74 |
|
|
wchar_t *<[str]>;
|
75 |
|
|
wchar_t *<[format]>;
|
76 |
|
|
|
77 |
|
|
|
78 |
|
|
DESCRIPTION
|
79 |
|
|
<<wscanf>> scans a series of input fields from standard input,
|
80 |
|
|
one wide character at a time. Each field is interpreted according to
|
81 |
|
|
a format specifier passed to <<wscanf>> in the format string at
|
82 |
|
|
<<*<[format]>>>. <<wscanf>> stores the interpreted input from
|
83 |
|
|
each field at the address passed to it as the corresponding argument
|
84 |
|
|
following <[format]>. You must supply the same number of
|
85 |
|
|
format specifiers and address arguments as there are input fields.
|
86 |
|
|
|
87 |
|
|
There must be sufficient address arguments for the given format
|
88 |
|
|
specifiers; if not the results are unpredictable and likely
|
89 |
|
|
disasterous. Excess address arguments are merely ignored.
|
90 |
|
|
|
91 |
|
|
<<wscanf>> often produces unexpected results if the input diverges from
|
92 |
|
|
an expected pattern. Since the combination of <<gets>> or <<fgets>>
|
93 |
|
|
followed by <<swscanf>> is safe and easy, that is the preferred way
|
94 |
|
|
to be certain that a program is synchronized with input at the end
|
95 |
|
|
of a line.
|
96 |
|
|
|
97 |
|
|
<<fwscanf>> and <<swscanf>> are identical to <<wscanf>>, other than the
|
98 |
|
|
source of input: <<fwscanf>> reads from a file, and <<swscanf>>
|
99 |
|
|
from a string.
|
100 |
|
|
|
101 |
|
|
The routines <<_wscanf_r>>, <<_fwscanf_r>>, and <<_swscanf_r>> are reentrant
|
102 |
|
|
versions of <<wscanf>>, <<fwscanf>>, and <<swscanf>> that take an additional
|
103 |
|
|
first argument pointing to a reentrancy structure.
|
104 |
|
|
|
105 |
|
|
The string at <<*<[format]>>> is a wide character sequence composed
|
106 |
|
|
of zero or more directives. Directives are composed of
|
107 |
|
|
one or more whitespace characters, non-whitespace characters,
|
108 |
|
|
and format specifications.
|
109 |
|
|
|
110 |
|
|
Whitespace characters are blank (<< >>), tab (<<\t>>), or
|
111 |
|
|
newline (<<\n>>).
|
112 |
|
|
When <<wscanf>> encounters a whitespace character in the format string
|
113 |
|
|
it will read (but not store) all consecutive whitespace characters
|
114 |
|
|
up to the next non-whitespace character in the input.
|
115 |
|
|
|
116 |
|
|
Non-whitespace characters are all other ASCII characters except the
|
117 |
|
|
percent sign (<<%>>). When <<wscanf>> encounters a non-whitespace
|
118 |
|
|
character in the format string it will read, but not store
|
119 |
|
|
a matching non-whitespace character.
|
120 |
|
|
|
121 |
|
|
Format specifications tell <<wscanf>> to read and convert characters
|
122 |
|
|
from the input field into specific types of values, and store then
|
123 |
|
|
in the locations specified by the address arguments.
|
124 |
|
|
|
125 |
|
|
Trailing whitespace is left unread unless explicitly
|
126 |
|
|
matched in the format string.
|
127 |
|
|
|
128 |
|
|
The format specifiers must begin with a percent sign (<<%>>)
|
129 |
|
|
and have the following form:
|
130 |
|
|
|
131 |
|
|
. %[*][<[width]>][<[size]>]<[type]>
|
132 |
|
|
|
133 |
|
|
Each format specification begins with the percent character (<<%>>).
|
134 |
|
|
The other fields are:
|
135 |
|
|
o+
|
136 |
|
|
o *
|
137 |
|
|
an optional marker; if present, it suppresses interpretation and
|
138 |
|
|
assignment of this input field.
|
139 |
|
|
|
140 |
|
|
o <[width]>
|
141 |
|
|
an optional maximum field width: a decimal integer,
|
142 |
|
|
which controls the maximum number of characters that
|
143 |
|
|
will be read before converting the current input field. If the
|
144 |
|
|
input field has fewer than <[width]> characters, <<wscanf>>
|
145 |
|
|
reads all the characters in the field, and then
|
146 |
|
|
proceeds with the next field and its format specification.
|
147 |
|
|
|
148 |
|
|
If a whitespace or a non-convertable wide character occurs
|
149 |
|
|
before <[width]> character are read, the characters up
|
150 |
|
|
to that character are read, converted, and stored.
|
151 |
|
|
Then <<wscanf>> proceeds to the next format specification.
|
152 |
|
|
|
153 |
|
|
o size
|
154 |
|
|
<<h>>, <<j>>, <<l>>, <<L>>, <<t>>, and <<z>> are optional size
|
155 |
|
|
characters which override the default way that <<wscanf>>
|
156 |
|
|
interprets the data type of the corresponding argument.
|
157 |
|
|
|
158 |
|
|
|
159 |
|
|
.Modifier Type(s)
|
160 |
|
|
. hh d, i, o, u, x, n convert input to char,
|
161 |
|
|
. store in char object
|
162 |
|
|
.
|
163 |
|
|
. h d, i, o, u, x, n convert input to short,
|
164 |
|
|
. store in short object
|
165 |
|
|
.
|
166 |
|
|
. h e, f, c, s, p no effect
|
167 |
|
|
.
|
168 |
|
|
. j d, i, o, u, x, n convert input to intmax_t,
|
169 |
|
|
. store in intmax_t object
|
170 |
|
|
.
|
171 |
|
|
. j all others no effect
|
172 |
|
|
.
|
173 |
|
|
. l d, i, o, u, x, n convert input to long,
|
174 |
|
|
. store in long object
|
175 |
|
|
.
|
176 |
|
|
. l e, f, g convert input to double
|
177 |
|
|
. store in a double object
|
178 |
|
|
.
|
179 |
|
|
. l c, s, [ the input is stored in a wchar_t object
|
180 |
|
|
.
|
181 |
|
|
. l p no effect
|
182 |
|
|
.
|
183 |
|
|
. ll d, i, o, u, x, n convert to long long,
|
184 |
|
|
. store in long long
|
185 |
|
|
.
|
186 |
|
|
. L d, i, o, u, x, n convert to long long,
|
187 |
|
|
. store in long long
|
188 |
|
|
.
|
189 |
|
|
. L e, f, g, E, G convert to long double,
|
190 |
|
|
. store in long double
|
191 |
|
|
.
|
192 |
|
|
. L all others no effect
|
193 |
|
|
.
|
194 |
|
|
. t d, i, o, u, x, n convert input to ptrdiff_t,
|
195 |
|
|
. store in ptrdiff_t object
|
196 |
|
|
.
|
197 |
|
|
. t all others no effect
|
198 |
|
|
.
|
199 |
|
|
. z d, i, o, u, x, n convert input to size_t,
|
200 |
|
|
. store in size_t object
|
201 |
|
|
.
|
202 |
|
|
. z all others no effect
|
203 |
|
|
.
|
204 |
|
|
|
205 |
|
|
|
206 |
|
|
o <[type]>
|
207 |
|
|
|
208 |
|
|
A character to specify what kind of conversion
|
209 |
|
|
<<wscanf>> performs. Here is a table of the conversion
|
210 |
|
|
characters:
|
211 |
|
|
|
212 |
|
|
o+
|
213 |
|
|
o %
|
214 |
|
|
No conversion is done; the percent character (<<%>>) is stored.
|
215 |
|
|
|
216 |
|
|
o c
|
217 |
|
|
Scans one wide character. Corresponding <[arg]>: <<(char *arg)>>.
|
218 |
|
|
Otherwise, if an <<l>> specifier is present, the corresponding
|
219 |
|
|
<[arg]> is a <<(wchar_t *arg)>>.
|
220 |
|
|
|
221 |
|
|
o s
|
222 |
|
|
Reads a character string into the array supplied.
|
223 |
|
|
Corresponding <[arg]>: <<(char arg[])>>.
|
224 |
|
|
If an <<l>> specifier is present, the corresponding <[arg]> is a <<(wchar_t *arg)>>.
|
225 |
|
|
|
226 |
|
|
o [<[pattern]>]
|
227 |
|
|
Reads a non-empty character string into memory
|
228 |
|
|
starting at <[arg]>. This area must be large
|
229 |
|
|
enough to accept the sequence and a
|
230 |
|
|
terminating null character which will be added
|
231 |
|
|
automatically. (<[pattern]> is discussed in the paragraph following
|
232 |
|
|
this table). Corresponding <[arg]>: <<(char *arg)>>.
|
233 |
|
|
If an <<l>> specifier is present, the corresponding <[arg]> is
|
234 |
|
|
a <<(wchar_t *arg)>>.
|
235 |
|
|
|
236 |
|
|
o d
|
237 |
|
|
Reads a decimal integer into the corresponding <[arg]>: <<(int *arg)>>.
|
238 |
|
|
|
239 |
|
|
o o
|
240 |
|
|
Reads an octal integer into the corresponding <[arg]>: <<(int *arg)>>.
|
241 |
|
|
|
242 |
|
|
o u
|
243 |
|
|
Reads an unsigned decimal integer into the corresponding
|
244 |
|
|
<[arg]>: <<(unsigned int *arg)>>.
|
245 |
|
|
|
246 |
|
|
o x,X
|
247 |
|
|
Read a hexadecimal integer into the corresponding <[arg]>:
|
248 |
|
|
<<(int *arg)>>.
|
249 |
|
|
|
250 |
|
|
o e, f, g
|
251 |
|
|
Read a floating-point number into the corresponding <[arg]>:
|
252 |
|
|
<<(float *arg)>>.
|
253 |
|
|
|
254 |
|
|
o E, F, G
|
255 |
|
|
Read a floating-point number into the corresponding <[arg]>:
|
256 |
|
|
<<(double *arg)>>.
|
257 |
|
|
|
258 |
|
|
o i
|
259 |
|
|
Reads a decimal, octal or hexadecimal integer into the
|
260 |
|
|
corresponding <[arg]>: <<(int *arg)>>.
|
261 |
|
|
|
262 |
|
|
o n
|
263 |
|
|
Stores the number of characters read in the corresponding
|
264 |
|
|
<[arg]>: <<(int *arg)>>.
|
265 |
|
|
|
266 |
|
|
o p
|
267 |
|
|
Stores a scanned pointer. ANSI C leaves the details
|
268 |
|
|
to each implementation; this implementation treats
|
269 |
|
|
<<%p>> exactly the same as <<%U>>. Corresponding
|
270 |
|
|
<[arg]>: <<(void **arg)>>.
|
271 |
|
|
o-
|
272 |
|
|
|
273 |
|
|
A <[pattern]> of characters surrounded by square brackets can be used
|
274 |
|
|
instead of the <<s>> type character. <[pattern]> is a set of
|
275 |
|
|
characters which define a search set of possible characters making up
|
276 |
|
|
the <<wscanf>> input field. If the first character in the brackets is a
|
277 |
|
|
caret (<<^>>), the search set is inverted to include all ASCII characters
|
278 |
|
|
except those between the brackets. There is no range facility as is
|
279 |
|
|
defined in the corresponding non-wide character scanf functions.
|
280 |
|
|
Ranges are not part of the POSIX standard.
|
281 |
|
|
|
282 |
|
|
Here are some <[pattern]> examples:
|
283 |
|
|
o+
|
284 |
|
|
o %[abcd]
|
285 |
|
|
matches wide character strings containing only
|
286 |
|
|
<<a>>, <<b>>, <<c>>, and <<d>>.
|
287 |
|
|
|
288 |
|
|
o %[^abcd]
|
289 |
|
|
matches wide character strings containing any characters except
|
290 |
|
|
<<a>>, <<b>>, <<c>>, or <<d>>.
|
291 |
|
|
|
292 |
|
|
o %[A-DW-Z]
|
293 |
|
|
Note: No wide character ranges, so this expression matches wide
|
294 |
|
|
character strings containing <<A>>, <<->>, <<D>>, <<W>>, <<Z>>.
|
295 |
|
|
o-
|
296 |
|
|
|
297 |
|
|
Floating point numbers (for field types <<e>>, <<f>>, <<g>>, <<E>>,
|
298 |
|
|
<<F>>, <<G>>) must correspond to the following general form:
|
299 |
|
|
|
300 |
|
|
. [+/-] ddddd[.]ddd [E|e[+|-]ddd]
|
301 |
|
|
|
302 |
|
|
where objects inclosed in square brackets are optional, and <<ddd>>
|
303 |
|
|
represents decimal, octal, or hexadecimal digits.
|
304 |
|
|
o-
|
305 |
|
|
|
306 |
|
|
RETURNS
|
307 |
|
|
<<wscanf>> returns the number of input fields successfully
|
308 |
|
|
scanned, converted and stored; the return value does
|
309 |
|
|
not include scanned fields which were not stored.
|
310 |
|
|
|
311 |
|
|
If <<wscanf>> attempts to read at end-of-file, the return
|
312 |
|
|
value is <<EOF>>.
|
313 |
|
|
|
314 |
|
|
If no fields were stored, the return value is <<0>>.
|
315 |
|
|
|
316 |
|
|
<<wscanf>> might stop scanning a particular field before
|
317 |
|
|
reaching the normal field end character, or may
|
318 |
|
|
terminate entirely.
|
319 |
|
|
|
320 |
|
|
<<wscanf>> stops scanning and storing the current field
|
321 |
|
|
and moves to the next input field (if any)
|
322 |
|
|
in any of the following situations:
|
323 |
|
|
|
324 |
|
|
O+
|
325 |
|
|
o The assignment suppressing character (<<*>>) appears
|
326 |
|
|
after the <<%>> in the format specification; the current
|
327 |
|
|
input field is scanned but not stored.
|
328 |
|
|
|
329 |
|
|
o <[width]> characters have been read (<[width]> is a
|
330 |
|
|
width specification, a positive decimal integer).
|
331 |
|
|
|
332 |
|
|
o The next wide character read cannot be converted
|
333 |
|
|
under the the current format (for example,
|
334 |
|
|
if a <<Z>> is read when the format is decimal).
|
335 |
|
|
|
336 |
|
|
o The next wide character in the input field does not appear
|
337 |
|
|
in the search set (or does appear in the inverted search set).
|
338 |
|
|
O-
|
339 |
|
|
|
340 |
|
|
When <<wscanf>> stops scanning the current input field for one of
|
341 |
|
|
these reasons, the next character is considered unread and
|
342 |
|
|
used as the first character of the following input field, or the
|
343 |
|
|
first character in a subsequent read operation on the input.
|
344 |
|
|
|
345 |
|
|
<<wscanf>> will terminate under the following circumstances:
|
346 |
|
|
|
347 |
|
|
O+
|
348 |
|
|
o The next wide character in the input field conflicts
|
349 |
|
|
with a corresponding non-whitespace character in the
|
350 |
|
|
format string.
|
351 |
|
|
|
352 |
|
|
o The next wide character in the input field is <<WEOF>>.
|
353 |
|
|
|
354 |
|
|
o The format string has been exhausted.
|
355 |
|
|
O-
|
356 |
|
|
|
357 |
|
|
When the format string contains a wide character sequence that is
|
358 |
|
|
not part of a format specification, the same wide character
|
359 |
|
|
sequence must appear in the input; <<wscanf>> will
|
360 |
|
|
scan but not store the matched characters. If a
|
361 |
|
|
conflict occurs, the first conflicting wide character remains in the
|
362 |
|
|
input as if it had never been read.
|
363 |
|
|
|
364 |
|
|
PORTABILITY
|
365 |
|
|
<<wscanf>> is C99, POSIX-1.2008.
|
366 |
|
|
|
367 |
|
|
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
|
368 |
|
|
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
|
369 |
|
|
*/
|
370 |
|
|
|
371 |
|
|
#include <_ansi.h>
|
372 |
|
|
#include <reent.h>
|
373 |
|
|
#include <stdio.h>
|
374 |
|
|
#include <wchar.h>
|
375 |
|
|
#include <stdarg.h>
|
376 |
|
|
#include "local.h"
|
377 |
|
|
|
378 |
|
|
#ifndef _REENT_ONLY
|
379 |
|
|
|
380 |
|
|
int
|
381 |
|
|
swscanf (_CONST wchar_t *str, _CONST wchar_t *fmt, ...)
|
382 |
|
|
{
|
383 |
|
|
int ret;
|
384 |
|
|
va_list ap;
|
385 |
|
|
FILE f;
|
386 |
|
|
|
387 |
|
|
f._flags = __SRD | __SSTR;
|
388 |
|
|
f._bf._base = f._p = (unsigned char *) str;
|
389 |
|
|
f._bf._size = f._r = wcslen (str) * sizeof (wchar_t);
|
390 |
|
|
f._read = __seofread;
|
391 |
|
|
f._ub._base = NULL;
|
392 |
|
|
f._lb._base = NULL;
|
393 |
|
|
f._file = -1; /* No file. */
|
394 |
|
|
va_start (ap, fmt);
|
395 |
|
|
ret = __ssvfwscanf_r (_REENT, &f, fmt, ap);
|
396 |
|
|
va_end (ap);
|
397 |
|
|
return ret;
|
398 |
|
|
}
|
399 |
|
|
|
400 |
|
|
#endif /* !_REENT_ONLY */
|
401 |
|
|
|
402 |
|
|
int
|
403 |
|
|
_swscanf_r (struct _reent *ptr, _CONST wchar_t *str, _CONST wchar_t *fmt, ...)
|
404 |
|
|
{
|
405 |
|
|
int ret;
|
406 |
|
|
va_list ap;
|
407 |
|
|
FILE f;
|
408 |
|
|
|
409 |
|
|
f._flags = __SRD | __SSTR;
|
410 |
|
|
f._bf._base = f._p = (unsigned char *) str;
|
411 |
|
|
f._bf._size = f._r = wcslen (str) * sizeof (wchar_t);
|
412 |
|
|
f._read = __seofread;
|
413 |
|
|
f._ub._base = NULL;
|
414 |
|
|
f._lb._base = NULL;
|
415 |
|
|
f._file = -1; /* No file. */
|
416 |
|
|
va_start (ap, fmt);
|
417 |
|
|
ret = __ssvfwscanf_r (ptr, &f, fmt, ap);
|
418 |
|
|
va_end (ap);
|
419 |
|
|
return ret;
|
420 |
|
|
}
|