[BACK]Return to get_str.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpz

Annotation of OpenXM_contrib/gmp/mpz/get_str.c, Revision 1.1.1.3

1.1       maekawa     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:
1.1.1.3 ! ohara       7: Copyright 1991, 1993, 1994, 1996, 2000, 2001, 2002 Free Software Foundation,
        !             8: Inc.
1.1       maekawa     9:
                     10: This file is part of the GNU MP Library.
                     11:
                     12: The GNU MP Library is free software; you can redistribute it and/or modify
1.1.1.2   maekawa    13: it under the terms of the GNU Lesser General Public License as published by
                     14: the Free Software Foundation; either version 2.1 of the License, or (at your
1.1       maekawa    15: option) any later version.
                     16:
                     17: The GNU MP Library is distributed in the hope that it will be useful, but
                     18: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1.1.1.2   maekawa    19: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1.1       maekawa    20: License for more details.
                     21:
1.1.1.2   maekawa    22: You should have received a copy of the GNU Lesser General Public License
1.1       maekawa    23: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
                     24: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                     25: MA 02111-1307, USA. */
                     26:
1.1.1.3 ! ohara      27: #include <string.h> /* for strlen */
1.1       maekawa    28: #include "gmp.h"
                     29: #include "gmp-impl.h"
1.1.1.3 ! ohara      30: #include "longlong.h"
1.1       maekawa    31:
                     32: char *
                     33: mpz_get_str (char *res_str, int base, mpz_srcptr x)
                     34: {
                     35:   mp_ptr xp;
                     36:   mp_size_t x_size = x->_mp_size;
1.1.1.3 ! ohara      37:   char *str;
1.1       maekawa    38:   char *return_str;
                     39:   size_t str_size;
1.1.1.3 ! ohara      40:   size_t alloc_size = 0;
1.1       maekawa    41:   char *num_to_text;
                     42:   int i;
                     43:   TMP_DECL (marker);
                     44:
                     45:   if (base >= 0)
                     46:     {
                     47:       if (base == 0)
                     48:        base = 10;
                     49:       num_to_text = "0123456789abcdefghijklmnopqrstuvwxyz";
                     50:     }
                     51:   else
                     52:     {
                     53:       base = -base;
                     54:       num_to_text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                     55:     }
                     56:
1.1.1.3 ! ohara      57:   /* allocate string for the user if necessary */
        !            58:   if (res_str == NULL)
1.1       maekawa    59:     {
1.1.1.3 ! ohara      60:       /* digits, null terminator, possible minus sign */
        !            61:       MPN_SIZEINBASE (alloc_size, PTR(x), ABS(x_size), base);
        !            62:       alloc_size += 1 + (x_size<0);
        !            63:       res_str = (char *) (*__gmp_allocate_func) (alloc_size);
1.1       maekawa    64:     }
                     65:   return_str = res_str;
                     66:
                     67:   if (x_size < 0)
                     68:     {
                     69:       *res_str++ = '-';
                     70:       x_size = -x_size;
                     71:     }
                     72:
1.1.1.3 ! ohara      73:   /* mpn_get_str clobbers its input on non power-of-2 bases */
        !            74:   TMP_MARK (marker);
        !            75:   xp = x->_mp_d;
        !            76:   if (! POW2_P (base))
        !            77:     {
        !            78:       xp = TMP_ALLOC_LIMBS (x_size + 1);  /* +1 in case x_size==0 */
        !            79:       MPN_COPY (xp, x->_mp_d, x_size);
        !            80:     }
1.1       maekawa    81:
1.1.1.3 ! ohara      82:   str_size = mpn_get_str ((unsigned char *) res_str, base, xp, x_size);
        !            83:   ASSERT (alloc_size == 0 || str_size <= alloc_size - (SIZ(x) < 0));
1.1       maekawa    84:
1.1.1.3 ! ohara      85:   /* might have a leading zero, skip it */
        !            86:   str = res_str;
        !            87:   if (*res_str == 0 && str_size != 1)
1.1       maekawa    88:     {
                     89:       str_size--;
                     90:       str++;
1.1.1.3 ! ohara      91:       ASSERT (*str != 0);  /* at most one leading zero */
1.1       maekawa    92:     }
                     93:
1.1.1.3 ! ohara      94:   /* Convert result to printable chars, and move down if there was a leading
        !            95:      zero.  */
1.1       maekawa    96:   for (i = 0; i < str_size; i++)
                     97:     res_str[i] = num_to_text[str[i]];
                     98:   res_str[str_size] = 0;
                     99:
                    100:   TMP_FREE (marker);
1.1.1.3 ! ohara     101:
        !           102:   /* if allocated then resize down to the actual space required */
        !           103:   if (alloc_size != 0)
        !           104:     {
        !           105:       size_t  actual_size = str_size + 1 + (res_str - return_str);
        !           106:       ASSERT (actual_size == strlen (return_str) + 1);
        !           107:       __GMP_REALLOCATE_FUNC_MAYBE_TYPE (return_str, alloc_size, actual_size,
        !           108:                                         char);
        !           109:     }
1.1       maekawa   110:   return return_str;
                    111: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>