xref: /dflybsd-src/contrib/gmp/scanf/sscanffuns.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /* __gmp_sscanf_funs -- support for formatted input from a string.
286d7f5d3SJohn Marino 
386d7f5d3SJohn Marino    THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY.  THEY'RE ALMOST
486d7f5d3SJohn Marino    CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
586d7f5d3SJohn Marino    FUTURE GNU MP RELEASES.
686d7f5d3SJohn Marino 
786d7f5d3SJohn Marino Copyright 2001, 2002, 2003, 2009 Free Software Foundation, Inc.
886d7f5d3SJohn Marino 
986d7f5d3SJohn Marino This file is part of the GNU MP Library.
1086d7f5d3SJohn Marino 
1186d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
1286d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
1386d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
1486d7f5d3SJohn Marino option) any later version.
1586d7f5d3SJohn Marino 
1686d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
1786d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1886d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1986d7f5d3SJohn Marino License for more details.
2086d7f5d3SJohn Marino 
2186d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
2286d7f5d3SJohn Marino along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
2386d7f5d3SJohn Marino 
2486d7f5d3SJohn Marino #include <stdio.h>
2586d7f5d3SJohn Marino #include <stdarg.h>
2686d7f5d3SJohn Marino #include "gmp.h"
2786d7f5d3SJohn Marino #include "gmp-impl.h"
2886d7f5d3SJohn Marino 
2986d7f5d3SJohn Marino 
3086d7f5d3SJohn Marino #if 0
3186d7f5d3SJohn Marino static int
3286d7f5d3SJohn Marino scan (const char **sp, const char *fmt, ...)
3386d7f5d3SJohn Marino {
3486d7f5d3SJohn Marino     va_list ap;
3586d7f5d3SJohn Marino     int ret;
3686d7f5d3SJohn Marino 
3786d7f5d3SJohn Marino     va_start(ap, fmt);
3886d7f5d3SJohn Marino     ret = vsscanf(*sp, fmt, ap);
3986d7f5d3SJohn Marino     va_end(ap);
4086d7f5d3SJohn Marino 
4186d7f5d3SJohn Marino     return ret;
4286d7f5d3SJohn Marino }
4386d7f5d3SJohn Marino #else
4486d7f5d3SJohn Marino static int
scan(const char ** sp,const char * fmt,...)4586d7f5d3SJohn Marino scan (const char **sp, const char *fmt, ...)
4686d7f5d3SJohn Marino {
4786d7f5d3SJohn Marino   va_list ap;
4886d7f5d3SJohn Marino   void *p1, *p2;
4986d7f5d3SJohn Marino   int ret;
5086d7f5d3SJohn Marino 
5186d7f5d3SJohn Marino   va_start (ap, fmt);
5286d7f5d3SJohn Marino   p1 = va_arg (ap, void *);
5386d7f5d3SJohn Marino   p2 = va_arg (ap, void *);
5486d7f5d3SJohn Marino 
5586d7f5d3SJohn Marino   ret = sscanf (*sp, fmt, p1, p2);
5686d7f5d3SJohn Marino 
5786d7f5d3SJohn Marino   va_end (ap);
5886d7f5d3SJohn Marino 
5986d7f5d3SJohn Marino   return ret;
6086d7f5d3SJohn Marino }
6186d7f5d3SJohn Marino #endif
6286d7f5d3SJohn Marino 
6386d7f5d3SJohn Marino static void
step(const char ** sp,int n)6486d7f5d3SJohn Marino step (const char **sp, int n)
6586d7f5d3SJohn Marino {
6686d7f5d3SJohn Marino   ASSERT (n >= 0);
6786d7f5d3SJohn Marino 
6886d7f5d3SJohn Marino   /* shouldn't push us past the end of the string */
6986d7f5d3SJohn Marino #if WANT_ASSERT
7086d7f5d3SJohn Marino   {
7186d7f5d3SJohn Marino     int  i;
7286d7f5d3SJohn Marino     for (i = 0; i < n; i++)
7386d7f5d3SJohn Marino       ASSERT ((*sp)[i] != '\0');
7486d7f5d3SJohn Marino   }
7586d7f5d3SJohn Marino #endif
7686d7f5d3SJohn Marino 
7786d7f5d3SJohn Marino   (*sp) += n;
7886d7f5d3SJohn Marino }
7986d7f5d3SJohn Marino 
8086d7f5d3SJohn Marino static int
get(const char ** sp)8186d7f5d3SJohn Marino get (const char **sp)
8286d7f5d3SJohn Marino {
8386d7f5d3SJohn Marino   const char  *s;
8486d7f5d3SJohn Marino   int  c;
8586d7f5d3SJohn Marino   s = *sp;
8686d7f5d3SJohn Marino   c = (unsigned char) *s++;
8786d7f5d3SJohn Marino   if (c == '\0')
8886d7f5d3SJohn Marino     return EOF;
8986d7f5d3SJohn Marino   *sp = s;
9086d7f5d3SJohn Marino   return c;
9186d7f5d3SJohn Marino }
9286d7f5d3SJohn Marino 
9386d7f5d3SJohn Marino static void
unget(int c,const char ** sp)9486d7f5d3SJohn Marino unget (int c, const char **sp)
9586d7f5d3SJohn Marino {
9686d7f5d3SJohn Marino   const char  *s;
9786d7f5d3SJohn Marino   s = *sp;
9886d7f5d3SJohn Marino   if (c == EOF)
9986d7f5d3SJohn Marino     {
10086d7f5d3SJohn Marino       ASSERT (*s == '\0');
10186d7f5d3SJohn Marino       return;
10286d7f5d3SJohn Marino     }
10386d7f5d3SJohn Marino   s--;
10486d7f5d3SJohn Marino   ASSERT ((unsigned char) *s == c);
10586d7f5d3SJohn Marino   *sp = s;
10686d7f5d3SJohn Marino }
10786d7f5d3SJohn Marino 
10886d7f5d3SJohn Marino const struct gmp_doscan_funs_t  __gmp_sscanf_funs = {
10986d7f5d3SJohn Marino   (gmp_doscan_scan_t)  scan,
11086d7f5d3SJohn Marino   (gmp_doscan_step_t)  step,
11186d7f5d3SJohn Marino   (gmp_doscan_get_t)   get,
11286d7f5d3SJohn Marino   (gmp_doscan_unget_t) unget,
11386d7f5d3SJohn Marino };
114