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

Annotation of OpenXM_contrib/gmp/mpz/out_str.c, Revision 1.1.1.1

1.1       maekawa     1: /* mpz_out_str(stream, base, integer) -- Output to STREAM the multi prec.
                      2:    integer INTEGER in base BASE.
                      3:
                      4: Copyright (C) 1991, 1993, 1994, 1996 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 Library General Public License as published by
                     10: the Free Software Foundation; either version 2 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 Library General Public
                     16: License for more details.
                     17:
                     18: You should have received a copy of the GNU Library 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: #if __STDC__
                     29: mpz_out_str (FILE *stream, int base, mpz_srcptr x)
                     30: #else
                     31: mpz_out_str (stream, base, x)
                     32:      FILE *stream;
                     33:      int base;
                     34:      mpz_srcptr x;
                     35: #endif
                     36: {
                     37:   mp_ptr xp;
                     38:   mp_size_t x_size = x->_mp_size;
                     39:   unsigned char *str;
                     40:   size_t str_size;
                     41:   size_t i;
                     42:   size_t written;
                     43:   char *num_to_text;
                     44:   TMP_DECL (marker);
                     45:
                     46:   if (stream == 0)
                     47:     stream = stdout;
                     48:
                     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:   if (x_size == 0)
                     62:     {
                     63:       fputc ('0', stream);
                     64:       return ferror (stream) ? 0 : 1;
                     65:     }
                     66:
                     67:   written = 0;
                     68:
                     69:   if (x_size < 0)
                     70:     {
                     71:       fputc ('-', stream);
                     72:       x_size = -x_size;
                     73:       written = 1;
                     74:     }
                     75:
                     76:   TMP_MARK (marker);
                     77:   str_size = ((size_t) (x_size * BITS_PER_MP_LIMB
                     78:                        * __mp_bases[base].chars_per_bit_exactly)) + 3;
                     79:   str = (unsigned char *) TMP_ALLOC (str_size);
                     80:
                     81:   /* Move the number to convert into temporary space, since mpn_get_str
                     82:      clobbers its argument + needs one extra high limb....  */
                     83:   xp = (mp_ptr) TMP_ALLOC ((x_size + 1) * BYTES_PER_MP_LIMB);
                     84:   MPN_COPY (xp, x->_mp_d, x_size);
                     85:
                     86:   str_size = mpn_get_str (str, base, xp, x_size);
                     87:
                     88:   /* mpn_get_str might make some leading zeros.  Skip them.  */
                     89:   while (*str == 0)
                     90:     {
                     91:       str_size--;
                     92:       str++;
                     93:     }
                     94:
                     95:   /* Translate to printable chars.  */
                     96:   for (i = 0; i < str_size; i++)
                     97:     str[i] = num_to_text[str[i]];
                     98:   str[str_size] = 0;
                     99:
                    100:   {
                    101:     size_t fwret;
                    102:     fwret = fwrite ((char *) str, 1, str_size, stream);
                    103:     written += fwret;
                    104:   }
                    105:
                    106:   TMP_FREE (marker);
                    107:   return ferror (stream) ? 0 : written;
                    108: }

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