1/* mpf_inp_str(dest_float, stream, base) -- Input a number in base
2   BASE from stdio stream STREAM and store the result in DEST_FLOAT.
3
4Copyright 1999, 2001, 2002, 2004, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5Contributed by the Arenaire and Cacao projects, INRIA.
6(Copied from GMP, file mpf/inp_str.c)
7
8This file is part of the GNU MPFR Library.
9
10The GNU MPFR Library is free software; you can redistribute it and/or modify
11it under the terms of the GNU Lesser General Public License as published by
12the Free Software Foundation; either version 3 of the License, or (at your
13option) any later version.
14
15The GNU MPFR Library is distributed in the hope that it will be useful, but
16WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
18License for more details.
19
20You should have received a copy of the GNU Lesser General Public License
21along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
22http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
2351 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
24
25#include <ctype.h>
26
27#include "mpfr-impl.h"
28
29size_t
30mpfr_inp_str (mpfr_ptr rop, FILE *stream, int base, mpfr_rnd_t rnd_mode)
31{
32  unsigned char *str;
33  size_t alloc_size, str_size;
34  int c;
35  int retval;
36  size_t nread;
37
38  if (stream == NULL)
39    stream = stdin;
40
41  alloc_size = 100;
42  str = (unsigned char *) (*__gmp_allocate_func) (alloc_size);
43  str_size = 0;
44  nread = 0;
45
46  /* Skip whitespace.  */
47  do
48    {
49      c = getc (stream);
50      nread++;
51    }
52  while (isspace (c));
53
54  /* number of characters read is nread */
55
56  for (;;)
57    {
58      if (str_size >= alloc_size)
59        {
60          size_t old_alloc_size = alloc_size;
61          alloc_size = alloc_size * 3 / 2;
62          str = (unsigned char *)
63            (*__gmp_reallocate_func) (str, old_alloc_size, alloc_size);
64        }
65      if (c == EOF || isspace (c))
66        break;
67      str[str_size++] = (unsigned char) c;
68      c = getc (stream);
69    }
70  ungetc (c, stream);
71
72  /* number of characters read is nread + str_size - 1 */
73
74  /* we can exit the for loop only by the break instruction,
75     then necessarily str_size >= alloc_size was checked, so
76     now str_size < alloc_size */
77
78  str[str_size] = '\0';
79
80  retval = mpfr_set_str (rop, (char *) str, base, rnd_mode);
81  (*__gmp_free_func) (str, alloc_size);
82
83  if (retval == -1)
84    return 0;                   /* error */
85
86 return str_size + nread - 1;
87}
88