FIX BUILD (with GHC 6.2.x): System.Directory.Internals is no more
[ghc-hetmet.git] / rts / gmp / mpz / get_str.c
1 /* mpz_get_str (string, base, mp_src) -- Convert the multiple precision
2    number MP_SRC to a string STRING of base BASE.  If STRING is NULL
3    allocate space for the result.  In any case, return a pointer to the
4    result.  If STRING is not NULL, the caller must ensure enough space is
5    available to store the result.
6
7 Copyright (C) 1991, 1993, 1994, 1996 Free Software Foundation, Inc.
8
9 This file is part of the GNU MP Library.
10
11 The GNU MP Library is free software; you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 2.1 of the License, or (at your
14 option) any later version.
15
16 The GNU MP Library is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
19 License for more details.
20
21 You should have received a copy of the GNU Lesser General Public License
22 along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
23 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24 MA 02111-1307, USA. */
25
26 #include "gmp.h"
27 #include "gmp-impl.h"
28
29 char *
30 #if __STDC__
31 mpz_get_str (char *res_str, int base, mpz_srcptr x)
32 #else
33 mpz_get_str (res_str, base, x)
34      char *res_str;
35      int base;
36      mpz_srcptr x;
37 #endif
38 {
39   mp_ptr xp;
40   mp_size_t x_size = x->_mp_size;
41   unsigned char *str;
42   char *return_str;
43   size_t str_size;
44   char *num_to_text;
45   int i;
46   TMP_DECL (marker);
47
48   TMP_MARK (marker);
49   if (base >= 0)
50     {
51       if (base == 0)
52         base = 10;
53       num_to_text = "0123456789abcdefghijklmnopqrstuvwxyz";
54     }
55   else
56     {
57       base = -base;
58       num_to_text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
59     }
60
61   /* We allways allocate space for the string.  If the caller passed a
62      NULL pointer for RES_STR, we allocate permanent space and return
63      a pointer to that to the caller.  */
64   str_size = ((size_t) (ABS (x_size) * BITS_PER_MP_LIMB
65                         * __mp_bases[base].chars_per_bit_exactly)) + 3;
66   if (res_str == 0)
67     {
68       /* We didn't get a string from the user.  Allocate one (and return
69          a pointer to it).  */
70       res_str = (char *) (*_mp_allocate_func) (str_size);
71       /* Make str, the variable used for raw result from mpn_get_str,
72          point to the same string, but just after a possible minus sign.  */
73       str = (unsigned char *) res_str + 1;
74     }
75   else
76     {
77       /* Use TMP_ALLOC to get temporary space, since we need a few extra bytes
78          that we can't expect to caller to supply us with.  */
79       str = (unsigned char *) TMP_ALLOC (str_size);
80     }
81
82   return_str = res_str;
83
84   if (x_size == 0)
85     {
86       res_str[0] = '0';
87       res_str[1] = 0;
88       TMP_FREE (marker);
89       return res_str;
90     }
91   if (x_size < 0)
92     {
93       *res_str++ = '-';
94       x_size = -x_size;
95     }
96
97   /* Move the number to convert into temporary space, since mpn_get_str
98      clobbers its argument + needs one extra high limb....  */
99   xp = (mp_ptr) TMP_ALLOC ((x_size + 1) * BYTES_PER_MP_LIMB);
100   MPN_COPY (xp, x->_mp_d, x_size);
101
102   str_size = mpn_get_str (str, base, xp, x_size);
103
104   /* mpn_get_str might make some leading zeros.  Skip them.  */
105   while (*str == 0)
106     {
107       str_size--;
108       str++;
109     }
110
111   /* Translate result to printable chars and move result to RES_STR.  */
112   for (i = 0; i < str_size; i++)
113     res_str[i] = num_to_text[str[i]];
114   res_str[str_size] = 0;
115
116   TMP_FREE (marker);
117   return return_str;
118 }