Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

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