Annotation of OpenXM_contrib/gmp/mpfr/inp_str.c, Revision 1.1.1.1
1.1 ohara 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 1999, 2001 Free Software Foundation, Inc.
5: (Copied from GMP, file mpf/inp_str.c)
6:
7: This file is part of the MPFR Library.
8:
9: The 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 2.1 of the License, or (at your
12: option) any later version.
13:
14: The 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.
18:
19: You should have received a copy of the GNU Lesser General Public License
20: along with the MPFR Library; see the file COPYING.LIB. If not, write to
21: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22: MA 02111-1307, USA. */
23:
24: #include <stdio.h>
25: #include <ctype.h>
26: #include "gmp.h"
27: #include "gmp-impl.h"
28: #include "mpfr.h"
29: #include "mpfr-impl.h"
30:
31: size_t
32: mpfr_inp_str (mpfr_ptr rop, FILE *stream, int base, mp_rnd_t rnd_mode)
33: {
34: char *str;
35: size_t alloc_size, str_size;
36: int c;
37: size_t retval;
38: size_t nread;
39:
40: MPFR_CLEAR_FLAGS(rop);
41: if (stream == 0)
42: stream = stdin;
43:
44: alloc_size = 100;
45: str = (char *) (*__gmp_allocate_func) (alloc_size);
46: str_size = 0;
47: nread = 0;
48:
49: /* Skip whitespace. */
50: do
51: {
52: c = getc (stream);
53: nread++;
54: }
55: while (isspace (c));
56:
57: for (;;)
58: {
59: if (str_size >= alloc_size)
60: {
61: size_t old_alloc_size = alloc_size;
62: alloc_size = alloc_size * 3 / 2;
63: str = (char *) (*__gmp_reallocate_func) (str, old_alloc_size, alloc_size);
64: }
65: if (c == EOF || isspace (c))
66: break;
67: str[str_size++] = c;
68: c = getc (stream);
69: }
70: ungetc (c, stream);
71:
72: if (str_size >= alloc_size)
73: {
74: size_t old_alloc_size = alloc_size;
75: alloc_size = alloc_size * 3 / 2;
76: str = (char *) (*__gmp_reallocate_func) (str, old_alloc_size, alloc_size);
77: }
78: str[str_size] = 0;
79:
80: retval = mpfr_set_str (rop, 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;
87: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>