Home | History | Annotate | Line # | Download | only in scanf
      1      1.1  mrg /* __gmp_sscanf_funs -- support for formatted input from a string.
      2      1.1  mrg 
      3      1.1  mrg    THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY.  THEY'RE ALMOST
      4      1.1  mrg    CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
      5      1.1  mrg    FUTURE GNU MP RELEASES.
      6      1.1  mrg 
      7  1.1.1.2  mrg Copyright 2001-2003, 2009 Free Software Foundation, Inc.
      8      1.1  mrg 
      9      1.1  mrg This file is part of the GNU MP Library.
     10      1.1  mrg 
     11      1.1  mrg The GNU MP Library is free software; you can redistribute it and/or modify
     12  1.1.1.2  mrg it under the terms of either:
     13  1.1.1.2  mrg 
     14  1.1.1.2  mrg   * the GNU Lesser General Public License as published by the Free
     15  1.1.1.2  mrg     Software Foundation; either version 3 of the License, or (at your
     16  1.1.1.2  mrg     option) any later version.
     17  1.1.1.2  mrg 
     18  1.1.1.2  mrg or
     19  1.1.1.2  mrg 
     20  1.1.1.2  mrg   * the GNU General Public License as published by the Free Software
     21  1.1.1.2  mrg     Foundation; either version 2 of the License, or (at your option) any
     22  1.1.1.2  mrg     later version.
     23  1.1.1.2  mrg 
     24  1.1.1.2  mrg or both in parallel, as here.
     25      1.1  mrg 
     26      1.1  mrg The GNU MP Library is distributed in the hope that it will be useful, but
     27      1.1  mrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     28  1.1.1.2  mrg or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     29  1.1.1.2  mrg for more details.
     30      1.1  mrg 
     31  1.1.1.2  mrg You should have received copies of the GNU General Public License and the
     32  1.1.1.2  mrg GNU Lesser General Public License along with the GNU MP Library.  If not,
     33  1.1.1.2  mrg see https://www.gnu.org/licenses/.  */
     34      1.1  mrg 
     35      1.1  mrg #include <stdio.h>
     36      1.1  mrg #include <stdarg.h>
     37      1.1  mrg #include "gmp-impl.h"
     38      1.1  mrg 
     39      1.1  mrg 
     40      1.1  mrg #if 0
     41      1.1  mrg static int
     42      1.1  mrg scan (const char **sp, const char *fmt, ...)
     43      1.1  mrg {
     44      1.1  mrg     va_list ap;
     45      1.1  mrg     int ret;
     46      1.1  mrg 
     47      1.1  mrg     va_start(ap, fmt);
     48      1.1  mrg     ret = vsscanf(*sp, fmt, ap);
     49      1.1  mrg     va_end(ap);
     50      1.1  mrg 
     51      1.1  mrg     return ret;
     52      1.1  mrg }
     53      1.1  mrg #else
     54      1.1  mrg static int
     55      1.1  mrg scan (const char **sp, const char *fmt, ...)
     56      1.1  mrg {
     57      1.1  mrg   va_list ap;
     58      1.1  mrg   void *p1, *p2;
     59      1.1  mrg   int ret;
     60      1.1  mrg 
     61      1.1  mrg   va_start (ap, fmt);
     62      1.1  mrg   p1 = va_arg (ap, void *);
     63      1.1  mrg   p2 = va_arg (ap, void *);
     64      1.1  mrg 
     65      1.1  mrg   ret = sscanf (*sp, fmt, p1, p2);
     66      1.1  mrg 
     67      1.1  mrg   va_end (ap);
     68      1.1  mrg 
     69      1.1  mrg   return ret;
     70      1.1  mrg }
     71      1.1  mrg #endif
     72      1.1  mrg 
     73      1.1  mrg static void
     74      1.1  mrg step (const char **sp, int n)
     75      1.1  mrg {
     76      1.1  mrg   ASSERT (n >= 0);
     77      1.1  mrg 
     78      1.1  mrg   /* shouldn't push us past the end of the string */
     79      1.1  mrg #if WANT_ASSERT
     80      1.1  mrg   {
     81      1.1  mrg     int  i;
     82      1.1  mrg     for (i = 0; i < n; i++)
     83      1.1  mrg       ASSERT ((*sp)[i] != '\0');
     84      1.1  mrg   }
     85      1.1  mrg #endif
     86      1.1  mrg 
     87      1.1  mrg   (*sp) += n;
     88      1.1  mrg }
     89      1.1  mrg 
     90      1.1  mrg static int
     91      1.1  mrg get (const char **sp)
     92      1.1  mrg {
     93      1.1  mrg   const char  *s;
     94      1.1  mrg   int  c;
     95      1.1  mrg   s = *sp;
     96      1.1  mrg   c = (unsigned char) *s++;
     97      1.1  mrg   if (c == '\0')
     98      1.1  mrg     return EOF;
     99      1.1  mrg   *sp = s;
    100      1.1  mrg   return c;
    101      1.1  mrg }
    102      1.1  mrg 
    103      1.1  mrg static void
    104      1.1  mrg unget (int c, const char **sp)
    105      1.1  mrg {
    106      1.1  mrg   const char  *s;
    107      1.1  mrg   s = *sp;
    108      1.1  mrg   if (c == EOF)
    109      1.1  mrg     {
    110      1.1  mrg       ASSERT (*s == '\0');
    111      1.1  mrg       return;
    112      1.1  mrg     }
    113      1.1  mrg   s--;
    114      1.1  mrg   ASSERT ((unsigned char) *s == c);
    115      1.1  mrg   *sp = s;
    116      1.1  mrg }
    117      1.1  mrg 
    118      1.1  mrg const struct gmp_doscan_funs_t  __gmp_sscanf_funs = {
    119      1.1  mrg   (gmp_doscan_scan_t)  scan,
    120      1.1  mrg   (gmp_doscan_step_t)  step,
    121      1.1  mrg   (gmp_doscan_get_t)   get,
    122      1.1  mrg   (gmp_doscan_unget_t) unget,
    123      1.1  mrg };
    124