Annotation of OpenXM_contrib/gmp/mpq/get_str.c, Revision 1.1
1.1 ! ohara 1: /* mpq_get_str -- mpq to string conversion.
! 2:
! 3: Copyright 2001, 2002 Free Software Foundation, Inc.
! 4:
! 5: This file is part of the GNU MP Library.
! 6:
! 7: The GNU MP Library is free software; you can redistribute it and/or modify
! 8: it under the terms of the GNU Lesser General Public License as published by
! 9: the Free Software Foundation; either version 2.1 of the License, or (at your
! 10: option) any later version.
! 11:
! 12: The GNU MP Library is distributed in the hope that it will be useful, but
! 13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
! 14: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
! 15: License for more details.
! 16:
! 17: You should have received a copy of the GNU Lesser General Public License
! 18: along with the GNU MP Library; see the file COPYING.LIB. If not, write to
! 19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
! 20: MA 02111-1307, USA. */
! 21:
! 22: #include <stdio.h>
! 23: #include <string.h>
! 24: #include "gmp.h"
! 25: #include "gmp-impl.h"
! 26:
! 27: char *
! 28: mpq_get_str (char *str, int base, mpq_srcptr q)
! 29: {
! 30: size_t str_alloc, len;
! 31:
! 32: ASSERT (ABS(base) >= 2);
! 33: ASSERT (ABS(base) <= 36);
! 34:
! 35: str_alloc = 0;
! 36: if (str == NULL)
! 37: {
! 38: /* This is an overestimate since we don't bother checking how much of
! 39: the high limbs of num and den are used. +2 for rounding up the
! 40: chars per bit of num and den. +3 for sign, slash and '\0'. */
! 41: str_alloc = ((size_t) ((ABS (q->_mp_num._mp_size) + q->_mp_den._mp_size)
! 42: * BITS_PER_MP_LIMB
! 43: * __mp_bases[ABS(base)].chars_per_bit_exactly))
! 44: + 5;
! 45: str = (char *) (*__gmp_allocate_func) (str_alloc);
! 46: }
! 47:
! 48: mpz_get_str (str, base, mpq_numref(q));
! 49: len = strlen (str);
! 50: if (! MPZ_EQUAL_1_P (mpq_denref (q)))
! 51: {
! 52: str[len++] = '/';
! 53: mpz_get_str (str+len, base, mpq_denref(q));
! 54: len += strlen (str+len);
! 55: }
! 56:
! 57: ASSERT (len == strlen(str));
! 58: ASSERT (str_alloc == 0 || len+1 <= str_alloc);
! 59: ASSERT (len+1 <= /* size recommended to applications */
! 60: mpz_sizeinbase (mpq_numref(q), ABS(base)) +
! 61: mpz_sizeinbase (mpq_denref(q), ABS(base)) + 3);
! 62:
! 63: if (str_alloc != 0)
! 64: __GMP_REALLOCATE_FUNC_MAYBE_TYPE (str, str_alloc, len+1, char);
! 65:
! 66: return str;
! 67: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>