xref: /netbsd-src/external/lgpl3/gmp/dist/scanf/sscanffuns.c (revision c38e7cc395b1472a774ff828e46123de44c628e9)
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.h"
38 #include "gmp-impl.h"
39 
40 
41 #if 0
42 static int
43 scan (const char **sp, const char *fmt, ...)
44 {
45     va_list ap;
46     int ret;
47 
48     va_start(ap, fmt);
49     ret = vsscanf(*sp, fmt, ap);
50     va_end(ap);
51 
52     return ret;
53 }
54 #else
55 static int
56 scan (const char **sp, const char *fmt, ...)
57 {
58   va_list ap;
59   void *p1, *p2;
60   int ret;
61 
62   va_start (ap, fmt);
63   p1 = va_arg (ap, void *);
64   p2 = va_arg (ap, void *);
65 
66   ret = sscanf (*sp, fmt, p1, p2);
67 
68   va_end (ap);
69 
70   return ret;
71 }
72 #endif
73 
74 static void
75 step (const char **sp, int n)
76 {
77   ASSERT (n >= 0);
78 
79   /* shouldn't push us past the end of the string */
80 #if WANT_ASSERT
81   {
82     int  i;
83     for (i = 0; i < n; i++)
84       ASSERT ((*sp)[i] != '\0');
85   }
86 #endif
87 
88   (*sp) += n;
89 }
90 
91 static int
92 get (const char **sp)
93 {
94   const char  *s;
95   int  c;
96   s = *sp;
97   c = (unsigned char) *s++;
98   if (c == '\0')
99     return EOF;
100   *sp = s;
101   return c;
102 }
103 
104 static void
105 unget (int c, const char **sp)
106 {
107   const char  *s;
108   s = *sp;
109   if (c == EOF)
110     {
111       ASSERT (*s == '\0');
112       return;
113     }
114   s--;
115   ASSERT ((unsigned char) *s == c);
116   *sp = s;
117 }
118 
119 const struct gmp_doscan_funs_t  __gmp_sscanf_funs = {
120   (gmp_doscan_scan_t)  scan,
121   (gmp_doscan_step_t)  step,
122   (gmp_doscan_get_t)   get,
123   (gmp_doscan_unget_t) unget,
124 };
125