blob: 3ee6b63d52e3d54debe9fd5bd7830153190bb018 [file] [log] [blame]
Austin Schuhdace2a62020-08-18 10:56:48 -07001/* __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
7Copyright 2001-2003, 2009 Free Software Foundation, Inc.
8
9This file is part of the GNU MP Library.
10
11The GNU MP Library is free software; you can redistribute it and/or modify
12it 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
18or
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
24or both in parallel, as here.
25
26The GNU MP Library is distributed in the hope that it will be useful, but
27WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
28or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29for more details.
30
31You should have received copies of the GNU General Public License and the
32GNU Lesser General Public License along with the GNU MP Library. If not,
33see https://www.gnu.org/licenses/. */
34
35#include <stdio.h>
36#include <stdarg.h>
37#include "gmp-impl.h"
38
39
40#if 0
41static int
42scan (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
54static int
55scan (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
73static void
74step (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
90static int
91get (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
103static void
104unget (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
118const 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};