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 | |
---|
4 | Copyright 1996, 2000, 2001, 2002 Free Software Foundation, Inc. |
---|
5 | |
---|
6 | This file is part of the GNU MP Library. |
---|
7 | |
---|
8 | The GNU MP Library is free software; you can redistribute it and/or modify |
---|
9 | it under the terms of the GNU Lesser General Public License as published by |
---|
10 | the Free Software Foundation; either version 2.1 of the License, or (at your |
---|
11 | option) any later version. |
---|
12 | |
---|
13 | The GNU MP Library is distributed in the hope that it will be useful, but |
---|
14 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
15 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
---|
16 | License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU Lesser General Public License |
---|
19 | along with the GNU MP Library; see the file COPYING.LIB. If not, write to |
---|
20 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
---|
21 | MA 02111-1307, USA. */ |
---|
22 | |
---|
23 | #include <stdio.h> |
---|
24 | #include <ctype.h> |
---|
25 | #include "gmp.h" |
---|
26 | #include "gmp-impl.h" |
---|
27 | |
---|
28 | size_t |
---|
29 | mpf_inp_str (mpf_ptr rop, FILE *stream, int base) |
---|
30 | { |
---|
31 | char *str; |
---|
32 | size_t alloc_size, str_size; |
---|
33 | int c; |
---|
34 | size_t retval; |
---|
35 | size_t nread; |
---|
36 | |
---|
37 | if (stream == 0) |
---|
38 | stream = stdin; |
---|
39 | |
---|
40 | alloc_size = 100; |
---|
41 | str = (char *) (*__gmp_allocate_func) (alloc_size); |
---|
42 | str_size = 0; |
---|
43 | nread = 0; |
---|
44 | |
---|
45 | /* Skip whitespace. */ |
---|
46 | do |
---|
47 | { |
---|
48 | c = getc (stream); |
---|
49 | nread++; |
---|
50 | } |
---|
51 | while (isspace (c)); |
---|
52 | |
---|
53 | for (;;) |
---|
54 | { |
---|
55 | if (str_size >= alloc_size) |
---|
56 | { |
---|
57 | size_t old_alloc_size = alloc_size; |
---|
58 | alloc_size = alloc_size * 3 / 2; |
---|
59 | str = (char *) (*__gmp_reallocate_func) (str, old_alloc_size, alloc_size); |
---|
60 | } |
---|
61 | if (c == EOF || isspace (c)) |
---|
62 | break; |
---|
63 | str[str_size++] = c; |
---|
64 | c = getc (stream); |
---|
65 | } |
---|
66 | ungetc (c, stream); |
---|
67 | nread--; |
---|
68 | |
---|
69 | if (str_size >= alloc_size) |
---|
70 | { |
---|
71 | size_t old_alloc_size = alloc_size; |
---|
72 | alloc_size = alloc_size * 3 / 2; |
---|
73 | str = (char *) (*__gmp_reallocate_func) (str, old_alloc_size, alloc_size); |
---|
74 | } |
---|
75 | str[str_size] = 0; |
---|
76 | |
---|
77 | retval = mpf_set_str (rop, str, base); |
---|
78 | (*__gmp_free_func) (str, alloc_size); |
---|
79 | |
---|
80 | if (retval == -1) |
---|
81 | return 0; /* error */ |
---|
82 | |
---|
83 | return str_size + nread; |
---|
84 | } |
---|