#ident "%W% %G% %U%"
/*
/* Copyright John Moyer 1996
/* permission is granted to use this for any purpose provided proper
/* credit is given to the author
*/

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <math.h>

#include "bignum.h"

int ascii2bcd( char *p, BigNum *b)
{
int i, j, l;

i = 0;
b->isign = 1;
while (p[i] == ' ' || p[i] == '\t' )
  {
  if ( p[i] == '\0' )
    return 1;	/* end of string with no number */
  i++;
  if ( i >= ISIZE )
    return 2;	/* exceeded size */
  }
if ( p[i] == '+' )
  {
  i++;
  }
else if ( p[i] == '-' )
  {
  b->isign = -1;
  i++;
  }

l = strlen(&p[i]); /* length of string */

for( j = 0 ; p[i] != '\0' && p[i] != '\n' ; i++ )
  {
  if ( p[i] < '0' || p[i] > '9' )
    {
#ifdef DEBUG
    fprintf(stderr,"p=%s,p[%d]=%c\n",p,i,p[i]);
#endif
    return 3;	/* not a digit */
    }
  b->x[l-j-1] = p[i] - '0';
  j++;
  if ( j >= ISIZE )
    {
    b->c = ISIZE ;
    return 4;	/* too many digits */
    }
  }
b->c = j;
if ( j == 0 )
  b->isign = 0;
for ( ; j < ISIZE ; j++ )
  b->x[j] = 0;

return 0;
}


