Import gmp-4.3.1
[dragonfly.git] / contrib / gmp / scanf / sscanffuns.c
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, 2002, 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 the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
15
16 The GNU MP Library is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
19 License for more details.
20
21 You should have received a copy of the GNU Lesser General Public License
22 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
23
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include "gmp.h"
27 #include "gmp-impl.h"
28
29
30 #if 0
31 static int
32 scan (const char **sp, const char *fmt, ...)
33 {
34     va_list ap;
35     int ret;
36
37     va_start(ap, fmt);
38     ret = vsscanf(*sp, fmt, ap);
39     va_end(ap);
40
41     return ret;
42 }
43 #else
44 static int
45 scan (const char **sp, const char *fmt, ...)
46 {
47   va_list ap;
48   void *p1, *p2;
49   int ret;
50
51   va_start (ap, fmt);
52   p1 = va_arg (ap, void *);
53   p2 = va_arg (ap, void *);
54
55   ret = sscanf (*sp, fmt, p1, p2);
56
57   va_end (ap);
58
59   return ret;
60 }
61 #endif
62
63 static void
64 step (const char **sp, int n)
65 {
66   ASSERT (n >= 0);
67
68   /* shouldn't push us past the end of the string */
69 #if WANT_ASSERT
70   {
71     int  i;
72     for (i = 0; i < n; i++)
73       ASSERT ((*sp)[i] != '\0');
74   }
75 #endif
76
77   (*sp) += n;
78 }
79
80 static int
81 get (const char **sp)
82 {
83   const char  *s;
84   int  c;
85   s = *sp;
86   c = (unsigned char) *s++;
87   if (c == '\0')
88     return EOF;
89   *sp = s;
90   return c;
91 }
92
93 static void
94 unget (int c, const char **sp)
95 {
96   const char  *s;
97   s = *sp;
98   if (c == EOF)
99     {
100       ASSERT (*s == '\0');
101       return;
102     }
103   s--;
104   ASSERT ((unsigned char) *s == c);
105   *sp = s;
106 }
107
108 const struct gmp_doscan_funs_t  __gmp_sscanf_funs = {
109   (gmp_doscan_scan_t)  scan,
110   (gmp_doscan_step_t)  step,
111   (gmp_doscan_get_t)   get,
112   (gmp_doscan_unget_t) unget,
113 };