Annotation of OpenXM_contrib/gmp/mpbsd/min.c, Revision 1.1.1.3
1.1 maekawa 1: /* min(MINT) -- Do decimal input from standard input and store result in
2: MINT.
3:
1.1.1.3 ! ohara 4: Copyright 1991, 1994, 1996, 2000, 2001 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: digit_value_in_base (int c, int base)
31: {
32: int digit;
33:
34: if (isdigit (c))
35: digit = c - '0';
36: else if (islower (c))
37: digit = c - 'a' + 10;
38: else if (isupper (c))
39: digit = c - 'A' + 10;
40: else
41: return -1;
42:
43: if (digit < base)
44: return digit;
45: return -1;
46: }
1.1 maekawa 47:
48: void
49: min (MINT *dest)
50: {
51: char *str;
52: size_t alloc_size, str_size;
53: int c;
54: int negative;
55: mp_size_t dest_size;
56:
57: alloc_size = 100;
1.1.1.3 ! ohara 58: str = (char *) (*__gmp_allocate_func) (alloc_size);
1.1 maekawa 59: str_size = 0;
60:
61: /* Skip whitespace. */
62: do
63: c = getc (stdin);
64: while (isspace (c));
65:
66: negative = 0;
67: if (c == '-')
68: {
69: negative = 1;
70: c = getc (stdin);
71: }
72:
73: if (digit_value_in_base (c, 10) < 0)
74: return; /* error if no digits */
75:
76: for (;;)
77: {
78: int dig;
79: if (str_size >= alloc_size)
80: {
81: size_t old_alloc_size = alloc_size;
82: alloc_size = alloc_size * 3 / 2;
1.1.1.3 ! ohara 83: str = (char *) (*__gmp_reallocate_func) (str, old_alloc_size, alloc_size);
1.1 maekawa 84: }
85: dig = digit_value_in_base (c, 10);
86: if (dig < 0)
87: break;
88: str[str_size++] = dig;
89: c = getc (stdin);
90: }
91:
92: ungetc (c, stdin);
93:
94: dest_size = str_size / __mp_bases[10].chars_per_limb + 1;
95: if (dest->_mp_alloc < dest_size)
96: _mp_realloc (dest, dest_size);
97:
98: dest_size = mpn_set_str (dest->_mp_d, (unsigned char *) str, str_size, 10);
99: dest->_mp_size = negative ? -dest_size : dest_size;
100:
1.1.1.3 ! ohara 101: (*__gmp_free_func) (str, alloc_size);
1.1 maekawa 102: return;
103: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>