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. */
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. */
32 mpfr_inp_str (mpfr_ptr rop
, FILE *stream
, int base
, mpfr_rnd_t rnd_mode
)
35 size_t alloc_size
, str_size
;
44 str
= (unsigned char *) (*__gmp_allocate_func
) (alloc_size
);
48 /* Skip whitespace. */
56 /* number of characters read is nread */
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
))
69 str
[str_size
++] = (unsigned char) c
;
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 */
82 retval
= mpfr_set_str (rop
, (char *) str
, base
, rnd_mode
);
83 (*__gmp_free_func
) (str
, alloc_size
);
88 return str_size
+ nread
- 1;