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
7Copyright 2001, 2002, 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 the GNU Lesser General Public License as published by
13the Free Software Foundation; either version 3 of the License, or (at your
14option) any later version.
15
16The GNU MP Library is distributed in the hope that it will be useful, but
17WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
19License for more details.
20
21You should have received a copy of the GNU Lesser General Public License
22along 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
31static int
32scan (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
44static int
45scan (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
63static void
64step (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
80static int
81get (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
93static void
94unget (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
108const 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};
114