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

Annotation of OpenXM_contrib/gmp/mpbsd/min.c, Revision 1.1.1.2

1.1       maekawa     1: /* min(MINT) -- Do decimal input from standard input and store result in
                      2:    MINT.
                      3:
1.1.1.2 ! maekawa     4: Copyright (C) 1991, 1994, 1996, 2000 Free Software Foundation, Inc.
1.1       maekawa     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
1.1.1.2 ! maekawa     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
1.1       maekawa    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
1.1.1.2 ! maekawa    15: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1.1       maekawa    16: License for more details.
                     17:
1.1.1.2 ! maekawa    18: You should have received a copy of the GNU Lesser General Public License
1.1       maekawa    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 <ctype.h>
                     25: #include "mp.h"
                     26: #include "gmp.h"
                     27: #include "gmp-impl.h"
1.1.1.2 ! maekawa    28:
        !            29: static int
        !            30: #if __STDC__
        !            31: digit_value_in_base (int c, int base)
        !            32: #else
        !            33: digit_value_in_base (c, base)
        !            34:      int c;
        !            35:      int base;
        !            36: #endif
        !            37: {
        !            38:   int digit;
        !            39:
        !            40:   if (isdigit (c))
        !            41:     digit = c - '0';
        !            42:   else if (islower (c))
        !            43:     digit = c - 'a' + 10;
        !            44:   else if (isupper (c))
        !            45:     digit = c - 'A' + 10;
        !            46:   else
        !            47:     return -1;
        !            48:
        !            49:   if (digit < base)
        !            50:     return digit;
        !            51:   return -1;
        !            52: }
1.1       maekawa    53:
                     54: void
                     55: #if __STDC__
                     56: min (MINT *dest)
                     57: #else
                     58: min (dest)
                     59:      MINT *dest;
                     60: #endif
                     61: {
                     62:   char *str;
                     63:   size_t alloc_size, str_size;
                     64:   int c;
                     65:   int negative;
                     66:   mp_size_t dest_size;
                     67:
                     68:   alloc_size = 100;
                     69:   str = (char *) (*_mp_allocate_func) (alloc_size);
                     70:   str_size = 0;
                     71:
                     72:   /* Skip whitespace.  */
                     73:   do
                     74:     c = getc (stdin);
                     75:   while (isspace (c));
                     76:
                     77:   negative = 0;
                     78:   if (c == '-')
                     79:     {
                     80:       negative = 1;
                     81:       c = getc (stdin);
                     82:     }
                     83:
                     84:   if (digit_value_in_base (c, 10) < 0)
                     85:     return;                    /* error if no digits */
                     86:
                     87:   for (;;)
                     88:     {
                     89:       int dig;
                     90:       if (str_size >= alloc_size)
                     91:        {
                     92:          size_t old_alloc_size = alloc_size;
                     93:          alloc_size = alloc_size * 3 / 2;
                     94:          str = (char *) (*_mp_reallocate_func) (str, old_alloc_size, alloc_size);
                     95:        }
                     96:       dig = digit_value_in_base (c, 10);
                     97:       if (dig < 0)
                     98:        break;
                     99:       str[str_size++] = dig;
                    100:       c = getc (stdin);
                    101:     }
                    102:
                    103:   ungetc (c, stdin);
                    104:
                    105:   dest_size = str_size / __mp_bases[10].chars_per_limb + 1;
                    106:   if (dest->_mp_alloc < dest_size)
                    107:     _mp_realloc (dest, dest_size);
                    108:
                    109:   dest_size = mpn_set_str (dest->_mp_d, (unsigned char *) str, str_size, 10);
                    110:   dest->_mp_size = negative ? -dest_size : dest_size;
                    111:
                    112:   (*_mp_free_func) (str, alloc_size);
                    113:   return;
                    114: }

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