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