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 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 Library General Public License as published by
10 the Free Software Foundation; either version 2 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 Library General Public
16 License for more details.
18 You should have received a copy of the GNU Library 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. */
29 digit_value_in_base (c, base)
51 mpz_inp_str (mpz_ptr dest, FILE *stream, int base)
53 mpz_inp_str (dest, stream, base)
60 size_t alloc_size, str_size;
70 str = (char *) (*_mp_allocate_func) (alloc_size);
74 /* Skip whitespace. */
89 if (digit_value_in_base (c, base == 0 ? 10 : base) < 0)
90 return 0; /* error if no digits */
92 /* If BASE is 0, try to find out the base by looking at the initial
102 if (c == 'x' || c == 'X')
114 if (str_size >= alloc_size)
116 size_t old_alloc_size = alloc_size;
117 alloc_size = alloc_size * 3 / 2;
118 str = (char *) (*_mp_reallocate_func) (str, old_alloc_size, alloc_size);
120 dig = digit_value_in_base (c, base);
123 str[str_size++] = dig;
129 dest_size = str_size / __mp_bases[base].chars_per_limb + 1;
130 if (dest->_mp_alloc < dest_size)
131 _mpz_realloc (dest, dest_size);
133 dest_size = mpn_set_str (dest->_mp_d, (unsigned char *) str, str_size, base);
134 dest->_mp_size = negative ? -dest_size : dest_size;
136 (*_mp_free_func) (str, alloc_size);
137 return str_size + nread;