~ubuntu-branches/ubuntu/quantal/gclcvs/quantal

« back to all changes in this revision

Viewing changes to gmp3/mpbsd/min.c

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2004-06-24 15:13:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040624151346-xh0xaaktyyp7aorc
Tags: 2.7.0-26
C_GC_OFFSET is 2 on m68k-linux

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* min(MINT) -- Do decimal input from standard input and store result in
 
2
   MINT.
 
3
 
 
4
Copyright 1991, 1994, 1996, 2000, 2001 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 Lesser General Public License as published by
 
10
the Free Software Foundation; either version 2.1 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 Lesser General Public
 
16
License for more details.
 
17
 
 
18
You should have received a copy of the GNU Lesser 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 <ctype.h>
 
25
#include "mp.h"
 
26
#include "gmp.h"
 
27
#include "gmp-impl.h"
 
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
}
 
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;
 
58
  str = (char *) (*__gmp_allocate_func) (alloc_size);
 
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;
 
83
          str = (char *) (*__gmp_reallocate_func) (str, old_alloc_size, alloc_size);
 
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
 
 
101
  (*__gmp_free_func) (str, alloc_size);
 
102
  return;
 
103
}