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 OF THE FUNCTIONS IN THIS FILE, ONLY mpz_inp_str IS FOR EXTERNAL USE, THE
5 REST ARE INTERNALS AND ARE ALMOST CERTAIN TO BE SUBJECT TO INCOMPATIBLE
6 CHANGES OR DISAPPEAR COMPLETELY IN FUTURE GNU MP RELEASES.
8 Copyright 1991, 1993, 1994, 1996, 1998, 2000, 2001, 2002, 2003 Free Software
11 This file is part of the GNU MP Library.
13 The GNU MP Library is free software; you can redistribute it and/or modify
14 it under the terms of the GNU Lesser General Public License as published by
15 the Free Software Foundation; either version 3 of the License, or (at your
16 option) any later version.
18 The GNU MP Library is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
21 License for more details.
23 You should have received a copy of the GNU Lesser General Public License
24 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
31 extern const unsigned char __gmp_digit_value_tab[];
32 #define digit_value_tab __gmp_digit_value_tab
35 mpz_inp_str (mpz_ptr x, FILE *stream, int base)
45 /* Skip whitespace. */
53 return mpz_inp_str_nowhite (x, stream, base, c, nread);
56 /* shared by mpq_inp_str */
58 mpz_inp_str_nowhite (mpz_ptr x, FILE *stream, int base, int c, size_t nread)
61 size_t alloc_size, str_size;
64 const unsigned char *digit_value;
66 ASSERT_ALWAYS (EOF == -1); /* FIXME: handle this by adding explicit */
67 /* comparisons of c and EOF before each */
68 /* read of digit_value[]. */
70 digit_value = digit_value_tab;
73 /* For bases > 36, use the collating sequence
74 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz. */
77 return 0; /* too large base */
88 if (c == EOF || digit_value[c] >= (base == 0 ? 10 : base))
89 return 0; /* error if no digits */
91 /* If BASE is 0, try to find out the base by looking at the initial
101 if (c == 'x' || c == 'X')
107 else if (c == 'b' || c == 'B')
116 /* Skip leading zeros. */
124 str = (char *) (*__gmp_allocate_func) (alloc_size);
130 dig = digit_value[c];
133 if (str_size >= alloc_size)
135 size_t old_alloc_size = alloc_size;
136 alloc_size = alloc_size * 3 / 2;
137 str = (char *) (*__gmp_reallocate_func) (str, old_alloc_size, alloc_size);
139 str[str_size++] = dig;
147 /* Make sure the string is not empty, mpn_set_str would fail. */
154 xsize = 2 + (mp_size_t)
155 (str_size / (GMP_NUMB_BITS * mp_bases[base].chars_per_bit_exactly));
156 MPZ_REALLOC (x, xsize);
158 /* Convert the byte array in base BASE to our bignum format. */
159 xsize = mpn_set_str (x->_mp_d, (unsigned char *) str, str_size, base);
160 x->_mp_size = negative ? -xsize : xsize;
162 (*__gmp_free_func) (str, alloc_size);