beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpf / inp_str.c
blob45cc34cebbabd6cf5c2247db2e1440269f400889
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.
4 Copyright 1996, 2000-2002, 2005 Free Software Foundation, Inc.
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of either:
11 * the GNU Lesser General Public License as published by the Free
12 Software Foundation; either version 3 of the License, or (at your
13 option) any later version.
17 * the GNU General Public License as published by the Free Software
18 Foundation; either version 2 of the License, or (at your option) any
19 later version.
21 or both in parallel, as here.
23 The GNU MP Library is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 for more details.
28 You should have received copies of the GNU General Public License and the
29 GNU Lesser General Public License along with the GNU MP Library. If not,
30 see https://www.gnu.org/licenses/. */
32 #include <stdio.h>
33 #include <ctype.h>
34 #include "gmp.h"
35 #include "gmp-impl.h"
37 size_t
38 mpf_inp_str (mpf_ptr rop, FILE *stream, int base)
40 char *str;
41 size_t alloc_size, str_size;
42 int c;
43 int res;
44 size_t nread;
46 if (stream == 0)
47 stream = stdin;
49 alloc_size = 100;
50 str = (char *) (*__gmp_allocate_func) (alloc_size);
51 str_size = 0;
52 nread = 0;
54 /* Skip whitespace. */
57 c = getc (stream);
58 nread++;
60 while (isspace (c));
62 for (;;)
64 if (str_size >= alloc_size)
66 size_t old_alloc_size = alloc_size;
67 alloc_size = alloc_size * 3 / 2;
68 str = (char *) (*__gmp_reallocate_func) (str, old_alloc_size, alloc_size);
70 if (c == EOF || isspace (c))
71 break;
72 str[str_size++] = c;
73 c = getc (stream);
75 ungetc (c, stream);
76 nread--;
78 if (str_size >= alloc_size)
80 size_t old_alloc_size = alloc_size;
81 alloc_size = alloc_size * 3 / 2;
82 str = (char *) (*__gmp_reallocate_func) (str, old_alloc_size, alloc_size);
84 str[str_size] = 0;
86 res = mpf_set_str (rop, str, base);
87 (*__gmp_free_func) (str, alloc_size);
89 if (res == -1)
90 return 0; /* error */
92 return str_size + nread;