1 | /* mpz_out_str(stream, base, integer) -- Output to STREAM the multi prec. |
---|
2 | integer INTEGER in base BASE. |
---|
3 | |
---|
4 | Copyright 1991, 1993, 1994, 1996, 2001 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 "gmp.h" |
---|
25 | #include "gmp-impl.h" |
---|
26 | |
---|
27 | size_t |
---|
28 | mpz_out_str (FILE *stream, int base, mpz_srcptr x) |
---|
29 | { |
---|
30 | mp_ptr xp; |
---|
31 | mp_size_t x_size = x->_mp_size; |
---|
32 | unsigned char *str; |
---|
33 | size_t str_size; |
---|
34 | size_t i; |
---|
35 | size_t written; |
---|
36 | char *num_to_text; |
---|
37 | TMP_DECL (marker); |
---|
38 | |
---|
39 | if (stream == 0) |
---|
40 | stream = stdout; |
---|
41 | |
---|
42 | if (base >= 0) |
---|
43 | { |
---|
44 | if (base == 0) |
---|
45 | base = 10; |
---|
46 | num_to_text = "0123456789abcdefghijklmnopqrstuvwxyz"; |
---|
47 | } |
---|
48 | else |
---|
49 | { |
---|
50 | base = -base; |
---|
51 | num_to_text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
---|
52 | } |
---|
53 | |
---|
54 | if (x_size == 0) |
---|
55 | { |
---|
56 | fputc ('0', stream); |
---|
57 | return ferror (stream) ? 0 : 1; |
---|
58 | } |
---|
59 | |
---|
60 | written = 0; |
---|
61 | |
---|
62 | if (x_size < 0) |
---|
63 | { |
---|
64 | fputc ('-', stream); |
---|
65 | x_size = -x_size; |
---|
66 | written = 1; |
---|
67 | } |
---|
68 | |
---|
69 | TMP_MARK (marker); |
---|
70 | str_size = ((size_t) (x_size * BITS_PER_MP_LIMB |
---|
71 | * __mp_bases[base].chars_per_bit_exactly)) + 3; |
---|
72 | str = (unsigned char *) TMP_ALLOC (str_size); |
---|
73 | |
---|
74 | /* Move the number to convert into temporary space, since mpn_get_str |
---|
75 | clobbers its argument + needs one extra high limb.... */ |
---|
76 | xp = (mp_ptr) TMP_ALLOC ((x_size + 1) * BYTES_PER_MP_LIMB); |
---|
77 | MPN_COPY (xp, x->_mp_d, x_size); |
---|
78 | |
---|
79 | str_size = mpn_get_str (str, base, xp, x_size); |
---|
80 | |
---|
81 | /* mpn_get_str might make some leading zeros. Skip them. */ |
---|
82 | while (*str == 0) |
---|
83 | { |
---|
84 | str_size--; |
---|
85 | str++; |
---|
86 | } |
---|
87 | |
---|
88 | /* Translate to printable chars. */ |
---|
89 | for (i = 0; i < str_size; i++) |
---|
90 | str[i] = num_to_text[str[i]]; |
---|
91 | str[str_size] = 0; |
---|
92 | |
---|
93 | { |
---|
94 | size_t fwret; |
---|
95 | fwret = fwrite ((char *) str, 1, str_size, stream); |
---|
96 | written += fwret; |
---|
97 | } |
---|
98 | |
---|
99 | TMP_FREE (marker); |
---|
100 | return ferror (stream) ? 0 : written; |
---|
101 | } |
---|