1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LSTDIO/SSCANF.C Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,404 @@
1.4 +/* SSCANF.C
1.5 + *
1.6 + * Portions Copyright (c) 1990-2006 Nokia Corporation and/or its subsidiary(-ies).
1.7 + * All rights reserved.
1.8 + */
1.9 +
1.10 +/*
1.11 + * Copyright (c) 1990 The Regents of the University of California.
1.12 + * All rights reserved.
1.13 + *
1.14 + * Redistribution and use in source and binary forms are permitted
1.15 + * provided that the above copyright notice and this paragraph are
1.16 + * duplicated in all such forms and that any documentation,
1.17 + * advertising materials, and other materials related to such
1.18 + * distribution and use acknowledge that the software was developed
1.19 + * by the University of California, Berkeley. The name of the
1.20 + * University may not be used to endorse or promote products derived
1.21 + * from this software without specific prior written permission.
1.22 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1.23 + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1.24 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1.25 + */
1.26 +
1.27 +/*
1.28 +
1.29 +FUNCTION
1.30 + <<scanf>>, <<fscanf>>, <<sscanf>>---scan and format input
1.31 +
1.32 +INDEX
1.33 + scanf
1.34 +INDEX
1.35 + fscanf
1.36 +INDEX
1.37 + sscanf
1.38 +
1.39 +ANSI_SYNOPSIS
1.40 + #include <stdio.h>
1.41 +
1.42 + int scanf(const char *<[format]> [, <[arg]>, ...]);
1.43 + int fscanf(FILE *<[fd]>, const char *<[format]> [, <[arg]>, ...]);
1.44 + int sscanf(const char *<[str]>, const char *<[format]>
1.45 + [, <[arg]>, ...]);
1.46 +
1.47 +
1.48 +TRAD_SYNOPSIS
1.49 + #include <stdio.h>
1.50 +
1.51 + int scanf(<[format]> [, <[arg]>, ...])
1.52 + char *<[format]>;
1.53 +
1.54 + int fscanf(<[fd]>, <[format]> [, <[arg]>, ...]);
1.55 + FILE *<[fd]>;
1.56 + char *<[format]>;
1.57 +
1.58 + int sscanf(<[str]>, <[format]> [, <[arg]>, ...]);
1.59 + char *<[str]>;
1.60 + char *<[format]>;
1.61 +
1.62 +
1.63 +DESCRIPTION
1.64 + <<scanf>> scans a series of input fields from standard input,
1.65 + one character at a time. Each field is interpreted according to
1.66 + a format specifier passed to <<scanf>> in the format string at
1.67 + <<*<[format]>>>. <<scanf>> stores the interpreted input from
1.68 + each field at the address passed to it as the corresponding argument
1.69 + following <[format]>. You must supply the same number of
1.70 + format specifiers and address arguments as there are input fields.
1.71 +
1.72 + There must be sufficient address arguments for the given format
1.73 + specifiers; if not the results are unpredictable and likely
1.74 + disasterous. Excess address arguments are merely ignored.
1.75 +
1.76 + <<scanf>> often produces unexpected results if the input diverges from
1.77 + an expected pattern. Since the combination of <<gets>> or <<fgets>>
1.78 + followed by <<sscanf>> is safe and easy, that is the preferred way
1.79 + to be certain that a program is synchronized with input at the end
1.80 + of a line.
1.81 +
1.82 + <<fscanf>> and <<sscanf>> are identical to <<scanf>>, other than the
1.83 + source of input: <<fscanf>> reads from a file, and <<sscanf>>
1.84 + from a string.
1.85 +
1.86 + The string at <<*<[format]>>> is a character sequence composed
1.87 + of zero or more directives. Directives are composed of
1.88 + one or more whitespace characters, non-whitespace characters,
1.89 + and format specifications.
1.90 +
1.91 + Whitespace characters are blank (<< >>), tab (<<\t>>), or
1.92 + newline (<<\n>>).
1.93 + When <<scanf>> encounters a whitespace character in the format string
1.94 + it will read (but not store) all consecutive whitespace characters
1.95 + up to the next non-whitespace character in the input.
1.96 +
1.97 + Non-whitespace characters are all other ASCII characters except the
1.98 + percent sign (<<%>>). When <<scanf>> encounters a non-whitespace
1.99 + character in the format string it will read, but not store
1.100 + a matching non-whitespace character.
1.101 +
1.102 + Format specifications tell <<scanf>> to read and convert characters
1.103 + from the input field into specific types of values, and store then
1.104 + in the locations specified by the address arguments.
1.105 +
1.106 + Trailing whitespace is left unread unless explicitly
1.107 + matched in the format string.
1.108 +
1.109 + The format specifiers must begin with a percent sign (<<%>>)
1.110 + and have the following form:
1.111 +
1.112 +. %[*][<[width]>][<[size]>]<[type]>
1.113 +
1.114 + Each format specification begins with the percent character (<<%>>).
1.115 + The other fields are:
1.116 + o+
1.117 + o *
1.118 + an optional marker; if present, it suppresses interpretation and
1.119 + assignment of this input field.
1.120 +
1.121 + o <[width]>
1.122 + an optional maximum field width: a decimal integer,
1.123 + which controls the maximum number of characters that
1.124 + will be read before converting the current input field. If the
1.125 + input field has fewer than <[width]> characters, <<scanf>>
1.126 + reads all the characters in the field, and then
1.127 + proceeds with the next field and its format specification.
1.128 +
1.129 + If a whitespace or a non-convertable character occurs
1.130 + before <[width]> character are read, the characters up
1.131 + to that character are read, converted, and stored.
1.132 + Then <<scanf>> proceeds to the next format specification.
1.133 +
1.134 + o size
1.135 + <<h>>, <<l>>, and <<L>> are optional size characters which
1.136 + override the default way that <<scanf>> interprets the
1.137 + data type of the corresponding argument.
1.138 +
1.139 +
1.140 +.Modifier Type(s)
1.141 +. h d, i, o, u, x convert input to short,
1.142 +. store in short object
1.143 +.
1.144 +. h D, I, O, U, X no effect
1.145 +. e, f, c, s, n, p
1.146 +.
1.147 +. l d, i, o, u, x convert input to long,
1.148 +. store in long object
1.149 +.
1.150 +. l e, f, g convert input to double
1.151 +. store in a double object
1.152 +.
1.153 +. l D, I, O, U, X no effect
1.154 +. c, s, n, p
1.155 +.
1.156 +. L d, i, o, u, x convert to long double,
1.157 +. store in long double
1.158 +.
1.159 +. L all others no effect
1.160 +
1.161 +
1.162 + o <[type]>
1.163 +
1.164 + A character to specify what kind of conversion
1.165 + <<scanf>> performs. Here is a table of the conversion
1.166 + characters:
1.167 +
1.168 + o+
1.169 + o %
1.170 + No conversion is done; the percent character (<<%>>) is stored.
1.171 +
1.172 + o c
1.173 + Scans one character. Corresponding <[arg]>: <<(char *arg)>>.
1.174 +
1.175 + o s
1.176 + Reads a character string into the array supplied.
1.177 + Corresponding <[arg]>: <<(char arg[])>>.
1.178 +
1.179 + o [<[pattern]>]
1.180 + Reads a non-empty character string into memory
1.181 + starting at <[arg]>. This area must be large
1.182 + enough to accept the sequence and a
1.183 + terminating null character which will be added
1.184 + automatically. (<[pattern]> is discussed in the paragraph following
1.185 + this table). Corresponding <[arg]>: <<(char *arg)>>.
1.186 +
1.187 + o d
1.188 + Reads a decimal integer into the corresponding <[arg]>: <<(int *arg)>>.
1.189 +
1.190 + o D
1.191 + Reads a decimal integer into the corresponding
1.192 + <[arg]>: <<(long *arg)>>.
1.193 +
1.194 + o o
1.195 + Reads an octal integer into the corresponding <[arg]>: <<(int *arg)>>.
1.196 +
1.197 + o O
1.198 + Reads an octal integer into the corresponding <[arg]>: <<(long *arg)>>.
1.199 +
1.200 + o u
1.201 + Reads an unsigned decimal integer into the corresponding
1.202 + <[arg]>: <<(unsigned int *arg)>>.
1.203 +
1.204 +
1.205 + o U
1.206 + Reads an unsigned decimal integer into the corresponding <[arg]>:
1.207 + <<(unsigned long *arg)>>.
1.208 +
1.209 + o x,X
1.210 + Read a hexadecimal integer into the corresponding <[arg]>:
1.211 + <<(int *arg)>>.
1.212 +
1.213 + o e, f, g
1.214 + Read a floating point number into the corresponding <[arg]>:
1.215 + <<(float *arg)>>.
1.216 +
1.217 + o E, F, G
1.218 + Read a floating point number into the corresponding <[arg]>:
1.219 + <<(double *arg)>>.
1.220 +
1.221 + o i
1.222 + Reads a decimal, octal or hexadecimal integer into the
1.223 + corresponding <[arg]>: <<(int *arg)>>.
1.224 +
1.225 + o I
1.226 + Reads a decimal, octal or hexadecimal integer into the
1.227 + corresponding <[arg]>: <<(long *arg)>>.
1.228 +
1.229 + o n
1.230 + Stores the number of characters read in the corresponding
1.231 + <[arg]>: <<(int *arg)>>.
1.232 +
1.233 + o p
1.234 + Stores a scanned pointer. ANSI C leaves the details
1.235 + to each implementation; this implementation treats
1.236 + <<%p>> exactly the same as <<%U>>. Corresponding
1.237 + <[arg]>: <<(void **arg)>>.
1.238 + o-
1.239 +
1.240 + A <[pattern]> of characters surrounded by square brackets can be used
1.241 + instead of the <<s>> type character. <[pattern]> is a set of
1.242 + characters which define a search set of possible characters making up
1.243 + the <<scanf>> input field. If the first character in the brackets is a
1.244 + caret (<<^>>), the search set is inverted to include all ASCII characters
1.245 + except those between the brackets. There is also a range facility
1.246 + which you can use as a shortcut. <<%[0-9] >> matches all decimal digits.
1.247 + The hyphen must not be the first or last character in the set.
1.248 + The character prior to the hyphen must be lexically less than the
1.249 + character after it.
1.250 +
1.251 + Here are some <[pattern]> examples:
1.252 + o+
1.253 + o %[abcd]
1.254 + matches strings containing only <<a>>, <<b>>, <<c>>, and <<d>>.
1.255 +
1.256 + o %[^abcd]
1.257 + matches strings containing any characters except <<a>>, <<b>>,
1.258 + <<c>>, or <<d>>
1.259 +
1.260 + o %[A-DW-Z]
1.261 + matches strings containing <<A>>, <<B>>, <<C>>, <<D>>, <<W>>,
1.262 + <<X>>, <<Y>>, <<Z>>
1.263 +
1.264 + o %[z-a]
1.265 + matches the characters <<z>>, <<->>, and <<a>>
1.266 + o-
1.267 +
1.268 + Floating point numbers (for field types <<e>>, <<f>>, <<g>>, <<E>>,
1.269 + <<F>>, <<G>>) must correspond to the following general form:
1.270 +
1.271 +. [+/-] ddddd[.]ddd [E|e[+|-]ddd]
1.272 +
1.273 + where objects inclosed in square brackets are optional, and <<ddd>>
1.274 + represents decimal, octal, or hexadecimal digits.
1.275 + o-
1.276 +
1.277 +RETURNS
1.278 + <<scanf>> returns the number of input fields successfully
1.279 + scanned, converted and stored; the return value does
1.280 + not include scanned fields which were not stored.
1.281 +
1.282 + If <<scanf>> attempts to read at end-of-file, the return
1.283 + value is <<EOF>>.
1.284 +
1.285 + If no fields were stored, the return value is <<0>>.
1.286 +
1.287 + <<scanf>> might stop scanning a particular field before
1.288 + reaching the normal field end character, or may
1.289 + terminate entirely.
1.290 +
1.291 + <<scanf>> stops scanning and storing the current field
1.292 + and moves to the next input field (if any)
1.293 + in any of the following situations:
1.294 +
1.295 + O+
1.296 + o The assignment suppressing character (<<*>>) appears
1.297 + after the <<%>> in the format specification; the current
1.298 + input field is scanned but not stored.
1.299 +
1.300 + o <[width]> characters have been read (<[width]> is a
1.301 + width specification, a positive decimal integer).
1.302 +
1.303 + o The next character read cannot be converted
1.304 + under the the current format (for example,
1.305 + if a <<Z>> is read when the format is decimal).
1.306 +
1.307 + o The next character in the input field does not appear
1.308 + in the search set (or does appear in the inverted search set).
1.309 + O-
1.310 +
1.311 + When <<scanf>> stops scanning the current input field for one of
1.312 + these reasons, the next character is considered unread and
1.313 + used as the first character of the following input field, or the
1.314 + first character in a subsequent read operation on the input.
1.315 +
1.316 + <<scanf>> will terminate under the following circumstances:
1.317 +
1.318 + O+
1.319 + o The next character in the input field conflicts
1.320 + with a corresponding non-whitespace character in the
1.321 + format string.
1.322 +
1.323 + o The next character in the input field is <<EOF>>.
1.324 +
1.325 + o The format string has been exhausted.
1.326 + O-
1.327 +
1.328 + When the format string contains a character sequence that is
1.329 + not part of a format specification, the same character
1.330 + sequence must appear in the input; <<scanf>> will
1.331 + scan but not store the matched characters. If a
1.332 + conflict occurs, the first conflicting character remains in the input
1.333 + as if it had never been read.
1.334 +
1.335 +PORTABILITY
1.336 +<<scanf>> is ANSI C.
1.337 +
1.338 +Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
1.339 +<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
1.340 +*/
1.341 +
1.342 +#include <_ansi.h>
1.343 +#include <reent.h>
1.344 +#include <stdio.h>
1.345 +#include <string.h>
1.346 +#include <stdarg.h>
1.347 +#include "LOCAL.H"
1.348 +
1.349 +static
1.350 +int
1.351 +eofread (void* cookie,char *buf,int len)
1.352 +{
1.353 + cookie=cookie;
1.354 + buf=buf;
1.355 + len=len;
1.356 + return 0;
1.357 +}
1.358 +
1.359 +/**
1.360 +Read formatted data from string.
1.361 +Reads data from the buffer specified and stores it
1.362 +into the locations given by argument(s).
1.363 +Locations pointed by each argument are filled with their corresponding type
1.364 +of value requested in the format string.
1.365 +
1.366 +@param str Buffer containing the string to be parsed for data.
1.367 +@param fmt String that can contain one or more of these items:
1.368 + Whitespace characters: the function will read and ignore any whitespace
1.369 + characters (this includes blank, newline and tab characters) encountered
1.370 + before the next non-whitespace character. This includes any quantity of
1.371 + whitespace characters (including none). On-whitespace characters (any
1.372 + character not including blank, newline, tab, or any format specifier
1.373 + begining with % character): this cause that the function read and discard
1.374 + any character that match the given non-whitespace character. If this
1.375 + character is not found the function ends returning error.
1.376 + Format specifiers: A sequence of characters begining with '%' indicates
1.377 + that next data has to be readed and stored at the location pointed by its
1.378 + corresponding argument with a given format that is specified following this
1.379 + prototype: %[*][width][modifiers]type
1.380 +
1.381 +@return On Success, The number of items succesfully read. This count doesn't include any
1.382 + ignored fields.
1.383 + On Failure, returns EOF, if an error has occurred before the first assignation
1.384 + could be done and errno may be set.
1.385 +*/
1.386 +EXPORT_C int sscanf(const char *str, const char *fmt, ...)
1.387 +{
1.388 + int ret;
1.389 + va_list ap;
1.390 + FILE f;
1.391 +
1.392 + f._data = _REENT2;
1.393 + if (!f._data)
1.394 + return EOF; // Memory for library globals is not allocated (errno not set).
1.395 +
1.396 + f._flags = __SRD;
1.397 + f._bf._base = f._p = (unsigned char *) str;
1.398 + f._bf._size = f._r = strlen (str);
1.399 + f._read = eofread;
1.400 + f._ub._base = NULL;
1.401 + f._lb._base = NULL;
1.402 +
1.403 + va_start (ap, fmt);
1.404 + ret = __svfscanf (&f, fmt, ap);
1.405 + va_end (ap);
1.406 + return ret;
1.407 +}