1 /* mpz_inp_str(dest_integer, stream, base) -- Input a number in base
2 BASE from stdio stream STREAM and store the result in DEST_INTEGER.
4 Copyright (C) 1991, 1993, 1994, 1996, 1998, 2000 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 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.
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.
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. */
30 digit_value_in_base (int c, int base)
32 digit_value_in_base (c, base)
55 mpz_inp_str (mpz_ptr x, FILE *stream, int base)
57 mpz_inp_str (x, stream, base)
64 size_t alloc_size, str_size;
75 /* Skip whitespace. */
91 if (digit_value_in_base (c, base == 0 ? 10 : base) < 0)
92 return 0; /* error if no digits */
94 /* If BASE is 0, try to find out the base by looking at the initial
104 if (c == 'x' || c == 'X')
110 else if (c == 'b' || c == 'B')
119 /* Skip leading zeros. */
127 str = (char *) (*_mp_allocate_func) (alloc_size);
133 if (str_size >= alloc_size)
135 size_t old_alloc_size = alloc_size;
136 alloc_size = alloc_size * 3 / 2;
137 str = (char *) (*_mp_reallocate_func) (str, old_alloc_size, alloc_size);
139 dig = digit_value_in_base (c, base);
142 str[str_size++] = dig;
148 /* Make sure the string is not empty, mpn_set_str would fail. */
152 (*_mp_free_func) (str, alloc_size);
156 xsize = (((mp_size_t) (str_size / __mp_bases[base].chars_per_bit_exactly))
157 / BITS_PER_MP_LIMB + 2);
158 if (x->_mp_alloc < xsize)
159 _mpz_realloc (x, xsize);
161 /* Convert the byte array in base BASE to our bignum format. */
162 xsize = mpn_set_str (x->_mp_d, (unsigned char *) str, str_size, base);
163 x->_mp_size = negative ? -xsize : xsize;
165 (*_mp_free_func) (str, alloc_size);
166 return str_size + nread;