beta-0.89.2
[luatex.git] / source / libs / mpfr / mpfr-3.1.3 / src / inp_str.c
blobc46276f072285c08521f1b8807e043294c11678a
1 /* mpfr_inp_str -- input a number in base BASE from stdio stream STREAM
2 and store the result in ROP
4 Copyright 1999, 2001-2002, 2004, 2006-2015 Free Software Foundation, Inc.
5 Contributed by the AriC and Caramel projects, INRIA.
7 This file is part of the GNU MPFR Library.
9 The GNU MPFR Library is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
14 The GNU MPFR Library is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
21 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
22 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
24 #include <ctype.h>
26 #include "mpfr-impl.h"
28 /* The original version of this function came from GMP's mpf/inp_str.c;
29 it has been adapted for MPFR. */
31 size_t
32 mpfr_inp_str (mpfr_ptr rop, FILE *stream, int base, mpfr_rnd_t rnd_mode)
34 unsigned char *str;
35 size_t alloc_size, str_size;
36 int c;
37 int retval;
38 size_t nread;
40 if (stream == NULL)
41 stream = stdin;
43 alloc_size = 100;
44 str = (unsigned char *) (*__gmp_allocate_func) (alloc_size);
45 str_size = 0;
46 nread = 0;
48 /* Skip whitespace. */
51 c = getc (stream);
52 nread++;
54 while (isspace (c));
56 /* number of characters read is nread */
58 for (;;)
60 if (str_size >= alloc_size)
62 size_t old_alloc_size = alloc_size;
63 alloc_size = alloc_size * 3 / 2;
64 str = (unsigned char *)
65 (*__gmp_reallocate_func) (str, old_alloc_size, alloc_size);
67 if (c == EOF || isspace (c))
68 break;
69 str[str_size++] = (unsigned char) c;
70 c = getc (stream);
72 ungetc (c, stream);
74 /* number of characters read is nread + str_size - 1 */
76 /* we can exit the for loop only by the break instruction,
77 then necessarily str_size >= alloc_size was checked, so
78 now str_size < alloc_size */
80 str[str_size] = '\0';
82 retval = mpfr_set_str (rop, (char *) str, base, rnd_mode);
83 (*__gmp_free_func) (str, alloc_size);
85 if (retval == -1)
86 return 0; /* error */
88 return str_size + nread - 1;